blob: 9ba013a852d2ff5414f182c66041a320cb6cc5e7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaard04a0202016-01-26 23:30:18 +010023#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
119 EMSG2(_(e_notopen), fname);
120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200135ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100162ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100170 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171 }
172}
173
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200174 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100175ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176{
177 if (log_fd != NULL)
178 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100181 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100183 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184 }
185}
186
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100188ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100192 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100196 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197 }
198}
199
200 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100201ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100209 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 }
211}
212
213 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100214ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215{
216 if (log_fd != NULL)
217 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100222 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223 }
224}
225
226 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100227ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100228{
229 if (log_fd != NULL)
230 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100235 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236 }
237}
238
239 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100240ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100241{
242 if (log_fd != NULL)
243 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100248 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100249 }
250}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100251
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252#ifdef _WIN32
253# undef PERROR
254# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
255 (char_u *)msg, (char_u *)strerror_win32(errno))
256
257 static char *
258strerror_win32(int eno)
259{
260 static LPVOID msgbuf = NULL;
261 char_u *ptr;
262
263 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200264 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100265 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200266 msgbuf = NULL;
267 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100268 FormatMessage(
269 FORMAT_MESSAGE_ALLOCATE_BUFFER |
270 FORMAT_MESSAGE_FROM_SYSTEM |
271 FORMAT_MESSAGE_IGNORE_INSERTS,
272 NULL,
273 eno,
274 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
275 (LPTSTR) &msgbuf,
276 0,
277 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200278 if (msgbuf != NULL)
279 /* chomp \r or \n */
280 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
281 switch (*ptr)
282 {
283 case '\r':
284 STRMOVE(ptr, ptr + 1);
285 ptr--;
286 break;
287 case '\n':
288 if (*(ptr + 1) == '\0')
289 *ptr = '\0';
290 else
291 *ptr = ' ';
292 break;
293 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100294 return msgbuf;
295}
296#endif
297
Bram Moolenaar77073442016-02-13 23:23:53 +0100298/*
299 * The list of all allocated channels.
300 */
301static channel_T *first_channel = NULL;
302static int next_ch_id = 0;
303
304/*
305 * Allocate a new channel. The refcount is set to 1.
306 * The channel isn't actually used until it is opened.
307 * Returns NULL if out of memory.
308 */
309 channel_T *
310add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100311{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200312 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100314
Bram Moolenaar77073442016-02-13 23:23:53 +0100315 if (channel == NULL)
316 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317
Bram Moolenaar77073442016-02-13 23:23:53 +0100318 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100319 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100320
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200321 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100323 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100324#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100325 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100326#endif
327#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100328 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100329#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100330 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100331 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100332
Bram Moolenaar77073442016-02-13 23:23:53 +0100333 if (first_channel != NULL)
334 {
335 first_channel->ch_prev = channel;
336 channel->ch_next = first_channel;
337 }
338 first_channel = channel;
339
340 channel->ch_refcount = 1;
341 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100342}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100343
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200344 int
345has_any_channel(void)
346{
347 return first_channel != NULL;
348}
349
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100350/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100351 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100352 * Return TRUE if "channel" has a callback and the associated job wasn't
353 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100354 */
355 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100356channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100357{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100359 int has_out_msg;
360 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100361
362 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100363 if (channel->ch_job_killed && channel->ch_job == NULL)
364 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100365
366 /* If there is a close callback it may still need to be invoked. */
367 if (channel->ch_close_cb != NULL)
368 return TRUE;
369
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200370 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200371 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200372 return TRUE;
373
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100374 /* If there is no callback then nobody can get readahead. If the fd is
375 * closed and there is no readahead then the callback won't be called. */
376 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
377 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
378 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100379 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
380 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
381 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
382 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
383 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
384 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100385 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100386 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200387 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200388 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
389 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200390 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200391 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
392 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100393}
394
395/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200396 * Close a channel and free all its resources.
397 */
398 static void
399channel_free_contents(channel_T *channel)
400{
401 channel_close(channel, TRUE);
402 channel_clear(channel);
403 ch_log(channel, "Freeing channel");
404}
405
406 static void
407channel_free_channel(channel_T *channel)
408{
409 if (channel->ch_next != NULL)
410 channel->ch_next->ch_prev = channel->ch_prev;
411 if (channel->ch_prev == NULL)
412 first_channel = channel->ch_next;
413 else
414 channel->ch_prev->ch_next = channel->ch_next;
415 vim_free(channel);
416}
417
418 static void
419channel_free(channel_T *channel)
420{
421 if (!in_free_unref_items)
422 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200423 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200424 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425 else
426 {
427 channel_free_contents(channel);
428 channel_free_channel(channel);
429 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200430 }
431}
432
433/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100435 * possible, there is no callback to be invoked or the associated job was
436 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100437 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100439 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440channel_may_free(channel_T *channel)
441{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100442 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100443 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100444 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100445 return TRUE;
446 }
447 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100448}
449
450/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100451 * Decrement the reference count on "channel" and maybe free it when it goes
452 * down to zero. Don't free it if there is a pending action.
453 * Returns TRUE when the channel is no longer referenced.
454 */
455 int
456channel_unref(channel_T *channel)
457{
458 if (channel != NULL && --channel->ch_refcount <= 0)
459 return channel_may_free(channel);
460 return FALSE;
461}
462
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200463 int
464free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100465{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 int did_free = FALSE;
467 channel_T *ch;
468
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200469 /* This is invoked from the garbage collector, which only runs at a safe
470 * point. */
471 ++safe_to_invoke_callback;
472
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200473 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200474 if (!channel_still_useful(ch)
475 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200476 {
477 /* Free the channel and ordinary items it contains, but don't
478 * recurse into Lists, Dictionaries etc. */
479 channel_free_contents(ch);
480 did_free = TRUE;
481 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200482
483 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200484 return did_free;
485}
486
487 void
488free_unused_channels(int copyID, int mask)
489{
490 channel_T *ch;
491 channel_T *ch_next;
492
493 for (ch = first_channel; ch != NULL; ch = ch_next)
494 {
495 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200496 if (!channel_still_useful(ch)
497 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200498 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200499 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200500 channel_free_channel(ch);
501 }
502 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100503}
504
Bram Moolenaard04a0202016-01-26 23:30:18 +0100505#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100506
507#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
508 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100509channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100510{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100511 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200512 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100513
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100514 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100516 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100517 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200518 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100519}
520#endif
521
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525#ifdef FEAT_GUI_X11
526 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527messageFromServer(XtPointer clientData,
528 int *unused1 UNUSED,
529 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100530{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100531 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100532}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100533#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100534
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100536# if GTK_CHECK_VERSION(3,0,0)
537 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538messageFromServer(GIOChannel *unused1 UNUSED,
539 GIOCondition unused2 UNUSED,
540 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100541{
542 channel_read_fd(GPOINTER_TO_INT(clientData));
543 return TRUE; /* Return FALSE instead in case the event source is to
544 * be removed after this function returns. */
545}
546# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100547 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200548messageFromServer(gpointer clientData,
549 gint unused1 UNUSED,
550 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100551{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100552 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100553}
Bram Moolenaar98921892016-02-23 17:14:37 +0100554# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100555#endif
556
557 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200558channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100559{
Bram Moolenaarde279892016-03-11 22:19:44 +0100560 if (!CH_HAS_GUI)
561 return;
562
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# ifdef FEAT_GUI_X11
564 /* Tell notifier we are interested in being called
565 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100566 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
567 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100568 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100569 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100570 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200571 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100572 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100573# else
574# ifdef FEAT_GUI_GTK
575 /* Tell gdk we are interested in being called when there
576 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100577 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100578# if GTK_CHECK_VERSION(3,0,0)
579 {
580 GIOChannel *chnnl = g_io_channel_unix_new(
581 (gint)channel->ch_part[part].ch_fd);
582
583 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
584 chnnl,
585 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200586 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100587 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
588
589 g_io_channel_unref(chnnl);
590 }
591# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100592 channel->ch_part[part].ch_inputHandler = gdk_input_add(
593 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100594 (GdkInputCondition)
595 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200596 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100597 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100598# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100599# endif
600# endif
601}
602
Bram Moolenaarde279892016-03-11 22:19:44 +0100603 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100604channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100605{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100606 if (channel->CH_SOCK_FD != INVALID_FD)
607 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100608 if (channel->CH_OUT_FD != INVALID_FD)
609 channel_gui_register_one(channel, PART_OUT);
610 if (channel->CH_ERR_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612}
613
614/*
615 * Register any of our file descriptors with the GUI event handling system.
616 * Called when the GUI has started.
617 */
618 void
619channel_gui_register_all(void)
620{
Bram Moolenaar77073442016-02-13 23:23:53 +0100621 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622
Bram Moolenaar77073442016-02-13 23:23:53 +0100623 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100624 channel_gui_register(channel);
625}
626
627 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200628channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100629{
630# ifdef FEAT_GUI_X11
631 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
632 {
633 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
634 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
635 }
636# else
637# ifdef FEAT_GUI_GTK
638 if (channel->ch_part[part].ch_inputHandler != 0)
639 {
640# if GTK_CHECK_VERSION(3,0,0)
641 g_source_remove(channel->ch_part[part].ch_inputHandler);
642# else
643 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
644# endif
645 channel->ch_part[part].ch_inputHandler = 0;
646 }
647# endif
648# endif
649}
650
651 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100652channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100653{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200654 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100655
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100656 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100657 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658}
659
660#endif
661
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100662static char *e_cannot_connect = N_("E902: Cannot connect to port");
663
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100665 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100666 * "waittime" is the time in msec to wait for the connection.
667 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100668 * Returns the channel for success.
669 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100670 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100671 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100672channel_open(
673 char *hostname,
674 int port_in,
675 int waittime,
676 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100677{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100678 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100681#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100682 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100683 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100684#else
685 int port = port_in;
686#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100687 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100688 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100690#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691 channel_init_winsock();
692#endif
693
Bram Moolenaar77073442016-02-13 23:23:53 +0100694 channel = add_channel();
695 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100697 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100698 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100699 }
700
701 /* Get the server internet address and put into addr structure */
702 /* fill in the socket address structure and connect to server */
703 vim_memset((char *)&server, 0, sizeof(server));
704 server.sin_family = AF_INET;
705 server.sin_port = htons(port);
706 if ((host = gethostbyname(hostname)) == NULL)
707 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200709 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100710 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100711 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100712 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100713 {
714 char *p;
715
716 /* When using host->h_addr directly ubsan warns for it to not be
717 * aligned. First copy the pointer to aviod that. */
718 memcpy(&p, &host->h_addr, sizeof(p));
719 memcpy((char *)&server.sin_addr, p, host->h_length);
720 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100722 /* On Mac and Solaris a zero timeout almost never works. At least wait
723 * one millisecond. Let's do it for all systems, because we don't know why
724 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100725 if (waittime == 0)
726 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100727
728 /*
729 * For Unix we need to call connect() again after connect() failed.
730 * On Win32 one time is sufficient.
731 */
732 while (TRUE)
733 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100734 long elapsed_msec = 0;
735 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100737 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100738 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100739 sd = socket(AF_INET, SOCK_STREAM, 0);
740 if (sd == -1)
741 {
742 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200743 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100744 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100745 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100746 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100747
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100748 if (waittime >= 0)
749 {
750 /* Make connect() non-blocking. */
751 if (
752#ifdef _WIN32
753 ioctlsocket(sd, FIONBIO, &val) < 0
754#else
755 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
756#endif
757 )
758 {
759 SOCK_ERRNO;
760 ch_errorn(channel,
761 "channel_open: Connect failed with errno %d", errno);
762 sock_close(sd);
763 channel_free(channel);
764 return NULL;
765 }
766 }
767
768 /* Try connecting to the server. */
769 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
770 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
771
Bram Moolenaar045a2842016-03-08 22:33:07 +0100772 if (ret == 0)
773 /* The connection could be established. */
774 break;
775
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100776 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100777 if (waittime < 0 || (errno != EWOULDBLOCK
778 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100779#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100780 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100781#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100782 ))
783 {
784 ch_errorn(channel,
785 "channel_open: Connect failed with errno %d", errno);
786 PERROR(_(e_cannot_connect));
787 sock_close(sd);
788 channel_free(channel);
789 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100790 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100791
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100792 /* Limit the waittime to 50 msec. If it doesn't work within this
793 * time we close the socket and try creating it again. */
794 waitnow = waittime > 50 ? 50 : waittime;
795
Bram Moolenaar045a2842016-03-08 22:33:07 +0100796 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100797 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100798#ifndef WIN32
799 if (errno != ECONNREFUSED)
800#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801 {
802 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100803 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100805#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 int so_error = 0;
807 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 struct timeval start_tv;
809 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100810#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100811 FD_ZERO(&rfds);
812 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100813 FD_ZERO(&wfds);
814 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100815
Bram Moolenaar562ca712016-03-09 21:50:05 +0100816 tv.tv_sec = waitnow / 1000;
817 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100818#ifndef WIN32
819 gettimeofday(&start_tv, NULL);
820#endif
821 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100822 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100823 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100824
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100825 if (ret < 0)
826 {
827 SOCK_ERRNO;
828 ch_errorn(channel,
829 "channel_open: Connect failed with errno %d", errno);
830 PERROR(_(e_cannot_connect));
831 sock_close(sd);
832 channel_free(channel);
833 return NULL;
834 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100835
Bram Moolenaare081e212016-02-28 22:33:46 +0100836#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100837 /* On Win32: select() is expected to work and wait for up to
838 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100839 if (FD_ISSET(sd, &wfds))
840 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100841 elapsed_msec = waitnow;
842 if (waittime > 1 && elapsed_msec < waittime)
843 {
844 waittime -= elapsed_msec;
845 continue;
846 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100847#else
848 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100849 * After putting the socket in non-blocking mode, connect() will
850 * return EINPROGRESS, select() will not wait (as if writing is
851 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100852 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100853 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100854 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100855 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100856 {
857 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100858 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100859 if (ret < 0 || (so_error != 0
860 && so_error != EWOULDBLOCK
861 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100862# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100864# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100865 ))
866 {
867 ch_errorn(channel,
868 "channel_open: Connect failed with errno %d",
869 so_error);
870 PERROR(_(e_cannot_connect));
871 sock_close(sd);
872 channel_free(channel);
873 return NULL;
874 }
875 }
876
Bram Moolenaar045a2842016-03-08 22:33:07 +0100877 if (FD_ISSET(sd, &wfds) && so_error == 0)
878 /* Did not detect an error, connection is established. */
879 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100880
Bram Moolenaar045a2842016-03-08 22:33:07 +0100881 gettimeofday(&end_tv, NULL);
882 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
883 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100884#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100885 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100886
887#ifndef WIN32
888 if (waittime > 1 && elapsed_msec < waittime)
889 {
890 /* The port isn't ready but we also didn't get an error.
891 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100892 * yet. Select() may return early, wait until the remaining
893 * "waitnow" and try again. */
894 waitnow -= elapsed_msec;
895 waittime -= elapsed_msec;
896 if (waitnow > 0)
897 {
898 mch_delay((long)waitnow, TRUE);
899 ui_breakcheck();
900 waittime -= waitnow;
901 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100902 if (!got_int)
903 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100904 if (waittime <= 0)
905 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100906 waittime = 1;
907 continue;
908 }
909 /* we were interrupted, behave as if timed out */
910 }
911#endif
912
913 /* We timed out. */
914 ch_error(channel, "Connection timed out");
915 sock_close(sd);
916 channel_free(channel);
917 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100918 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100919
Bram Moolenaar045a2842016-03-08 22:33:07 +0100920 ch_log(channel, "Connection made");
921
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100922 if (waittime >= 0)
923 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100924#ifdef _WIN32
925 val = 0;
926 ioctlsocket(sd, FIONBIO, &val);
927#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100928 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100929#endif
930 }
931
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100932 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100933 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100934 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
935 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200936 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100937
938#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100939 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100940#endif
941
Bram Moolenaar77073442016-02-13 23:23:53 +0100942 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100943}
944
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945/*
946 * Implements ch_open().
947 */
948 channel_T *
949channel_open_func(typval_T *argvars)
950{
951 char_u *address;
952 char_u *p;
953 char *rest;
954 int port;
955 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200956 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957
958 address = get_tv_string(&argvars[0]);
959 if (argvars[1].v_type != VAR_UNKNOWN
960 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
961 {
962 EMSG(_(e_invarg));
963 return NULL;
964 }
965
966 /* parse address */
967 p = vim_strchr(address, ':');
968 if (p == NULL)
969 {
970 EMSG2(_(e_invarg2), address);
971 return NULL;
972 }
973 *p++ = NUL;
974 port = strtol((char *)p, &rest, 10);
975 if (*address == NUL || port <= 0 || *rest != NUL)
976 {
977 p[-1] = ':';
978 EMSG2(_(e_invarg2), address);
979 return NULL;
980 }
981
982 /* parse options */
983 clear_job_options(&opt);
984 opt.jo_mode = MODE_JSON;
985 opt.jo_timeout = 2000;
986 if (get_job_options(&argvars[1], &opt,
987 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200988 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100989 if (opt.jo_timeout < 0)
990 {
991 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200992 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100993 }
994
995 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
996 if (channel != NULL)
997 {
998 opt.jo_set = JO_ALL;
999 channel_set_options(channel, &opt);
1000 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001001theend:
1002 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001003 return channel;
1004}
1005
Bram Moolenaarde279892016-03-11 22:19:44 +01001006 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001007ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001008{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001009 sock_T *fd = &channel->ch_part[part].ch_fd;
1010
Bram Moolenaarde279892016-03-11 22:19:44 +01001011 if (*fd != INVALID_FD)
1012 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001013 if (part == PART_SOCK)
1014 sock_close(*fd);
1015 else
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 Moolenaarc4da1132017-07-15 19:39:43 +02001441 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001442 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001443 return;
1444 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001445
1446 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1447 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1448 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001449 if (!can_write_buf_line(channel))
1450 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001451 write_buf_line(buf, lnum, channel);
1452 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001453 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001454
1455 if (written == 1)
1456 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1457 else if (written > 1)
1458 ch_logn(channel, "written %d lines to channel", written);
1459
Bram Moolenaar014069a2016-03-03 22:51:40 +01001460 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001461 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001462 {
1463 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001464 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001465 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001466
1467 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001468 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001469 }
1470 else
1471 ch_logn(channel, "Still %d more lines to write",
1472 buf->b_ml.ml_line_count - lnum + 1);
1473}
1474
1475/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001476 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001477 */
1478 void
1479channel_buffer_free(buf_T *buf)
1480{
1481 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001482 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001483
1484 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001485 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001486 {
1487 chanpart_T *ch_part = &channel->ch_part[part];
1488
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001489 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001490 {
1491 ch_logs(channel, "%s buffer has been wiped out",
1492 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001493 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001494 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001495 }
1496}
1497
1498/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001499 * Write any lines waiting to be written to a channel.
1500 */
1501 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001502channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001503{
1504 channel_T *channel;
1505
1506 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1507 {
1508 chanpart_T *in_part = &channel->ch_part[PART_IN];
1509
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001510 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001511 {
1512 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001513 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001514 else
1515 channel_write_in(channel);
1516 }
1517 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001518}
1519
1520/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001521 * Write appended lines above the last one in "buf" to the channel.
1522 */
1523 void
1524channel_write_new_lines(buf_T *buf)
1525{
1526 channel_T *channel;
1527 int found_one = FALSE;
1528
1529 /* There could be more than one channel for the buffer, loop over all of
1530 * them. */
1531 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1532 {
1533 chanpart_T *in_part = &channel->ch_part[PART_IN];
1534 linenr_T lnum;
1535 int written = 0;
1536
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001537 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001538 {
1539 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001540 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001541 found_one = TRUE;
1542 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1543 ++lnum)
1544 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001545 if (!can_write_buf_line(channel))
1546 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001547 write_buf_line(buf, lnum, channel);
1548 ++written;
1549 }
1550
1551 if (written == 1)
1552 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1553 else if (written > 1)
1554 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001555 if (lnum < buf->b_ml.ml_line_count)
1556 ch_logn(channel, "Still %d more lines to write",
1557 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001558
1559 in_part->ch_buf_bot = lnum;
1560 }
1561 }
1562 if (!found_one)
1563 buf->b_write_to_channel = FALSE;
1564}
1565
1566/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001567 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001568 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001569 */
1570 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001571invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1572 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001573{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001574 typval_T rettv;
1575 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001576
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001577 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001578 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001579
Bram Moolenaar77073442016-02-13 23:23:53 +01001580 argv[0].v_type = VAR_CHANNEL;
1581 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001582
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001583 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1584 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001585 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001586 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001587}
1588
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001589/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001590 * Return the first node from "channel"/"part" without removing it.
1591 * Returns NULL if there is nothing.
1592 */
1593 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001594channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001595{
1596 readq_T *head = &channel->ch_part[part].ch_head;
1597
1598 return head->rq_next;
1599}
1600
1601/*
1602 * Return a pointer to the first NL in "node".
1603 * Skips over NUL characters.
1604 * Returns NULL if there is no NL.
1605 */
1606 char_u *
1607channel_first_nl(readq_T *node)
1608{
1609 char_u *buffer = node->rq_buffer;
1610 long_u i;
1611
1612 for (i = 0; i < node->rq_buflen; ++i)
1613 if (buffer[i] == NL)
1614 return buffer + i;
1615 return NULL;
1616}
1617
1618/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001619 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001620 * The caller must free it.
1621 * Returns NULL if there is nothing.
1622 */
1623 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001624channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001625{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001626 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001627 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001628 char_u *p;
1629
Bram Moolenaar77073442016-02-13 23:23:53 +01001630 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001631 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001632 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001633 p = node->rq_buffer;
1634 head->rq_next = node->rq_next;
1635 if (node->rq_next == NULL)
1636 head->rq_prev = NULL;
1637 else
1638 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001639 vim_free(node);
1640 return p;
1641}
1642
1643/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001644 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001645 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001646 */
1647 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001648channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001649{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001650 readq_T *head = &channel->ch_part[part].ch_head;
1651 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001652 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001653 char_u *res;
1654 char_u *p;
1655
1656 /* If there is only one buffer just get that one. */
1657 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1658 return channel_get(channel, part);
1659
1660 /* Concatenate everything into one buffer. */
1661 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001662 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001663 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001664 if (res == NULL)
1665 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001666 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001667 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001668 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001669 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001670 p += node->rq_buflen;
1671 }
1672 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001673
1674 /* Free all buffers */
1675 do
1676 {
1677 p = channel_get(channel, part);
1678 vim_free(p);
1679 } while (p != NULL);
1680
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001681 /* turn all NUL into NL */
1682 while (len > 0)
1683 {
1684 --len;
1685 if (res[len] == NUL)
1686 res[len] = NL;
1687 }
1688
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001689 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001690}
1691
1692/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001693 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001694 * Caller must check these bytes are available.
1695 */
1696 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001697channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001698{
1699 readq_T *head = &channel->ch_part[part].ch_head;
1700 readq_T *node = head->rq_next;
1701 char_u *buf = node->rq_buffer;
1702
1703 mch_memmove(buf, buf + len, node->rq_buflen - len);
1704 node->rq_buflen -= len;
1705}
1706
1707/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001708 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001709 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001710 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001711 */
1712 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001713channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001714{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001715 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001716 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001717 readq_T *last_node;
1718 readq_T *n;
1719 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001720 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001721 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001722
Bram Moolenaar77073442016-02-13 23:23:53 +01001723 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001724 return FAIL;
1725
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001726 last_node = node->rq_next;
1727 len = node->rq_buflen + last_node->rq_buflen + 1;
1728 if (want_nl)
1729 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001730 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001731 {
1732 last_node = last_node->rq_next;
1733 len += last_node->rq_buflen;
1734 }
1735
1736 p = newbuf = alloc(len);
1737 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001738 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001739 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001740 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001741 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001742 node->rq_buffer = newbuf;
1743 for (n = node; n != last_node; )
1744 {
1745 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001746 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001747 p += n->rq_buflen;
1748 vim_free(n->rq_buffer);
1749 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001750 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001751
1752 /* dispose of the collapsed nodes and their buffers */
1753 for (n = node->rq_next; n != last_node; )
1754 {
1755 n = n->rq_next;
1756 vim_free(n->rq_prev);
1757 }
1758 node->rq_next = last_node->rq_next;
1759 if (last_node->rq_next == NULL)
1760 head->rq_prev = node;
1761 else
1762 last_node->rq_next->rq_prev = node;
1763 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001764 return OK;
1765}
1766
1767/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001768 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001769 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001770 * Returns OK or FAIL.
1771 */
1772 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001773channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001774 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001775{
1776 readq_T *node;
1777 readq_T *head = &channel->ch_part[part].ch_head;
1778 char_u *p;
1779 int i;
1780
1781 node = (readq_T *)alloc(sizeof(readq_T));
1782 if (node == NULL)
1783 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001784 /* A NUL is added at the end, because netbeans code expects that.
1785 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001786 node->rq_buffer = alloc(len + 1);
1787 if (node->rq_buffer == NULL)
1788 {
1789 vim_free(node);
1790 return FAIL; /* out of memory */
1791 }
1792
1793 if (channel->ch_part[part].ch_mode == MODE_NL)
1794 {
1795 /* Drop any CR before a NL. */
1796 p = node->rq_buffer;
1797 for (i = 0; i < len; ++i)
1798 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1799 *p++ = buf[i];
1800 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001801 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 }
1803 else
1804 {
1805 mch_memmove(node->rq_buffer, buf, len);
1806 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001807 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001808 }
1809
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001810 if (prepend)
1811 {
1812 /* preend node to the head of the queue */
1813 node->rq_next = head->rq_next;
1814 node->rq_prev = NULL;
1815 if (head->rq_next == NULL)
1816 head->rq_prev = node;
1817 else
1818 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001819 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001820 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001821 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001822 {
1823 /* append node to the tail of the queue */
1824 node->rq_next = NULL;
1825 node->rq_prev = head->rq_prev;
1826 if (head->rq_prev == NULL)
1827 head->rq_next = node;
1828 else
1829 head->rq_prev->rq_next = node;
1830 head->rq_prev = node;
1831 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001832
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001833 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001834 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001835 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001836 fprintf(log_fd, "'");
1837 if (fwrite(buf, len, 1, log_fd) != 1)
1838 return FAIL;
1839 fprintf(log_fd, "'\n");
1840 }
1841 return OK;
1842}
1843
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001844/*
1845 * Try to fill the buffer of "reader".
1846 * Returns FALSE when nothing was added.
1847 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001848 static int
1849channel_fill(js_read_T *reader)
1850{
1851 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001852 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001853 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001854 int keeplen;
1855 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001856 char_u *p;
1857
1858 if (next == NULL)
1859 return FALSE;
1860
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001861 keeplen = reader->js_end - reader->js_buf;
1862 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001863 {
1864 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001865 addlen = (int)STRLEN(next);
1866 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001867 if (p == NULL)
1868 {
1869 vim_free(next);
1870 return FALSE;
1871 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001872 mch_memmove(p, reader->js_buf, keeplen);
1873 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001874 vim_free(next);
1875 next = p;
1876 }
1877
1878 vim_free(reader->js_buf);
1879 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001880 return TRUE;
1881}
1882
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001883/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001884 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001885 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001886 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001887 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001888 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001889channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890{
1891 js_read_T reader;
1892 typval_T listtv;
1893 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001894 chanpart_T *chanpart = &channel->ch_part[part];
1895 jsonq_T *head = &chanpart->ch_json_head;
1896 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001897 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001898
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001899 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001900 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001901
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001902 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001903 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001904 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001905 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001906 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001907
1908 /* When a message is incomplete we wait for a short while for more to
1909 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001910 * or list will make us hang.
1911 * Do not generate error messages, they will be written in a channel log. */
1912 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001913 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001914 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001915 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001916 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001917 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001918 /* Only accept the response when it is a list with at least two
1919 * items. */
1920 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001921 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001922 if (listtv.v_type != VAR_LIST)
1923 ch_error(channel, "Did not receive a list, discarding");
1924 else
1925 ch_errorn(channel, "Expected list with two items, got %d",
1926 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001927 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001928 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001929 else
1930 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001931 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1932 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001933 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001934 else
1935 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001936 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001937 item->jq_value = alloc_tv();
1938 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001939 {
1940 vim_free(item);
1941 clear_tv(&listtv);
1942 }
1943 else
1944 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001945 *item->jq_value = listtv;
1946 item->jq_prev = head->jq_prev;
1947 head->jq_prev = item;
1948 item->jq_next = NULL;
1949 if (item->jq_prev == NULL)
1950 head->jq_next = item;
1951 else
1952 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001953 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001954 }
1955 }
1956 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001957
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001958 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001959 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001960 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001961 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001962 size_t buflen = STRLEN(reader.js_buf);
1963
1964 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001965 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001966 /* First time encountering incomplete message or after receiving
1967 * more (but still incomplete): set a deadline of 100 msec. */
1968 ch_logn(channel,
1969 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001970 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001971 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001972 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001973#ifdef WIN32
1974 chanpart->ch_deadline = GetTickCount() + 100L;
1975#else
1976 gettimeofday(&chanpart->ch_deadline, NULL);
1977 chanpart->ch_deadline.tv_usec += 100 * 1000;
1978 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1979 {
1980 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1981 ++chanpart->ch_deadline.tv_sec;
1982 }
1983#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001984 }
1985 else
1986 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001987 int timeout;
1988#ifdef WIN32
1989 timeout = GetTickCount() > chanpart->ch_deadline;
1990#else
1991 {
1992 struct timeval now_tv;
1993
1994 gettimeofday(&now_tv, NULL);
1995 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1996 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1997 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1998 }
1999#endif
2000 if (timeout)
2001 {
2002 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002003 chanpart->ch_wait_len = 0;
2004 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002005 }
2006 else
2007 {
2008 reader.js_used = 0;
2009 ch_log(channel, "still waiting on incomplete message");
2010 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002011 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002012 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002013
2014 if (status == FAIL)
2015 {
2016 ch_error(channel, "Decoding failed - discarding input");
2017 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002018 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002019 }
2020 else if (reader.js_buf[reader.js_used] != NUL)
2021 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002022 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002023 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002024 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2025 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002026 ret = status == MAYBE ? FALSE: TRUE;
2027 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002028 else
2029 ret = FALSE;
2030
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002031 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002032 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002033}
2034
2035/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002036 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002037 */
2038 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002039remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002040{
Bram Moolenaar77073442016-02-13 23:23:53 +01002041 if (node->cq_prev == NULL)
2042 head->cq_next = node->cq_next;
2043 else
2044 node->cq_prev->cq_next = node->cq_next;
2045 if (node->cq_next == NULL)
2046 head->cq_prev = node->cq_prev;
2047 else
2048 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002049}
2050
2051/*
2052 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002053 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002054 */
2055 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002056remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057{
Bram Moolenaar77073442016-02-13 23:23:53 +01002058 if (node->jq_prev == NULL)
2059 head->jq_next = node->jq_next;
2060 else
2061 node->jq_prev->jq_next = node->jq_next;
2062 if (node->jq_next == NULL)
2063 head->jq_prev = node->jq_prev;
2064 else
2065 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066 vim_free(node);
2067}
2068
2069/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002070 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002071 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002072 * When "id" is zero or negative jut get the first message. But not the one
2073 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002074 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002075 * Return OK when found and return the value in "rettv".
2076 * Return FAIL otherwise.
2077 */
2078 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002079channel_get_json(
2080 channel_T *channel,
2081 ch_part_T part,
2082 int id,
2083 int without_callback,
2084 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002085{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002086 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002087 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002088
Bram Moolenaar77073442016-02-13 23:23:53 +01002089 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002090 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002091 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002092 typval_T *tv = &l->lv_first->li_tv;
2093
Bram Moolenaar958dc692016-12-01 15:34:12 +01002094 if ((without_callback || !item->jq_no_callback)
2095 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002096 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002097 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002098 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002099 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002100 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002101 if (tv->v_type == VAR_NUMBER)
2102 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002103 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002104 return OK;
2105 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002106 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002107 }
2108 return FAIL;
2109}
2110
Bram Moolenaar958dc692016-12-01 15:34:12 +01002111/*
2112 * Put back "rettv" into the JSON queue, there was no callback for it.
2113 * Takes over the values in "rettv".
2114 */
2115 static void
2116channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2117{
2118 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2119 jsonq_T *item = head->jq_next;
2120 jsonq_T *newitem;
2121
2122 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2123 /* last item was pushed back, append to the end */
2124 item = NULL;
2125 else while (item != NULL && item->jq_no_callback)
2126 /* append after the last item that was pushed back */
2127 item = item->jq_next;
2128
2129 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2130 if (newitem == NULL)
2131 clear_tv(rettv);
2132 else
2133 {
2134 newitem->jq_value = alloc_tv();
2135 if (newitem->jq_value == NULL)
2136 {
2137 vim_free(newitem);
2138 clear_tv(rettv);
2139 }
2140 else
2141 {
2142 newitem->jq_no_callback = FALSE;
2143 *newitem->jq_value = *rettv;
2144 if (item == NULL)
2145 {
2146 /* append to the end */
2147 newitem->jq_prev = head->jq_prev;
2148 head->jq_prev = newitem;
2149 newitem->jq_next = NULL;
2150 if (newitem->jq_prev == NULL)
2151 head->jq_next = newitem;
2152 else
2153 newitem->jq_prev->jq_next = newitem;
2154 }
2155 else
2156 {
2157 /* append after "item" */
2158 newitem->jq_prev = item;
2159 newitem->jq_next = item->jq_next;
2160 item->jq_next = newitem;
2161 if (newitem->jq_next == NULL)
2162 head->jq_prev = newitem;
2163 else
2164 newitem->jq_next->jq_prev = newitem;
2165 }
2166 }
2167 }
2168}
2169
Bram Moolenaarece61b02016-02-20 21:39:05 +01002170#define CH_JSON_MAX_ARGS 4
2171
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002172/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002173 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002174 * "argv[0]" is the command string.
2175 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002176 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002177 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002178channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002179{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002180 char_u *cmd = argv[0].vval.v_string;
2181 char_u *arg;
2182 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002183
Bram Moolenaarece61b02016-02-20 21:39:05 +01002184 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002185 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002186 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002187 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002188 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002189 return;
2190 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002191 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002192 if (arg == NULL)
2193 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002194
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002195 if (STRCMP(cmd, "ex") == 0)
2196 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002197 int save_called_emsg = called_emsg;
2198
2199 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002200 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002201 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002202 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002203 --emsg_silent;
2204 if (called_emsg)
2205 ch_logs(channel, "Ex command error: '%s'",
2206 (char *)get_vim_var_str(VV_ERRMSG));
2207 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208 }
2209 else if (STRCMP(cmd, "normal") == 0)
2210 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002212
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002213 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002214 ea.arg = arg;
2215 ea.addr_count = 0;
2216 ea.forceit = TRUE; /* no mapping */
2217 ex_normal(&ea);
2218 }
2219 else if (STRCMP(cmd, "redraw") == 0)
2220 {
2221 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002222
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002223 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002224 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002225 ex_redraw(&ea);
2226 showruler(FALSE);
2227 setcursor();
2228 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002229#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002230 if (gui.in_use)
2231 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002232 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002233 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002234 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002235#endif
2236 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002237 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002238 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002239 int is_call = cmd[0] == 'c';
2240 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002241
Bram Moolenaarece61b02016-02-20 21:39:05 +01002242 if (argv[id_idx].v_type != VAR_UNKNOWN
2243 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002244 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002245 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002247 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002248 }
2249 else if (is_call && argv[2].v_type != VAR_LIST)
2250 {
2251 ch_error(channel, "third argument for call must be a list");
2252 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002253 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002254 }
2255 else
2256 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002257 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002258 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002259 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002260 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002261
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002262 /* Don't pollute the display with errors. */
2263 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002265 {
2266 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002267 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002268 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002269 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002270 {
2271 ch_logs(channel, "Calling '%s'", (char *)arg);
2272 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2273 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002274 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002275
2276 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002277 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002278 int id = argv[id_idx].vval.v_number;
2279
Bram Moolenaar55fab432016-02-07 16:53:13 +01002280 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002281 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002282 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002283 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002284 /* If evaluation failed or the result can't be encoded
2285 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002286 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002287 err_tv.v_type = VAR_STRING;
2288 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002289 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002290 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002291 if (json != NULL)
2292 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002293 channel_send(channel,
2294 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002295 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002296 vim_free(json);
2297 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002298 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002299 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002300 if (tv == &res_tv)
2301 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002302 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002303 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002304 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002305 }
2306 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002307 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002308 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002309 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002310 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002311}
2312
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002313/*
2314 * Invoke the callback at "cbhead".
2315 * Does not redraw but sets channel_need_redraw.
2316 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002317 static void
2318invoke_one_time_callback(
2319 channel_T *channel,
2320 cbq_T *cbhead,
2321 cbq_T *item,
2322 typval_T *argv)
2323{
2324 ch_logs(channel, "Invoking one-time callback %s",
2325 (char *)item->cq_callback);
2326 /* Remove the item from the list first, if the callback
2327 * invokes ch_close() the list will be cleared. */
2328 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002329 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002330 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002331 vim_free(item);
2332}
2333
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002334 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002335append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002336{
2337 buf_T *save_curbuf = curbuf;
2338 linenr_T lnum = buffer->b_ml.ml_line_count;
2339 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002340 chanpart_T *ch_part = &channel->ch_part[part];
2341 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002342 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002343
2344 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2345 {
2346 if (!ch_part->ch_nomod_error)
2347 {
2348 ch_error(channel, "Buffer is not modifiable, cannot append");
2349 ch_part->ch_nomod_error = TRUE;
2350 }
2351 return;
2352 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002353
2354 /* If the buffer is also used as input insert above the last
2355 * line. Don't write these lines. */
2356 if (save_write_to)
2357 {
2358 --lnum;
2359 buffer->b_write_to_channel = FALSE;
2360 }
2361
2362 /* Append to the buffer */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002363 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002364
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002365 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002366 curbuf = buffer;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002367 curwin->w_buffer = curbuf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002368 u_sync(TRUE);
2369 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002370 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002371
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002372 if (empty)
2373 {
2374 /* The buffer is empty, replace the first (dummy) line. */
2375 ml_replace(lnum, msg, TRUE);
2376 lnum = 0;
2377 }
2378 else
2379 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002380 appended_lines_mark(lnum, 1L);
2381 curbuf = save_curbuf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002382 curwin->w_buffer = curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002383 if (ch_part->ch_nomodifiable)
2384 buffer->b_p_ma = FALSE;
2385 else
2386 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002387
2388 if (buffer->b_nwindows > 0)
2389 {
2390 win_T *wp;
2391 win_T *save_curwin;
2392
2393 FOR_ALL_WINDOWS(wp)
2394 {
2395 if (wp->w_buffer == buffer
2396 && (save_write_to
2397 ? wp->w_cursor.lnum == lnum + 1
2398 : (wp->w_cursor.lnum == lnum
2399 && wp->w_cursor.col == 0)))
2400 {
2401 ++wp->w_cursor.lnum;
2402 save_curwin = curwin;
2403 curwin = wp;
2404 curbuf = curwin->w_buffer;
2405 scroll_cursor_bot(0, FALSE);
2406 curwin = save_curwin;
2407 curbuf = curwin->w_buffer;
2408 }
2409 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002410 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002411 channel_need_redraw = TRUE;
2412 }
2413
2414 if (save_write_to)
2415 {
2416 channel_T *ch;
2417
2418 /* Find channels reading from this buffer and adjust their
2419 * next-to-read line number. */
2420 buffer->b_write_to_channel = TRUE;
2421 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2422 {
2423 chanpart_T *in_part = &ch->ch_part[PART_IN];
2424
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002425 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002426 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2427 }
2428 }
2429}
2430
Bram Moolenaar437905c2016-04-26 19:01:05 +02002431 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002432drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002433{
2434 char_u *msg;
2435
2436 while ((msg = channel_get(channel, part)) != NULL)
2437 {
2438 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2439 vim_free(msg);
2440 }
2441}
2442
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002443/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002444 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002445 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002446 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002447 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002448 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002449may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002450{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002451 char_u *msg = NULL;
2452 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002453 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002454 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002455 chanpart_T *ch_part = &channel->ch_part[part];
2456 ch_mode_T ch_mode = ch_part->ch_mode;
2457 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002458 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002459 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002460 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002461 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002462 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002463
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002464 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002465 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002466 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002467
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002468 /* Use a message-specific callback, part callback or channel callback */
2469 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2470 if (cbitem->cq_seq_nr == 0)
2471 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002472 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002473 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002474 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002475 partial = cbitem->cq_partial;
2476 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002477 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002478 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002479 callback = ch_part->ch_callback;
2480 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002481 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002482 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002483 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002484 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002485 partial = channel->ch_partial;
2486 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002487
Bram Moolenaarec68a992016-10-03 21:37:41 +02002488 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002489 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2490 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002491 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002492 /* buffer was wiped out or unloaded */
2493 ch_logs(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002494 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002495 buffer = NULL;
2496 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002497
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002498 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002499 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002500 listitem_T *item;
2501 int argc = 0;
2502
Bram Moolenaard7ece102016-02-02 23:23:02 +01002503 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002504 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002505 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002506 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002507 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002508 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002509 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002510 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002511
Bram Moolenaarece61b02016-02-20 21:39:05 +01002512 for (item = listtv->vval.v_list->lv_first;
2513 item != NULL && argc < CH_JSON_MAX_ARGS;
2514 item = item->li_next)
2515 argv[argc++] = item->li_tv;
2516 while (argc < CH_JSON_MAX_ARGS)
2517 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002518
Bram Moolenaarece61b02016-02-20 21:39:05 +01002519 if (argv[0].v_type == VAR_STRING)
2520 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002521 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002522 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002523 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002524 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002525 }
2526
Bram Moolenaarece61b02016-02-20 21:39:05 +01002527 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002528 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002529 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002530 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002531 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002532 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002533 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002534 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002535 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002536 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002537 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002538 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002539 return FALSE;
2540 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002541 else
2542 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002543 /* If there is no callback or buffer drop the message. */
2544 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002545 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002546 /* If there is a close callback it may use ch_read() to get the
2547 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002548 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002549 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002550 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002551 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002552
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002553 if (ch_mode == MODE_NL)
2554 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002555 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002556 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002557 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002558
2559 /* See if we have a message ending in NL in the first buffer. If
2560 * not try to concatenate the first and the second buffer. */
2561 while (TRUE)
2562 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002563 node = channel_peek(channel, part);
2564 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002565 if (nl != NULL)
2566 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002567 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002568 {
2569 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2570 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002571 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002572 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002573 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002574 buf = node->rq_buffer;
2575
Bram Moolenaarec68a992016-10-03 21:37:41 +02002576 if (nl == NULL)
2577 {
2578 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002579 char_u *new_buf;
2580
2581 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2582 if (new_buf == NULL)
2583 /* This might fail over and over again, should the message
2584 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002585 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002586 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002587 node->rq_buffer = buf;
2588 nl = buf + node->rq_buflen++;
2589 *nl = NUL;
2590 }
2591
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002592 /* Convert NUL to NL, the internal representation. */
2593 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2594 if (*p == NUL)
2595 *p = NL;
2596
2597 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002598 {
2599 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002600 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002601 *nl = NUL;
2602 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002603 else
2604 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002605 /* Copy the message into allocated memory (excluding the NL)
2606 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002607 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002608 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002609 }
2610 }
2611 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002612 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002613 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002614 * get everything we have.
2615 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002616 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002617 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002618
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002619 if (msg == NULL)
2620 return FALSE; /* out of memory (and avoids Coverity warning) */
2621
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002622 argv[1].v_type = VAR_STRING;
2623 argv[1].vval.v_string = msg;
2624 }
2625
Bram Moolenaara07fec92016-02-05 21:04:08 +01002626 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002627 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002628 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002629
Bram Moolenaar958dc692016-12-01 15:34:12 +01002630 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002631 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002632 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002633 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002634 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002635 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002636 break;
2637 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002638 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002639 {
2640 if (channel->ch_drop_never)
2641 {
2642 /* message must be read with ch_read() */
2643 channel_push_json(channel, part, listtv);
2644 listtv = NULL;
2645 }
2646 else
2647 ch_logn(channel, "Dropping message %d without callback",
2648 seq_nr);
2649 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002650 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002651 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002652 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002653 if (buffer != NULL)
2654 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002655 if (msg == NULL)
2656 /* JSON or JS mode: re-encode the message. */
2657 msg = json_encode(listtv, ch_mode);
2658 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002659 {
2660 if (buffer->b_term != NULL)
2661 write_to_term(buffer, msg, channel);
2662 else
2663 append_to_buffer(buffer, msg, channel, part);
2664 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002665 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002666
Bram Moolenaar187db502016-02-27 14:44:26 +01002667 if (callback != NULL)
2668 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002669 if (cbitem != NULL)
2670 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2671 else
2672 {
2673 /* invoke the channel callback */
2674 ch_logs(channel, "Invoking channel callback %s",
2675 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002676 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002677 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002678 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002679 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002680 else
Bram Moolenaar958dc692016-12-01 15:34:12 +01002681 ch_logn(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002682
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002683 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002684 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002685 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002686
2687 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002688}
2689
2690/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002691 * Return TRUE when channel "channel" is open for writing to.
2692 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002693 */
2694 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002695channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002696{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002697 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002698 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002699}
2700
2701/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002702 * Return TRUE when channel "channel" is open for reading or writing.
2703 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002704 */
2705 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002706channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002707{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002708 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002709 || channel->CH_IN_FD != INVALID_FD
2710 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002711 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002712}
2713
2714/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002715 * Return TRUE if "channel" has JSON or other typeahead.
2716 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002717 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002718channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002719{
2720 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2721
2722 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2723 {
2724 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2725 jsonq_T *item = head->jq_next;
2726
2727 return item != NULL;
2728 }
2729 return channel_peek(channel, part) != NULL;
2730}
2731
2732/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002733 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002734 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002735 */
2736 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002737channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002738{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002739 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002740 int has_readahead = FALSE;
2741
Bram Moolenaar77073442016-02-13 23:23:53 +01002742 if (channel == NULL)
2743 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002744 if (req_part == PART_OUT)
2745 {
2746 if (channel->CH_OUT_FD != INVALID_FD)
2747 return "open";
2748 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002749 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002750 }
2751 else if (req_part == PART_ERR)
2752 {
2753 if (channel->CH_ERR_FD != INVALID_FD)
2754 return "open";
2755 if (channel_has_readahead(channel, PART_ERR))
2756 has_readahead = TRUE;
2757 }
2758 else
2759 {
2760 if (channel_is_open(channel))
2761 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002762 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002763 if (channel_has_readahead(channel, part))
2764 {
2765 has_readahead = TRUE;
2766 break;
2767 }
2768 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002769
2770 if (has_readahead)
2771 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002772 return "closed";
2773}
2774
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002775 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002776channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002777{
2778 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002779 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002780 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002781 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002782 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002783
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002784 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002785 STRCAT(namebuf, "_");
2786 tail = STRLEN(namebuf);
2787
2788 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002789 if (chanpart->ch_fd != INVALID_FD)
2790 status = "open";
2791 else if (channel_has_readahead(channel, part))
2792 status = "buffered";
2793 else
2794 status = "closed";
2795 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002796
2797 STRCPY(namebuf + tail, "mode");
2798 switch (chanpart->ch_mode)
2799 {
2800 case MODE_NL: s = "NL"; break;
2801 case MODE_RAW: s = "RAW"; break;
2802 case MODE_JSON: s = "JSON"; break;
2803 case MODE_JS: s = "JS"; break;
2804 }
2805 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2806
2807 STRCPY(namebuf + tail, "io");
2808 if (part == PART_SOCK)
2809 s = "socket";
2810 else switch (chanpart->ch_io)
2811 {
2812 case JIO_NULL: s = "null"; break;
2813 case JIO_PIPE: s = "pipe"; break;
2814 case JIO_FILE: s = "file"; break;
2815 case JIO_BUFFER: s = "buffer"; break;
2816 case JIO_OUT: s = "out"; break;
2817 }
2818 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2819
2820 STRCPY(namebuf + tail, "timeout");
2821 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2822}
2823
2824 void
2825channel_info(channel_T *channel, dict_T *dict)
2826{
2827 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002828 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002829
2830 if (channel->ch_hostname != NULL)
2831 {
2832 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2833 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2834 channel_part_info(channel, dict, "sock", PART_SOCK);
2835 }
2836 else
2837 {
2838 channel_part_info(channel, dict, "out", PART_OUT);
2839 channel_part_info(channel, dict, "err", PART_ERR);
2840 channel_part_info(channel, dict, "in", PART_IN);
2841 }
2842}
2843
Bram Moolenaar77073442016-02-13 23:23:53 +01002844/*
2845 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002846 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002847 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002848 */
2849 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002850channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002851{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002852 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002853
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002854#ifdef FEAT_GUI
2855 channel_gui_unregister(channel);
2856#endif
2857
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002858 ch_close_part(channel, PART_SOCK);
2859 ch_close_part(channel, PART_IN);
2860 ch_close_part(channel, PART_OUT);
2861 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002862
Bram Moolenaar8b374212016-02-24 20:43:06 +01002863 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002864 {
2865 typval_T argv[1];
2866 typval_T rettv;
2867 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002868 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002869
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002870 /* Invoke callbacks before the close callback, since it's weird to
2871 * first invoke the close callback. Increment the refcount to avoid
2872 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002873 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002874 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002875 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002876 while (may_invoke_callback(channel, part))
2877 ;
2878
2879 /* Invoke the close callback, if still set. */
2880 if (channel->ch_close_cb != NULL)
2881 {
2882 ch_logs(channel, "Invoking close callback %s",
2883 (char *)channel->ch_close_cb);
2884 argv[0].v_type = VAR_CHANNEL;
2885 argv[0].vval.v_channel = channel;
2886 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002887 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002888 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002889 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002890 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002891 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002892
2893 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002894 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002895 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002896 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002897
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002898 --channel->ch_refcount;
2899
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002900 if (channel_need_redraw)
2901 {
2902 channel_need_redraw = FALSE;
2903 redraw_after_callback();
2904 }
2905
Bram Moolenaar958dc692016-12-01 15:34:12 +01002906 if (!channel->ch_drop_never)
2907 /* any remaining messages are useless now */
2908 for (part = PART_SOCK; part < PART_IN; ++part)
2909 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002910 }
2911
2912 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002913}
2914
Bram Moolenaard04a0202016-01-26 23:30:18 +01002915/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002916 * Close the "in" part channel "channel".
2917 */
2918 void
2919channel_close_in(channel_T *channel)
2920{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002921 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002922}
2923
2924/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002925 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002926 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002927 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002928channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002929{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002930 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2931 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002932
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002933 while (channel_peek(channel, part) != NULL)
2934 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002935
2936 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002937 {
2938 cbq_T *node = cb_head->cq_next;
2939
2940 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002941 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002942 vim_free(node);
2943 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002944
2945 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002946 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002947 free_tv(json_head->jq_next->jq_value);
2948 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002949 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002950
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002951 free_callback(channel->ch_part[part].ch_callback,
2952 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002953 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002954 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002955}
2956
2957/*
2958 * Clear all the read buffers on "channel".
2959 */
2960 void
2961channel_clear(channel_T *channel)
2962{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002963 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002964 vim_free(channel->ch_hostname);
2965 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002966 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002967 channel_clear_one(channel, PART_OUT);
2968 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002969 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002970 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002971 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002972 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002973 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002974 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002975 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002976}
2977
Bram Moolenaar77073442016-02-13 23:23:53 +01002978#if defined(EXITFREE) || defined(PROTO)
2979 void
2980channel_free_all(void)
2981{
2982 channel_T *channel;
2983
Bram Moolenaard6051b52016-02-28 15:49:03 +01002984 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002985 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2986 channel_clear(channel);
2987}
2988#endif
2989
2990
Bram Moolenaar715d2852016-04-30 17:06:31 +02002991/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002992#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002993
2994/* Buffer size for reading incoming messages. */
2995#define MAXMSGSIZE 4096
2996
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002997#if defined(HAVE_SELECT)
2998/*
2999 * Add write fds where we are waiting for writing to be possible.
3000 */
3001 static int
3002channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3003{
3004 int maxfd = maxfd_arg;
3005 channel_T *ch;
3006
3007 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3008 {
3009 chanpart_T *in_part = &ch->ch_part[PART_IN];
3010
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003011 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003012 {
3013 FD_SET((int)in_part->ch_fd, wfds);
3014 if ((int)in_part->ch_fd >= maxfd)
3015 maxfd = (int)in_part->ch_fd + 1;
3016 }
3017 }
3018 return maxfd;
3019}
3020#else
3021/*
3022 * Add write fds where we are waiting for writing to be possible.
3023 */
3024 static int
3025channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3026{
3027 int nfd = nfd_in;
3028 channel_T *ch;
3029
3030 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3031 {
3032 chanpart_T *in_part = &ch->ch_part[PART_IN];
3033
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003034 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003035 {
3036 in_part->ch_poll_idx = nfd;
3037 fds[nfd].fd = in_part->ch_fd;
3038 fds[nfd].events = POLLOUT;
3039 ++nfd;
3040 }
3041 else
3042 in_part->ch_poll_idx = -1;
3043 }
3044 return nfd;
3045}
3046#endif
3047
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003048typedef enum {
3049 CW_READY,
3050 CW_NOT_READY,
3051 CW_ERROR
3052} channel_wait_result;
3053
Bram Moolenaard04a0202016-01-26 23:30:18 +01003054/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003055 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003056 * Return CW_READY when there is something to read.
3057 * Return CW_NOT_READY when there is nothing to read.
3058 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003059 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003060 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003061channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003062{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003063 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003064 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003065
Bram Moolenaard8070362016-02-15 21:56:54 +01003066# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003067 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003068 {
3069 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003070 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003071 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003072 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003073
3074 /* reading from a pipe, not a socket */
3075 while (TRUE)
3076 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003077 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3078
3079 if (r && nread > 0)
3080 return CW_READY;
3081 if (r == 0)
3082 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003083
3084 /* perhaps write some buffer lines */
3085 channel_write_any_lines();
3086
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003087 sleep_time = deadline - GetTickCount();
3088 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003089 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003090 /* Wait for a little while. Very short at first, up to 10 msec
3091 * after looping a few times. */
3092 if (sleep_time > delay)
3093 sleep_time = delay;
3094 Sleep(sleep_time);
3095 delay = delay * 2;
3096 if (delay > 10)
3097 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003098 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003099 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003100 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003101#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003102 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003103#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003104 struct timeval tval;
3105 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003106 fd_set wfds;
3107 int ret;
3108 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003109
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003110 tval.tv_sec = timeout / 1000;
3111 tval.tv_usec = (timeout % 1000) * 1000;
3112 for (;;)
3113 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003114 FD_ZERO(&rfds);
3115 FD_SET((int)fd, &rfds);
3116
3117 /* Write lines to a pipe when a pipe can be written to. Need to
3118 * set this every time, some buffers may be done. */
3119 maxfd = (int)fd + 1;
3120 FD_ZERO(&wfds);
3121 maxfd = channel_fill_wfds(maxfd, &wfds);
3122
3123 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003124# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003125 SOCK_ERRNO;
3126 if (ret == -1 && errno == EINTR)
3127 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003128# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003129 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003130 {
3131 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003132 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003133 channel_write_any_lines();
3134 continue;
3135 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003136 break;
3137 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003138#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003139 for (;;)
3140 {
3141 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3142 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003143
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003144 fds[0].fd = fd;
3145 fds[0].events = POLLIN;
3146 nfd = channel_fill_poll_write(nfd, fds);
3147 if (poll(fds, nfd, timeout) > 0)
3148 {
3149 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003150 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003151 channel_write_any_lines();
3152 continue;
3153 }
3154 break;
3155 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003156#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003157 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003158 return CW_NOT_READY;
3159}
3160
3161 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003162ch_close_part_on_error(
3163 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003164{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003165 char msgbuf[80];
3166
3167 vim_snprintf(msgbuf, sizeof(msgbuf),
3168 "%%s(): Read %s from ch_part[%d], closing",
3169 (is_err ? "error" : "EOF"), part);
3170
3171 if (is_err)
3172 /* Do not call emsg(), most likely the other end just exited. */
3173 ch_errors(channel, msgbuf, func);
3174 else
3175 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003176
3177 /* Queue a "DETACH" netbeans message in the command queue in order to
3178 * terminate the netbeans session later. Do not end the session here
3179 * directly as we may be running in the context of a call to
3180 * netbeans_parse_messages():
3181 * netbeans_parse_messages
3182 * -> autocmd triggered while processing the netbeans cmd
3183 * -> ui_breakcheck
3184 * -> gui event loop or select loop
3185 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003186 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003187 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003188 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003189 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003190 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3191
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003192 /* When reading is not possible close this part of the channel. Don't
3193 * close the channel yet, there may be something to read on another part. */
3194 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003195
3196#ifdef FEAT_GUI
3197 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003198 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003199#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003200}
3201
3202 static void
3203channel_close_now(channel_T *channel)
3204{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003205 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003206 if (channel->ch_nb_close_cb != NULL)
3207 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003208 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003209}
3210
3211/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003212 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003213 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003214 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003215 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003216 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003217channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003218{
3219 static char_u *buf = NULL;
3220 int len = 0;
3221 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003222 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003223 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003224
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003225 fd = channel->ch_part[part].ch_fd;
3226 if (fd == INVALID_FD)
3227 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003228 ch_errors(channel, "channel_read() called while %s part is closed",
3229 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003230 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003231 }
3232 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003233
3234 /* Allocate a buffer to read into. */
3235 if (buf == NULL)
3236 {
3237 buf = alloc(MAXMSGSIZE);
3238 if (buf == NULL)
3239 return; /* out of memory! */
3240 }
3241
3242 /* Keep on reading for as long as there is something to read.
3243 * Use select() or poll() to avoid blocking on a message that is exactly
3244 * MAXMSGSIZE long. */
3245 for (;;)
3246 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003247 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003248 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003249 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003250 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003251 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003252 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003253 if (len <= 0)
3254 break; /* error or nothing more to read */
3255
3256 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003257 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003258 readlen += len;
3259 if (len < MAXMSGSIZE)
3260 break; /* did read everything that's available */
3261 }
3262
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003263 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003264 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003265 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003266
3267#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003268 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003269 if (CH_HAS_GUI && gtk_main_level() > 0)
3270 gtk_main_quit();
3271#endif
3272}
3273
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003274/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003275 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003276 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003277 * Returns what was read in allocated memory.
3278 * Returns NULL in case of error or timeout.
3279 */
3280 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003281channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003282{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003283 char_u *buf;
3284 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003285 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003286 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003287 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003288 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003289
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003290 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003291 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003292
3293 while (TRUE)
3294 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003295 node = channel_peek(channel, part);
3296 if (node != NULL)
3297 {
3298 if (mode == MODE_RAW || (mode == MODE_NL
3299 && channel_first_nl(node) != NULL))
3300 /* got a complete message */
3301 break;
3302 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3303 continue;
3304 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003305
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003306 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003307 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003308 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003309 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003310 {
3311 ch_log(channel, "Timed out");
3312 return NULL;
3313 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003314 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003315 }
3316
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003317 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003318 if (mode == MODE_RAW)
3319 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003320 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003321 }
3322 else
3323 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003324 char_u *p;
3325
3326 buf = node->rq_buffer;
3327 nl = channel_first_nl(node);
3328
3329 /* Convert NUL to NL, the internal representation. */
3330 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3331 if (*p == NUL)
3332 *p = NL;
3333
3334 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003335 {
3336 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003337 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003338 *nl = NUL;
3339 }
3340 else
3341 {
3342 /* Copy the message into allocated memory and remove it from the
3343 * buffer. */
3344 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003345 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003346 }
3347 }
3348 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003349 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003350 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003351}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003352
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003353/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003354 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003355 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003356 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003357 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003358 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003359 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003360channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003361 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003362 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003363 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003364 int id,
3365 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003366{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003367 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003368 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003369 int timeout;
3370 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003371
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003372 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003373 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003374 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003375 for (;;)
3376 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003377 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003378
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003379 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003380 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003381 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003382 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003383 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003384 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003385
Bram Moolenaard7ece102016-02-02 23:23:02 +01003386 if (!more)
3387 {
3388 /* Handle any other messages in the queue. If done some more
3389 * messages may have arrived. */
3390 if (channel_parse_messages())
3391 continue;
3392
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003393 /* Wait for up to the timeout. If there was an incomplete message
3394 * use the deadline for that. */
3395 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003396 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003397 {
3398#ifdef WIN32
3399 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3400#else
3401 {
3402 struct timeval now_tv;
3403
3404 gettimeofday(&now_tv, NULL);
3405 timeout = (chanpart->ch_deadline.tv_sec
3406 - now_tv.tv_sec) * 1000
3407 + (chanpart->ch_deadline.tv_usec
3408 - now_tv.tv_usec) / 1000
3409 + 1;
3410 }
3411#endif
3412 if (timeout < 0)
3413 {
3414 /* Something went wrong, channel_parse_json() didn't
3415 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003416 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003417 timeout = timeout_arg;
3418 }
3419 else if (timeout > timeout_arg)
3420 timeout = timeout_arg;
3421 }
3422 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003423 if (fd == INVALID_FD
3424 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003425 {
3426 if (timeout == timeout_arg)
3427 {
3428 if (fd != INVALID_FD)
3429 ch_log(channel, "Timed out");
3430 break;
3431 }
3432 }
3433 else
3434 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003435 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003436 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003437 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003438 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003439}
3440
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003441/*
3442 * Common for ch_read() and ch_readraw().
3443 */
3444 void
3445common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3446{
3447 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003448 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003449 jobopt_T opt;
3450 int mode;
3451 int timeout;
3452 int id = -1;
3453 typval_T *listtv = NULL;
3454
3455 /* return an empty string by default */
3456 rettv->v_type = VAR_STRING;
3457 rettv->vval.v_string = NULL;
3458
3459 clear_job_options(&opt);
3460 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3461 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003462 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003463
Bram Moolenaar437905c2016-04-26 19:01:05 +02003464 if (opt.jo_set & JO_PART)
3465 part = opt.jo_part;
3466 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003467 if (channel != NULL)
3468 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003469 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003470 part = channel_part_read(channel);
3471 mode = channel_get_mode(channel, part);
3472 timeout = channel_get_timeout(channel, part);
3473 if (opt.jo_set & JO_TIMEOUT)
3474 timeout = opt.jo_timeout;
3475
3476 if (raw || mode == MODE_RAW || mode == MODE_NL)
3477 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3478 else
3479 {
3480 if (opt.jo_set & JO_ID)
3481 id = opt.jo_id;
3482 channel_read_json_block(channel, part, timeout, id, &listtv);
3483 if (listtv != NULL)
3484 {
3485 *rettv = *listtv;
3486 vim_free(listtv);
3487 }
3488 else
3489 {
3490 rettv->v_type = VAR_SPECIAL;
3491 rettv->vval.v_number = VVAL_NONE;
3492 }
3493 }
3494 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003495
3496theend:
3497 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003498}
3499
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003500# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3501 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003502/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003503 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003504 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003505 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003506 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003507channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003508{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003509 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003510 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003511
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003512 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003513 for (channel = first_channel; channel != NULL;
3514 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003515 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003516 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003517 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003518 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003519 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003520 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003521 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003522 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003523 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003524}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003525# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003526
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003527# if defined(WIN32) || defined(PROTO)
3528/*
3529 * Check the channels for anything that is ready to be read.
3530 * The data is put in the read queue.
3531 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003532 void
3533channel_handle_events(void)
3534{
3535 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003536 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003537 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003538
3539 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3540 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003541 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003542 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003543 {
3544 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003545 if (fd != INVALID_FD)
3546 {
3547 int r = channel_wait(channel, fd, 0);
3548
3549 if (r == CW_READY)
3550 channel_read(channel, part, "channel_handle_events");
3551 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003552 ch_close_part_on_error(channel, part, TRUE,
3553 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003554 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003555 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003556 }
3557}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003558# endif
3559
Bram Moolenaard04a0202016-01-26 23:30:18 +01003560/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003561 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003562 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003563 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003564 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003565 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003566channel_send(
3567 channel_T *channel,
3568 ch_part_T part,
3569 char_u *buf,
3570 int len,
3571 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003572{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003573 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003574 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003575
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003576 fd = channel->ch_part[part].ch_fd;
3577 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003578 {
3579 if (!channel->ch_error && fun != NULL)
3580 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003581 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003582 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003583 }
3584 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003585 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003586 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003587
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003588 if (log_fd != NULL)
3589 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003590 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003591 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003592 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003593 fprintf(log_fd, "'\n");
3594 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003595 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003596 }
3597
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003599 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003600 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003601 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003602 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003603 {
3604 if (!channel->ch_error && fun != NULL)
3605 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003606 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003607 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003608 }
3609 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003610 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003611 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003612
3613 channel->ch_error = FALSE;
3614 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003615}
3616
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003617/*
3618 * Common for "ch_sendexpr()" and "ch_sendraw()".
3619 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003620 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003621 * Otherwise returns NULL.
3622 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003623 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003624send_common(
3625 typval_T *argvars,
3626 char_u *text,
3627 int id,
3628 int eval,
3629 jobopt_T *opt,
3630 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003631 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003632{
3633 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003634 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003635
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003636 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003637 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003638 if (channel == NULL)
3639 return NULL;
3640 part_send = channel_part_send(channel);
3641 *part_read = channel_part_read(channel);
3642
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003643 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3644 return NULL;
3645
3646 /* Set the callback. An empty callback means no callback and not reading
3647 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3648 * allowed. */
3649 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3650 {
3651 if (eval)
3652 {
3653 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3654 return NULL;
3655 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003656 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003657 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003658 }
3659
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003660 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003661 && opt->jo_callback == NULL)
3662 return channel;
3663 return NULL;
3664}
3665
3666/*
3667 * common for "ch_evalexpr()" and "ch_sendexpr()"
3668 */
3669 void
3670ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3671{
3672 char_u *text;
3673 typval_T *listtv;
3674 channel_T *channel;
3675 int id;
3676 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003677 ch_part_T part_send;
3678 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003679 jobopt_T opt;
3680 int timeout;
3681
3682 /* return an empty string by default */
3683 rettv->v_type = VAR_STRING;
3684 rettv->vval.v_string = NULL;
3685
Bram Moolenaar437905c2016-04-26 19:01:05 +02003686 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003687 if (channel == NULL)
3688 return;
3689 part_send = channel_part_send(channel);
3690
3691 ch_mode = channel_get_mode(channel, part_send);
3692 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3693 {
3694 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3695 return;
3696 }
3697
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003698 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003699 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003700 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003701 if (text == NULL)
3702 return;
3703
3704 channel = send_common(argvars, text, id, eval, &opt,
3705 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3706 vim_free(text);
3707 if (channel != NULL && eval)
3708 {
3709 if (opt.jo_set & JO_TIMEOUT)
3710 timeout = opt.jo_timeout;
3711 else
3712 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003713 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3714 == OK)
3715 {
3716 list_T *list = listtv->vval.v_list;
3717
3718 /* Move the item from the list and then change the type to
3719 * avoid the value being freed. */
3720 *rettv = list->lv_last->li_tv;
3721 list->lv_last->li_tv.v_type = VAR_NUMBER;
3722 free_tv(listtv);
3723 }
3724 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003725 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003726}
3727
3728/*
3729 * common for "ch_evalraw()" and "ch_sendraw()"
3730 */
3731 void
3732ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3733{
3734 char_u buf[NUMBUFLEN];
3735 char_u *text;
3736 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003737 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003738 jobopt_T opt;
3739 int timeout;
3740
3741 /* return an empty string by default */
3742 rettv->v_type = VAR_STRING;
3743 rettv->vval.v_string = NULL;
3744
3745 text = get_tv_string_buf(&argvars[1], buf);
3746 channel = send_common(argvars, text, 0, eval, &opt,
3747 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3748 if (channel != NULL && eval)
3749 {
3750 if (opt.jo_set & JO_TIMEOUT)
3751 timeout = opt.jo_timeout;
3752 else
3753 timeout = channel_get_timeout(channel, part_read);
3754 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3755 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003756 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003757}
3758
Bram Moolenaard04a0202016-01-26 23:30:18 +01003759# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003760/*
3761 * Add open channels to the poll struct.
3762 * Return the adjusted struct index.
3763 * The type of "fds" is hidden to avoid problems with the function proto.
3764 */
3765 int
3766channel_poll_setup(int nfd_in, void *fds_in)
3767{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003768 int nfd = nfd_in;
3769 channel_T *channel;
3770 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003771 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003772
Bram Moolenaar77073442016-02-13 23:23:53 +01003773 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003774 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003775 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003776 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003777 chanpart_T *ch_part = &channel->ch_part[part];
3778
3779 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003780 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003781 ch_part->ch_poll_idx = nfd;
3782 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003783 fds[nfd].events = POLLIN;
3784 nfd++;
3785 }
3786 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003787 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003788 }
3789 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003790
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003791 nfd = channel_fill_poll_write(nfd, fds);
3792
Bram Moolenaare0874f82016-01-24 20:36:41 +01003793 return nfd;
3794}
3795
3796/*
3797 * The type of "fds" is hidden to avoid problems with the function proto.
3798 */
3799 int
3800channel_poll_check(int ret_in, void *fds_in)
3801{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003802 int ret = ret_in;
3803 channel_T *channel;
3804 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003805 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003806 int idx;
3807 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003808
Bram Moolenaar77073442016-02-13 23:23:53 +01003809 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003810 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003811 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003812 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003813 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003814
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003815 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003816 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003817 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003818 --ret;
3819 }
3820 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003821
3822 in_part = &channel->ch_part[PART_IN];
3823 idx = in_part->ch_poll_idx;
3824 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3825 {
3826 if (in_part->ch_buf_append)
3827 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003828 if (in_part->ch_bufref.br_buf != NULL)
3829 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003830 }
3831 else
3832 channel_write_in(channel);
3833 --ret;
3834 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003835 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003836
3837 return ret;
3838}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003839# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003840
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003841# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003842/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003843 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003844 */
3845 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003846channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003847{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003848 int maxfd = maxfd_in;
3849 channel_T *channel;
3850 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003851 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003852 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003853
Bram Moolenaar77073442016-02-13 23:23:53 +01003854 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003855 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003856 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003857 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003858 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003859
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003860 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003861 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003862 FD_SET((int)fd, rfds);
3863 if (maxfd < (int)fd)
3864 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003865 }
3866 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003867 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003868
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003869 maxfd = channel_fill_wfds(maxfd, wfds);
3870
Bram Moolenaare0874f82016-01-24 20:36:41 +01003871 return maxfd;
3872}
3873
3874/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003875 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003876 */
3877 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003878channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003879{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003880 int ret = ret_in;
3881 channel_T *channel;
3882 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003883 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003884 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003885 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003886
Bram Moolenaar77073442016-02-13 23:23:53 +01003887 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003888 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003889 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003890 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003891 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003892
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003893 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003894 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003895 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003896 --ret;
3897 }
3898 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003899
3900 in_part = &channel->ch_part[PART_IN];
3901 if (ret > 0 && in_part->ch_fd != INVALID_FD
3902 && FD_ISSET(in_part->ch_fd, wfds))
3903 {
3904 if (in_part->ch_buf_append)
3905 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003906 if (in_part->ch_bufref.br_buf != NULL)
3907 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003908 }
3909 else
3910 channel_write_in(channel);
3911 --ret;
3912 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003913 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003914
3915 return ret;
3916}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003917# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003918
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003919/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003920 * Execute queued up commands.
3921 * Invoked from the main loop when it's safe to execute received commands.
3922 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003923 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003924 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003925channel_parse_messages(void)
3926{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003927 channel_T *channel = first_channel;
3928 int ret = FALSE;
3929 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003930 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003931#ifdef ELAPSED_FUNC
3932 ELAPSED_TYPE start_tv;
3933
3934 ELAPSED_INIT(start_tv);
3935#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003936
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003937 ++safe_to_invoke_callback;
3938
Bram Moolenaard0b65022016-03-06 21:50:33 +01003939 /* Only do this message when another message was given, otherwise we get
3940 * lots of them. */
3941 if (did_log_msg)
3942 {
3943 ch_log(NULL, "looking for messages on channels");
3944 did_log_msg = FALSE;
3945 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003946 while (channel != NULL)
3947 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003948 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003949 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003950 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003951 channel_close_now(channel);
3952 /* channel may have been freed, start over */
3953 channel = first_channel;
3954 continue;
3955 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003956 if (channel->ch_to_be_freed)
3957 {
3958 channel_free(channel);
3959 /* channel has been freed, start over */
3960 channel = first_channel;
3961 continue;
3962 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003963 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003964 {
3965 /* channel is no longer useful, free it */
3966 channel_free(channel);
3967 channel = first_channel;
3968 part = PART_SOCK;
3969 continue;
3970 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003971 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003972 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003973 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003974 /* Increase the refcount, in case the handler causes the channel
3975 * to be unreferenced or closed. */
3976 ++channel->ch_refcount;
3977 r = may_invoke_callback(channel, part);
3978 if (r == OK)
3979 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003980 if (channel_unref(channel) || (r == OK
3981#ifdef ELAPSED_FUNC
3982 /* Limit the time we loop here to 100 msec, otherwise
3983 * Vim becomes unresponsive when the callback takes
3984 * more than a bit of time. */
3985 && ELAPSED_FUNC(start_tv) < 100L
3986#endif
3987 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003988 {
3989 /* channel was freed or something was done, start over */
3990 channel = first_channel;
3991 part = PART_SOCK;
3992 continue;
3993 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003994 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003995 if (part < PART_ERR)
3996 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003997 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003998 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003999 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004000 part = PART_SOCK;
4001 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004002 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004003
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004004 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004005 {
4006 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004007 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01004008 }
4009
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004010 --safe_to_invoke_callback;
4011
Bram Moolenaard7ece102016-02-02 23:23:02 +01004012 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004013}
4014
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004015/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004016 * Return TRUE if any channel has readahead. That means we should not block on
4017 * waiting for input.
4018 */
4019 int
4020channel_any_readahead(void)
4021{
4022 channel_T *channel = first_channel;
4023 ch_part_T part = PART_SOCK;
4024
4025 while (channel != NULL)
4026 {
4027 if (channel_has_readahead(channel, part))
4028 return TRUE;
4029 if (part < PART_ERR)
4030 ++part;
4031 else
4032 {
4033 channel = channel->ch_next;
4034 part = PART_SOCK;
4035 }
4036 }
4037 return FALSE;
4038}
4039
4040/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004041 * Mark references to lists used in channels.
4042 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004043 int
4044set_ref_in_channel(int copyID)
4045{
Bram Moolenaar77073442016-02-13 23:23:53 +01004046 int abort = FALSE;
4047 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004048 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004049
Bram Moolenaar77073442016-02-13 23:23:53 +01004050 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004051 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004052 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004053 tv.v_type = VAR_CHANNEL;
4054 tv.vval.v_channel = channel;
4055 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004056 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004057 return abort;
4058}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004059
4060/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004061 * Return the "part" to write to for "channel".
4062 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004063 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004064channel_part_send(channel_T *channel)
4065{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004066 if (channel->CH_SOCK_FD == INVALID_FD)
4067 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004068 return PART_SOCK;
4069}
4070
4071/*
4072 * Return the default "part" to read from for "channel".
4073 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004074 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004075channel_part_read(channel_T *channel)
4076{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004077 if (channel->CH_SOCK_FD == INVALID_FD)
4078 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004079 return PART_SOCK;
4080}
4081
4082/*
4083 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004084 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004085 */
4086 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004087channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004088{
Bram Moolenaar77073442016-02-13 23:23:53 +01004089 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004090 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004091 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004092}
4093
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004094/*
4095 * Return the timeout of "channel"/"part"
4096 */
4097 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004098channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004099{
4100 return channel->ch_part[part].ch_timeout;
4101}
4102
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004103 static int
4104handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4105{
4106 char_u *val = get_tv_string(item);
4107
4108 opt->jo_set |= jo;
4109 if (STRCMP(val, "nl") == 0)
4110 *modep = MODE_NL;
4111 else if (STRCMP(val, "raw") == 0)
4112 *modep = MODE_RAW;
4113 else if (STRCMP(val, "js") == 0)
4114 *modep = MODE_JS;
4115 else if (STRCMP(val, "json") == 0)
4116 *modep = MODE_JSON;
4117 else
4118 {
4119 EMSG2(_(e_invarg2), val);
4120 return FAIL;
4121 }
4122 return OK;
4123}
4124
4125 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004126handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004127{
4128 char_u *val = get_tv_string(item);
4129
4130 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4131 if (STRCMP(val, "null") == 0)
4132 opt->jo_io[part] = JIO_NULL;
4133 else if (STRCMP(val, "pipe") == 0)
4134 opt->jo_io[part] = JIO_PIPE;
4135 else if (STRCMP(val, "file") == 0)
4136 opt->jo_io[part] = JIO_FILE;
4137 else if (STRCMP(val, "buffer") == 0)
4138 opt->jo_io[part] = JIO_BUFFER;
4139 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4140 opt->jo_io[part] = JIO_OUT;
4141 else
4142 {
4143 EMSG2(_(e_invarg2), val);
4144 return FAIL;
4145 }
4146 return OK;
4147}
4148
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004149/*
4150 * Clear a jobopt_T before using it.
4151 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004152 void
4153clear_job_options(jobopt_T *opt)
4154{
4155 vim_memset(opt, 0, sizeof(jobopt_T));
4156}
4157
4158/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004159 * Free any members of a jobopt_T.
4160 */
4161 void
4162free_job_options(jobopt_T *opt)
4163{
4164 if (opt->jo_partial != NULL)
4165 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004166 else if (opt->jo_callback != NULL)
4167 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004168 if (opt->jo_out_partial != NULL)
4169 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004170 else if (opt->jo_out_cb != NULL)
4171 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004172 if (opt->jo_err_partial != NULL)
4173 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004174 else if (opt->jo_err_cb != NULL)
4175 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004176 if (opt->jo_close_partial != NULL)
4177 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004178 else if (opt->jo_close_cb != NULL)
4179 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004180 if (opt->jo_exit_partial != NULL)
4181 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004182 else if (opt->jo_exit_cb != NULL)
4183 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004184}
4185
4186/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004187 * Get the PART_ number from the first character of an option name.
4188 */
4189 static int
4190part_from_char(int c)
4191{
4192 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4193}
4194
4195/*
4196 * Get the option entries from the dict in "tv", parse them and put the result
4197 * in "opt".
4198 * Only accept options in "supported".
4199 * If an option value is invalid return FAIL.
4200 */
4201 int
4202get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4203{
4204 typval_T *item;
4205 char_u *val;
4206 dict_T *dict;
4207 int todo;
4208 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004209 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004210
4211 opt->jo_set = 0;
4212 if (tv->v_type == VAR_UNKNOWN)
4213 return OK;
4214 if (tv->v_type != VAR_DICT)
4215 {
4216 EMSG(_(e_invarg));
4217 return FAIL;
4218 }
4219 dict = tv->vval.v_dict;
4220 if (dict == NULL)
4221 return OK;
4222
4223 todo = (int)dict->dv_hashtab.ht_used;
4224 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4225 if (!HASHITEM_EMPTY(hi))
4226 {
4227 item = &dict_lookup(hi)->di_tv;
4228
4229 if (STRCMP(hi->hi_key, "mode") == 0)
4230 {
4231 if (!(supported & JO_MODE))
4232 break;
4233 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4234 return FAIL;
4235 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004236 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004237 {
4238 if (!(supported & JO_IN_MODE))
4239 break;
4240 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4241 == FAIL)
4242 return FAIL;
4243 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004244 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004245 {
4246 if (!(supported & JO_OUT_MODE))
4247 break;
4248 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4249 == FAIL)
4250 return FAIL;
4251 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004252 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004253 {
4254 if (!(supported & JO_ERR_MODE))
4255 break;
4256 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4257 == FAIL)
4258 return FAIL;
4259 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004260 else if (STRCMP(hi->hi_key, "in_io") == 0
4261 || STRCMP(hi->hi_key, "out_io") == 0
4262 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004263 {
4264 if (!(supported & JO_OUT_IO))
4265 break;
4266 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4267 return FAIL;
4268 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004269 else if (STRCMP(hi->hi_key, "in_name") == 0
4270 || STRCMP(hi->hi_key, "out_name") == 0
4271 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004272 {
4273 part = part_from_char(*hi->hi_key);
4274
4275 if (!(supported & JO_OUT_IO))
4276 break;
4277 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4278 opt->jo_io_name[part] =
4279 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4280 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004281 else if (STRCMP(hi->hi_key, "in_buf") == 0
4282 || STRCMP(hi->hi_key, "out_buf") == 0
4283 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004284 {
4285 part = part_from_char(*hi->hi_key);
4286
4287 if (!(supported & JO_OUT_IO))
4288 break;
4289 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4290 opt->jo_io_buf[part] = get_tv_number(item);
4291 if (opt->jo_io_buf[part] <= 0)
4292 {
4293 EMSG2(_(e_invarg2), get_tv_string(item));
4294 return FAIL;
4295 }
4296 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4297 {
4298 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4299 return FAIL;
4300 }
4301 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004302 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4303 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4304 {
4305 part = part_from_char(*hi->hi_key);
4306
4307 if (!(supported & JO_OUT_IO))
4308 break;
4309 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4310 opt->jo_modifiable[part] = get_tv_number(item);
4311 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004312 else if (STRCMP(hi->hi_key, "out_msg") == 0
4313 || STRCMP(hi->hi_key, "err_msg") == 0)
4314 {
4315 part = part_from_char(*hi->hi_key);
4316
4317 if (!(supported & JO_OUT_IO))
4318 break;
4319 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4320 opt->jo_message[part] = get_tv_number(item);
4321 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004322 else if (STRCMP(hi->hi_key, "in_top") == 0
4323 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004324 {
4325 linenr_T *lp;
4326
4327 if (!(supported & JO_OUT_IO))
4328 break;
4329 if (hi->hi_key[3] == 't')
4330 {
4331 lp = &opt->jo_in_top;
4332 opt->jo_set |= JO_IN_TOP;
4333 }
4334 else
4335 {
4336 lp = &opt->jo_in_bot;
4337 opt->jo_set |= JO_IN_BOT;
4338 }
4339 *lp = get_tv_number(item);
4340 if (*lp < 0)
4341 {
4342 EMSG2(_(e_invarg2), get_tv_string(item));
4343 return FAIL;
4344 }
4345 }
4346 else if (STRCMP(hi->hi_key, "channel") == 0)
4347 {
4348 if (!(supported & JO_OUT_IO))
4349 break;
4350 opt->jo_set |= JO_CHANNEL;
4351 if (item->v_type != VAR_CHANNEL)
4352 {
4353 EMSG2(_(e_invarg2), "channel");
4354 return FAIL;
4355 }
4356 opt->jo_channel = item->vval.v_channel;
4357 }
4358 else if (STRCMP(hi->hi_key, "callback") == 0)
4359 {
4360 if (!(supported & JO_CALLBACK))
4361 break;
4362 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004363 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004364 if (opt->jo_callback == NULL)
4365 {
4366 EMSG2(_(e_invarg2), "callback");
4367 return FAIL;
4368 }
4369 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004370 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004371 {
4372 if (!(supported & JO_OUT_CALLBACK))
4373 break;
4374 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004375 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004376 if (opt->jo_out_cb == NULL)
4377 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004378 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004379 return FAIL;
4380 }
4381 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004382 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004383 {
4384 if (!(supported & JO_ERR_CALLBACK))
4385 break;
4386 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004387 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004388 if (opt->jo_err_cb == NULL)
4389 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004390 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004391 return FAIL;
4392 }
4393 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004394 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004395 {
4396 if (!(supported & JO_CLOSE_CALLBACK))
4397 break;
4398 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004399 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004400 if (opt->jo_close_cb == NULL)
4401 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004402 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004403 return FAIL;
4404 }
4405 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004406 else if (STRCMP(hi->hi_key, "drop") == 0)
4407 {
4408 int never = FALSE;
4409 val = get_tv_string(item);
4410
4411 if (STRCMP(val, "never") == 0)
4412 never = TRUE;
4413 else if (STRCMP(val, "auto") != 0)
4414 {
4415 EMSG2(_(e_invarg2), "drop");
4416 return FAIL;
4417 }
4418 opt->jo_drop_never = never;
4419 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004420 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4421 {
4422 if (!(supported & JO_EXIT_CB))
4423 break;
4424 opt->jo_set |= JO_EXIT_CB;
4425 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4426 if (opt->jo_exit_cb == NULL)
4427 {
4428 EMSG2(_(e_invarg2), "exit_cb");
4429 return FAIL;
4430 }
4431 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004432 else if (STRCMP(hi->hi_key, "waittime") == 0)
4433 {
4434 if (!(supported & JO_WAITTIME))
4435 break;
4436 opt->jo_set |= JO_WAITTIME;
4437 opt->jo_waittime = get_tv_number(item);
4438 }
4439 else if (STRCMP(hi->hi_key, "timeout") == 0)
4440 {
4441 if (!(supported & JO_TIMEOUT))
4442 break;
4443 opt->jo_set |= JO_TIMEOUT;
4444 opt->jo_timeout = get_tv_number(item);
4445 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004446 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004447 {
4448 if (!(supported & JO_OUT_TIMEOUT))
4449 break;
4450 opt->jo_set |= JO_OUT_TIMEOUT;
4451 opt->jo_out_timeout = get_tv_number(item);
4452 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004453 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004454 {
4455 if (!(supported & JO_ERR_TIMEOUT))
4456 break;
4457 opt->jo_set |= JO_ERR_TIMEOUT;
4458 opt->jo_err_timeout = get_tv_number(item);
4459 }
4460 else if (STRCMP(hi->hi_key, "part") == 0)
4461 {
4462 if (!(supported & JO_PART))
4463 break;
4464 opt->jo_set |= JO_PART;
4465 val = get_tv_string(item);
4466 if (STRCMP(val, "err") == 0)
4467 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004468 else if (STRCMP(val, "out") == 0)
4469 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004470 else
4471 {
4472 EMSG2(_(e_invarg2), val);
4473 return FAIL;
4474 }
4475 }
4476 else if (STRCMP(hi->hi_key, "id") == 0)
4477 {
4478 if (!(supported & JO_ID))
4479 break;
4480 opt->jo_set |= JO_ID;
4481 opt->jo_id = get_tv_number(item);
4482 }
4483 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4484 {
4485 if (!(supported & JO_STOPONEXIT))
4486 break;
4487 opt->jo_set |= JO_STOPONEXIT;
4488 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4489 opt->jo_soe_buf);
4490 if (opt->jo_stoponexit == NULL)
4491 {
4492 EMSG2(_(e_invarg2), "stoponexit");
4493 return FAIL;
4494 }
4495 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004496 else if (STRCMP(hi->hi_key, "block_write") == 0)
4497 {
4498 if (!(supported & JO_BLOCK_WRITE))
4499 break;
4500 opt->jo_set |= JO_BLOCK_WRITE;
4501 opt->jo_block_write = get_tv_number(item);
4502 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004503 else
4504 break;
4505 --todo;
4506 }
4507 if (todo > 0)
4508 {
4509 EMSG2(_(e_invarg2), hi->hi_key);
4510 return FAIL;
4511 }
4512
4513 return OK;
4514}
4515
4516/*
4517 * Get the channel from the argument.
4518 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004519 * When "check_open" is TRUE check that the channel can be used.
4520 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004521 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004522 */
4523 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004524get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004525{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004526 channel_T *channel = NULL;
4527 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004528
4529 if (tv->v_type == VAR_JOB)
4530 {
4531 if (tv->vval.v_job != NULL)
4532 channel = tv->vval.v_job->jv_channel;
4533 }
4534 else if (tv->v_type == VAR_CHANNEL)
4535 {
4536 channel = tv->vval.v_channel;
4537 }
4538 else
4539 {
4540 EMSG2(_(e_invarg2), get_tv_string(tv));
4541 return NULL;
4542 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004543 if (channel != NULL && reading)
4544 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004545 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004546
Bram Moolenaar437905c2016-04-26 19:01:05 +02004547 if (check_open && (channel == NULL || (!channel_is_open(channel)
4548 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004549 {
4550 EMSG(_("E906: not an open channel"));
4551 return NULL;
4552 }
4553 return channel;
4554}
4555
4556static job_T *first_job = NULL;
4557
4558 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004559job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004560{
4561 ch_log(job->jv_channel, "Freeing job");
4562 if (job->jv_channel != NULL)
4563 {
4564 /* The link from the channel to the job doesn't count as a reference,
4565 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004566 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004567 * NULL the reference. We don't set ch_job_killed, unreferencing the
4568 * job doesn't mean it stops running. */
4569 job->jv_channel->ch_job = NULL;
4570 channel_unref(job->jv_channel);
4571 }
4572 mch_clear_job(job);
4573
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004574 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004575 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576}
4577
4578 static void
4579job_free_job(job_T *job)
4580{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004581 if (job->jv_next != NULL)
4582 job->jv_next->jv_prev = job->jv_prev;
4583 if (job->jv_prev == NULL)
4584 first_job = job->jv_next;
4585 else
4586 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004587 vim_free(job);
4588}
4589
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004590 static void
4591job_free(job_T *job)
4592{
4593 if (!in_free_unref_items)
4594 {
4595 job_free_contents(job);
4596 job_free_job(job);
4597 }
4598}
4599
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004600#if defined(EXITFREE) || defined(PROTO)
4601 void
4602job_free_all(void)
4603{
4604 while (first_job != NULL)
4605 job_free(first_job);
4606}
4607#endif
4608
4609/*
4610 * Return TRUE if we need to check if the process of "job" has ended.
4611 */
4612 static int
4613job_need_end_check(job_T *job)
4614{
4615 return job->jv_status == JOB_STARTED
4616 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4617}
4618
4619/*
4620 * Return TRUE if the channel of "job" is still useful.
4621 */
4622 static int
4623job_channel_still_useful(job_T *job)
4624{
4625 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4626}
4627
4628/*
4629 * Return TRUE if the job should not be freed yet. Do not free the job when
4630 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4631 * or when the associated channel will do something with the job output.
4632 */
4633 static int
4634job_still_useful(job_T *job)
4635{
4636 return job_need_end_check(job) || job_channel_still_useful(job);
4637}
4638
4639/*
4640 * NOTE: Must call job_cleanup() only once right after the status of "job"
4641 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4642 * mch_detect_ended_job() returned non-NULL).
4643 */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004644 static void
4645job_cleanup(job_T *job)
4646{
4647 if (job->jv_status != JOB_ENDED)
4648 return;
4649
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004650 /* Ready to cleanup the job. */
4651 job->jv_status = JOB_FINISHED;
4652
Bram Moolenaar97792de2016-10-15 18:36:49 +02004653 if (job->jv_exit_cb != NULL)
4654 {
4655 typval_T argv[3];
4656 typval_T rettv;
4657 int dummy;
4658
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004659 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004660 ++job->jv_refcount;
4661 argv[0].v_type = VAR_JOB;
4662 argv[0].vval.v_job = job;
4663 argv[1].v_type = VAR_NUMBER;
4664 argv[1].vval.v_number = job->jv_exitval;
4665 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4666 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4667 job->jv_exit_partial, NULL);
4668 clear_tv(&rettv);
4669 --job->jv_refcount;
4670 channel_need_redraw = TRUE;
4671 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004672
4673 /* Do not free the job in case the close callback of the associated channel
4674 * isn't invoked yet and may get information by job_info(). */
4675 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004676 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004677 /* The job was already unreferenced and the associated channel was
4678 * detached, now that it ended it can be freed. Careful: caller must
4679 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004680 job_free(job);
4681 }
4682}
4683
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004684/*
4685 * Mark references in jobs that are still useful.
4686 */
4687 int
4688set_ref_in_job(int copyID)
4689{
4690 int abort = FALSE;
4691 job_T *job;
4692 typval_T tv;
4693
4694 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004695 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004696 {
4697 tv.v_type = VAR_JOB;
4698 tv.vval.v_job = job;
4699 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4700 }
4701 return abort;
4702}
4703
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004704/*
4705 * Dereference "job". Note that after this "job" may have been freed.
4706 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004707 void
4708job_unref(job_T *job)
4709{
4710 if (job != NULL && --job->jv_refcount <= 0)
4711 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004712 /* Do not free the job if there is a channel where the close callback
4713 * may get the job info. */
4714 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004715 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004716 /* Do not free the job when it has not ended yet and there is a
4717 * "stoponexit" flag or an exit callback. */
4718 if (!job_need_end_check(job))
4719 {
4720 job_free(job);
4721 }
4722 else if (job->jv_channel != NULL)
4723 {
4724 /* Do remove the link to the channel, otherwise it hangs
4725 * around until Vim exits. See job_free() for refcount. */
4726 ch_log(job->jv_channel, "detaching channel from job");
4727 job->jv_channel->ch_job = NULL;
4728 channel_unref(job->jv_channel);
4729 job->jv_channel = NULL;
4730 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004731 }
4732 }
4733}
4734
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004735 int
4736free_unused_jobs_contents(int copyID, int mask)
4737{
4738 int did_free = FALSE;
4739 job_T *job;
4740
4741 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004742 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004743 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004744 {
4745 /* Free the channel and ordinary items it contains, but don't
4746 * recurse into Lists, Dictionaries etc. */
4747 job_free_contents(job);
4748 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004749 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004750 return did_free;
4751}
4752
4753 void
4754free_unused_jobs(int copyID, int mask)
4755{
4756 job_T *job;
4757 job_T *job_next;
4758
4759 for (job = first_job; job != NULL; job = job_next)
4760 {
4761 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004762 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004763 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004765 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004766 job_free_job(job);
4767 }
4768 }
4769}
4770
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004771/*
4772 * Allocate a job. Sets the refcount to one and sets options default.
4773 */
4774 static job_T *
4775job_alloc(void)
4776{
4777 job_T *job;
4778
4779 job = (job_T *)alloc_clear(sizeof(job_T));
4780 if (job != NULL)
4781 {
4782 job->jv_refcount = 1;
4783 job->jv_stoponexit = vim_strsave((char_u *)"term");
4784
4785 if (first_job != NULL)
4786 {
4787 first_job->jv_prev = job;
4788 job->jv_next = first_job;
4789 }
4790 first_job = job;
4791 }
4792 return job;
4793}
4794
4795 void
4796job_set_options(job_T *job, jobopt_T *opt)
4797{
4798 if (opt->jo_set & JO_STOPONEXIT)
4799 {
4800 vim_free(job->jv_stoponexit);
4801 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4802 job->jv_stoponexit = NULL;
4803 else
4804 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4805 }
4806 if (opt->jo_set & JO_EXIT_CB)
4807 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004808 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004809 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004810 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004811 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004812 job->jv_exit_partial = NULL;
4813 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004814 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004815 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004816 job->jv_exit_partial = opt->jo_exit_partial;
4817 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004818 {
4819 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004820 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004821 }
4822 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004823 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004824 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004825 func_ref(job->jv_exit_cb);
4826 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004827 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004828 }
4829}
4830
4831/*
4832 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4833 */
4834 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004835job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004836{
4837 job_T *job;
4838
4839 for (job = first_job; job != NULL; job = job->jv_next)
4840 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4841 mch_stop_job(job, job->jv_stoponexit);
4842}
4843
4844/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004845 * Return TRUE when there is any job that has an exit callback and might exit,
4846 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004847 */
4848 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004849has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004850{
4851 job_T *job;
4852
4853 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004854 /* Only should check if the channel has been closed, if the channel is
4855 * open the job won't exit. */
4856 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004857 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004858 return TRUE;
4859 return FALSE;
4860}
4861
Bram Moolenaar97792de2016-10-15 18:36:49 +02004862#define MAX_CHECK_ENDED 8
4863
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004864/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004865 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004866 */
4867 void
4868job_check_ended(void)
4869{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004870 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004871
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004872 if (first_job == NULL)
4873 return;
4874
Bram Moolenaar97792de2016-10-15 18:36:49 +02004875 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004876 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004877 /* NOTE: mch_detect_ended_job() must only return a job of which the
4878 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004879 job_T *job = mch_detect_ended_job(first_job);
4880
4881 if (job == NULL)
4882 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004883 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004884 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004885
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004886 if (channel_need_redraw)
4887 {
4888 channel_need_redraw = FALSE;
4889 redraw_after_callback();
4890 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004891}
4892
4893/*
4894 * "job_start()" function
4895 */
4896 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004897job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004898{
4899 job_T *job;
4900 char_u *cmd = NULL;
4901#if defined(UNIX)
4902# define USE_ARGV
4903 char **argv = NULL;
4904 int argc = 0;
4905#else
4906 garray_T ga;
4907#endif
4908 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004909 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004910
4911 job = job_alloc();
4912 if (job == NULL)
4913 return NULL;
4914
4915 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004916#ifndef USE_ARGV
4917 ga_init2(&ga, (int)sizeof(char*), 20);
4918#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004919
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004920 if (opt_arg != NULL)
4921 opt = *opt_arg;
4922 else
4923 {
4924 /* Default mode is NL. */
4925 clear_job_options(&opt);
4926 opt.jo_mode = MODE_NL;
4927 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004928 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4929 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004930 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004931 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004932
4933 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004934 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004935 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4936 && opt.jo_io[part] == JIO_FILE
4937 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4938 || *opt.jo_io_name[part] == NUL))
4939 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004940 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004941 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004942 }
4943
4944 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4945 {
4946 buf_T *buf = NULL;
4947
4948 /* check that we can find the buffer before starting the job */
4949 if (opt.jo_set & JO_IN_BUF)
4950 {
4951 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4952 if (buf == NULL)
4953 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4954 }
4955 else if (!(opt.jo_set & JO_IN_NAME))
4956 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004957 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004958 }
4959 else
4960 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4961 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004962 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004963 if (buf->b_ml.ml_mfp == NULL)
4964 {
4965 char_u numbuf[NUMBUFLEN];
4966 char_u *s;
4967
4968 if (opt.jo_set & JO_IN_BUF)
4969 {
4970 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4971 s = numbuf;
4972 }
4973 else
4974 s = opt.jo_io_name[PART_IN];
4975 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004976 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004977 }
4978 job->jv_in_buf = buf;
4979 }
4980
4981 job_set_options(job, &opt);
4982
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004983 if (argvars[0].v_type == VAR_STRING)
4984 {
4985 /* Command is a string. */
4986 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004987 if (cmd == NULL || *cmd == NUL)
4988 {
4989 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004990 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004991 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004992#ifdef USE_ARGV
4993 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004994 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004995 argv[argc] = NULL;
4996#endif
4997 }
4998 else if (argvars[0].v_type != VAR_LIST
4999 || argvars[0].vval.v_list == NULL
5000 || argvars[0].vval.v_list->lv_len < 1)
5001 {
5002 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005003 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005004 }
5005 else
5006 {
5007 list_T *l = argvars[0].vval.v_list;
5008 listitem_T *li;
5009 char_u *s;
5010
5011#ifdef USE_ARGV
5012 /* Pass argv[] to mch_call_shell(). */
5013 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5014 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005015 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005016#endif
5017 for (li = l->lv_first; li != NULL; li = li->li_next)
5018 {
5019 s = get_tv_string_chk(&li->li_tv);
5020 if (s == NULL)
5021 goto theend;
5022#ifdef USE_ARGV
5023 argv[argc++] = (char *)s;
5024#else
5025 /* Only escape when needed, double quotes are not always allowed. */
5026 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
5027 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005028# ifdef WIN32
5029 int old_ssl = p_ssl;
5030
5031 /* This is using CreateProcess, not cmd.exe. Always use
5032 * double quote and backslashes. */
5033 p_ssl = 0;
5034# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005035 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005036# ifdef WIN32
5037 p_ssl = old_ssl;
5038# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005039 if (s == NULL)
5040 goto theend;
5041 ga_concat(&ga, s);
5042 vim_free(s);
5043 }
5044 else
5045 ga_concat(&ga, s);
5046 if (li->li_next != NULL)
5047 ga_append(&ga, ' ');
5048#endif
5049 }
5050#ifdef USE_ARGV
5051 argv[argc] = NULL;
5052#else
5053 cmd = ga.ga_data;
5054#endif
5055 }
5056
5057#ifdef USE_ARGV
5058 if (ch_log_active())
5059 {
5060 garray_T ga;
5061 int i;
5062
5063 ga_init2(&ga, (int)sizeof(char), 200);
5064 for (i = 0; i < argc; ++i)
5065 {
5066 if (i > 0)
5067 ga_concat(&ga, (char_u *)" ");
5068 ga_concat(&ga, (char_u *)argv[i]);
5069 }
5070 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
5071 ga_clear(&ga);
5072 }
5073 mch_start_job(argv, job, &opt);
5074#else
5075 ch_logs(NULL, "Starting job: %s", (char *)cmd);
5076 mch_start_job((char *)cmd, job, &opt);
5077#endif
5078
5079 /* If the channel is reading from a buffer, write lines now. */
5080 if (job->jv_channel != NULL)
5081 channel_write_in(job->jv_channel);
5082
5083theend:
5084#ifdef USE_ARGV
5085 vim_free(argv);
5086#else
5087 vim_free(ga.ga_data);
5088#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005089 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005090 return job;
5091}
5092
5093/*
5094 * Get the status of "job" and invoke the exit callback when needed.
5095 * The returned string is not allocated.
5096 */
5097 char *
5098job_status(job_T *job)
5099{
5100 char *result;
5101
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005102 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005103 /* No need to check, dead is dead. */
5104 result = "dead";
5105 else if (job->jv_status == JOB_FAILED)
5106 result = "fail";
5107 else
5108 {
5109 result = mch_job_status(job);
5110 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005111 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005112 }
5113 return result;
5114}
5115
Bram Moolenaar8950a562016-03-12 15:22:55 +01005116/*
5117 * Implementation of job_info().
5118 */
5119 void
5120job_info(job_T *job, dict_T *dict)
5121{
5122 dictitem_T *item;
5123 varnumber_T nr;
5124
5125 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5126
5127 item = dictitem_alloc((char_u *)"channel");
5128 if (item == NULL)
5129 return;
5130 item->di_tv.v_lock = 0;
5131 item->di_tv.v_type = VAR_CHANNEL;
5132 item->di_tv.vval.v_channel = job->jv_channel;
5133 if (job->jv_channel != NULL)
5134 ++job->jv_channel->ch_refcount;
5135 if (dict_add(dict, item) == FAIL)
5136 dictitem_free(item);
5137
5138#ifdef UNIX
5139 nr = job->jv_pid;
5140#else
5141 nr = job->jv_proc_info.dwProcessId;
5142#endif
5143 dict_add_nr_str(dict, "process", nr, NULL);
5144
5145 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005146 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005147 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5148}
5149
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005150 int
5151job_stop(job_T *job, typval_T *argvars)
5152{
5153 char_u *arg;
5154
5155 if (argvars[1].v_type == VAR_UNKNOWN)
5156 arg = (char_u *)"";
5157 else
5158 {
5159 arg = get_tv_string_chk(&argvars[1]);
5160 if (arg == NULL)
5161 {
5162 EMSG(_(e_invarg));
5163 return 0;
5164 }
5165 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005166 if (job->jv_status == JOB_ENDED)
5167 {
5168 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5169 return 0;
5170 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005171 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
5172 if (mch_stop_job(job, arg) == FAIL)
5173 return 0;
5174
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005175 /* Assume that only "kill" will kill the job. */
5176 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005177 job->jv_channel->ch_job_killed = TRUE;
5178
5179 /* We don't try freeing the job, obviously the caller still has a
5180 * reference to it. */
5181 return 1;
5182}
5183
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005184#endif /* FEAT_JOB_CHANNEL */