blob: de2f50de2b858b8a29bfa0dc53e807a3c08d4222 [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 Moolenaar71eeb742017-09-13 22:18:01 +0200141ch_log_lead(const 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 Moolenaar4ac2e8d2018-04-08 12:38:26 +0200161#ifndef PROTO /* prototype is in vim.h */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100162 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200163ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164{
165 if (log_fd != NULL)
166 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200167 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100168
Bram Moolenaar77073442016-02-13 23:23:53 +0100169 ch_log_lead("", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200170 va_start(ap, fmt);
171 vfprintf(log_fd, fmt, ap);
172 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100173 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100174 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100175 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176 }
177}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200178#endif
179
180 static void
181ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200182#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
183 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200184#endif
185 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100186
187 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200188ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200192 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193
Bram Moolenaar77073442016-02-13 23:23:53 +0100194 ch_log_lead("ERR ", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200195 va_start(ap, fmt);
196 vfprintf(log_fd, fmt, ap);
197 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100198 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100199 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100200 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 }
202}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100203
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100204#ifdef _WIN32
205# undef PERROR
206# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
207 (char_u *)msg, (char_u *)strerror_win32(errno))
208
209 static char *
210strerror_win32(int eno)
211{
212 static LPVOID msgbuf = NULL;
213 char_u *ptr;
214
215 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200216 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100217 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200218 msgbuf = NULL;
219 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100220 FormatMessage(
221 FORMAT_MESSAGE_ALLOCATE_BUFFER |
222 FORMAT_MESSAGE_FROM_SYSTEM |
223 FORMAT_MESSAGE_IGNORE_INSERTS,
224 NULL,
225 eno,
226 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
227 (LPTSTR) &msgbuf,
228 0,
229 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200230 if (msgbuf != NULL)
231 /* chomp \r or \n */
232 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
233 switch (*ptr)
234 {
235 case '\r':
236 STRMOVE(ptr, ptr + 1);
237 ptr--;
238 break;
239 case '\n':
240 if (*(ptr + 1) == '\0')
241 *ptr = '\0';
242 else
243 *ptr = ' ';
244 break;
245 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100246 return msgbuf;
247}
248#endif
249
Bram Moolenaar77073442016-02-13 23:23:53 +0100250/*
251 * The list of all allocated channels.
252 */
253static channel_T *first_channel = NULL;
254static int next_ch_id = 0;
255
256/*
257 * Allocate a new channel. The refcount is set to 1.
258 * The channel isn't actually used until it is opened.
259 * Returns NULL if out of memory.
260 */
261 channel_T *
262add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100263{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200264 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100265 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100266
Bram Moolenaar77073442016-02-13 23:23:53 +0100267 if (channel == NULL)
268 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100269
Bram Moolenaar77073442016-02-13 23:23:53 +0100270 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100271 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100272
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200273 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100274 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100275 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100276#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100277 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100278#endif
279#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100280 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100281#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100282 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100283 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100284
Bram Moolenaar77073442016-02-13 23:23:53 +0100285 if (first_channel != NULL)
286 {
287 first_channel->ch_prev = channel;
288 channel->ch_next = first_channel;
289 }
290 first_channel = channel;
291
292 channel->ch_refcount = 1;
293 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100294}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100295
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200296 int
297has_any_channel(void)
298{
299 return first_channel != NULL;
300}
301
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100302/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100303 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100304 * Return TRUE if "channel" has a callback and the associated job wasn't
305 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100306 */
307 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100308channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100309{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100310 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100311 int has_out_msg;
312 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100313
314 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100315 if (channel->ch_job_killed && channel->ch_job == NULL)
316 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100317
318 /* If there is a close callback it may still need to be invoked. */
319 if (channel->ch_close_cb != NULL)
320 return TRUE;
321
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200322 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200323 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200324 return TRUE;
325
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100326 /* If there is no callback then nobody can get readahead. If the fd is
327 * closed and there is no readahead then the callback won't be called. */
328 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100329 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
330 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100331 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
332 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
333 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
334 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
335 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
336 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100337 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100338 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200339 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200340 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
341 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200342 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200343 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
344 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100345}
346
347/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200348 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
349 */
350 static int
351channel_can_close(channel_T *channel)
352{
353 return channel->ch_to_be_closed == 0;
354}
355
356/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200357 * Close a channel and free all its resources.
358 */
359 static void
360channel_free_contents(channel_T *channel)
361{
362 channel_close(channel, TRUE);
363 channel_clear(channel);
364 ch_log(channel, "Freeing channel");
365}
366
367 static void
368channel_free_channel(channel_T *channel)
369{
370 if (channel->ch_next != NULL)
371 channel->ch_next->ch_prev = channel->ch_prev;
372 if (channel->ch_prev == NULL)
373 first_channel = channel->ch_next;
374 else
375 channel->ch_prev->ch_next = channel->ch_next;
376 vim_free(channel);
377}
378
379 static void
380channel_free(channel_T *channel)
381{
382 if (!in_free_unref_items)
383 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200384 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200385 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200386 else
387 {
388 channel_free_contents(channel);
389 channel_free_channel(channel);
390 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200391 }
392}
393
394/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100395 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100396 * possible, there is no callback to be invoked or the associated job was
397 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100398 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100399 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100400 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100401channel_may_free(channel_T *channel)
402{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100403 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100404 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100405 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100406 return TRUE;
407 }
408 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100409}
410
411/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100412 * Decrement the reference count on "channel" and maybe free it when it goes
413 * down to zero. Don't free it if there is a pending action.
414 * Returns TRUE when the channel is no longer referenced.
415 */
416 int
417channel_unref(channel_T *channel)
418{
419 if (channel != NULL && --channel->ch_refcount <= 0)
420 return channel_may_free(channel);
421 return FALSE;
422}
423
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200424 int
425free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100426{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200427 int did_free = FALSE;
428 channel_T *ch;
429
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200430 /* This is invoked from the garbage collector, which only runs at a safe
431 * point. */
432 ++safe_to_invoke_callback;
433
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200434 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200435 if (!channel_still_useful(ch)
436 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437 {
438 /* Free the channel and ordinary items it contains, but don't
439 * recurse into Lists, Dictionaries etc. */
440 channel_free_contents(ch);
441 did_free = TRUE;
442 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200443
444 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200445 return did_free;
446}
447
448 void
449free_unused_channels(int copyID, int mask)
450{
451 channel_T *ch;
452 channel_T *ch_next;
453
454 for (ch = first_channel; ch != NULL; ch = ch_next)
455 {
456 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200457 if (!channel_still_useful(ch)
458 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200459 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200460 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200461 channel_free_channel(ch);
462 }
463 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100464}
465
Bram Moolenaard04a0202016-01-26 23:30:18 +0100466#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100467
468#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
469 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100470channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100471{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100472 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200473 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100474
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100475 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100476 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200477 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100478 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200479 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100480}
481#endif
482
Bram Moolenaare0874f82016-01-24 20:36:41 +0100483/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100484 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100485 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100486#ifdef FEAT_GUI_X11
487 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200488messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200489 int *unused1 UNUSED,
490 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100491{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100492 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100493}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100494#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100495
Bram Moolenaard04a0202016-01-26 23:30:18 +0100496#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100497# if GTK_CHECK_VERSION(3,0,0)
498 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200499messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200500 GIOCondition unused2 UNUSED,
501 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100502{
503 channel_read_fd(GPOINTER_TO_INT(clientData));
504 return TRUE; /* Return FALSE instead in case the event source is to
505 * be removed after this function returns. */
506}
507# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100508 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200509messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200510 gint unused1 UNUSED,
511 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100513 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100514}
Bram Moolenaar98921892016-02-23 17:14:37 +0100515# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100516#endif
517
518 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200519channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100520{
Bram Moolenaarde279892016-03-11 22:19:44 +0100521 if (!CH_HAS_GUI)
522 return;
523
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200524 /* gets stuck in handling events for a not connected channel */
525 if (channel->ch_keep_open)
526 return;
527
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100528# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200529 /* Tell notifier we are interested in being called when there is input on
530 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100531 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200532 {
533 ch_log(channel, "Registering part %s with fd %d",
534 part_names[part], channel->ch_part[part].ch_fd);
535
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100536 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100537 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100538 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100539 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200540 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100541 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200542 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100543# else
544# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200545 /* Tell gdk we are interested in being called when there is input on the
546 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100547 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100548 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200549 ch_log(channel, "Registering part %s with fd %d",
550 part_names[part], channel->ch_part[part].ch_fd);
551# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100552 GIOChannel *chnnl = g_io_channel_unix_new(
553 (gint)channel->ch_part[part].ch_fd);
554
555 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
556 chnnl,
557 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200558 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100559 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
560
561 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100562# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100563 channel->ch_part[part].ch_inputHandler = gdk_input_add(
564 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100565 (GdkInputCondition)
566 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200567 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100568 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100569# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200570 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100571# endif
572# endif
573}
574
Bram Moolenaarde279892016-03-11 22:19:44 +0100575 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100576channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100577{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100578 if (channel->CH_SOCK_FD != INVALID_FD)
579 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200580 if (channel->CH_OUT_FD != INVALID_FD
581 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100582 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200583 if (channel->CH_ERR_FD != INVALID_FD
584 && channel->CH_ERR_FD != channel->CH_SOCK_FD
585 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100586 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100587}
588
589/*
590 * Register any of our file descriptors with the GUI event handling system.
591 * Called when the GUI has started.
592 */
593 void
594channel_gui_register_all(void)
595{
Bram Moolenaar77073442016-02-13 23:23:53 +0100596 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100597
Bram Moolenaar77073442016-02-13 23:23:53 +0100598 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100599 channel_gui_register(channel);
600}
601
602 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200603channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100604{
605# ifdef FEAT_GUI_X11
606 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
607 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200608 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100609 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
610 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
611 }
612# else
613# ifdef FEAT_GUI_GTK
614 if (channel->ch_part[part].ch_inputHandler != 0)
615 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200616 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100617# if GTK_CHECK_VERSION(3,0,0)
618 g_source_remove(channel->ch_part[part].ch_inputHandler);
619# else
620 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
621# endif
622 channel->ch_part[part].ch_inputHandler = 0;
623 }
624# endif
625# endif
626}
627
628 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100629channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100630{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200631 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100632
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100633 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100634 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100635}
636
637#endif
638
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100639static char *e_cannot_connect = N_("E902: Cannot connect to port");
640
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100642 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100643 * "waittime" is the time in msec to wait for the connection.
644 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100645 * Returns the channel for success.
646 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100647 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100648 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100649channel_open(
650 char *hostname,
651 int port_in,
652 int waittime,
653 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100655 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100656 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100657 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100658#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100659 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100660 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100661#else
662 int port = port_in;
663#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100664 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100665 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100666
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100667#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100668 channel_init_winsock();
669#endif
670
Bram Moolenaar77073442016-02-13 23:23:53 +0100671 channel = add_channel();
672 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100675 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100676 }
677
678 /* Get the server internet address and put into addr structure */
679 /* fill in the socket address structure and connect to server */
680 vim_memset((char *)&server, 0, sizeof(server));
681 server.sin_family = AF_INET;
682 server.sin_port = htons(port);
683 if ((host = gethostbyname(hostname)) == NULL)
684 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100685 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200686 PERROR(_("E901: gethostbyname() 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 Moolenaar7173b472017-01-14 17:04:38 +0100690 {
691 char *p;
692
Bram Moolenaar2e324952018-04-14 14:37:07 +0200693 /* When using host->h_addr_list[0] directly ubsan warns for it to not
694 * be aligned. First copy the pointer to avoid that. */
695 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100696 memcpy((char *)&server.sin_addr, p, host->h_length);
697 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100698
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100699 /* On Mac and Solaris a zero timeout almost never works. At least wait
700 * one millisecond. Let's do it for all systems, because we don't know why
701 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100702 if (waittime == 0)
703 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100704
705 /*
706 * For Unix we need to call connect() again after connect() failed.
707 * On Win32 one time is sufficient.
708 */
709 while (TRUE)
710 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100711 long elapsed_msec = 0;
712 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100713
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100714 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100715 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100716 sd = socket(AF_INET, SOCK_STREAM, 0);
717 if (sd == -1)
718 {
719 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200720 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100721 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100722 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100723 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100724
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100725 if (waittime >= 0)
726 {
727 /* Make connect() non-blocking. */
728 if (
729#ifdef _WIN32
730 ioctlsocket(sd, FIONBIO, &val) < 0
731#else
732 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
733#endif
734 )
735 {
736 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200737 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100738 "channel_open: Connect failed with errno %d", errno);
739 sock_close(sd);
740 channel_free(channel);
741 return NULL;
742 }
743 }
744
745 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200746 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100747 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
748
Bram Moolenaar045a2842016-03-08 22:33:07 +0100749 if (ret == 0)
750 /* The connection could be established. */
751 break;
752
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100753 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100754 if (waittime < 0 || (errno != EWOULDBLOCK
755 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100756#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100757 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100758#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100759 ))
760 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200761 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100762 "channel_open: Connect failed with errno %d", errno);
763 PERROR(_(e_cannot_connect));
764 sock_close(sd);
765 channel_free(channel);
766 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100767 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100768
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100769 /* Limit the waittime to 50 msec. If it doesn't work within this
770 * time we close the socket and try creating it again. */
771 waitnow = waittime > 50 ? 50 : waittime;
772
Bram Moolenaar045a2842016-03-08 22:33:07 +0100773 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100774 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100775#ifndef WIN32
776 if (errno != ECONNREFUSED)
777#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100778 {
779 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100780 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100781 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100782#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100783 int so_error = 0;
784 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100785 struct timeval start_tv;
786 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100787#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100788 FD_ZERO(&rfds);
789 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100790 FD_ZERO(&wfds);
791 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100792
Bram Moolenaar562ca712016-03-09 21:50:05 +0100793 tv.tv_sec = waitnow / 1000;
794 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100795#ifndef WIN32
796 gettimeofday(&start_tv, NULL);
797#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200798 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100799 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100800 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100801
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100802 if (ret < 0)
803 {
804 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200805 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 "channel_open: Connect failed with errno %d", errno);
807 PERROR(_(e_cannot_connect));
808 sock_close(sd);
809 channel_free(channel);
810 return NULL;
811 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100812
Bram Moolenaare081e212016-02-28 22:33:46 +0100813#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100814 /* On Win32: select() is expected to work and wait for up to
815 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100816 if (FD_ISSET(sd, &wfds))
817 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100818 elapsed_msec = waitnow;
819 if (waittime > 1 && elapsed_msec < waittime)
820 {
821 waittime -= elapsed_msec;
822 continue;
823 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100824#else
825 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100826 * After putting the socket in non-blocking mode, connect() will
827 * return EINPROGRESS, select() will not wait (as if writing is
828 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100829 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100830 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100831 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100832 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100833 {
834 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100835 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100836 if (ret < 0 || (so_error != 0
837 && so_error != EWOULDBLOCK
838 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100839# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100840 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100841# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100842 ))
843 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200844 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100845 "channel_open: Connect failed with errno %d",
846 so_error);
847 PERROR(_(e_cannot_connect));
848 sock_close(sd);
849 channel_free(channel);
850 return NULL;
851 }
852 }
853
Bram Moolenaar045a2842016-03-08 22:33:07 +0100854 if (FD_ISSET(sd, &wfds) && so_error == 0)
855 /* Did not detect an error, connection is established. */
856 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100857
Bram Moolenaar045a2842016-03-08 22:33:07 +0100858 gettimeofday(&end_tv, NULL);
859 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
860 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100861#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100862 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100863
864#ifndef WIN32
865 if (waittime > 1 && elapsed_msec < waittime)
866 {
867 /* The port isn't ready but we also didn't get an error.
868 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100869 * yet. Select() may return early, wait until the remaining
870 * "waitnow" and try again. */
871 waitnow -= elapsed_msec;
872 waittime -= elapsed_msec;
873 if (waitnow > 0)
874 {
875 mch_delay((long)waitnow, TRUE);
876 ui_breakcheck();
877 waittime -= waitnow;
878 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100879 if (!got_int)
880 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100881 if (waittime <= 0)
882 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100883 waittime = 1;
884 continue;
885 }
886 /* we were interrupted, behave as if timed out */
887 }
888#endif
889
890 /* We timed out. */
891 ch_error(channel, "Connection timed out");
892 sock_close(sd);
893 channel_free(channel);
894 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100895 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100896
Bram Moolenaar045a2842016-03-08 22:33:07 +0100897 ch_log(channel, "Connection made");
898
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100899 if (waittime >= 0)
900 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100901#ifdef _WIN32
902 val = 0;
903 ioctlsocket(sd, FIONBIO, &val);
904#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100905 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100906#endif
907 }
908
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100909 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100910 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100911 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
912 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200913 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100914
915#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100916 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100917#endif
918
Bram Moolenaar77073442016-02-13 23:23:53 +0100919 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100920}
921
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100922/*
923 * Implements ch_open().
924 */
925 channel_T *
926channel_open_func(typval_T *argvars)
927{
928 char_u *address;
929 char_u *p;
930 char *rest;
931 int port;
932 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200933 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100934
935 address = get_tv_string(&argvars[0]);
936 if (argvars[1].v_type != VAR_UNKNOWN
937 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
938 {
939 EMSG(_(e_invarg));
940 return NULL;
941 }
942
943 /* parse address */
944 p = vim_strchr(address, ':');
945 if (p == NULL)
946 {
947 EMSG2(_(e_invarg2), address);
948 return NULL;
949 }
950 *p++ = NUL;
951 port = strtol((char *)p, &rest, 10);
952 if (*address == NUL || port <= 0 || *rest != NUL)
953 {
954 p[-1] = ':';
955 EMSG2(_(e_invarg2), address);
956 return NULL;
957 }
958
959 /* parse options */
960 clear_job_options(&opt);
961 opt.jo_mode = MODE_JSON;
962 opt.jo_timeout = 2000;
963 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200964 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200965 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100966 if (opt.jo_timeout < 0)
967 {
968 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200969 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100970 }
971
972 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
973 if (channel != NULL)
974 {
975 opt.jo_set = JO_ALL;
976 channel_set_options(channel, &opt);
977 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200978theend:
979 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100980 return channel;
981}
982
Bram Moolenaarde279892016-03-11 22:19:44 +0100983 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200984ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100985{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200986 sock_T *fd = &channel->ch_part[part].ch_fd;
987
Bram Moolenaarde279892016-03-11 22:19:44 +0100988 if (*fd != INVALID_FD)
989 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200990 if (part == PART_SOCK)
991 sock_close(*fd);
992 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200993 {
994 /* When using a pty the same FD is set on multiple parts, only
995 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200996 if ((part == PART_IN || channel->CH_IN_FD != *fd)
997 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
998 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200999 {
1000#ifdef WIN32
1001 if (channel->ch_named_pipe)
1002 DisconnectNamedPipe((HANDLE)fd);
1003#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001004 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001005 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001006 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001007 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001008
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001009 /* channel is closed, may want to end the job if it was the last */
1010 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001011 }
1012}
1013
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001014 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001015channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001016{
Bram Moolenaarde279892016-03-11 22:19:44 +01001017 if (in != INVALID_FD)
1018 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001019 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001020 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001021# if defined(UNIX)
1022 /* Do not end the job when all output channels are closed, wait until
1023 * the job ended. */
1024 if (isatty(in))
1025 channel->ch_to_be_closed |= (1U << PART_IN);
1026# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001027 }
1028 if (out != INVALID_FD)
1029 {
1030# if defined(FEAT_GUI)
1031 channel_gui_unregister_one(channel, PART_OUT);
1032# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001033 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001034 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001035 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001036# if defined(FEAT_GUI)
1037 channel_gui_register_one(channel, PART_OUT);
1038# endif
1039 }
1040 if (err != INVALID_FD)
1041 {
1042# if defined(FEAT_GUI)
1043 channel_gui_unregister_one(channel, PART_ERR);
1044# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001045 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001046 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001047 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001048# if defined(FEAT_GUI)
1049 channel_gui_register_one(channel, PART_ERR);
1050# endif
1051 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001052}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001053
Bram Moolenaard6051b52016-02-28 15:49:03 +01001054/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001055 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001056 * This does not keep a refcount, when the job is freed ch_job is cleared.
1057 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001058 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001059channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001060{
Bram Moolenaar77073442016-02-13 23:23:53 +01001061 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001062
1063 channel_set_options(channel, options);
1064
1065 if (job->jv_in_buf != NULL)
1066 {
1067 chanpart_T *in_part = &channel->ch_part[PART_IN];
1068
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001069 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001070 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001071 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001072 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001073 {
1074 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1075 {
1076 /* Special mode: send last-but-one line when appending a line
1077 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001078 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001079 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001080 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001081 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001082 }
1083 else
1084 in_part->ch_buf_top = options->jo_in_top;
1085 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001086 else
1087 in_part->ch_buf_top = 1;
1088 if (options->jo_set & JO_IN_BOT)
1089 in_part->ch_buf_bot = options->jo_in_bot;
1090 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001091 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001092 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001093}
1094
1095/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001096 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001097 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001098 */
1099 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001100find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001101{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001102 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001103 buf_T *save_curbuf = curbuf;
1104
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001105 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001106 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001107 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001108 if (buf == NULL)
1109 buf = buflist_findname_exp(name);
1110 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001111 if (buf == NULL)
1112 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001113 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001114 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001115 if (buf == NULL)
1116 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001117 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001118 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001119#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001120 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1121 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001122#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001123 if (curbuf->b_ml.ml_mfp == NULL)
1124 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001125 if (msg)
1126 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001127 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001128 changed_bytes(1, 0);
1129 curbuf = save_curbuf;
1130 }
1131
1132 return buf;
1133}
1134
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001135 static void
1136set_callback(
1137 char_u **cbp,
1138 partial_T **pp,
1139 char_u *callback,
1140 partial_T *partial)
1141{
1142 free_callback(*cbp, *pp);
1143 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001144 {
1145 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001146 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001147 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001148 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001149 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001150 func_ref(*cbp);
1151 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001152 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001153 else
1154 *cbp = NULL;
1155 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001156 if (partial != NULL)
1157 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001158}
1159
Bram Moolenaar187db502016-02-27 14:44:26 +01001160/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001161 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001162 */
1163 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001164channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001165{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001166 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001167
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001168 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001169 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001170 channel->ch_part[part].ch_mode = opt->jo_mode;
1171 if (opt->jo_set & JO_IN_MODE)
1172 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1173 if (opt->jo_set & JO_OUT_MODE)
1174 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1175 if (opt->jo_set & JO_ERR_MODE)
1176 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001177
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001178 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001179 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001180 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1181 if (opt->jo_set & JO_OUT_TIMEOUT)
1182 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1183 if (opt->jo_set & JO_ERR_TIMEOUT)
1184 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001185 if (opt->jo_set & JO_BLOCK_WRITE)
1186 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001187
1188 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001189 set_callback(&channel->ch_callback, &channel->ch_partial,
1190 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001191 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001192 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1193 &channel->ch_part[PART_OUT].ch_partial,
1194 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001195 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001196 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1197 &channel->ch_part[PART_ERR].ch_partial,
1198 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001199 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001200 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1201 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001202 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001203
1204 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1205 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001206 buf_T *buf;
1207
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001208 /* writing output to a buffer. Default mode is NL. */
1209 if (!(opt->jo_set & JO_OUT_MODE))
1210 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001211 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001212 {
1213 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1214 if (buf == NULL)
1215 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1216 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001217 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001218 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001219 int msg = TRUE;
1220
1221 if (opt->jo_set2 & JO2_OUT_MSG)
1222 msg = opt->jo_message[PART_OUT];
1223 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001224 }
1225 if (buf != NULL)
1226 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001227 if (opt->jo_set & JO_OUT_MODIFIABLE)
1228 channel->ch_part[PART_OUT].ch_nomodifiable =
1229 !opt->jo_modifiable[PART_OUT];
1230
1231 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1232 {
1233 EMSG(_(e_modifiable));
1234 }
1235 else
1236 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001237 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001238 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001239 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001240 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001241 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001242 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001243
1244 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1245 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1246 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1247 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 buf_T *buf;
1249
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001250 /* writing err to a buffer. Default mode is NL. */
1251 if (!(opt->jo_set & JO_ERR_MODE))
1252 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1253 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001254 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001255 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001256 {
1257 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1258 if (buf == NULL)
1259 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1260 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001261 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001262 {
1263 int msg = TRUE;
1264
1265 if (opt->jo_set2 & JO2_ERR_MSG)
1266 msg = opt->jo_message[PART_ERR];
1267 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1268 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001269 if (buf != NULL)
1270 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001271 if (opt->jo_set & JO_ERR_MODIFIABLE)
1272 channel->ch_part[PART_ERR].ch_nomodifiable =
1273 !opt->jo_modifiable[PART_ERR];
1274 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1275 {
1276 EMSG(_(e_modifiable));
1277 }
1278 else
1279 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001280 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001281 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001282 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001283 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001284 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001285 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001286
1287 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1288 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1289 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001290}
1291
1292/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001293 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001294 */
1295 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001296channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001297 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001298 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001299 char_u *callback,
1300 partial_T *partial,
1301 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001302{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001303 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001304 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1305
1306 if (item != NULL)
1307 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001308 item->cq_partial = partial;
1309 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001310 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001311 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001312 item->cq_callback = callback;
1313 }
1314 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001315 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001316 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001317 func_ref(item->cq_callback);
1318 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001319 item->cq_seq_nr = id;
1320 item->cq_prev = head->cq_prev;
1321 head->cq_prev = item;
1322 item->cq_next = NULL;
1323 if (item->cq_prev == NULL)
1324 head->cq_next = item;
1325 else
1326 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001327 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001328}
1329
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001330 static void
1331write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1332{
1333 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001334 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001335 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001336 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001337
Bram Moolenaar655da312016-05-28 22:22:34 +02001338 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001339 if ((p = alloc(len + 2)) == NULL)
1340 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001341 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001342
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001343 if (channel->ch_write_text_mode)
1344 p[len] = CAR;
1345 else
1346 {
1347 for (i = 0; i < len; ++i)
1348 if (p[i] == NL)
1349 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001350
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001351 p[len] = NL;
1352 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001353 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001354 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001355 vim_free(p);
1356}
1357
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001358/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001359 * Return TRUE if "channel" can be written to.
1360 * Returns FALSE if the input is closed or the write would block.
1361 */
1362 static int
1363can_write_buf_line(channel_T *channel)
1364{
1365 chanpart_T *in_part = &channel->ch_part[PART_IN];
1366
1367 if (in_part->ch_fd == INVALID_FD)
1368 return FALSE; /* pipe was closed */
1369
1370 /* for testing: block every other attempt to write */
1371 if (in_part->ch_block_write == 1)
1372 in_part->ch_block_write = -1;
1373 else if (in_part->ch_block_write == -1)
1374 in_part->ch_block_write = 1;
1375
1376 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1377#ifndef WIN32
1378 {
1379# if defined(HAVE_SELECT)
1380 struct timeval tval;
1381 fd_set wfds;
1382 int ret;
1383
1384 FD_ZERO(&wfds);
1385 FD_SET((int)in_part->ch_fd, &wfds);
1386 tval.tv_sec = 0;
1387 tval.tv_usec = 0;
1388 for (;;)
1389 {
1390 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1391# ifdef EINTR
1392 SOCK_ERRNO;
1393 if (ret == -1 && errno == EINTR)
1394 continue;
1395# endif
1396 if (ret <= 0 || in_part->ch_block_write == 1)
1397 {
1398 if (ret > 0)
1399 ch_log(channel, "FAKED Input not ready for writing");
1400 else
1401 ch_log(channel, "Input not ready for writing");
1402 return FALSE;
1403 }
1404 break;
1405 }
1406# else
1407 struct pollfd fds;
1408
1409 fds.fd = in_part->ch_fd;
1410 fds.events = POLLOUT;
1411 if (poll(&fds, 1, 0) <= 0)
1412 {
1413 ch_log(channel, "Input not ready for writing");
1414 return FALSE;
1415 }
1416 if (in_part->ch_block_write == 1)
1417 {
1418 ch_log(channel, "FAKED Input not ready for writing");
1419 return FALSE;
1420 }
1421# endif
1422 }
1423#endif
1424 return TRUE;
1425}
1426
1427/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001428 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001429 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001430 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001431channel_write_in(channel_T *channel)
1432{
1433 chanpart_T *in_part = &channel->ch_part[PART_IN];
1434 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001435 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001436 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001437
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001438 if (buf == NULL || in_part->ch_buf_append)
1439 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001440 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001441 {
1442 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001443 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001444 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001445 return;
1446 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001447
1448 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1449 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1450 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001451 if (!can_write_buf_line(channel))
1452 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001453 write_buf_line(buf, lnum, channel);
1454 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001455 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001456
1457 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001458 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001459 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001460 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001461
Bram Moolenaar014069a2016-03-03 22:51:40 +01001462 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001463 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001464 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001465#if defined(FEAT_TERMINAL)
1466 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001467 if (channel->ch_job != NULL)
1468 term_send_eof(channel);
1469#endif
1470
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001471 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001472 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001473 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001474
1475 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001476 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001477 }
1478 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001479 ch_log(channel, "Still %ld more lines to write",
1480 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001481}
1482
1483/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001484 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001485 */
1486 void
1487channel_buffer_free(buf_T *buf)
1488{
1489 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001490 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001491
1492 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001493 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001494 {
1495 chanpart_T *ch_part = &channel->ch_part[part];
1496
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001497 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001498 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001499 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001500 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001501 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001502 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001503 }
1504}
1505
1506/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001507 * Write any lines waiting to be written to "channel".
1508 */
1509 static void
1510channel_write_input(channel_T *channel)
1511{
1512 chanpart_T *in_part = &channel->ch_part[PART_IN];
1513
1514 if (in_part->ch_writeque.wq_next != NULL)
1515 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1516 else if (in_part->ch_bufref.br_buf != NULL)
1517 {
1518 if (in_part->ch_buf_append)
1519 channel_write_new_lines(in_part->ch_bufref.br_buf);
1520 else
1521 channel_write_in(channel);
1522 }
1523}
1524
1525/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001526 * Write any lines waiting to be written to a channel.
1527 */
1528 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001529channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001530{
1531 channel_T *channel;
1532
1533 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001534 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001535}
1536
1537/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001538 * Write appended lines above the last one in "buf" to the channel.
1539 */
1540 void
1541channel_write_new_lines(buf_T *buf)
1542{
1543 channel_T *channel;
1544 int found_one = FALSE;
1545
1546 /* There could be more than one channel for the buffer, loop over all of
1547 * them. */
1548 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1549 {
1550 chanpart_T *in_part = &channel->ch_part[PART_IN];
1551 linenr_T lnum;
1552 int written = 0;
1553
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001554 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001555 {
1556 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001557 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001558 found_one = TRUE;
1559 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1560 ++lnum)
1561 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001562 if (!can_write_buf_line(channel))
1563 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001564 write_buf_line(buf, lnum, channel);
1565 ++written;
1566 }
1567
1568 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001569 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001570 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001571 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001572 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001573 ch_log(channel, "Still %ld more lines to write",
1574 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001575
1576 in_part->ch_buf_bot = lnum;
1577 }
1578 }
1579 if (!found_one)
1580 buf->b_write_to_channel = FALSE;
1581}
1582
1583/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001584 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001585 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001586 */
1587 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001588invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1589 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001590{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001591 typval_T rettv;
1592 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001593
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001594 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001595 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001596
Bram Moolenaar77073442016-02-13 23:23:53 +01001597 argv[0].v_type = VAR_CHANNEL;
1598 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001599
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001600 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1601 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001602 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001603 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001604}
1605
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001606/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001607 * Return the first node from "channel"/"part" without removing it.
1608 * Returns NULL if there is nothing.
1609 */
1610 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001611channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001612{
1613 readq_T *head = &channel->ch_part[part].ch_head;
1614
1615 return head->rq_next;
1616}
1617
1618/*
1619 * Return a pointer to the first NL in "node".
1620 * Skips over NUL characters.
1621 * Returns NULL if there is no NL.
1622 */
1623 char_u *
1624channel_first_nl(readq_T *node)
1625{
1626 char_u *buffer = node->rq_buffer;
1627 long_u i;
1628
1629 for (i = 0; i < node->rq_buflen; ++i)
1630 if (buffer[i] == NL)
1631 return buffer + i;
1632 return NULL;
1633}
1634
1635/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001636 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001637 * The caller must free it.
1638 * Returns NULL if there is nothing.
1639 */
1640 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001641channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001642{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001643 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001644 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001645 char_u *p;
1646
Bram Moolenaar77073442016-02-13 23:23:53 +01001647 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001648 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001649 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001650 p = node->rq_buffer;
1651 head->rq_next = node->rq_next;
1652 if (node->rq_next == NULL)
1653 head->rq_prev = NULL;
1654 else
1655 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001656 vim_free(node);
1657 return p;
1658}
1659
1660/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001661 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001662 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001663 */
1664 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001665channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001666{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001667 readq_T *head = &channel->ch_part[part].ch_head;
1668 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001669 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001670 char_u *res;
1671 char_u *p;
1672
1673 /* If there is only one buffer just get that one. */
1674 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1675 return channel_get(channel, part);
1676
1677 /* Concatenate everything into one buffer. */
1678 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001679 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001680 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001681 if (res == NULL)
1682 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001683 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001684 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001685 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001686 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001687 p += node->rq_buflen;
1688 }
1689 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001690
1691 /* Free all buffers */
1692 do
1693 {
1694 p = channel_get(channel, part);
1695 vim_free(p);
1696 } while (p != NULL);
1697
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001698 /* turn all NUL into NL */
1699 while (len > 0)
1700 {
1701 --len;
1702 if (res[len] == NUL)
1703 res[len] = NL;
1704 }
1705
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001706 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001707}
1708
1709/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001710 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001711 * Caller must check these bytes are available.
1712 */
1713 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001714channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001715{
1716 readq_T *head = &channel->ch_part[part].ch_head;
1717 readq_T *node = head->rq_next;
1718 char_u *buf = node->rq_buffer;
1719
1720 mch_memmove(buf, buf + len, node->rq_buflen - len);
1721 node->rq_buflen -= len;
1722}
1723
1724/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001725 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001726 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001727 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001728 */
1729 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001730channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001731{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001732 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001733 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734 readq_T *last_node;
1735 readq_T *n;
1736 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001737 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001738 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001739
Bram Moolenaar77073442016-02-13 23:23:53 +01001740 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001741 return FAIL;
1742
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001743 last_node = node->rq_next;
1744 len = node->rq_buflen + last_node->rq_buflen + 1;
1745 if (want_nl)
1746 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001747 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001748 {
1749 last_node = last_node->rq_next;
1750 len += last_node->rq_buflen;
1751 }
1752
1753 p = newbuf = alloc(len);
1754 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001755 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001756 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001757 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001758 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001759 node->rq_buffer = newbuf;
1760 for (n = node; n != last_node; )
1761 {
1762 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001763 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001764 p += n->rq_buflen;
1765 vim_free(n->rq_buffer);
1766 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001767 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001768
1769 /* dispose of the collapsed nodes and their buffers */
1770 for (n = node->rq_next; n != last_node; )
1771 {
1772 n = n->rq_next;
1773 vim_free(n->rq_prev);
1774 }
1775 node->rq_next = last_node->rq_next;
1776 if (last_node->rq_next == NULL)
1777 head->rq_prev = node;
1778 else
1779 last_node->rq_next->rq_prev = node;
1780 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001781 return OK;
1782}
1783
1784/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001786 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001787 * Returns OK or FAIL.
1788 */
1789 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001790channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001791 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001792{
1793 readq_T *node;
1794 readq_T *head = &channel->ch_part[part].ch_head;
1795 char_u *p;
1796 int i;
1797
1798 node = (readq_T *)alloc(sizeof(readq_T));
1799 if (node == NULL)
1800 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001801 /* A NUL is added at the end, because netbeans code expects that.
1802 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001803 node->rq_buffer = alloc(len + 1);
1804 if (node->rq_buffer == NULL)
1805 {
1806 vim_free(node);
1807 return FAIL; /* out of memory */
1808 }
1809
1810 if (channel->ch_part[part].ch_mode == MODE_NL)
1811 {
1812 /* Drop any CR before a NL. */
1813 p = node->rq_buffer;
1814 for (i = 0; i < len; ++i)
1815 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1816 *p++ = buf[i];
1817 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001818 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001819 }
1820 else
1821 {
1822 mch_memmove(node->rq_buffer, buf, len);
1823 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001825 }
1826
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001827 if (prepend)
1828 {
1829 /* preend node to the head of the queue */
1830 node->rq_next = head->rq_next;
1831 node->rq_prev = NULL;
1832 if (head->rq_next == NULL)
1833 head->rq_prev = node;
1834 else
1835 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001836 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001837 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001838 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001839 {
1840 /* append node to the tail of the queue */
1841 node->rq_next = NULL;
1842 node->rq_prev = head->rq_prev;
1843 if (head->rq_prev == NULL)
1844 head->rq_next = node;
1845 else
1846 head->rq_prev->rq_next = node;
1847 head->rq_prev = node;
1848 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001849
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001850 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001851 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001852 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001853 fprintf(log_fd, "'");
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001854 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001855 fprintf(log_fd, "'\n");
1856 }
1857 return OK;
1858}
1859
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001860/*
1861 * Try to fill the buffer of "reader".
1862 * Returns FALSE when nothing was added.
1863 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001864 static int
1865channel_fill(js_read_T *reader)
1866{
1867 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001868 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001869 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001870 int keeplen;
1871 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001872 char_u *p;
1873
1874 if (next == NULL)
1875 return FALSE;
1876
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001877 keeplen = reader->js_end - reader->js_buf;
1878 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001879 {
1880 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001881 addlen = (int)STRLEN(next);
1882 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001883 if (p == NULL)
1884 {
1885 vim_free(next);
1886 return FALSE;
1887 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001888 mch_memmove(p, reader->js_buf, keeplen);
1889 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001890 vim_free(next);
1891 next = p;
1892 }
1893
1894 vim_free(reader->js_buf);
1895 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001896 return TRUE;
1897}
1898
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001899/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001900 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001901 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001902 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001903 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001904 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001905channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001906{
1907 js_read_T reader;
1908 typval_T listtv;
1909 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001910 chanpart_T *chanpart = &channel->ch_part[part];
1911 jsonq_T *head = &chanpart->ch_json_head;
1912 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001913 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001914
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001915 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001916 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001917
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001918 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001919 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001920 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001921 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001922 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001923
1924 /* When a message is incomplete we wait for a short while for more to
1925 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001926 * or list will make us hang.
1927 * Do not generate error messages, they will be written in a channel log. */
1928 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001929 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001930 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001931 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001932 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001933 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001934 /* Only accept the response when it is a list with at least two
1935 * items. */
1936 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001937 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001938 if (listtv.v_type != VAR_LIST)
1939 ch_error(channel, "Did not receive a list, discarding");
1940 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001941 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001942 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001943 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001944 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001945 else
1946 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001947 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1948 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001950 else
1951 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001952 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001953 item->jq_value = alloc_tv();
1954 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001955 {
1956 vim_free(item);
1957 clear_tv(&listtv);
1958 }
1959 else
1960 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001961 *item->jq_value = listtv;
1962 item->jq_prev = head->jq_prev;
1963 head->jq_prev = item;
1964 item->jq_next = NULL;
1965 if (item->jq_prev == NULL)
1966 head->jq_next = item;
1967 else
1968 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001969 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001970 }
1971 }
1972 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001973
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001974 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001975 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001976 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001977 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001978 size_t buflen = STRLEN(reader.js_buf);
1979
1980 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001981 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001982 /* First time encountering incomplete message or after receiving
1983 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001984 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001985 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001986 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001987 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001988 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001989#ifdef WIN32
1990 chanpart->ch_deadline = GetTickCount() + 100L;
1991#else
1992 gettimeofday(&chanpart->ch_deadline, NULL);
1993 chanpart->ch_deadline.tv_usec += 100 * 1000;
1994 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1995 {
1996 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1997 ++chanpart->ch_deadline.tv_sec;
1998 }
1999#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002000 }
2001 else
2002 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002003 int timeout;
2004#ifdef WIN32
2005 timeout = GetTickCount() > chanpart->ch_deadline;
2006#else
2007 {
2008 struct timeval now_tv;
2009
2010 gettimeofday(&now_tv, NULL);
2011 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2012 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2013 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2014 }
2015#endif
2016 if (timeout)
2017 {
2018 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002019 chanpart->ch_wait_len = 0;
2020 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002021 }
2022 else
2023 {
2024 reader.js_used = 0;
2025 ch_log(channel, "still waiting on incomplete message");
2026 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002027 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002028 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002029
2030 if (status == FAIL)
2031 {
2032 ch_error(channel, "Decoding failed - discarding input");
2033 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002034 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002035 }
2036 else if (reader.js_buf[reader.js_used] != NUL)
2037 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002038 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002039 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002040 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2041 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002042 ret = status == MAYBE ? FALSE: TRUE;
2043 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002044 else
2045 ret = FALSE;
2046
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002047 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002048 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002049}
2050
2051/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002052 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002053 */
2054 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002055remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002056{
Bram Moolenaar77073442016-02-13 23:23:53 +01002057 if (node->cq_prev == NULL)
2058 head->cq_next = node->cq_next;
2059 else
2060 node->cq_prev->cq_next = node->cq_next;
2061 if (node->cq_next == NULL)
2062 head->cq_prev = node->cq_prev;
2063 else
2064 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002065}
2066
2067/*
2068 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002069 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002070 */
2071 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002072remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002073{
Bram Moolenaar77073442016-02-13 23:23:53 +01002074 if (node->jq_prev == NULL)
2075 head->jq_next = node->jq_next;
2076 else
2077 node->jq_prev->jq_next = node->jq_next;
2078 if (node->jq_next == NULL)
2079 head->jq_prev = node->jq_prev;
2080 else
2081 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002082 vim_free(node);
2083}
2084
2085/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002086 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002087 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002088 * When "id" is zero or negative jut get the first message. But not the one
2089 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002090 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002091 * Return OK when found and return the value in "rettv".
2092 * Return FAIL otherwise.
2093 */
2094 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002095channel_get_json(
2096 channel_T *channel,
2097 ch_part_T part,
2098 int id,
2099 int without_callback,
2100 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002101{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002102 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002103 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002104
Bram Moolenaar77073442016-02-13 23:23:53 +01002105 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002106 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002107 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002108 typval_T *tv = &l->lv_first->li_tv;
2109
Bram Moolenaar958dc692016-12-01 15:34:12 +01002110 if ((without_callback || !item->jq_no_callback)
2111 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002112 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002113 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002114 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002115 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002116 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002117 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002118 ch_log(channel, "Getting JSON message %ld",
2119 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002120 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002121 return OK;
2122 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002123 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002124 }
2125 return FAIL;
2126}
2127
Bram Moolenaar958dc692016-12-01 15:34:12 +01002128/*
2129 * Put back "rettv" into the JSON queue, there was no callback for it.
2130 * Takes over the values in "rettv".
2131 */
2132 static void
2133channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2134{
2135 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2136 jsonq_T *item = head->jq_next;
2137 jsonq_T *newitem;
2138
2139 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2140 /* last item was pushed back, append to the end */
2141 item = NULL;
2142 else while (item != NULL && item->jq_no_callback)
2143 /* append after the last item that was pushed back */
2144 item = item->jq_next;
2145
2146 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2147 if (newitem == NULL)
2148 clear_tv(rettv);
2149 else
2150 {
2151 newitem->jq_value = alloc_tv();
2152 if (newitem->jq_value == NULL)
2153 {
2154 vim_free(newitem);
2155 clear_tv(rettv);
2156 }
2157 else
2158 {
2159 newitem->jq_no_callback = FALSE;
2160 *newitem->jq_value = *rettv;
2161 if (item == NULL)
2162 {
2163 /* append to the end */
2164 newitem->jq_prev = head->jq_prev;
2165 head->jq_prev = newitem;
2166 newitem->jq_next = NULL;
2167 if (newitem->jq_prev == NULL)
2168 head->jq_next = newitem;
2169 else
2170 newitem->jq_prev->jq_next = newitem;
2171 }
2172 else
2173 {
2174 /* append after "item" */
2175 newitem->jq_prev = item;
2176 newitem->jq_next = item->jq_next;
2177 item->jq_next = newitem;
2178 if (newitem->jq_next == NULL)
2179 head->jq_prev = newitem;
2180 else
2181 newitem->jq_next->jq_prev = newitem;
2182 }
2183 }
2184 }
2185}
2186
Bram Moolenaarece61b02016-02-20 21:39:05 +01002187#define CH_JSON_MAX_ARGS 4
2188
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002189/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002190 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002191 * "argv[0]" is the command string.
2192 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002193 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002195channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002196{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002197 char_u *cmd = argv[0].vval.v_string;
2198 char_u *arg;
2199 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002200
Bram Moolenaarece61b02016-02-20 21:39:05 +01002201 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002202 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002203 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002204 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002205 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002206 return;
2207 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002208 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002209 if (arg == NULL)
2210 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002212 if (STRCMP(cmd, "ex") == 0)
2213 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002214 int save_called_emsg = called_emsg;
2215
2216 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002217 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002218 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002219 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002220 --emsg_silent;
2221 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002222 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002223 (char *)get_vim_var_str(VV_ERRMSG));
2224 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002225 }
2226 else if (STRCMP(cmd, "normal") == 0)
2227 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002228 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002229
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002230 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002231 ea.arg = arg;
2232 ea.addr_count = 0;
2233 ea.forceit = TRUE; /* no mapping */
2234 ex_normal(&ea);
2235 }
2236 else if (STRCMP(cmd, "redraw") == 0)
2237 {
2238 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002239
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002240 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002241 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002242 ex_redraw(&ea);
2243 showruler(FALSE);
2244 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002245 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002247 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002248 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002249 int is_call = cmd[0] == 'c';
2250 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002251
Bram Moolenaarece61b02016-02-20 21:39:05 +01002252 if (argv[id_idx].v_type != VAR_UNKNOWN
2253 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002254 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002256 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002257 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002258 }
2259 else if (is_call && argv[2].v_type != VAR_LIST)
2260 {
2261 ch_error(channel, "third argument for call must be a list");
2262 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002263 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002264 }
2265 else
2266 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002267 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002268 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002269 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002270 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002271
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002272 /* Don't pollute the display with errors. */
2273 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002274 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002275 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002276 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002278 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002279 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002280 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002281 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002282 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2283 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002284 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002285
2286 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002287 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002288 int id = argv[id_idx].vval.v_number;
2289
Bram Moolenaar55fab432016-02-07 16:53:13 +01002290 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002291 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002292 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002293 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002294 /* If evaluation failed or the result can't be encoded
2295 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002296 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002297 err_tv.v_type = VAR_STRING;
2298 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002299 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002300 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002301 if (json != NULL)
2302 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002303 channel_send(channel,
2304 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002305 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002306 vim_free(json);
2307 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002308 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002309 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002310 if (tv == &res_tv)
2311 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002312 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002313 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002314 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002315 }
2316 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002317 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002318 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002319 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002320 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002321}
2322
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002323/*
2324 * Invoke the callback at "cbhead".
2325 * Does not redraw but sets channel_need_redraw.
2326 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002327 static void
2328invoke_one_time_callback(
2329 channel_T *channel,
2330 cbq_T *cbhead,
2331 cbq_T *item,
2332 typval_T *argv)
2333{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002334 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002335 (char *)item->cq_callback);
2336 /* Remove the item from the list first, if the callback
2337 * invokes ch_close() the list will be cleared. */
2338 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002339 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002340 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002341 vim_free(item);
2342}
2343
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002344 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002345append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002346{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002347 bufref_T save_curbuf = {NULL, 0, 0};
2348 win_T *save_curwin = NULL;
2349 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002350 linenr_T lnum = buffer->b_ml.ml_line_count;
2351 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002352 chanpart_T *ch_part = &channel->ch_part[part];
2353 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002354 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002355
2356 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2357 {
2358 if (!ch_part->ch_nomod_error)
2359 {
2360 ch_error(channel, "Buffer is not modifiable, cannot append");
2361 ch_part->ch_nomod_error = TRUE;
2362 }
2363 return;
2364 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002365
2366 /* If the buffer is also used as input insert above the last
2367 * line. Don't write these lines. */
2368 if (save_write_to)
2369 {
2370 --lnum;
2371 buffer->b_write_to_channel = FALSE;
2372 }
2373
2374 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002375 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002376
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002377 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002378
2379 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2380 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2381
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002382 u_sync(TRUE);
2383 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002384 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002385
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002386 if (empty)
2387 {
2388 /* The buffer is empty, replace the first (dummy) line. */
2389 ml_replace(lnum, msg, TRUE);
2390 lnum = 0;
2391 }
2392 else
2393 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002394 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002395
2396 /* Restore curbuf/curwin/curtab */
2397 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2398
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002399 if (ch_part->ch_nomodifiable)
2400 buffer->b_p_ma = FALSE;
2401 else
2402 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002403
2404 if (buffer->b_nwindows > 0)
2405 {
2406 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002407
2408 FOR_ALL_WINDOWS(wp)
2409 {
2410 if (wp->w_buffer == buffer
2411 && (save_write_to
2412 ? wp->w_cursor.lnum == lnum + 1
2413 : (wp->w_cursor.lnum == lnum
2414 && wp->w_cursor.col == 0)))
2415 {
2416 ++wp->w_cursor.lnum;
2417 save_curwin = curwin;
2418 curwin = wp;
2419 curbuf = curwin->w_buffer;
2420 scroll_cursor_bot(0, FALSE);
2421 curwin = save_curwin;
2422 curbuf = curwin->w_buffer;
2423 }
2424 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002425 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002426 channel_need_redraw = TRUE;
2427 }
2428
2429 if (save_write_to)
2430 {
2431 channel_T *ch;
2432
2433 /* Find channels reading from this buffer and adjust their
2434 * next-to-read line number. */
2435 buffer->b_write_to_channel = TRUE;
2436 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2437 {
2438 chanpart_T *in_part = &ch->ch_part[PART_IN];
2439
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002440 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002441 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2442 }
2443 }
2444}
2445
Bram Moolenaar437905c2016-04-26 19:01:05 +02002446 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002447drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002448{
2449 char_u *msg;
2450
2451 while ((msg = channel_get(channel, part)) != NULL)
2452 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002453 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002454 vim_free(msg);
2455 }
2456}
2457
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002458/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002459 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002460 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002461 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002462 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002463 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002464may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002465{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002466 char_u *msg = NULL;
2467 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002468 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002469 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002470 chanpart_T *ch_part = &channel->ch_part[part];
2471 ch_mode_T ch_mode = ch_part->ch_mode;
2472 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002473 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002474 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002475 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002476 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002477 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002478
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002479 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002480 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002481 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002482
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002483 /* Use a message-specific callback, part callback or channel callback */
2484 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2485 if (cbitem->cq_seq_nr == 0)
2486 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002487 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002488 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002489 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002490 partial = cbitem->cq_partial;
2491 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002492 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002493 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002494 callback = ch_part->ch_callback;
2495 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002496 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002497 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002498 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002499 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002500 partial = channel->ch_partial;
2501 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002502
Bram Moolenaarec68a992016-10-03 21:37:41 +02002503 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002504 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2505 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002506 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002507 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002508 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002509 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002510 buffer = NULL;
2511 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002512
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002513 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002514 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002515 listitem_T *item;
2516 int argc = 0;
2517
Bram Moolenaard7ece102016-02-02 23:23:02 +01002518 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002519 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002520 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002521 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002522 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002523 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002524 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002525 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002526
Bram Moolenaarece61b02016-02-20 21:39:05 +01002527 for (item = listtv->vval.v_list->lv_first;
2528 item != NULL && argc < CH_JSON_MAX_ARGS;
2529 item = item->li_next)
2530 argv[argc++] = item->li_tv;
2531 while (argc < CH_JSON_MAX_ARGS)
2532 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002533
Bram Moolenaarece61b02016-02-20 21:39:05 +01002534 if (argv[0].v_type == VAR_STRING)
2535 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002536 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002537 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002538 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002539 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002540 }
2541
Bram Moolenaarece61b02016-02-20 21:39:05 +01002542 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002543 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002544 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002545 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002546 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002547 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002548 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002549 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002550 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002551 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002552 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002553 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002554 return FALSE;
2555 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002556 else
2557 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002558 /* If there is no callback or buffer drop the message. */
2559 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002560 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002561 /* If there is a close callback it may use ch_read() to get the
2562 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002563 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002564 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002565 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002566 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002567
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002568 if (ch_mode == MODE_NL)
2569 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002570 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002571 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002572 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002573
2574 /* See if we have a message ending in NL in the first buffer. If
2575 * not try to concatenate the first and the second buffer. */
2576 while (TRUE)
2577 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002578 node = channel_peek(channel, part);
2579 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002580 if (nl != NULL)
2581 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002582 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002583 {
2584 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2585 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002586 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002587 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002588 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002589 buf = node->rq_buffer;
2590
Bram Moolenaarec68a992016-10-03 21:37:41 +02002591 if (nl == NULL)
2592 {
2593 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002594 char_u *new_buf;
2595
2596 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2597 if (new_buf == NULL)
2598 /* This might fail over and over again, should the message
2599 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002600 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002601 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002602 node->rq_buffer = buf;
2603 nl = buf + node->rq_buflen++;
2604 *nl = NUL;
2605 }
2606
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002607 /* Convert NUL to NL, the internal representation. */
2608 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2609 if (*p == NUL)
2610 *p = NL;
2611
2612 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002613 {
2614 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002615 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002616 *nl = NUL;
2617 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002618 else
2619 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002620 /* Copy the message into allocated memory (excluding the NL)
2621 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002622 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002623 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002624 }
2625 }
2626 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002627 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002628 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002629 * get everything we have.
2630 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002631 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002632 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002633
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002634 if (msg == NULL)
2635 return FALSE; /* out of memory (and avoids Coverity warning) */
2636
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002637 argv[1].v_type = VAR_STRING;
2638 argv[1].vval.v_string = msg;
2639 }
2640
Bram Moolenaara07fec92016-02-05 21:04:08 +01002641 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002642 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002643 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002644
Bram Moolenaar958dc692016-12-01 15:34:12 +01002645 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002646 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002647 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002648 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002649 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002650 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002651 break;
2652 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002653 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002654 {
2655 if (channel->ch_drop_never)
2656 {
2657 /* message must be read with ch_read() */
2658 channel_push_json(channel, part, listtv);
2659 listtv = NULL;
2660 }
2661 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002662 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002663 seq_nr);
2664 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002665 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002666 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002667 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002668 if (buffer != NULL)
2669 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002670 if (msg == NULL)
2671 /* JSON or JS mode: re-encode the message. */
2672 msg = json_encode(listtv, ch_mode);
2673 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002674 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002675#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002676 if (buffer->b_term != NULL)
2677 write_to_term(buffer, msg, channel);
2678 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002679#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002680 append_to_buffer(buffer, msg, channel, part);
2681 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002682 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002683
Bram Moolenaar187db502016-02-27 14:44:26 +01002684 if (callback != NULL)
2685 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002686 if (cbitem != NULL)
2687 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2688 else
2689 {
2690 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002691 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002692 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002693 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002694 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002695 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002696 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002697 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002698 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002699
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002700 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002701 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002702 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002703
2704 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002705}
2706
2707/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002708 * Return TRUE when channel "channel" is open for writing to.
2709 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002710 */
2711 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002712channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002713{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002714 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002715 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002716}
2717
2718/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002719 * Return TRUE when channel "channel" is open for reading or writing.
2720 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002721 */
2722 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002723channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002724{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002725 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002726 || channel->CH_IN_FD != INVALID_FD
2727 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002728 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002729}
2730
2731/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002732 * Return TRUE if "channel" has JSON or other typeahead.
2733 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002734 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002735channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002736{
2737 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2738
2739 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2740 {
2741 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2742 jsonq_T *item = head->jq_next;
2743
2744 return item != NULL;
2745 }
2746 return channel_peek(channel, part) != NULL;
2747}
2748
2749/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002750 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002751 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002752 */
2753 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002754channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002755{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002756 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002757 int has_readahead = FALSE;
2758
Bram Moolenaar77073442016-02-13 23:23:53 +01002759 if (channel == NULL)
2760 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002761 if (req_part == PART_OUT)
2762 {
2763 if (channel->CH_OUT_FD != INVALID_FD)
2764 return "open";
2765 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002766 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002767 }
2768 else if (req_part == PART_ERR)
2769 {
2770 if (channel->CH_ERR_FD != INVALID_FD)
2771 return "open";
2772 if (channel_has_readahead(channel, PART_ERR))
2773 has_readahead = TRUE;
2774 }
2775 else
2776 {
2777 if (channel_is_open(channel))
2778 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002779 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002780 if (channel_has_readahead(channel, part))
2781 {
2782 has_readahead = TRUE;
2783 break;
2784 }
2785 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002786
2787 if (has_readahead)
2788 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002789 return "closed";
2790}
2791
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002792 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002793channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002794{
2795 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002796 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002797 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002798 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002799 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002800
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002801 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002802 STRCAT(namebuf, "_");
2803 tail = STRLEN(namebuf);
2804
2805 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002806 if (chanpart->ch_fd != INVALID_FD)
2807 status = "open";
2808 else if (channel_has_readahead(channel, part))
2809 status = "buffered";
2810 else
2811 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002812 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002813
2814 STRCPY(namebuf + tail, "mode");
2815 switch (chanpart->ch_mode)
2816 {
2817 case MODE_NL: s = "NL"; break;
2818 case MODE_RAW: s = "RAW"; break;
2819 case MODE_JSON: s = "JSON"; break;
2820 case MODE_JS: s = "JS"; break;
2821 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002822 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002823
2824 STRCPY(namebuf + tail, "io");
2825 if (part == PART_SOCK)
2826 s = "socket";
2827 else switch (chanpart->ch_io)
2828 {
2829 case JIO_NULL: s = "null"; break;
2830 case JIO_PIPE: s = "pipe"; break;
2831 case JIO_FILE: s = "file"; break;
2832 case JIO_BUFFER: s = "buffer"; break;
2833 case JIO_OUT: s = "out"; break;
2834 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002835 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002836
2837 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002838 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002839}
2840
2841 void
2842channel_info(channel_T *channel, dict_T *dict)
2843{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002844 dict_add_number(dict, "id", channel->ch_id);
2845 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002846
2847 if (channel->ch_hostname != NULL)
2848 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002849 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2850 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002851 channel_part_info(channel, dict, "sock", PART_SOCK);
2852 }
2853 else
2854 {
2855 channel_part_info(channel, dict, "out", PART_OUT);
2856 channel_part_info(channel, dict, "err", PART_ERR);
2857 channel_part_info(channel, dict, "in", PART_IN);
2858 }
2859}
2860
Bram Moolenaar77073442016-02-13 23:23:53 +01002861/*
2862 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002863 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002864 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002865 */
2866 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002867channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002868{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002869 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002870
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002871#ifdef FEAT_GUI
2872 channel_gui_unregister(channel);
2873#endif
2874
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002875 ch_close_part(channel, PART_SOCK);
2876 ch_close_part(channel, PART_IN);
2877 ch_close_part(channel, PART_OUT);
2878 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002879
Bram Moolenaara9f02812017-08-05 17:40:38 +02002880 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002881 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002882 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002883
Bram Moolenaara9f02812017-08-05 17:40:38 +02002884 /* Invoke callbacks and flush buffers before the close callback. */
2885 if (channel->ch_close_cb != NULL)
2886 ch_log(channel,
2887 "Invoking callbacks and flushing buffers before closing");
2888 for (part = PART_SOCK; part < PART_IN; ++part)
2889 {
2890 if (channel->ch_close_cb != NULL
2891 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2892 {
2893 /* Increment the refcount to avoid the channel being freed
2894 * halfway. */
2895 ++channel->ch_refcount;
2896 if (channel->ch_close_cb == NULL)
2897 ch_log(channel, "flushing %s buffers before closing",
2898 part_names[part]);
2899 while (may_invoke_callback(channel, part))
2900 ;
2901 --channel->ch_refcount;
2902 }
2903 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002904
Bram Moolenaara9f02812017-08-05 17:40:38 +02002905 if (channel->ch_close_cb != NULL)
2906 {
2907 typval_T argv[1];
2908 typval_T rettv;
2909 int dummy;
2910
2911 /* Increment the refcount to avoid the channel being freed
2912 * halfway. */
2913 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002914 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002915 (char *)channel->ch_close_cb);
2916 argv[0].v_type = VAR_CHANNEL;
2917 argv[0].vval.v_channel = channel;
2918 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002919 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002920 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002921 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002922 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002923
Bram Moolenaara9f02812017-08-05 17:40:38 +02002924 /* the callback is only called once */
2925 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2926 channel->ch_close_cb = NULL;
2927 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002928
Bram Moolenaara9f02812017-08-05 17:40:38 +02002929 if (channel_need_redraw)
2930 {
2931 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002932 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002933 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002934
Bram Moolenaara9f02812017-08-05 17:40:38 +02002935 if (!channel->ch_drop_never)
2936 /* any remaining messages are useless now */
2937 for (part = PART_SOCK; part < PART_IN; ++part)
2938 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002939
2940 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002941 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002942 }
2943
2944 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002945
2946#ifdef FEAT_TERMINAL
2947 term_channel_closed(channel);
2948#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002949}
2950
Bram Moolenaard04a0202016-01-26 23:30:18 +01002951/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002952 * Close the "in" part channel "channel".
2953 */
2954 void
2955channel_close_in(channel_T *channel)
2956{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002957 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002958}
2959
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002960 static void
2961remove_from_writeque(writeq_T *wq, writeq_T *entry)
2962{
2963 ga_clear(&entry->wq_ga);
2964 wq->wq_next = entry->wq_next;
2965 if (wq->wq_next == NULL)
2966 wq->wq_prev = NULL;
2967 else
2968 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002969 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002970}
2971
Bram Moolenaar0874a832016-09-01 15:11:51 +02002972/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002973 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002974 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002975 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002976channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002977{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002978 chanpart_T *ch_part = &channel->ch_part[part];
2979 jsonq_T *json_head = &ch_part->ch_json_head;
2980 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002981
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002982 while (channel_peek(channel, part) != NULL)
2983 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002984
2985 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002986 {
2987 cbq_T *node = cb_head->cq_next;
2988
2989 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002990 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002991 vim_free(node);
2992 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002993
2994 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002995 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002996 free_tv(json_head->jq_next->jq_value);
2997 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002998 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002999
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003000 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3001 ch_part->ch_callback = NULL;
3002 ch_part->ch_partial = NULL;
3003
3004 while (ch_part->ch_writeque.wq_next != NULL)
3005 remove_from_writeque(&ch_part->ch_writeque,
3006 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003007}
3008
3009/*
3010 * Clear all the read buffers on "channel".
3011 */
3012 void
3013channel_clear(channel_T *channel)
3014{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003015 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003016 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003017 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003018 channel_clear_one(channel, PART_OUT);
3019 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003020 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003021 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003022 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003023 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003024 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003025 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003026 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003027}
3028
Bram Moolenaar77073442016-02-13 23:23:53 +01003029#if defined(EXITFREE) || defined(PROTO)
3030 void
3031channel_free_all(void)
3032{
3033 channel_T *channel;
3034
Bram Moolenaard6051b52016-02-28 15:49:03 +01003035 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003036 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3037 channel_clear(channel);
3038}
3039#endif
3040
3041
Bram Moolenaar715d2852016-04-30 17:06:31 +02003042/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003043#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044
3045/* Buffer size for reading incoming messages. */
3046#define MAXMSGSIZE 4096
3047
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003048#if defined(HAVE_SELECT)
3049/*
3050 * Add write fds where we are waiting for writing to be possible.
3051 */
3052 static int
3053channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3054{
3055 int maxfd = maxfd_arg;
3056 channel_T *ch;
3057
3058 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3059 {
3060 chanpart_T *in_part = &ch->ch_part[PART_IN];
3061
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003062 if (in_part->ch_fd != INVALID_FD
3063 && (in_part->ch_bufref.br_buf != NULL
3064 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003065 {
3066 FD_SET((int)in_part->ch_fd, wfds);
3067 if ((int)in_part->ch_fd >= maxfd)
3068 maxfd = (int)in_part->ch_fd + 1;
3069 }
3070 }
3071 return maxfd;
3072}
3073#else
3074/*
3075 * Add write fds where we are waiting for writing to be possible.
3076 */
3077 static int
3078channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3079{
3080 int nfd = nfd_in;
3081 channel_T *ch;
3082
3083 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3084 {
3085 chanpart_T *in_part = &ch->ch_part[PART_IN];
3086
Bram Moolenaar683b7962017-08-19 15:51:59 +02003087 if (in_part->ch_fd != INVALID_FD
3088 && (in_part->ch_bufref.br_buf != NULL
3089 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003090 {
3091 in_part->ch_poll_idx = nfd;
3092 fds[nfd].fd = in_part->ch_fd;
3093 fds[nfd].events = POLLOUT;
3094 ++nfd;
3095 }
3096 else
3097 in_part->ch_poll_idx = -1;
3098 }
3099 return nfd;
3100}
3101#endif
3102
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003103typedef enum {
3104 CW_READY,
3105 CW_NOT_READY,
3106 CW_ERROR
3107} channel_wait_result;
3108
Bram Moolenaard04a0202016-01-26 23:30:18 +01003109/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003110 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003111 * Return CW_READY when there is something to read.
3112 * Return CW_NOT_READY when there is nothing to read.
3113 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003114 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003115 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003116channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003117{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003118 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003119 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003120
Bram Moolenaard8070362016-02-15 21:56:54 +01003121# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003122 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003123 {
3124 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003125 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003126 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003127 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003128
3129 /* reading from a pipe, not a socket */
3130 while (TRUE)
3131 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003132 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3133
3134 if (r && nread > 0)
3135 return CW_READY;
3136 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003137 {
3138 DWORD err = GetLastError();
3139
3140 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3141 return CW_ERROR;
3142
3143 if (channel->ch_named_pipe)
3144 {
3145 DisconnectNamedPipe((HANDLE)fd);
3146 ConnectNamedPipe((HANDLE)fd, NULL);
3147 }
3148 else
3149 return CW_ERROR;
3150 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003151
3152 /* perhaps write some buffer lines */
3153 channel_write_any_lines();
3154
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003155 sleep_time = deadline - GetTickCount();
3156 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003157 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003158 /* Wait for a little while. Very short at first, up to 10 msec
3159 * after looping a few times. */
3160 if (sleep_time > delay)
3161 sleep_time = delay;
3162 Sleep(sleep_time);
3163 delay = delay * 2;
3164 if (delay > 10)
3165 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003166 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003167 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003168 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003169#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003170 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003171#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003172 struct timeval tval;
3173 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003174 fd_set wfds;
3175 int ret;
3176 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003177
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003178 tval.tv_sec = timeout / 1000;
3179 tval.tv_usec = (timeout % 1000) * 1000;
3180 for (;;)
3181 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003182 FD_ZERO(&rfds);
3183 FD_SET((int)fd, &rfds);
3184
3185 /* Write lines to a pipe when a pipe can be written to. Need to
3186 * set this every time, some buffers may be done. */
3187 maxfd = (int)fd + 1;
3188 FD_ZERO(&wfds);
3189 maxfd = channel_fill_wfds(maxfd, &wfds);
3190
3191 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003192# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003193 SOCK_ERRNO;
3194 if (ret == -1 && errno == EINTR)
3195 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003196# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003197 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003198 {
3199 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003200 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003201 channel_write_any_lines();
3202 continue;
3203 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003204 break;
3205 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003206#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003207 for (;;)
3208 {
3209 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3210 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003211
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003212 fds[0].fd = fd;
3213 fds[0].events = POLLIN;
3214 nfd = channel_fill_poll_write(nfd, fds);
3215 if (poll(fds, nfd, timeout) > 0)
3216 {
3217 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003218 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003219 channel_write_any_lines();
3220 continue;
3221 }
3222 break;
3223 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003224#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003225 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003226 return CW_NOT_READY;
3227}
3228
3229 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003230ch_close_part_on_error(
3231 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003232{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003233 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003234
3235 if (is_err)
3236 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003237 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003238 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003239 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003240
3241 /* Queue a "DETACH" netbeans message in the command queue in order to
3242 * terminate the netbeans session later. Do not end the session here
3243 * directly as we may be running in the context of a call to
3244 * netbeans_parse_messages():
3245 * netbeans_parse_messages
3246 * -> autocmd triggered while processing the netbeans cmd
3247 * -> ui_breakcheck
3248 * -> gui event loop or select loop
3249 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003250 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003251 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003252 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003253 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003254 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3255
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003256 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003257 * close the channel yet, there may be something to read on another part.
3258 * When stdout and stderr use the same FD we get the error only on one of
3259 * them, also close the other. */
3260 if (part == PART_OUT || part == PART_ERR)
3261 {
3262 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3263
3264 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3265 ch_close_part(channel, other);
3266 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003267 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003268
3269#ifdef FEAT_GUI
3270 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003271 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003272#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003273}
3274
3275 static void
3276channel_close_now(channel_T *channel)
3277{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003278 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003279 if (channel->ch_nb_close_cb != NULL)
3280 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003281 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003282}
3283
3284/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003285 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003286 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003287 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003288 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003289 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003290channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003291{
3292 static char_u *buf = NULL;
3293 int len = 0;
3294 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003295 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003296 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003297
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003298 fd = channel->ch_part[part].ch_fd;
3299 if (fd == INVALID_FD)
3300 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003301 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003302 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003303 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003304 }
3305 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003306
3307 /* Allocate a buffer to read into. */
3308 if (buf == NULL)
3309 {
3310 buf = alloc(MAXMSGSIZE);
3311 if (buf == NULL)
3312 return; /* out of memory! */
3313 }
3314
3315 /* Keep on reading for as long as there is something to read.
3316 * Use select() or poll() to avoid blocking on a message that is exactly
3317 * MAXMSGSIZE long. */
3318 for (;;)
3319 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003320 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003321 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003322 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003323 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003324 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003325 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003326 if (len <= 0)
3327 break; /* error or nothing more to read */
3328
3329 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003330 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003331 readlen += len;
3332 if (len < MAXMSGSIZE)
3333 break; /* did read everything that's available */
3334 }
3335
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003336 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003337 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003338 {
3339 if (!channel->ch_keep_open)
3340 ch_close_part_on_error(channel, part, (len < 0), func);
3341 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003342#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003343 else if (CH_HAS_GUI && gtk_main_level() > 0)
3344 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003345 gtk_main_quit();
3346#endif
3347}
3348
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003349/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003350 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003351 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003352 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003353 * Returns what was read in allocated memory.
3354 * Returns NULL in case of error or timeout.
3355 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003356 static char_u *
3357channel_read_block(channel_T *channel, ch_part_T part, int timeout, int raw)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003358{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003359 char_u *buf;
3360 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003361 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003362 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003363 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003364 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003365
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003366 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003367 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003368
3369 while (TRUE)
3370 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003371 node = channel_peek(channel, part);
3372 if (node != NULL)
3373 {
3374 if (mode == MODE_RAW || (mode == MODE_NL
3375 && channel_first_nl(node) != NULL))
3376 /* got a complete message */
3377 break;
3378 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3379 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003380 /* If not blocking or nothing more is coming then return what we
3381 * have. */
3382 if (raw || fd == INVALID_FD)
3383 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003384 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003385
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003386 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003387 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003388 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003389 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003390 {
3391 ch_log(channel, "Timed out");
3392 return NULL;
3393 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003394 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003395 }
3396
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003397 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003398 if (mode == MODE_RAW)
3399 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003400 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003401 }
3402 else
3403 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003404 char_u *p;
3405
3406 buf = node->rq_buffer;
3407 nl = channel_first_nl(node);
3408
3409 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003410 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003411 if (*p == NUL)
3412 *p = NL;
3413
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003414 if (nl == NULL)
3415 {
3416 /* must be a closed channel with missing NL */
3417 msg = channel_get(channel, part);
3418 }
3419 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003420 {
3421 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003422 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003423 *nl = NUL;
3424 }
3425 else
3426 {
3427 /* Copy the message into allocated memory and remove it from the
3428 * buffer. */
3429 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003430 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003431 }
3432 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003433 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003434 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003435 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003436}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003437
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003438/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003439 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003440 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003441 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003442 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003443 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003444 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003445channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003446 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003447 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003448 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003449 int id,
3450 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003451{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003452 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003453 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003454 int timeout;
3455 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003456
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003457 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003458 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003459 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003460 for (;;)
3461 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003462 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003463
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003464 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003465 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003466 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003467 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003468 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003469 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003470
Bram Moolenaard7ece102016-02-02 23:23:02 +01003471 if (!more)
3472 {
3473 /* Handle any other messages in the queue. If done some more
3474 * messages may have arrived. */
3475 if (channel_parse_messages())
3476 continue;
3477
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003478 /* Wait for up to the timeout. If there was an incomplete message
3479 * use the deadline for that. */
3480 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003481 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003482 {
3483#ifdef WIN32
3484 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3485#else
3486 {
3487 struct timeval now_tv;
3488
3489 gettimeofday(&now_tv, NULL);
3490 timeout = (chanpart->ch_deadline.tv_sec
3491 - now_tv.tv_sec) * 1000
3492 + (chanpart->ch_deadline.tv_usec
3493 - now_tv.tv_usec) / 1000
3494 + 1;
3495 }
3496#endif
3497 if (timeout < 0)
3498 {
3499 /* Something went wrong, channel_parse_json() didn't
3500 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003501 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003502 timeout = timeout_arg;
3503 }
3504 else if (timeout > timeout_arg)
3505 timeout = timeout_arg;
3506 }
3507 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003508 if (fd == INVALID_FD
3509 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003510 {
3511 if (timeout == timeout_arg)
3512 {
3513 if (fd != INVALID_FD)
3514 ch_log(channel, "Timed out");
3515 break;
3516 }
3517 }
3518 else
3519 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003520 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003521 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003522 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003523 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003524}
3525
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003526/*
3527 * Common for ch_read() and ch_readraw().
3528 */
3529 void
3530common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3531{
3532 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003533 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003534 jobopt_T opt;
3535 int mode;
3536 int timeout;
3537 int id = -1;
3538 typval_T *listtv = NULL;
3539
3540 /* return an empty string by default */
3541 rettv->v_type = VAR_STRING;
3542 rettv->vval.v_string = NULL;
3543
3544 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003545 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003546 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003547 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003548
Bram Moolenaar437905c2016-04-26 19:01:05 +02003549 if (opt.jo_set & JO_PART)
3550 part = opt.jo_part;
3551 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003552 if (channel != NULL)
3553 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003554 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003555 part = channel_part_read(channel);
3556 mode = channel_get_mode(channel, part);
3557 timeout = channel_get_timeout(channel, part);
3558 if (opt.jo_set & JO_TIMEOUT)
3559 timeout = opt.jo_timeout;
3560
3561 if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003562 rettv->vval.v_string = channel_read_block(channel, part,
3563 timeout, raw);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003564 else
3565 {
3566 if (opt.jo_set & JO_ID)
3567 id = opt.jo_id;
3568 channel_read_json_block(channel, part, timeout, id, &listtv);
3569 if (listtv != NULL)
3570 {
3571 *rettv = *listtv;
3572 vim_free(listtv);
3573 }
3574 else
3575 {
3576 rettv->v_type = VAR_SPECIAL;
3577 rettv->vval.v_number = VVAL_NONE;
3578 }
3579 }
3580 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003581
3582theend:
3583 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003584}
3585
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003586# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3587 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003588/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003589 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003590 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003591 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003592 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003593channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003594{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003595 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003596 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003597
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003599 for (channel = first_channel; channel != NULL;
3600 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003601 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003602 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003603 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003604 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003605 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003606 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003607 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003608 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003609 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003610}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003611# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003612
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003613# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003614/*
3615 * Check the channels for anything that is ready to be read.
3616 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003617 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003618 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003619 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003620channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003621{
3622 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003623 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003624 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003625
3626 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3627 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003628 if (only_keep_open && !channel->ch_keep_open)
3629 continue;
3630
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003631 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003632 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003633 {
3634 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003635 if (fd != INVALID_FD)
3636 {
3637 int r = channel_wait(channel, fd, 0);
3638
3639 if (r == CW_READY)
3640 channel_read(channel, part, "channel_handle_events");
3641 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003642 ch_close_part_on_error(channel, part, TRUE,
3643 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003644 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003645 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003646 }
3647}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003648# endif
3649
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003650# if defined(FEAT_GUI) || defined(PROTO)
3651/*
3652 * Return TRUE when there is any channel with a keep_open flag.
3653 */
3654 int
3655channel_any_keep_open()
3656{
3657 channel_T *channel;
3658
3659 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3660 if (channel->ch_keep_open)
3661 return TRUE;
3662 return FALSE;
3663}
3664# endif
3665
Bram Moolenaard04a0202016-01-26 23:30:18 +01003666/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003667 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003668 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003669 */
3670 void
3671channel_set_nonblock(channel_T *channel, ch_part_T part)
3672{
3673 chanpart_T *ch_part = &channel->ch_part[part];
3674 int fd = ch_part->ch_fd;
3675
3676 if (fd != INVALID_FD)
3677 {
3678#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003679 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003680
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003681 ioctlsocket(fd, FIONBIO, &val);
3682#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003683 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003684#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003685 ch_part->ch_nonblocking = TRUE;
3686 }
3687}
3688
3689/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003690 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003691 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003692 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003693 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003694 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003695channel_send(
3696 channel_T *channel,
3697 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003698 char_u *buf_arg,
3699 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003700 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003701{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003702 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003703 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003704 chanpart_T *ch_part = &channel->ch_part[part];
3705 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003706
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003707 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003708 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003709 {
3710 if (!channel->ch_error && fun != NULL)
3711 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003712 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003713 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003714 }
3715 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003716 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003717 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003718
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003719 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003720 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003721 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003722 fprintf(log_fd, "'");
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003723 ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003724 fprintf(log_fd, "'\n");
3725 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003726 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003727 }
3728
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003729 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003730 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003731 writeq_T *wq = &ch_part->ch_writeque;
3732 char_u *buf;
3733 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003734
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003735 if (wq->wq_next != NULL)
3736 {
3737 /* first write what was queued */
3738 buf = wq->wq_next->wq_ga.ga_data;
3739 len = wq->wq_next->wq_ga.ga_len;
3740 did_use_queue = TRUE;
3741 }
3742 else
3743 {
3744 if (len_arg == 0)
3745 /* nothing to write, called from channel_select_check() */
3746 return OK;
3747 buf = buf_arg;
3748 len = len_arg;
3749 }
3750
3751 if (part == PART_SOCK)
3752 res = sock_write(fd, (char *)buf, len);
3753 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003754 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003755 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003756#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003757 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003758 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003759 DisconnectNamedPipe((HANDLE)fd);
3760 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003761 }
3762#endif
3763
3764 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003765 if (res < 0 && (errno == EWOULDBLOCK
3766#ifdef EAGAIN
3767 || errno == EAGAIN
3768#endif
3769 ))
3770 res = 0; /* nothing got written */
3771
3772 if (res >= 0 && ch_part->ch_nonblocking)
3773 {
3774 writeq_T *entry = wq->wq_next;
3775
3776 if (did_use_queue)
3777 ch_log(channel, "Sent %d bytes now", res);
3778 if (res == len)
3779 {
3780 /* Wrote all the buf[len] bytes. */
3781 if (entry != NULL)
3782 {
3783 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003784 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003785 continue;
3786 }
3787 if (did_use_queue)
3788 ch_log(channel, "Write queue empty");
3789 }
3790 else
3791 {
3792 /* Wrote only buf[res] bytes, can't write more now. */
3793 if (entry != NULL)
3794 {
3795 if (res > 0)
3796 {
3797 /* Remove the bytes that were written. */
3798 mch_memmove(entry->wq_ga.ga_data,
3799 (char *)entry->wq_ga.ga_data + res,
3800 len - res);
3801 entry->wq_ga.ga_len -= res;
3802 }
3803 buf = buf_arg;
3804 len = len_arg;
3805 }
3806 else
3807 {
3808 buf += res;
3809 len -= res;
3810 }
3811 ch_log(channel, "Adding %d bytes to the write queue", len);
3812
3813 /* Append the not written bytes of the argument to the write
3814 * buffer. Limit entries to 4000 bytes. */
3815 if (wq->wq_prev != NULL
3816 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3817 {
3818 writeq_T *last = wq->wq_prev;
3819
3820 /* append to the last entry */
3821 if (ga_grow(&last->wq_ga, len) == OK)
3822 {
3823 mch_memmove((char *)last->wq_ga.ga_data
3824 + last->wq_ga.ga_len,
3825 buf, len);
3826 last->wq_ga.ga_len += len;
3827 }
3828 }
3829 else
3830 {
3831 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3832
3833 if (last != NULL)
3834 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003835 last->wq_prev = wq->wq_prev;
3836 last->wq_next = NULL;
3837 if (wq->wq_prev == NULL)
3838 wq->wq_next = last;
3839 else
3840 wq->wq_prev->wq_next = last;
3841 wq->wq_prev = last;
3842 ga_init2(&last->wq_ga, 1, 1000);
3843 if (ga_grow(&last->wq_ga, len) == OK)
3844 {
3845 mch_memmove(last->wq_ga.ga_data, buf, len);
3846 last->wq_ga.ga_len = len;
3847 }
3848 }
3849 }
3850 }
3851 }
3852 else if (res != len)
3853 {
3854 if (!channel->ch_error && fun != NULL)
3855 {
3856 ch_error(channel, "%s(): write failed", fun);
3857 EMSG2(_("E631: %s(): write failed"), fun);
3858 }
3859 channel->ch_error = TRUE;
3860 return FAIL;
3861 }
3862
3863 channel->ch_error = FALSE;
3864 return OK;
3865 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003866}
3867
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003868/*
3869 * Common for "ch_sendexpr()" and "ch_sendraw()".
3870 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003871 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003872 * Otherwise returns NULL.
3873 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003874 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003875send_common(
3876 typval_T *argvars,
3877 char_u *text,
3878 int id,
3879 int eval,
3880 jobopt_T *opt,
3881 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003882 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003883{
3884 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003885 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003886
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003887 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003888 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003889 if (channel == NULL)
3890 return NULL;
3891 part_send = channel_part_send(channel);
3892 *part_read = channel_part_read(channel);
3893
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003894 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003895 return NULL;
3896
3897 /* Set the callback. An empty callback means no callback and not reading
3898 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3899 * allowed. */
3900 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3901 {
3902 if (eval)
3903 {
3904 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3905 return NULL;
3906 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003907 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003908 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003909 }
3910
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003911 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003912 && opt->jo_callback == NULL)
3913 return channel;
3914 return NULL;
3915}
3916
3917/*
3918 * common for "ch_evalexpr()" and "ch_sendexpr()"
3919 */
3920 void
3921ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3922{
3923 char_u *text;
3924 typval_T *listtv;
3925 channel_T *channel;
3926 int id;
3927 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003928 ch_part_T part_send;
3929 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003930 jobopt_T opt;
3931 int timeout;
3932
3933 /* return an empty string by default */
3934 rettv->v_type = VAR_STRING;
3935 rettv->vval.v_string = NULL;
3936
Bram Moolenaar437905c2016-04-26 19:01:05 +02003937 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003938 if (channel == NULL)
3939 return;
3940 part_send = channel_part_send(channel);
3941
3942 ch_mode = channel_get_mode(channel, part_send);
3943 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3944 {
3945 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3946 return;
3947 }
3948
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003949 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003950 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003951 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003952 if (text == NULL)
3953 return;
3954
3955 channel = send_common(argvars, text, id, eval, &opt,
3956 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3957 vim_free(text);
3958 if (channel != NULL && eval)
3959 {
3960 if (opt.jo_set & JO_TIMEOUT)
3961 timeout = opt.jo_timeout;
3962 else
3963 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003964 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3965 == OK)
3966 {
3967 list_T *list = listtv->vval.v_list;
3968
3969 /* Move the item from the list and then change the type to
3970 * avoid the value being freed. */
3971 *rettv = list->lv_last->li_tv;
3972 list->lv_last->li_tv.v_type = VAR_NUMBER;
3973 free_tv(listtv);
3974 }
3975 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003976 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003977}
3978
3979/*
3980 * common for "ch_evalraw()" and "ch_sendraw()"
3981 */
3982 void
3983ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3984{
3985 char_u buf[NUMBUFLEN];
3986 char_u *text;
3987 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003988 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003989 jobopt_T opt;
3990 int timeout;
3991
3992 /* return an empty string by default */
3993 rettv->v_type = VAR_STRING;
3994 rettv->vval.v_string = NULL;
3995
3996 text = get_tv_string_buf(&argvars[1], buf);
3997 channel = send_common(argvars, text, 0, eval, &opt,
3998 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3999 if (channel != NULL && eval)
4000 {
4001 if (opt.jo_set & JO_TIMEOUT)
4002 timeout = opt.jo_timeout;
4003 else
4004 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004005 rettv->vval.v_string = channel_read_block(channel, part_read,
4006 timeout, TRUE);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004007 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004008 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004009}
4010
Bram Moolenaarf3360612017-10-01 16:21:31 +02004011# define KEEP_OPEN_TIME 20 /* msec */
4012
Bram Moolenaard04a0202016-01-26 23:30:18 +01004013# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004014/*
4015 * Add open channels to the poll struct.
4016 * Return the adjusted struct index.
4017 * The type of "fds" is hidden to avoid problems with the function proto.
4018 */
4019 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004020channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004021{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004022 int nfd = nfd_in;
4023 channel_T *channel;
4024 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004025 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004026
Bram Moolenaar77073442016-02-13 23:23:53 +01004027 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004028 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004029 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004030 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004031 chanpart_T *ch_part = &channel->ch_part[part];
4032
4033 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004034 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004035 if (channel->ch_keep_open)
4036 {
4037 /* For unknown reason poll() returns immediately for a
4038 * keep-open channel. Instead of adding it to the fds add
4039 * a short timeout and check, like polling. */
4040 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4041 *towait = KEEP_OPEN_TIME;
4042 }
4043 else
4044 {
4045 ch_part->ch_poll_idx = nfd;
4046 fds[nfd].fd = ch_part->ch_fd;
4047 fds[nfd].events = POLLIN;
4048 nfd++;
4049 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004050 }
4051 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004052 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004053 }
4054 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004055
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004056 nfd = channel_fill_poll_write(nfd, fds);
4057
Bram Moolenaare0874f82016-01-24 20:36:41 +01004058 return nfd;
4059}
4060
4061/*
4062 * The type of "fds" is hidden to avoid problems with the function proto.
4063 */
4064 int
4065channel_poll_check(int ret_in, void *fds_in)
4066{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004067 int ret = ret_in;
4068 channel_T *channel;
4069 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004070 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004071 int idx;
4072 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004073
Bram Moolenaar77073442016-02-13 23:23:53 +01004074 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004075 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004076 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004077 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004078 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004079
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004080 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004081 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004082 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004083 --ret;
4084 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004085 else if (channel->ch_part[part].ch_fd != INVALID_FD
4086 && channel->ch_keep_open)
4087 {
4088 /* polling a keep-open channel */
4089 channel_read(channel, part, "channel_poll_check_keep_open");
4090 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004091 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004092
4093 in_part = &channel->ch_part[PART_IN];
4094 idx = in_part->ch_poll_idx;
4095 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4096 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004097 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004098 --ret;
4099 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004100 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004101
4102 return ret;
4103}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004104# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004105
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004106# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004107
Bram Moolenaare0874f82016-01-24 20:36:41 +01004108/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004109 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004110 */
4111 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004112channel_select_setup(
4113 int maxfd_in,
4114 void *rfds_in,
4115 void *wfds_in,
4116 struct timeval *tv,
4117 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004118{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004119 int maxfd = maxfd_in;
4120 channel_T *channel;
4121 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004122 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004123 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004124
Bram Moolenaar77073442016-02-13 23:23:53 +01004125 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004126 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004127 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004128 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004129 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004130
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004131 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004132 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004133 if (channel->ch_keep_open)
4134 {
4135 /* For unknown reason select() returns immediately for a
4136 * keep-open channel. Instead of adding it to the rfds add
4137 * a short timeout and check, like polling. */
4138 if (*tvp == NULL || tv->tv_sec > 0
4139 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4140 {
4141 *tvp = tv;
4142 tv->tv_sec = 0;
4143 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4144 }
4145 }
4146 else
4147 {
4148 FD_SET((int)fd, rfds);
4149 if (maxfd < (int)fd)
4150 maxfd = (int)fd;
4151 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004152 }
4153 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004154 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004155
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004156 maxfd = channel_fill_wfds(maxfd, wfds);
4157
Bram Moolenaare0874f82016-01-24 20:36:41 +01004158 return maxfd;
4159}
4160
4161/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004162 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004163 */
4164 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004165channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004166{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004167 int ret = ret_in;
4168 channel_T *channel;
4169 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004170 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004171 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004172 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004173
Bram Moolenaar77073442016-02-13 23:23:53 +01004174 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004175 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004176 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004177 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004178 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004179
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004180 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004181 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004182 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004183 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004184 --ret;
4185 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004186 else if (fd != INVALID_FD && channel->ch_keep_open)
4187 {
4188 /* polling a keep-open channel */
4189 channel_read(channel, part, "channel_select_check_keep_open");
4190 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004191 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004192
4193 in_part = &channel->ch_part[PART_IN];
4194 if (ret > 0 && in_part->ch_fd != INVALID_FD
4195 && FD_ISSET(in_part->ch_fd, wfds))
4196 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004197 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004198 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004199 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004200 --ret;
4201 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004202 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004203
4204 return ret;
4205}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004206# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004207
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004208/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004209 * Execute queued up commands.
4210 * Invoked from the main loop when it's safe to execute received commands.
4211 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004212 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004213 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004214channel_parse_messages(void)
4215{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004216 channel_T *channel = first_channel;
4217 int ret = FALSE;
4218 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004219 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004220#ifdef ELAPSED_FUNC
4221 ELAPSED_TYPE start_tv;
4222
4223 ELAPSED_INIT(start_tv);
4224#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004225
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004226 ++safe_to_invoke_callback;
4227
Bram Moolenaard0b65022016-03-06 21:50:33 +01004228 /* Only do this message when another message was given, otherwise we get
4229 * lots of them. */
4230 if (did_log_msg)
4231 {
4232 ch_log(NULL, "looking for messages on channels");
4233 did_log_msg = FALSE;
4234 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004235 while (channel != NULL)
4236 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004237 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004238 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004239 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004240 channel_close_now(channel);
4241 /* channel may have been freed, start over */
4242 channel = first_channel;
4243 continue;
4244 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004245 if (channel->ch_to_be_freed)
4246 {
4247 channel_free(channel);
4248 /* channel has been freed, start over */
4249 channel = first_channel;
4250 continue;
4251 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004252 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004253 {
4254 /* channel is no longer useful, free it */
4255 channel_free(channel);
4256 channel = first_channel;
4257 part = PART_SOCK;
4258 continue;
4259 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004260 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004261 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004262 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004263 /* Increase the refcount, in case the handler causes the channel
4264 * to be unreferenced or closed. */
4265 ++channel->ch_refcount;
4266 r = may_invoke_callback(channel, part);
4267 if (r == OK)
4268 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004269 if (channel_unref(channel) || (r == OK
4270#ifdef ELAPSED_FUNC
4271 /* Limit the time we loop here to 100 msec, otherwise
4272 * Vim becomes unresponsive when the callback takes
4273 * more than a bit of time. */
4274 && ELAPSED_FUNC(start_tv) < 100L
4275#endif
4276 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004277 {
4278 /* channel was freed or something was done, start over */
4279 channel = first_channel;
4280 part = PART_SOCK;
4281 continue;
4282 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004283 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004284 if (part < PART_ERR)
4285 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004286 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004287 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004288 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004289 part = PART_SOCK;
4290 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004291 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004292
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004293 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004294 {
4295 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004296 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004297 }
4298
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004299 --safe_to_invoke_callback;
4300
Bram Moolenaard7ece102016-02-02 23:23:02 +01004301 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004302}
4303
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004304/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004305 * Return TRUE if any channel has readahead. That means we should not block on
4306 * waiting for input.
4307 */
4308 int
4309channel_any_readahead(void)
4310{
4311 channel_T *channel = first_channel;
4312 ch_part_T part = PART_SOCK;
4313
4314 while (channel != NULL)
4315 {
4316 if (channel_has_readahead(channel, part))
4317 return TRUE;
4318 if (part < PART_ERR)
4319 ++part;
4320 else
4321 {
4322 channel = channel->ch_next;
4323 part = PART_SOCK;
4324 }
4325 }
4326 return FALSE;
4327}
4328
4329/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004330 * Mark references to lists used in channels.
4331 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004332 int
4333set_ref_in_channel(int copyID)
4334{
Bram Moolenaar77073442016-02-13 23:23:53 +01004335 int abort = FALSE;
4336 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004337 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004338
Bram Moolenaar77073442016-02-13 23:23:53 +01004339 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004340 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004341 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004342 tv.v_type = VAR_CHANNEL;
4343 tv.vval.v_channel = channel;
4344 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004345 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004346 return abort;
4347}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004348
4349/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004350 * Return the "part" to write to for "channel".
4351 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004352 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004353channel_part_send(channel_T *channel)
4354{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004355 if (channel->CH_SOCK_FD == INVALID_FD)
4356 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004357 return PART_SOCK;
4358}
4359
4360/*
4361 * Return the default "part" to read from for "channel".
4362 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004363 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004364channel_part_read(channel_T *channel)
4365{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004366 if (channel->CH_SOCK_FD == INVALID_FD)
4367 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004368 return PART_SOCK;
4369}
4370
4371/*
4372 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004373 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004374 */
4375 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004376channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004377{
Bram Moolenaar77073442016-02-13 23:23:53 +01004378 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004379 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004380 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004381}
4382
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004383/*
4384 * Return the timeout of "channel"/"part"
4385 */
4386 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004387channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004388{
4389 return channel->ch_part[part].ch_timeout;
4390}
4391
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004392 static int
4393handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4394{
4395 char_u *val = get_tv_string(item);
4396
4397 opt->jo_set |= jo;
4398 if (STRCMP(val, "nl") == 0)
4399 *modep = MODE_NL;
4400 else if (STRCMP(val, "raw") == 0)
4401 *modep = MODE_RAW;
4402 else if (STRCMP(val, "js") == 0)
4403 *modep = MODE_JS;
4404 else if (STRCMP(val, "json") == 0)
4405 *modep = MODE_JSON;
4406 else
4407 {
4408 EMSG2(_(e_invarg2), val);
4409 return FAIL;
4410 }
4411 return OK;
4412}
4413
4414 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004415handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004416{
4417 char_u *val = get_tv_string(item);
4418
4419 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4420 if (STRCMP(val, "null") == 0)
4421 opt->jo_io[part] = JIO_NULL;
4422 else if (STRCMP(val, "pipe") == 0)
4423 opt->jo_io[part] = JIO_PIPE;
4424 else if (STRCMP(val, "file") == 0)
4425 opt->jo_io[part] = JIO_FILE;
4426 else if (STRCMP(val, "buffer") == 0)
4427 opt->jo_io[part] = JIO_BUFFER;
4428 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4429 opt->jo_io[part] = JIO_OUT;
4430 else
4431 {
4432 EMSG2(_(e_invarg2), val);
4433 return FAIL;
4434 }
4435 return OK;
4436}
4437
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004438/*
4439 * Clear a jobopt_T before using it.
4440 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004441 void
4442clear_job_options(jobopt_T *opt)
4443{
4444 vim_memset(opt, 0, sizeof(jobopt_T));
4445}
4446
4447/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004448 * Free any members of a jobopt_T.
4449 */
4450 void
4451free_job_options(jobopt_T *opt)
4452{
4453 if (opt->jo_partial != NULL)
4454 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004455 else if (opt->jo_callback != NULL)
4456 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004457 if (opt->jo_out_partial != NULL)
4458 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 else if (opt->jo_out_cb != NULL)
4460 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004461 if (opt->jo_err_partial != NULL)
4462 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004463 else if (opt->jo_err_cb != NULL)
4464 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004465 if (opt->jo_close_partial != NULL)
4466 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004467 else if (opt->jo_close_cb != NULL)
4468 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004469 if (opt->jo_exit_partial != NULL)
4470 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004471 else if (opt->jo_exit_cb != NULL)
4472 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004473 if (opt->jo_env != NULL)
4474 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004475}
4476
4477/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004478 * Get the PART_ number from the first character of an option name.
4479 */
4480 static int
4481part_from_char(int c)
4482{
4483 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4484}
4485
4486/*
4487 * Get the option entries from the dict in "tv", parse them and put the result
4488 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004489 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004490 * If an option value is invalid return FAIL.
4491 */
4492 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004493get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494{
4495 typval_T *item;
4496 char_u *val;
4497 dict_T *dict;
4498 int todo;
4499 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004500 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004501
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004502 if (tv->v_type == VAR_UNKNOWN)
4503 return OK;
4504 if (tv->v_type != VAR_DICT)
4505 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004506 EMSG(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004507 return FAIL;
4508 }
4509 dict = tv->vval.v_dict;
4510 if (dict == NULL)
4511 return OK;
4512
4513 todo = (int)dict->dv_hashtab.ht_used;
4514 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4515 if (!HASHITEM_EMPTY(hi))
4516 {
4517 item = &dict_lookup(hi)->di_tv;
4518
4519 if (STRCMP(hi->hi_key, "mode") == 0)
4520 {
4521 if (!(supported & JO_MODE))
4522 break;
4523 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4524 return FAIL;
4525 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004526 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004527 {
4528 if (!(supported & JO_IN_MODE))
4529 break;
4530 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4531 == FAIL)
4532 return FAIL;
4533 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004534 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004535 {
4536 if (!(supported & JO_OUT_MODE))
4537 break;
4538 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4539 == FAIL)
4540 return FAIL;
4541 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004542 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004543 {
4544 if (!(supported & JO_ERR_MODE))
4545 break;
4546 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4547 == FAIL)
4548 return FAIL;
4549 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004550 else if (STRCMP(hi->hi_key, "in_io") == 0
4551 || STRCMP(hi->hi_key, "out_io") == 0
4552 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004553 {
4554 if (!(supported & JO_OUT_IO))
4555 break;
4556 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4557 return FAIL;
4558 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004559 else if (STRCMP(hi->hi_key, "in_name") == 0
4560 || STRCMP(hi->hi_key, "out_name") == 0
4561 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004562 {
4563 part = part_from_char(*hi->hi_key);
4564
4565 if (!(supported & JO_OUT_IO))
4566 break;
4567 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4568 opt->jo_io_name[part] =
4569 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4570 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004571 else if (STRCMP(hi->hi_key, "pty") == 0)
4572 {
4573 if (!(supported & JO_MODE))
4574 break;
4575 opt->jo_pty = get_tv_number(item);
4576 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004577 else if (STRCMP(hi->hi_key, "in_buf") == 0
4578 || STRCMP(hi->hi_key, "out_buf") == 0
4579 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004580 {
4581 part = part_from_char(*hi->hi_key);
4582
4583 if (!(supported & JO_OUT_IO))
4584 break;
4585 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4586 opt->jo_io_buf[part] = get_tv_number(item);
4587 if (opt->jo_io_buf[part] <= 0)
4588 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004589 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004590 return FAIL;
4591 }
4592 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4593 {
4594 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4595 return FAIL;
4596 }
4597 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004598 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4599 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4600 {
4601 part = part_from_char(*hi->hi_key);
4602
4603 if (!(supported & JO_OUT_IO))
4604 break;
4605 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4606 opt->jo_modifiable[part] = get_tv_number(item);
4607 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004608 else if (STRCMP(hi->hi_key, "out_msg") == 0
4609 || STRCMP(hi->hi_key, "err_msg") == 0)
4610 {
4611 part = part_from_char(*hi->hi_key);
4612
4613 if (!(supported & JO_OUT_IO))
4614 break;
4615 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4616 opt->jo_message[part] = get_tv_number(item);
4617 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004618 else if (STRCMP(hi->hi_key, "in_top") == 0
4619 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004620 {
4621 linenr_T *lp;
4622
4623 if (!(supported & JO_OUT_IO))
4624 break;
4625 if (hi->hi_key[3] == 't')
4626 {
4627 lp = &opt->jo_in_top;
4628 opt->jo_set |= JO_IN_TOP;
4629 }
4630 else
4631 {
4632 lp = &opt->jo_in_bot;
4633 opt->jo_set |= JO_IN_BOT;
4634 }
4635 *lp = get_tv_number(item);
4636 if (*lp < 0)
4637 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004638 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004639 return FAIL;
4640 }
4641 }
4642 else if (STRCMP(hi->hi_key, "channel") == 0)
4643 {
4644 if (!(supported & JO_OUT_IO))
4645 break;
4646 opt->jo_set |= JO_CHANNEL;
4647 if (item->v_type != VAR_CHANNEL)
4648 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004649 EMSG2(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004650 return FAIL;
4651 }
4652 opt->jo_channel = item->vval.v_channel;
4653 }
4654 else if (STRCMP(hi->hi_key, "callback") == 0)
4655 {
4656 if (!(supported & JO_CALLBACK))
4657 break;
4658 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004659 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004660 if (opt->jo_callback == NULL)
4661 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004662 EMSG2(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004663 return FAIL;
4664 }
4665 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004666 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004667 {
4668 if (!(supported & JO_OUT_CALLBACK))
4669 break;
4670 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004671 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004672 if (opt->jo_out_cb == NULL)
4673 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004674 EMSG2(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004675 return FAIL;
4676 }
4677 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004678 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004679 {
4680 if (!(supported & JO_ERR_CALLBACK))
4681 break;
4682 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004683 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004684 if (opt->jo_err_cb == NULL)
4685 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004686 EMSG2(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004687 return FAIL;
4688 }
4689 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004690 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004691 {
4692 if (!(supported & JO_CLOSE_CALLBACK))
4693 break;
4694 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004695 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004696 if (opt->jo_close_cb == NULL)
4697 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004698 EMSG2(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004699 return FAIL;
4700 }
4701 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004702 else if (STRCMP(hi->hi_key, "drop") == 0)
4703 {
4704 int never = FALSE;
4705 val = get_tv_string(item);
4706
4707 if (STRCMP(val, "never") == 0)
4708 never = TRUE;
4709 else if (STRCMP(val, "auto") != 0)
4710 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004711 EMSG3(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004712 return FAIL;
4713 }
4714 opt->jo_drop_never = never;
4715 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004716 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4717 {
4718 if (!(supported & JO_EXIT_CB))
4719 break;
4720 opt->jo_set |= JO_EXIT_CB;
4721 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4722 if (opt->jo_exit_cb == NULL)
4723 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004724 EMSG2(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004725 return FAIL;
4726 }
4727 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004728#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004729 else if (STRCMP(hi->hi_key, "term_name") == 0)
4730 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004731 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004732 break;
4733 opt->jo_set2 |= JO2_TERM_NAME;
4734 opt->jo_term_name = get_tv_string_chk(item);
4735 if (opt->jo_term_name == NULL)
4736 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004737 EMSG2(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004738 return FAIL;
4739 }
4740 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004741 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4742 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004743 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004744 break;
4745 val = get_tv_string(item);
4746 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4747 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004748 EMSG3(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004749 return FAIL;
4750 }
4751 opt->jo_set2 |= JO2_TERM_FINISH;
4752 opt->jo_term_finish = *val;
4753 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004754 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4755 {
4756 char_u *p;
4757
4758 if (!(supported2 & JO2_TERM_OPENCMD))
4759 break;
4760 opt->jo_set2 |= JO2_TERM_OPENCMD;
4761 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4762 if (p != NULL)
4763 {
4764 /* Must have %d and no other %. */
4765 p = vim_strchr(p, '%');
4766 if (p != NULL && (p[1] != 'd'
4767 || vim_strchr(p + 2, '%') != NULL))
4768 p = NULL;
4769 }
4770 if (p == NULL)
4771 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004772 EMSG2(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004773 return FAIL;
4774 }
4775 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004776 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4777 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004778 char_u *p;
4779
4780 if (!(supported2 & JO2_EOF_CHARS))
4781 break;
4782 opt->jo_set2 |= JO2_EOF_CHARS;
4783 p = opt->jo_eof_chars = get_tv_string_chk(item);
4784 if (p == NULL)
4785 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004786 EMSG2(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004787 return FAIL;
4788 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004789 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004790 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4791 {
4792 if (!(supported2 & JO2_TERM_ROWS))
4793 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004794 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004795 opt->jo_term_rows = get_tv_number(item);
4796 }
4797 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4798 {
4799 if (!(supported2 & JO2_TERM_COLS))
4800 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004801 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004802 opt->jo_term_cols = get_tv_number(item);
4803 }
4804 else if (STRCMP(hi->hi_key, "vertical") == 0)
4805 {
4806 if (!(supported2 & JO2_VERTICAL))
4807 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004808 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004809 opt->jo_vertical = get_tv_number(item);
4810 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004811 else if (STRCMP(hi->hi_key, "curwin") == 0)
4812 {
4813 if (!(supported2 & JO2_CURWIN))
4814 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004815 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaarda43b612017-08-11 22:27:50 +02004816 opt->jo_curwin = get_tv_number(item);
4817 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004818 else if (STRCMP(hi->hi_key, "hidden") == 0)
4819 {
4820 if (!(supported2 & JO2_HIDDEN))
4821 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004822 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004823 opt->jo_hidden = get_tv_number(item);
4824 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004825 else if (STRCMP(hi->hi_key, "norestore") == 0)
4826 {
4827 if (!(supported2 & JO2_NORESTORE))
4828 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004829 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004830 opt->jo_term_norestore = get_tv_number(item);
4831 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004832 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4833 {
4834 if (!(supported2 & JO2_TERM_KILL))
4835 break;
4836 opt->jo_set2 |= JO2_TERM_KILL;
4837 opt->jo_term_kill = get_tv_string_chk(item);
4838 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004839# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4840 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4841 {
4842 int n = 0;
4843 listitem_T *li;
4844 long_u rgb[16];
4845
4846 if (!(supported2 & JO2_ANSI_COLORS))
4847 break;
4848
4849 if (item == NULL || item->v_type != VAR_LIST
4850 || item->vval.v_list == NULL)
4851 {
4852 EMSG2(_(e_invargval), "ansi_colors");
4853 return FAIL;
4854 }
4855
4856 li = item->vval.v_list->lv_first;
4857 for (; li != NULL && n < 16; li = li->li_next, n++)
4858 {
4859 char_u *color_name;
4860 guicolor_T guicolor;
4861
4862 color_name = get_tv_string_chk(&li->li_tv);
4863 if (color_name == NULL)
4864 return FAIL;
4865
4866 guicolor = GUI_GET_COLOR(color_name);
4867 if (guicolor == INVALCOLOR)
4868 return FAIL;
4869
4870 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4871 }
4872
4873 if (n != 16 || li != NULL)
4874 {
4875 EMSG2(_(e_invargval), "ansi_colors");
4876 return FAIL;
4877 }
4878
4879 opt->jo_set2 |= JO2_ANSI_COLORS;
4880 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4881 }
4882# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004883#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004884 else if (STRCMP(hi->hi_key, "env") == 0)
4885 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004886 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004887 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004888 if (item->v_type != VAR_DICT)
4889 {
4890 EMSG2(_(e_invargval), "env");
4891 return FAIL;
4892 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004893 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004894 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004895 if (opt->jo_env != NULL)
4896 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004897 }
4898 else if (STRCMP(hi->hi_key, "cwd") == 0)
4899 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004900 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004901 break;
4902 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4903 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4904 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004905 EMSG2(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004906 return FAIL;
4907 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004908 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004909 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004910 else if (STRCMP(hi->hi_key, "waittime") == 0)
4911 {
4912 if (!(supported & JO_WAITTIME))
4913 break;
4914 opt->jo_set |= JO_WAITTIME;
4915 opt->jo_waittime = get_tv_number(item);
4916 }
4917 else if (STRCMP(hi->hi_key, "timeout") == 0)
4918 {
4919 if (!(supported & JO_TIMEOUT))
4920 break;
4921 opt->jo_set |= JO_TIMEOUT;
4922 opt->jo_timeout = get_tv_number(item);
4923 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004924 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004925 {
4926 if (!(supported & JO_OUT_TIMEOUT))
4927 break;
4928 opt->jo_set |= JO_OUT_TIMEOUT;
4929 opt->jo_out_timeout = get_tv_number(item);
4930 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004931 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004932 {
4933 if (!(supported & JO_ERR_TIMEOUT))
4934 break;
4935 opt->jo_set |= JO_ERR_TIMEOUT;
4936 opt->jo_err_timeout = get_tv_number(item);
4937 }
4938 else if (STRCMP(hi->hi_key, "part") == 0)
4939 {
4940 if (!(supported & JO_PART))
4941 break;
4942 opt->jo_set |= JO_PART;
4943 val = get_tv_string(item);
4944 if (STRCMP(val, "err") == 0)
4945 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004946 else if (STRCMP(val, "out") == 0)
4947 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004948 else
4949 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004950 EMSG3(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004951 return FAIL;
4952 }
4953 }
4954 else if (STRCMP(hi->hi_key, "id") == 0)
4955 {
4956 if (!(supported & JO_ID))
4957 break;
4958 opt->jo_set |= JO_ID;
4959 opt->jo_id = get_tv_number(item);
4960 }
4961 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4962 {
4963 if (!(supported & JO_STOPONEXIT))
4964 break;
4965 opt->jo_set |= JO_STOPONEXIT;
4966 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4967 opt->jo_soe_buf);
4968 if (opt->jo_stoponexit == NULL)
4969 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004970 EMSG2(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004971 return FAIL;
4972 }
4973 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004974 else if (STRCMP(hi->hi_key, "block_write") == 0)
4975 {
4976 if (!(supported & JO_BLOCK_WRITE))
4977 break;
4978 opt->jo_set |= JO_BLOCK_WRITE;
4979 opt->jo_block_write = get_tv_number(item);
4980 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004981 else
4982 break;
4983 --todo;
4984 }
4985 if (todo > 0)
4986 {
4987 EMSG2(_(e_invarg2), hi->hi_key);
4988 return FAIL;
4989 }
4990
4991 return OK;
4992}
4993
4994/*
4995 * Get the channel from the argument.
4996 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004997 * When "check_open" is TRUE check that the channel can be used.
4998 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004999 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005000 */
5001 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005002get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005003{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005004 channel_T *channel = NULL;
5005 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005006
5007 if (tv->v_type == VAR_JOB)
5008 {
5009 if (tv->vval.v_job != NULL)
5010 channel = tv->vval.v_job->jv_channel;
5011 }
5012 else if (tv->v_type == VAR_CHANNEL)
5013 {
5014 channel = tv->vval.v_channel;
5015 }
5016 else
5017 {
5018 EMSG2(_(e_invarg2), get_tv_string(tv));
5019 return NULL;
5020 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005021 if (channel != NULL && reading)
5022 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005023 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005024
Bram Moolenaar437905c2016-04-26 19:01:05 +02005025 if (check_open && (channel == NULL || (!channel_is_open(channel)
5026 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005027 {
5028 EMSG(_("E906: not an open channel"));
5029 return NULL;
5030 }
5031 return channel;
5032}
5033
5034static job_T *first_job = NULL;
5035
5036 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005037job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005038{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005039 int i;
5040
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005041 ch_log(job->jv_channel, "Freeing job");
5042 if (job->jv_channel != NULL)
5043 {
5044 /* The link from the channel to the job doesn't count as a reference,
5045 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005046 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005047 * NULL the reference. We don't set ch_job_killed, unreferencing the
5048 * job doesn't mean it stops running. */
5049 job->jv_channel->ch_job = NULL;
5050 channel_unref(job->jv_channel);
5051 }
5052 mch_clear_job(job);
5053
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005054 vim_free(job->jv_tty_in);
5055 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005056 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005057 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005058 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005059 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005060 for (i = 0; job->jv_argv[i] != NULL; i++)
5061 vim_free(job->jv_argv[i]);
5062 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005063 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005064}
5065
5066 static void
5067job_free_job(job_T *job)
5068{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005069 if (job->jv_next != NULL)
5070 job->jv_next->jv_prev = job->jv_prev;
5071 if (job->jv_prev == NULL)
5072 first_job = job->jv_next;
5073 else
5074 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005075 vim_free(job);
5076}
5077
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005078 static void
5079job_free(job_T *job)
5080{
5081 if (!in_free_unref_items)
5082 {
5083 job_free_contents(job);
5084 job_free_job(job);
5085 }
5086}
5087
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005088#if defined(EXITFREE) || defined(PROTO)
5089 void
5090job_free_all(void)
5091{
5092 while (first_job != NULL)
5093 job_free(first_job);
5094}
5095#endif
5096
5097/*
5098 * Return TRUE if we need to check if the process of "job" has ended.
5099 */
5100 static int
5101job_need_end_check(job_T *job)
5102{
5103 return job->jv_status == JOB_STARTED
5104 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5105}
5106
5107/*
5108 * Return TRUE if the channel of "job" is still useful.
5109 */
5110 static int
5111job_channel_still_useful(job_T *job)
5112{
5113 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5114}
5115
5116/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005117 * Return TRUE if the channel of "job" is closeable.
5118 */
5119 static int
5120job_channel_can_close(job_T *job)
5121{
5122 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5123}
5124
5125/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005126 * Return TRUE if the job should not be freed yet. Do not free the job when
5127 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5128 * or when the associated channel will do something with the job output.
5129 */
5130 static int
5131job_still_useful(job_T *job)
5132{
5133 return job_need_end_check(job) || job_channel_still_useful(job);
5134}
5135
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005136#if defined(GUI_MAY_FORK) || defined(PROTO)
5137/*
5138 * Return TRUE when there is any running job that we care about.
5139 */
5140 int
5141job_any_running()
5142{
5143 job_T *job;
5144
5145 for (job = first_job; job != NULL; job = job->jv_next)
5146 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005147 {
5148 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005149 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005150 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005151 return FALSE;
5152}
5153#endif
5154
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005155#if !defined(USE_ARGV) || defined(PROTO)
5156/*
5157 * Escape one argument for an external command.
5158 * Returns the escaped string in allocated memory. NULL when out of memory.
5159 */
5160 static char_u *
5161win32_escape_arg(char_u *arg)
5162{
5163 int slen, dlen;
5164 int escaping = 0;
5165 int i;
5166 char_u *s, *d;
5167 char_u *escaped_arg;
5168 int has_spaces = FALSE;
5169
5170 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005171 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005172 dlen = slen;
5173 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5174 {
5175 if (*s == '"' || *s == '\\')
5176 ++dlen;
5177 if (*s == ' ' || *s == '\t')
5178 has_spaces = TRUE;
5179 }
5180
5181 if (has_spaces)
5182 dlen += 2;
5183
5184 if (dlen == slen)
5185 return vim_strsave(arg);
5186
5187 /* Allocate memory for the result and fill it. */
5188 escaped_arg = alloc(dlen + 1);
5189 if (escaped_arg == NULL)
5190 return NULL;
5191 memset(escaped_arg, 0, dlen+1);
5192
5193 d = escaped_arg;
5194
5195 if (has_spaces)
5196 *d++ = '"';
5197
5198 for (s = arg; *s != NUL;)
5199 {
5200 switch (*s)
5201 {
5202 case '"':
5203 for (i = 0; i < escaping; i++)
5204 *d++ = '\\';
5205 escaping = 0;
5206 *d++ = '\\';
5207 *d++ = *s++;
5208 break;
5209 case '\\':
5210 escaping++;
5211 *d++ = *s++;
5212 break;
5213 default:
5214 escaping = 0;
5215 MB_COPY_CHAR(s, d);
5216 break;
5217 }
5218 }
5219
5220 /* add terminating quote and finish with a NUL */
5221 if (has_spaces)
5222 {
5223 for (i = 0; i < escaping; i++)
5224 *d++ = '\\';
5225 *d++ = '"';
5226 }
5227 *d = NUL;
5228
5229 return escaped_arg;
5230}
5231
5232/*
5233 * Build a command line from a list, taking care of escaping.
5234 * The result is put in gap->ga_data.
5235 * Returns FAIL when out of memory.
5236 */
5237 int
5238win32_build_cmd(list_T *l, garray_T *gap)
5239{
5240 listitem_T *li;
5241 char_u *s;
5242
5243 for (li = l->lv_first; li != NULL; li = li->li_next)
5244 {
5245 s = get_tv_string_chk(&li->li_tv);
5246 if (s == NULL)
5247 return FAIL;
5248 s = win32_escape_arg(s);
5249 if (s == NULL)
5250 return FAIL;
5251 ga_concat(gap, s);
5252 vim_free(s);
5253 if (li->li_next != NULL)
5254 ga_append(gap, ' ');
5255 }
5256 return OK;
5257}
5258#endif
5259
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005260/*
5261 * NOTE: Must call job_cleanup() only once right after the status of "job"
5262 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5263 * mch_detect_ended_job() returned non-NULL).
5264 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005265 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005266job_cleanup(job_T *job)
5267{
5268 if (job->jv_status != JOB_ENDED)
5269 return;
5270
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005271 /* Ready to cleanup the job. */
5272 job->jv_status = JOB_FINISHED;
5273
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005274 /* When only channel-in is kept open, close explicitly. */
5275 if (job->jv_channel != NULL)
5276 ch_close_part(job->jv_channel, PART_IN);
5277
Bram Moolenaar97792de2016-10-15 18:36:49 +02005278 if (job->jv_exit_cb != NULL)
5279 {
5280 typval_T argv[3];
5281 typval_T rettv;
5282 int dummy;
5283
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005284 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005285 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005286 ++job->jv_refcount;
5287 argv[0].v_type = VAR_JOB;
5288 argv[0].vval.v_job = job;
5289 argv[1].v_type = VAR_NUMBER;
5290 argv[1].vval.v_number = job->jv_exitval;
5291 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5292 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5293 job->jv_exit_partial, NULL);
5294 clear_tv(&rettv);
5295 --job->jv_refcount;
5296 channel_need_redraw = TRUE;
5297 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005298
5299 /* Do not free the job in case the close callback of the associated channel
5300 * isn't invoked yet and may get information by job_info(). */
5301 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005302 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005303 /* The job was already unreferenced and the associated channel was
5304 * detached, now that it ended it can be freed. Careful: caller must
5305 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005306 job_free(job);
5307 }
5308}
5309
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005310/*
5311 * Mark references in jobs that are still useful.
5312 */
5313 int
5314set_ref_in_job(int copyID)
5315{
5316 int abort = FALSE;
5317 job_T *job;
5318 typval_T tv;
5319
5320 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005321 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005322 {
5323 tv.v_type = VAR_JOB;
5324 tv.vval.v_job = job;
5325 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5326 }
5327 return abort;
5328}
5329
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005330/*
5331 * Dereference "job". Note that after this "job" may have been freed.
5332 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005333 void
5334job_unref(job_T *job)
5335{
5336 if (job != NULL && --job->jv_refcount <= 0)
5337 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005338 /* Do not free the job if there is a channel where the close callback
5339 * may get the job info. */
5340 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005341 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005342 /* Do not free the job when it has not ended yet and there is a
5343 * "stoponexit" flag or an exit callback. */
5344 if (!job_need_end_check(job))
5345 {
5346 job_free(job);
5347 }
5348 else if (job->jv_channel != NULL)
5349 {
5350 /* Do remove the link to the channel, otherwise it hangs
5351 * around until Vim exits. See job_free() for refcount. */
5352 ch_log(job->jv_channel, "detaching channel from job");
5353 job->jv_channel->ch_job = NULL;
5354 channel_unref(job->jv_channel);
5355 job->jv_channel = NULL;
5356 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005357 }
5358 }
5359}
5360
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005361 int
5362free_unused_jobs_contents(int copyID, int mask)
5363{
5364 int did_free = FALSE;
5365 job_T *job;
5366
5367 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005368 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005369 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005370 {
5371 /* Free the channel and ordinary items it contains, but don't
5372 * recurse into Lists, Dictionaries etc. */
5373 job_free_contents(job);
5374 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005375 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005376 return did_free;
5377}
5378
5379 void
5380free_unused_jobs(int copyID, int mask)
5381{
5382 job_T *job;
5383 job_T *job_next;
5384
5385 for (job = first_job; job != NULL; job = job_next)
5386 {
5387 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005388 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005389 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005390 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005391 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005392 job_free_job(job);
5393 }
5394 }
5395}
5396
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005397/*
5398 * Allocate a job. Sets the refcount to one and sets options default.
5399 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005400 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005401job_alloc(void)
5402{
5403 job_T *job;
5404
5405 job = (job_T *)alloc_clear(sizeof(job_T));
5406 if (job != NULL)
5407 {
5408 job->jv_refcount = 1;
5409 job->jv_stoponexit = vim_strsave((char_u *)"term");
5410
5411 if (first_job != NULL)
5412 {
5413 first_job->jv_prev = job;
5414 job->jv_next = first_job;
5415 }
5416 first_job = job;
5417 }
5418 return job;
5419}
5420
5421 void
5422job_set_options(job_T *job, jobopt_T *opt)
5423{
5424 if (opt->jo_set & JO_STOPONEXIT)
5425 {
5426 vim_free(job->jv_stoponexit);
5427 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5428 job->jv_stoponexit = NULL;
5429 else
5430 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5431 }
5432 if (opt->jo_set & JO_EXIT_CB)
5433 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005434 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005435 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005436 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005437 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005438 job->jv_exit_partial = NULL;
5439 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005440 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005441 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005442 job->jv_exit_partial = opt->jo_exit_partial;
5443 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005444 {
5445 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005446 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005447 }
5448 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005449 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005450 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005451 func_ref(job->jv_exit_cb);
5452 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005453 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005454 }
5455}
5456
5457/*
5458 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5459 */
5460 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005461job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005462{
5463 job_T *job;
5464
5465 for (job = first_job; job != NULL; job = job->jv_next)
5466 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005467 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005468}
5469
5470/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005471 * Return TRUE when there is any job that has an exit callback and might exit,
5472 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005473 */
5474 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005475has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005476{
5477 job_T *job;
5478
5479 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005480 /* Only should check if the channel has been closed, if the channel is
5481 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005482 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5483 || (job->jv_status == JOB_FINISHED
5484 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005485 return TRUE;
5486 return FALSE;
5487}
5488
Bram Moolenaar97792de2016-10-15 18:36:49 +02005489#define MAX_CHECK_ENDED 8
5490
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005491/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005492 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005493 */
5494 void
5495job_check_ended(void)
5496{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005497 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005498
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005499 if (first_job == NULL)
5500 return;
5501
Bram Moolenaar97792de2016-10-15 18:36:49 +02005502 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005503 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005504 /* NOTE: mch_detect_ended_job() must only return a job of which the
5505 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005506 job_T *job = mch_detect_ended_job(first_job);
5507
5508 if (job == NULL)
5509 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005510 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005511 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005512
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005513 if (channel_need_redraw)
5514 {
5515 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005516 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005517 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005518}
5519
5520/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005521 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005522 * "argv_arg" is only for Unix.
5523 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005524 * The returned job has a refcount of one.
5525 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005526 */
5527 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005528job_start(
5529 typval_T *argvars,
5530 char **argv_arg,
5531 jobopt_T *opt_arg,
5532 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005533{
5534 job_T *job;
5535 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005536 char **argv = NULL;
5537 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005538#if defined(UNIX)
5539# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005540 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005541#else
5542 garray_T ga;
5543#endif
5544 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005545 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005546
5547 job = job_alloc();
5548 if (job == NULL)
5549 return NULL;
5550
5551 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005552#ifndef USE_ARGV
5553 ga_init2(&ga, (int)sizeof(char*), 20);
5554#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005555
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005556 if (opt_arg != NULL)
5557 opt = *opt_arg;
5558 else
5559 {
5560 /* Default mode is NL. */
5561 clear_job_options(&opt);
5562 opt.jo_mode = MODE_NL;
5563 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005564 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5565 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5566 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005567 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005568 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005569
5570 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005571 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005572 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5573 && opt.jo_io[part] == JIO_FILE
5574 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5575 || *opt.jo_io_name[part] == NUL))
5576 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005577 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005578 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005579 }
5580
5581 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5582 {
5583 buf_T *buf = NULL;
5584
5585 /* check that we can find the buffer before starting the job */
5586 if (opt.jo_set & JO_IN_BUF)
5587 {
5588 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5589 if (buf == NULL)
5590 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5591 }
5592 else if (!(opt.jo_set & JO_IN_NAME))
5593 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005594 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005595 }
5596 else
5597 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5598 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005599 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005600 if (buf->b_ml.ml_mfp == NULL)
5601 {
5602 char_u numbuf[NUMBUFLEN];
5603 char_u *s;
5604
5605 if (opt.jo_set & JO_IN_BUF)
5606 {
5607 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5608 s = numbuf;
5609 }
5610 else
5611 s = opt.jo_io_name[PART_IN];
5612 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005613 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005614 }
5615 job->jv_in_buf = buf;
5616 }
5617
5618 job_set_options(job, &opt);
5619
Bram Moolenaar13568252018-03-16 20:46:58 +01005620#ifdef USE_ARGV
5621 if (argv_arg != NULL)
5622 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005623 /* Make a copy of argv_arg for job->jv_argv. */
5624 for (i = 0; argv_arg[i] != NULL; i++)
5625 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005626 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005627 if (argv == NULL)
5628 goto theend;
5629 for (i = 0; i < argc; i++)
5630 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5631 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005632 }
5633 else
5634#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005635 if (argvars[0].v_type == VAR_STRING)
5636 {
5637 /* Command is a string. */
5638 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005639 if (cmd == NULL || *cmd == NUL)
5640 {
5641 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005642 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005643 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005644
5645 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005646 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005647 }
5648 else if (argvars[0].v_type != VAR_LIST
5649 || argvars[0].vval.v_list == NULL
5650 || argvars[0].vval.v_list->lv_len < 1)
5651 {
5652 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005653 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005654 }
5655 else
5656 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005657 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005658
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005659 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005660 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005661#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005662 if (win32_build_cmd(l, &ga) == FAIL)
5663 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005664 cmd = ga.ga_data;
5665#endif
5666 }
5667
Bram Moolenaar20608922018-04-21 22:30:08 +02005668 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005669 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005670
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005671#ifdef USE_ARGV
5672 if (ch_log_active())
5673 {
5674 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005675
5676 ga_init2(&ga, (int)sizeof(char), 200);
5677 for (i = 0; i < argc; ++i)
5678 {
5679 if (i > 0)
5680 ga_concat(&ga, (char_u *)" ");
5681 ga_concat(&ga, (char_u *)argv[i]);
5682 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005683 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005684 ga_clear(&ga);
5685 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005686 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005687#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005688 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005689 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005690#endif
5691
5692 /* If the channel is reading from a buffer, write lines now. */
5693 if (job->jv_channel != NULL)
5694 channel_write_in(job->jv_channel);
5695
5696theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005697#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005698 vim_free(ga.ga_data);
5699#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005700 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005701 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005702 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005703 return job;
5704}
5705
5706/*
5707 * Get the status of "job" and invoke the exit callback when needed.
5708 * The returned string is not allocated.
5709 */
5710 char *
5711job_status(job_T *job)
5712{
5713 char *result;
5714
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005715 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005716 /* No need to check, dead is dead. */
5717 result = "dead";
5718 else if (job->jv_status == JOB_FAILED)
5719 result = "fail";
5720 else
5721 {
5722 result = mch_job_status(job);
5723 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005724 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005725 }
5726 return result;
5727}
5728
Bram Moolenaar8950a562016-03-12 15:22:55 +01005729/*
5730 * Implementation of job_info().
5731 */
5732 void
5733job_info(job_T *job, dict_T *dict)
5734{
5735 dictitem_T *item;
5736 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005737 list_T *l;
5738 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005739
Bram Moolenaare0be1672018-07-08 16:50:37 +02005740 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005741
5742 item = dictitem_alloc((char_u *)"channel");
5743 if (item == NULL)
5744 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005745 item->di_tv.v_type = VAR_CHANNEL;
5746 item->di_tv.vval.v_channel = job->jv_channel;
5747 if (job->jv_channel != NULL)
5748 ++job->jv_channel->ch_refcount;
5749 if (dict_add(dict, item) == FAIL)
5750 dictitem_free(item);
5751
5752#ifdef UNIX
5753 nr = job->jv_pid;
5754#else
5755 nr = job->jv_proc_info.dwProcessId;
5756#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005757 dict_add_number(dict, "process", nr);
5758 dict_add_string(dict, "tty_in", job->jv_tty_in);
5759 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005760
Bram Moolenaare0be1672018-07-08 16:50:37 +02005761 dict_add_number(dict, "exitval", job->jv_exitval);
5762 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5763 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005764
5765 l = list_alloc();
5766 if (l != NULL)
5767 {
5768 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005769 if (job->jv_argv != NULL)
5770 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005771 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005772 }
5773}
5774
5775/*
5776 * Implementation of job_info() to return info for all jobs.
5777 */
5778 void
5779job_info_all(list_T *l)
5780{
5781 job_T *job;
5782 typval_T tv;
5783
5784 for (job = first_job; job != NULL; job = job->jv_next)
5785 {
5786 tv.v_type = VAR_JOB;
5787 tv.vval.v_job = job;
5788
5789 if (list_append_tv(l, &tv) != OK)
5790 return;
5791 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005792}
5793
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005794/*
5795 * Send a signal to "job". Implements job_stop().
5796 * When "type" is not NULL use this for the type.
5797 * Otherwise use argvars[1] for the type.
5798 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005799 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005800job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005801{
5802 char_u *arg;
5803
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005804 if (type != NULL)
5805 arg = (char_u *)type;
5806 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005807 arg = (char_u *)"";
5808 else
5809 {
5810 arg = get_tv_string_chk(&argvars[1]);
5811 if (arg == NULL)
5812 {
5813 EMSG(_(e_invarg));
5814 return 0;
5815 }
5816 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005817 if (job->jv_status == JOB_FAILED)
5818 {
5819 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5820 return 0;
5821 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005822 if (job->jv_status == JOB_ENDED)
5823 {
5824 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5825 return 0;
5826 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005827 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005828 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005829 return 0;
5830
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005831 /* Assume that only "kill" will kill the job. */
5832 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005833 job->jv_channel->ch_job_killed = TRUE;
5834
5835 /* We don't try freeing the job, obviously the caller still has a
5836 * reference to it. */
5837 return 1;
5838}
5839
Bram Moolenaarf2732452018-06-03 14:47:35 +02005840 void
5841invoke_prompt_callback(void)
5842{
5843 typval_T rettv;
5844 int dummy;
5845 typval_T argv[2];
5846 char_u *text;
5847 char_u *prompt;
5848 linenr_T lnum = curbuf->b_ml.ml_line_count;
5849
5850 // Add a new line for the prompt before invoking the callback, so that
5851 // text can always be inserted above the last line.
5852 ml_append(lnum, (char_u *)"", 0, FALSE);
5853 curwin->w_cursor.lnum = lnum + 1;
5854 curwin->w_cursor.col = 0;
5855
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005856 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005857 return;
5858 text = ml_get(lnum);
5859 prompt = prompt_text();
5860 if (STRLEN(text) >= STRLEN(prompt))
5861 text += STRLEN(prompt);
5862 argv[0].v_type = VAR_STRING;
5863 argv[0].vval.v_string = vim_strsave(text);
5864 argv[1].v_type = VAR_UNKNOWN;
5865
5866 call_func(curbuf->b_prompt_callback,
5867 (int)STRLEN(curbuf->b_prompt_callback),
5868 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5869 curbuf->b_prompt_partial, NULL);
5870 clear_tv(&argv[0]);
5871 clear_tv(&rettv);
5872}
5873
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005874/*
5875 * Return TRUE when the interrupt callback was invoked.
5876 */
5877 int
5878invoke_prompt_interrupt(void)
5879{
5880 typval_T rettv;
5881 int dummy;
5882 typval_T argv[1];
5883
5884 if (curbuf->b_prompt_interrupt == NULL
5885 || *curbuf->b_prompt_interrupt == NUL)
5886 return FALSE;
5887 argv[0].v_type = VAR_UNKNOWN;
5888
5889 got_int = FALSE; // don't skip executing commands
5890 call_func(curbuf->b_prompt_interrupt,
5891 (int)STRLEN(curbuf->b_prompt_interrupt),
5892 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5893 curbuf->b_prompt_int_partial, NULL);
5894 clear_tv(&rettv);
5895 return TRUE;
5896}
5897
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005898#endif /* FEAT_JOB_CHANNEL */