blob: 9885dfe1f7b76f1f7eaaf66af33f6077a0f15919 [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. */
1019 if ((part == PART_IN || channel->ch_part[PART_IN].ch_fd != *fd)
1020 && (part == PART_OUT
1021 || channel->ch_part[PART_OUT].ch_fd != *fd)
1022 && (part == PART_ERR
1023 || channel->ch_part[PART_ERR].ch_fd != *fd))
1024 fd_close(*fd);
1025 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001026 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001027
1028 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001029 }
1030}
1031
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001032 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001033channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001034{
Bram Moolenaarde279892016-03-11 22:19:44 +01001035 if (in != INVALID_FD)
1036 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001037 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001038 channel->CH_IN_FD = in;
1039 }
1040 if (out != INVALID_FD)
1041 {
1042# if defined(FEAT_GUI)
1043 channel_gui_unregister_one(channel, PART_OUT);
1044# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001045 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001046 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001047 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001048# if defined(FEAT_GUI)
1049 channel_gui_register_one(channel, PART_OUT);
1050# endif
1051 }
1052 if (err != INVALID_FD)
1053 {
1054# if defined(FEAT_GUI)
1055 channel_gui_unregister_one(channel, PART_ERR);
1056# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001057 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001058 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001059 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001060# if defined(FEAT_GUI)
1061 channel_gui_register_one(channel, PART_ERR);
1062# endif
1063 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001064}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001065
Bram Moolenaard6051b52016-02-28 15:49:03 +01001066/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001067 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001068 * This does not keep a refcount, when the job is freed ch_job is cleared.
1069 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001070 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001071channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001072{
Bram Moolenaar77073442016-02-13 23:23:53 +01001073 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001074
1075 channel_set_options(channel, options);
1076
1077 if (job->jv_in_buf != NULL)
1078 {
1079 chanpart_T *in_part = &channel->ch_part[PART_IN];
1080
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001081 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001082 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001083 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001084 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001085 {
1086 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1087 {
1088 /* Special mode: send last-but-one line when appending a line
1089 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001090 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001091 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001092 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001093 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001094 }
1095 else
1096 in_part->ch_buf_top = options->jo_in_top;
1097 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001098 else
1099 in_part->ch_buf_top = 1;
1100 if (options->jo_set & JO_IN_BOT)
1101 in_part->ch_buf_bot = options->jo_in_bot;
1102 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001103 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001104 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001105}
1106
1107/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001108 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001109 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001110 */
1111 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001112find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001113{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001114 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001115 buf_T *save_curbuf = curbuf;
1116
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001117 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001118 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001119 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001120 if (buf == NULL)
1121 buf = buflist_findname_exp(name);
1122 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001123 if (buf == NULL)
1124 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001125 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001126 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001127 if (buf == NULL)
1128 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001129 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001130 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001131#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001132 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1133 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001134#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001135 if (curbuf->b_ml.ml_mfp == NULL)
1136 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001137 if (msg)
1138 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001139 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001140 changed_bytes(1, 0);
1141 curbuf = save_curbuf;
1142 }
1143
1144 return buf;
1145}
1146
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001147 static void
1148set_callback(
1149 char_u **cbp,
1150 partial_T **pp,
1151 char_u *callback,
1152 partial_T *partial)
1153{
1154 free_callback(*cbp, *pp);
1155 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001156 {
1157 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001158 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001159 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001160 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001161 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001162 func_ref(*cbp);
1163 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001164 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001165 else
1166 *cbp = NULL;
1167 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001168 if (partial != NULL)
1169 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001170}
1171
Bram Moolenaar187db502016-02-27 14:44:26 +01001172/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001173 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001174 */
1175 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001176channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001177{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001178 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001179
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001180 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001181 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001182 channel->ch_part[part].ch_mode = opt->jo_mode;
1183 if (opt->jo_set & JO_IN_MODE)
1184 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1185 if (opt->jo_set & JO_OUT_MODE)
1186 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1187 if (opt->jo_set & JO_ERR_MODE)
1188 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001189
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001190 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001191 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001192 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1193 if (opt->jo_set & JO_OUT_TIMEOUT)
1194 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1195 if (opt->jo_set & JO_ERR_TIMEOUT)
1196 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001197 if (opt->jo_set & JO_BLOCK_WRITE)
1198 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001199
1200 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001201 set_callback(&channel->ch_callback, &channel->ch_partial,
1202 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001203 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001204 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1205 &channel->ch_part[PART_OUT].ch_partial,
1206 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001207 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001208 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1209 &channel->ch_part[PART_ERR].ch_partial,
1210 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001211 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001212 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1213 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001214 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001215
1216 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1217 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001218 buf_T *buf;
1219
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001220 /* writing output to a buffer. Default mode is NL. */
1221 if (!(opt->jo_set & JO_OUT_MODE))
1222 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001223 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001224 {
1225 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1226 if (buf == NULL)
1227 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1228 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001229 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001231 int msg = TRUE;
1232
1233 if (opt->jo_set2 & JO2_OUT_MSG)
1234 msg = opt->jo_message[PART_OUT];
1235 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001236 }
1237 if (buf != NULL)
1238 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001239 if (opt->jo_set & JO_OUT_MODIFIABLE)
1240 channel->ch_part[PART_OUT].ch_nomodifiable =
1241 !opt->jo_modifiable[PART_OUT];
1242
1243 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1244 {
1245 EMSG(_(e_modifiable));
1246 }
1247 else
1248 {
1249 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001250 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001251 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001252 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001253 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001254 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001255
1256 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1257 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1258 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1259 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001260 buf_T *buf;
1261
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001262 /* writing err to a buffer. Default mode is NL. */
1263 if (!(opt->jo_set & JO_ERR_MODE))
1264 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1265 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001266 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001267 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001268 {
1269 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1270 if (buf == NULL)
1271 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1272 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001273 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001274 {
1275 int msg = TRUE;
1276
1277 if (opt->jo_set2 & JO2_ERR_MSG)
1278 msg = opt->jo_message[PART_ERR];
1279 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1280 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001281 if (buf != NULL)
1282 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001283 if (opt->jo_set & JO_ERR_MODIFIABLE)
1284 channel->ch_part[PART_ERR].ch_nomodifiable =
1285 !opt->jo_modifiable[PART_ERR];
1286 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1287 {
1288 EMSG(_(e_modifiable));
1289 }
1290 else
1291 {
1292 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001293 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001294 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001295 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001296 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001297 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001298
1299 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1300 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1301 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001302}
1303
1304/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001305 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001306 */
1307 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001308channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001309 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001310 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001311 char_u *callback,
1312 partial_T *partial,
1313 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001314{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001315 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001316 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1317
1318 if (item != NULL)
1319 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001320 item->cq_partial = partial;
1321 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001322 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001323 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001324 item->cq_callback = callback;
1325 }
1326 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001327 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001328 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001329 func_ref(item->cq_callback);
1330 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001331 item->cq_seq_nr = id;
1332 item->cq_prev = head->cq_prev;
1333 head->cq_prev = item;
1334 item->cq_next = NULL;
1335 if (item->cq_prev == NULL)
1336 head->cq_next = item;
1337 else
1338 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001339 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001340}
1341
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001342 static void
1343write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1344{
1345 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001346 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001347 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001348 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001349
Bram Moolenaar655da312016-05-28 22:22:34 +02001350 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001351 if ((p = alloc(len + 2)) == NULL)
1352 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001353 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001354
1355 for (i = 0; i < len; ++i)
1356 if (p[i] == NL)
1357 p[i] = NUL;
1358
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001359 p[len] = NL;
1360 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001361 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001362 vim_free(p);
1363}
1364
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001365/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001366 * Return TRUE if "channel" can be written to.
1367 * Returns FALSE if the input is closed or the write would block.
1368 */
1369 static int
1370can_write_buf_line(channel_T *channel)
1371{
1372 chanpart_T *in_part = &channel->ch_part[PART_IN];
1373
1374 if (in_part->ch_fd == INVALID_FD)
1375 return FALSE; /* pipe was closed */
1376
1377 /* for testing: block every other attempt to write */
1378 if (in_part->ch_block_write == 1)
1379 in_part->ch_block_write = -1;
1380 else if (in_part->ch_block_write == -1)
1381 in_part->ch_block_write = 1;
1382
1383 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1384#ifndef WIN32
1385 {
1386# if defined(HAVE_SELECT)
1387 struct timeval tval;
1388 fd_set wfds;
1389 int ret;
1390
1391 FD_ZERO(&wfds);
1392 FD_SET((int)in_part->ch_fd, &wfds);
1393 tval.tv_sec = 0;
1394 tval.tv_usec = 0;
1395 for (;;)
1396 {
1397 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1398# ifdef EINTR
1399 SOCK_ERRNO;
1400 if (ret == -1 && errno == EINTR)
1401 continue;
1402# endif
1403 if (ret <= 0 || in_part->ch_block_write == 1)
1404 {
1405 if (ret > 0)
1406 ch_log(channel, "FAKED Input not ready for writing");
1407 else
1408 ch_log(channel, "Input not ready for writing");
1409 return FALSE;
1410 }
1411 break;
1412 }
1413# else
1414 struct pollfd fds;
1415
1416 fds.fd = in_part->ch_fd;
1417 fds.events = POLLOUT;
1418 if (poll(&fds, 1, 0) <= 0)
1419 {
1420 ch_log(channel, "Input not ready for writing");
1421 return FALSE;
1422 }
1423 if (in_part->ch_block_write == 1)
1424 {
1425 ch_log(channel, "FAKED Input not ready for writing");
1426 return FALSE;
1427 }
1428# endif
1429 }
1430#endif
1431 return TRUE;
1432}
1433
1434/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001435 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001436 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001437 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001438channel_write_in(channel_T *channel)
1439{
1440 chanpart_T *in_part = &channel->ch_part[PART_IN];
1441 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001442 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001443 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001444
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001445 if (buf == NULL || in_part->ch_buf_append)
1446 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001447 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001448 {
1449 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001450 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001451 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001452 return;
1453 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001454
1455 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1456 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1457 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001458 if (!can_write_buf_line(channel))
1459 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001460 write_buf_line(buf, lnum, channel);
1461 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001462 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001463
1464 if (written == 1)
1465 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1466 else if (written > 1)
1467 ch_logn(channel, "written %d lines to channel", written);
1468
Bram Moolenaar014069a2016-03-03 22:51:40 +01001469 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001470 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001471 {
1472 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001473 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001474 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001475
1476 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001477 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001478 }
1479 else
1480 ch_logn(channel, "Still %d more lines to write",
1481 buf->b_ml.ml_line_count - lnum + 1);
1482}
1483
1484/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001485 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001486 */
1487 void
1488channel_buffer_free(buf_T *buf)
1489{
1490 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001491 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001492
1493 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001494 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001495 {
1496 chanpart_T *ch_part = &channel->ch_part[part];
1497
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001498 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001499 {
1500 ch_logs(channel, "%s buffer has been wiped out",
1501 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001502 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001503 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001504 }
1505}
1506
1507/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001508 * Write any lines waiting to be written to a channel.
1509 */
1510 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001511channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001512{
1513 channel_T *channel;
1514
1515 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1516 {
1517 chanpart_T *in_part = &channel->ch_part[PART_IN];
1518
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001519 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001520 {
1521 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001522 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001523 else
1524 channel_write_in(channel);
1525 }
1526 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001527}
1528
1529/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530 * Write appended lines above the last one in "buf" to the channel.
1531 */
1532 void
1533channel_write_new_lines(buf_T *buf)
1534{
1535 channel_T *channel;
1536 int found_one = FALSE;
1537
1538 /* There could be more than one channel for the buffer, loop over all of
1539 * them. */
1540 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1541 {
1542 chanpart_T *in_part = &channel->ch_part[PART_IN];
1543 linenr_T lnum;
1544 int written = 0;
1545
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001546 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001547 {
1548 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001549 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001550 found_one = TRUE;
1551 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1552 ++lnum)
1553 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001554 if (!can_write_buf_line(channel))
1555 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001556 write_buf_line(buf, lnum, channel);
1557 ++written;
1558 }
1559
1560 if (written == 1)
1561 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1562 else if (written > 1)
1563 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001564 if (lnum < buf->b_ml.ml_line_count)
1565 ch_logn(channel, "Still %d more lines to write",
1566 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001567
1568 in_part->ch_buf_bot = lnum;
1569 }
1570 }
1571 if (!found_one)
1572 buf->b_write_to_channel = FALSE;
1573}
1574
1575/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001576 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001577 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001578 */
1579 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001580invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1581 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001582{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001583 typval_T rettv;
1584 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001585
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001586 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001587 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001588
Bram Moolenaar77073442016-02-13 23:23:53 +01001589 argv[0].v_type = VAR_CHANNEL;
1590 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001591
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001592 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1593 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001594 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001595 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001596}
1597
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001598/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001599 * Return the first node from "channel"/"part" without removing it.
1600 * Returns NULL if there is nothing.
1601 */
1602 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001603channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001604{
1605 readq_T *head = &channel->ch_part[part].ch_head;
1606
1607 return head->rq_next;
1608}
1609
1610/*
1611 * Return a pointer to the first NL in "node".
1612 * Skips over NUL characters.
1613 * Returns NULL if there is no NL.
1614 */
1615 char_u *
1616channel_first_nl(readq_T *node)
1617{
1618 char_u *buffer = node->rq_buffer;
1619 long_u i;
1620
1621 for (i = 0; i < node->rq_buflen; ++i)
1622 if (buffer[i] == NL)
1623 return buffer + i;
1624 return NULL;
1625}
1626
1627/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001628 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001629 * The caller must free it.
1630 * Returns NULL if there is nothing.
1631 */
1632 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001633channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001634{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001635 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001636 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001637 char_u *p;
1638
Bram Moolenaar77073442016-02-13 23:23:53 +01001639 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001640 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001641 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001642 p = node->rq_buffer;
1643 head->rq_next = node->rq_next;
1644 if (node->rq_next == NULL)
1645 head->rq_prev = NULL;
1646 else
1647 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001648 vim_free(node);
1649 return p;
1650}
1651
1652/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001653 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001654 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001655 */
1656 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001657channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001658{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001659 readq_T *head = &channel->ch_part[part].ch_head;
1660 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001661 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001662 char_u *res;
1663 char_u *p;
1664
1665 /* If there is only one buffer just get that one. */
1666 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1667 return channel_get(channel, part);
1668
1669 /* Concatenate everything into one buffer. */
1670 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001671 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001672 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001673 if (res == NULL)
1674 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001675 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001676 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001677 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001678 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001679 p += node->rq_buflen;
1680 }
1681 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001682
1683 /* Free all buffers */
1684 do
1685 {
1686 p = channel_get(channel, part);
1687 vim_free(p);
1688 } while (p != NULL);
1689
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001690 /* turn all NUL into NL */
1691 while (len > 0)
1692 {
1693 --len;
1694 if (res[len] == NUL)
1695 res[len] = NL;
1696 }
1697
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001698 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001699}
1700
1701/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001702 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001703 * Caller must check these bytes are available.
1704 */
1705 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001706channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001707{
1708 readq_T *head = &channel->ch_part[part].ch_head;
1709 readq_T *node = head->rq_next;
1710 char_u *buf = node->rq_buffer;
1711
1712 mch_memmove(buf, buf + len, node->rq_buflen - len);
1713 node->rq_buflen -= len;
1714}
1715
1716/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001717 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001718 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001719 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001720 */
1721 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001722channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001723{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001724 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001725 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001726 readq_T *last_node;
1727 readq_T *n;
1728 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001730 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001731
Bram Moolenaar77073442016-02-13 23:23:53 +01001732 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001733 return FAIL;
1734
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001735 last_node = node->rq_next;
1736 len = node->rq_buflen + last_node->rq_buflen + 1;
1737 if (want_nl)
1738 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001739 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001740 {
1741 last_node = last_node->rq_next;
1742 len += last_node->rq_buflen;
1743 }
1744
1745 p = newbuf = alloc(len);
1746 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001747 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001748 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001749 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001750 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001751 node->rq_buffer = newbuf;
1752 for (n = node; n != last_node; )
1753 {
1754 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001755 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001756 p += n->rq_buflen;
1757 vim_free(n->rq_buffer);
1758 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001759 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001760
1761 /* dispose of the collapsed nodes and their buffers */
1762 for (n = node->rq_next; n != last_node; )
1763 {
1764 n = n->rq_next;
1765 vim_free(n->rq_prev);
1766 }
1767 node->rq_next = last_node->rq_next;
1768 if (last_node->rq_next == NULL)
1769 head->rq_prev = node;
1770 else
1771 last_node->rq_next->rq_prev = node;
1772 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001773 return OK;
1774}
1775
1776/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001777 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001778 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001779 * Returns OK or FAIL.
1780 */
1781 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001782channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001783 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001784{
1785 readq_T *node;
1786 readq_T *head = &channel->ch_part[part].ch_head;
1787 char_u *p;
1788 int i;
1789
1790 node = (readq_T *)alloc(sizeof(readq_T));
1791 if (node == NULL)
1792 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001793 /* A NUL is added at the end, because netbeans code expects that.
1794 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001795 node->rq_buffer = alloc(len + 1);
1796 if (node->rq_buffer == NULL)
1797 {
1798 vim_free(node);
1799 return FAIL; /* out of memory */
1800 }
1801
1802 if (channel->ch_part[part].ch_mode == MODE_NL)
1803 {
1804 /* Drop any CR before a NL. */
1805 p = node->rq_buffer;
1806 for (i = 0; i < len; ++i)
1807 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1808 *p++ = buf[i];
1809 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001810 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001811 }
1812 else
1813 {
1814 mch_memmove(node->rq_buffer, buf, len);
1815 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001816 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001817 }
1818
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001819 if (prepend)
1820 {
1821 /* preend node to the head of the queue */
1822 node->rq_next = head->rq_next;
1823 node->rq_prev = NULL;
1824 if (head->rq_next == NULL)
1825 head->rq_prev = node;
1826 else
1827 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001828 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001829 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001830 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001831 {
1832 /* append node to the tail of the queue */
1833 node->rq_next = NULL;
1834 node->rq_prev = head->rq_prev;
1835 if (head->rq_prev == NULL)
1836 head->rq_next = node;
1837 else
1838 head->rq_prev->rq_next = node;
1839 head->rq_prev = node;
1840 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001841
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001842 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001844 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001845 fprintf(log_fd, "'");
1846 if (fwrite(buf, len, 1, log_fd) != 1)
1847 return FAIL;
1848 fprintf(log_fd, "'\n");
1849 }
1850 return OK;
1851}
1852
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001853/*
1854 * Try to fill the buffer of "reader".
1855 * Returns FALSE when nothing was added.
1856 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 static int
1858channel_fill(js_read_T *reader)
1859{
1860 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001861 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001863 int keeplen;
1864 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001865 char_u *p;
1866
1867 if (next == NULL)
1868 return FALSE;
1869
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001870 keeplen = reader->js_end - reader->js_buf;
1871 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001872 {
1873 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001874 addlen = (int)STRLEN(next);
1875 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001876 if (p == NULL)
1877 {
1878 vim_free(next);
1879 return FALSE;
1880 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001881 mch_memmove(p, reader->js_buf, keeplen);
1882 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001883 vim_free(next);
1884 next = p;
1885 }
1886
1887 vim_free(reader->js_buf);
1888 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001889 return TRUE;
1890}
1891
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001892/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001893 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001895 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001896 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001897 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001898channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001899{
1900 js_read_T reader;
1901 typval_T listtv;
1902 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001903 chanpart_T *chanpart = &channel->ch_part[part];
1904 jsonq_T *head = &chanpart->ch_json_head;
1905 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001906 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001907
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001908 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001909 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001910
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001911 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001912 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001913 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001914 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001915 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001916
1917 /* When a message is incomplete we wait for a short while for more to
1918 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001919 * or list will make us hang.
1920 * Do not generate error messages, they will be written in a channel log. */
1921 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001922 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001923 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001924 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001925 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001926 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001927 /* Only accept the response when it is a list with at least two
1928 * items. */
1929 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001930 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001931 if (listtv.v_type != VAR_LIST)
1932 ch_error(channel, "Did not receive a list, discarding");
1933 else
1934 ch_errorn(channel, "Expected list with two items, got %d",
1935 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001936 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001937 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001938 else
1939 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001940 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1941 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001942 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001943 else
1944 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001945 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001946 item->jq_value = alloc_tv();
1947 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001948 {
1949 vim_free(item);
1950 clear_tv(&listtv);
1951 }
1952 else
1953 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001954 *item->jq_value = listtv;
1955 item->jq_prev = head->jq_prev;
1956 head->jq_prev = item;
1957 item->jq_next = NULL;
1958 if (item->jq_prev == NULL)
1959 head->jq_next = item;
1960 else
1961 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001962 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001963 }
1964 }
1965 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001966
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001967 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001968 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001970 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001971 size_t buflen = STRLEN(reader.js_buf);
1972
1973 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001974 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001975 /* First time encountering incomplete message or after receiving
1976 * more (but still incomplete): set a deadline of 100 msec. */
1977 ch_logn(channel,
1978 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001979 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001980 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001981 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001982#ifdef WIN32
1983 chanpart->ch_deadline = GetTickCount() + 100L;
1984#else
1985 gettimeofday(&chanpart->ch_deadline, NULL);
1986 chanpart->ch_deadline.tv_usec += 100 * 1000;
1987 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1988 {
1989 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1990 ++chanpart->ch_deadline.tv_sec;
1991 }
1992#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001993 }
1994 else
1995 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001996 int timeout;
1997#ifdef WIN32
1998 timeout = GetTickCount() > chanpart->ch_deadline;
1999#else
2000 {
2001 struct timeval now_tv;
2002
2003 gettimeofday(&now_tv, NULL);
2004 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2005 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2006 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2007 }
2008#endif
2009 if (timeout)
2010 {
2011 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002012 chanpart->ch_wait_len = 0;
2013 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002014 }
2015 else
2016 {
2017 reader.js_used = 0;
2018 ch_log(channel, "still waiting on incomplete message");
2019 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002020 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002021 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002022
2023 if (status == FAIL)
2024 {
2025 ch_error(channel, "Decoding failed - discarding input");
2026 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002027 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002028 }
2029 else if (reader.js_buf[reader.js_used] != NUL)
2030 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002031 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002032 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002033 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2034 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002035 ret = status == MAYBE ? FALSE: TRUE;
2036 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002037 else
2038 ret = FALSE;
2039
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002040 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002041 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002042}
2043
2044/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002045 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002046 */
2047 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002048remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002049{
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 if (node->cq_prev == NULL)
2051 head->cq_next = node->cq_next;
2052 else
2053 node->cq_prev->cq_next = node->cq_next;
2054 if (node->cq_next == NULL)
2055 head->cq_prev = node->cq_prev;
2056 else
2057 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002058}
2059
2060/*
2061 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002062 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002063 */
2064 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002065remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066{
Bram Moolenaar77073442016-02-13 23:23:53 +01002067 if (node->jq_prev == NULL)
2068 head->jq_next = node->jq_next;
2069 else
2070 node->jq_prev->jq_next = node->jq_next;
2071 if (node->jq_next == NULL)
2072 head->jq_prev = node->jq_prev;
2073 else
2074 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002075 vim_free(node);
2076}
2077
2078/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002079 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002080 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002081 * When "id" is zero or negative jut get the first message. But not the one
2082 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002083 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002084 * Return OK when found and return the value in "rettv".
2085 * Return FAIL otherwise.
2086 */
2087 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002088channel_get_json(
2089 channel_T *channel,
2090 ch_part_T part,
2091 int id,
2092 int without_callback,
2093 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002094{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002095 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002096 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002097
Bram Moolenaar77073442016-02-13 23:23:53 +01002098 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002099 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002100 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002101 typval_T *tv = &l->lv_first->li_tv;
2102
Bram Moolenaar958dc692016-12-01 15:34:12 +01002103 if ((without_callback || !item->jq_no_callback)
2104 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002105 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002106 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002107 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002108 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002109 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002110 if (tv->v_type == VAR_NUMBER)
2111 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002112 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002113 return OK;
2114 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002115 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002116 }
2117 return FAIL;
2118}
2119
Bram Moolenaar958dc692016-12-01 15:34:12 +01002120/*
2121 * Put back "rettv" into the JSON queue, there was no callback for it.
2122 * Takes over the values in "rettv".
2123 */
2124 static void
2125channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2126{
2127 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2128 jsonq_T *item = head->jq_next;
2129 jsonq_T *newitem;
2130
2131 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2132 /* last item was pushed back, append to the end */
2133 item = NULL;
2134 else while (item != NULL && item->jq_no_callback)
2135 /* append after the last item that was pushed back */
2136 item = item->jq_next;
2137
2138 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2139 if (newitem == NULL)
2140 clear_tv(rettv);
2141 else
2142 {
2143 newitem->jq_value = alloc_tv();
2144 if (newitem->jq_value == NULL)
2145 {
2146 vim_free(newitem);
2147 clear_tv(rettv);
2148 }
2149 else
2150 {
2151 newitem->jq_no_callback = FALSE;
2152 *newitem->jq_value = *rettv;
2153 if (item == NULL)
2154 {
2155 /* append to the end */
2156 newitem->jq_prev = head->jq_prev;
2157 head->jq_prev = newitem;
2158 newitem->jq_next = NULL;
2159 if (newitem->jq_prev == NULL)
2160 head->jq_next = newitem;
2161 else
2162 newitem->jq_prev->jq_next = newitem;
2163 }
2164 else
2165 {
2166 /* append after "item" */
2167 newitem->jq_prev = item;
2168 newitem->jq_next = item->jq_next;
2169 item->jq_next = newitem;
2170 if (newitem->jq_next == NULL)
2171 head->jq_prev = newitem;
2172 else
2173 newitem->jq_next->jq_prev = newitem;
2174 }
2175 }
2176 }
2177}
2178
Bram Moolenaarece61b02016-02-20 21:39:05 +01002179#define CH_JSON_MAX_ARGS 4
2180
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002181/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002182 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002183 * "argv[0]" is the command string.
2184 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002185 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002186 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002187channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002188{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002189 char_u *cmd = argv[0].vval.v_string;
2190 char_u *arg;
2191 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192
Bram Moolenaarece61b02016-02-20 21:39:05 +01002193 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002194 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002195 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002196 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002197 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002198 return;
2199 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002201 if (arg == NULL)
2202 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002204 if (STRCMP(cmd, "ex") == 0)
2205 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002206 int save_called_emsg = called_emsg;
2207
2208 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002209 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002210 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002212 --emsg_silent;
2213 if (called_emsg)
2214 ch_logs(channel, "Ex command error: '%s'",
2215 (char *)get_vim_var_str(VV_ERRMSG));
2216 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002217 }
2218 else if (STRCMP(cmd, "normal") == 0)
2219 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002220 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002221
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002222 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002223 ea.arg = arg;
2224 ea.addr_count = 0;
2225 ea.forceit = TRUE; /* no mapping */
2226 ex_normal(&ea);
2227 }
2228 else if (STRCMP(cmd, "redraw") == 0)
2229 {
2230 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002231
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002232 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002233 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002234 ex_redraw(&ea);
2235 showruler(FALSE);
2236 setcursor();
2237 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002238#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002239 if (gui.in_use)
2240 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002241 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002242 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002243 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002244#endif
2245 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002246 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002247 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002248 int is_call = cmd[0] == 'c';
2249 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002250
Bram Moolenaarece61b02016-02-20 21:39:05 +01002251 if (argv[id_idx].v_type != VAR_UNKNOWN
2252 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002253 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002254 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002255 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002256 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002257 }
2258 else if (is_call && argv[2].v_type != VAR_LIST)
2259 {
2260 ch_error(channel, "third argument for call must be a list");
2261 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002262 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002263 }
2264 else
2265 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002266 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002267 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002268 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002269 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002270
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002271 /* Don't pollute the display with errors. */
2272 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002273 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002274 {
2275 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002276 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002277 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002278 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002279 {
2280 ch_logs(channel, "Calling '%s'", (char *)arg);
2281 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2282 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002283 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002284
2285 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002286 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002287 int id = argv[id_idx].vval.v_number;
2288
Bram Moolenaar55fab432016-02-07 16:53:13 +01002289 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002290 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002291 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002292 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002293 /* If evaluation failed or the result can't be encoded
2294 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002295 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002296 err_tv.v_type = VAR_STRING;
2297 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002298 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002299 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002300 if (json != NULL)
2301 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002302 channel_send(channel,
2303 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002304 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002305 vim_free(json);
2306 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002307 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002308 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002309 if (tv == &res_tv)
2310 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002311 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002312 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002313 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002314 }
2315 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002316 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002317 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002318 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002319 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002320}
2321
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002322/*
2323 * Invoke the callback at "cbhead".
2324 * Does not redraw but sets channel_need_redraw.
2325 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002326 static void
2327invoke_one_time_callback(
2328 channel_T *channel,
2329 cbq_T *cbhead,
2330 cbq_T *item,
2331 typval_T *argv)
2332{
2333 ch_logs(channel, "Invoking one-time callback %s",
2334 (char *)item->cq_callback);
2335 /* Remove the item from the list first, if the callback
2336 * invokes ch_close() the list will be cleared. */
2337 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002338 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002339 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002340 vim_free(item);
2341}
2342
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002343 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002344append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002345{
2346 buf_T *save_curbuf = curbuf;
2347 linenr_T lnum = buffer->b_ml.ml_line_count;
2348 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002349 chanpart_T *ch_part = &channel->ch_part[part];
2350 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002351 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002352
2353 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2354 {
2355 if (!ch_part->ch_nomod_error)
2356 {
2357 ch_error(channel, "Buffer is not modifiable, cannot append");
2358 ch_part->ch_nomod_error = TRUE;
2359 }
2360 return;
2361 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002362
2363 /* If the buffer is also used as input insert above the last
2364 * line. Don't write these lines. */
2365 if (save_write_to)
2366 {
2367 --lnum;
2368 buffer->b_write_to_channel = FALSE;
2369 }
2370
2371 /* Append to the buffer */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002372 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002373
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002374 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002375 curbuf = buffer;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002376 curwin->w_buffer = curbuf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002377 u_sync(TRUE);
2378 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002379 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002380
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002381 if (empty)
2382 {
2383 /* The buffer is empty, replace the first (dummy) line. */
2384 ml_replace(lnum, msg, TRUE);
2385 lnum = 0;
2386 }
2387 else
2388 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002389 appended_lines_mark(lnum, 1L);
2390 curbuf = save_curbuf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002391 curwin->w_buffer = curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002392 if (ch_part->ch_nomodifiable)
2393 buffer->b_p_ma = FALSE;
2394 else
2395 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002396
2397 if (buffer->b_nwindows > 0)
2398 {
2399 win_T *wp;
2400 win_T *save_curwin;
2401
2402 FOR_ALL_WINDOWS(wp)
2403 {
2404 if (wp->w_buffer == buffer
2405 && (save_write_to
2406 ? wp->w_cursor.lnum == lnum + 1
2407 : (wp->w_cursor.lnum == lnum
2408 && wp->w_cursor.col == 0)))
2409 {
2410 ++wp->w_cursor.lnum;
2411 save_curwin = curwin;
2412 curwin = wp;
2413 curbuf = curwin->w_buffer;
2414 scroll_cursor_bot(0, FALSE);
2415 curwin = save_curwin;
2416 curbuf = curwin->w_buffer;
2417 }
2418 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002419 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002420 channel_need_redraw = TRUE;
2421 }
2422
2423 if (save_write_to)
2424 {
2425 channel_T *ch;
2426
2427 /* Find channels reading from this buffer and adjust their
2428 * next-to-read line number. */
2429 buffer->b_write_to_channel = TRUE;
2430 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2431 {
2432 chanpart_T *in_part = &ch->ch_part[PART_IN];
2433
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002434 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002435 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2436 }
2437 }
2438}
2439
Bram Moolenaar437905c2016-04-26 19:01:05 +02002440 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002441drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002442{
2443 char_u *msg;
2444
2445 while ((msg = channel_get(channel, part)) != NULL)
2446 {
2447 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2448 vim_free(msg);
2449 }
2450}
2451
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002452/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002453 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002454 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002455 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002456 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002457 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002458may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002459{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002460 char_u *msg = NULL;
2461 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002462 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002463 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002464 chanpart_T *ch_part = &channel->ch_part[part];
2465 ch_mode_T ch_mode = ch_part->ch_mode;
2466 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002467 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002468 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002469 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002470 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002471 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002472
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002473 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002474 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002475 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002476
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002477 /* Use a message-specific callback, part callback or channel callback */
2478 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2479 if (cbitem->cq_seq_nr == 0)
2480 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002481 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002482 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002483 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002484 partial = cbitem->cq_partial;
2485 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002486 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002487 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002488 callback = ch_part->ch_callback;
2489 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002490 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002491 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002492 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002493 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002494 partial = channel->ch_partial;
2495 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002496
Bram Moolenaarec68a992016-10-03 21:37:41 +02002497 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002498 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2499 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002500 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002501 /* buffer was wiped out or unloaded */
2502 ch_logs(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002503 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002504 buffer = NULL;
2505 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002506
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002507 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002508 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002509 listitem_T *item;
2510 int argc = 0;
2511
Bram Moolenaard7ece102016-02-02 23:23:02 +01002512 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002513 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002514 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002515 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002516 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002517 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002518 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002519 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002520
Bram Moolenaarece61b02016-02-20 21:39:05 +01002521 for (item = listtv->vval.v_list->lv_first;
2522 item != NULL && argc < CH_JSON_MAX_ARGS;
2523 item = item->li_next)
2524 argv[argc++] = item->li_tv;
2525 while (argc < CH_JSON_MAX_ARGS)
2526 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002527
Bram Moolenaarece61b02016-02-20 21:39:05 +01002528 if (argv[0].v_type == VAR_STRING)
2529 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002530 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002531 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002532 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002533 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002534 }
2535
Bram Moolenaarece61b02016-02-20 21:39:05 +01002536 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002537 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002538 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002539 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002540 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002541 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002542 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002543 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002544 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002545 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002546 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002547 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002548 return FALSE;
2549 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002550 else
2551 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002552 /* If there is no callback or buffer drop the message. */
2553 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002554 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002555 /* If there is a close callback it may use ch_read() to get the
2556 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002557 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002558 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002559 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002560 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002561
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002562 if (ch_mode == MODE_NL)
2563 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002564 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002565 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002566 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002567
2568 /* See if we have a message ending in NL in the first buffer. If
2569 * not try to concatenate the first and the second buffer. */
2570 while (TRUE)
2571 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002572 node = channel_peek(channel, part);
2573 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002574 if (nl != NULL)
2575 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002576 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002577 {
2578 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2579 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002580 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002581 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002582 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002583 buf = node->rq_buffer;
2584
Bram Moolenaarec68a992016-10-03 21:37:41 +02002585 if (nl == NULL)
2586 {
2587 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002588 char_u *new_buf;
2589
2590 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2591 if (new_buf == NULL)
2592 /* This might fail over and over again, should the message
2593 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002594 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002595 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002596 node->rq_buffer = buf;
2597 nl = buf + node->rq_buflen++;
2598 *nl = NUL;
2599 }
2600
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002601 /* Convert NUL to NL, the internal representation. */
2602 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2603 if (*p == NUL)
2604 *p = NL;
2605
2606 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002607 {
2608 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002609 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002610 *nl = NUL;
2611 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002612 else
2613 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002614 /* Copy the message into allocated memory (excluding the NL)
2615 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002616 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002617 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002618 }
2619 }
2620 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002621 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002622 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002623 * get everything we have.
2624 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002625 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002626 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002627
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002628 if (msg == NULL)
2629 return FALSE; /* out of memory (and avoids Coverity warning) */
2630
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002631 argv[1].v_type = VAR_STRING;
2632 argv[1].vval.v_string = msg;
2633 }
2634
Bram Moolenaara07fec92016-02-05 21:04:08 +01002635 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002636 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002637 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002638
Bram Moolenaar958dc692016-12-01 15:34:12 +01002639 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002640 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002641 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002642 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002643 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002644 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002645 break;
2646 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002647 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002648 {
2649 if (channel->ch_drop_never)
2650 {
2651 /* message must be read with ch_read() */
2652 channel_push_json(channel, part, listtv);
2653 listtv = NULL;
2654 }
2655 else
2656 ch_logn(channel, "Dropping message %d without callback",
2657 seq_nr);
2658 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002659 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002660 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002661 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002662 if (buffer != NULL)
2663 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002664 if (msg == NULL)
2665 /* JSON or JS mode: re-encode the message. */
2666 msg = json_encode(listtv, ch_mode);
2667 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002668 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002669#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002670 if (buffer->b_term != NULL)
2671 write_to_term(buffer, msg, channel);
2672 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002673#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002674 append_to_buffer(buffer, msg, channel, part);
2675 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002676 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002677
Bram Moolenaar187db502016-02-27 14:44:26 +01002678 if (callback != NULL)
2679 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002680 if (cbitem != NULL)
2681 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2682 else
2683 {
2684 /* invoke the channel callback */
2685 ch_logs(channel, "Invoking channel callback %s",
2686 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002687 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002688 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002689 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002690 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002691 else
Bram Moolenaar958dc692016-12-01 15:34:12 +01002692 ch_logn(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002693
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002694 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002695 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002696 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002697
2698 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002699}
2700
2701/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002702 * Return TRUE when channel "channel" is open for writing to.
2703 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002704 */
2705 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002706channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002707{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002708 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002709 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002710}
2711
2712/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002713 * Return TRUE when channel "channel" is open for reading or writing.
2714 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002715 */
2716 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002717channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002718{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002719 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002720 || channel->CH_IN_FD != INVALID_FD
2721 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002722 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002723}
2724
2725/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002726 * Return TRUE if "channel" has JSON or other typeahead.
2727 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002728 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002729channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002730{
2731 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2732
2733 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2734 {
2735 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2736 jsonq_T *item = head->jq_next;
2737
2738 return item != NULL;
2739 }
2740 return channel_peek(channel, part) != NULL;
2741}
2742
2743/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002744 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002745 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002746 */
2747 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002748channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002749{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002750 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002751 int has_readahead = FALSE;
2752
Bram Moolenaar77073442016-02-13 23:23:53 +01002753 if (channel == NULL)
2754 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002755 if (req_part == PART_OUT)
2756 {
2757 if (channel->CH_OUT_FD != INVALID_FD)
2758 return "open";
2759 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002760 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002761 }
2762 else if (req_part == PART_ERR)
2763 {
2764 if (channel->CH_ERR_FD != INVALID_FD)
2765 return "open";
2766 if (channel_has_readahead(channel, PART_ERR))
2767 has_readahead = TRUE;
2768 }
2769 else
2770 {
2771 if (channel_is_open(channel))
2772 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002773 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002774 if (channel_has_readahead(channel, part))
2775 {
2776 has_readahead = TRUE;
2777 break;
2778 }
2779 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002780
2781 if (has_readahead)
2782 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002783 return "closed";
2784}
2785
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002786 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002787channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002788{
2789 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002790 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002791 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002792 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002793 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002794
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002795 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002796 STRCAT(namebuf, "_");
2797 tail = STRLEN(namebuf);
2798
2799 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002800 if (chanpart->ch_fd != INVALID_FD)
2801 status = "open";
2802 else if (channel_has_readahead(channel, part))
2803 status = "buffered";
2804 else
2805 status = "closed";
2806 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002807
2808 STRCPY(namebuf + tail, "mode");
2809 switch (chanpart->ch_mode)
2810 {
2811 case MODE_NL: s = "NL"; break;
2812 case MODE_RAW: s = "RAW"; break;
2813 case MODE_JSON: s = "JSON"; break;
2814 case MODE_JS: s = "JS"; break;
2815 }
2816 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2817
2818 STRCPY(namebuf + tail, "io");
2819 if (part == PART_SOCK)
2820 s = "socket";
2821 else switch (chanpart->ch_io)
2822 {
2823 case JIO_NULL: s = "null"; break;
2824 case JIO_PIPE: s = "pipe"; break;
2825 case JIO_FILE: s = "file"; break;
2826 case JIO_BUFFER: s = "buffer"; break;
2827 case JIO_OUT: s = "out"; break;
2828 }
2829 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2830
2831 STRCPY(namebuf + tail, "timeout");
2832 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2833}
2834
2835 void
2836channel_info(channel_T *channel, dict_T *dict)
2837{
2838 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002839 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002840
2841 if (channel->ch_hostname != NULL)
2842 {
2843 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2844 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2845 channel_part_info(channel, dict, "sock", PART_SOCK);
2846 }
2847 else
2848 {
2849 channel_part_info(channel, dict, "out", PART_OUT);
2850 channel_part_info(channel, dict, "err", PART_ERR);
2851 channel_part_info(channel, dict, "in", PART_IN);
2852 }
2853}
2854
Bram Moolenaar77073442016-02-13 23:23:53 +01002855/*
2856 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002857 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002858 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002859 */
2860 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002861channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002862{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002863 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002864
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002865#ifdef FEAT_GUI
2866 channel_gui_unregister(channel);
2867#endif
2868
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002869 ch_close_part(channel, PART_SOCK);
2870 ch_close_part(channel, PART_IN);
2871 ch_close_part(channel, PART_OUT);
2872 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002873
Bram Moolenaar8b374212016-02-24 20:43:06 +01002874 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002875 {
2876 typval_T argv[1];
2877 typval_T rettv;
2878 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002879 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002880
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002881 /* Invoke callbacks before the close callback, since it's weird to
2882 * first invoke the close callback. Increment the refcount to avoid
2883 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002884 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002885 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002886 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002887 while (may_invoke_callback(channel, part))
2888 ;
2889
2890 /* Invoke the close callback, if still set. */
2891 if (channel->ch_close_cb != NULL)
2892 {
2893 ch_logs(channel, "Invoking close callback %s",
2894 (char *)channel->ch_close_cb);
2895 argv[0].v_type = VAR_CHANNEL;
2896 argv[0].vval.v_channel = channel;
2897 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002898 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002899 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002900 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002901 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002902 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002903
2904 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002905 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002906 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002907 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002908
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002909 --channel->ch_refcount;
2910
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002911 if (channel_need_redraw)
2912 {
2913 channel_need_redraw = FALSE;
2914 redraw_after_callback();
2915 }
2916
Bram Moolenaar958dc692016-12-01 15:34:12 +01002917 if (!channel->ch_drop_never)
2918 /* any remaining messages are useless now */
2919 for (part = PART_SOCK; part < PART_IN; ++part)
2920 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002921 }
2922
2923 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002924}
2925
Bram Moolenaard04a0202016-01-26 23:30:18 +01002926/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002927 * Close the "in" part channel "channel".
2928 */
2929 void
2930channel_close_in(channel_T *channel)
2931{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002932 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002933}
2934
2935/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002936 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002937 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002938 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002939channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002940{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002941 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2942 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002943
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002944 while (channel_peek(channel, part) != NULL)
2945 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002946
2947 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002948 {
2949 cbq_T *node = cb_head->cq_next;
2950
2951 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002952 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002953 vim_free(node);
2954 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002955
2956 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002957 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002958 free_tv(json_head->jq_next->jq_value);
2959 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002960 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002961
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002962 free_callback(channel->ch_part[part].ch_callback,
2963 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002964 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002965 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002966}
2967
2968/*
2969 * Clear all the read buffers on "channel".
2970 */
2971 void
2972channel_clear(channel_T *channel)
2973{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002974 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002975 vim_free(channel->ch_hostname);
2976 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002977 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002978 channel_clear_one(channel, PART_OUT);
2979 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002980 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002981 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002982 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002983 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002984 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002985 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002986 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002987}
2988
Bram Moolenaar77073442016-02-13 23:23:53 +01002989#if defined(EXITFREE) || defined(PROTO)
2990 void
2991channel_free_all(void)
2992{
2993 channel_T *channel;
2994
Bram Moolenaard6051b52016-02-28 15:49:03 +01002995 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002996 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2997 channel_clear(channel);
2998}
2999#endif
3000
3001
Bram Moolenaar715d2852016-04-30 17:06:31 +02003002/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003003#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003004
3005/* Buffer size for reading incoming messages. */
3006#define MAXMSGSIZE 4096
3007
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003008#if defined(HAVE_SELECT)
3009/*
3010 * Add write fds where we are waiting for writing to be possible.
3011 */
3012 static int
3013channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3014{
3015 int maxfd = maxfd_arg;
3016 channel_T *ch;
3017
3018 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3019 {
3020 chanpart_T *in_part = &ch->ch_part[PART_IN];
3021
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003022 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003023 {
3024 FD_SET((int)in_part->ch_fd, wfds);
3025 if ((int)in_part->ch_fd >= maxfd)
3026 maxfd = (int)in_part->ch_fd + 1;
3027 }
3028 }
3029 return maxfd;
3030}
3031#else
3032/*
3033 * Add write fds where we are waiting for writing to be possible.
3034 */
3035 static int
3036channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3037{
3038 int nfd = nfd_in;
3039 channel_T *ch;
3040
3041 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3042 {
3043 chanpart_T *in_part = &ch->ch_part[PART_IN];
3044
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003045 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003046 {
3047 in_part->ch_poll_idx = nfd;
3048 fds[nfd].fd = in_part->ch_fd;
3049 fds[nfd].events = POLLOUT;
3050 ++nfd;
3051 }
3052 else
3053 in_part->ch_poll_idx = -1;
3054 }
3055 return nfd;
3056}
3057#endif
3058
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003059typedef enum {
3060 CW_READY,
3061 CW_NOT_READY,
3062 CW_ERROR
3063} channel_wait_result;
3064
Bram Moolenaard04a0202016-01-26 23:30:18 +01003065/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003066 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003067 * Return CW_READY when there is something to read.
3068 * Return CW_NOT_READY when there is nothing to read.
3069 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003070 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003071 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003072channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003073{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003074 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003075 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003076
Bram Moolenaard8070362016-02-15 21:56:54 +01003077# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003078 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003079 {
3080 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003081 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003082 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003083 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003084
3085 /* reading from a pipe, not a socket */
3086 while (TRUE)
3087 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003088 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3089
3090 if (r && nread > 0)
3091 return CW_READY;
3092 if (r == 0)
3093 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003094
3095 /* perhaps write some buffer lines */
3096 channel_write_any_lines();
3097
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003098 sleep_time = deadline - GetTickCount();
3099 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003100 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003101 /* Wait for a little while. Very short at first, up to 10 msec
3102 * after looping a few times. */
3103 if (sleep_time > delay)
3104 sleep_time = delay;
3105 Sleep(sleep_time);
3106 delay = delay * 2;
3107 if (delay > 10)
3108 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003109 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003110 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003111 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003112#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003113 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003114#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003115 struct timeval tval;
3116 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003117 fd_set wfds;
3118 int ret;
3119 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003120
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003121 tval.tv_sec = timeout / 1000;
3122 tval.tv_usec = (timeout % 1000) * 1000;
3123 for (;;)
3124 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003125 FD_ZERO(&rfds);
3126 FD_SET((int)fd, &rfds);
3127
3128 /* Write lines to a pipe when a pipe can be written to. Need to
3129 * set this every time, some buffers may be done. */
3130 maxfd = (int)fd + 1;
3131 FD_ZERO(&wfds);
3132 maxfd = channel_fill_wfds(maxfd, &wfds);
3133
3134 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003135# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003136 SOCK_ERRNO;
3137 if (ret == -1 && errno == EINTR)
3138 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003139# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003140 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003141 {
3142 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003143 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003144 channel_write_any_lines();
3145 continue;
3146 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003147 break;
3148 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003149#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003150 for (;;)
3151 {
3152 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3153 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003154
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003155 fds[0].fd = fd;
3156 fds[0].events = POLLIN;
3157 nfd = channel_fill_poll_write(nfd, fds);
3158 if (poll(fds, nfd, timeout) > 0)
3159 {
3160 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003161 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003162 channel_write_any_lines();
3163 continue;
3164 }
3165 break;
3166 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003167#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003168 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003169 return CW_NOT_READY;
3170}
3171
3172 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003173ch_close_part_on_error(
3174 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003175{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003176 char msgbuf[80];
3177
3178 vim_snprintf(msgbuf, sizeof(msgbuf),
3179 "%%s(): Read %s from ch_part[%d], closing",
3180 (is_err ? "error" : "EOF"), part);
3181
3182 if (is_err)
3183 /* Do not call emsg(), most likely the other end just exited. */
3184 ch_errors(channel, msgbuf, func);
3185 else
3186 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003187
3188 /* Queue a "DETACH" netbeans message in the command queue in order to
3189 * terminate the netbeans session later. Do not end the session here
3190 * directly as we may be running in the context of a call to
3191 * netbeans_parse_messages():
3192 * netbeans_parse_messages
3193 * -> autocmd triggered while processing the netbeans cmd
3194 * -> ui_breakcheck
3195 * -> gui event loop or select loop
3196 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003197 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003198 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003199 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003200 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003201 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3202
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003203 /* When reading is not possible close this part of the channel. Don't
3204 * close the channel yet, there may be something to read on another part. */
3205 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003206
3207#ifdef FEAT_GUI
3208 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003209 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003210#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003211}
3212
3213 static void
3214channel_close_now(channel_T *channel)
3215{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003216 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003217 if (channel->ch_nb_close_cb != NULL)
3218 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003219 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003220}
3221
3222/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003223 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003224 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003225 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003226 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003227 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003228channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003229{
3230 static char_u *buf = NULL;
3231 int len = 0;
3232 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003233 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003234 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003235
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003236 fd = channel->ch_part[part].ch_fd;
3237 if (fd == INVALID_FD)
3238 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003239 ch_errors(channel, "channel_read() called while %s part is closed",
3240 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003241 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003242 }
3243 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003244
3245 /* Allocate a buffer to read into. */
3246 if (buf == NULL)
3247 {
3248 buf = alloc(MAXMSGSIZE);
3249 if (buf == NULL)
3250 return; /* out of memory! */
3251 }
3252
3253 /* Keep on reading for as long as there is something to read.
3254 * Use select() or poll() to avoid blocking on a message that is exactly
3255 * MAXMSGSIZE long. */
3256 for (;;)
3257 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003258 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003259 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003260 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003261 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003262 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003263 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003264 if (len <= 0)
3265 break; /* error or nothing more to read */
3266
3267 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003268 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003269 readlen += len;
3270 if (len < MAXMSGSIZE)
3271 break; /* did read everything that's available */
3272 }
3273
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003274 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003275 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003276 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003277
3278#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003279 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003280 if (CH_HAS_GUI && gtk_main_level() > 0)
3281 gtk_main_quit();
3282#endif
3283}
3284
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003285/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003286 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003287 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003288 * Returns what was read in allocated memory.
3289 * Returns NULL in case of error or timeout.
3290 */
3291 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003292channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003293{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003294 char_u *buf;
3295 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003296 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003297 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003298 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003299 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003300
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003301 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003302 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003303
3304 while (TRUE)
3305 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003306 node = channel_peek(channel, part);
3307 if (node != NULL)
3308 {
3309 if (mode == MODE_RAW || (mode == MODE_NL
3310 && channel_first_nl(node) != NULL))
3311 /* got a complete message */
3312 break;
3313 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3314 continue;
3315 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003316
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003317 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003318 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003319 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003320 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003321 {
3322 ch_log(channel, "Timed out");
3323 return NULL;
3324 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003325 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003326 }
3327
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003328 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003329 if (mode == MODE_RAW)
3330 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003331 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003332 }
3333 else
3334 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003335 char_u *p;
3336
3337 buf = node->rq_buffer;
3338 nl = channel_first_nl(node);
3339
3340 /* Convert NUL to NL, the internal representation. */
3341 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3342 if (*p == NUL)
3343 *p = NL;
3344
3345 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003346 {
3347 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003348 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003349 *nl = NUL;
3350 }
3351 else
3352 {
3353 /* Copy the message into allocated memory and remove it from the
3354 * buffer. */
3355 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003356 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003357 }
3358 }
3359 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003360 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003361 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003362}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003363
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003364/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003365 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003366 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003367 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003368 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003369 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003370 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003371channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003372 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003373 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003374 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003375 int id,
3376 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003377{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003378 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003379 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003380 int timeout;
3381 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003382
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003383 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003384 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003385 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003386 for (;;)
3387 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003388 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003389
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003390 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003391 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003392 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003393 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003394 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003395 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003396
Bram Moolenaard7ece102016-02-02 23:23:02 +01003397 if (!more)
3398 {
3399 /* Handle any other messages in the queue. If done some more
3400 * messages may have arrived. */
3401 if (channel_parse_messages())
3402 continue;
3403
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003404 /* Wait for up to the timeout. If there was an incomplete message
3405 * use the deadline for that. */
3406 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003407 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003408 {
3409#ifdef WIN32
3410 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3411#else
3412 {
3413 struct timeval now_tv;
3414
3415 gettimeofday(&now_tv, NULL);
3416 timeout = (chanpart->ch_deadline.tv_sec
3417 - now_tv.tv_sec) * 1000
3418 + (chanpart->ch_deadline.tv_usec
3419 - now_tv.tv_usec) / 1000
3420 + 1;
3421 }
3422#endif
3423 if (timeout < 0)
3424 {
3425 /* Something went wrong, channel_parse_json() didn't
3426 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003427 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003428 timeout = timeout_arg;
3429 }
3430 else if (timeout > timeout_arg)
3431 timeout = timeout_arg;
3432 }
3433 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003434 if (fd == INVALID_FD
3435 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003436 {
3437 if (timeout == timeout_arg)
3438 {
3439 if (fd != INVALID_FD)
3440 ch_log(channel, "Timed out");
3441 break;
3442 }
3443 }
3444 else
3445 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003446 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003447 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003448 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003449 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003450}
3451
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003452/*
3453 * Common for ch_read() and ch_readraw().
3454 */
3455 void
3456common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3457{
3458 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003459 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003460 jobopt_T opt;
3461 int mode;
3462 int timeout;
3463 int id = -1;
3464 typval_T *listtv = NULL;
3465
3466 /* return an empty string by default */
3467 rettv->v_type = VAR_STRING;
3468 rettv->vval.v_string = NULL;
3469
3470 clear_job_options(&opt);
3471 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3472 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003473 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003474
Bram Moolenaar437905c2016-04-26 19:01:05 +02003475 if (opt.jo_set & JO_PART)
3476 part = opt.jo_part;
3477 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003478 if (channel != NULL)
3479 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003480 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003481 part = channel_part_read(channel);
3482 mode = channel_get_mode(channel, part);
3483 timeout = channel_get_timeout(channel, part);
3484 if (opt.jo_set & JO_TIMEOUT)
3485 timeout = opt.jo_timeout;
3486
3487 if (raw || mode == MODE_RAW || mode == MODE_NL)
3488 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3489 else
3490 {
3491 if (opt.jo_set & JO_ID)
3492 id = opt.jo_id;
3493 channel_read_json_block(channel, part, timeout, id, &listtv);
3494 if (listtv != NULL)
3495 {
3496 *rettv = *listtv;
3497 vim_free(listtv);
3498 }
3499 else
3500 {
3501 rettv->v_type = VAR_SPECIAL;
3502 rettv->vval.v_number = VVAL_NONE;
3503 }
3504 }
3505 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003506
3507theend:
3508 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003509}
3510
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003511# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3512 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003513/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003514 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003515 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003516 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003517 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003518channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003519{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003520 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003521 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003522
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003523 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003524 for (channel = first_channel; channel != NULL;
3525 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003526 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003527 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003528 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003529 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003530 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003531 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003532 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003533 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003534 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003535}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003536# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003537
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003538# if defined(WIN32) || defined(PROTO)
3539/*
3540 * Check the channels for anything that is ready to be read.
3541 * The data is put in the read queue.
3542 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003543 void
3544channel_handle_events(void)
3545{
3546 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003547 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003548 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003549
3550 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3551 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003552 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003553 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003554 {
3555 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003556 if (fd != INVALID_FD)
3557 {
3558 int r = channel_wait(channel, fd, 0);
3559
3560 if (r == CW_READY)
3561 channel_read(channel, part, "channel_handle_events");
3562 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003563 ch_close_part_on_error(channel, part, TRUE,
3564 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003565 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003566 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003567 }
3568}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003569# endif
3570
Bram Moolenaard04a0202016-01-26 23:30:18 +01003571/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003572 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003573 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003574 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003575 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003576 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003577channel_send(
3578 channel_T *channel,
3579 ch_part_T part,
3580 char_u *buf,
3581 int len,
3582 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003583{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003584 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003585 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003586
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003587 fd = channel->ch_part[part].ch_fd;
3588 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003589 {
3590 if (!channel->ch_error && fun != NULL)
3591 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003592 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003593 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003594 }
3595 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003596 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003597 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003598
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003599 if (log_fd != NULL)
3600 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003601 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003602 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003603 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003604 fprintf(log_fd, "'\n");
3605 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003606 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003607 }
3608
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003609 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003610 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003611 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003612 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003613 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003614 {
3615 if (!channel->ch_error && fun != NULL)
3616 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003617 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003618 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003619 }
3620 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003621 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003622 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003623
3624 channel->ch_error = FALSE;
3625 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003626}
3627
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003628/*
3629 * Common for "ch_sendexpr()" and "ch_sendraw()".
3630 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003631 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003632 * Otherwise returns NULL.
3633 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003634 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003635send_common(
3636 typval_T *argvars,
3637 char_u *text,
3638 int id,
3639 int eval,
3640 jobopt_T *opt,
3641 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003642 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003643{
3644 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003645 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003646
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003647 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003648 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003649 if (channel == NULL)
3650 return NULL;
3651 part_send = channel_part_send(channel);
3652 *part_read = channel_part_read(channel);
3653
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003654 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3655 return NULL;
3656
3657 /* Set the callback. An empty callback means no callback and not reading
3658 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3659 * allowed. */
3660 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3661 {
3662 if (eval)
3663 {
3664 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3665 return NULL;
3666 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003667 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003668 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003669 }
3670
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003671 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003672 && opt->jo_callback == NULL)
3673 return channel;
3674 return NULL;
3675}
3676
3677/*
3678 * common for "ch_evalexpr()" and "ch_sendexpr()"
3679 */
3680 void
3681ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3682{
3683 char_u *text;
3684 typval_T *listtv;
3685 channel_T *channel;
3686 int id;
3687 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003688 ch_part_T part_send;
3689 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003690 jobopt_T opt;
3691 int timeout;
3692
3693 /* return an empty string by default */
3694 rettv->v_type = VAR_STRING;
3695 rettv->vval.v_string = NULL;
3696
Bram Moolenaar437905c2016-04-26 19:01:05 +02003697 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003698 if (channel == NULL)
3699 return;
3700 part_send = channel_part_send(channel);
3701
3702 ch_mode = channel_get_mode(channel, part_send);
3703 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3704 {
3705 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3706 return;
3707 }
3708
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003709 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003710 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003711 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003712 if (text == NULL)
3713 return;
3714
3715 channel = send_common(argvars, text, id, eval, &opt,
3716 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3717 vim_free(text);
3718 if (channel != NULL && eval)
3719 {
3720 if (opt.jo_set & JO_TIMEOUT)
3721 timeout = opt.jo_timeout;
3722 else
3723 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003724 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3725 == OK)
3726 {
3727 list_T *list = listtv->vval.v_list;
3728
3729 /* Move the item from the list and then change the type to
3730 * avoid the value being freed. */
3731 *rettv = list->lv_last->li_tv;
3732 list->lv_last->li_tv.v_type = VAR_NUMBER;
3733 free_tv(listtv);
3734 }
3735 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003736 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003737}
3738
3739/*
3740 * common for "ch_evalraw()" and "ch_sendraw()"
3741 */
3742 void
3743ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3744{
3745 char_u buf[NUMBUFLEN];
3746 char_u *text;
3747 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003748 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003749 jobopt_T opt;
3750 int timeout;
3751
3752 /* return an empty string by default */
3753 rettv->v_type = VAR_STRING;
3754 rettv->vval.v_string = NULL;
3755
3756 text = get_tv_string_buf(&argvars[1], buf);
3757 channel = send_common(argvars, text, 0, eval, &opt,
3758 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3759 if (channel != NULL && eval)
3760 {
3761 if (opt.jo_set & JO_TIMEOUT)
3762 timeout = opt.jo_timeout;
3763 else
3764 timeout = channel_get_timeout(channel, part_read);
3765 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3766 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003767 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003768}
3769
Bram Moolenaard04a0202016-01-26 23:30:18 +01003770# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003771/*
3772 * Add open channels to the poll struct.
3773 * Return the adjusted struct index.
3774 * The type of "fds" is hidden to avoid problems with the function proto.
3775 */
3776 int
3777channel_poll_setup(int nfd_in, void *fds_in)
3778{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003779 int nfd = nfd_in;
3780 channel_T *channel;
3781 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003782 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003783
Bram Moolenaar77073442016-02-13 23:23:53 +01003784 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003785 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003786 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003787 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003788 chanpart_T *ch_part = &channel->ch_part[part];
3789
3790 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003791 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003792 ch_part->ch_poll_idx = nfd;
3793 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003794 fds[nfd].events = POLLIN;
3795 nfd++;
3796 }
3797 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003798 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003799 }
3800 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003801
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003802 nfd = channel_fill_poll_write(nfd, fds);
3803
Bram Moolenaare0874f82016-01-24 20:36:41 +01003804 return nfd;
3805}
3806
3807/*
3808 * The type of "fds" is hidden to avoid problems with the function proto.
3809 */
3810 int
3811channel_poll_check(int ret_in, void *fds_in)
3812{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003813 int ret = ret_in;
3814 channel_T *channel;
3815 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003816 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003817 int idx;
3818 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003819
Bram Moolenaar77073442016-02-13 23:23:53 +01003820 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003821 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003822 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003823 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003824 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003825
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003826 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003827 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003828 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003829 --ret;
3830 }
3831 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003832
3833 in_part = &channel->ch_part[PART_IN];
3834 idx = in_part->ch_poll_idx;
3835 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3836 {
3837 if (in_part->ch_buf_append)
3838 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003839 if (in_part->ch_bufref.br_buf != NULL)
3840 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003841 }
3842 else
3843 channel_write_in(channel);
3844 --ret;
3845 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003846 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003847
3848 return ret;
3849}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003850# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003851
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003852# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003853/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003854 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003855 */
3856 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003857channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003858{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003859 int maxfd = maxfd_in;
3860 channel_T *channel;
3861 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003862 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003863 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003864
Bram Moolenaar77073442016-02-13 23:23:53 +01003865 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003866 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003867 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003868 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003869 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003870
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003871 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003872 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003873 FD_SET((int)fd, rfds);
3874 if (maxfd < (int)fd)
3875 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003876 }
3877 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003878 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003879
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003880 maxfd = channel_fill_wfds(maxfd, wfds);
3881
Bram Moolenaare0874f82016-01-24 20:36:41 +01003882 return maxfd;
3883}
3884
3885/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003886 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003887 */
3888 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003889channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003890{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003891 int ret = ret_in;
3892 channel_T *channel;
3893 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003894 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003895 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003896 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003897
Bram Moolenaar77073442016-02-13 23:23:53 +01003898 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003899 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003900 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003901 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003902 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003903
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003904 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003905 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003906 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003907 --ret;
3908 }
3909 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003910
3911 in_part = &channel->ch_part[PART_IN];
3912 if (ret > 0 && in_part->ch_fd != INVALID_FD
3913 && FD_ISSET(in_part->ch_fd, wfds))
3914 {
3915 if (in_part->ch_buf_append)
3916 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003917 if (in_part->ch_bufref.br_buf != NULL)
3918 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003919 }
3920 else
3921 channel_write_in(channel);
3922 --ret;
3923 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003924 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003925
3926 return ret;
3927}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003928# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003929
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003930/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003931 * Execute queued up commands.
3932 * Invoked from the main loop when it's safe to execute received commands.
3933 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003934 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003935 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003936channel_parse_messages(void)
3937{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003938 channel_T *channel = first_channel;
3939 int ret = FALSE;
3940 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003941 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003942#ifdef ELAPSED_FUNC
3943 ELAPSED_TYPE start_tv;
3944
3945 ELAPSED_INIT(start_tv);
3946#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003947
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003948 ++safe_to_invoke_callback;
3949
Bram Moolenaard0b65022016-03-06 21:50:33 +01003950 /* Only do this message when another message was given, otherwise we get
3951 * lots of them. */
3952 if (did_log_msg)
3953 {
3954 ch_log(NULL, "looking for messages on channels");
3955 did_log_msg = FALSE;
3956 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003957 while (channel != NULL)
3958 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003959 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003960 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003961 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003962 channel_close_now(channel);
3963 /* channel may have been freed, start over */
3964 channel = first_channel;
3965 continue;
3966 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003967 if (channel->ch_to_be_freed)
3968 {
3969 channel_free(channel);
3970 /* channel has been freed, start over */
3971 channel = first_channel;
3972 continue;
3973 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003974 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003975 {
3976 /* channel is no longer useful, free it */
3977 channel_free(channel);
3978 channel = first_channel;
3979 part = PART_SOCK;
3980 continue;
3981 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003982 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003983 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003984 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003985 /* Increase the refcount, in case the handler causes the channel
3986 * to be unreferenced or closed. */
3987 ++channel->ch_refcount;
3988 r = may_invoke_callback(channel, part);
3989 if (r == OK)
3990 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003991 if (channel_unref(channel) || (r == OK
3992#ifdef ELAPSED_FUNC
3993 /* Limit the time we loop here to 100 msec, otherwise
3994 * Vim becomes unresponsive when the callback takes
3995 * more than a bit of time. */
3996 && ELAPSED_FUNC(start_tv) < 100L
3997#endif
3998 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003999 {
4000 /* channel was freed or something was done, start over */
4001 channel = first_channel;
4002 part = PART_SOCK;
4003 continue;
4004 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004005 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004006 if (part < PART_ERR)
4007 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004008 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004009 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004010 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004011 part = PART_SOCK;
4012 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004013 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004014
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004015 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004016 {
4017 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004018 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01004019 }
4020
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004021 --safe_to_invoke_callback;
4022
Bram Moolenaard7ece102016-02-02 23:23:02 +01004023 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004024}
4025
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004026/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004027 * Return TRUE if any channel has readahead. That means we should not block on
4028 * waiting for input.
4029 */
4030 int
4031channel_any_readahead(void)
4032{
4033 channel_T *channel = first_channel;
4034 ch_part_T part = PART_SOCK;
4035
4036 while (channel != NULL)
4037 {
4038 if (channel_has_readahead(channel, part))
4039 return TRUE;
4040 if (part < PART_ERR)
4041 ++part;
4042 else
4043 {
4044 channel = channel->ch_next;
4045 part = PART_SOCK;
4046 }
4047 }
4048 return FALSE;
4049}
4050
4051/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004052 * Mark references to lists used in channels.
4053 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004054 int
4055set_ref_in_channel(int copyID)
4056{
Bram Moolenaar77073442016-02-13 23:23:53 +01004057 int abort = FALSE;
4058 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004059 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004060
Bram Moolenaar77073442016-02-13 23:23:53 +01004061 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004062 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004063 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004064 tv.v_type = VAR_CHANNEL;
4065 tv.vval.v_channel = channel;
4066 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004067 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004068 return abort;
4069}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004070
4071/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004072 * Return the "part" to write to for "channel".
4073 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004074 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004075channel_part_send(channel_T *channel)
4076{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004077 if (channel->CH_SOCK_FD == INVALID_FD)
4078 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004079 return PART_SOCK;
4080}
4081
4082/*
4083 * Return the default "part" to read from for "channel".
4084 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004085 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004086channel_part_read(channel_T *channel)
4087{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004088 if (channel->CH_SOCK_FD == INVALID_FD)
4089 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004090 return PART_SOCK;
4091}
4092
4093/*
4094 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004095 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004096 */
4097 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004098channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004099{
Bram Moolenaar77073442016-02-13 23:23:53 +01004100 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004101 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004102 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004103}
4104
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004105/*
4106 * Return the timeout of "channel"/"part"
4107 */
4108 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004109channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004110{
4111 return channel->ch_part[part].ch_timeout;
4112}
4113
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004114 static int
4115handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4116{
4117 char_u *val = get_tv_string(item);
4118
4119 opt->jo_set |= jo;
4120 if (STRCMP(val, "nl") == 0)
4121 *modep = MODE_NL;
4122 else if (STRCMP(val, "raw") == 0)
4123 *modep = MODE_RAW;
4124 else if (STRCMP(val, "js") == 0)
4125 *modep = MODE_JS;
4126 else if (STRCMP(val, "json") == 0)
4127 *modep = MODE_JSON;
4128 else
4129 {
4130 EMSG2(_(e_invarg2), val);
4131 return FAIL;
4132 }
4133 return OK;
4134}
4135
4136 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004137handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004138{
4139 char_u *val = get_tv_string(item);
4140
4141 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4142 if (STRCMP(val, "null") == 0)
4143 opt->jo_io[part] = JIO_NULL;
4144 else if (STRCMP(val, "pipe") == 0)
4145 opt->jo_io[part] = JIO_PIPE;
4146 else if (STRCMP(val, "file") == 0)
4147 opt->jo_io[part] = JIO_FILE;
4148 else if (STRCMP(val, "buffer") == 0)
4149 opt->jo_io[part] = JIO_BUFFER;
4150 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4151 opt->jo_io[part] = JIO_OUT;
4152 else
4153 {
4154 EMSG2(_(e_invarg2), val);
4155 return FAIL;
4156 }
4157 return OK;
4158}
4159
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004160/*
4161 * Clear a jobopt_T before using it.
4162 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004163 void
4164clear_job_options(jobopt_T *opt)
4165{
4166 vim_memset(opt, 0, sizeof(jobopt_T));
4167}
4168
4169/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004170 * Free any members of a jobopt_T.
4171 */
4172 void
4173free_job_options(jobopt_T *opt)
4174{
4175 if (opt->jo_partial != NULL)
4176 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004177 else if (opt->jo_callback != NULL)
4178 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004179 if (opt->jo_out_partial != NULL)
4180 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004181 else if (opt->jo_out_cb != NULL)
4182 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004183 if (opt->jo_err_partial != NULL)
4184 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004185 else if (opt->jo_err_cb != NULL)
4186 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004187 if (opt->jo_close_partial != NULL)
4188 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004189 else if (opt->jo_close_cb != NULL)
4190 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004191 if (opt->jo_exit_partial != NULL)
4192 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004193 else if (opt->jo_exit_cb != NULL)
4194 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004195}
4196
4197/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004198 * Get the PART_ number from the first character of an option name.
4199 */
4200 static int
4201part_from_char(int c)
4202{
4203 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4204}
4205
4206/*
4207 * Get the option entries from the dict in "tv", parse them and put the result
4208 * in "opt".
4209 * Only accept options in "supported".
4210 * If an option value is invalid return FAIL.
4211 */
4212 int
4213get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4214{
4215 typval_T *item;
4216 char_u *val;
4217 dict_T *dict;
4218 int todo;
4219 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004220 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004221
4222 opt->jo_set = 0;
4223 if (tv->v_type == VAR_UNKNOWN)
4224 return OK;
4225 if (tv->v_type != VAR_DICT)
4226 {
4227 EMSG(_(e_invarg));
4228 return FAIL;
4229 }
4230 dict = tv->vval.v_dict;
4231 if (dict == NULL)
4232 return OK;
4233
4234 todo = (int)dict->dv_hashtab.ht_used;
4235 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4236 if (!HASHITEM_EMPTY(hi))
4237 {
4238 item = &dict_lookup(hi)->di_tv;
4239
4240 if (STRCMP(hi->hi_key, "mode") == 0)
4241 {
4242 if (!(supported & JO_MODE))
4243 break;
4244 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4245 return FAIL;
4246 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004247 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004248 {
4249 if (!(supported & JO_IN_MODE))
4250 break;
4251 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4252 == FAIL)
4253 return FAIL;
4254 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004255 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004256 {
4257 if (!(supported & JO_OUT_MODE))
4258 break;
4259 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4260 == FAIL)
4261 return FAIL;
4262 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004263 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004264 {
4265 if (!(supported & JO_ERR_MODE))
4266 break;
4267 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4268 == FAIL)
4269 return FAIL;
4270 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004271 else if (STRCMP(hi->hi_key, "in_io") == 0
4272 || STRCMP(hi->hi_key, "out_io") == 0
4273 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004274 {
4275 if (!(supported & JO_OUT_IO))
4276 break;
4277 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4278 return FAIL;
4279 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004280 else if (STRCMP(hi->hi_key, "in_name") == 0
4281 || STRCMP(hi->hi_key, "out_name") == 0
4282 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004283 {
4284 part = part_from_char(*hi->hi_key);
4285
4286 if (!(supported & JO_OUT_IO))
4287 break;
4288 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4289 opt->jo_io_name[part] =
4290 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4291 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004292 else if (STRCMP(hi->hi_key, "pty") == 0)
4293 {
4294 if (!(supported & JO_MODE))
4295 break;
4296 opt->jo_pty = get_tv_number(item);
4297 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004298 else if (STRCMP(hi->hi_key, "in_buf") == 0
4299 || STRCMP(hi->hi_key, "out_buf") == 0
4300 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004301 {
4302 part = part_from_char(*hi->hi_key);
4303
4304 if (!(supported & JO_OUT_IO))
4305 break;
4306 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4307 opt->jo_io_buf[part] = get_tv_number(item);
4308 if (opt->jo_io_buf[part] <= 0)
4309 {
4310 EMSG2(_(e_invarg2), get_tv_string(item));
4311 return FAIL;
4312 }
4313 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4314 {
4315 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4316 return FAIL;
4317 }
4318 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004319 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4320 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4321 {
4322 part = part_from_char(*hi->hi_key);
4323
4324 if (!(supported & JO_OUT_IO))
4325 break;
4326 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4327 opt->jo_modifiable[part] = get_tv_number(item);
4328 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004329 else if (STRCMP(hi->hi_key, "out_msg") == 0
4330 || STRCMP(hi->hi_key, "err_msg") == 0)
4331 {
4332 part = part_from_char(*hi->hi_key);
4333
4334 if (!(supported & JO_OUT_IO))
4335 break;
4336 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4337 opt->jo_message[part] = get_tv_number(item);
4338 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004339 else if (STRCMP(hi->hi_key, "in_top") == 0
4340 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004341 {
4342 linenr_T *lp;
4343
4344 if (!(supported & JO_OUT_IO))
4345 break;
4346 if (hi->hi_key[3] == 't')
4347 {
4348 lp = &opt->jo_in_top;
4349 opt->jo_set |= JO_IN_TOP;
4350 }
4351 else
4352 {
4353 lp = &opt->jo_in_bot;
4354 opt->jo_set |= JO_IN_BOT;
4355 }
4356 *lp = get_tv_number(item);
4357 if (*lp < 0)
4358 {
4359 EMSG2(_(e_invarg2), get_tv_string(item));
4360 return FAIL;
4361 }
4362 }
4363 else if (STRCMP(hi->hi_key, "channel") == 0)
4364 {
4365 if (!(supported & JO_OUT_IO))
4366 break;
4367 opt->jo_set |= JO_CHANNEL;
4368 if (item->v_type != VAR_CHANNEL)
4369 {
4370 EMSG2(_(e_invarg2), "channel");
4371 return FAIL;
4372 }
4373 opt->jo_channel = item->vval.v_channel;
4374 }
4375 else if (STRCMP(hi->hi_key, "callback") == 0)
4376 {
4377 if (!(supported & JO_CALLBACK))
4378 break;
4379 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004380 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004381 if (opt->jo_callback == NULL)
4382 {
4383 EMSG2(_(e_invarg2), "callback");
4384 return FAIL;
4385 }
4386 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004387 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004388 {
4389 if (!(supported & JO_OUT_CALLBACK))
4390 break;
4391 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004392 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004393 if (opt->jo_out_cb == NULL)
4394 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004395 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004396 return FAIL;
4397 }
4398 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004399 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004400 {
4401 if (!(supported & JO_ERR_CALLBACK))
4402 break;
4403 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004404 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004405 if (opt->jo_err_cb == NULL)
4406 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004407 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004408 return FAIL;
4409 }
4410 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004411 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004412 {
4413 if (!(supported & JO_CLOSE_CALLBACK))
4414 break;
4415 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004416 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004417 if (opt->jo_close_cb == NULL)
4418 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004419 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004420 return FAIL;
4421 }
4422 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004423 else if (STRCMP(hi->hi_key, "drop") == 0)
4424 {
4425 int never = FALSE;
4426 val = get_tv_string(item);
4427
4428 if (STRCMP(val, "never") == 0)
4429 never = TRUE;
4430 else if (STRCMP(val, "auto") != 0)
4431 {
4432 EMSG2(_(e_invarg2), "drop");
4433 return FAIL;
4434 }
4435 opt->jo_drop_never = never;
4436 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004437 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4438 {
4439 if (!(supported & JO_EXIT_CB))
4440 break;
4441 opt->jo_set |= JO_EXIT_CB;
4442 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4443 if (opt->jo_exit_cb == NULL)
4444 {
4445 EMSG2(_(e_invarg2), "exit_cb");
4446 return FAIL;
4447 }
4448 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004449 else if (STRCMP(hi->hi_key, "waittime") == 0)
4450 {
4451 if (!(supported & JO_WAITTIME))
4452 break;
4453 opt->jo_set |= JO_WAITTIME;
4454 opt->jo_waittime = get_tv_number(item);
4455 }
4456 else if (STRCMP(hi->hi_key, "timeout") == 0)
4457 {
4458 if (!(supported & JO_TIMEOUT))
4459 break;
4460 opt->jo_set |= JO_TIMEOUT;
4461 opt->jo_timeout = get_tv_number(item);
4462 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004463 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004464 {
4465 if (!(supported & JO_OUT_TIMEOUT))
4466 break;
4467 opt->jo_set |= JO_OUT_TIMEOUT;
4468 opt->jo_out_timeout = get_tv_number(item);
4469 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004470 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004471 {
4472 if (!(supported & JO_ERR_TIMEOUT))
4473 break;
4474 opt->jo_set |= JO_ERR_TIMEOUT;
4475 opt->jo_err_timeout = get_tv_number(item);
4476 }
4477 else if (STRCMP(hi->hi_key, "part") == 0)
4478 {
4479 if (!(supported & JO_PART))
4480 break;
4481 opt->jo_set |= JO_PART;
4482 val = get_tv_string(item);
4483 if (STRCMP(val, "err") == 0)
4484 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004485 else if (STRCMP(val, "out") == 0)
4486 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004487 else
4488 {
4489 EMSG2(_(e_invarg2), val);
4490 return FAIL;
4491 }
4492 }
4493 else if (STRCMP(hi->hi_key, "id") == 0)
4494 {
4495 if (!(supported & JO_ID))
4496 break;
4497 opt->jo_set |= JO_ID;
4498 opt->jo_id = get_tv_number(item);
4499 }
4500 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4501 {
4502 if (!(supported & JO_STOPONEXIT))
4503 break;
4504 opt->jo_set |= JO_STOPONEXIT;
4505 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4506 opt->jo_soe_buf);
4507 if (opt->jo_stoponexit == NULL)
4508 {
4509 EMSG2(_(e_invarg2), "stoponexit");
4510 return FAIL;
4511 }
4512 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004513 else if (STRCMP(hi->hi_key, "block_write") == 0)
4514 {
4515 if (!(supported & JO_BLOCK_WRITE))
4516 break;
4517 opt->jo_set |= JO_BLOCK_WRITE;
4518 opt->jo_block_write = get_tv_number(item);
4519 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004520 else
4521 break;
4522 --todo;
4523 }
4524 if (todo > 0)
4525 {
4526 EMSG2(_(e_invarg2), hi->hi_key);
4527 return FAIL;
4528 }
4529
4530 return OK;
4531}
4532
4533/*
4534 * Get the channel from the argument.
4535 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004536 * When "check_open" is TRUE check that the channel can be used.
4537 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004538 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004539 */
4540 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004541get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004542{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004543 channel_T *channel = NULL;
4544 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004545
4546 if (tv->v_type == VAR_JOB)
4547 {
4548 if (tv->vval.v_job != NULL)
4549 channel = tv->vval.v_job->jv_channel;
4550 }
4551 else if (tv->v_type == VAR_CHANNEL)
4552 {
4553 channel = tv->vval.v_channel;
4554 }
4555 else
4556 {
4557 EMSG2(_(e_invarg2), get_tv_string(tv));
4558 return NULL;
4559 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004560 if (channel != NULL && reading)
4561 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004562 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004563
Bram Moolenaar437905c2016-04-26 19:01:05 +02004564 if (check_open && (channel == NULL || (!channel_is_open(channel)
4565 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004566 {
4567 EMSG(_("E906: not an open channel"));
4568 return NULL;
4569 }
4570 return channel;
4571}
4572
4573static job_T *first_job = NULL;
4574
4575 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004577{
4578 ch_log(job->jv_channel, "Freeing job");
4579 if (job->jv_channel != NULL)
4580 {
4581 /* The link from the channel to the job doesn't count as a reference,
4582 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004583 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004584 * NULL the reference. We don't set ch_job_killed, unreferencing the
4585 * job doesn't mean it stops running. */
4586 job->jv_channel->ch_job = NULL;
4587 channel_unref(job->jv_channel);
4588 }
4589 mch_clear_job(job);
4590
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004591 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004592 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004593}
4594
4595 static void
4596job_free_job(job_T *job)
4597{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004598 if (job->jv_next != NULL)
4599 job->jv_next->jv_prev = job->jv_prev;
4600 if (job->jv_prev == NULL)
4601 first_job = job->jv_next;
4602 else
4603 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004604 vim_free(job);
4605}
4606
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004607 static void
4608job_free(job_T *job)
4609{
4610 if (!in_free_unref_items)
4611 {
4612 job_free_contents(job);
4613 job_free_job(job);
4614 }
4615}
4616
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004617#if defined(EXITFREE) || defined(PROTO)
4618 void
4619job_free_all(void)
4620{
4621 while (first_job != NULL)
4622 job_free(first_job);
4623}
4624#endif
4625
4626/*
4627 * Return TRUE if we need to check if the process of "job" has ended.
4628 */
4629 static int
4630job_need_end_check(job_T *job)
4631{
4632 return job->jv_status == JOB_STARTED
4633 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4634}
4635
4636/*
4637 * Return TRUE if the channel of "job" is still useful.
4638 */
4639 static int
4640job_channel_still_useful(job_T *job)
4641{
4642 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4643}
4644
4645/*
4646 * Return TRUE if the job should not be freed yet. Do not free the job when
4647 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4648 * or when the associated channel will do something with the job output.
4649 */
4650 static int
4651job_still_useful(job_T *job)
4652{
4653 return job_need_end_check(job) || job_channel_still_useful(job);
4654}
4655
4656/*
4657 * NOTE: Must call job_cleanup() only once right after the status of "job"
4658 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4659 * mch_detect_ended_job() returned non-NULL).
4660 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004661 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02004662job_cleanup(job_T *job)
4663{
4664 if (job->jv_status != JOB_ENDED)
4665 return;
4666
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004667 /* Ready to cleanup the job. */
4668 job->jv_status = JOB_FINISHED;
4669
Bram Moolenaar97792de2016-10-15 18:36:49 +02004670 if (job->jv_exit_cb != NULL)
4671 {
4672 typval_T argv[3];
4673 typval_T rettv;
4674 int dummy;
4675
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004676 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004677 ++job->jv_refcount;
4678 argv[0].v_type = VAR_JOB;
4679 argv[0].vval.v_job = job;
4680 argv[1].v_type = VAR_NUMBER;
4681 argv[1].vval.v_number = job->jv_exitval;
4682 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4683 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4684 job->jv_exit_partial, NULL);
4685 clear_tv(&rettv);
4686 --job->jv_refcount;
4687 channel_need_redraw = TRUE;
4688 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004689
4690 /* Do not free the job in case the close callback of the associated channel
4691 * isn't invoked yet and may get information by job_info(). */
4692 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004693 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004694 /* The job was already unreferenced and the associated channel was
4695 * detached, now that it ended it can be freed. Careful: caller must
4696 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004697 job_free(job);
4698 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004699
4700#ifdef FEAT_TERMINAL
4701 term_job_ended(job);
4702#endif
Bram Moolenaar97792de2016-10-15 18:36:49 +02004703}
4704
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004705/*
4706 * Mark references in jobs that are still useful.
4707 */
4708 int
4709set_ref_in_job(int copyID)
4710{
4711 int abort = FALSE;
4712 job_T *job;
4713 typval_T tv;
4714
4715 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004716 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004717 {
4718 tv.v_type = VAR_JOB;
4719 tv.vval.v_job = job;
4720 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4721 }
4722 return abort;
4723}
4724
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004725/*
4726 * Dereference "job". Note that after this "job" may have been freed.
4727 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004728 void
4729job_unref(job_T *job)
4730{
4731 if (job != NULL && --job->jv_refcount <= 0)
4732 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004733 /* Do not free the job if there is a channel where the close callback
4734 * may get the job info. */
4735 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004736 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004737 /* Do not free the job when it has not ended yet and there is a
4738 * "stoponexit" flag or an exit callback. */
4739 if (!job_need_end_check(job))
4740 {
4741 job_free(job);
4742 }
4743 else if (job->jv_channel != NULL)
4744 {
4745 /* Do remove the link to the channel, otherwise it hangs
4746 * around until Vim exits. See job_free() for refcount. */
4747 ch_log(job->jv_channel, "detaching channel from job");
4748 job->jv_channel->ch_job = NULL;
4749 channel_unref(job->jv_channel);
4750 job->jv_channel = NULL;
4751 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004752 }
4753 }
4754}
4755
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004756 int
4757free_unused_jobs_contents(int copyID, int mask)
4758{
4759 int did_free = FALSE;
4760 job_T *job;
4761
4762 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004763 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004764 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004765 {
4766 /* Free the channel and ordinary items it contains, but don't
4767 * recurse into Lists, Dictionaries etc. */
4768 job_free_contents(job);
4769 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004770 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004771 return did_free;
4772}
4773
4774 void
4775free_unused_jobs(int copyID, int mask)
4776{
4777 job_T *job;
4778 job_T *job_next;
4779
4780 for (job = first_job; job != NULL; job = job_next)
4781 {
4782 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004783 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004784 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004786 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004787 job_free_job(job);
4788 }
4789 }
4790}
4791
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004792/*
4793 * Allocate a job. Sets the refcount to one and sets options default.
4794 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004795 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004796job_alloc(void)
4797{
4798 job_T *job;
4799
4800 job = (job_T *)alloc_clear(sizeof(job_T));
4801 if (job != NULL)
4802 {
4803 job->jv_refcount = 1;
4804 job->jv_stoponexit = vim_strsave((char_u *)"term");
4805
4806 if (first_job != NULL)
4807 {
4808 first_job->jv_prev = job;
4809 job->jv_next = first_job;
4810 }
4811 first_job = job;
4812 }
4813 return job;
4814}
4815
4816 void
4817job_set_options(job_T *job, jobopt_T *opt)
4818{
4819 if (opt->jo_set & JO_STOPONEXIT)
4820 {
4821 vim_free(job->jv_stoponexit);
4822 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4823 job->jv_stoponexit = NULL;
4824 else
4825 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4826 }
4827 if (opt->jo_set & JO_EXIT_CB)
4828 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004829 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004830 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004831 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004832 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004833 job->jv_exit_partial = NULL;
4834 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004835 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004836 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004837 job->jv_exit_partial = opt->jo_exit_partial;
4838 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004839 {
4840 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004841 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004842 }
4843 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004844 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004845 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004846 func_ref(job->jv_exit_cb);
4847 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004848 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004849 }
4850}
4851
4852/*
4853 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4854 */
4855 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004856job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004857{
4858 job_T *job;
4859
4860 for (job = first_job; job != NULL; job = job->jv_next)
4861 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4862 mch_stop_job(job, job->jv_stoponexit);
4863}
4864
4865/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004866 * Return TRUE when there is any job that has an exit callback and might exit,
4867 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004868 */
4869 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004870has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004871{
4872 job_T *job;
4873
4874 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004875 /* Only should check if the channel has been closed, if the channel is
4876 * open the job won't exit. */
4877 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004878 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004879 return TRUE;
4880 return FALSE;
4881}
4882
Bram Moolenaar97792de2016-10-15 18:36:49 +02004883#define MAX_CHECK_ENDED 8
4884
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004885/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004886 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004887 */
4888 void
4889job_check_ended(void)
4890{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004891 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004892
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004893 if (first_job == NULL)
4894 return;
4895
Bram Moolenaar97792de2016-10-15 18:36:49 +02004896 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004897 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004898 /* NOTE: mch_detect_ended_job() must only return a job of which the
4899 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004900 job_T *job = mch_detect_ended_job(first_job);
4901
4902 if (job == NULL)
4903 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004904 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004905 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004906
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004907 if (channel_need_redraw)
4908 {
4909 channel_need_redraw = FALSE;
4910 redraw_after_callback();
4911 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004912}
4913
4914/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02004915 * Create a job and return it. Implements job_start().
4916 * The returned job has a refcount of one.
4917 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004918 */
4919 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004920job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004921{
4922 job_T *job;
4923 char_u *cmd = NULL;
4924#if defined(UNIX)
4925# define USE_ARGV
4926 char **argv = NULL;
4927 int argc = 0;
4928#else
4929 garray_T ga;
4930#endif
4931 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004932 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004933
4934 job = job_alloc();
4935 if (job == NULL)
4936 return NULL;
4937
4938 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004939#ifndef USE_ARGV
4940 ga_init2(&ga, (int)sizeof(char*), 20);
4941#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004942
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004943 if (opt_arg != NULL)
4944 opt = *opt_arg;
4945 else
4946 {
4947 /* Default mode is NL. */
4948 clear_job_options(&opt);
4949 opt.jo_mode = MODE_NL;
4950 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004951 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4952 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004953 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004954 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004955
4956 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004957 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004958 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4959 && opt.jo_io[part] == JIO_FILE
4960 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4961 || *opt.jo_io_name[part] == NUL))
4962 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004963 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004964 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004965 }
4966
4967 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4968 {
4969 buf_T *buf = NULL;
4970
4971 /* check that we can find the buffer before starting the job */
4972 if (opt.jo_set & JO_IN_BUF)
4973 {
4974 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4975 if (buf == NULL)
4976 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4977 }
4978 else if (!(opt.jo_set & JO_IN_NAME))
4979 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004980 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004981 }
4982 else
4983 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4984 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004985 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004986 if (buf->b_ml.ml_mfp == NULL)
4987 {
4988 char_u numbuf[NUMBUFLEN];
4989 char_u *s;
4990
4991 if (opt.jo_set & JO_IN_BUF)
4992 {
4993 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4994 s = numbuf;
4995 }
4996 else
4997 s = opt.jo_io_name[PART_IN];
4998 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004999 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005000 }
5001 job->jv_in_buf = buf;
5002 }
5003
5004 job_set_options(job, &opt);
5005
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005006 if (argvars[0].v_type == VAR_STRING)
5007 {
5008 /* Command is a string. */
5009 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005010 if (cmd == NULL || *cmd == NUL)
5011 {
5012 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005013 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005014 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005015#ifdef USE_ARGV
5016 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005017 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005018 argv[argc] = NULL;
5019#endif
5020 }
5021 else if (argvars[0].v_type != VAR_LIST
5022 || argvars[0].vval.v_list == NULL
5023 || argvars[0].vval.v_list->lv_len < 1)
5024 {
5025 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005026 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005027 }
5028 else
5029 {
5030 list_T *l = argvars[0].vval.v_list;
5031 listitem_T *li;
5032 char_u *s;
5033
5034#ifdef USE_ARGV
5035 /* Pass argv[] to mch_call_shell(). */
5036 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5037 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005038 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005039#endif
5040 for (li = l->lv_first; li != NULL; li = li->li_next)
5041 {
5042 s = get_tv_string_chk(&li->li_tv);
5043 if (s == NULL)
5044 goto theend;
5045#ifdef USE_ARGV
5046 argv[argc++] = (char *)s;
5047#else
5048 /* Only escape when needed, double quotes are not always allowed. */
5049 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
5050 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005051# ifdef WIN32
5052 int old_ssl = p_ssl;
5053
5054 /* This is using CreateProcess, not cmd.exe. Always use
5055 * double quote and backslashes. */
5056 p_ssl = 0;
5057# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005058 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005059# ifdef WIN32
5060 p_ssl = old_ssl;
5061# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005062 if (s == NULL)
5063 goto theend;
5064 ga_concat(&ga, s);
5065 vim_free(s);
5066 }
5067 else
5068 ga_concat(&ga, s);
5069 if (li->li_next != NULL)
5070 ga_append(&ga, ' ');
5071#endif
5072 }
5073#ifdef USE_ARGV
5074 argv[argc] = NULL;
5075#else
5076 cmd = ga.ga_data;
5077#endif
5078 }
5079
5080#ifdef USE_ARGV
5081 if (ch_log_active())
5082 {
5083 garray_T ga;
5084 int i;
5085
5086 ga_init2(&ga, (int)sizeof(char), 200);
5087 for (i = 0; i < argc; ++i)
5088 {
5089 if (i > 0)
5090 ga_concat(&ga, (char_u *)" ");
5091 ga_concat(&ga, (char_u *)argv[i]);
5092 }
5093 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
5094 ga_clear(&ga);
5095 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005096 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005097#else
5098 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005099 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005100#endif
5101
5102 /* If the channel is reading from a buffer, write lines now. */
5103 if (job->jv_channel != NULL)
5104 channel_write_in(job->jv_channel);
5105
5106theend:
5107#ifdef USE_ARGV
5108 vim_free(argv);
5109#else
5110 vim_free(ga.ga_data);
5111#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005112 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005113 return job;
5114}
5115
5116/*
5117 * Get the status of "job" and invoke the exit callback when needed.
5118 * The returned string is not allocated.
5119 */
5120 char *
5121job_status(job_T *job)
5122{
5123 char *result;
5124
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005125 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005126 /* No need to check, dead is dead. */
5127 result = "dead";
5128 else if (job->jv_status == JOB_FAILED)
5129 result = "fail";
5130 else
5131 {
5132 result = mch_job_status(job);
5133 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005134 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005135 }
5136 return result;
5137}
5138
Bram Moolenaar8950a562016-03-12 15:22:55 +01005139/*
5140 * Implementation of job_info().
5141 */
5142 void
5143job_info(job_T *job, dict_T *dict)
5144{
5145 dictitem_T *item;
5146 varnumber_T nr;
5147
5148 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5149
5150 item = dictitem_alloc((char_u *)"channel");
5151 if (item == NULL)
5152 return;
5153 item->di_tv.v_lock = 0;
5154 item->di_tv.v_type = VAR_CHANNEL;
5155 item->di_tv.vval.v_channel = job->jv_channel;
5156 if (job->jv_channel != NULL)
5157 ++job->jv_channel->ch_refcount;
5158 if (dict_add(dict, item) == FAIL)
5159 dictitem_free(item);
5160
5161#ifdef UNIX
5162 nr = job->jv_pid;
5163#else
5164 nr = job->jv_proc_info.dwProcessId;
5165#endif
5166 dict_add_nr_str(dict, "process", nr, NULL);
5167
5168 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005169 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005170 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5171}
5172
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005173/*
5174 * Send a signal to "job". Implements job_stop().
5175 * When "type" is not NULL use this for the type.
5176 * Otherwise use argvars[1] for the type.
5177 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005178 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005179job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005180{
5181 char_u *arg;
5182
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005183 if (type != NULL)
5184 arg = (char_u *)type;
5185 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005186 arg = (char_u *)"";
5187 else
5188 {
5189 arg = get_tv_string_chk(&argvars[1]);
5190 if (arg == NULL)
5191 {
5192 EMSG(_(e_invarg));
5193 return 0;
5194 }
5195 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005196 if (job->jv_status == JOB_FAILED)
5197 {
5198 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5199 return 0;
5200 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005201 if (job->jv_status == JOB_ENDED)
5202 {
5203 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5204 return 0;
5205 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005206 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
5207 if (mch_stop_job(job, arg) == FAIL)
5208 return 0;
5209
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005210 /* Assume that only "kill" will kill the job. */
5211 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005212 job->jv_channel->ch_job_killed = TRUE;
5213
5214 /* We don't try freeing the job, obviously the caller still has a
5215 * reference to it. */
5216 return 1;
5217}
5218
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005219#endif /* FEAT_JOB_CHANNEL */