blob: fab35899a4a1bba4e0a61ebe0f22da0cb2113479 [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 Moolenaar2f3a90a2017-08-03 14:49:29 +0200162ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200166 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167
Bram Moolenaar77073442016-02-13 23:23:53 +0100168 ch_log_lead("", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200169 va_start(ap, fmt);
170 vfprintf(log_fd, fmt, ap);
171 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100174 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 }
176}
177
178 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200179ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180{
181 if (log_fd != NULL)
182 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200183 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184
Bram Moolenaar77073442016-02-13 23:23:53 +0100185 ch_log_lead("ERR ", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200186 va_start(ap, fmt);
187 vfprintf(log_fd, fmt, ap);
188 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100194
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100195#ifdef _WIN32
196# undef PERROR
197# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
198 (char_u *)msg, (char_u *)strerror_win32(errno))
199
200 static char *
201strerror_win32(int eno)
202{
203 static LPVOID msgbuf = NULL;
204 char_u *ptr;
205
206 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200207 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100208 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200209 msgbuf = NULL;
210 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100211 FormatMessage(
212 FORMAT_MESSAGE_ALLOCATE_BUFFER |
213 FORMAT_MESSAGE_FROM_SYSTEM |
214 FORMAT_MESSAGE_IGNORE_INSERTS,
215 NULL,
216 eno,
217 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
218 (LPTSTR) &msgbuf,
219 0,
220 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200221 if (msgbuf != NULL)
222 /* chomp \r or \n */
223 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
224 switch (*ptr)
225 {
226 case '\r':
227 STRMOVE(ptr, ptr + 1);
228 ptr--;
229 break;
230 case '\n':
231 if (*(ptr + 1) == '\0')
232 *ptr = '\0';
233 else
234 *ptr = ' ';
235 break;
236 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100237 return msgbuf;
238}
239#endif
240
Bram Moolenaar77073442016-02-13 23:23:53 +0100241/*
242 * The list of all allocated channels.
243 */
244static channel_T *first_channel = NULL;
245static int next_ch_id = 0;
246
247/*
248 * Allocate a new channel. The refcount is set to 1.
249 * The channel isn't actually used until it is opened.
250 * Returns NULL if out of memory.
251 */
252 channel_T *
253add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100254{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200255 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100256 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100257
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if (channel == NULL)
259 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100260
Bram Moolenaar77073442016-02-13 23:23:53 +0100261 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100262 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100263
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200264 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100265 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100266 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100267#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100268 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100269#endif
270#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100271 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100272#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100273 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100274 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100275
Bram Moolenaar77073442016-02-13 23:23:53 +0100276 if (first_channel != NULL)
277 {
278 first_channel->ch_prev = channel;
279 channel->ch_next = first_channel;
280 }
281 first_channel = channel;
282
283 channel->ch_refcount = 1;
284 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100285}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100286
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200287 int
288has_any_channel(void)
289{
290 return first_channel != NULL;
291}
292
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100293/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100294 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100295 * Return TRUE if "channel" has a callback and the associated job wasn't
296 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100297 */
298 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100299channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100300{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100301 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100302 int has_out_msg;
303 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100304
305 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100306 if (channel->ch_job_killed && channel->ch_job == NULL)
307 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100308
309 /* If there is a close callback it may still need to be invoked. */
310 if (channel->ch_close_cb != NULL)
311 return TRUE;
312
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200313 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200314 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200315 return TRUE;
316
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100317 /* If there is no callback then nobody can get readahead. If the fd is
318 * closed and there is no readahead then the callback won't be called. */
319 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
320 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
321 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100322 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
323 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
324 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
325 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
326 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
327 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100328 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100329 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200330 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
332 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200333 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200334 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
335 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100336}
337
338/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200339 * Close a channel and free all its resources.
340 */
341 static void
342channel_free_contents(channel_T *channel)
343{
344 channel_close(channel, TRUE);
345 channel_clear(channel);
346 ch_log(channel, "Freeing channel");
347}
348
349 static void
350channel_free_channel(channel_T *channel)
351{
352 if (channel->ch_next != NULL)
353 channel->ch_next->ch_prev = channel->ch_prev;
354 if (channel->ch_prev == NULL)
355 first_channel = channel->ch_next;
356 else
357 channel->ch_prev->ch_next = channel->ch_next;
358 vim_free(channel);
359}
360
361 static void
362channel_free(channel_T *channel)
363{
364 if (!in_free_unref_items)
365 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200366 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200367 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200368 else
369 {
370 channel_free_contents(channel);
371 channel_free_channel(channel);
372 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 }
374}
375
376/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100378 * possible, there is no callback to be invoked or the associated job was
379 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100380 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100382 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100383channel_may_free(channel_T *channel)
384{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100385 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100386 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100387 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100388 return TRUE;
389 }
390 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100391}
392
393/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100394 * Decrement the reference count on "channel" and maybe free it when it goes
395 * down to zero. Don't free it if there is a pending action.
396 * Returns TRUE when the channel is no longer referenced.
397 */
398 int
399channel_unref(channel_T *channel)
400{
401 if (channel != NULL && --channel->ch_refcount <= 0)
402 return channel_may_free(channel);
403 return FALSE;
404}
405
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200406 int
407free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100408{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200409 int did_free = FALSE;
410 channel_T *ch;
411
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200412 /* This is invoked from the garbage collector, which only runs at a safe
413 * point. */
414 ++safe_to_invoke_callback;
415
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200416 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200417 if (!channel_still_useful(ch)
418 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200419 {
420 /* Free the channel and ordinary items it contains, but don't
421 * recurse into Lists, Dictionaries etc. */
422 channel_free_contents(ch);
423 did_free = TRUE;
424 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425
426 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200427 return did_free;
428}
429
430 void
431free_unused_channels(int copyID, int mask)
432{
433 channel_T *ch;
434 channel_T *ch_next;
435
436 for (ch = first_channel; ch != NULL; ch = ch_next)
437 {
438 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200439 if (!channel_still_useful(ch)
440 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200441 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200442 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200443 channel_free_channel(ch);
444 }
445 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100446}
447
Bram Moolenaard04a0202016-01-26 23:30:18 +0100448#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100449
450#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
451 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100452channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100453{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100454 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200455 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100456
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100457 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100458 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200459 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100460 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200461 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100462}
463#endif
464
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100466 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100468#ifdef FEAT_GUI_X11
469 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200470messageFromServer(XtPointer clientData,
471 int *unused1 UNUSED,
472 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100473{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100474 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100475}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100476#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100477
Bram Moolenaard04a0202016-01-26 23:30:18 +0100478#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100479# if GTK_CHECK_VERSION(3,0,0)
480 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200481messageFromServer(GIOChannel *unused1 UNUSED,
482 GIOCondition unused2 UNUSED,
483 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100484{
485 channel_read_fd(GPOINTER_TO_INT(clientData));
486 return TRUE; /* Return FALSE instead in case the event source is to
487 * be removed after this function returns. */
488}
489# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100490 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200491messageFromServer(gpointer clientData,
492 gint unused1 UNUSED,
493 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100495 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
Bram Moolenaar98921892016-02-23 17:14:37 +0100497# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498#endif
499
500 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200501channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100502{
Bram Moolenaarde279892016-03-11 22:19:44 +0100503 if (!CH_HAS_GUI)
504 return;
505
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100506# ifdef FEAT_GUI_X11
507 /* Tell notifier we are interested in being called
508 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100509 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
510 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100511 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100512 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100513 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200514 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100515 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100516# else
517# ifdef FEAT_GUI_GTK
518 /* Tell gdk we are interested in being called when there
519 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100520 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100521# if GTK_CHECK_VERSION(3,0,0)
522 {
523 GIOChannel *chnnl = g_io_channel_unix_new(
524 (gint)channel->ch_part[part].ch_fd);
525
526 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
527 chnnl,
528 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200529 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100530 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
531
532 g_io_channel_unref(chnnl);
533 }
534# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100535 channel->ch_part[part].ch_inputHandler = gdk_input_add(
536 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100537 (GdkInputCondition)
538 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200539 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100540 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100541# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100542# endif
543# endif
544}
545
Bram Moolenaarde279892016-03-11 22:19:44 +0100546 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100547channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100548{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100549 if (channel->CH_SOCK_FD != INVALID_FD)
550 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100551 if (channel->CH_OUT_FD != INVALID_FD)
552 channel_gui_register_one(channel, PART_OUT);
553 if (channel->CH_ERR_FD != INVALID_FD)
554 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100555}
556
557/*
558 * Register any of our file descriptors with the GUI event handling system.
559 * Called when the GUI has started.
560 */
561 void
562channel_gui_register_all(void)
563{
Bram Moolenaar77073442016-02-13 23:23:53 +0100564 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100565
Bram Moolenaar77073442016-02-13 23:23:53 +0100566 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100567 channel_gui_register(channel);
568}
569
570 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200571channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100572{
573# ifdef FEAT_GUI_X11
574 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
575 {
576 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
577 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
578 }
579# else
580# ifdef FEAT_GUI_GTK
581 if (channel->ch_part[part].ch_inputHandler != 0)
582 {
583# if GTK_CHECK_VERSION(3,0,0)
584 g_source_remove(channel->ch_part[part].ch_inputHandler);
585# else
586 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
587# endif
588 channel->ch_part[part].ch_inputHandler = 0;
589 }
590# endif
591# endif
592}
593
594 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100595channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100596{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200597 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100598
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100599 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100600 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100601}
602
603#endif
604
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100605static char *e_cannot_connect = N_("E902: Cannot connect to port");
606
Bram Moolenaard04a0202016-01-26 23:30:18 +0100607/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100608 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100609 * "waittime" is the time in msec to wait for the connection.
610 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100611 * Returns the channel for success.
612 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100613 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100614 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100615channel_open(
616 char *hostname,
617 int port_in,
618 int waittime,
619 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100620{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100621 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100623 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100624#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100625 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100626 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100627#else
628 int port = port_in;
629#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100631 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100632
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100633#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100634 channel_init_winsock();
635#endif
636
Bram Moolenaar77073442016-02-13 23:23:53 +0100637 channel = add_channel();
638 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100639 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100640 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100641 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100642 }
643
644 /* Get the server internet address and put into addr structure */
645 /* fill in the socket address structure and connect to server */
646 vim_memset((char *)&server, 0, sizeof(server));
647 server.sin_family = AF_INET;
648 server.sin_port = htons(port);
649 if ((host = gethostbyname(hostname)) == NULL)
650 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100651 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200652 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100653 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100654 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100656 {
657 char *p;
658
659 /* When using host->h_addr directly ubsan warns for it to not be
660 * aligned. First copy the pointer to aviod that. */
661 memcpy(&p, &host->h_addr, sizeof(p));
662 memcpy((char *)&server.sin_addr, p, host->h_length);
663 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100665 /* On Mac and Solaris a zero timeout almost never works. At least wait
666 * one millisecond. Let's do it for all systems, because we don't know why
667 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100668 if (waittime == 0)
669 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670
671 /*
672 * For Unix we need to call connect() again after connect() failed.
673 * On Win32 one time is sufficient.
674 */
675 while (TRUE)
676 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100677 long elapsed_msec = 0;
678 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100679
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 sd = socket(AF_INET, SOCK_STREAM, 0);
683 if (sd == -1)
684 {
685 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200686 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100687 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100690
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100691 if (waittime >= 0)
692 {
693 /* Make connect() non-blocking. */
694 if (
695#ifdef _WIN32
696 ioctlsocket(sd, FIONBIO, &val) < 0
697#else
698 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
699#endif
700 )
701 {
702 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200703 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100704 "channel_open: Connect failed with errno %d", errno);
705 sock_close(sd);
706 channel_free(channel);
707 return NULL;
708 }
709 }
710
711 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200712 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100713 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
714
Bram Moolenaar045a2842016-03-08 22:33:07 +0100715 if (ret == 0)
716 /* The connection could be established. */
717 break;
718
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100719 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100720 if (waittime < 0 || (errno != EWOULDBLOCK
721 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100722#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100723 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100724#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100725 ))
726 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200727 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100728 "channel_open: Connect failed with errno %d", errno);
729 PERROR(_(e_cannot_connect));
730 sock_close(sd);
731 channel_free(channel);
732 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100733 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100734
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100735 /* Limit the waittime to 50 msec. If it doesn't work within this
736 * time we close the socket and try creating it again. */
737 waitnow = waittime > 50 ? 50 : waittime;
738
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100740 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100741#ifndef WIN32
742 if (errno != ECONNREFUSED)
743#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100744 {
745 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100746 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100747 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100748#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100749 int so_error = 0;
750 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100751 struct timeval start_tv;
752 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100753#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100754 FD_ZERO(&rfds);
755 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100756 FD_ZERO(&wfds);
757 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100758
Bram Moolenaar562ca712016-03-09 21:50:05 +0100759 tv.tv_sec = waitnow / 1000;
760 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100761#ifndef WIN32
762 gettimeofday(&start_tv, NULL);
763#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200764 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100765 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100766 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100767
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768 if (ret < 0)
769 {
770 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200771 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100772 "channel_open: Connect failed with errno %d", errno);
773 PERROR(_(e_cannot_connect));
774 sock_close(sd);
775 channel_free(channel);
776 return NULL;
777 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100778
Bram Moolenaare081e212016-02-28 22:33:46 +0100779#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100780 /* On Win32: select() is expected to work and wait for up to
781 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100782 if (FD_ISSET(sd, &wfds))
783 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100784 elapsed_msec = waitnow;
785 if (waittime > 1 && elapsed_msec < waittime)
786 {
787 waittime -= elapsed_msec;
788 continue;
789 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100790#else
791 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100792 * After putting the socket in non-blocking mode, connect() will
793 * return EINPROGRESS, select() will not wait (as if writing is
794 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100795 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100796 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100797 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100798 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100799 {
800 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100801 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100802 if (ret < 0 || (so_error != 0
803 && so_error != EWOULDBLOCK
804 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100805# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100807# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100808 ))
809 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200810 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100811 "channel_open: Connect failed with errno %d",
812 so_error);
813 PERROR(_(e_cannot_connect));
814 sock_close(sd);
815 channel_free(channel);
816 return NULL;
817 }
818 }
819
Bram Moolenaar045a2842016-03-08 22:33:07 +0100820 if (FD_ISSET(sd, &wfds) && so_error == 0)
821 /* Did not detect an error, connection is established. */
822 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100823
Bram Moolenaar045a2842016-03-08 22:33:07 +0100824 gettimeofday(&end_tv, NULL);
825 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
826 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100827#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100828 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100829
830#ifndef WIN32
831 if (waittime > 1 && elapsed_msec < waittime)
832 {
833 /* The port isn't ready but we also didn't get an error.
834 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100835 * yet. Select() may return early, wait until the remaining
836 * "waitnow" and try again. */
837 waitnow -= elapsed_msec;
838 waittime -= elapsed_msec;
839 if (waitnow > 0)
840 {
841 mch_delay((long)waitnow, TRUE);
842 ui_breakcheck();
843 waittime -= waitnow;
844 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100845 if (!got_int)
846 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100847 if (waittime <= 0)
848 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100849 waittime = 1;
850 continue;
851 }
852 /* we were interrupted, behave as if timed out */
853 }
854#endif
855
856 /* We timed out. */
857 ch_error(channel, "Connection timed out");
858 sock_close(sd);
859 channel_free(channel);
860 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100861 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100862
Bram Moolenaar045a2842016-03-08 22:33:07 +0100863 ch_log(channel, "Connection made");
864
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100865 if (waittime >= 0)
866 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100867#ifdef _WIN32
868 val = 0;
869 ioctlsocket(sd, FIONBIO, &val);
870#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100871 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100872#endif
873 }
874
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100875 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100876 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100877 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
878 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200879 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100880
881#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100882 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100883#endif
884
Bram Moolenaar77073442016-02-13 23:23:53 +0100885 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100886}
887
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100888/*
889 * Implements ch_open().
890 */
891 channel_T *
892channel_open_func(typval_T *argvars)
893{
894 char_u *address;
895 char_u *p;
896 char *rest;
897 int port;
898 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200899 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100900
901 address = get_tv_string(&argvars[0]);
902 if (argvars[1].v_type != VAR_UNKNOWN
903 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
904 {
905 EMSG(_(e_invarg));
906 return NULL;
907 }
908
909 /* parse address */
910 p = vim_strchr(address, ':');
911 if (p == NULL)
912 {
913 EMSG2(_(e_invarg2), address);
914 return NULL;
915 }
916 *p++ = NUL;
917 port = strtol((char *)p, &rest, 10);
918 if (*address == NUL || port <= 0 || *rest != NUL)
919 {
920 p[-1] = ':';
921 EMSG2(_(e_invarg2), address);
922 return NULL;
923 }
924
925 /* parse options */
926 clear_job_options(&opt);
927 opt.jo_mode = MODE_JSON;
928 opt.jo_timeout = 2000;
929 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200930 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200931 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100932 if (opt.jo_timeout < 0)
933 {
934 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200935 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100936 }
937
938 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
939 if (channel != NULL)
940 {
941 opt.jo_set = JO_ALL;
942 channel_set_options(channel, &opt);
943 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200944theend:
945 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100946 return channel;
947}
948
Bram Moolenaarde279892016-03-11 22:19:44 +0100949 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200950ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100951{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200952 sock_T *fd = &channel->ch_part[part].ch_fd;
953
Bram Moolenaarde279892016-03-11 22:19:44 +0100954 if (*fd != INVALID_FD)
955 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200956 if (part == PART_SOCK)
957 sock_close(*fd);
958 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200959 {
960 /* When using a pty the same FD is set on multiple parts, only
961 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200962 if ((part == PART_IN || channel->CH_IN_FD != *fd)
963 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
964 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200965 fd_close(*fd);
966 }
Bram Moolenaarde279892016-03-11 22:19:44 +0100967 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200968
969 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +0100970 }
971}
972
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100973 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100974channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100975{
Bram Moolenaarde279892016-03-11 22:19:44 +0100976 if (in != INVALID_FD)
977 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200978 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +0100979 channel->CH_IN_FD = in;
980 }
981 if (out != INVALID_FD)
982 {
983# if defined(FEAT_GUI)
984 channel_gui_unregister_one(channel, PART_OUT);
985# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200986 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +0100987 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200988 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +0100989# if defined(FEAT_GUI)
990 channel_gui_register_one(channel, PART_OUT);
991# endif
992 }
993 if (err != INVALID_FD)
994 {
995# if defined(FEAT_GUI)
996 channel_gui_unregister_one(channel, PART_ERR);
997# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200998 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +0100999 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001000 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001001# if defined(FEAT_GUI)
1002 channel_gui_register_one(channel, PART_ERR);
1003# endif
1004 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001005}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001006
Bram Moolenaard6051b52016-02-28 15:49:03 +01001007/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001008 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001009 * This does not keep a refcount, when the job is freed ch_job is cleared.
1010 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001011 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001012channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001013{
Bram Moolenaar77073442016-02-13 23:23:53 +01001014 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001015
1016 channel_set_options(channel, options);
1017
1018 if (job->jv_in_buf != NULL)
1019 {
1020 chanpart_T *in_part = &channel->ch_part[PART_IN];
1021
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001022 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001023 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001024 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001025 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001026 {
1027 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1028 {
1029 /* Special mode: send last-but-one line when appending a line
1030 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001031 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001032 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001033 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001034 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001035 }
1036 else
1037 in_part->ch_buf_top = options->jo_in_top;
1038 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001039 else
1040 in_part->ch_buf_top = 1;
1041 if (options->jo_set & JO_IN_BOT)
1042 in_part->ch_buf_bot = options->jo_in_bot;
1043 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001044 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001045 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001046}
1047
1048/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001049 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001050 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001051 */
1052 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001053find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001054{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001055 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001056 buf_T *save_curbuf = curbuf;
1057
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001058 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001059 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001060 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001061 if (buf == NULL)
1062 buf = buflist_findname_exp(name);
1063 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001064 if (buf == NULL)
1065 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001066 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001067 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001068 if (buf == NULL)
1069 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001070 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001071 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001072#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001073 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1074 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001075#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001076 if (curbuf->b_ml.ml_mfp == NULL)
1077 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001078 if (msg)
1079 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001080 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001081 changed_bytes(1, 0);
1082 curbuf = save_curbuf;
1083 }
1084
1085 return buf;
1086}
1087
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001088 static void
1089set_callback(
1090 char_u **cbp,
1091 partial_T **pp,
1092 char_u *callback,
1093 partial_T *partial)
1094{
1095 free_callback(*cbp, *pp);
1096 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001097 {
1098 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001099 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001100 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001101 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001102 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001103 func_ref(*cbp);
1104 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001105 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001106 else
1107 *cbp = NULL;
1108 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001109 if (partial != NULL)
1110 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001111}
1112
Bram Moolenaar187db502016-02-27 14:44:26 +01001113/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001115 */
1116 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001117channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001118{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001119 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001120
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001121 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001122 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001123 channel->ch_part[part].ch_mode = opt->jo_mode;
1124 if (opt->jo_set & JO_IN_MODE)
1125 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1126 if (opt->jo_set & JO_OUT_MODE)
1127 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1128 if (opt->jo_set & JO_ERR_MODE)
1129 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001130
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001131 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001132 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001133 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1134 if (opt->jo_set & JO_OUT_TIMEOUT)
1135 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1136 if (opt->jo_set & JO_ERR_TIMEOUT)
1137 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001138 if (opt->jo_set & JO_BLOCK_WRITE)
1139 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140
1141 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001142 set_callback(&channel->ch_callback, &channel->ch_partial,
1143 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001144 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001145 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1146 &channel->ch_part[PART_OUT].ch_partial,
1147 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001148 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001149 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1150 &channel->ch_part[PART_ERR].ch_partial,
1151 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001152 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001153 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1154 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001155 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001156
1157 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1158 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001159 buf_T *buf;
1160
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001161 /* writing output to a buffer. Default mode is NL. */
1162 if (!(opt->jo_set & JO_OUT_MODE))
1163 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001164 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001165 {
1166 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1167 if (buf == NULL)
1168 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1169 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001170 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001171 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001172 int msg = TRUE;
1173
1174 if (opt->jo_set2 & JO2_OUT_MSG)
1175 msg = opt->jo_message[PART_OUT];
1176 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001177 }
1178 if (buf != NULL)
1179 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001180 if (opt->jo_set & JO_OUT_MODIFIABLE)
1181 channel->ch_part[PART_OUT].ch_nomodifiable =
1182 !opt->jo_modifiable[PART_OUT];
1183
1184 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1185 {
1186 EMSG(_(e_modifiable));
1187 }
1188 else
1189 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001190 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001191 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001192 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001193 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001194 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001195 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001196
1197 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1198 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1199 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1200 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001201 buf_T *buf;
1202
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001203 /* writing err to a buffer. Default mode is NL. */
1204 if (!(opt->jo_set & JO_ERR_MODE))
1205 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1206 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001207 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001208 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001209 {
1210 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1211 if (buf == NULL)
1212 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1213 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001214 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001215 {
1216 int msg = TRUE;
1217
1218 if (opt->jo_set2 & JO2_ERR_MSG)
1219 msg = opt->jo_message[PART_ERR];
1220 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1221 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 if (buf != NULL)
1223 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001224 if (opt->jo_set & JO_ERR_MODIFIABLE)
1225 channel->ch_part[PART_ERR].ch_nomodifiable =
1226 !opt->jo_modifiable[PART_ERR];
1227 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1228 {
1229 EMSG(_(e_modifiable));
1230 }
1231 else
1232 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001233 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001234 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001235 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001236 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001237 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001238 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001239
1240 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1241 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1242 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001243}
1244
1245/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001246 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001247 */
1248 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001249channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001250 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001251 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001252 char_u *callback,
1253 partial_T *partial,
1254 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001255{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001256 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001257 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1258
1259 if (item != NULL)
1260 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001261 item->cq_partial = partial;
1262 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001263 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001264 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001265 item->cq_callback = callback;
1266 }
1267 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001268 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001269 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001270 func_ref(item->cq_callback);
1271 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001272 item->cq_seq_nr = id;
1273 item->cq_prev = head->cq_prev;
1274 head->cq_prev = item;
1275 item->cq_next = NULL;
1276 if (item->cq_prev == NULL)
1277 head->cq_next = item;
1278 else
1279 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001280 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001281}
1282
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001283 static void
1284write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1285{
1286 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001287 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001288 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001289 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001290
Bram Moolenaar655da312016-05-28 22:22:34 +02001291 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001292 if ((p = alloc(len + 2)) == NULL)
1293 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001294 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001295
1296 for (i = 0; i < len; ++i)
1297 if (p[i] == NL)
1298 p[i] = NUL;
1299
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001300 p[len] = NL;
1301 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001302 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001303 vim_free(p);
1304}
1305
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001306/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001307 * Return TRUE if "channel" can be written to.
1308 * Returns FALSE if the input is closed or the write would block.
1309 */
1310 static int
1311can_write_buf_line(channel_T *channel)
1312{
1313 chanpart_T *in_part = &channel->ch_part[PART_IN];
1314
1315 if (in_part->ch_fd == INVALID_FD)
1316 return FALSE; /* pipe was closed */
1317
1318 /* for testing: block every other attempt to write */
1319 if (in_part->ch_block_write == 1)
1320 in_part->ch_block_write = -1;
1321 else if (in_part->ch_block_write == -1)
1322 in_part->ch_block_write = 1;
1323
1324 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1325#ifndef WIN32
1326 {
1327# if defined(HAVE_SELECT)
1328 struct timeval tval;
1329 fd_set wfds;
1330 int ret;
1331
1332 FD_ZERO(&wfds);
1333 FD_SET((int)in_part->ch_fd, &wfds);
1334 tval.tv_sec = 0;
1335 tval.tv_usec = 0;
1336 for (;;)
1337 {
1338 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1339# ifdef EINTR
1340 SOCK_ERRNO;
1341 if (ret == -1 && errno == EINTR)
1342 continue;
1343# endif
1344 if (ret <= 0 || in_part->ch_block_write == 1)
1345 {
1346 if (ret > 0)
1347 ch_log(channel, "FAKED Input not ready for writing");
1348 else
1349 ch_log(channel, "Input not ready for writing");
1350 return FALSE;
1351 }
1352 break;
1353 }
1354# else
1355 struct pollfd fds;
1356
1357 fds.fd = in_part->ch_fd;
1358 fds.events = POLLOUT;
1359 if (poll(&fds, 1, 0) <= 0)
1360 {
1361 ch_log(channel, "Input not ready for writing");
1362 return FALSE;
1363 }
1364 if (in_part->ch_block_write == 1)
1365 {
1366 ch_log(channel, "FAKED Input not ready for writing");
1367 return FALSE;
1368 }
1369# endif
1370 }
1371#endif
1372 return TRUE;
1373}
1374
1375/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001376 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001377 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001378 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001379channel_write_in(channel_T *channel)
1380{
1381 chanpart_T *in_part = &channel->ch_part[PART_IN];
1382 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001383 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001384 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001385
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001386 if (buf == NULL || in_part->ch_buf_append)
1387 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001388 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001389 {
1390 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001391 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001392 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001393 return;
1394 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001395
1396 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1397 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1398 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001399 if (!can_write_buf_line(channel))
1400 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001401 write_buf_line(buf, lnum, channel);
1402 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001403 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001404
1405 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001406 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001407 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001408 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001409
Bram Moolenaar014069a2016-03-03 22:51:40 +01001410 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001411 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001412 {
1413 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001414 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001416
1417 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001418 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001419 }
1420 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001421 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001422 buf->b_ml.ml_line_count - lnum + 1);
1423}
1424
1425/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001426 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001427 */
1428 void
1429channel_buffer_free(buf_T *buf)
1430{
1431 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001432 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001433
1434 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001435 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001436 {
1437 chanpart_T *ch_part = &channel->ch_part[part];
1438
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001439 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001440 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001441 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001442 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001443 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001444 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001445 }
1446}
1447
1448/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001449 * Write any lines waiting to be written to "channel".
1450 */
1451 static void
1452channel_write_input(channel_T *channel)
1453{
1454 chanpart_T *in_part = &channel->ch_part[PART_IN];
1455
1456 if (in_part->ch_writeque.wq_next != NULL)
1457 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1458 else if (in_part->ch_bufref.br_buf != NULL)
1459 {
1460 if (in_part->ch_buf_append)
1461 channel_write_new_lines(in_part->ch_bufref.br_buf);
1462 else
1463 channel_write_in(channel);
1464 }
1465}
1466
1467/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 * Write any lines waiting to be written to a channel.
1469 */
1470 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001471channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001472{
1473 channel_T *channel;
1474
1475 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001476 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001477}
1478
1479/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001480 * Write appended lines above the last one in "buf" to the channel.
1481 */
1482 void
1483channel_write_new_lines(buf_T *buf)
1484{
1485 channel_T *channel;
1486 int found_one = FALSE;
1487
1488 /* There could be more than one channel for the buffer, loop over all of
1489 * them. */
1490 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1491 {
1492 chanpart_T *in_part = &channel->ch_part[PART_IN];
1493 linenr_T lnum;
1494 int written = 0;
1495
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001496 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001497 {
1498 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001499 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001500 found_one = TRUE;
1501 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1502 ++lnum)
1503 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 if (!can_write_buf_line(channel))
1505 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506 write_buf_line(buf, lnum, channel);
1507 ++written;
1508 }
1509
1510 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001511 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001512 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001513 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001514 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001515 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001516 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001517
1518 in_part->ch_buf_bot = lnum;
1519 }
1520 }
1521 if (!found_one)
1522 buf->b_write_to_channel = FALSE;
1523}
1524
1525/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001527 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001528 */
1529 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001530invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1531 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001532{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001533 typval_T rettv;
1534 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001535
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001536 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001537 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001538
Bram Moolenaar77073442016-02-13 23:23:53 +01001539 argv[0].v_type = VAR_CHANNEL;
1540 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001541
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001542 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1543 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001544 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001545 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001546}
1547
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001548/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001549 * Return the first node from "channel"/"part" without removing it.
1550 * Returns NULL if there is nothing.
1551 */
1552 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001553channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001554{
1555 readq_T *head = &channel->ch_part[part].ch_head;
1556
1557 return head->rq_next;
1558}
1559
1560/*
1561 * Return a pointer to the first NL in "node".
1562 * Skips over NUL characters.
1563 * Returns NULL if there is no NL.
1564 */
1565 char_u *
1566channel_first_nl(readq_T *node)
1567{
1568 char_u *buffer = node->rq_buffer;
1569 long_u i;
1570
1571 for (i = 0; i < node->rq_buflen; ++i)
1572 if (buffer[i] == NL)
1573 return buffer + i;
1574 return NULL;
1575}
1576
1577/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001578 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001579 * The caller must free it.
1580 * Returns NULL if there is nothing.
1581 */
1582 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001583channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001584{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001585 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001586 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001587 char_u *p;
1588
Bram Moolenaar77073442016-02-13 23:23:53 +01001589 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001590 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001591 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001592 p = node->rq_buffer;
1593 head->rq_next = node->rq_next;
1594 if (node->rq_next == NULL)
1595 head->rq_prev = NULL;
1596 else
1597 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001598 vim_free(node);
1599 return p;
1600}
1601
1602/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001603 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001604 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001605 */
1606 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001607channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001608{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001609 readq_T *head = &channel->ch_part[part].ch_head;
1610 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001611 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001612 char_u *res;
1613 char_u *p;
1614
1615 /* If there is only one buffer just get that one. */
1616 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1617 return channel_get(channel, part);
1618
1619 /* Concatenate everything into one buffer. */
1620 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001621 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001622 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001623 if (res == NULL)
1624 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001625 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001626 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001627 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001628 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001629 p += node->rq_buflen;
1630 }
1631 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001632
1633 /* Free all buffers */
1634 do
1635 {
1636 p = channel_get(channel, part);
1637 vim_free(p);
1638 } while (p != NULL);
1639
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001640 /* turn all NUL into NL */
1641 while (len > 0)
1642 {
1643 --len;
1644 if (res[len] == NUL)
1645 res[len] = NL;
1646 }
1647
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001648 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001649}
1650
1651/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001652 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001653 * Caller must check these bytes are available.
1654 */
1655 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001656channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001657{
1658 readq_T *head = &channel->ch_part[part].ch_head;
1659 readq_T *node = head->rq_next;
1660 char_u *buf = node->rq_buffer;
1661
1662 mch_memmove(buf, buf + len, node->rq_buflen - len);
1663 node->rq_buflen -= len;
1664}
1665
1666/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001667 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001668 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001669 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670 */
1671 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001672channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001674 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001675 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001676 readq_T *last_node;
1677 readq_T *n;
1678 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001679 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001680 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681
Bram Moolenaar77073442016-02-13 23:23:53 +01001682 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001683 return FAIL;
1684
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001685 last_node = node->rq_next;
1686 len = node->rq_buflen + last_node->rq_buflen + 1;
1687 if (want_nl)
1688 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001689 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001690 {
1691 last_node = last_node->rq_next;
1692 len += last_node->rq_buflen;
1693 }
1694
1695 p = newbuf = alloc(len);
1696 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001698 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001699 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001700 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001701 node->rq_buffer = newbuf;
1702 for (n = node; n != last_node; )
1703 {
1704 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001705 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001706 p += n->rq_buflen;
1707 vim_free(n->rq_buffer);
1708 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001709 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001710
1711 /* dispose of the collapsed nodes and their buffers */
1712 for (n = node->rq_next; n != last_node; )
1713 {
1714 n = n->rq_next;
1715 vim_free(n->rq_prev);
1716 }
1717 node->rq_next = last_node->rq_next;
1718 if (last_node->rq_next == NULL)
1719 head->rq_prev = node;
1720 else
1721 last_node->rq_next->rq_prev = node;
1722 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001723 return OK;
1724}
1725
1726/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001728 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001729 * Returns OK or FAIL.
1730 */
1731 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001732channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001733 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001734{
1735 readq_T *node;
1736 readq_T *head = &channel->ch_part[part].ch_head;
1737 char_u *p;
1738 int i;
1739
1740 node = (readq_T *)alloc(sizeof(readq_T));
1741 if (node == NULL)
1742 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001743 /* A NUL is added at the end, because netbeans code expects that.
1744 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001745 node->rq_buffer = alloc(len + 1);
1746 if (node->rq_buffer == NULL)
1747 {
1748 vim_free(node);
1749 return FAIL; /* out of memory */
1750 }
1751
1752 if (channel->ch_part[part].ch_mode == MODE_NL)
1753 {
1754 /* Drop any CR before a NL. */
1755 p = node->rq_buffer;
1756 for (i = 0; i < len; ++i)
1757 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1758 *p++ = buf[i];
1759 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001760 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001761 }
1762 else
1763 {
1764 mch_memmove(node->rq_buffer, buf, len);
1765 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001766 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767 }
1768
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001769 if (prepend)
1770 {
1771 /* preend node to the head of the queue */
1772 node->rq_next = head->rq_next;
1773 node->rq_prev = NULL;
1774 if (head->rq_next == NULL)
1775 head->rq_prev = node;
1776 else
1777 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001779 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001780 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001781 {
1782 /* append node to the tail of the queue */
1783 node->rq_next = NULL;
1784 node->rq_prev = head->rq_prev;
1785 if (head->rq_prev == NULL)
1786 head->rq_next = node;
1787 else
1788 head->rq_prev->rq_next = node;
1789 head->rq_prev = node;
1790 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001791
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001792 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001794 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001795 fprintf(log_fd, "'");
1796 if (fwrite(buf, len, 1, log_fd) != 1)
1797 return FAIL;
1798 fprintf(log_fd, "'\n");
1799 }
1800 return OK;
1801}
1802
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001803/*
1804 * Try to fill the buffer of "reader".
1805 * Returns FALSE when nothing was added.
1806 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001807 static int
1808channel_fill(js_read_T *reader)
1809{
1810 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001811 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001812 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001813 int keeplen;
1814 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001815 char_u *p;
1816
1817 if (next == NULL)
1818 return FALSE;
1819
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001820 keeplen = reader->js_end - reader->js_buf;
1821 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001822 {
1823 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001824 addlen = (int)STRLEN(next);
1825 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001826 if (p == NULL)
1827 {
1828 vim_free(next);
1829 return FALSE;
1830 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001831 mch_memmove(p, reader->js_buf, keeplen);
1832 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001833 vim_free(next);
1834 next = p;
1835 }
1836
1837 vim_free(reader->js_buf);
1838 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001839 return TRUE;
1840}
1841
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001842/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001843 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001845 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001846 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001848channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849{
1850 js_read_T reader;
1851 typval_T listtv;
1852 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001853 chanpart_T *chanpart = &channel->ch_part[part];
1854 jsonq_T *head = &chanpart->ch_json_head;
1855 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001856 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001859 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001860
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001861 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001863 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001864 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001865 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001866
1867 /* When a message is incomplete we wait for a short while for more to
1868 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001869 * or list will make us hang.
1870 * Do not generate error messages, they will be written in a channel log. */
1871 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001872 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001873 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001874 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001875 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001877 /* Only accept the response when it is a list with at least two
1878 * items. */
1879 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001880 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001881 if (listtv.v_type != VAR_LIST)
1882 ch_error(channel, "Did not receive a list, discarding");
1883 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001884 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001885 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001886 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001887 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001888 else
1889 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001890 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1891 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001893 else
1894 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001895 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001896 item->jq_value = alloc_tv();
1897 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001898 {
1899 vim_free(item);
1900 clear_tv(&listtv);
1901 }
1902 else
1903 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001904 *item->jq_value = listtv;
1905 item->jq_prev = head->jq_prev;
1906 head->jq_prev = item;
1907 item->jq_next = NULL;
1908 if (item->jq_prev == NULL)
1909 head->jq_next = item;
1910 else
1911 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001912 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001913 }
1914 }
1915 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001916
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001917 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001918 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001919 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001920 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001921 size_t buflen = STRLEN(reader.js_buf);
1922
1923 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001924 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001925 /* First time encountering incomplete message or after receiving
1926 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001927 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001928 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001929 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001930 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001931 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001932#ifdef WIN32
1933 chanpart->ch_deadline = GetTickCount() + 100L;
1934#else
1935 gettimeofday(&chanpart->ch_deadline, NULL);
1936 chanpart->ch_deadline.tv_usec += 100 * 1000;
1937 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1938 {
1939 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1940 ++chanpart->ch_deadline.tv_sec;
1941 }
1942#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001943 }
1944 else
1945 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001946 int timeout;
1947#ifdef WIN32
1948 timeout = GetTickCount() > chanpart->ch_deadline;
1949#else
1950 {
1951 struct timeval now_tv;
1952
1953 gettimeofday(&now_tv, NULL);
1954 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1955 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1956 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1957 }
1958#endif
1959 if (timeout)
1960 {
1961 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001962 chanpart->ch_wait_len = 0;
1963 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001964 }
1965 else
1966 {
1967 reader.js_used = 0;
1968 ch_log(channel, "still waiting on incomplete message");
1969 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001970 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001971 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001972
1973 if (status == FAIL)
1974 {
1975 ch_error(channel, "Decoding failed - discarding input");
1976 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001977 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 }
1979 else if (reader.js_buf[reader.js_used] != NUL)
1980 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001981 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001982 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001983 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1984 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001985 ret = status == MAYBE ? FALSE: TRUE;
1986 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001987 else
1988 ret = FALSE;
1989
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001990 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001991 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001992}
1993
1994/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001995 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001996 */
1997 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001998remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001999{
Bram Moolenaar77073442016-02-13 23:23:53 +01002000 if (node->cq_prev == NULL)
2001 head->cq_next = node->cq_next;
2002 else
2003 node->cq_prev->cq_next = node->cq_next;
2004 if (node->cq_next == NULL)
2005 head->cq_prev = node->cq_prev;
2006 else
2007 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002008}
2009
2010/*
2011 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002012 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002013 */
2014 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002015remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016{
Bram Moolenaar77073442016-02-13 23:23:53 +01002017 if (node->jq_prev == NULL)
2018 head->jq_next = node->jq_next;
2019 else
2020 node->jq_prev->jq_next = node->jq_next;
2021 if (node->jq_next == NULL)
2022 head->jq_prev = node->jq_prev;
2023 else
2024 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002025 vim_free(node);
2026}
2027
2028/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002029 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002030 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002031 * When "id" is zero or negative jut get the first message. But not the one
2032 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002033 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002034 * Return OK when found and return the value in "rettv".
2035 * Return FAIL otherwise.
2036 */
2037 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002038channel_get_json(
2039 channel_T *channel,
2040 ch_part_T part,
2041 int id,
2042 int without_callback,
2043 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002044{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002045 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002046 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047
Bram Moolenaar77073442016-02-13 23:23:53 +01002048 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002049 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002051 typval_T *tv = &l->lv_first->li_tv;
2052
Bram Moolenaar958dc692016-12-01 15:34:12 +01002053 if ((without_callback || !item->jq_no_callback)
2054 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002055 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002056 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002057 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002058 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002059 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002060 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002061 ch_log(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002062 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002063 return OK;
2064 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002065 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066 }
2067 return FAIL;
2068}
2069
Bram Moolenaar958dc692016-12-01 15:34:12 +01002070/*
2071 * Put back "rettv" into the JSON queue, there was no callback for it.
2072 * Takes over the values in "rettv".
2073 */
2074 static void
2075channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2076{
2077 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2078 jsonq_T *item = head->jq_next;
2079 jsonq_T *newitem;
2080
2081 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2082 /* last item was pushed back, append to the end */
2083 item = NULL;
2084 else while (item != NULL && item->jq_no_callback)
2085 /* append after the last item that was pushed back */
2086 item = item->jq_next;
2087
2088 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2089 if (newitem == NULL)
2090 clear_tv(rettv);
2091 else
2092 {
2093 newitem->jq_value = alloc_tv();
2094 if (newitem->jq_value == NULL)
2095 {
2096 vim_free(newitem);
2097 clear_tv(rettv);
2098 }
2099 else
2100 {
2101 newitem->jq_no_callback = FALSE;
2102 *newitem->jq_value = *rettv;
2103 if (item == NULL)
2104 {
2105 /* append to the end */
2106 newitem->jq_prev = head->jq_prev;
2107 head->jq_prev = newitem;
2108 newitem->jq_next = NULL;
2109 if (newitem->jq_prev == NULL)
2110 head->jq_next = newitem;
2111 else
2112 newitem->jq_prev->jq_next = newitem;
2113 }
2114 else
2115 {
2116 /* append after "item" */
2117 newitem->jq_prev = item;
2118 newitem->jq_next = item->jq_next;
2119 item->jq_next = newitem;
2120 if (newitem->jq_next == NULL)
2121 head->jq_prev = newitem;
2122 else
2123 newitem->jq_next->jq_prev = newitem;
2124 }
2125 }
2126 }
2127}
2128
Bram Moolenaarece61b02016-02-20 21:39:05 +01002129#define CH_JSON_MAX_ARGS 4
2130
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002131/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002132 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002133 * "argv[0]" is the command string.
2134 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002135 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002136 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002137channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002138{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002139 char_u *cmd = argv[0].vval.v_string;
2140 char_u *arg;
2141 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002142
Bram Moolenaarece61b02016-02-20 21:39:05 +01002143 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002144 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002145 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002146 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002147 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002148 return;
2149 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002150 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002151 if (arg == NULL)
2152 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002153
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002154 if (STRCMP(cmd, "ex") == 0)
2155 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002156 int save_called_emsg = called_emsg;
2157
2158 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002159 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002160 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002161 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002162 --emsg_silent;
2163 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002164 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002165 (char *)get_vim_var_str(VV_ERRMSG));
2166 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002167 }
2168 else if (STRCMP(cmd, "normal") == 0)
2169 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002170 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002171
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002172 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002173 ea.arg = arg;
2174 ea.addr_count = 0;
2175 ea.forceit = TRUE; /* no mapping */
2176 ex_normal(&ea);
2177 }
2178 else if (STRCMP(cmd, "redraw") == 0)
2179 {
2180 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002181
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002182 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002183 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002184 ex_redraw(&ea);
2185 showruler(FALSE);
2186 setcursor();
2187 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002188#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002189 if (gui.in_use)
2190 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002191 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002193 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002194#endif
2195 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002196 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002197 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 int is_call = cmd[0] == 'c';
2199 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002200
Bram Moolenaarece61b02016-02-20 21:39:05 +01002201 if (argv[id_idx].v_type != VAR_UNKNOWN
2202 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002204 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002205 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002206 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002207 }
2208 else if (is_call && argv[2].v_type != VAR_LIST)
2209 {
2210 ch_error(channel, "third argument for call must be a list");
2211 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002212 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002213 }
2214 else
2215 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002216 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002217 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002218 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002219 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002220
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002221 /* Don't pollute the display with errors. */
2222 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002223 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002224 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002225 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002226 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002227 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002228 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002229 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002230 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002231 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2232 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002233 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002234
2235 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002236 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002237 int id = argv[id_idx].vval.v_number;
2238
Bram Moolenaar55fab432016-02-07 16:53:13 +01002239 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002240 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002241 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002242 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002243 /* If evaluation failed or the result can't be encoded
2244 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002245 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246 err_tv.v_type = VAR_STRING;
2247 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002248 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002249 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002250 if (json != NULL)
2251 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002252 channel_send(channel,
2253 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002254 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002255 vim_free(json);
2256 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002257 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002258 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002259 if (tv == &res_tv)
2260 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002261 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002262 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002263 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002264 }
2265 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002266 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002267 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002268 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002269 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002270}
2271
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002272/*
2273 * Invoke the callback at "cbhead".
2274 * Does not redraw but sets channel_need_redraw.
2275 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002276 static void
2277invoke_one_time_callback(
2278 channel_T *channel,
2279 cbq_T *cbhead,
2280 cbq_T *item,
2281 typval_T *argv)
2282{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002283 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002284 (char *)item->cq_callback);
2285 /* Remove the item from the list first, if the callback
2286 * invokes ch_close() the list will be cleared. */
2287 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002288 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002289 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002290 vim_free(item);
2291}
2292
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002293 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002294append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002295{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002296 bufref_T save_curbuf = {NULL, 0, 0};
2297 win_T *save_curwin = NULL;
2298 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002299 linenr_T lnum = buffer->b_ml.ml_line_count;
2300 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002301 chanpart_T *ch_part = &channel->ch_part[part];
2302 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002303 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002304
2305 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2306 {
2307 if (!ch_part->ch_nomod_error)
2308 {
2309 ch_error(channel, "Buffer is not modifiable, cannot append");
2310 ch_part->ch_nomod_error = TRUE;
2311 }
2312 return;
2313 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002314
2315 /* If the buffer is also used as input insert above the last
2316 * line. Don't write these lines. */
2317 if (save_write_to)
2318 {
2319 --lnum;
2320 buffer->b_write_to_channel = FALSE;
2321 }
2322
2323 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002324 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002325
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002326 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002327
2328 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2329 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2330
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002331 u_sync(TRUE);
2332 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002333 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002334
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002335 if (empty)
2336 {
2337 /* The buffer is empty, replace the first (dummy) line. */
2338 ml_replace(lnum, msg, TRUE);
2339 lnum = 0;
2340 }
2341 else
2342 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002343 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002344
2345 /* Restore curbuf/curwin/curtab */
2346 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2347
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002348 if (ch_part->ch_nomodifiable)
2349 buffer->b_p_ma = FALSE;
2350 else
2351 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002352
2353 if (buffer->b_nwindows > 0)
2354 {
2355 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002356
2357 FOR_ALL_WINDOWS(wp)
2358 {
2359 if (wp->w_buffer == buffer
2360 && (save_write_to
2361 ? wp->w_cursor.lnum == lnum + 1
2362 : (wp->w_cursor.lnum == lnum
2363 && wp->w_cursor.col == 0)))
2364 {
2365 ++wp->w_cursor.lnum;
2366 save_curwin = curwin;
2367 curwin = wp;
2368 curbuf = curwin->w_buffer;
2369 scroll_cursor_bot(0, FALSE);
2370 curwin = save_curwin;
2371 curbuf = curwin->w_buffer;
2372 }
2373 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002374 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002375 channel_need_redraw = TRUE;
2376 }
2377
2378 if (save_write_to)
2379 {
2380 channel_T *ch;
2381
2382 /* Find channels reading from this buffer and adjust their
2383 * next-to-read line number. */
2384 buffer->b_write_to_channel = TRUE;
2385 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2386 {
2387 chanpart_T *in_part = &ch->ch_part[PART_IN];
2388
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002389 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002390 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2391 }
2392 }
2393}
2394
Bram Moolenaar437905c2016-04-26 19:01:05 +02002395 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002396drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002397{
2398 char_u *msg;
2399
2400 while ((msg = channel_get(channel, part)) != NULL)
2401 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002402 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002403 vim_free(msg);
2404 }
2405}
2406
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002407/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002408 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002409 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002410 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002411 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002412 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002413may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002414{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002415 char_u *msg = NULL;
2416 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002417 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002418 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002419 chanpart_T *ch_part = &channel->ch_part[part];
2420 ch_mode_T ch_mode = ch_part->ch_mode;
2421 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002422 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002423 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002424 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002425 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002426 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002427
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002428 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002429 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002430 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002431
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002432 /* Use a message-specific callback, part callback or channel callback */
2433 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2434 if (cbitem->cq_seq_nr == 0)
2435 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002436 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002437 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002438 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002439 partial = cbitem->cq_partial;
2440 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002441 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002442 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002443 callback = ch_part->ch_callback;
2444 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002445 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002446 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002447 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002448 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002449 partial = channel->ch_partial;
2450 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002451
Bram Moolenaarec68a992016-10-03 21:37:41 +02002452 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002453 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2454 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002455 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002456 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002457 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002458 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002459 buffer = NULL;
2460 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002462 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002463 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002464 listitem_T *item;
2465 int argc = 0;
2466
Bram Moolenaard7ece102016-02-02 23:23:02 +01002467 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002468 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002469 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002470 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002471 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002472 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002473 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002474 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002475
Bram Moolenaarece61b02016-02-20 21:39:05 +01002476 for (item = listtv->vval.v_list->lv_first;
2477 item != NULL && argc < CH_JSON_MAX_ARGS;
2478 item = item->li_next)
2479 argv[argc++] = item->li_tv;
2480 while (argc < CH_JSON_MAX_ARGS)
2481 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002482
Bram Moolenaarece61b02016-02-20 21:39:05 +01002483 if (argv[0].v_type == VAR_STRING)
2484 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002485 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002486 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002487 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002488 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002489 }
2490
Bram Moolenaarece61b02016-02-20 21:39:05 +01002491 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002492 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002493 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002494 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002495 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002496 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002497 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002498 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002499 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002500 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002501 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002502 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002503 return FALSE;
2504 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002505 else
2506 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002507 /* If there is no callback or buffer drop the message. */
2508 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002509 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002510 /* If there is a close callback it may use ch_read() to get the
2511 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002512 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002513 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002514 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002515 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002516
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002517 if (ch_mode == MODE_NL)
2518 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002519 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002520 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002521 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002522
2523 /* See if we have a message ending in NL in the first buffer. If
2524 * not try to concatenate the first and the second buffer. */
2525 while (TRUE)
2526 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002527 node = channel_peek(channel, part);
2528 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002529 if (nl != NULL)
2530 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002531 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002532 {
2533 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2534 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002535 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002536 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002537 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002538 buf = node->rq_buffer;
2539
Bram Moolenaarec68a992016-10-03 21:37:41 +02002540 if (nl == NULL)
2541 {
2542 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002543 char_u *new_buf;
2544
2545 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2546 if (new_buf == NULL)
2547 /* This might fail over and over again, should the message
2548 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002549 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002550 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002551 node->rq_buffer = buf;
2552 nl = buf + node->rq_buflen++;
2553 *nl = NUL;
2554 }
2555
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002556 /* Convert NUL to NL, the internal representation. */
2557 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2558 if (*p == NUL)
2559 *p = NL;
2560
2561 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002562 {
2563 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002564 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002565 *nl = NUL;
2566 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002567 else
2568 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002569 /* Copy the message into allocated memory (excluding the NL)
2570 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002571 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002572 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002573 }
2574 }
2575 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002576 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002577 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002578 * get everything we have.
2579 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002580 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002581 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002582
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002583 if (msg == NULL)
2584 return FALSE; /* out of memory (and avoids Coverity warning) */
2585
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002586 argv[1].v_type = VAR_STRING;
2587 argv[1].vval.v_string = msg;
2588 }
2589
Bram Moolenaara07fec92016-02-05 21:04:08 +01002590 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002591 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002592 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002593
Bram Moolenaar958dc692016-12-01 15:34:12 +01002594 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002595 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002596 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002597 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002598 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002599 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002600 break;
2601 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002602 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002603 {
2604 if (channel->ch_drop_never)
2605 {
2606 /* message must be read with ch_read() */
2607 channel_push_json(channel, part, listtv);
2608 listtv = NULL;
2609 }
2610 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002611 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002612 seq_nr);
2613 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002614 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002615 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002616 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002617 if (buffer != NULL)
2618 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002619 if (msg == NULL)
2620 /* JSON or JS mode: re-encode the message. */
2621 msg = json_encode(listtv, ch_mode);
2622 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002623 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002624#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002625 if (buffer->b_term != NULL)
2626 write_to_term(buffer, msg, channel);
2627 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002628#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002629 append_to_buffer(buffer, msg, channel, part);
2630 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002631 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002632
Bram Moolenaar187db502016-02-27 14:44:26 +01002633 if (callback != NULL)
2634 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002635 if (cbitem != NULL)
2636 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2637 else
2638 {
2639 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002640 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002641 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002642 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002643 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002644 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002645 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002646 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002647 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002648
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002649 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002650 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002651 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002652
2653 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002654}
2655
2656/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002657 * Return TRUE when channel "channel" is open for writing to.
2658 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002659 */
2660 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002661channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002662{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002663 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002664 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002665}
2666
2667/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002668 * Return TRUE when channel "channel" is open for reading or writing.
2669 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002670 */
2671 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002672channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002673{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002675 || channel->CH_IN_FD != INVALID_FD
2676 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002677 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002678}
2679
2680/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002681 * Return TRUE if "channel" has JSON or other typeahead.
2682 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002683 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002684channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002685{
2686 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2687
2688 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2689 {
2690 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2691 jsonq_T *item = head->jq_next;
2692
2693 return item != NULL;
2694 }
2695 return channel_peek(channel, part) != NULL;
2696}
2697
2698/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002699 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002700 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002701 */
2702 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002703channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002704{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002705 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002706 int has_readahead = FALSE;
2707
Bram Moolenaar77073442016-02-13 23:23:53 +01002708 if (channel == NULL)
2709 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002710 if (req_part == PART_OUT)
2711 {
2712 if (channel->CH_OUT_FD != INVALID_FD)
2713 return "open";
2714 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002715 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002716 }
2717 else if (req_part == PART_ERR)
2718 {
2719 if (channel->CH_ERR_FD != INVALID_FD)
2720 return "open";
2721 if (channel_has_readahead(channel, PART_ERR))
2722 has_readahead = TRUE;
2723 }
2724 else
2725 {
2726 if (channel_is_open(channel))
2727 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002728 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002729 if (channel_has_readahead(channel, part))
2730 {
2731 has_readahead = TRUE;
2732 break;
2733 }
2734 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002735
2736 if (has_readahead)
2737 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002738 return "closed";
2739}
2740
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002741 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002742channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002743{
2744 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002745 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002746 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002747 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002748 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002749
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002750 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002751 STRCAT(namebuf, "_");
2752 tail = STRLEN(namebuf);
2753
2754 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002755 if (chanpart->ch_fd != INVALID_FD)
2756 status = "open";
2757 else if (channel_has_readahead(channel, part))
2758 status = "buffered";
2759 else
2760 status = "closed";
2761 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002762
2763 STRCPY(namebuf + tail, "mode");
2764 switch (chanpart->ch_mode)
2765 {
2766 case MODE_NL: s = "NL"; break;
2767 case MODE_RAW: s = "RAW"; break;
2768 case MODE_JSON: s = "JSON"; break;
2769 case MODE_JS: s = "JS"; break;
2770 }
2771 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2772
2773 STRCPY(namebuf + tail, "io");
2774 if (part == PART_SOCK)
2775 s = "socket";
2776 else switch (chanpart->ch_io)
2777 {
2778 case JIO_NULL: s = "null"; break;
2779 case JIO_PIPE: s = "pipe"; break;
2780 case JIO_FILE: s = "file"; break;
2781 case JIO_BUFFER: s = "buffer"; break;
2782 case JIO_OUT: s = "out"; break;
2783 }
2784 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2785
2786 STRCPY(namebuf + tail, "timeout");
2787 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2788}
2789
2790 void
2791channel_info(channel_T *channel, dict_T *dict)
2792{
2793 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002794 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002795
2796 if (channel->ch_hostname != NULL)
2797 {
2798 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2799 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2800 channel_part_info(channel, dict, "sock", PART_SOCK);
2801 }
2802 else
2803 {
2804 channel_part_info(channel, dict, "out", PART_OUT);
2805 channel_part_info(channel, dict, "err", PART_ERR);
2806 channel_part_info(channel, dict, "in", PART_IN);
2807 }
2808}
2809
Bram Moolenaar77073442016-02-13 23:23:53 +01002810/*
2811 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002812 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002813 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002814 */
2815 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002816channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002817{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002818 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002819
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002820#ifdef FEAT_GUI
2821 channel_gui_unregister(channel);
2822#endif
2823
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002824 ch_close_part(channel, PART_SOCK);
2825 ch_close_part(channel, PART_IN);
2826 ch_close_part(channel, PART_OUT);
2827 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002828
Bram Moolenaara9f02812017-08-05 17:40:38 +02002829 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002830 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002831 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002832
Bram Moolenaara9f02812017-08-05 17:40:38 +02002833 /* Invoke callbacks and flush buffers before the close callback. */
2834 if (channel->ch_close_cb != NULL)
2835 ch_log(channel,
2836 "Invoking callbacks and flushing buffers before closing");
2837 for (part = PART_SOCK; part < PART_IN; ++part)
2838 {
2839 if (channel->ch_close_cb != NULL
2840 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2841 {
2842 /* Increment the refcount to avoid the channel being freed
2843 * halfway. */
2844 ++channel->ch_refcount;
2845 if (channel->ch_close_cb == NULL)
2846 ch_log(channel, "flushing %s buffers before closing",
2847 part_names[part]);
2848 while (may_invoke_callback(channel, part))
2849 ;
2850 --channel->ch_refcount;
2851 }
2852 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002853
Bram Moolenaara9f02812017-08-05 17:40:38 +02002854 if (channel->ch_close_cb != NULL)
2855 {
2856 typval_T argv[1];
2857 typval_T rettv;
2858 int dummy;
2859
2860 /* Increment the refcount to avoid the channel being freed
2861 * halfway. */
2862 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002863 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002864 (char *)channel->ch_close_cb);
2865 argv[0].v_type = VAR_CHANNEL;
2866 argv[0].vval.v_channel = channel;
2867 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002868 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002869 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002870 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002871 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002872
Bram Moolenaara9f02812017-08-05 17:40:38 +02002873 /* the callback is only called once */
2874 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2875 channel->ch_close_cb = NULL;
2876 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002877
Bram Moolenaara9f02812017-08-05 17:40:38 +02002878 --channel->ch_refcount;
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002879
Bram Moolenaara9f02812017-08-05 17:40:38 +02002880 if (channel_need_redraw)
2881 {
2882 channel_need_redraw = FALSE;
2883 redraw_after_callback();
2884 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002885
Bram Moolenaara9f02812017-08-05 17:40:38 +02002886 if (!channel->ch_drop_never)
2887 /* any remaining messages are useless now */
2888 for (part = PART_SOCK; part < PART_IN; ++part)
2889 drop_messages(channel, part);
2890 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002891 }
2892
2893 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002894
2895#ifdef FEAT_TERMINAL
2896 term_channel_closed(channel);
2897#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002898}
2899
Bram Moolenaard04a0202016-01-26 23:30:18 +01002900/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002901 * Close the "in" part channel "channel".
2902 */
2903 void
2904channel_close_in(channel_T *channel)
2905{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002906 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002907}
2908
2909/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002910 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002911 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002912 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002913channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002914{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002915 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2916 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002917
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002918 while (channel_peek(channel, part) != NULL)
2919 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002920
2921 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002922 {
2923 cbq_T *node = cb_head->cq_next;
2924
2925 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002926 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002927 vim_free(node);
2928 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002929
2930 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002931 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002932 free_tv(json_head->jq_next->jq_value);
2933 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002934 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002935
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002936 free_callback(channel->ch_part[part].ch_callback,
2937 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002938 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002939 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002940}
2941
2942/*
2943 * Clear all the read buffers on "channel".
2944 */
2945 void
2946channel_clear(channel_T *channel)
2947{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002948 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002949 vim_free(channel->ch_hostname);
2950 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002951 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002952 channel_clear_one(channel, PART_OUT);
2953 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002954 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002955 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002956 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002957 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002958 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002959 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002960 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002961}
2962
Bram Moolenaar77073442016-02-13 23:23:53 +01002963#if defined(EXITFREE) || defined(PROTO)
2964 void
2965channel_free_all(void)
2966{
2967 channel_T *channel;
2968
Bram Moolenaard6051b52016-02-28 15:49:03 +01002969 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002970 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2971 channel_clear(channel);
2972}
2973#endif
2974
2975
Bram Moolenaar715d2852016-04-30 17:06:31 +02002976/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002977#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002978
2979/* Buffer size for reading incoming messages. */
2980#define MAXMSGSIZE 4096
2981
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002982#if defined(HAVE_SELECT)
2983/*
2984 * Add write fds where we are waiting for writing to be possible.
2985 */
2986 static int
2987channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2988{
2989 int maxfd = maxfd_arg;
2990 channel_T *ch;
2991
2992 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2993 {
2994 chanpart_T *in_part = &ch->ch_part[PART_IN];
2995
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02002996 if (in_part->ch_fd != INVALID_FD
2997 && (in_part->ch_bufref.br_buf != NULL
2998 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002999 {
3000 FD_SET((int)in_part->ch_fd, wfds);
3001 if ((int)in_part->ch_fd >= maxfd)
3002 maxfd = (int)in_part->ch_fd + 1;
3003 }
3004 }
3005 return maxfd;
3006}
3007#else
3008/*
3009 * Add write fds where we are waiting for writing to be possible.
3010 */
3011 static int
3012channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3013{
3014 int nfd = nfd_in;
3015 channel_T *ch;
3016
3017 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3018 {
3019 chanpart_T *in_part = &ch->ch_part[PART_IN];
3020
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003021 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003022 {
3023 in_part->ch_poll_idx = nfd;
3024 fds[nfd].fd = in_part->ch_fd;
3025 fds[nfd].events = POLLOUT;
3026 ++nfd;
3027 }
3028 else
3029 in_part->ch_poll_idx = -1;
3030 }
3031 return nfd;
3032}
3033#endif
3034
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003035typedef enum {
3036 CW_READY,
3037 CW_NOT_READY,
3038 CW_ERROR
3039} channel_wait_result;
3040
Bram Moolenaard04a0202016-01-26 23:30:18 +01003041/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003042 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003043 * Return CW_READY when there is something to read.
3044 * Return CW_NOT_READY when there is nothing to read.
3045 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003046 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003047 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003048channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003049{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003050 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003051 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003052
Bram Moolenaard8070362016-02-15 21:56:54 +01003053# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003054 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003055 {
3056 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003057 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003058 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003059 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003060
3061 /* reading from a pipe, not a socket */
3062 while (TRUE)
3063 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003064 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3065
3066 if (r && nread > 0)
3067 return CW_READY;
3068 if (r == 0)
3069 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003070
3071 /* perhaps write some buffer lines */
3072 channel_write_any_lines();
3073
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003074 sleep_time = deadline - GetTickCount();
3075 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003076 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003077 /* Wait for a little while. Very short at first, up to 10 msec
3078 * after looping a few times. */
3079 if (sleep_time > delay)
3080 sleep_time = delay;
3081 Sleep(sleep_time);
3082 delay = delay * 2;
3083 if (delay > 10)
3084 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003085 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003086 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003087 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003088#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003089 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003090#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003091 struct timeval tval;
3092 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003093 fd_set wfds;
3094 int ret;
3095 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003096
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003097 tval.tv_sec = timeout / 1000;
3098 tval.tv_usec = (timeout % 1000) * 1000;
3099 for (;;)
3100 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003101 FD_ZERO(&rfds);
3102 FD_SET((int)fd, &rfds);
3103
3104 /* Write lines to a pipe when a pipe can be written to. Need to
3105 * set this every time, some buffers may be done. */
3106 maxfd = (int)fd + 1;
3107 FD_ZERO(&wfds);
3108 maxfd = channel_fill_wfds(maxfd, &wfds);
3109
3110 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003111# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003112 SOCK_ERRNO;
3113 if (ret == -1 && errno == EINTR)
3114 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003115# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003116 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003117 {
3118 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003119 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003120 channel_write_any_lines();
3121 continue;
3122 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003123 break;
3124 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003125#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003126 for (;;)
3127 {
3128 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3129 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003130
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003131 fds[0].fd = fd;
3132 fds[0].events = POLLIN;
3133 nfd = channel_fill_poll_write(nfd, fds);
3134 if (poll(fds, nfd, timeout) > 0)
3135 {
3136 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003137 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003138 channel_write_any_lines();
3139 continue;
3140 }
3141 break;
3142 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003143#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003144 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003145 return CW_NOT_READY;
3146}
3147
3148 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003149ch_close_part_on_error(
3150 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003151{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003152 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003153
3154 if (is_err)
3155 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003156 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003157 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003158 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003159
3160 /* Queue a "DETACH" netbeans message in the command queue in order to
3161 * terminate the netbeans session later. Do not end the session here
3162 * directly as we may be running in the context of a call to
3163 * netbeans_parse_messages():
3164 * netbeans_parse_messages
3165 * -> autocmd triggered while processing the netbeans cmd
3166 * -> ui_breakcheck
3167 * -> gui event loop or select loop
3168 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003169 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003170 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003171 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003172 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003173 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3174
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003175 /* When reading is not possible close this part of the channel. Don't
3176 * close the channel yet, there may be something to read on another part. */
3177 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003178
3179#ifdef FEAT_GUI
3180 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003181 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003182#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003183}
3184
3185 static void
3186channel_close_now(channel_T *channel)
3187{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003188 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003189 if (channel->ch_nb_close_cb != NULL)
3190 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003191 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003192}
3193
3194/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003195 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003196 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003197 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003198 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003199 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003200channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003201{
3202 static char_u *buf = NULL;
3203 int len = 0;
3204 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003205 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003206 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003207
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003208 fd = channel->ch_part[part].ch_fd;
3209 if (fd == INVALID_FD)
3210 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003211 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003212 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003213 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003214 }
3215 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003216
3217 /* Allocate a buffer to read into. */
3218 if (buf == NULL)
3219 {
3220 buf = alloc(MAXMSGSIZE);
3221 if (buf == NULL)
3222 return; /* out of memory! */
3223 }
3224
3225 /* Keep on reading for as long as there is something to read.
3226 * Use select() or poll() to avoid blocking on a message that is exactly
3227 * MAXMSGSIZE long. */
3228 for (;;)
3229 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003230 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003231 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003232 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003233 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003234 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003235 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236 if (len <= 0)
3237 break; /* error or nothing more to read */
3238
3239 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003240 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003241 readlen += len;
3242 if (len < MAXMSGSIZE)
3243 break; /* did read everything that's available */
3244 }
3245
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003246 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003247 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003248 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003249
3250#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003251 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003252 if (CH_HAS_GUI && gtk_main_level() > 0)
3253 gtk_main_quit();
3254#endif
3255}
3256
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003257/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003258 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003259 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003260 * Returns what was read in allocated memory.
3261 * Returns NULL in case of error or timeout.
3262 */
3263 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003264channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003265{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003266 char_u *buf;
3267 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003268 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003269 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003270 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003271 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003272
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003273 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003274 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003275
3276 while (TRUE)
3277 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003278 node = channel_peek(channel, part);
3279 if (node != NULL)
3280 {
3281 if (mode == MODE_RAW || (mode == MODE_NL
3282 && channel_first_nl(node) != NULL))
3283 /* got a complete message */
3284 break;
3285 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3286 continue;
3287 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003288
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003289 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003290 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003291 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003292 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003293 {
3294 ch_log(channel, "Timed out");
3295 return NULL;
3296 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003297 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003298 }
3299
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003300 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003301 if (mode == MODE_RAW)
3302 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003303 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003304 }
3305 else
3306 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003307 char_u *p;
3308
3309 buf = node->rq_buffer;
3310 nl = channel_first_nl(node);
3311
3312 /* Convert NUL to NL, the internal representation. */
3313 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3314 if (*p == NUL)
3315 *p = NL;
3316
3317 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003318 {
3319 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003320 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003321 *nl = NUL;
3322 }
3323 else
3324 {
3325 /* Copy the message into allocated memory and remove it from the
3326 * buffer. */
3327 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003328 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003329 }
3330 }
3331 if (log_fd != NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003332 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003333 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003334}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003335
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003336/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003337 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003338 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003339 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003340 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003341 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003342 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003343channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003344 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003345 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003346 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003347 int id,
3348 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003349{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003350 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003351 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003352 int timeout;
3353 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003354
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003355 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003356 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003357 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003358 for (;;)
3359 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003360 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003361
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003362 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003363 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003364 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003365 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003366 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003367 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003368
Bram Moolenaard7ece102016-02-02 23:23:02 +01003369 if (!more)
3370 {
3371 /* Handle any other messages in the queue. If done some more
3372 * messages may have arrived. */
3373 if (channel_parse_messages())
3374 continue;
3375
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003376 /* Wait for up to the timeout. If there was an incomplete message
3377 * use the deadline for that. */
3378 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003379 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003380 {
3381#ifdef WIN32
3382 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3383#else
3384 {
3385 struct timeval now_tv;
3386
3387 gettimeofday(&now_tv, NULL);
3388 timeout = (chanpart->ch_deadline.tv_sec
3389 - now_tv.tv_sec) * 1000
3390 + (chanpart->ch_deadline.tv_usec
3391 - now_tv.tv_usec) / 1000
3392 + 1;
3393 }
3394#endif
3395 if (timeout < 0)
3396 {
3397 /* Something went wrong, channel_parse_json() didn't
3398 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003399 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003400 timeout = timeout_arg;
3401 }
3402 else if (timeout > timeout_arg)
3403 timeout = timeout_arg;
3404 }
3405 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003406 if (fd == INVALID_FD
3407 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003408 {
3409 if (timeout == timeout_arg)
3410 {
3411 if (fd != INVALID_FD)
3412 ch_log(channel, "Timed out");
3413 break;
3414 }
3415 }
3416 else
3417 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003418 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003419 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003420 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003421 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003422}
3423
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003424/*
3425 * Common for ch_read() and ch_readraw().
3426 */
3427 void
3428common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3429{
3430 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003431 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003432 jobopt_T opt;
3433 int mode;
3434 int timeout;
3435 int id = -1;
3436 typval_T *listtv = NULL;
3437
3438 /* return an empty string by default */
3439 rettv->v_type = VAR_STRING;
3440 rettv->vval.v_string = NULL;
3441
3442 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003443 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003444 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003445 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003446
Bram Moolenaar437905c2016-04-26 19:01:05 +02003447 if (opt.jo_set & JO_PART)
3448 part = opt.jo_part;
3449 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003450 if (channel != NULL)
3451 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003452 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003453 part = channel_part_read(channel);
3454 mode = channel_get_mode(channel, part);
3455 timeout = channel_get_timeout(channel, part);
3456 if (opt.jo_set & JO_TIMEOUT)
3457 timeout = opt.jo_timeout;
3458
3459 if (raw || mode == MODE_RAW || mode == MODE_NL)
3460 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3461 else
3462 {
3463 if (opt.jo_set & JO_ID)
3464 id = opt.jo_id;
3465 channel_read_json_block(channel, part, timeout, id, &listtv);
3466 if (listtv != NULL)
3467 {
3468 *rettv = *listtv;
3469 vim_free(listtv);
3470 }
3471 else
3472 {
3473 rettv->v_type = VAR_SPECIAL;
3474 rettv->vval.v_number = VVAL_NONE;
3475 }
3476 }
3477 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003478
3479theend:
3480 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003481}
3482
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003483# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3484 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003485/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003486 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003487 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003488 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003489 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003490channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003491{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003492 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003493 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003494
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003495 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003496 for (channel = first_channel; channel != NULL;
3497 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003498 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003499 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003500 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003501 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003502 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003503 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003504 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003505 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003506 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003507}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003508# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003509
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003510# if defined(WIN32) || defined(PROTO)
3511/*
3512 * Check the channels for anything that is ready to be read.
3513 * The data is put in the read queue.
3514 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003515 void
3516channel_handle_events(void)
3517{
3518 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003519 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003520 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003521
3522 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3523 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003524 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003525 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003526 {
3527 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003528 if (fd != INVALID_FD)
3529 {
3530 int r = channel_wait(channel, fd, 0);
3531
3532 if (r == CW_READY)
3533 channel_read(channel, part, "channel_handle_events");
3534 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003535 ch_close_part_on_error(channel, part, TRUE,
3536 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003537 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003538 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003539 }
3540}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003541# endif
3542
Bram Moolenaard04a0202016-01-26 23:30:18 +01003543/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003544 * Set "channel"/"part" to non-blocking.
3545 */
3546 void
3547channel_set_nonblock(channel_T *channel, ch_part_T part)
3548{
3549 chanpart_T *ch_part = &channel->ch_part[part];
3550 int fd = ch_part->ch_fd;
3551
3552 if (fd != INVALID_FD)
3553 {
3554#ifdef _WIN32
3555 if (part == PART_SOCK)
3556 {
3557 u_long val = 1;
3558
3559 ioctlsocket(fd, FIONBIO, &val);
3560 }
3561 else
3562#endif
3563 fcntl(fd, F_SETFL, O_NONBLOCK);
3564 ch_part->ch_nonblocking = TRUE;
3565 }
3566}
3567
3568/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003569 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003570 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003571 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003572 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003573 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003574channel_send(
3575 channel_T *channel,
3576 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003577 char_u *buf_arg,
3578 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003579 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003580{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003581 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003582 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003583 chanpart_T *ch_part = &channel->ch_part[part];
3584 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003585
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003586 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003587 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003588 {
3589 if (!channel->ch_error && fun != NULL)
3590 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003591 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003592 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003593 }
3594 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003595 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003596 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003597
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003598 if (log_fd != NULL)
3599 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003600 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003601 fprintf(log_fd, "'");
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003602 ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003603 fprintf(log_fd, "'\n");
3604 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003605 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003606 }
3607
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003608 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003609 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003610 writeq_T *wq = &ch_part->ch_writeque;
3611 char_u *buf;
3612 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003613
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003614 if (wq->wq_next != NULL)
3615 {
3616 /* first write what was queued */
3617 buf = wq->wq_next->wq_ga.ga_data;
3618 len = wq->wq_next->wq_ga.ga_len;
3619 did_use_queue = TRUE;
3620 }
3621 else
3622 {
3623 if (len_arg == 0)
3624 /* nothing to write, called from channel_select_check() */
3625 return OK;
3626 buf = buf_arg;
3627 len = len_arg;
3628 }
3629
3630 if (part == PART_SOCK)
3631 res = sock_write(fd, (char *)buf, len);
3632 else
3633 res = fd_write(fd, (char *)buf, len);
3634 if (res < 0 && (errno == EWOULDBLOCK
3635#ifdef EAGAIN
3636 || errno == EAGAIN
3637#endif
3638 ))
3639 res = 0; /* nothing got written */
3640
3641 if (res >= 0 && ch_part->ch_nonblocking)
3642 {
3643 writeq_T *entry = wq->wq_next;
3644
3645 if (did_use_queue)
3646 ch_log(channel, "Sent %d bytes now", res);
3647 if (res == len)
3648 {
3649 /* Wrote all the buf[len] bytes. */
3650 if (entry != NULL)
3651 {
3652 /* Remove the entry from the write queue. */
3653 ga_clear(&entry->wq_ga);
3654 wq->wq_next = entry->wq_next;
3655 if (wq->wq_next == NULL)
3656 wq->wq_prev = NULL;
3657 else
3658 wq->wq_next->wq_prev = NULL;
3659 continue;
3660 }
3661 if (did_use_queue)
3662 ch_log(channel, "Write queue empty");
3663 }
3664 else
3665 {
3666 /* Wrote only buf[res] bytes, can't write more now. */
3667 if (entry != NULL)
3668 {
3669 if (res > 0)
3670 {
3671 /* Remove the bytes that were written. */
3672 mch_memmove(entry->wq_ga.ga_data,
3673 (char *)entry->wq_ga.ga_data + res,
3674 len - res);
3675 entry->wq_ga.ga_len -= res;
3676 }
3677 buf = buf_arg;
3678 len = len_arg;
3679 }
3680 else
3681 {
3682 buf += res;
3683 len -= res;
3684 }
3685 ch_log(channel, "Adding %d bytes to the write queue", len);
3686
3687 /* Append the not written bytes of the argument to the write
3688 * buffer. Limit entries to 4000 bytes. */
3689 if (wq->wq_prev != NULL
3690 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3691 {
3692 writeq_T *last = wq->wq_prev;
3693
3694 /* append to the last entry */
3695 if (ga_grow(&last->wq_ga, len) == OK)
3696 {
3697 mch_memmove((char *)last->wq_ga.ga_data
3698 + last->wq_ga.ga_len,
3699 buf, len);
3700 last->wq_ga.ga_len += len;
3701 }
3702 }
3703 else
3704 {
3705 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3706
3707 if (last != NULL)
3708 {
3709 ch_log(channel, "Creating new entry");
3710 last->wq_prev = wq->wq_prev;
3711 last->wq_next = NULL;
3712 if (wq->wq_prev == NULL)
3713 wq->wq_next = last;
3714 else
3715 wq->wq_prev->wq_next = last;
3716 wq->wq_prev = last;
3717 ga_init2(&last->wq_ga, 1, 1000);
3718 if (ga_grow(&last->wq_ga, len) == OK)
3719 {
3720 mch_memmove(last->wq_ga.ga_data, buf, len);
3721 last->wq_ga.ga_len = len;
3722 }
3723 }
3724 }
3725 }
3726 }
3727 else if (res != len)
3728 {
3729 if (!channel->ch_error && fun != NULL)
3730 {
3731 ch_error(channel, "%s(): write failed", fun);
3732 EMSG2(_("E631: %s(): write failed"), fun);
3733 }
3734 channel->ch_error = TRUE;
3735 return FAIL;
3736 }
3737
3738 channel->ch_error = FALSE;
3739 return OK;
3740 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003741}
3742
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003743/*
3744 * Common for "ch_sendexpr()" and "ch_sendraw()".
3745 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003746 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003747 * Otherwise returns NULL.
3748 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003749 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003750send_common(
3751 typval_T *argvars,
3752 char_u *text,
3753 int id,
3754 int eval,
3755 jobopt_T *opt,
3756 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003757 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003758{
3759 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003760 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003761
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003762 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003763 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003764 if (channel == NULL)
3765 return NULL;
3766 part_send = channel_part_send(channel);
3767 *part_read = channel_part_read(channel);
3768
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003769 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003770 return NULL;
3771
3772 /* Set the callback. An empty callback means no callback and not reading
3773 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3774 * allowed. */
3775 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3776 {
3777 if (eval)
3778 {
3779 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3780 return NULL;
3781 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003782 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003783 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003784 }
3785
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003786 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003787 && opt->jo_callback == NULL)
3788 return channel;
3789 return NULL;
3790}
3791
3792/*
3793 * common for "ch_evalexpr()" and "ch_sendexpr()"
3794 */
3795 void
3796ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3797{
3798 char_u *text;
3799 typval_T *listtv;
3800 channel_T *channel;
3801 int id;
3802 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003803 ch_part_T part_send;
3804 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003805 jobopt_T opt;
3806 int timeout;
3807
3808 /* return an empty string by default */
3809 rettv->v_type = VAR_STRING;
3810 rettv->vval.v_string = NULL;
3811
Bram Moolenaar437905c2016-04-26 19:01:05 +02003812 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003813 if (channel == NULL)
3814 return;
3815 part_send = channel_part_send(channel);
3816
3817 ch_mode = channel_get_mode(channel, part_send);
3818 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3819 {
3820 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3821 return;
3822 }
3823
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003824 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003825 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003826 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003827 if (text == NULL)
3828 return;
3829
3830 channel = send_common(argvars, text, id, eval, &opt,
3831 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3832 vim_free(text);
3833 if (channel != NULL && eval)
3834 {
3835 if (opt.jo_set & JO_TIMEOUT)
3836 timeout = opt.jo_timeout;
3837 else
3838 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003839 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3840 == OK)
3841 {
3842 list_T *list = listtv->vval.v_list;
3843
3844 /* Move the item from the list and then change the type to
3845 * avoid the value being freed. */
3846 *rettv = list->lv_last->li_tv;
3847 list->lv_last->li_tv.v_type = VAR_NUMBER;
3848 free_tv(listtv);
3849 }
3850 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003851 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852}
3853
3854/*
3855 * common for "ch_evalraw()" and "ch_sendraw()"
3856 */
3857 void
3858ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3859{
3860 char_u buf[NUMBUFLEN];
3861 char_u *text;
3862 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003863 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003864 jobopt_T opt;
3865 int timeout;
3866
3867 /* return an empty string by default */
3868 rettv->v_type = VAR_STRING;
3869 rettv->vval.v_string = NULL;
3870
3871 text = get_tv_string_buf(&argvars[1], buf);
3872 channel = send_common(argvars, text, 0, eval, &opt,
3873 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3874 if (channel != NULL && eval)
3875 {
3876 if (opt.jo_set & JO_TIMEOUT)
3877 timeout = opt.jo_timeout;
3878 else
3879 timeout = channel_get_timeout(channel, part_read);
3880 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3881 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003882 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003883}
3884
Bram Moolenaard04a0202016-01-26 23:30:18 +01003885# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003886/*
3887 * Add open channels to the poll struct.
3888 * Return the adjusted struct index.
3889 * The type of "fds" is hidden to avoid problems with the function proto.
3890 */
3891 int
3892channel_poll_setup(int nfd_in, void *fds_in)
3893{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003894 int nfd = nfd_in;
3895 channel_T *channel;
3896 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003897 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003898
Bram Moolenaar77073442016-02-13 23:23:53 +01003899 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003900 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003901 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003902 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003903 chanpart_T *ch_part = &channel->ch_part[part];
3904
3905 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003906 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003907 ch_part->ch_poll_idx = nfd;
3908 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003909 fds[nfd].events = POLLIN;
3910 nfd++;
3911 }
3912 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003913 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003914 }
3915 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003916
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003917 nfd = channel_fill_poll_write(nfd, fds);
3918
Bram Moolenaare0874f82016-01-24 20:36:41 +01003919 return nfd;
3920}
3921
3922/*
3923 * The type of "fds" is hidden to avoid problems with the function proto.
3924 */
3925 int
3926channel_poll_check(int ret_in, void *fds_in)
3927{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003928 int ret = ret_in;
3929 channel_T *channel;
3930 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003931 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003932 int idx;
3933 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003934
Bram Moolenaar77073442016-02-13 23:23:53 +01003935 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003936 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003937 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003938 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003939 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003940
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003941 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003942 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003943 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003944 --ret;
3945 }
3946 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003947
3948 in_part = &channel->ch_part[PART_IN];
3949 idx = in_part->ch_poll_idx;
3950 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3951 {
3952 if (in_part->ch_buf_append)
3953 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003954 if (in_part->ch_bufref.br_buf != NULL)
3955 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003956 }
3957 else
3958 channel_write_in(channel);
3959 --ret;
3960 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003961 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003962
3963 return ret;
3964}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003965# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003966
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003967# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003968/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003969 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003970 */
3971 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003972channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003973{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003974 int maxfd = maxfd_in;
3975 channel_T *channel;
3976 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003977 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003978 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003979
Bram Moolenaar77073442016-02-13 23:23:53 +01003980 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003981 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003982 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003983 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003984 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003985
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003986 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003987 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003988 FD_SET((int)fd, rfds);
3989 if (maxfd < (int)fd)
3990 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003991 }
3992 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003993 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003994
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003995 maxfd = channel_fill_wfds(maxfd, wfds);
3996
Bram Moolenaare0874f82016-01-24 20:36:41 +01003997 return maxfd;
3998}
3999
4000/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004001 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004002 */
4003 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004004channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004005{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004006 int ret = ret_in;
4007 channel_T *channel;
4008 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004009 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004010 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004011 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004012
Bram Moolenaar77073442016-02-13 23:23:53 +01004013 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004014 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004015 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004016 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004017 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004018
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004019 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004020 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004021 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004022 --ret;
4023 }
4024 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004025
4026 in_part = &channel->ch_part[PART_IN];
4027 if (ret > 0 && in_part->ch_fd != INVALID_FD
4028 && FD_ISSET(in_part->ch_fd, wfds))
4029 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004030 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004031 --ret;
4032 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004033 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004034
4035 return ret;
4036}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004037# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004038
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004039/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004040 * Execute queued up commands.
4041 * Invoked from the main loop when it's safe to execute received commands.
4042 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004043 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004044 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004045channel_parse_messages(void)
4046{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004047 channel_T *channel = first_channel;
4048 int ret = FALSE;
4049 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004050 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004051#ifdef ELAPSED_FUNC
4052 ELAPSED_TYPE start_tv;
4053
4054 ELAPSED_INIT(start_tv);
4055#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004056
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004057 ++safe_to_invoke_callback;
4058
Bram Moolenaard0b65022016-03-06 21:50:33 +01004059 /* Only do this message when another message was given, otherwise we get
4060 * lots of them. */
4061 if (did_log_msg)
4062 {
4063 ch_log(NULL, "looking for messages on channels");
4064 did_log_msg = FALSE;
4065 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004066 while (channel != NULL)
4067 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004068 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004069 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004070 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004071 channel_close_now(channel);
4072 /* channel may have been freed, start over */
4073 channel = first_channel;
4074 continue;
4075 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004076 if (channel->ch_to_be_freed)
4077 {
4078 channel_free(channel);
4079 /* channel has been freed, start over */
4080 channel = first_channel;
4081 continue;
4082 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004083 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004084 {
4085 /* channel is no longer useful, free it */
4086 channel_free(channel);
4087 channel = first_channel;
4088 part = PART_SOCK;
4089 continue;
4090 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004091 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004092 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004093 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004094 /* Increase the refcount, in case the handler causes the channel
4095 * to be unreferenced or closed. */
4096 ++channel->ch_refcount;
4097 r = may_invoke_callback(channel, part);
4098 if (r == OK)
4099 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004100 if (channel_unref(channel) || (r == OK
4101#ifdef ELAPSED_FUNC
4102 /* Limit the time we loop here to 100 msec, otherwise
4103 * Vim becomes unresponsive when the callback takes
4104 * more than a bit of time. */
4105 && ELAPSED_FUNC(start_tv) < 100L
4106#endif
4107 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004108 {
4109 /* channel was freed or something was done, start over */
4110 channel = first_channel;
4111 part = PART_SOCK;
4112 continue;
4113 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004114 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004115 if (part < PART_ERR)
4116 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004117 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004118 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004119 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004120 part = PART_SOCK;
4121 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004122 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004123
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004124 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004125 {
4126 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004127 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01004128 }
4129
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004130 --safe_to_invoke_callback;
4131
Bram Moolenaard7ece102016-02-02 23:23:02 +01004132 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004133}
4134
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004135/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004136 * Return TRUE if any channel has readahead. That means we should not block on
4137 * waiting for input.
4138 */
4139 int
4140channel_any_readahead(void)
4141{
4142 channel_T *channel = first_channel;
4143 ch_part_T part = PART_SOCK;
4144
4145 while (channel != NULL)
4146 {
4147 if (channel_has_readahead(channel, part))
4148 return TRUE;
4149 if (part < PART_ERR)
4150 ++part;
4151 else
4152 {
4153 channel = channel->ch_next;
4154 part = PART_SOCK;
4155 }
4156 }
4157 return FALSE;
4158}
4159
4160/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004161 * Mark references to lists used in channels.
4162 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004163 int
4164set_ref_in_channel(int copyID)
4165{
Bram Moolenaar77073442016-02-13 23:23:53 +01004166 int abort = FALSE;
4167 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004168 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004169
Bram Moolenaar77073442016-02-13 23:23:53 +01004170 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004171 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004172 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004173 tv.v_type = VAR_CHANNEL;
4174 tv.vval.v_channel = channel;
4175 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004176 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004177 return abort;
4178}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004179
4180/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004181 * Return the "part" to write to for "channel".
4182 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004183 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004184channel_part_send(channel_T *channel)
4185{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004186 if (channel->CH_SOCK_FD == INVALID_FD)
4187 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004188 return PART_SOCK;
4189}
4190
4191/*
4192 * Return the default "part" to read from for "channel".
4193 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004194 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004195channel_part_read(channel_T *channel)
4196{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004197 if (channel->CH_SOCK_FD == INVALID_FD)
4198 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004199 return PART_SOCK;
4200}
4201
4202/*
4203 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004204 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004205 */
4206 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004207channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004208{
Bram Moolenaar77073442016-02-13 23:23:53 +01004209 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004210 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004211 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004212}
4213
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004214/*
4215 * Return the timeout of "channel"/"part"
4216 */
4217 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004218channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004219{
4220 return channel->ch_part[part].ch_timeout;
4221}
4222
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004223 static int
4224handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4225{
4226 char_u *val = get_tv_string(item);
4227
4228 opt->jo_set |= jo;
4229 if (STRCMP(val, "nl") == 0)
4230 *modep = MODE_NL;
4231 else if (STRCMP(val, "raw") == 0)
4232 *modep = MODE_RAW;
4233 else if (STRCMP(val, "js") == 0)
4234 *modep = MODE_JS;
4235 else if (STRCMP(val, "json") == 0)
4236 *modep = MODE_JSON;
4237 else
4238 {
4239 EMSG2(_(e_invarg2), val);
4240 return FAIL;
4241 }
4242 return OK;
4243}
4244
4245 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004246handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004247{
4248 char_u *val = get_tv_string(item);
4249
4250 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4251 if (STRCMP(val, "null") == 0)
4252 opt->jo_io[part] = JIO_NULL;
4253 else if (STRCMP(val, "pipe") == 0)
4254 opt->jo_io[part] = JIO_PIPE;
4255 else if (STRCMP(val, "file") == 0)
4256 opt->jo_io[part] = JIO_FILE;
4257 else if (STRCMP(val, "buffer") == 0)
4258 opt->jo_io[part] = JIO_BUFFER;
4259 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4260 opt->jo_io[part] = JIO_OUT;
4261 else
4262 {
4263 EMSG2(_(e_invarg2), val);
4264 return FAIL;
4265 }
4266 return OK;
4267}
4268
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004269/*
4270 * Clear a jobopt_T before using it.
4271 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004272 void
4273clear_job_options(jobopt_T *opt)
4274{
4275 vim_memset(opt, 0, sizeof(jobopt_T));
4276}
4277
4278/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004279 * Free any members of a jobopt_T.
4280 */
4281 void
4282free_job_options(jobopt_T *opt)
4283{
4284 if (opt->jo_partial != NULL)
4285 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004286 else if (opt->jo_callback != NULL)
4287 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004288 if (opt->jo_out_partial != NULL)
4289 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004290 else if (opt->jo_out_cb != NULL)
4291 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004292 if (opt->jo_err_partial != NULL)
4293 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004294 else if (opt->jo_err_cb != NULL)
4295 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004296 if (opt->jo_close_partial != NULL)
4297 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004298 else if (opt->jo_close_cb != NULL)
4299 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004300 if (opt->jo_exit_partial != NULL)
4301 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004302 else if (opt->jo_exit_cb != NULL)
4303 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004304 if (opt->jo_env != NULL)
4305 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004306}
4307
4308/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004309 * Get the PART_ number from the first character of an option name.
4310 */
4311 static int
4312part_from_char(int c)
4313{
4314 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4315}
4316
4317/*
4318 * Get the option entries from the dict in "tv", parse them and put the result
4319 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004320 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004321 * If an option value is invalid return FAIL.
4322 */
4323 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004324get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004325{
4326 typval_T *item;
4327 char_u *val;
4328 dict_T *dict;
4329 int todo;
4330 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004331 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004332
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004333 if (tv->v_type == VAR_UNKNOWN)
4334 return OK;
4335 if (tv->v_type != VAR_DICT)
4336 {
4337 EMSG(_(e_invarg));
4338 return FAIL;
4339 }
4340 dict = tv->vval.v_dict;
4341 if (dict == NULL)
4342 return OK;
4343
4344 todo = (int)dict->dv_hashtab.ht_used;
4345 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4346 if (!HASHITEM_EMPTY(hi))
4347 {
4348 item = &dict_lookup(hi)->di_tv;
4349
4350 if (STRCMP(hi->hi_key, "mode") == 0)
4351 {
4352 if (!(supported & JO_MODE))
4353 break;
4354 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4355 return FAIL;
4356 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004357 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 {
4359 if (!(supported & JO_IN_MODE))
4360 break;
4361 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4362 == FAIL)
4363 return FAIL;
4364 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004365 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004366 {
4367 if (!(supported & JO_OUT_MODE))
4368 break;
4369 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4370 == FAIL)
4371 return FAIL;
4372 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004373 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004374 {
4375 if (!(supported & JO_ERR_MODE))
4376 break;
4377 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4378 == FAIL)
4379 return FAIL;
4380 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004381 else if (STRCMP(hi->hi_key, "in_io") == 0
4382 || STRCMP(hi->hi_key, "out_io") == 0
4383 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004384 {
4385 if (!(supported & JO_OUT_IO))
4386 break;
4387 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4388 return FAIL;
4389 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004390 else if (STRCMP(hi->hi_key, "in_name") == 0
4391 || STRCMP(hi->hi_key, "out_name") == 0
4392 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004393 {
4394 part = part_from_char(*hi->hi_key);
4395
4396 if (!(supported & JO_OUT_IO))
4397 break;
4398 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4399 opt->jo_io_name[part] =
4400 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4401 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004402 else if (STRCMP(hi->hi_key, "pty") == 0)
4403 {
4404 if (!(supported & JO_MODE))
4405 break;
4406 opt->jo_pty = get_tv_number(item);
4407 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004408 else if (STRCMP(hi->hi_key, "in_buf") == 0
4409 || STRCMP(hi->hi_key, "out_buf") == 0
4410 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004411 {
4412 part = part_from_char(*hi->hi_key);
4413
4414 if (!(supported & JO_OUT_IO))
4415 break;
4416 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4417 opt->jo_io_buf[part] = get_tv_number(item);
4418 if (opt->jo_io_buf[part] <= 0)
4419 {
4420 EMSG2(_(e_invarg2), get_tv_string(item));
4421 return FAIL;
4422 }
4423 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4424 {
4425 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4426 return FAIL;
4427 }
4428 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004429 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4430 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4431 {
4432 part = part_from_char(*hi->hi_key);
4433
4434 if (!(supported & JO_OUT_IO))
4435 break;
4436 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4437 opt->jo_modifiable[part] = get_tv_number(item);
4438 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004439 else if (STRCMP(hi->hi_key, "out_msg") == 0
4440 || STRCMP(hi->hi_key, "err_msg") == 0)
4441 {
4442 part = part_from_char(*hi->hi_key);
4443
4444 if (!(supported & JO_OUT_IO))
4445 break;
4446 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4447 opt->jo_message[part] = get_tv_number(item);
4448 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004449 else if (STRCMP(hi->hi_key, "in_top") == 0
4450 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004451 {
4452 linenr_T *lp;
4453
4454 if (!(supported & JO_OUT_IO))
4455 break;
4456 if (hi->hi_key[3] == 't')
4457 {
4458 lp = &opt->jo_in_top;
4459 opt->jo_set |= JO_IN_TOP;
4460 }
4461 else
4462 {
4463 lp = &opt->jo_in_bot;
4464 opt->jo_set |= JO_IN_BOT;
4465 }
4466 *lp = get_tv_number(item);
4467 if (*lp < 0)
4468 {
4469 EMSG2(_(e_invarg2), get_tv_string(item));
4470 return FAIL;
4471 }
4472 }
4473 else if (STRCMP(hi->hi_key, "channel") == 0)
4474 {
4475 if (!(supported & JO_OUT_IO))
4476 break;
4477 opt->jo_set |= JO_CHANNEL;
4478 if (item->v_type != VAR_CHANNEL)
4479 {
4480 EMSG2(_(e_invarg2), "channel");
4481 return FAIL;
4482 }
4483 opt->jo_channel = item->vval.v_channel;
4484 }
4485 else if (STRCMP(hi->hi_key, "callback") == 0)
4486 {
4487 if (!(supported & JO_CALLBACK))
4488 break;
4489 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004490 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491 if (opt->jo_callback == NULL)
4492 {
4493 EMSG2(_(e_invarg2), "callback");
4494 return FAIL;
4495 }
4496 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004497 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004498 {
4499 if (!(supported & JO_OUT_CALLBACK))
4500 break;
4501 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004502 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004503 if (opt->jo_out_cb == NULL)
4504 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004505 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004506 return FAIL;
4507 }
4508 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004509 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004510 {
4511 if (!(supported & JO_ERR_CALLBACK))
4512 break;
4513 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004514 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004515 if (opt->jo_err_cb == NULL)
4516 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004517 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004518 return FAIL;
4519 }
4520 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004521 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004522 {
4523 if (!(supported & JO_CLOSE_CALLBACK))
4524 break;
4525 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004526 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004527 if (opt->jo_close_cb == NULL)
4528 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004529 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004530 return FAIL;
4531 }
4532 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004533 else if (STRCMP(hi->hi_key, "drop") == 0)
4534 {
4535 int never = FALSE;
4536 val = get_tv_string(item);
4537
4538 if (STRCMP(val, "never") == 0)
4539 never = TRUE;
4540 else if (STRCMP(val, "auto") != 0)
4541 {
4542 EMSG2(_(e_invarg2), "drop");
4543 return FAIL;
4544 }
4545 opt->jo_drop_never = never;
4546 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004547 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4548 {
4549 if (!(supported & JO_EXIT_CB))
4550 break;
4551 opt->jo_set |= JO_EXIT_CB;
4552 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4553 if (opt->jo_exit_cb == NULL)
4554 {
4555 EMSG2(_(e_invarg2), "exit_cb");
4556 return FAIL;
4557 }
4558 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004559#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004560 else if (STRCMP(hi->hi_key, "term_name") == 0)
4561 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004562 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004563 break;
4564 opt->jo_set2 |= JO2_TERM_NAME;
4565 opt->jo_term_name = get_tv_string_chk(item);
4566 if (opt->jo_term_name == NULL)
4567 {
4568 EMSG2(_(e_invarg2), "term_name");
4569 return FAIL;
4570 }
4571 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004572 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4573 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004574 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004575 break;
4576 val = get_tv_string(item);
4577 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4578 {
Bram Moolenaarf1237f12017-08-11 15:45:28 +02004579 EMSG2(_(e_invarg2), val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004580 return FAIL;
4581 }
4582 opt->jo_set2 |= JO2_TERM_FINISH;
4583 opt->jo_term_finish = *val;
4584 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004585 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4586 {
4587 char_u *p;
4588
4589 if (!(supported2 & JO2_TERM_OPENCMD))
4590 break;
4591 opt->jo_set2 |= JO2_TERM_OPENCMD;
4592 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4593 if (p != NULL)
4594 {
4595 /* Must have %d and no other %. */
4596 p = vim_strchr(p, '%');
4597 if (p != NULL && (p[1] != 'd'
4598 || vim_strchr(p + 2, '%') != NULL))
4599 p = NULL;
4600 }
4601 if (p == NULL)
4602 {
4603 EMSG2(_(e_invarg2), "term_opencmd");
4604 return FAIL;
4605 }
4606 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004607 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4608 {
4609 if (!(supported2 & JO2_TERM_ROWS))
4610 break;
4611 opt->jo_set |= JO2_TERM_ROWS;
4612 opt->jo_term_rows = get_tv_number(item);
4613 }
4614 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4615 {
4616 if (!(supported2 & JO2_TERM_COLS))
4617 break;
4618 opt->jo_set |= JO2_TERM_COLS;
4619 opt->jo_term_cols = get_tv_number(item);
4620 }
4621 else if (STRCMP(hi->hi_key, "vertical") == 0)
4622 {
4623 if (!(supported2 & JO2_VERTICAL))
4624 break;
4625 opt->jo_set |= JO2_VERTICAL;
4626 opt->jo_vertical = get_tv_number(item);
4627 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004628 else if (STRCMP(hi->hi_key, "curwin") == 0)
4629 {
4630 if (!(supported2 & JO2_CURWIN))
4631 break;
4632 opt->jo_set |= JO2_CURWIN;
4633 opt->jo_curwin = get_tv_number(item);
4634 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004635 else if (STRCMP(hi->hi_key, "hidden") == 0)
4636 {
4637 if (!(supported2 & JO2_HIDDEN))
4638 break;
4639 opt->jo_set |= JO2_HIDDEN;
4640 opt->jo_hidden = get_tv_number(item);
4641 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004642#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004643 else if (STRCMP(hi->hi_key, "env") == 0)
4644 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004645 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004646 break;
4647 opt->jo_set |= JO2_ENV;
4648 opt->jo_env = item->vval.v_dict;
4649 ++item->vval.v_dict->dv_refcount;
4650 }
4651 else if (STRCMP(hi->hi_key, "cwd") == 0)
4652 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004653 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004654 break;
4655 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4656 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4657 {
4658 EMSG2(_(e_invarg2), "cwd");
4659 return FAIL;
4660 }
4661 opt->jo_set |= JO2_CWD;
4662 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004663 else if (STRCMP(hi->hi_key, "waittime") == 0)
4664 {
4665 if (!(supported & JO_WAITTIME))
4666 break;
4667 opt->jo_set |= JO_WAITTIME;
4668 opt->jo_waittime = get_tv_number(item);
4669 }
4670 else if (STRCMP(hi->hi_key, "timeout") == 0)
4671 {
4672 if (!(supported & JO_TIMEOUT))
4673 break;
4674 opt->jo_set |= JO_TIMEOUT;
4675 opt->jo_timeout = get_tv_number(item);
4676 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004677 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004678 {
4679 if (!(supported & JO_OUT_TIMEOUT))
4680 break;
4681 opt->jo_set |= JO_OUT_TIMEOUT;
4682 opt->jo_out_timeout = get_tv_number(item);
4683 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004684 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004685 {
4686 if (!(supported & JO_ERR_TIMEOUT))
4687 break;
4688 opt->jo_set |= JO_ERR_TIMEOUT;
4689 opt->jo_err_timeout = get_tv_number(item);
4690 }
4691 else if (STRCMP(hi->hi_key, "part") == 0)
4692 {
4693 if (!(supported & JO_PART))
4694 break;
4695 opt->jo_set |= JO_PART;
4696 val = get_tv_string(item);
4697 if (STRCMP(val, "err") == 0)
4698 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004699 else if (STRCMP(val, "out") == 0)
4700 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004701 else
4702 {
4703 EMSG2(_(e_invarg2), val);
4704 return FAIL;
4705 }
4706 }
4707 else if (STRCMP(hi->hi_key, "id") == 0)
4708 {
4709 if (!(supported & JO_ID))
4710 break;
4711 opt->jo_set |= JO_ID;
4712 opt->jo_id = get_tv_number(item);
4713 }
4714 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4715 {
4716 if (!(supported & JO_STOPONEXIT))
4717 break;
4718 opt->jo_set |= JO_STOPONEXIT;
4719 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4720 opt->jo_soe_buf);
4721 if (opt->jo_stoponexit == NULL)
4722 {
4723 EMSG2(_(e_invarg2), "stoponexit");
4724 return FAIL;
4725 }
4726 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004727 else if (STRCMP(hi->hi_key, "block_write") == 0)
4728 {
4729 if (!(supported & JO_BLOCK_WRITE))
4730 break;
4731 opt->jo_set |= JO_BLOCK_WRITE;
4732 opt->jo_block_write = get_tv_number(item);
4733 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004734 else
4735 break;
4736 --todo;
4737 }
4738 if (todo > 0)
4739 {
4740 EMSG2(_(e_invarg2), hi->hi_key);
4741 return FAIL;
4742 }
4743
4744 return OK;
4745}
4746
4747/*
4748 * Get the channel from the argument.
4749 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004750 * When "check_open" is TRUE check that the channel can be used.
4751 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004752 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004753 */
4754 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004755get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004756{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004757 channel_T *channel = NULL;
4758 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004759
4760 if (tv->v_type == VAR_JOB)
4761 {
4762 if (tv->vval.v_job != NULL)
4763 channel = tv->vval.v_job->jv_channel;
4764 }
4765 else if (tv->v_type == VAR_CHANNEL)
4766 {
4767 channel = tv->vval.v_channel;
4768 }
4769 else
4770 {
4771 EMSG2(_(e_invarg2), get_tv_string(tv));
4772 return NULL;
4773 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004774 if (channel != NULL && reading)
4775 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004776 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004777
Bram Moolenaar437905c2016-04-26 19:01:05 +02004778 if (check_open && (channel == NULL || (!channel_is_open(channel)
4779 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004780 {
4781 EMSG(_("E906: not an open channel"));
4782 return NULL;
4783 }
4784 return channel;
4785}
4786
4787static job_T *first_job = NULL;
4788
4789 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004790job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004791{
4792 ch_log(job->jv_channel, "Freeing job");
4793 if (job->jv_channel != NULL)
4794 {
4795 /* The link from the channel to the job doesn't count as a reference,
4796 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004797 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004798 * NULL the reference. We don't set ch_job_killed, unreferencing the
4799 * job doesn't mean it stops running. */
4800 job->jv_channel->ch_job = NULL;
4801 channel_unref(job->jv_channel);
4802 }
4803 mch_clear_job(job);
4804
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004805 vim_free(job->jv_tty_name);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004806 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004807 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004808}
4809
4810 static void
4811job_free_job(job_T *job)
4812{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004813 if (job->jv_next != NULL)
4814 job->jv_next->jv_prev = job->jv_prev;
4815 if (job->jv_prev == NULL)
4816 first_job = job->jv_next;
4817 else
4818 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004819 vim_free(job);
4820}
4821
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004822 static void
4823job_free(job_T *job)
4824{
4825 if (!in_free_unref_items)
4826 {
4827 job_free_contents(job);
4828 job_free_job(job);
4829 }
4830}
4831
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004832#if defined(EXITFREE) || defined(PROTO)
4833 void
4834job_free_all(void)
4835{
4836 while (first_job != NULL)
4837 job_free(first_job);
4838}
4839#endif
4840
4841/*
4842 * Return TRUE if we need to check if the process of "job" has ended.
4843 */
4844 static int
4845job_need_end_check(job_T *job)
4846{
4847 return job->jv_status == JOB_STARTED
4848 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4849}
4850
4851/*
4852 * Return TRUE if the channel of "job" is still useful.
4853 */
4854 static int
4855job_channel_still_useful(job_T *job)
4856{
4857 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4858}
4859
4860/*
4861 * Return TRUE if the job should not be freed yet. Do not free the job when
4862 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4863 * or when the associated channel will do something with the job output.
4864 */
4865 static int
4866job_still_useful(job_T *job)
4867{
4868 return job_need_end_check(job) || job_channel_still_useful(job);
4869}
4870
Bram Moolenaardcaa6132017-08-13 17:13:09 +02004871#if !defined(USE_ARGV) || defined(PROTO)
4872/*
4873 * Escape one argument for an external command.
4874 * Returns the escaped string in allocated memory. NULL when out of memory.
4875 */
4876 static char_u *
4877win32_escape_arg(char_u *arg)
4878{
4879 int slen, dlen;
4880 int escaping = 0;
4881 int i;
4882 char_u *s, *d;
4883 char_u *escaped_arg;
4884 int has_spaces = FALSE;
4885
4886 /* First count the number of extra bytes required. */
4887 slen = STRLEN(arg);
4888 dlen = slen;
4889 for (s = arg; *s != NUL; MB_PTR_ADV(s))
4890 {
4891 if (*s == '"' || *s == '\\')
4892 ++dlen;
4893 if (*s == ' ' || *s == '\t')
4894 has_spaces = TRUE;
4895 }
4896
4897 if (has_spaces)
4898 dlen += 2;
4899
4900 if (dlen == slen)
4901 return vim_strsave(arg);
4902
4903 /* Allocate memory for the result and fill it. */
4904 escaped_arg = alloc(dlen + 1);
4905 if (escaped_arg == NULL)
4906 return NULL;
4907 memset(escaped_arg, 0, dlen+1);
4908
4909 d = escaped_arg;
4910
4911 if (has_spaces)
4912 *d++ = '"';
4913
4914 for (s = arg; *s != NUL;)
4915 {
4916 switch (*s)
4917 {
4918 case '"':
4919 for (i = 0; i < escaping; i++)
4920 *d++ = '\\';
4921 escaping = 0;
4922 *d++ = '\\';
4923 *d++ = *s++;
4924 break;
4925 case '\\':
4926 escaping++;
4927 *d++ = *s++;
4928 break;
4929 default:
4930 escaping = 0;
4931 MB_COPY_CHAR(s, d);
4932 break;
4933 }
4934 }
4935
4936 /* add terminating quote and finish with a NUL */
4937 if (has_spaces)
4938 {
4939 for (i = 0; i < escaping; i++)
4940 *d++ = '\\';
4941 *d++ = '"';
4942 }
4943 *d = NUL;
4944
4945 return escaped_arg;
4946}
4947
4948/*
4949 * Build a command line from a list, taking care of escaping.
4950 * The result is put in gap->ga_data.
4951 * Returns FAIL when out of memory.
4952 */
4953 int
4954win32_build_cmd(list_T *l, garray_T *gap)
4955{
4956 listitem_T *li;
4957 char_u *s;
4958
4959 for (li = l->lv_first; li != NULL; li = li->li_next)
4960 {
4961 s = get_tv_string_chk(&li->li_tv);
4962 if (s == NULL)
4963 return FAIL;
4964 s = win32_escape_arg(s);
4965 if (s == NULL)
4966 return FAIL;
4967 ga_concat(gap, s);
4968 vim_free(s);
4969 if (li->li_next != NULL)
4970 ga_append(gap, ' ');
4971 }
4972 return OK;
4973}
4974#endif
4975
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004976/*
4977 * NOTE: Must call job_cleanup() only once right after the status of "job"
4978 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4979 * mch_detect_ended_job() returned non-NULL).
4980 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004981 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02004982job_cleanup(job_T *job)
4983{
4984 if (job->jv_status != JOB_ENDED)
4985 return;
4986
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004987 /* Ready to cleanup the job. */
4988 job->jv_status = JOB_FINISHED;
4989
Bram Moolenaar97792de2016-10-15 18:36:49 +02004990 if (job->jv_exit_cb != NULL)
4991 {
4992 typval_T argv[3];
4993 typval_T rettv;
4994 int dummy;
4995
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004996 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02004997 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02004998 ++job->jv_refcount;
4999 argv[0].v_type = VAR_JOB;
5000 argv[0].vval.v_job = job;
5001 argv[1].v_type = VAR_NUMBER;
5002 argv[1].vval.v_number = job->jv_exitval;
5003 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5004 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5005 job->jv_exit_partial, NULL);
5006 clear_tv(&rettv);
5007 --job->jv_refcount;
5008 channel_need_redraw = TRUE;
5009 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005010
5011 /* Do not free the job in case the close callback of the associated channel
5012 * isn't invoked yet and may get information by job_info(). */
5013 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005014 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005015 /* The job was already unreferenced and the associated channel was
5016 * detached, now that it ended it can be freed. Careful: caller must
5017 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005018 job_free(job);
5019 }
5020}
5021
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005022/*
5023 * Mark references in jobs that are still useful.
5024 */
5025 int
5026set_ref_in_job(int copyID)
5027{
5028 int abort = FALSE;
5029 job_T *job;
5030 typval_T tv;
5031
5032 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005033 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005034 {
5035 tv.v_type = VAR_JOB;
5036 tv.vval.v_job = job;
5037 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5038 }
5039 return abort;
5040}
5041
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005042/*
5043 * Dereference "job". Note that after this "job" may have been freed.
5044 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005045 void
5046job_unref(job_T *job)
5047{
5048 if (job != NULL && --job->jv_refcount <= 0)
5049 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005050 /* Do not free the job if there is a channel where the close callback
5051 * may get the job info. */
5052 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005053 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005054 /* Do not free the job when it has not ended yet and there is a
5055 * "stoponexit" flag or an exit callback. */
5056 if (!job_need_end_check(job))
5057 {
5058 job_free(job);
5059 }
5060 else if (job->jv_channel != NULL)
5061 {
5062 /* Do remove the link to the channel, otherwise it hangs
5063 * around until Vim exits. See job_free() for refcount. */
5064 ch_log(job->jv_channel, "detaching channel from job");
5065 job->jv_channel->ch_job = NULL;
5066 channel_unref(job->jv_channel);
5067 job->jv_channel = NULL;
5068 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005069 }
5070 }
5071}
5072
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005073 int
5074free_unused_jobs_contents(int copyID, int mask)
5075{
5076 int did_free = FALSE;
5077 job_T *job;
5078
5079 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005080 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005081 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005082 {
5083 /* Free the channel and ordinary items it contains, but don't
5084 * recurse into Lists, Dictionaries etc. */
5085 job_free_contents(job);
5086 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005087 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005088 return did_free;
5089}
5090
5091 void
5092free_unused_jobs(int copyID, int mask)
5093{
5094 job_T *job;
5095 job_T *job_next;
5096
5097 for (job = first_job; job != NULL; job = job_next)
5098 {
5099 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005100 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005101 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005102 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005103 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005104 job_free_job(job);
5105 }
5106 }
5107}
5108
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005109/*
5110 * Allocate a job. Sets the refcount to one and sets options default.
5111 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005112 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005113job_alloc(void)
5114{
5115 job_T *job;
5116
5117 job = (job_T *)alloc_clear(sizeof(job_T));
5118 if (job != NULL)
5119 {
5120 job->jv_refcount = 1;
5121 job->jv_stoponexit = vim_strsave((char_u *)"term");
5122
5123 if (first_job != NULL)
5124 {
5125 first_job->jv_prev = job;
5126 job->jv_next = first_job;
5127 }
5128 first_job = job;
5129 }
5130 return job;
5131}
5132
5133 void
5134job_set_options(job_T *job, jobopt_T *opt)
5135{
5136 if (opt->jo_set & JO_STOPONEXIT)
5137 {
5138 vim_free(job->jv_stoponexit);
5139 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5140 job->jv_stoponexit = NULL;
5141 else
5142 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5143 }
5144 if (opt->jo_set & JO_EXIT_CB)
5145 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005146 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005147 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005148 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005149 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005150 job->jv_exit_partial = NULL;
5151 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005152 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005153 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005154 job->jv_exit_partial = opt->jo_exit_partial;
5155 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005156 {
5157 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005158 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005159 }
5160 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005161 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005162 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005163 func_ref(job->jv_exit_cb);
5164 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005165 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005166 }
5167}
5168
5169/*
5170 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5171 */
5172 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005173job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005174{
5175 job_T *job;
5176
5177 for (job = first_job; job != NULL; job = job->jv_next)
5178 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005179 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005180}
5181
5182/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005183 * Return TRUE when there is any job that has an exit callback and might exit,
5184 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005185 */
5186 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005187has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005188{
5189 job_T *job;
5190
5191 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005192 /* Only should check if the channel has been closed, if the channel is
5193 * open the job won't exit. */
5194 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005195 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005196 return TRUE;
5197 return FALSE;
5198}
5199
Bram Moolenaar97792de2016-10-15 18:36:49 +02005200#define MAX_CHECK_ENDED 8
5201
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005202/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005203 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005204 */
5205 void
5206job_check_ended(void)
5207{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005208 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005209
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005210 if (first_job == NULL)
5211 return;
5212
Bram Moolenaar97792de2016-10-15 18:36:49 +02005213 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005214 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005215 /* NOTE: mch_detect_ended_job() must only return a job of which the
5216 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005217 job_T *job = mch_detect_ended_job(first_job);
5218
5219 if (job == NULL)
5220 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005221 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005222 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005223
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005224 if (channel_need_redraw)
5225 {
5226 channel_need_redraw = FALSE;
5227 redraw_after_callback();
5228 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005229}
5230
5231/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005232 * Create a job and return it. Implements job_start().
5233 * The returned job has a refcount of one.
5234 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005235 */
5236 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005237job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005238{
5239 job_T *job;
5240 char_u *cmd = NULL;
5241#if defined(UNIX)
5242# define USE_ARGV
5243 char **argv = NULL;
5244 int argc = 0;
5245#else
5246 garray_T ga;
5247#endif
5248 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005249 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005250
5251 job = job_alloc();
5252 if (job == NULL)
5253 return NULL;
5254
5255 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005256#ifndef USE_ARGV
5257 ga_init2(&ga, (int)sizeof(char*), 20);
5258#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005259
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005260 if (opt_arg != NULL)
5261 opt = *opt_arg;
5262 else
5263 {
5264 /* Default mode is NL. */
5265 clear_job_options(&opt);
5266 opt.jo_mode = MODE_NL;
5267 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005268 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5269 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5270 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005271 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005272 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005273
5274 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005275 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005276 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5277 && opt.jo_io[part] == JIO_FILE
5278 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5279 || *opt.jo_io_name[part] == NUL))
5280 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005281 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005282 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005283 }
5284
5285 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5286 {
5287 buf_T *buf = NULL;
5288
5289 /* check that we can find the buffer before starting the job */
5290 if (opt.jo_set & JO_IN_BUF)
5291 {
5292 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5293 if (buf == NULL)
5294 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5295 }
5296 else if (!(opt.jo_set & JO_IN_NAME))
5297 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005298 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005299 }
5300 else
5301 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5302 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005303 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005304 if (buf->b_ml.ml_mfp == NULL)
5305 {
5306 char_u numbuf[NUMBUFLEN];
5307 char_u *s;
5308
5309 if (opt.jo_set & JO_IN_BUF)
5310 {
5311 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5312 s = numbuf;
5313 }
5314 else
5315 s = opt.jo_io_name[PART_IN];
5316 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005317 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005318 }
5319 job->jv_in_buf = buf;
5320 }
5321
5322 job_set_options(job, &opt);
5323
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005324 if (argvars[0].v_type == VAR_STRING)
5325 {
5326 /* Command is a string. */
5327 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005328 if (cmd == NULL || *cmd == NUL)
5329 {
5330 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005331 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005332 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005333#ifdef USE_ARGV
5334 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005335 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005336 argv[argc] = NULL;
5337#endif
5338 }
5339 else if (argvars[0].v_type != VAR_LIST
5340 || argvars[0].vval.v_list == NULL
5341 || argvars[0].vval.v_list->lv_len < 1)
5342 {
5343 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005344 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005345 }
5346 else
5347 {
5348 list_T *l = argvars[0].vval.v_list;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005349#ifdef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005350 listitem_T *li;
5351 char_u *s;
5352
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005353 /* Pass argv[] to mch_call_shell(). */
5354 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5355 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005356 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005357 for (li = l->lv_first; li != NULL; li = li->li_next)
5358 {
5359 s = get_tv_string_chk(&li->li_tv);
5360 if (s == NULL)
5361 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005362 argv[argc++] = (char *)s;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005363 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005364 argv[argc] = NULL;
5365#else
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005366 if (win32_build_cmd(l, &ga) == FAIL)
5367 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005368 cmd = ga.ga_data;
5369#endif
5370 }
5371
5372#ifdef USE_ARGV
5373 if (ch_log_active())
5374 {
5375 garray_T ga;
5376 int i;
5377
5378 ga_init2(&ga, (int)sizeof(char), 200);
5379 for (i = 0; i < argc; ++i)
5380 {
5381 if (i > 0)
5382 ga_concat(&ga, (char_u *)" ");
5383 ga_concat(&ga, (char_u *)argv[i]);
5384 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005385 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005386 ga_clear(&ga);
5387 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005388 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005389#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005390 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005391 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005392#endif
5393
5394 /* If the channel is reading from a buffer, write lines now. */
5395 if (job->jv_channel != NULL)
5396 channel_write_in(job->jv_channel);
5397
5398theend:
5399#ifdef USE_ARGV
5400 vim_free(argv);
5401#else
5402 vim_free(ga.ga_data);
5403#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005404 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005405 return job;
5406}
5407
5408/*
5409 * Get the status of "job" and invoke the exit callback when needed.
5410 * The returned string is not allocated.
5411 */
5412 char *
5413job_status(job_T *job)
5414{
5415 char *result;
5416
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005417 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005418 /* No need to check, dead is dead. */
5419 result = "dead";
5420 else if (job->jv_status == JOB_FAILED)
5421 result = "fail";
5422 else
5423 {
5424 result = mch_job_status(job);
5425 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005426 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005427 }
5428 return result;
5429}
5430
Bram Moolenaar8950a562016-03-12 15:22:55 +01005431/*
5432 * Implementation of job_info().
5433 */
5434 void
5435job_info(job_T *job, dict_T *dict)
5436{
5437 dictitem_T *item;
5438 varnumber_T nr;
5439
5440 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5441
5442 item = dictitem_alloc((char_u *)"channel");
5443 if (item == NULL)
5444 return;
5445 item->di_tv.v_lock = 0;
5446 item->di_tv.v_type = VAR_CHANNEL;
5447 item->di_tv.vval.v_channel = job->jv_channel;
5448 if (job->jv_channel != NULL)
5449 ++job->jv_channel->ch_refcount;
5450 if (dict_add(dict, item) == FAIL)
5451 dictitem_free(item);
5452
5453#ifdef UNIX
5454 nr = job->jv_pid;
5455#else
5456 nr = job->jv_proc_info.dwProcessId;
5457#endif
5458 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005459 dict_add_nr_str(dict, "tty", 0L,
5460 job->jv_tty_name != NULL ? job->jv_tty_name : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005461
5462 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005463 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005464 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5465}
5466
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005467/*
5468 * Send a signal to "job". Implements job_stop().
5469 * When "type" is not NULL use this for the type.
5470 * Otherwise use argvars[1] for the type.
5471 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005472 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005473job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005474{
5475 char_u *arg;
5476
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005477 if (type != NULL)
5478 arg = (char_u *)type;
5479 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005480 arg = (char_u *)"";
5481 else
5482 {
5483 arg = get_tv_string_chk(&argvars[1]);
5484 if (arg == NULL)
5485 {
5486 EMSG(_(e_invarg));
5487 return 0;
5488 }
5489 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005490 if (job->jv_status == JOB_FAILED)
5491 {
5492 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5493 return 0;
5494 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005495 if (job->jv_status == JOB_ENDED)
5496 {
5497 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5498 return 0;
5499 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005500 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005501 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005502 return 0;
5503
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005504 /* Assume that only "kill" will kill the job. */
5505 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005506 job->jv_channel->ch_job_killed = TRUE;
5507
5508 /* We don't try freeing the job, obviously the caller still has a
5509 * reference to it. */
5510 return 1;
5511}
5512
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005513#endif /* FEAT_JOB_CHANNEL */