blob: ffee334b54aea1e2a14d40482ca18ab4e6fd8f94 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaard04a0202016-01-26 23:30:18 +010023#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
119 EMSG2(_(e_notopen), fname);
120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200135ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100162ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100170 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171 }
172}
173
174 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100175ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176{
177 if (log_fd != NULL)
178 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100181 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100183 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184 }
185}
186
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100188ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100192 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100196 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197 }
198}
199
200 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100201ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100209 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 }
211}
212
213 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100214ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215{
216 if (log_fd != NULL)
217 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100222 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223 }
224}
225
226 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100227ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100228{
229 if (log_fd != NULL)
230 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100235 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236 }
237}
238
239 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100240ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100241{
242 if (log_fd != NULL)
243 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100248 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100249 }
250}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100251
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252#ifdef _WIN32
253# undef PERROR
254# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
255 (char_u *)msg, (char_u *)strerror_win32(errno))
256
257 static char *
258strerror_win32(int eno)
259{
260 static LPVOID msgbuf = NULL;
261 char_u *ptr;
262
263 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200264 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100265 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200266 msgbuf = NULL;
267 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100268 FormatMessage(
269 FORMAT_MESSAGE_ALLOCATE_BUFFER |
270 FORMAT_MESSAGE_FROM_SYSTEM |
271 FORMAT_MESSAGE_IGNORE_INSERTS,
272 NULL,
273 eno,
274 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
275 (LPTSTR) &msgbuf,
276 0,
277 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200278 if (msgbuf != NULL)
279 /* chomp \r or \n */
280 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
281 switch (*ptr)
282 {
283 case '\r':
284 STRMOVE(ptr, ptr + 1);
285 ptr--;
286 break;
287 case '\n':
288 if (*(ptr + 1) == '\0')
289 *ptr = '\0';
290 else
291 *ptr = ' ';
292 break;
293 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100294 return msgbuf;
295}
296#endif
297
Bram Moolenaar77073442016-02-13 23:23:53 +0100298/*
299 * The list of all allocated channels.
300 */
301static channel_T *first_channel = NULL;
302static int next_ch_id = 0;
303
304/*
305 * Allocate a new channel. The refcount is set to 1.
306 * The channel isn't actually used until it is opened.
307 * Returns NULL if out of memory.
308 */
309 channel_T *
310add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100311{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200312 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100314
Bram Moolenaar77073442016-02-13 23:23:53 +0100315 if (channel == NULL)
316 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317
Bram Moolenaar77073442016-02-13 23:23:53 +0100318 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100319 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100320
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200321 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100323 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100324#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100325 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100326#endif
327#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100328 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100329#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100330 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100331 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100332
Bram Moolenaar77073442016-02-13 23:23:53 +0100333 if (first_channel != NULL)
334 {
335 first_channel->ch_prev = channel;
336 channel->ch_next = first_channel;
337 }
338 first_channel = channel;
339
340 channel->ch_refcount = 1;
341 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100342}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100343
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200344 int
345has_any_channel(void)
346{
347 return first_channel != NULL;
348}
349
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100350/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100351 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100352 * Return TRUE if "channel" has a callback and the associated job wasn't
353 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100354 */
355 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100356channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100357{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100359 int has_out_msg;
360 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100361
362 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100363 if (channel->ch_job_killed && channel->ch_job == NULL)
364 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100365
366 /* If there is a close callback it may still need to be invoked. */
367 if (channel->ch_close_cb != NULL)
368 return TRUE;
369
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200370 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200371 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200372 return TRUE;
373
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100374 /* If there is no callback then nobody can get readahead. If the fd is
375 * closed and there is no readahead then the callback won't be called. */
376 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
377 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
378 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100379 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
380 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
381 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
382 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
383 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
384 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100385 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100386 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200387 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200388 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
389 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200390 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200391 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
392 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100393}
394
395/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200396 * Close a channel and free all its resources.
397 */
398 static void
399channel_free_contents(channel_T *channel)
400{
401 channel_close(channel, TRUE);
402 channel_clear(channel);
403 ch_log(channel, "Freeing channel");
404}
405
406 static void
407channel_free_channel(channel_T *channel)
408{
409 if (channel->ch_next != NULL)
410 channel->ch_next->ch_prev = channel->ch_prev;
411 if (channel->ch_prev == NULL)
412 first_channel = channel->ch_next;
413 else
414 channel->ch_prev->ch_next = channel->ch_next;
415 vim_free(channel);
416}
417
418 static void
419channel_free(channel_T *channel)
420{
421 if (!in_free_unref_items)
422 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200423 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200424 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425 else
426 {
427 channel_free_contents(channel);
428 channel_free_channel(channel);
429 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200430 }
431}
432
433/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100435 * possible, there is no callback to be invoked or the associated job was
436 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100437 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100439 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440channel_may_free(channel_T *channel)
441{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100442 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100443 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100444 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100445 return TRUE;
446 }
447 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100448}
449
450/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100451 * Decrement the reference count on "channel" and maybe free it when it goes
452 * down to zero. Don't free it if there is a pending action.
453 * Returns TRUE when the channel is no longer referenced.
454 */
455 int
456channel_unref(channel_T *channel)
457{
458 if (channel != NULL && --channel->ch_refcount <= 0)
459 return channel_may_free(channel);
460 return FALSE;
461}
462
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200463 int
464free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100465{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 int did_free = FALSE;
467 channel_T *ch;
468
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200469 /* This is invoked from the garbage collector, which only runs at a safe
470 * point. */
471 ++safe_to_invoke_callback;
472
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200473 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200474 if (!channel_still_useful(ch)
475 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200476 {
477 /* Free the channel and ordinary items it contains, but don't
478 * recurse into Lists, Dictionaries etc. */
479 channel_free_contents(ch);
480 did_free = TRUE;
481 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200482
483 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200484 return did_free;
485}
486
487 void
488free_unused_channels(int copyID, int mask)
489{
490 channel_T *ch;
491 channel_T *ch_next;
492
493 for (ch = first_channel; ch != NULL; ch = ch_next)
494 {
495 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200496 if (!channel_still_useful(ch)
497 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200498 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200499 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200500 channel_free_channel(ch);
501 }
502 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100503}
504
Bram Moolenaard04a0202016-01-26 23:30:18 +0100505#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100506
507#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
508 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100509channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100510{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100511 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200512 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100513
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100514 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100516 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100517 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200518 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100519}
520#endif
521
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525#ifdef FEAT_GUI_X11
526 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527messageFromServer(XtPointer clientData,
528 int *unused1 UNUSED,
529 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100530{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100531 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100532}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100533#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100534
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100536# if GTK_CHECK_VERSION(3,0,0)
537 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538messageFromServer(GIOChannel *unused1 UNUSED,
539 GIOCondition unused2 UNUSED,
540 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100541{
542 channel_read_fd(GPOINTER_TO_INT(clientData));
543 return TRUE; /* Return FALSE instead in case the event source is to
544 * be removed after this function returns. */
545}
546# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100547 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200548messageFromServer(gpointer clientData,
549 gint unused1 UNUSED,
550 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100551{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100552 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100553}
Bram Moolenaar98921892016-02-23 17:14:37 +0100554# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100555#endif
556
557 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200558channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100559{
Bram Moolenaarde279892016-03-11 22:19:44 +0100560 if (!CH_HAS_GUI)
561 return;
562
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# ifdef FEAT_GUI_X11
564 /* Tell notifier we are interested in being called
565 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100566 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
567 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100568 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100569 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100570 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200571 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100572 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100573# else
574# ifdef FEAT_GUI_GTK
575 /* Tell gdk we are interested in being called when there
576 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100577 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100578# if GTK_CHECK_VERSION(3,0,0)
579 {
580 GIOChannel *chnnl = g_io_channel_unix_new(
581 (gint)channel->ch_part[part].ch_fd);
582
583 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
584 chnnl,
585 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200586 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100587 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
588
589 g_io_channel_unref(chnnl);
590 }
591# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100592 channel->ch_part[part].ch_inputHandler = gdk_input_add(
593 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100594 (GdkInputCondition)
595 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200596 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100597 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100598# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100599# endif
600# endif
601}
602
Bram Moolenaarde279892016-03-11 22:19:44 +0100603 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100604channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100605{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100606 if (channel->CH_SOCK_FD != INVALID_FD)
607 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100608 if (channel->CH_OUT_FD != INVALID_FD)
609 channel_gui_register_one(channel, PART_OUT);
610 if (channel->CH_ERR_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612}
613
614/*
615 * Register any of our file descriptors with the GUI event handling system.
616 * Called when the GUI has started.
617 */
618 void
619channel_gui_register_all(void)
620{
Bram Moolenaar77073442016-02-13 23:23:53 +0100621 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622
Bram Moolenaar77073442016-02-13 23:23:53 +0100623 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100624 channel_gui_register(channel);
625}
626
627 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200628channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100629{
630# ifdef FEAT_GUI_X11
631 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
632 {
633 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
634 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
635 }
636# else
637# ifdef FEAT_GUI_GTK
638 if (channel->ch_part[part].ch_inputHandler != 0)
639 {
640# if GTK_CHECK_VERSION(3,0,0)
641 g_source_remove(channel->ch_part[part].ch_inputHandler);
642# else
643 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
644# endif
645 channel->ch_part[part].ch_inputHandler = 0;
646 }
647# endif
648# endif
649}
650
651 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100652channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100653{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200654 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100655
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100656 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100657 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658}
659
660#endif
661
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100662static char *e_cannot_connect = N_("E902: Cannot connect to port");
663
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100665 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100666 * "waittime" is the time in msec to wait for the connection.
667 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100668 * Returns the channel for success.
669 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100670 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100671 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100672channel_open(
673 char *hostname,
674 int port_in,
675 int waittime,
676 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100677{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100678 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100681#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100682 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100683 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100684#else
685 int port = port_in;
686#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100687 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100688 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100690#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691 channel_init_winsock();
692#endif
693
Bram Moolenaar77073442016-02-13 23:23:53 +0100694 channel = add_channel();
695 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100697 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100698 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100699 }
700
701 /* Get the server internet address and put into addr structure */
702 /* fill in the socket address structure and connect to server */
703 vim_memset((char *)&server, 0, sizeof(server));
704 server.sin_family = AF_INET;
705 server.sin_port = htons(port);
706 if ((host = gethostbyname(hostname)) == NULL)
707 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200709 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100710 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100711 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100712 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100713 {
714 char *p;
715
716 /* When using host->h_addr directly ubsan warns for it to not be
717 * aligned. First copy the pointer to aviod that. */
718 memcpy(&p, &host->h_addr, sizeof(p));
719 memcpy((char *)&server.sin_addr, p, host->h_length);
720 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100722 /* On Mac and Solaris a zero timeout almost never works. At least wait
723 * one millisecond. Let's do it for all systems, because we don't know why
724 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100725 if (waittime == 0)
726 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100727
728 /*
729 * For Unix we need to call connect() again after connect() failed.
730 * On Win32 one time is sufficient.
731 */
732 while (TRUE)
733 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100734 long elapsed_msec = 0;
735 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100737 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100738 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100739 sd = socket(AF_INET, SOCK_STREAM, 0);
740 if (sd == -1)
741 {
742 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200743 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100744 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100745 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100746 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100747
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100748 if (waittime >= 0)
749 {
750 /* Make connect() non-blocking. */
751 if (
752#ifdef _WIN32
753 ioctlsocket(sd, FIONBIO, &val) < 0
754#else
755 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
756#endif
757 )
758 {
759 SOCK_ERRNO;
760 ch_errorn(channel,
761 "channel_open: Connect failed with errno %d", errno);
762 sock_close(sd);
763 channel_free(channel);
764 return NULL;
765 }
766 }
767
768 /* Try connecting to the server. */
769 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
770 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
771
Bram Moolenaar045a2842016-03-08 22:33:07 +0100772 if (ret == 0)
773 /* The connection could be established. */
774 break;
775
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100776 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100777 if (waittime < 0 || (errno != EWOULDBLOCK
778 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100779#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100780 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100781#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100782 ))
783 {
784 ch_errorn(channel,
785 "channel_open: Connect failed with errno %d", errno);
786 PERROR(_(e_cannot_connect));
787 sock_close(sd);
788 channel_free(channel);
789 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100790 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100791
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100792 /* Limit the waittime to 50 msec. If it doesn't work within this
793 * time we close the socket and try creating it again. */
794 waitnow = waittime > 50 ? 50 : waittime;
795
Bram Moolenaar045a2842016-03-08 22:33:07 +0100796 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100797 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100798#ifndef WIN32
799 if (errno != ECONNREFUSED)
800#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801 {
802 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100803 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100805#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 int so_error = 0;
807 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 struct timeval start_tv;
809 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100810#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100811 FD_ZERO(&rfds);
812 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100813 FD_ZERO(&wfds);
814 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100815
Bram Moolenaar562ca712016-03-09 21:50:05 +0100816 tv.tv_sec = waitnow / 1000;
817 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100818#ifndef WIN32
819 gettimeofday(&start_tv, NULL);
820#endif
821 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100822 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100823 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100824
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100825 if (ret < 0)
826 {
827 SOCK_ERRNO;
828 ch_errorn(channel,
829 "channel_open: Connect failed with errno %d", errno);
830 PERROR(_(e_cannot_connect));
831 sock_close(sd);
832 channel_free(channel);
833 return NULL;
834 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100835
Bram Moolenaare081e212016-02-28 22:33:46 +0100836#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100837 /* On Win32: select() is expected to work and wait for up to
838 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100839 if (FD_ISSET(sd, &wfds))
840 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100841 elapsed_msec = waitnow;
842 if (waittime > 1 && elapsed_msec < waittime)
843 {
844 waittime -= elapsed_msec;
845 continue;
846 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100847#else
848 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100849 * After putting the socket in non-blocking mode, connect() will
850 * return EINPROGRESS, select() will not wait (as if writing is
851 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100852 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100853 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100854 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100855 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100856 {
857 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100858 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100859 if (ret < 0 || (so_error != 0
860 && so_error != EWOULDBLOCK
861 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100862# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100864# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100865 ))
866 {
867 ch_errorn(channel,
868 "channel_open: Connect failed with errno %d",
869 so_error);
870 PERROR(_(e_cannot_connect));
871 sock_close(sd);
872 channel_free(channel);
873 return NULL;
874 }
875 }
876
Bram Moolenaar045a2842016-03-08 22:33:07 +0100877 if (FD_ISSET(sd, &wfds) && so_error == 0)
878 /* Did not detect an error, connection is established. */
879 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100880
Bram Moolenaar045a2842016-03-08 22:33:07 +0100881 gettimeofday(&end_tv, NULL);
882 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
883 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100884#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100885 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100886
887#ifndef WIN32
888 if (waittime > 1 && elapsed_msec < waittime)
889 {
890 /* The port isn't ready but we also didn't get an error.
891 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100892 * yet. Select() may return early, wait until the remaining
893 * "waitnow" and try again. */
894 waitnow -= elapsed_msec;
895 waittime -= elapsed_msec;
896 if (waitnow > 0)
897 {
898 mch_delay((long)waitnow, TRUE);
899 ui_breakcheck();
900 waittime -= waitnow;
901 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100902 if (!got_int)
903 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100904 if (waittime <= 0)
905 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100906 waittime = 1;
907 continue;
908 }
909 /* we were interrupted, behave as if timed out */
910 }
911#endif
912
913 /* We timed out. */
914 ch_error(channel, "Connection timed out");
915 sock_close(sd);
916 channel_free(channel);
917 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100918 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100919
Bram Moolenaar045a2842016-03-08 22:33:07 +0100920 ch_log(channel, "Connection made");
921
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100922 if (waittime >= 0)
923 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100924#ifdef _WIN32
925 val = 0;
926 ioctlsocket(sd, FIONBIO, &val);
927#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100928 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100929#endif
930 }
931
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100932 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100933 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100934 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
935 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200936 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100937
938#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100939 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100940#endif
941
Bram Moolenaar77073442016-02-13 23:23:53 +0100942 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100943}
944
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945/*
946 * Implements ch_open().
947 */
948 channel_T *
949channel_open_func(typval_T *argvars)
950{
951 char_u *address;
952 char_u *p;
953 char *rest;
954 int port;
955 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200956 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957
958 address = get_tv_string(&argvars[0]);
959 if (argvars[1].v_type != VAR_UNKNOWN
960 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
961 {
962 EMSG(_(e_invarg));
963 return NULL;
964 }
965
966 /* parse address */
967 p = vim_strchr(address, ':');
968 if (p == NULL)
969 {
970 EMSG2(_(e_invarg2), address);
971 return NULL;
972 }
973 *p++ = NUL;
974 port = strtol((char *)p, &rest, 10);
975 if (*address == NUL || port <= 0 || *rest != NUL)
976 {
977 p[-1] = ':';
978 EMSG2(_(e_invarg2), address);
979 return NULL;
980 }
981
982 /* parse options */
983 clear_job_options(&opt);
984 opt.jo_mode = MODE_JSON;
985 opt.jo_timeout = 2000;
986 if (get_job_options(&argvars[1], &opt,
987 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200988 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100989 if (opt.jo_timeout < 0)
990 {
991 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200992 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100993 }
994
995 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
996 if (channel != NULL)
997 {
998 opt.jo_set = JO_ALL;
999 channel_set_options(channel, &opt);
1000 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001001theend:
1002 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001003 return channel;
1004}
1005
Bram Moolenaarde279892016-03-11 22:19:44 +01001006 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001007ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001008{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001009 sock_T *fd = &channel->ch_part[part].ch_fd;
1010
Bram Moolenaarde279892016-03-11 22:19:44 +01001011 if (*fd != INVALID_FD)
1012 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001013 if (part == PART_SOCK)
1014 sock_close(*fd);
1015 else
1016 fd_close(*fd);
Bram Moolenaarde279892016-03-11 22:19:44 +01001017 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001018
1019 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001020 }
1021}
1022
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001023 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001024channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001025{
Bram Moolenaarde279892016-03-11 22:19:44 +01001026 if (in != INVALID_FD)
1027 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001028 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001029 channel->CH_IN_FD = in;
1030 }
1031 if (out != INVALID_FD)
1032 {
1033# if defined(FEAT_GUI)
1034 channel_gui_unregister_one(channel, PART_OUT);
1035# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001036 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001037 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001038 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001039# if defined(FEAT_GUI)
1040 channel_gui_register_one(channel, PART_OUT);
1041# endif
1042 }
1043 if (err != INVALID_FD)
1044 {
1045# if defined(FEAT_GUI)
1046 channel_gui_unregister_one(channel, PART_ERR);
1047# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001048 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001049 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001050 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001051# if defined(FEAT_GUI)
1052 channel_gui_register_one(channel, PART_ERR);
1053# endif
1054 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001055}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001056
Bram Moolenaard6051b52016-02-28 15:49:03 +01001057/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001058 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001059 * This does not keep a refcount, when the job is freed ch_job is cleared.
1060 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001061 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001062channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001063{
Bram Moolenaar77073442016-02-13 23:23:53 +01001064 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001065
1066 channel_set_options(channel, options);
1067
1068 if (job->jv_in_buf != NULL)
1069 {
1070 chanpart_T *in_part = &channel->ch_part[PART_IN];
1071
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001072 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001073 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001074 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001075 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001076 {
1077 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1078 {
1079 /* Special mode: send last-but-one line when appending a line
1080 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001081 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001082 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001083 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001084 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001085 }
1086 else
1087 in_part->ch_buf_top = options->jo_in_top;
1088 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001089 else
1090 in_part->ch_buf_top = 1;
1091 if (options->jo_set & JO_IN_BOT)
1092 in_part->ch_buf_bot = options->jo_in_bot;
1093 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001094 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001095 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001096}
1097
1098/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001100 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001101 */
1102 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001103find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001104{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001105 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001106 buf_T *save_curbuf = curbuf;
1107
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001108 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001109 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001110 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001111 if (buf == NULL)
1112 buf = buflist_findname_exp(name);
1113 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001114 if (buf == NULL)
1115 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001116 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001117 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001118 if (buf == NULL)
1119 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001120 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001121 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001122#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001123 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1124 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001125#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001126 if (curbuf->b_ml.ml_mfp == NULL)
1127 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001128 if (msg)
1129 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001130 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001131 changed_bytes(1, 0);
1132 curbuf = save_curbuf;
1133 }
1134
1135 return buf;
1136}
1137
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001138 static void
1139set_callback(
1140 char_u **cbp,
1141 partial_T **pp,
1142 char_u *callback,
1143 partial_T *partial)
1144{
1145 free_callback(*cbp, *pp);
1146 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001147 {
1148 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001149 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001150 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001151 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001152 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001153 func_ref(*cbp);
1154 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001155 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001156 else
1157 *cbp = NULL;
1158 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001159 if (partial != NULL)
1160 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001161}
1162
Bram Moolenaar187db502016-02-27 14:44:26 +01001163/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001164 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001165 */
1166 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001167channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001168{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001169 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001170
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001171 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001172 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001173 channel->ch_part[part].ch_mode = opt->jo_mode;
1174 if (opt->jo_set & JO_IN_MODE)
1175 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1176 if (opt->jo_set & JO_OUT_MODE)
1177 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1178 if (opt->jo_set & JO_ERR_MODE)
1179 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001180
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001181 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001182 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001183 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1184 if (opt->jo_set & JO_OUT_TIMEOUT)
1185 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1186 if (opt->jo_set & JO_ERR_TIMEOUT)
1187 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001188 if (opt->jo_set & JO_BLOCK_WRITE)
1189 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001190
1191 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001192 set_callback(&channel->ch_callback, &channel->ch_partial,
1193 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001194 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001195 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1196 &channel->ch_part[PART_OUT].ch_partial,
1197 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001198 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001199 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1200 &channel->ch_part[PART_ERR].ch_partial,
1201 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001202 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001203 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1204 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001205 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001206
1207 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1208 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001209 buf_T *buf;
1210
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001211 /* writing output to a buffer. Default mode is NL. */
1212 if (!(opt->jo_set & JO_OUT_MODE))
1213 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001214 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001215 {
1216 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1217 if (buf == NULL)
1218 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1219 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001220 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001221 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001222 int msg = TRUE;
1223
1224 if (opt->jo_set2 & JO2_OUT_MSG)
1225 msg = opt->jo_message[PART_OUT];
1226 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001227 }
1228 if (buf != NULL)
1229 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001230 if (opt->jo_set & JO_OUT_MODIFIABLE)
1231 channel->ch_part[PART_OUT].ch_nomodifiable =
1232 !opt->jo_modifiable[PART_OUT];
1233
1234 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1235 {
1236 EMSG(_(e_modifiable));
1237 }
1238 else
1239 {
1240 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001241 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001242 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001243 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001245 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001246
1247 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1248 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1249 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1250 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 buf_T *buf;
1252
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001253 /* writing err to a buffer. Default mode is NL. */
1254 if (!(opt->jo_set & JO_ERR_MODE))
1255 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1256 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001257 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001258 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001259 {
1260 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1261 if (buf == NULL)
1262 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1263 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001264 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001265 {
1266 int msg = TRUE;
1267
1268 if (opt->jo_set2 & JO2_ERR_MSG)
1269 msg = opt->jo_message[PART_ERR];
1270 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1271 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001272 if (buf != NULL)
1273 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001274 if (opt->jo_set & JO_ERR_MODIFIABLE)
1275 channel->ch_part[PART_ERR].ch_nomodifiable =
1276 !opt->jo_modifiable[PART_ERR];
1277 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1278 {
1279 EMSG(_(e_modifiable));
1280 }
1281 else
1282 {
1283 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001284 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001285 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001286 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001287 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001288 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001289
1290 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1291 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1292 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001293}
1294
1295/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001296 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001297 */
1298 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001299channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001300 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001301 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001302 char_u *callback,
1303 partial_T *partial,
1304 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001305{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001306 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001307 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1308
1309 if (item != NULL)
1310 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001311 item->cq_partial = partial;
1312 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001313 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001314 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001315 item->cq_callback = callback;
1316 }
1317 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001318 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001319 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001320 func_ref(item->cq_callback);
1321 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001322 item->cq_seq_nr = id;
1323 item->cq_prev = head->cq_prev;
1324 head->cq_prev = item;
1325 item->cq_next = NULL;
1326 if (item->cq_prev == NULL)
1327 head->cq_next = item;
1328 else
1329 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001330 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001331}
1332
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001333 static void
1334write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1335{
1336 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001337 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001338 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001339 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001340
Bram Moolenaar655da312016-05-28 22:22:34 +02001341 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001342 if ((p = alloc(len + 2)) == NULL)
1343 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001344 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001345
1346 for (i = 0; i < len; ++i)
1347 if (p[i] == NL)
1348 p[i] = NUL;
1349
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001350 p[len] = NL;
1351 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001352 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001353 vim_free(p);
1354}
1355
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001356/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001357 * Return TRUE if "channel" can be written to.
1358 * Returns FALSE if the input is closed or the write would block.
1359 */
1360 static int
1361can_write_buf_line(channel_T *channel)
1362{
1363 chanpart_T *in_part = &channel->ch_part[PART_IN];
1364
1365 if (in_part->ch_fd == INVALID_FD)
1366 return FALSE; /* pipe was closed */
1367
1368 /* for testing: block every other attempt to write */
1369 if (in_part->ch_block_write == 1)
1370 in_part->ch_block_write = -1;
1371 else if (in_part->ch_block_write == -1)
1372 in_part->ch_block_write = 1;
1373
1374 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1375#ifndef WIN32
1376 {
1377# if defined(HAVE_SELECT)
1378 struct timeval tval;
1379 fd_set wfds;
1380 int ret;
1381
1382 FD_ZERO(&wfds);
1383 FD_SET((int)in_part->ch_fd, &wfds);
1384 tval.tv_sec = 0;
1385 tval.tv_usec = 0;
1386 for (;;)
1387 {
1388 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1389# ifdef EINTR
1390 SOCK_ERRNO;
1391 if (ret == -1 && errno == EINTR)
1392 continue;
1393# endif
1394 if (ret <= 0 || in_part->ch_block_write == 1)
1395 {
1396 if (ret > 0)
1397 ch_log(channel, "FAKED Input not ready for writing");
1398 else
1399 ch_log(channel, "Input not ready for writing");
1400 return FALSE;
1401 }
1402 break;
1403 }
1404# else
1405 struct pollfd fds;
1406
1407 fds.fd = in_part->ch_fd;
1408 fds.events = POLLOUT;
1409 if (poll(&fds, 1, 0) <= 0)
1410 {
1411 ch_log(channel, "Input not ready for writing");
1412 return FALSE;
1413 }
1414 if (in_part->ch_block_write == 1)
1415 {
1416 ch_log(channel, "FAKED Input not ready for writing");
1417 return FALSE;
1418 }
1419# endif
1420 }
1421#endif
1422 return TRUE;
1423}
1424
1425/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001426 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001427 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001428 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001429channel_write_in(channel_T *channel)
1430{
1431 chanpart_T *in_part = &channel->ch_part[PART_IN];
1432 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001433 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001434 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001435
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001436 if (buf == NULL || in_part->ch_buf_append)
1437 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001438 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001439 {
1440 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001441 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001442 return;
1443 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001444
1445 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1446 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1447 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001448 if (!can_write_buf_line(channel))
1449 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001450 write_buf_line(buf, lnum, channel);
1451 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001452 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001453
1454 if (written == 1)
1455 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1456 else if (written > 1)
1457 ch_logn(channel, "written %d lines to channel", written);
1458
Bram Moolenaar014069a2016-03-03 22:51:40 +01001459 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001460 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001461 {
1462 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001463 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001464 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001465
1466 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001467 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 }
1469 else
1470 ch_logn(channel, "Still %d more lines to write",
1471 buf->b_ml.ml_line_count - lnum + 1);
1472}
1473
1474/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001475 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001476 */
1477 void
1478channel_buffer_free(buf_T *buf)
1479{
1480 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001481 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001482
1483 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001484 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001485 {
1486 chanpart_T *ch_part = &channel->ch_part[part];
1487
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001488 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001489 {
1490 ch_logs(channel, "%s buffer has been wiped out",
1491 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001492 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001493 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001494 }
1495}
1496
1497/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001498 * Write any lines waiting to be written to a channel.
1499 */
1500 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001501channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001502{
1503 channel_T *channel;
1504
1505 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1506 {
1507 chanpart_T *in_part = &channel->ch_part[PART_IN];
1508
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001509 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001510 {
1511 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001512 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001513 else
1514 channel_write_in(channel);
1515 }
1516 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001517}
1518
1519/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001520 * Write appended lines above the last one in "buf" to the channel.
1521 */
1522 void
1523channel_write_new_lines(buf_T *buf)
1524{
1525 channel_T *channel;
1526 int found_one = FALSE;
1527
1528 /* There could be more than one channel for the buffer, loop over all of
1529 * them. */
1530 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1531 {
1532 chanpart_T *in_part = &channel->ch_part[PART_IN];
1533 linenr_T lnum;
1534 int written = 0;
1535
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001536 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001537 {
1538 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001539 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001540 found_one = TRUE;
1541 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1542 ++lnum)
1543 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001544 if (!can_write_buf_line(channel))
1545 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001546 write_buf_line(buf, lnum, channel);
1547 ++written;
1548 }
1549
1550 if (written == 1)
1551 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1552 else if (written > 1)
1553 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001554 if (lnum < buf->b_ml.ml_line_count)
1555 ch_logn(channel, "Still %d more lines to write",
1556 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001557
1558 in_part->ch_buf_bot = lnum;
1559 }
1560 }
1561 if (!found_one)
1562 buf->b_write_to_channel = FALSE;
1563}
1564
1565/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001566 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001567 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001568 */
1569 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001570invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1571 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001572{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001573 typval_T rettv;
1574 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001575
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001576 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001577 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001578
Bram Moolenaar77073442016-02-13 23:23:53 +01001579 argv[0].v_type = VAR_CHANNEL;
1580 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001581
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001582 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1583 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001584 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001585 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001586}
1587
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001588/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001589 * Return the first node from "channel"/"part" without removing it.
1590 * Returns NULL if there is nothing.
1591 */
1592 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001593channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001594{
1595 readq_T *head = &channel->ch_part[part].ch_head;
1596
1597 return head->rq_next;
1598}
1599
1600/*
1601 * Return a pointer to the first NL in "node".
1602 * Skips over NUL characters.
1603 * Returns NULL if there is no NL.
1604 */
1605 char_u *
1606channel_first_nl(readq_T *node)
1607{
1608 char_u *buffer = node->rq_buffer;
1609 long_u i;
1610
1611 for (i = 0; i < node->rq_buflen; ++i)
1612 if (buffer[i] == NL)
1613 return buffer + i;
1614 return NULL;
1615}
1616
1617/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619 * The caller must free it.
1620 * Returns NULL if there is nothing.
1621 */
1622 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001623channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001625 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001627 char_u *p;
1628
Bram Moolenaar77073442016-02-13 23:23:53 +01001629 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001630 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001631 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001632 p = node->rq_buffer;
1633 head->rq_next = node->rq_next;
1634 if (node->rq_next == NULL)
1635 head->rq_prev = NULL;
1636 else
1637 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001638 vim_free(node);
1639 return p;
1640}
1641
1642/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001643 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001644 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001645 */
1646 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001647channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001648{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001649 readq_T *head = &channel->ch_part[part].ch_head;
1650 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001651 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001652 char_u *res;
1653 char_u *p;
1654
1655 /* If there is only one buffer just get that one. */
1656 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1657 return channel_get(channel, part);
1658
1659 /* Concatenate everything into one buffer. */
1660 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001661 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001662 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001663 if (res == NULL)
1664 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001665 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001666 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001667 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001668 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001669 p += node->rq_buflen;
1670 }
1671 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001672
1673 /* Free all buffers */
1674 do
1675 {
1676 p = channel_get(channel, part);
1677 vim_free(p);
1678 } while (p != NULL);
1679
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001680 /* turn all NUL into NL */
1681 while (len > 0)
1682 {
1683 --len;
1684 if (res[len] == NUL)
1685 res[len] = NL;
1686 }
1687
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001688 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689}
1690
1691/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001692 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001693 * Caller must check these bytes are available.
1694 */
1695 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001696channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001697{
1698 readq_T *head = &channel->ch_part[part].ch_head;
1699 readq_T *node = head->rq_next;
1700 char_u *buf = node->rq_buffer;
1701
1702 mch_memmove(buf, buf + len, node->rq_buflen - len);
1703 node->rq_buflen -= len;
1704}
1705
1706/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001708 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001710 */
1711 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001712channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001714 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001716 readq_T *last_node;
1717 readq_T *n;
1718 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001719 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001720 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721
Bram Moolenaar77073442016-02-13 23:23:53 +01001722 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001723 return FAIL;
1724
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 last_node = node->rq_next;
1726 len = node->rq_buflen + last_node->rq_buflen + 1;
1727 if (want_nl)
1728 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001729 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001730 {
1731 last_node = last_node->rq_next;
1732 len += last_node->rq_buflen;
1733 }
1734
1735 p = newbuf = alloc(len);
1736 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001737 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001738 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001739 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001740 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001741 node->rq_buffer = newbuf;
1742 for (n = node; n != last_node; )
1743 {
1744 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001745 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001746 p += n->rq_buflen;
1747 vim_free(n->rq_buffer);
1748 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001749 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001750
1751 /* dispose of the collapsed nodes and their buffers */
1752 for (n = node->rq_next; n != last_node; )
1753 {
1754 n = n->rq_next;
1755 vim_free(n->rq_prev);
1756 }
1757 node->rq_next = last_node->rq_next;
1758 if (last_node->rq_next == NULL)
1759 head->rq_prev = node;
1760 else
1761 last_node->rq_next->rq_prev = node;
1762 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001763 return OK;
1764}
1765
1766/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001768 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 * Returns OK or FAIL.
1770 */
1771 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001772channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001773 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001774{
1775 readq_T *node;
1776 readq_T *head = &channel->ch_part[part].ch_head;
1777 char_u *p;
1778 int i;
1779
1780 node = (readq_T *)alloc(sizeof(readq_T));
1781 if (node == NULL)
1782 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001783 /* A NUL is added at the end, because netbeans code expects that.
1784 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785 node->rq_buffer = alloc(len + 1);
1786 if (node->rq_buffer == NULL)
1787 {
1788 vim_free(node);
1789 return FAIL; /* out of memory */
1790 }
1791
1792 if (channel->ch_part[part].ch_mode == MODE_NL)
1793 {
1794 /* Drop any CR before a NL. */
1795 p = node->rq_buffer;
1796 for (i = 0; i < len; ++i)
1797 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1798 *p++ = buf[i];
1799 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001800 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001801 }
1802 else
1803 {
1804 mch_memmove(node->rq_buffer, buf, len);
1805 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001806 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001807 }
1808
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001809 if (prepend)
1810 {
1811 /* preend node to the head of the queue */
1812 node->rq_next = head->rq_next;
1813 node->rq_prev = NULL;
1814 if (head->rq_next == NULL)
1815 head->rq_prev = node;
1816 else
1817 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001818 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001819 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001820 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001821 {
1822 /* append node to the tail of the queue */
1823 node->rq_next = NULL;
1824 node->rq_prev = head->rq_prev;
1825 if (head->rq_prev == NULL)
1826 head->rq_next = node;
1827 else
1828 head->rq_prev->rq_next = node;
1829 head->rq_prev = node;
1830 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001831
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001832 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001833 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001834 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001835 fprintf(log_fd, "'");
1836 if (fwrite(buf, len, 1, log_fd) != 1)
1837 return FAIL;
1838 fprintf(log_fd, "'\n");
1839 }
1840 return OK;
1841}
1842
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001843/*
1844 * Try to fill the buffer of "reader".
1845 * Returns FALSE when nothing was added.
1846 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001847 static int
1848channel_fill(js_read_T *reader)
1849{
1850 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001851 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001852 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001853 int keeplen;
1854 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001855 char_u *p;
1856
1857 if (next == NULL)
1858 return FALSE;
1859
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001860 keeplen = reader->js_end - reader->js_buf;
1861 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 {
1863 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001864 addlen = (int)STRLEN(next);
1865 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001866 if (p == NULL)
1867 {
1868 vim_free(next);
1869 return FALSE;
1870 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001871 mch_memmove(p, reader->js_buf, keeplen);
1872 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001873 vim_free(next);
1874 next = p;
1875 }
1876
1877 vim_free(reader->js_buf);
1878 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001879 return TRUE;
1880}
1881
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001882/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001883 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001884 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001885 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001886 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001887 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001888channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001889{
1890 js_read_T reader;
1891 typval_T listtv;
1892 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001893 chanpart_T *chanpart = &channel->ch_part[part];
1894 jsonq_T *head = &chanpart->ch_json_head;
1895 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001896 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001897
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001898 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001899 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001900
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001901 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001902 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001903 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001904 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001905 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001906
1907 /* When a message is incomplete we wait for a short while for more to
1908 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001909 * or list will make us hang.
1910 * Do not generate error messages, they will be written in a channel log. */
1911 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001912 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001913 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001914 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001915 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001916 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001917 /* Only accept the response when it is a list with at least two
1918 * items. */
1919 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001920 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001921 if (listtv.v_type != VAR_LIST)
1922 ch_error(channel, "Did not receive a list, discarding");
1923 else
1924 ch_errorn(channel, "Expected list with two items, got %d",
1925 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001926 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001927 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001928 else
1929 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001930 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1931 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001932 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001933 else
1934 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001935 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001936 item->jq_value = alloc_tv();
1937 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001938 {
1939 vim_free(item);
1940 clear_tv(&listtv);
1941 }
1942 else
1943 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001944 *item->jq_value = listtv;
1945 item->jq_prev = head->jq_prev;
1946 head->jq_prev = item;
1947 item->jq_next = NULL;
1948 if (item->jq_prev == NULL)
1949 head->jq_next = item;
1950 else
1951 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001952 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001953 }
1954 }
1955 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001956
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001957 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001958 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001959 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001960 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001961 size_t buflen = STRLEN(reader.js_buf);
1962
1963 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001964 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001965 /* First time encountering incomplete message or after receiving
1966 * more (but still incomplete): set a deadline of 100 msec. */
1967 ch_logn(channel,
1968 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001969 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001970 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001971 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001972#ifdef WIN32
1973 chanpart->ch_deadline = GetTickCount() + 100L;
1974#else
1975 gettimeofday(&chanpart->ch_deadline, NULL);
1976 chanpart->ch_deadline.tv_usec += 100 * 1000;
1977 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1978 {
1979 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1980 ++chanpart->ch_deadline.tv_sec;
1981 }
1982#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001983 }
1984 else
1985 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001986 int timeout;
1987#ifdef WIN32
1988 timeout = GetTickCount() > chanpart->ch_deadline;
1989#else
1990 {
1991 struct timeval now_tv;
1992
1993 gettimeofday(&now_tv, NULL);
1994 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1995 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1996 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1997 }
1998#endif
1999 if (timeout)
2000 {
2001 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002002 chanpart->ch_wait_len = 0;
2003 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002004 }
2005 else
2006 {
2007 reader.js_used = 0;
2008 ch_log(channel, "still waiting on incomplete message");
2009 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002010 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002011 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002012
2013 if (status == FAIL)
2014 {
2015 ch_error(channel, "Decoding failed - discarding input");
2016 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002017 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002018 }
2019 else if (reader.js_buf[reader.js_used] != NUL)
2020 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002021 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002022 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002023 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2024 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002025 ret = status == MAYBE ? FALSE: TRUE;
2026 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002027 else
2028 ret = FALSE;
2029
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002030 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002031 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002032}
2033
2034/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002035 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002036 */
2037 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002038remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002039{
Bram Moolenaar77073442016-02-13 23:23:53 +01002040 if (node->cq_prev == NULL)
2041 head->cq_next = node->cq_next;
2042 else
2043 node->cq_prev->cq_next = node->cq_next;
2044 if (node->cq_next == NULL)
2045 head->cq_prev = node->cq_prev;
2046 else
2047 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002048}
2049
2050/*
2051 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002052 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002053 */
2054 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002055remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002056{
Bram Moolenaar77073442016-02-13 23:23:53 +01002057 if (node->jq_prev == NULL)
2058 head->jq_next = node->jq_next;
2059 else
2060 node->jq_prev->jq_next = node->jq_next;
2061 if (node->jq_next == NULL)
2062 head->jq_prev = node->jq_prev;
2063 else
2064 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002065 vim_free(node);
2066}
2067
2068/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002069 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002070 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002071 * When "id" is zero or negative jut get the first message. But not the one
2072 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002073 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002074 * Return OK when found and return the value in "rettv".
2075 * Return FAIL otherwise.
2076 */
2077 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002078channel_get_json(
2079 channel_T *channel,
2080 ch_part_T part,
2081 int id,
2082 int without_callback,
2083 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002084{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002085 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002086 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002087
Bram Moolenaar77073442016-02-13 23:23:53 +01002088 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002089 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002090 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002091 typval_T *tv = &l->lv_first->li_tv;
2092
Bram Moolenaar958dc692016-12-01 15:34:12 +01002093 if ((without_callback || !item->jq_no_callback)
2094 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002095 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002096 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002097 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002098 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002099 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002100 if (tv->v_type == VAR_NUMBER)
2101 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002102 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002103 return OK;
2104 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002105 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002106 }
2107 return FAIL;
2108}
2109
Bram Moolenaar958dc692016-12-01 15:34:12 +01002110/*
2111 * Put back "rettv" into the JSON queue, there was no callback for it.
2112 * Takes over the values in "rettv".
2113 */
2114 static void
2115channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2116{
2117 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2118 jsonq_T *item = head->jq_next;
2119 jsonq_T *newitem;
2120
2121 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2122 /* last item was pushed back, append to the end */
2123 item = NULL;
2124 else while (item != NULL && item->jq_no_callback)
2125 /* append after the last item that was pushed back */
2126 item = item->jq_next;
2127
2128 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2129 if (newitem == NULL)
2130 clear_tv(rettv);
2131 else
2132 {
2133 newitem->jq_value = alloc_tv();
2134 if (newitem->jq_value == NULL)
2135 {
2136 vim_free(newitem);
2137 clear_tv(rettv);
2138 }
2139 else
2140 {
2141 newitem->jq_no_callback = FALSE;
2142 *newitem->jq_value = *rettv;
2143 if (item == NULL)
2144 {
2145 /* append to the end */
2146 newitem->jq_prev = head->jq_prev;
2147 head->jq_prev = newitem;
2148 newitem->jq_next = NULL;
2149 if (newitem->jq_prev == NULL)
2150 head->jq_next = newitem;
2151 else
2152 newitem->jq_prev->jq_next = newitem;
2153 }
2154 else
2155 {
2156 /* append after "item" */
2157 newitem->jq_prev = item;
2158 newitem->jq_next = item->jq_next;
2159 item->jq_next = newitem;
2160 if (newitem->jq_next == NULL)
2161 head->jq_prev = newitem;
2162 else
2163 newitem->jq_next->jq_prev = newitem;
2164 }
2165 }
2166 }
2167}
2168
Bram Moolenaarece61b02016-02-20 21:39:05 +01002169#define CH_JSON_MAX_ARGS 4
2170
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002171/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002172 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002173 * "argv[0]" is the command string.
2174 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002175 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002176 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002177channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002178{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002179 char_u *cmd = argv[0].vval.v_string;
2180 char_u *arg;
2181 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002182
Bram Moolenaarece61b02016-02-20 21:39:05 +01002183 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002184 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002185 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002186 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002187 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002188 return;
2189 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002190 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002191 if (arg == NULL)
2192 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002193
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194 if (STRCMP(cmd, "ex") == 0)
2195 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002196 int save_called_emsg = called_emsg;
2197
2198 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002199 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002200 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002201 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002202 --emsg_silent;
2203 if (called_emsg)
2204 ch_logs(channel, "Ex command error: '%s'",
2205 (char *)get_vim_var_str(VV_ERRMSG));
2206 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002207 }
2208 else if (STRCMP(cmd, "normal") == 0)
2209 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002210 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002211
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002212 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002213 ea.arg = arg;
2214 ea.addr_count = 0;
2215 ea.forceit = TRUE; /* no mapping */
2216 ex_normal(&ea);
2217 }
2218 else if (STRCMP(cmd, "redraw") == 0)
2219 {
2220 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002221
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002222 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002223 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002224 ex_redraw(&ea);
2225 showruler(FALSE);
2226 setcursor();
2227 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002228#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002229 if (gui.in_use)
2230 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002231 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002232 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002233 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002234#endif
2235 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002236 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002237 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002238 int is_call = cmd[0] == 'c';
2239 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002240
Bram Moolenaarece61b02016-02-20 21:39:05 +01002241 if (argv[id_idx].v_type != VAR_UNKNOWN
2242 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002243 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002244 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002245 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002246 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002247 }
2248 else if (is_call && argv[2].v_type != VAR_LIST)
2249 {
2250 ch_error(channel, "third argument for call must be a list");
2251 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002252 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002253 }
2254 else
2255 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002256 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002257 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002258 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002259 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002260
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002261 /* Don't pollute the display with errors. */
2262 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002263 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002264 {
2265 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002266 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002267 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002268 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002269 {
2270 ch_logs(channel, "Calling '%s'", (char *)arg);
2271 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2272 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002273 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002274
2275 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002276 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 int id = argv[id_idx].vval.v_number;
2278
Bram Moolenaar55fab432016-02-07 16:53:13 +01002279 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002280 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002281 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002282 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002283 /* If evaluation failed or the result can't be encoded
2284 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002285 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002286 err_tv.v_type = VAR_STRING;
2287 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002288 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002289 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002290 if (json != NULL)
2291 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002292 channel_send(channel,
2293 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002294 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002295 vim_free(json);
2296 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002297 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002298 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002299 if (tv == &res_tv)
2300 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002301 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002302 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002303 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002304 }
2305 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002306 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002307 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002308 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002309 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002310}
2311
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002312/*
2313 * Invoke the callback at "cbhead".
2314 * Does not redraw but sets channel_need_redraw.
2315 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002316 static void
2317invoke_one_time_callback(
2318 channel_T *channel,
2319 cbq_T *cbhead,
2320 cbq_T *item,
2321 typval_T *argv)
2322{
2323 ch_logs(channel, "Invoking one-time callback %s",
2324 (char *)item->cq_callback);
2325 /* Remove the item from the list first, if the callback
2326 * invokes ch_close() the list will be cleared. */
2327 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002328 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002329 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002330 vim_free(item);
2331}
2332
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002333 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002334append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002335{
2336 buf_T *save_curbuf = curbuf;
2337 linenr_T lnum = buffer->b_ml.ml_line_count;
2338 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002339 chanpart_T *ch_part = &channel->ch_part[part];
2340 int save_p_ma = buffer->b_p_ma;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002341 int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002342
2343 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2344 {
2345 if (!ch_part->ch_nomod_error)
2346 {
2347 ch_error(channel, "Buffer is not modifiable, cannot append");
2348 ch_part->ch_nomod_error = TRUE;
2349 }
2350 return;
2351 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002352
2353 /* If the buffer is also used as input insert above the last
2354 * line. Don't write these lines. */
2355 if (save_write_to)
2356 {
2357 --lnum;
2358 buffer->b_write_to_channel = FALSE;
2359 }
2360
2361 /* Append to the buffer */
2362 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2363
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002364 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002365 curbuf = buffer;
2366 u_sync(TRUE);
2367 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002368 ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002369
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002370 if (empty)
2371 {
2372 /* The buffer is empty, replace the first (dummy) line. */
2373 ml_replace(lnum, msg, TRUE);
2374 lnum = 0;
2375 }
2376 else
2377 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002378 appended_lines_mark(lnum, 1L);
2379 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002380 if (ch_part->ch_nomodifiable)
2381 buffer->b_p_ma = FALSE;
2382 else
2383 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002384
2385 if (buffer->b_nwindows > 0)
2386 {
2387 win_T *wp;
2388 win_T *save_curwin;
2389
2390 FOR_ALL_WINDOWS(wp)
2391 {
2392 if (wp->w_buffer == buffer
2393 && (save_write_to
2394 ? wp->w_cursor.lnum == lnum + 1
2395 : (wp->w_cursor.lnum == lnum
2396 && wp->w_cursor.col == 0)))
2397 {
2398 ++wp->w_cursor.lnum;
2399 save_curwin = curwin;
2400 curwin = wp;
2401 curbuf = curwin->w_buffer;
2402 scroll_cursor_bot(0, FALSE);
2403 curwin = save_curwin;
2404 curbuf = curwin->w_buffer;
2405 }
2406 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002407 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002408 channel_need_redraw = TRUE;
2409 }
2410
2411 if (save_write_to)
2412 {
2413 channel_T *ch;
2414
2415 /* Find channels reading from this buffer and adjust their
2416 * next-to-read line number. */
2417 buffer->b_write_to_channel = TRUE;
2418 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2419 {
2420 chanpart_T *in_part = &ch->ch_part[PART_IN];
2421
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002422 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002423 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2424 }
2425 }
2426}
2427
Bram Moolenaar437905c2016-04-26 19:01:05 +02002428 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002429drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002430{
2431 char_u *msg;
2432
2433 while ((msg = channel_get(channel, part)) != NULL)
2434 {
2435 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2436 vim_free(msg);
2437 }
2438}
2439
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002440/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002441 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002442 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002443 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002444 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002445 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002446may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002447{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002448 char_u *msg = NULL;
2449 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002450 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002451 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002452 chanpart_T *ch_part = &channel->ch_part[part];
2453 ch_mode_T ch_mode = ch_part->ch_mode;
2454 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002455 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002456 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002457 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002458 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002459 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002460
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002461 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002462 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002463 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002464
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002465 /* Use a message-specific callback, part callback or channel callback */
2466 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2467 if (cbitem->cq_seq_nr == 0)
2468 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002469 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002470 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002471 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002472 partial = cbitem->cq_partial;
2473 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002474 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002475 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002476 callback = ch_part->ch_callback;
2477 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002478 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002479 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002480 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002481 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002482 partial = channel->ch_partial;
2483 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002484
Bram Moolenaarec68a992016-10-03 21:37:41 +02002485 buffer = ch_part->ch_bufref.br_buf;
2486 if (buffer != NULL && !bufref_valid(&ch_part->ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002487 {
2488 /* buffer was wiped out */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002489 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002490 buffer = NULL;
2491 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002492
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002493 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002494 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002495 listitem_T *item;
2496 int argc = 0;
2497
Bram Moolenaard7ece102016-02-02 23:23:02 +01002498 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002499 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002500 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002501 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002502 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002503 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002504 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002505 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002506
Bram Moolenaarece61b02016-02-20 21:39:05 +01002507 for (item = listtv->vval.v_list->lv_first;
2508 item != NULL && argc < CH_JSON_MAX_ARGS;
2509 item = item->li_next)
2510 argv[argc++] = item->li_tv;
2511 while (argc < CH_JSON_MAX_ARGS)
2512 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002513
Bram Moolenaarece61b02016-02-20 21:39:05 +01002514 if (argv[0].v_type == VAR_STRING)
2515 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002516 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002517 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002518 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002519 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002520 }
2521
Bram Moolenaarece61b02016-02-20 21:39:05 +01002522 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002523 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002524 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002525 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002526 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002527 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002528 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002529 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002530 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002532 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002533 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002534 return FALSE;
2535 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002536 else
2537 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002538 /* If there is no callback or buffer drop the message. */
2539 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002540 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002541 /* If there is a close callback it may use ch_read() to get the
2542 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002543 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002544 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002545 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002546 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002547
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002548 if (ch_mode == MODE_NL)
2549 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002550 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002551 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002552 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002553
2554 /* See if we have a message ending in NL in the first buffer. If
2555 * not try to concatenate the first and the second buffer. */
2556 while (TRUE)
2557 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002558 node = channel_peek(channel, part);
2559 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002560 if (nl != NULL)
2561 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002562 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002563 {
2564 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2565 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002566 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002567 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002568 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002569 buf = node->rq_buffer;
2570
Bram Moolenaarec68a992016-10-03 21:37:41 +02002571 if (nl == NULL)
2572 {
2573 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002574 char_u *new_buf;
2575
2576 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2577 if (new_buf == NULL)
2578 /* This might fail over and over again, should the message
2579 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002580 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002581 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002582 node->rq_buffer = buf;
2583 nl = buf + node->rq_buflen++;
2584 *nl = NUL;
2585 }
2586
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002587 /* Convert NUL to NL, the internal representation. */
2588 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2589 if (*p == NUL)
2590 *p = NL;
2591
2592 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002593 {
2594 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002595 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002596 *nl = NUL;
2597 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002598 else
2599 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002600 /* Copy the message into allocated memory (excluding the NL)
2601 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002602 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002603 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002604 }
2605 }
2606 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002607 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002608 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002609 * get everything we have.
2610 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002611 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002612 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002613
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002614 if (msg == NULL)
2615 return FALSE; /* out of memory (and avoids Coverity warning) */
2616
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002617 argv[1].v_type = VAR_STRING;
2618 argv[1].vval.v_string = msg;
2619 }
2620
Bram Moolenaara07fec92016-02-05 21:04:08 +01002621 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002622 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002623 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002624
Bram Moolenaar958dc692016-12-01 15:34:12 +01002625 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002626 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002627 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002628 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002629 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002630 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002631 break;
2632 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002633 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002634 {
2635 if (channel->ch_drop_never)
2636 {
2637 /* message must be read with ch_read() */
2638 channel_push_json(channel, part, listtv);
2639 listtv = NULL;
2640 }
2641 else
2642 ch_logn(channel, "Dropping message %d without callback",
2643 seq_nr);
2644 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002645 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002646 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002647 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002648 if (buffer != NULL)
2649 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002650 if (msg == NULL)
2651 /* JSON or JS mode: re-encode the message. */
2652 msg = json_encode(listtv, ch_mode);
2653 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002654 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002655 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002656
Bram Moolenaar187db502016-02-27 14:44:26 +01002657 if (callback != NULL)
2658 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002659 if (cbitem != NULL)
2660 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2661 else
2662 {
2663 /* invoke the channel callback */
2664 ch_logs(channel, "Invoking channel callback %s",
2665 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002666 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002667 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002668 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002669 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002670 else
Bram Moolenaar958dc692016-12-01 15:34:12 +01002671 ch_logn(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002672
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002673 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002674 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002675 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002676
2677 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002678}
2679
2680/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002681 * Return TRUE when channel "channel" is open for writing to.
2682 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002683 */
2684 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002685channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002686{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002687 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002688 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002689}
2690
2691/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002692 * Return TRUE when channel "channel" is open for reading or writing.
2693 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002694 */
2695 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002696channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002697{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002698 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002699 || channel->CH_IN_FD != INVALID_FD
2700 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002701 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002702}
2703
2704/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002705 * Return TRUE if "channel" has JSON or other typeahead.
2706 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002707 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002708channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002709{
2710 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2711
2712 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2713 {
2714 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2715 jsonq_T *item = head->jq_next;
2716
2717 return item != NULL;
2718 }
2719 return channel_peek(channel, part) != NULL;
2720}
2721
2722/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002723 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002724 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002725 */
2726 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002727channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002728{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002729 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002730 int has_readahead = FALSE;
2731
Bram Moolenaar77073442016-02-13 23:23:53 +01002732 if (channel == NULL)
2733 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002734 if (req_part == PART_OUT)
2735 {
2736 if (channel->CH_OUT_FD != INVALID_FD)
2737 return "open";
2738 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002739 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002740 }
2741 else if (req_part == PART_ERR)
2742 {
2743 if (channel->CH_ERR_FD != INVALID_FD)
2744 return "open";
2745 if (channel_has_readahead(channel, PART_ERR))
2746 has_readahead = TRUE;
2747 }
2748 else
2749 {
2750 if (channel_is_open(channel))
2751 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002752 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002753 if (channel_has_readahead(channel, part))
2754 {
2755 has_readahead = TRUE;
2756 break;
2757 }
2758 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002759
2760 if (has_readahead)
2761 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002762 return "closed";
2763}
2764
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002765 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002766channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002767{
2768 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002769 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002770 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002771 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002772 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002773
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002774 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002775 STRCAT(namebuf, "_");
2776 tail = STRLEN(namebuf);
2777
2778 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002779 if (chanpart->ch_fd != INVALID_FD)
2780 status = "open";
2781 else if (channel_has_readahead(channel, part))
2782 status = "buffered";
2783 else
2784 status = "closed";
2785 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002786
2787 STRCPY(namebuf + tail, "mode");
2788 switch (chanpart->ch_mode)
2789 {
2790 case MODE_NL: s = "NL"; break;
2791 case MODE_RAW: s = "RAW"; break;
2792 case MODE_JSON: s = "JSON"; break;
2793 case MODE_JS: s = "JS"; break;
2794 }
2795 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2796
2797 STRCPY(namebuf + tail, "io");
2798 if (part == PART_SOCK)
2799 s = "socket";
2800 else switch (chanpart->ch_io)
2801 {
2802 case JIO_NULL: s = "null"; break;
2803 case JIO_PIPE: s = "pipe"; break;
2804 case JIO_FILE: s = "file"; break;
2805 case JIO_BUFFER: s = "buffer"; break;
2806 case JIO_OUT: s = "out"; break;
2807 }
2808 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2809
2810 STRCPY(namebuf + tail, "timeout");
2811 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2812}
2813
2814 void
2815channel_info(channel_T *channel, dict_T *dict)
2816{
2817 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002818 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002819
2820 if (channel->ch_hostname != NULL)
2821 {
2822 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2823 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2824 channel_part_info(channel, dict, "sock", PART_SOCK);
2825 }
2826 else
2827 {
2828 channel_part_info(channel, dict, "out", PART_OUT);
2829 channel_part_info(channel, dict, "err", PART_ERR);
2830 channel_part_info(channel, dict, "in", PART_IN);
2831 }
2832}
2833
Bram Moolenaar77073442016-02-13 23:23:53 +01002834/*
2835 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002836 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002837 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002838 */
2839 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002840channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002841{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002842 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002843
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002844#ifdef FEAT_GUI
2845 channel_gui_unregister(channel);
2846#endif
2847
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002848 ch_close_part(channel, PART_SOCK);
2849 ch_close_part(channel, PART_IN);
2850 ch_close_part(channel, PART_OUT);
2851 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002852
Bram Moolenaar8b374212016-02-24 20:43:06 +01002853 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002854 {
2855 typval_T argv[1];
2856 typval_T rettv;
2857 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002858 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002859
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002860 /* Invoke callbacks before the close callback, since it's weird to
2861 * first invoke the close callback. Increment the refcount to avoid
2862 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002863 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002864 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002865 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002866 while (may_invoke_callback(channel, part))
2867 ;
2868
2869 /* Invoke the close callback, if still set. */
2870 if (channel->ch_close_cb != NULL)
2871 {
2872 ch_logs(channel, "Invoking close callback %s",
2873 (char *)channel->ch_close_cb);
2874 argv[0].v_type = VAR_CHANNEL;
2875 argv[0].vval.v_channel = channel;
2876 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002877 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002878 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002879 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002880 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002881 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002882
2883 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002884 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002885 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002886 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002887
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002888 --channel->ch_refcount;
2889
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002890 if (channel_need_redraw)
2891 {
2892 channel_need_redraw = FALSE;
2893 redraw_after_callback();
2894 }
2895
Bram Moolenaar958dc692016-12-01 15:34:12 +01002896 if (!channel->ch_drop_never)
2897 /* any remaining messages are useless now */
2898 for (part = PART_SOCK; part < PART_IN; ++part)
2899 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002900 }
2901
2902 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002903}
2904
Bram Moolenaard04a0202016-01-26 23:30:18 +01002905/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002906 * Close the "in" part channel "channel".
2907 */
2908 void
2909channel_close_in(channel_T *channel)
2910{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002911 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002912}
2913
2914/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002915 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002916 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002917 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002918channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002919{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002920 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2921 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002922
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002923 while (channel_peek(channel, part) != NULL)
2924 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002925
2926 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002927 {
2928 cbq_T *node = cb_head->cq_next;
2929
2930 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002931 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002932 vim_free(node);
2933 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002934
2935 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002936 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002937 free_tv(json_head->jq_next->jq_value);
2938 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002939 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002940
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002941 free_callback(channel->ch_part[part].ch_callback,
2942 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002943 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002944 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002945}
2946
2947/*
2948 * Clear all the read buffers on "channel".
2949 */
2950 void
2951channel_clear(channel_T *channel)
2952{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002953 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002954 vim_free(channel->ch_hostname);
2955 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002956 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002957 channel_clear_one(channel, PART_OUT);
2958 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002959 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002960 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002961 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002962 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002963 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002964 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002965 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002966}
2967
Bram Moolenaar77073442016-02-13 23:23:53 +01002968#if defined(EXITFREE) || defined(PROTO)
2969 void
2970channel_free_all(void)
2971{
2972 channel_T *channel;
2973
Bram Moolenaard6051b52016-02-28 15:49:03 +01002974 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002975 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2976 channel_clear(channel);
2977}
2978#endif
2979
2980
Bram Moolenaar715d2852016-04-30 17:06:31 +02002981/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002982#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002983
2984/* Buffer size for reading incoming messages. */
2985#define MAXMSGSIZE 4096
2986
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002987#if defined(HAVE_SELECT)
2988/*
2989 * Add write fds where we are waiting for writing to be possible.
2990 */
2991 static int
2992channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2993{
2994 int maxfd = maxfd_arg;
2995 channel_T *ch;
2996
2997 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2998 {
2999 chanpart_T *in_part = &ch->ch_part[PART_IN];
3000
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003001 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003002 {
3003 FD_SET((int)in_part->ch_fd, wfds);
3004 if ((int)in_part->ch_fd >= maxfd)
3005 maxfd = (int)in_part->ch_fd + 1;
3006 }
3007 }
3008 return maxfd;
3009}
3010#else
3011/*
3012 * Add write fds where we are waiting for writing to be possible.
3013 */
3014 static int
3015channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3016{
3017 int nfd = nfd_in;
3018 channel_T *ch;
3019
3020 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3021 {
3022 chanpart_T *in_part = &ch->ch_part[PART_IN];
3023
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003024 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003025 {
3026 in_part->ch_poll_idx = nfd;
3027 fds[nfd].fd = in_part->ch_fd;
3028 fds[nfd].events = POLLOUT;
3029 ++nfd;
3030 }
3031 else
3032 in_part->ch_poll_idx = -1;
3033 }
3034 return nfd;
3035}
3036#endif
3037
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003038typedef enum {
3039 CW_READY,
3040 CW_NOT_READY,
3041 CW_ERROR
3042} channel_wait_result;
3043
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003045 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003046 * Return CW_READY when there is something to read.
3047 * Return CW_NOT_READY when there is nothing to read.
3048 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003049 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003050 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003051channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003052{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003053 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003054 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003055
Bram Moolenaard8070362016-02-15 21:56:54 +01003056# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003057 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003058 {
3059 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003060 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003061 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003062 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003063
3064 /* reading from a pipe, not a socket */
3065 while (TRUE)
3066 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003067 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3068
3069 if (r && nread > 0)
3070 return CW_READY;
3071 if (r == 0)
3072 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003073
3074 /* perhaps write some buffer lines */
3075 channel_write_any_lines();
3076
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003077 sleep_time = deadline - GetTickCount();
3078 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003079 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003080 /* Wait for a little while. Very short at first, up to 10 msec
3081 * after looping a few times. */
3082 if (sleep_time > delay)
3083 sleep_time = delay;
3084 Sleep(sleep_time);
3085 delay = delay * 2;
3086 if (delay > 10)
3087 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003088 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003089 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003090 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003091#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003092 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003093#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003094 struct timeval tval;
3095 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003096 fd_set wfds;
3097 int ret;
3098 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003099
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003100 tval.tv_sec = timeout / 1000;
3101 tval.tv_usec = (timeout % 1000) * 1000;
3102 for (;;)
3103 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003104 FD_ZERO(&rfds);
3105 FD_SET((int)fd, &rfds);
3106
3107 /* Write lines to a pipe when a pipe can be written to. Need to
3108 * set this every time, some buffers may be done. */
3109 maxfd = (int)fd + 1;
3110 FD_ZERO(&wfds);
3111 maxfd = channel_fill_wfds(maxfd, &wfds);
3112
3113 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003114# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003115 SOCK_ERRNO;
3116 if (ret == -1 && errno == EINTR)
3117 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003118# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003119 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003120 {
3121 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003122 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003123 channel_write_any_lines();
3124 continue;
3125 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003126 break;
3127 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003128#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003129 for (;;)
3130 {
3131 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3132 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003133
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003134 fds[0].fd = fd;
3135 fds[0].events = POLLIN;
3136 nfd = channel_fill_poll_write(nfd, fds);
3137 if (poll(fds, nfd, timeout) > 0)
3138 {
3139 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003140 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003141 channel_write_any_lines();
3142 continue;
3143 }
3144 break;
3145 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003146#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003147 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003148 return CW_NOT_READY;
3149}
3150
3151 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003152ch_close_part_on_error(
3153 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003154{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003155 char msgbuf[80];
3156
3157 vim_snprintf(msgbuf, sizeof(msgbuf),
3158 "%%s(): Read %s from ch_part[%d], closing",
3159 (is_err ? "error" : "EOF"), part);
3160
3161 if (is_err)
3162 /* Do not call emsg(), most likely the other end just exited. */
3163 ch_errors(channel, msgbuf, func);
3164 else
3165 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003166
3167 /* Queue a "DETACH" netbeans message in the command queue in order to
3168 * terminate the netbeans session later. Do not end the session here
3169 * directly as we may be running in the context of a call to
3170 * netbeans_parse_messages():
3171 * netbeans_parse_messages
3172 * -> autocmd triggered while processing the netbeans cmd
3173 * -> ui_breakcheck
3174 * -> gui event loop or select loop
3175 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003176 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003177 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003178 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003179 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003180 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3181
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003182 /* When reading is not possible close this part of the channel. Don't
3183 * close the channel yet, there may be something to read on another part. */
3184 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003185
3186#ifdef FEAT_GUI
3187 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003188 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003189#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003190}
3191
3192 static void
3193channel_close_now(channel_T *channel)
3194{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003195 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003196 if (channel->ch_nb_close_cb != NULL)
3197 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003198 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003199}
3200
3201/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003202 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003203 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003204 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003205 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003206 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003207channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003208{
3209 static char_u *buf = NULL;
3210 int len = 0;
3211 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003212 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003213 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003214
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003215 fd = channel->ch_part[part].ch_fd;
3216 if (fd == INVALID_FD)
3217 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003218 ch_errors(channel, "channel_read() called while %s part is closed",
3219 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003220 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003221 }
3222 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003223
3224 /* Allocate a buffer to read into. */
3225 if (buf == NULL)
3226 {
3227 buf = alloc(MAXMSGSIZE);
3228 if (buf == NULL)
3229 return; /* out of memory! */
3230 }
3231
3232 /* Keep on reading for as long as there is something to read.
3233 * Use select() or poll() to avoid blocking on a message that is exactly
3234 * MAXMSGSIZE long. */
3235 for (;;)
3236 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003237 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003238 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003239 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003240 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003241 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003242 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003243 if (len <= 0)
3244 break; /* error or nothing more to read */
3245
3246 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003247 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003248 readlen += len;
3249 if (len < MAXMSGSIZE)
3250 break; /* did read everything that's available */
3251 }
3252
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003253 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003254 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003255 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003256
3257#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003258 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003259 if (CH_HAS_GUI && gtk_main_level() > 0)
3260 gtk_main_quit();
3261#endif
3262}
3263
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003264/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003265 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003266 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003267 * Returns what was read in allocated memory.
3268 * Returns NULL in case of error or timeout.
3269 */
3270 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003271channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003272{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003273 char_u *buf;
3274 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003275 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003276 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003277 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003278 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003279
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003280 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003281 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003282
3283 while (TRUE)
3284 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003285 node = channel_peek(channel, part);
3286 if (node != NULL)
3287 {
3288 if (mode == MODE_RAW || (mode == MODE_NL
3289 && channel_first_nl(node) != NULL))
3290 /* got a complete message */
3291 break;
3292 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3293 continue;
3294 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003295
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003296 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003297 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003298 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003299 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003300 {
3301 ch_log(channel, "Timed out");
3302 return NULL;
3303 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003304 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003305 }
3306
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003307 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003308 if (mode == MODE_RAW)
3309 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003310 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003311 }
3312 else
3313 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003314 char_u *p;
3315
3316 buf = node->rq_buffer;
3317 nl = channel_first_nl(node);
3318
3319 /* Convert NUL to NL, the internal representation. */
3320 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3321 if (*p == NUL)
3322 *p = NL;
3323
3324 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003325 {
3326 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003327 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003328 *nl = NUL;
3329 }
3330 else
3331 {
3332 /* Copy the message into allocated memory and remove it from the
3333 * buffer. */
3334 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003335 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003336 }
3337 }
3338 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003339 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003340 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003341}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003342
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003343/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003344 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003345 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003346 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003347 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003348 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003349 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003350channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003351 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003352 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003353 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003354 int id,
3355 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003356{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003357 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003358 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003359 int timeout;
3360 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003361
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003362 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003363 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003364 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003365 for (;;)
3366 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003367 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003368
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003369 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003370 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003371 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003372 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003373 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003374 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003375
Bram Moolenaard7ece102016-02-02 23:23:02 +01003376 if (!more)
3377 {
3378 /* Handle any other messages in the queue. If done some more
3379 * messages may have arrived. */
3380 if (channel_parse_messages())
3381 continue;
3382
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003383 /* Wait for up to the timeout. If there was an incomplete message
3384 * use the deadline for that. */
3385 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003386 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003387 {
3388#ifdef WIN32
3389 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3390#else
3391 {
3392 struct timeval now_tv;
3393
3394 gettimeofday(&now_tv, NULL);
3395 timeout = (chanpart->ch_deadline.tv_sec
3396 - now_tv.tv_sec) * 1000
3397 + (chanpart->ch_deadline.tv_usec
3398 - now_tv.tv_usec) / 1000
3399 + 1;
3400 }
3401#endif
3402 if (timeout < 0)
3403 {
3404 /* Something went wrong, channel_parse_json() didn't
3405 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003406 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003407 timeout = timeout_arg;
3408 }
3409 else if (timeout > timeout_arg)
3410 timeout = timeout_arg;
3411 }
3412 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003413 if (fd == INVALID_FD
3414 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003415 {
3416 if (timeout == timeout_arg)
3417 {
3418 if (fd != INVALID_FD)
3419 ch_log(channel, "Timed out");
3420 break;
3421 }
3422 }
3423 else
3424 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003425 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003426 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003427 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003428 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003429}
3430
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003431/*
3432 * Common for ch_read() and ch_readraw().
3433 */
3434 void
3435common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3436{
3437 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003438 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003439 jobopt_T opt;
3440 int mode;
3441 int timeout;
3442 int id = -1;
3443 typval_T *listtv = NULL;
3444
3445 /* return an empty string by default */
3446 rettv->v_type = VAR_STRING;
3447 rettv->vval.v_string = NULL;
3448
3449 clear_job_options(&opt);
3450 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3451 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003452 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003453
Bram Moolenaar437905c2016-04-26 19:01:05 +02003454 if (opt.jo_set & JO_PART)
3455 part = opt.jo_part;
3456 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003457 if (channel != NULL)
3458 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003459 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003460 part = channel_part_read(channel);
3461 mode = channel_get_mode(channel, part);
3462 timeout = channel_get_timeout(channel, part);
3463 if (opt.jo_set & JO_TIMEOUT)
3464 timeout = opt.jo_timeout;
3465
3466 if (raw || mode == MODE_RAW || mode == MODE_NL)
3467 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3468 else
3469 {
3470 if (opt.jo_set & JO_ID)
3471 id = opt.jo_id;
3472 channel_read_json_block(channel, part, timeout, id, &listtv);
3473 if (listtv != NULL)
3474 {
3475 *rettv = *listtv;
3476 vim_free(listtv);
3477 }
3478 else
3479 {
3480 rettv->v_type = VAR_SPECIAL;
3481 rettv->vval.v_number = VVAL_NONE;
3482 }
3483 }
3484 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003485
3486theend:
3487 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003488}
3489
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003490# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3491 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003492/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003493 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003494 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003495 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003496 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003497channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003498{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003499 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003500 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003501
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003502 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003503 for (channel = first_channel; channel != NULL;
3504 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003505 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003506 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003507 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003508 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003509 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003510 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003511 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003512 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003513 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003514}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003515# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003516
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003517# if defined(WIN32) || defined(PROTO)
3518/*
3519 * Check the channels for anything that is ready to be read.
3520 * The data is put in the read queue.
3521 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003522 void
3523channel_handle_events(void)
3524{
3525 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003526 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003527 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003528
3529 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3530 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003531 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003532 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003533 {
3534 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003535 if (fd != INVALID_FD)
3536 {
3537 int r = channel_wait(channel, fd, 0);
3538
3539 if (r == CW_READY)
3540 channel_read(channel, part, "channel_handle_events");
3541 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003542 ch_close_part_on_error(channel, part, TRUE,
3543 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003544 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003545 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003546 }
3547}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003548# endif
3549
Bram Moolenaard04a0202016-01-26 23:30:18 +01003550/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003551 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003552 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003553 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003554 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003555 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003556channel_send(
3557 channel_T *channel,
3558 ch_part_T part,
3559 char_u *buf,
3560 int len,
3561 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003562{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003563 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003564 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003565
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003566 fd = channel->ch_part[part].ch_fd;
3567 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003568 {
3569 if (!channel->ch_error && fun != NULL)
3570 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003571 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003572 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003573 }
3574 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003575 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003576 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003577
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003578 if (log_fd != NULL)
3579 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003580 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003581 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003582 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003583 fprintf(log_fd, "'\n");
3584 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003585 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003586 }
3587
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003588 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003589 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003590 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003591 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003592 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003593 {
3594 if (!channel->ch_error && fun != NULL)
3595 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003596 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003597 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003598 }
3599 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003600 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003601 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003602
3603 channel->ch_error = FALSE;
3604 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003605}
3606
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003607/*
3608 * Common for "ch_sendexpr()" and "ch_sendraw()".
3609 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003610 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003611 * Otherwise returns NULL.
3612 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003613 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003614send_common(
3615 typval_T *argvars,
3616 char_u *text,
3617 int id,
3618 int eval,
3619 jobopt_T *opt,
3620 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003621 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003622{
3623 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003624 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003625
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003626 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003627 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003628 if (channel == NULL)
3629 return NULL;
3630 part_send = channel_part_send(channel);
3631 *part_read = channel_part_read(channel);
3632
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003633 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3634 return NULL;
3635
3636 /* Set the callback. An empty callback means no callback and not reading
3637 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3638 * allowed. */
3639 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3640 {
3641 if (eval)
3642 {
3643 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3644 return NULL;
3645 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003646 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003647 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003648 }
3649
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003650 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003651 && opt->jo_callback == NULL)
3652 return channel;
3653 return NULL;
3654}
3655
3656/*
3657 * common for "ch_evalexpr()" and "ch_sendexpr()"
3658 */
3659 void
3660ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3661{
3662 char_u *text;
3663 typval_T *listtv;
3664 channel_T *channel;
3665 int id;
3666 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003667 ch_part_T part_send;
3668 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003669 jobopt_T opt;
3670 int timeout;
3671
3672 /* return an empty string by default */
3673 rettv->v_type = VAR_STRING;
3674 rettv->vval.v_string = NULL;
3675
Bram Moolenaar437905c2016-04-26 19:01:05 +02003676 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003677 if (channel == NULL)
3678 return;
3679 part_send = channel_part_send(channel);
3680
3681 ch_mode = channel_get_mode(channel, part_send);
3682 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3683 {
3684 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3685 return;
3686 }
3687
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003688 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003689 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003690 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003691 if (text == NULL)
3692 return;
3693
3694 channel = send_common(argvars, text, id, eval, &opt,
3695 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3696 vim_free(text);
3697 if (channel != NULL && eval)
3698 {
3699 if (opt.jo_set & JO_TIMEOUT)
3700 timeout = opt.jo_timeout;
3701 else
3702 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003703 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3704 == OK)
3705 {
3706 list_T *list = listtv->vval.v_list;
3707
3708 /* Move the item from the list and then change the type to
3709 * avoid the value being freed. */
3710 *rettv = list->lv_last->li_tv;
3711 list->lv_last->li_tv.v_type = VAR_NUMBER;
3712 free_tv(listtv);
3713 }
3714 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003715 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003716}
3717
3718/*
3719 * common for "ch_evalraw()" and "ch_sendraw()"
3720 */
3721 void
3722ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3723{
3724 char_u buf[NUMBUFLEN];
3725 char_u *text;
3726 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003727 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003728 jobopt_T opt;
3729 int timeout;
3730
3731 /* return an empty string by default */
3732 rettv->v_type = VAR_STRING;
3733 rettv->vval.v_string = NULL;
3734
3735 text = get_tv_string_buf(&argvars[1], buf);
3736 channel = send_common(argvars, text, 0, eval, &opt,
3737 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3738 if (channel != NULL && eval)
3739 {
3740 if (opt.jo_set & JO_TIMEOUT)
3741 timeout = opt.jo_timeout;
3742 else
3743 timeout = channel_get_timeout(channel, part_read);
3744 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3745 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003746 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003747}
3748
Bram Moolenaard04a0202016-01-26 23:30:18 +01003749# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003750/*
3751 * Add open channels to the poll struct.
3752 * Return the adjusted struct index.
3753 * The type of "fds" is hidden to avoid problems with the function proto.
3754 */
3755 int
3756channel_poll_setup(int nfd_in, void *fds_in)
3757{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003758 int nfd = nfd_in;
3759 channel_T *channel;
3760 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003761 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003762
Bram Moolenaar77073442016-02-13 23:23:53 +01003763 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003764 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003765 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003766 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003767 chanpart_T *ch_part = &channel->ch_part[part];
3768
3769 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003770 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003771 ch_part->ch_poll_idx = nfd;
3772 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003773 fds[nfd].events = POLLIN;
3774 nfd++;
3775 }
3776 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003777 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003778 }
3779 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003780
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003781 nfd = channel_fill_poll_write(nfd, fds);
3782
Bram Moolenaare0874f82016-01-24 20:36:41 +01003783 return nfd;
3784}
3785
3786/*
3787 * The type of "fds" is hidden to avoid problems with the function proto.
3788 */
3789 int
3790channel_poll_check(int ret_in, void *fds_in)
3791{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003792 int ret = ret_in;
3793 channel_T *channel;
3794 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003795 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003796 int idx;
3797 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003798
Bram Moolenaar77073442016-02-13 23:23:53 +01003799 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003800 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003801 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003802 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003803 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003804
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003805 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003806 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003807 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003808 --ret;
3809 }
3810 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003811
3812 in_part = &channel->ch_part[PART_IN];
3813 idx = in_part->ch_poll_idx;
3814 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3815 {
3816 if (in_part->ch_buf_append)
3817 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003818 if (in_part->ch_bufref.br_buf != NULL)
3819 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003820 }
3821 else
3822 channel_write_in(channel);
3823 --ret;
3824 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003825 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003826
3827 return ret;
3828}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003829# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003830
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003831# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003832/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003833 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003834 */
3835 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003836channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003837{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003838 int maxfd = maxfd_in;
3839 channel_T *channel;
3840 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003841 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003842 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003843
Bram Moolenaar77073442016-02-13 23:23:53 +01003844 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003845 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003846 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003847 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003848 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003849
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003850 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003851 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003852 FD_SET((int)fd, rfds);
3853 if (maxfd < (int)fd)
3854 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003855 }
3856 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003857 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003858
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003859 maxfd = channel_fill_wfds(maxfd, wfds);
3860
Bram Moolenaare0874f82016-01-24 20:36:41 +01003861 return maxfd;
3862}
3863
3864/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003865 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003866 */
3867 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003868channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003869{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003870 int ret = ret_in;
3871 channel_T *channel;
3872 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003873 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003874 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003875 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003876
Bram Moolenaar77073442016-02-13 23:23:53 +01003877 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003878 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003879 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003880 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003881 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003882
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003883 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003884 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003885 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003886 --ret;
3887 }
3888 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003889
3890 in_part = &channel->ch_part[PART_IN];
3891 if (ret > 0 && in_part->ch_fd != INVALID_FD
3892 && FD_ISSET(in_part->ch_fd, wfds))
3893 {
3894 if (in_part->ch_buf_append)
3895 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003896 if (in_part->ch_bufref.br_buf != NULL)
3897 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003898 }
3899 else
3900 channel_write_in(channel);
3901 --ret;
3902 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003903 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003904
3905 return ret;
3906}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003907# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003908
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003909/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003910 * Execute queued up commands.
3911 * Invoked from the main loop when it's safe to execute received commands.
3912 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003913 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003914 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003915channel_parse_messages(void)
3916{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003917 channel_T *channel = first_channel;
3918 int ret = FALSE;
3919 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003920 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003921#ifdef ELAPSED_FUNC
3922 ELAPSED_TYPE start_tv;
3923
3924 ELAPSED_INIT(start_tv);
3925#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003926
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003927 ++safe_to_invoke_callback;
3928
Bram Moolenaard0b65022016-03-06 21:50:33 +01003929 /* Only do this message when another message was given, otherwise we get
3930 * lots of them. */
3931 if (did_log_msg)
3932 {
3933 ch_log(NULL, "looking for messages on channels");
3934 did_log_msg = FALSE;
3935 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003936 while (channel != NULL)
3937 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003938 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003939 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003940 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003941 channel_close_now(channel);
3942 /* channel may have been freed, start over */
3943 channel = first_channel;
3944 continue;
3945 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003946 if (channel->ch_to_be_freed)
3947 {
3948 channel_free(channel);
3949 /* channel has been freed, start over */
3950 channel = first_channel;
3951 continue;
3952 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003953 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003954 {
3955 /* channel is no longer useful, free it */
3956 channel_free(channel);
3957 channel = first_channel;
3958 part = PART_SOCK;
3959 continue;
3960 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003961 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003962 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003963 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003964 /* Increase the refcount, in case the handler causes the channel
3965 * to be unreferenced or closed. */
3966 ++channel->ch_refcount;
3967 r = may_invoke_callback(channel, part);
3968 if (r == OK)
3969 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003970 if (channel_unref(channel) || (r == OK
3971#ifdef ELAPSED_FUNC
3972 /* Limit the time we loop here to 100 msec, otherwise
3973 * Vim becomes unresponsive when the callback takes
3974 * more than a bit of time. */
3975 && ELAPSED_FUNC(start_tv) < 100L
3976#endif
3977 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003978 {
3979 /* channel was freed or something was done, start over */
3980 channel = first_channel;
3981 part = PART_SOCK;
3982 continue;
3983 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003984 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003985 if (part < PART_ERR)
3986 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003987 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003988 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003989 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003990 part = PART_SOCK;
3991 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003992 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003993
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003994 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003995 {
3996 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003997 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003998 }
3999
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004000 --safe_to_invoke_callback;
4001
Bram Moolenaard7ece102016-02-02 23:23:02 +01004002 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004003}
4004
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004005/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004006 * Return TRUE if any channel has readahead. That means we should not block on
4007 * waiting for input.
4008 */
4009 int
4010channel_any_readahead(void)
4011{
4012 channel_T *channel = first_channel;
4013 ch_part_T part = PART_SOCK;
4014
4015 while (channel != NULL)
4016 {
4017 if (channel_has_readahead(channel, part))
4018 return TRUE;
4019 if (part < PART_ERR)
4020 ++part;
4021 else
4022 {
4023 channel = channel->ch_next;
4024 part = PART_SOCK;
4025 }
4026 }
4027 return FALSE;
4028}
4029
4030/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004031 * Mark references to lists used in channels.
4032 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004033 int
4034set_ref_in_channel(int copyID)
4035{
Bram Moolenaar77073442016-02-13 23:23:53 +01004036 int abort = FALSE;
4037 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004038 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004039
Bram Moolenaar77073442016-02-13 23:23:53 +01004040 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004041 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004042 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004043 tv.v_type = VAR_CHANNEL;
4044 tv.vval.v_channel = channel;
4045 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004046 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004047 return abort;
4048}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004049
4050/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004051 * Return the "part" to write to for "channel".
4052 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004053 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004054channel_part_send(channel_T *channel)
4055{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004056 if (channel->CH_SOCK_FD == INVALID_FD)
4057 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004058 return PART_SOCK;
4059}
4060
4061/*
4062 * Return the default "part" to read from for "channel".
4063 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004064 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004065channel_part_read(channel_T *channel)
4066{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004067 if (channel->CH_SOCK_FD == INVALID_FD)
4068 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004069 return PART_SOCK;
4070}
4071
4072/*
4073 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004074 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004075 */
4076 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004077channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004078{
Bram Moolenaar77073442016-02-13 23:23:53 +01004079 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004080 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004081 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004082}
4083
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004084/*
4085 * Return the timeout of "channel"/"part"
4086 */
4087 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004088channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004089{
4090 return channel->ch_part[part].ch_timeout;
4091}
4092
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004093 static int
4094handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4095{
4096 char_u *val = get_tv_string(item);
4097
4098 opt->jo_set |= jo;
4099 if (STRCMP(val, "nl") == 0)
4100 *modep = MODE_NL;
4101 else if (STRCMP(val, "raw") == 0)
4102 *modep = MODE_RAW;
4103 else if (STRCMP(val, "js") == 0)
4104 *modep = MODE_JS;
4105 else if (STRCMP(val, "json") == 0)
4106 *modep = MODE_JSON;
4107 else
4108 {
4109 EMSG2(_(e_invarg2), val);
4110 return FAIL;
4111 }
4112 return OK;
4113}
4114
4115 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004116handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004117{
4118 char_u *val = get_tv_string(item);
4119
4120 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4121 if (STRCMP(val, "null") == 0)
4122 opt->jo_io[part] = JIO_NULL;
4123 else if (STRCMP(val, "pipe") == 0)
4124 opt->jo_io[part] = JIO_PIPE;
4125 else if (STRCMP(val, "file") == 0)
4126 opt->jo_io[part] = JIO_FILE;
4127 else if (STRCMP(val, "buffer") == 0)
4128 opt->jo_io[part] = JIO_BUFFER;
4129 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4130 opt->jo_io[part] = JIO_OUT;
4131 else
4132 {
4133 EMSG2(_(e_invarg2), val);
4134 return FAIL;
4135 }
4136 return OK;
4137}
4138
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004139/*
4140 * Clear a jobopt_T before using it.
4141 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004142 void
4143clear_job_options(jobopt_T *opt)
4144{
4145 vim_memset(opt, 0, sizeof(jobopt_T));
4146}
4147
4148/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004149 * Free any members of a jobopt_T.
4150 */
4151 void
4152free_job_options(jobopt_T *opt)
4153{
4154 if (opt->jo_partial != NULL)
4155 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 else if (opt->jo_callback != NULL)
4157 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004158 if (opt->jo_out_partial != NULL)
4159 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004160 else if (opt->jo_out_cb != NULL)
4161 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004162 if (opt->jo_err_partial != NULL)
4163 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004164 else if (opt->jo_err_cb != NULL)
4165 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004166 if (opt->jo_close_partial != NULL)
4167 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004168 else if (opt->jo_close_cb != NULL)
4169 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004170 if (opt->jo_exit_partial != NULL)
4171 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004172 else if (opt->jo_exit_cb != NULL)
4173 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004174}
4175
4176/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004177 * Get the PART_ number from the first character of an option name.
4178 */
4179 static int
4180part_from_char(int c)
4181{
4182 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4183}
4184
4185/*
4186 * Get the option entries from the dict in "tv", parse them and put the result
4187 * in "opt".
4188 * Only accept options in "supported".
4189 * If an option value is invalid return FAIL.
4190 */
4191 int
4192get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4193{
4194 typval_T *item;
4195 char_u *val;
4196 dict_T *dict;
4197 int todo;
4198 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004199 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200
4201 opt->jo_set = 0;
4202 if (tv->v_type == VAR_UNKNOWN)
4203 return OK;
4204 if (tv->v_type != VAR_DICT)
4205 {
4206 EMSG(_(e_invarg));
4207 return FAIL;
4208 }
4209 dict = tv->vval.v_dict;
4210 if (dict == NULL)
4211 return OK;
4212
4213 todo = (int)dict->dv_hashtab.ht_used;
4214 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4215 if (!HASHITEM_EMPTY(hi))
4216 {
4217 item = &dict_lookup(hi)->di_tv;
4218
4219 if (STRCMP(hi->hi_key, "mode") == 0)
4220 {
4221 if (!(supported & JO_MODE))
4222 break;
4223 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4224 return FAIL;
4225 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004226 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004227 {
4228 if (!(supported & JO_IN_MODE))
4229 break;
4230 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4231 == FAIL)
4232 return FAIL;
4233 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004234 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004235 {
4236 if (!(supported & JO_OUT_MODE))
4237 break;
4238 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4239 == FAIL)
4240 return FAIL;
4241 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004242 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004243 {
4244 if (!(supported & JO_ERR_MODE))
4245 break;
4246 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4247 == FAIL)
4248 return FAIL;
4249 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004250 else if (STRCMP(hi->hi_key, "in_io") == 0
4251 || STRCMP(hi->hi_key, "out_io") == 0
4252 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004253 {
4254 if (!(supported & JO_OUT_IO))
4255 break;
4256 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4257 return FAIL;
4258 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004259 else if (STRCMP(hi->hi_key, "in_name") == 0
4260 || STRCMP(hi->hi_key, "out_name") == 0
4261 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004262 {
4263 part = part_from_char(*hi->hi_key);
4264
4265 if (!(supported & JO_OUT_IO))
4266 break;
4267 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4268 opt->jo_io_name[part] =
4269 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4270 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004271 else if (STRCMP(hi->hi_key, "in_buf") == 0
4272 || STRCMP(hi->hi_key, "out_buf") == 0
4273 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004274 {
4275 part = part_from_char(*hi->hi_key);
4276
4277 if (!(supported & JO_OUT_IO))
4278 break;
4279 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4280 opt->jo_io_buf[part] = get_tv_number(item);
4281 if (opt->jo_io_buf[part] <= 0)
4282 {
4283 EMSG2(_(e_invarg2), get_tv_string(item));
4284 return FAIL;
4285 }
4286 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4287 {
4288 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4289 return FAIL;
4290 }
4291 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004292 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4293 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4294 {
4295 part = part_from_char(*hi->hi_key);
4296
4297 if (!(supported & JO_OUT_IO))
4298 break;
4299 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4300 opt->jo_modifiable[part] = get_tv_number(item);
4301 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004302 else if (STRCMP(hi->hi_key, "out_msg") == 0
4303 || STRCMP(hi->hi_key, "err_msg") == 0)
4304 {
4305 part = part_from_char(*hi->hi_key);
4306
4307 if (!(supported & JO_OUT_IO))
4308 break;
4309 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4310 opt->jo_message[part] = get_tv_number(item);
4311 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004312 else if (STRCMP(hi->hi_key, "in_top") == 0
4313 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004314 {
4315 linenr_T *lp;
4316
4317 if (!(supported & JO_OUT_IO))
4318 break;
4319 if (hi->hi_key[3] == 't')
4320 {
4321 lp = &opt->jo_in_top;
4322 opt->jo_set |= JO_IN_TOP;
4323 }
4324 else
4325 {
4326 lp = &opt->jo_in_bot;
4327 opt->jo_set |= JO_IN_BOT;
4328 }
4329 *lp = get_tv_number(item);
4330 if (*lp < 0)
4331 {
4332 EMSG2(_(e_invarg2), get_tv_string(item));
4333 return FAIL;
4334 }
4335 }
4336 else if (STRCMP(hi->hi_key, "channel") == 0)
4337 {
4338 if (!(supported & JO_OUT_IO))
4339 break;
4340 opt->jo_set |= JO_CHANNEL;
4341 if (item->v_type != VAR_CHANNEL)
4342 {
4343 EMSG2(_(e_invarg2), "channel");
4344 return FAIL;
4345 }
4346 opt->jo_channel = item->vval.v_channel;
4347 }
4348 else if (STRCMP(hi->hi_key, "callback") == 0)
4349 {
4350 if (!(supported & JO_CALLBACK))
4351 break;
4352 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004353 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004354 if (opt->jo_callback == NULL)
4355 {
4356 EMSG2(_(e_invarg2), "callback");
4357 return FAIL;
4358 }
4359 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004360 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004361 {
4362 if (!(supported & JO_OUT_CALLBACK))
4363 break;
4364 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004365 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004366 if (opt->jo_out_cb == NULL)
4367 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004368 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004369 return FAIL;
4370 }
4371 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004372 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004373 {
4374 if (!(supported & JO_ERR_CALLBACK))
4375 break;
4376 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004377 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004378 if (opt->jo_err_cb == NULL)
4379 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004380 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004381 return FAIL;
4382 }
4383 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004384 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004385 {
4386 if (!(supported & JO_CLOSE_CALLBACK))
4387 break;
4388 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004389 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004390 if (opt->jo_close_cb == NULL)
4391 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004392 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004393 return FAIL;
4394 }
4395 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004396 else if (STRCMP(hi->hi_key, "drop") == 0)
4397 {
4398 int never = FALSE;
4399 val = get_tv_string(item);
4400
4401 if (STRCMP(val, "never") == 0)
4402 never = TRUE;
4403 else if (STRCMP(val, "auto") != 0)
4404 {
4405 EMSG2(_(e_invarg2), "drop");
4406 return FAIL;
4407 }
4408 opt->jo_drop_never = never;
4409 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004410 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4411 {
4412 if (!(supported & JO_EXIT_CB))
4413 break;
4414 opt->jo_set |= JO_EXIT_CB;
4415 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4416 if (opt->jo_exit_cb == NULL)
4417 {
4418 EMSG2(_(e_invarg2), "exit_cb");
4419 return FAIL;
4420 }
4421 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004422 else if (STRCMP(hi->hi_key, "waittime") == 0)
4423 {
4424 if (!(supported & JO_WAITTIME))
4425 break;
4426 opt->jo_set |= JO_WAITTIME;
4427 opt->jo_waittime = get_tv_number(item);
4428 }
4429 else if (STRCMP(hi->hi_key, "timeout") == 0)
4430 {
4431 if (!(supported & JO_TIMEOUT))
4432 break;
4433 opt->jo_set |= JO_TIMEOUT;
4434 opt->jo_timeout = get_tv_number(item);
4435 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004436 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004437 {
4438 if (!(supported & JO_OUT_TIMEOUT))
4439 break;
4440 opt->jo_set |= JO_OUT_TIMEOUT;
4441 opt->jo_out_timeout = get_tv_number(item);
4442 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004443 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004444 {
4445 if (!(supported & JO_ERR_TIMEOUT))
4446 break;
4447 opt->jo_set |= JO_ERR_TIMEOUT;
4448 opt->jo_err_timeout = get_tv_number(item);
4449 }
4450 else if (STRCMP(hi->hi_key, "part") == 0)
4451 {
4452 if (!(supported & JO_PART))
4453 break;
4454 opt->jo_set |= JO_PART;
4455 val = get_tv_string(item);
4456 if (STRCMP(val, "err") == 0)
4457 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004458 else if (STRCMP(val, "out") == 0)
4459 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004460 else
4461 {
4462 EMSG2(_(e_invarg2), val);
4463 return FAIL;
4464 }
4465 }
4466 else if (STRCMP(hi->hi_key, "id") == 0)
4467 {
4468 if (!(supported & JO_ID))
4469 break;
4470 opt->jo_set |= JO_ID;
4471 opt->jo_id = get_tv_number(item);
4472 }
4473 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4474 {
4475 if (!(supported & JO_STOPONEXIT))
4476 break;
4477 opt->jo_set |= JO_STOPONEXIT;
4478 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4479 opt->jo_soe_buf);
4480 if (opt->jo_stoponexit == NULL)
4481 {
4482 EMSG2(_(e_invarg2), "stoponexit");
4483 return FAIL;
4484 }
4485 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004486 else if (STRCMP(hi->hi_key, "block_write") == 0)
4487 {
4488 if (!(supported & JO_BLOCK_WRITE))
4489 break;
4490 opt->jo_set |= JO_BLOCK_WRITE;
4491 opt->jo_block_write = get_tv_number(item);
4492 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004493 else
4494 break;
4495 --todo;
4496 }
4497 if (todo > 0)
4498 {
4499 EMSG2(_(e_invarg2), hi->hi_key);
4500 return FAIL;
4501 }
4502
4503 return OK;
4504}
4505
4506/*
4507 * Get the channel from the argument.
4508 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004509 * When "check_open" is TRUE check that the channel can be used.
4510 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004511 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004512 */
4513 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004514get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004515{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004516 channel_T *channel = NULL;
4517 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004518
4519 if (tv->v_type == VAR_JOB)
4520 {
4521 if (tv->vval.v_job != NULL)
4522 channel = tv->vval.v_job->jv_channel;
4523 }
4524 else if (tv->v_type == VAR_CHANNEL)
4525 {
4526 channel = tv->vval.v_channel;
4527 }
4528 else
4529 {
4530 EMSG2(_(e_invarg2), get_tv_string(tv));
4531 return NULL;
4532 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004533 if (channel != NULL && reading)
4534 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004535 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004536
Bram Moolenaar437905c2016-04-26 19:01:05 +02004537 if (check_open && (channel == NULL || (!channel_is_open(channel)
4538 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004539 {
4540 EMSG(_("E906: not an open channel"));
4541 return NULL;
4542 }
4543 return channel;
4544}
4545
4546static job_T *first_job = NULL;
4547
4548 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004549job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004550{
4551 ch_log(job->jv_channel, "Freeing job");
4552 if (job->jv_channel != NULL)
4553 {
4554 /* The link from the channel to the job doesn't count as a reference,
4555 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004556 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004557 * NULL the reference. We don't set ch_job_killed, unreferencing the
4558 * job doesn't mean it stops running. */
4559 job->jv_channel->ch_job = NULL;
4560 channel_unref(job->jv_channel);
4561 }
4562 mch_clear_job(job);
4563
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004564 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004565 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004566}
4567
4568 static void
4569job_free_job(job_T *job)
4570{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004571 if (job->jv_next != NULL)
4572 job->jv_next->jv_prev = job->jv_prev;
4573 if (job->jv_prev == NULL)
4574 first_job = job->jv_next;
4575 else
4576 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004577 vim_free(job);
4578}
4579
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004580 static void
4581job_free(job_T *job)
4582{
4583 if (!in_free_unref_items)
4584 {
4585 job_free_contents(job);
4586 job_free_job(job);
4587 }
4588}
4589
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004590#if defined(EXITFREE) || defined(PROTO)
4591 void
4592job_free_all(void)
4593{
4594 while (first_job != NULL)
4595 job_free(first_job);
4596}
4597#endif
4598
4599/*
4600 * Return TRUE if we need to check if the process of "job" has ended.
4601 */
4602 static int
4603job_need_end_check(job_T *job)
4604{
4605 return job->jv_status == JOB_STARTED
4606 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4607}
4608
4609/*
4610 * Return TRUE if the channel of "job" is still useful.
4611 */
4612 static int
4613job_channel_still_useful(job_T *job)
4614{
4615 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4616}
4617
4618/*
4619 * Return TRUE if the job should not be freed yet. Do not free the job when
4620 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4621 * or when the associated channel will do something with the job output.
4622 */
4623 static int
4624job_still_useful(job_T *job)
4625{
4626 return job_need_end_check(job) || job_channel_still_useful(job);
4627}
4628
4629/*
4630 * NOTE: Must call job_cleanup() only once right after the status of "job"
4631 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4632 * mch_detect_ended_job() returned non-NULL).
4633 */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004634 static void
4635job_cleanup(job_T *job)
4636{
4637 if (job->jv_status != JOB_ENDED)
4638 return;
4639
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004640 /* Ready to cleanup the job. */
4641 job->jv_status = JOB_FINISHED;
4642
Bram Moolenaar97792de2016-10-15 18:36:49 +02004643 if (job->jv_exit_cb != NULL)
4644 {
4645 typval_T argv[3];
4646 typval_T rettv;
4647 int dummy;
4648
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004649 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004650 ++job->jv_refcount;
4651 argv[0].v_type = VAR_JOB;
4652 argv[0].vval.v_job = job;
4653 argv[1].v_type = VAR_NUMBER;
4654 argv[1].vval.v_number = job->jv_exitval;
4655 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4656 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4657 job->jv_exit_partial, NULL);
4658 clear_tv(&rettv);
4659 --job->jv_refcount;
4660 channel_need_redraw = TRUE;
4661 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004662
4663 /* Do not free the job in case the close callback of the associated channel
4664 * isn't invoked yet and may get information by job_info(). */
4665 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004666 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004667 /* The job was already unreferenced and the associated channel was
4668 * detached, now that it ended it can be freed. Careful: caller must
4669 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004670 job_free(job);
4671 }
4672}
4673
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004674/*
4675 * Mark references in jobs that are still useful.
4676 */
4677 int
4678set_ref_in_job(int copyID)
4679{
4680 int abort = FALSE;
4681 job_T *job;
4682 typval_T tv;
4683
4684 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004685 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004686 {
4687 tv.v_type = VAR_JOB;
4688 tv.vval.v_job = job;
4689 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4690 }
4691 return abort;
4692}
4693
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004694/*
4695 * Dereference "job". Note that after this "job" may have been freed.
4696 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004697 void
4698job_unref(job_T *job)
4699{
4700 if (job != NULL && --job->jv_refcount <= 0)
4701 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004702 /* Do not free the job if there is a channel where the close callback
4703 * may get the job info. */
4704 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004705 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004706 /* Do not free the job when it has not ended yet and there is a
4707 * "stoponexit" flag or an exit callback. */
4708 if (!job_need_end_check(job))
4709 {
4710 job_free(job);
4711 }
4712 else if (job->jv_channel != NULL)
4713 {
4714 /* Do remove the link to the channel, otherwise it hangs
4715 * around until Vim exits. See job_free() for refcount. */
4716 ch_log(job->jv_channel, "detaching channel from job");
4717 job->jv_channel->ch_job = NULL;
4718 channel_unref(job->jv_channel);
4719 job->jv_channel = NULL;
4720 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004721 }
4722 }
4723}
4724
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004725 int
4726free_unused_jobs_contents(int copyID, int mask)
4727{
4728 int did_free = FALSE;
4729 job_T *job;
4730
4731 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004732 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004733 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004734 {
4735 /* Free the channel and ordinary items it contains, but don't
4736 * recurse into Lists, Dictionaries etc. */
4737 job_free_contents(job);
4738 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004739 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004740 return did_free;
4741}
4742
4743 void
4744free_unused_jobs(int copyID, int mask)
4745{
4746 job_T *job;
4747 job_T *job_next;
4748
4749 for (job = first_job; job != NULL; job = job_next)
4750 {
4751 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004752 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004753 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004754 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004755 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004756 job_free_job(job);
4757 }
4758 }
4759}
4760
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004761/*
4762 * Allocate a job. Sets the refcount to one and sets options default.
4763 */
4764 static job_T *
4765job_alloc(void)
4766{
4767 job_T *job;
4768
4769 job = (job_T *)alloc_clear(sizeof(job_T));
4770 if (job != NULL)
4771 {
4772 job->jv_refcount = 1;
4773 job->jv_stoponexit = vim_strsave((char_u *)"term");
4774
4775 if (first_job != NULL)
4776 {
4777 first_job->jv_prev = job;
4778 job->jv_next = first_job;
4779 }
4780 first_job = job;
4781 }
4782 return job;
4783}
4784
4785 void
4786job_set_options(job_T *job, jobopt_T *opt)
4787{
4788 if (opt->jo_set & JO_STOPONEXIT)
4789 {
4790 vim_free(job->jv_stoponexit);
4791 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4792 job->jv_stoponexit = NULL;
4793 else
4794 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4795 }
4796 if (opt->jo_set & JO_EXIT_CB)
4797 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004798 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004799 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004800 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004801 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004802 job->jv_exit_partial = NULL;
4803 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004804 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004805 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004806 job->jv_exit_partial = opt->jo_exit_partial;
4807 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004808 {
4809 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004810 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004811 }
4812 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004813 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004814 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004815 func_ref(job->jv_exit_cb);
4816 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004817 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004818 }
4819}
4820
4821/*
4822 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4823 */
4824 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004825job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004826{
4827 job_T *job;
4828
4829 for (job = first_job; job != NULL; job = job->jv_next)
4830 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4831 mch_stop_job(job, job->jv_stoponexit);
4832}
4833
4834/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004835 * Return TRUE when there is any job that has an exit callback and might exit,
4836 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004837 */
4838 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004839has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004840{
4841 job_T *job;
4842
4843 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004844 /* Only should check if the channel has been closed, if the channel is
4845 * open the job won't exit. */
4846 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004847 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004848 return TRUE;
4849 return FALSE;
4850}
4851
Bram Moolenaar97792de2016-10-15 18:36:49 +02004852#define MAX_CHECK_ENDED 8
4853
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004854/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004855 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004856 */
4857 void
4858job_check_ended(void)
4859{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004860 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004861
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004862 if (first_job == NULL)
4863 return;
4864
Bram Moolenaar97792de2016-10-15 18:36:49 +02004865 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004866 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004867 /* NOTE: mch_detect_ended_job() must only return a job of which the
4868 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004869 job_T *job = mch_detect_ended_job(first_job);
4870
4871 if (job == NULL)
4872 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004873 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004874 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004875
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004876 if (channel_need_redraw)
4877 {
4878 channel_need_redraw = FALSE;
4879 redraw_after_callback();
4880 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004881}
4882
4883/*
4884 * "job_start()" function
4885 */
4886 job_T *
4887job_start(typval_T *argvars)
4888{
4889 job_T *job;
4890 char_u *cmd = NULL;
4891#if defined(UNIX)
4892# define USE_ARGV
4893 char **argv = NULL;
4894 int argc = 0;
4895#else
4896 garray_T ga;
4897#endif
4898 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004899 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004900
4901 job = job_alloc();
4902 if (job == NULL)
4903 return NULL;
4904
4905 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004906#ifndef USE_ARGV
4907 ga_init2(&ga, (int)sizeof(char*), 20);
4908#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004909
4910 /* Default mode is NL. */
4911 clear_job_options(&opt);
4912 opt.jo_mode = MODE_NL;
4913 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004914 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4915 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004916 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004917
4918 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004919 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004920 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4921 && opt.jo_io[part] == JIO_FILE
4922 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4923 || *opt.jo_io_name[part] == NUL))
4924 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004925 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004926 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004927 }
4928
4929 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4930 {
4931 buf_T *buf = NULL;
4932
4933 /* check that we can find the buffer before starting the job */
4934 if (opt.jo_set & JO_IN_BUF)
4935 {
4936 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4937 if (buf == NULL)
4938 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4939 }
4940 else if (!(opt.jo_set & JO_IN_NAME))
4941 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004942 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004943 }
4944 else
4945 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4946 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004947 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004948 if (buf->b_ml.ml_mfp == NULL)
4949 {
4950 char_u numbuf[NUMBUFLEN];
4951 char_u *s;
4952
4953 if (opt.jo_set & JO_IN_BUF)
4954 {
4955 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4956 s = numbuf;
4957 }
4958 else
4959 s = opt.jo_io_name[PART_IN];
4960 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004961 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004962 }
4963 job->jv_in_buf = buf;
4964 }
4965
4966 job_set_options(job, &opt);
4967
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004968 if (argvars[0].v_type == VAR_STRING)
4969 {
4970 /* Command is a string. */
4971 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004972 if (cmd == NULL || *cmd == NUL)
4973 {
4974 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004975 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004976 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004977#ifdef USE_ARGV
4978 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004979 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004980 argv[argc] = NULL;
4981#endif
4982 }
4983 else if (argvars[0].v_type != VAR_LIST
4984 || argvars[0].vval.v_list == NULL
4985 || argvars[0].vval.v_list->lv_len < 1)
4986 {
4987 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004988 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004989 }
4990 else
4991 {
4992 list_T *l = argvars[0].vval.v_list;
4993 listitem_T *li;
4994 char_u *s;
4995
4996#ifdef USE_ARGV
4997 /* Pass argv[] to mch_call_shell(). */
4998 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4999 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005000 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005001#endif
5002 for (li = l->lv_first; li != NULL; li = li->li_next)
5003 {
5004 s = get_tv_string_chk(&li->li_tv);
5005 if (s == NULL)
5006 goto theend;
5007#ifdef USE_ARGV
5008 argv[argc++] = (char *)s;
5009#else
5010 /* Only escape when needed, double quotes are not always allowed. */
5011 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
5012 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005013# ifdef WIN32
5014 int old_ssl = p_ssl;
5015
5016 /* This is using CreateProcess, not cmd.exe. Always use
5017 * double quote and backslashes. */
5018 p_ssl = 0;
5019# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005020 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005021# ifdef WIN32
5022 p_ssl = old_ssl;
5023# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005024 if (s == NULL)
5025 goto theend;
5026 ga_concat(&ga, s);
5027 vim_free(s);
5028 }
5029 else
5030 ga_concat(&ga, s);
5031 if (li->li_next != NULL)
5032 ga_append(&ga, ' ');
5033#endif
5034 }
5035#ifdef USE_ARGV
5036 argv[argc] = NULL;
5037#else
5038 cmd = ga.ga_data;
5039#endif
5040 }
5041
5042#ifdef USE_ARGV
5043 if (ch_log_active())
5044 {
5045 garray_T ga;
5046 int i;
5047
5048 ga_init2(&ga, (int)sizeof(char), 200);
5049 for (i = 0; i < argc; ++i)
5050 {
5051 if (i > 0)
5052 ga_concat(&ga, (char_u *)" ");
5053 ga_concat(&ga, (char_u *)argv[i]);
5054 }
5055 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
5056 ga_clear(&ga);
5057 }
5058 mch_start_job(argv, job, &opt);
5059#else
5060 ch_logs(NULL, "Starting job: %s", (char *)cmd);
5061 mch_start_job((char *)cmd, job, &opt);
5062#endif
5063
5064 /* If the channel is reading from a buffer, write lines now. */
5065 if (job->jv_channel != NULL)
5066 channel_write_in(job->jv_channel);
5067
5068theend:
5069#ifdef USE_ARGV
5070 vim_free(argv);
5071#else
5072 vim_free(ga.ga_data);
5073#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005074 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005075 return job;
5076}
5077
5078/*
5079 * Get the status of "job" and invoke the exit callback when needed.
5080 * The returned string is not allocated.
5081 */
5082 char *
5083job_status(job_T *job)
5084{
5085 char *result;
5086
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005087 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005088 /* No need to check, dead is dead. */
5089 result = "dead";
5090 else if (job->jv_status == JOB_FAILED)
5091 result = "fail";
5092 else
5093 {
5094 result = mch_job_status(job);
5095 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005096 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005097 }
5098 return result;
5099}
5100
Bram Moolenaar8950a562016-03-12 15:22:55 +01005101/*
5102 * Implementation of job_info().
5103 */
5104 void
5105job_info(job_T *job, dict_T *dict)
5106{
5107 dictitem_T *item;
5108 varnumber_T nr;
5109
5110 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5111
5112 item = dictitem_alloc((char_u *)"channel");
5113 if (item == NULL)
5114 return;
5115 item->di_tv.v_lock = 0;
5116 item->di_tv.v_type = VAR_CHANNEL;
5117 item->di_tv.vval.v_channel = job->jv_channel;
5118 if (job->jv_channel != NULL)
5119 ++job->jv_channel->ch_refcount;
5120 if (dict_add(dict, item) == FAIL)
5121 dictitem_free(item);
5122
5123#ifdef UNIX
5124 nr = job->jv_pid;
5125#else
5126 nr = job->jv_proc_info.dwProcessId;
5127#endif
5128 dict_add_nr_str(dict, "process", nr, NULL);
5129
5130 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005131 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005132 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5133}
5134
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005135 int
5136job_stop(job_T *job, typval_T *argvars)
5137{
5138 char_u *arg;
5139
5140 if (argvars[1].v_type == VAR_UNKNOWN)
5141 arg = (char_u *)"";
5142 else
5143 {
5144 arg = get_tv_string_chk(&argvars[1]);
5145 if (arg == NULL)
5146 {
5147 EMSG(_(e_invarg));
5148 return 0;
5149 }
5150 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005151 if (job->jv_status == JOB_ENDED)
5152 {
5153 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5154 return 0;
5155 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005156 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
5157 if (mch_stop_job(job, arg) == FAIL)
5158 return 0;
5159
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005160 /* Assume that only "kill" will kill the job. */
5161 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005162 job->jv_channel->ch_job_killed = TRUE;
5163
5164 /* We don't try freeing the job, obviously the caller still has a
5165 * reference to it. */
5166 return 1;
5167}
5168
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005169#endif /* FEAT_JOB_CHANNEL */