blob: 34a1fc3e039e1f561b24f6d68e4828f7ccd14af6 [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{
Bram Moolenaar24058382019-01-24 23:11:49 +010083 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010084 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010085 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010086 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010087
Bram Moolenaar24058382019-01-24 23:11:49 +010088 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 {
Bram Moolenaar24058382019-01-24 23:11:49 +010090 if (todo > MAX_NAMED_PIPE_SIZE)
91 size = MAX_NAMED_PIPE_SIZE;
92 else
Bram Moolenaardec01202019-01-28 20:04:24 +010093 size = (DWORD)todo;
Bram Moolenaar24058382019-01-24 23:11:49 +010094 // If the pipe overflows while the job does not read the data, WriteFile
95 // will block forever. This abandons the write.
96 memset(&ov, 0, sizeof(ov));
97 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
98 {
99 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100100
Bram Moolenaar24058382019-01-24 23:11:49 +0100101 if (err != ERROR_IO_PENDING)
102 return -1;
103 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
104 return -1;
105 FlushFileBuffers(h);
106 }
107 todo -= nwrite;
108 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100109 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100110 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100111}
112
113 static void
114fd_close(sock_T fd)
115{
116 HANDLE h = (HANDLE)fd;
117
118 CloseHandle(h);
119}
120#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100121
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100122/* Log file opened with ch_logfile(). */
123static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100124#ifdef FEAT_RELTIME
125static proftime_T log_start;
126#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127
128 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100129ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100130{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100131 FILE *file = NULL;
132
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100133 if (log_fd != NULL)
134 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100135
136 if (*fname != NUL)
137 {
138 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
139 if (file == NULL)
140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100141 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100142 return;
143 }
144 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100145 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100146
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100147 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100148 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100149 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100150#ifdef FEAT_RELTIME
151 profile_start(&log_start);
152#endif
153 }
154}
155
156 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200157ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100158{
159 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100160}
161
162 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200163ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164{
165 if (log_fd != NULL)
166 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100167#ifdef FEAT_RELTIME
168 proftime_T log_now;
169
170 profile_start(&log_now);
171 profile_sub(&log_now, &log_start);
172 fprintf(log_fd, "%s ", profile_msg(&log_now));
173#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100174 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200175 {
176 if (part < PART_COUNT)
177 fprintf(log_fd, "%son %d(%s): ",
178 what, ch->ch_id, part_names[part]);
179 else
180 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
181 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 else
183 fprintf(log_fd, "%s: ", what);
184 }
185}
186
Bram Moolenaard0b65022016-03-06 21:50:33 +0100187static int did_log_msg = TRUE;
188
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200189#ifndef PROTO /* prototype is in vim.h */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100190 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200191ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192{
193 if (log_fd != NULL)
194 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200195 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100196
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200197 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200198 va_start(ap, fmt);
199 vfprintf(log_fd, fmt, ap);
200 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100201 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100203 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100204 }
205}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200206#endif
207
208 static void
209ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200210#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
211 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200212#endif
213 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214
215 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200216ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100217{
218 if (log_fd != NULL)
219 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200220 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200222 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200223 va_start(ap, fmt);
224 vfprintf(log_fd, fmt, ap);
225 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100226 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100228 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 }
230}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100231
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100232#ifdef _WIN32
233# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100234# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100235
236 static char *
237strerror_win32(int eno)
238{
239 static LPVOID msgbuf = NULL;
240 char_u *ptr;
241
242 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200243 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100244 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200245 msgbuf = NULL;
246 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100247 FormatMessage(
248 FORMAT_MESSAGE_ALLOCATE_BUFFER |
249 FORMAT_MESSAGE_FROM_SYSTEM |
250 FORMAT_MESSAGE_IGNORE_INSERTS,
251 NULL,
252 eno,
253 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
254 (LPTSTR) &msgbuf,
255 0,
256 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200257 if (msgbuf != NULL)
258 /* chomp \r or \n */
259 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
260 switch (*ptr)
261 {
262 case '\r':
263 STRMOVE(ptr, ptr + 1);
264 ptr--;
265 break;
266 case '\n':
267 if (*(ptr + 1) == '\0')
268 *ptr = '\0';
269 else
270 *ptr = ' ';
271 break;
272 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100273 return msgbuf;
274}
275#endif
276
Bram Moolenaar77073442016-02-13 23:23:53 +0100277/*
278 * The list of all allocated channels.
279 */
280static channel_T *first_channel = NULL;
281static int next_ch_id = 0;
282
283/*
284 * Allocate a new channel. The refcount is set to 1.
285 * The channel isn't actually used until it is opened.
286 * Returns NULL if out of memory.
287 */
288 channel_T *
289add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100290{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200291 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100292 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100293
Bram Moolenaar77073442016-02-13 23:23:53 +0100294 if (channel == NULL)
295 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100296
Bram Moolenaar77073442016-02-13 23:23:53 +0100297 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100298 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100299
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200300 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100301 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100302 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100303#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100304 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100305#endif
306#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100307 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100309 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100310 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100311
Bram Moolenaar77073442016-02-13 23:23:53 +0100312 if (first_channel != NULL)
313 {
314 first_channel->ch_prev = channel;
315 channel->ch_next = first_channel;
316 }
317 first_channel = channel;
318
319 channel->ch_refcount = 1;
320 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100321}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100322
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200323 int
324has_any_channel(void)
325{
326 return first_channel != NULL;
327}
328
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100329/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100330 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100331 * Return TRUE if "channel" has a callback and the associated job wasn't
332 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100333 */
334 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100335channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100336{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100337 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100338 int has_out_msg;
339 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100340
341 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100342 if (channel->ch_job_killed && channel->ch_job == NULL)
343 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344
345 /* If there is a close callback it may still need to be invoked. */
346 if (channel->ch_close_cb != NULL)
347 return TRUE;
348
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200349 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200350 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200351 return TRUE;
352
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100353 /* If there is no callback then nobody can get readahead. If the fd is
354 * closed and there is no readahead then the callback won't be called. */
355 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100356 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
357 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
359 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
360 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
361 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
362 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
363 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100365 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200366 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200367 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
368 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200369 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200370 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
371 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100372}
373
374/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200375 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
376 */
377 static int
378channel_can_close(channel_T *channel)
379{
380 return channel->ch_to_be_closed == 0;
381}
382
383/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200384 * Close a channel and free all its resources.
385 */
386 static void
387channel_free_contents(channel_T *channel)
388{
389 channel_close(channel, TRUE);
390 channel_clear(channel);
391 ch_log(channel, "Freeing channel");
392}
393
394 static void
395channel_free_channel(channel_T *channel)
396{
397 if (channel->ch_next != NULL)
398 channel->ch_next->ch_prev = channel->ch_prev;
399 if (channel->ch_prev == NULL)
400 first_channel = channel->ch_next;
401 else
402 channel->ch_prev->ch_next = channel->ch_next;
403 vim_free(channel);
404}
405
406 static void
407channel_free(channel_T *channel)
408{
409 if (!in_free_unref_items)
410 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200411 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200412 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200413 else
414 {
415 channel_free_contents(channel);
416 channel_free_channel(channel);
417 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200418 }
419}
420
421/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100422 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100423 * possible, there is no callback to be invoked or the associated job was
424 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100425 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100426 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100427 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100428channel_may_free(channel_T *channel)
429{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100430 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100431 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100432 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 return TRUE;
434 }
435 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436}
437
438/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100439 * Decrement the reference count on "channel" and maybe free it when it goes
440 * down to zero. Don't free it if there is a pending action.
441 * Returns TRUE when the channel is no longer referenced.
442 */
443 int
444channel_unref(channel_T *channel)
445{
446 if (channel != NULL && --channel->ch_refcount <= 0)
447 return channel_may_free(channel);
448 return FALSE;
449}
450
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200451 int
452free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100453{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200454 int did_free = FALSE;
455 channel_T *ch;
456
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200457 /* This is invoked from the garbage collector, which only runs at a safe
458 * point. */
459 ++safe_to_invoke_callback;
460
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200461 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200462 if (!channel_still_useful(ch)
463 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 {
465 /* Free the channel and ordinary items it contains, but don't
466 * recurse into Lists, Dictionaries etc. */
467 channel_free_contents(ch);
468 did_free = TRUE;
469 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200470
471 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200472 return did_free;
473}
474
475 void
476free_unused_channels(int copyID, int mask)
477{
478 channel_T *ch;
479 channel_T *ch_next;
480
481 for (ch = first_channel; ch != NULL; ch = ch_next)
482 {
483 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200484 if (!channel_still_useful(ch)
485 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200486 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200487 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200488 channel_free_channel(ch);
489 }
490 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100491}
492
Bram Moolenaard04a0202016-01-26 23:30:18 +0100493#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100494
495#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
496 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100497channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100498{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100499 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200500 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100501
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100502 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100503 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200504 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100505 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200506 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100507}
508#endif
509
Bram Moolenaare0874f82016-01-24 20:36:41 +0100510/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100511 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513#ifdef FEAT_GUI_X11
514 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200515messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200516 int *unused1 UNUSED,
517 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100519 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100521#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100524# if GTK_CHECK_VERSION(3,0,0)
525 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200526messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527 GIOCondition unused2 UNUSED,
528 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100529{
530 channel_read_fd(GPOINTER_TO_INT(clientData));
531 return TRUE; /* Return FALSE instead in case the event source is to
532 * be removed after this function returns. */
533}
534# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200536messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200537 gint unused1 UNUSED,
538 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100539{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100540 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100541}
Bram Moolenaar98921892016-02-23 17:14:37 +0100542# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100543#endif
544
545 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200546channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100547{
Bram Moolenaarde279892016-03-11 22:19:44 +0100548 if (!CH_HAS_GUI)
549 return;
550
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200551 /* gets stuck in handling events for a not connected channel */
552 if (channel->ch_keep_open)
553 return;
554
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100555# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200556 /* Tell notifier we are interested in being called when there is input on
557 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100558 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200559 {
560 ch_log(channel, "Registering part %s with fd %d",
561 part_names[part], channel->ch_part[part].ch_fd);
562
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100563 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100564 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100565 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100566 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200567 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100568 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200569 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100570# else
571# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200572 /* Tell gdk we are interested in being called when there is input on the
573 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100574 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100575 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200576 ch_log(channel, "Registering part %s with fd %d",
577 part_names[part], channel->ch_part[part].ch_fd);
578# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100579 GIOChannel *chnnl = g_io_channel_unix_new(
580 (gint)channel->ch_part[part].ch_fd);
581
582 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
583 chnnl,
584 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200585 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100586 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
587
588 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100589# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100590 channel->ch_part[part].ch_inputHandler = gdk_input_add(
591 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100592 (GdkInputCondition)
593 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200594 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100595 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100596# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200597 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100598# endif
599# endif
600}
601
Bram Moolenaarde279892016-03-11 22:19:44 +0100602 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100603channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100604{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100605 if (channel->CH_SOCK_FD != INVALID_FD)
606 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200607 if (channel->CH_OUT_FD != INVALID_FD
608 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100609 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200610 if (channel->CH_ERR_FD != INVALID_FD
611 && channel->CH_ERR_FD != channel->CH_SOCK_FD
612 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100613 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100614}
615
616/*
617 * Register any of our file descriptors with the GUI event handling system.
618 * Called when the GUI has started.
619 */
620 void
621channel_gui_register_all(void)
622{
Bram Moolenaar77073442016-02-13 23:23:53 +0100623 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100624
Bram Moolenaar77073442016-02-13 23:23:53 +0100625 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100626 channel_gui_register(channel);
627}
628
629 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200630channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100631{
632# ifdef FEAT_GUI_X11
633 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
634 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200635 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100636 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
637 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
638 }
639# else
640# ifdef FEAT_GUI_GTK
641 if (channel->ch_part[part].ch_inputHandler != 0)
642 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200643 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100644# if GTK_CHECK_VERSION(3,0,0)
645 g_source_remove(channel->ch_part[part].ch_inputHandler);
646# else
647 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
648# endif
649 channel->ch_part[part].ch_inputHandler = 0;
650 }
651# endif
652# endif
653}
654
655 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100656channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100657{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200658 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100659
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100660 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100661 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662}
663
664#endif
665
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100666static char *e_cannot_connect = N_("E902: Cannot connect to port");
667
Bram Moolenaard04a0202016-01-26 23:30:18 +0100668/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100669 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100670 * "waittime" is the time in msec to wait for the connection.
671 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100672 * Returns the channel for success.
673 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100675 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100676channel_open(
677 char *hostname,
678 int port_in,
679 int waittime,
680 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100683 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100685#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100687 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100688#else
689 int port = port_in;
690#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100691 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100692 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100694#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100695 channel_init_winsock();
696#endif
697
Bram Moolenaar77073442016-02-13 23:23:53 +0100698 channel = add_channel();
699 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100700 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100701 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100702 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100703 }
704
705 /* Get the server internet address and put into addr structure */
706 /* fill in the socket address structure and connect to server */
707 vim_memset((char *)&server, 0, sizeof(server));
708 server.sin_family = AF_INET;
709 server.sin_port = htons(port);
710 if ((host = gethostbyname(hostname)) == NULL)
711 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100712 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200713 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100714 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100715 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100716 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100717 {
718 char *p;
719
Bram Moolenaar2e324952018-04-14 14:37:07 +0200720 /* When using host->h_addr_list[0] directly ubsan warns for it to not
721 * be aligned. First copy the pointer to avoid that. */
722 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100723 memcpy((char *)&server.sin_addr, p, host->h_length);
724 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100725
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100726 /* On Mac and Solaris a zero timeout almost never works. At least wait
727 * one millisecond. Let's do it for all systems, because we don't know why
728 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100729 if (waittime == 0)
730 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731
732 /*
733 * For Unix we need to call connect() again after connect() failed.
734 * On Win32 one time is sufficient.
735 */
736 while (TRUE)
737 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100738 long elapsed_msec = 0;
739 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100740
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100741 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100742 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100743 sd = socket(AF_INET, SOCK_STREAM, 0);
744 if (sd == -1)
745 {
746 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200747 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100748 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100749 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100750 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100751
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100752 if (waittime >= 0)
753 {
754 /* Make connect() non-blocking. */
755 if (
756#ifdef _WIN32
757 ioctlsocket(sd, FIONBIO, &val) < 0
758#else
759 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
760#endif
761 )
762 {
763 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200764 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100765 "channel_open: Connect failed with errno %d", errno);
766 sock_close(sd);
767 channel_free(channel);
768 return NULL;
769 }
770 }
771
772 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200773 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100774 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
775
Bram Moolenaar045a2842016-03-08 22:33:07 +0100776 if (ret == 0)
777 /* The connection could be established. */
778 break;
779
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100780 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781 if (waittime < 0 || (errno != EWOULDBLOCK
782 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100783#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100784 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100785#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100786 ))
787 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200788 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 "channel_open: Connect failed with errno %d", errno);
790 PERROR(_(e_cannot_connect));
791 sock_close(sd);
792 channel_free(channel);
793 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100794 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100795
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100796 /* Limit the waittime to 50 msec. If it doesn't work within this
797 * time we close the socket and try creating it again. */
798 waitnow = waittime > 50 ? 50 : waittime;
799
Bram Moolenaar045a2842016-03-08 22:33:07 +0100800 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100801 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100802#ifndef WIN32
803 if (errno != ECONNREFUSED)
804#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100805 {
806 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100807 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100809#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100810 int so_error = 0;
811 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100812 struct timeval start_tv;
813 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100814#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100815 FD_ZERO(&rfds);
816 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100817 FD_ZERO(&wfds);
818 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100819
Bram Moolenaar562ca712016-03-09 21:50:05 +0100820 tv.tv_sec = waitnow / 1000;
821 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100822#ifndef WIN32
823 gettimeofday(&start_tv, NULL);
824#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200825 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100826 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100827 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100828
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100829 if (ret < 0)
830 {
831 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200832 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100833 "channel_open: Connect failed with errno %d", errno);
834 PERROR(_(e_cannot_connect));
835 sock_close(sd);
836 channel_free(channel);
837 return NULL;
838 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100839
Bram Moolenaare081e212016-02-28 22:33:46 +0100840#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100841 /* On Win32: select() is expected to work and wait for up to
842 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100843 if (FD_ISSET(sd, &wfds))
844 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100845 elapsed_msec = waitnow;
846 if (waittime > 1 && elapsed_msec < waittime)
847 {
848 waittime -= elapsed_msec;
849 continue;
850 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100851#else
852 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100853 * After putting the socket in non-blocking mode, connect() will
854 * return EINPROGRESS, select() will not wait (as if writing is
855 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100856 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100857 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100858 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100859 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100860 {
861 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100862 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 if (ret < 0 || (so_error != 0
864 && so_error != EWOULDBLOCK
865 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100866# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100867 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100868# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100869 ))
870 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200871 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100872 "channel_open: Connect failed with errno %d",
873 so_error);
874 PERROR(_(e_cannot_connect));
875 sock_close(sd);
876 channel_free(channel);
877 return NULL;
878 }
879 }
880
Bram Moolenaar045a2842016-03-08 22:33:07 +0100881 if (FD_ISSET(sd, &wfds) && so_error == 0)
882 /* Did not detect an error, connection is established. */
883 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100884
Bram Moolenaar045a2842016-03-08 22:33:07 +0100885 gettimeofday(&end_tv, NULL);
886 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
887 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100888#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100889 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100890
891#ifndef WIN32
892 if (waittime > 1 && elapsed_msec < waittime)
893 {
894 /* The port isn't ready but we also didn't get an error.
895 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100896 * yet. Select() may return early, wait until the remaining
897 * "waitnow" and try again. */
898 waitnow -= elapsed_msec;
899 waittime -= elapsed_msec;
900 if (waitnow > 0)
901 {
902 mch_delay((long)waitnow, TRUE);
903 ui_breakcheck();
904 waittime -= waitnow;
905 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100906 if (!got_int)
907 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100908 if (waittime <= 0)
909 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100910 waittime = 1;
911 continue;
912 }
913 /* we were interrupted, behave as if timed out */
914 }
915#endif
916
917 /* We timed out. */
918 ch_error(channel, "Connection timed out");
919 sock_close(sd);
920 channel_free(channel);
921 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100922 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100923
Bram Moolenaar045a2842016-03-08 22:33:07 +0100924 ch_log(channel, "Connection made");
925
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100926 if (waittime >= 0)
927 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100928#ifdef _WIN32
929 val = 0;
930 ioctlsocket(sd, FIONBIO, &val);
931#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100932 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100933#endif
934 }
935
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100936 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100937 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100938 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
939 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200940 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100941
942#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100943 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100944#endif
945
Bram Moolenaar77073442016-02-13 23:23:53 +0100946 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100947}
948
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100949/*
950 * Implements ch_open().
951 */
952 channel_T *
953channel_open_func(typval_T *argvars)
954{
955 char_u *address;
956 char_u *p;
957 char *rest;
958 int port;
959 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200960 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100961
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100962 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100963 if (argvars[1].v_type != VAR_UNKNOWN
964 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100966 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100967 return NULL;
968 }
969
970 /* parse address */
971 p = vim_strchr(address, ':');
972 if (p == NULL)
973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100974 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100975 return NULL;
976 }
977 *p++ = NUL;
978 port = strtol((char *)p, &rest, 10);
979 if (*address == NUL || port <= 0 || *rest != NUL)
980 {
981 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100982 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100983 return NULL;
984 }
985
986 /* parse options */
987 clear_job_options(&opt);
988 opt.jo_mode = MODE_JSON;
989 opt.jo_timeout = 2000;
990 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200991 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200992 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100993 if (opt.jo_timeout < 0)
994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100995 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200996 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100997 }
998
999 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1000 if (channel != NULL)
1001 {
1002 opt.jo_set = JO_ALL;
1003 channel_set_options(channel, &opt);
1004 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001005theend:
1006 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001007 return channel;
1008}
1009
Bram Moolenaarde279892016-03-11 22:19:44 +01001010 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001011ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001012{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001013 sock_T *fd = &channel->ch_part[part].ch_fd;
1014
Bram Moolenaarde279892016-03-11 22:19:44 +01001015 if (*fd != INVALID_FD)
1016 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001017 if (part == PART_SOCK)
1018 sock_close(*fd);
1019 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001020 {
1021 /* When using a pty the same FD is set on multiple parts, only
1022 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001023 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1024 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1025 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001026 {
1027#ifdef WIN32
1028 if (channel->ch_named_pipe)
1029 DisconnectNamedPipe((HANDLE)fd);
1030#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001031 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001032 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001033 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001034 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001035
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001036 /* channel is closed, may want to end the job if it was the last */
1037 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001038 }
1039}
1040
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001041 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001042channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001043{
Bram Moolenaarde279892016-03-11 22:19:44 +01001044 if (in != INVALID_FD)
1045 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001046 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001047 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001048# if defined(UNIX)
1049 /* Do not end the job when all output channels are closed, wait until
1050 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001051 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001052 channel->ch_to_be_closed |= (1U << PART_IN);
1053# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001054 }
1055 if (out != INVALID_FD)
1056 {
1057# if defined(FEAT_GUI)
1058 channel_gui_unregister_one(channel, PART_OUT);
1059# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001060 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001061 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001062 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001063# if defined(FEAT_GUI)
1064 channel_gui_register_one(channel, PART_OUT);
1065# endif
1066 }
1067 if (err != INVALID_FD)
1068 {
1069# if defined(FEAT_GUI)
1070 channel_gui_unregister_one(channel, PART_ERR);
1071# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001072 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001073 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001074 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001075# if defined(FEAT_GUI)
1076 channel_gui_register_one(channel, PART_ERR);
1077# endif
1078 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001079}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001080
Bram Moolenaard6051b52016-02-28 15:49:03 +01001081/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001082 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001083 * This does not keep a refcount, when the job is freed ch_job is cleared.
1084 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001085 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001086channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001087{
Bram Moolenaar77073442016-02-13 23:23:53 +01001088 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001089
1090 channel_set_options(channel, options);
1091
1092 if (job->jv_in_buf != NULL)
1093 {
1094 chanpart_T *in_part = &channel->ch_part[PART_IN];
1095
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001096 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001097 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001098 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001099 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001100 {
1101 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1102 {
1103 /* Special mode: send last-but-one line when appending a line
1104 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001105 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001106 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001107 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001108 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001109 }
1110 else
1111 in_part->ch_buf_top = options->jo_in_top;
1112 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001113 else
1114 in_part->ch_buf_top = 1;
1115 if (options->jo_set & JO_IN_BOT)
1116 in_part->ch_buf_bot = options->jo_in_bot;
1117 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001118 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001119 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001120}
1121
1122/*
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001123 * Prepare buffer "buf" for writing channel output to.
1124 */
1125 static void
1126prepare_buffer(buf_T *buf)
1127{
1128 buf_T *save_curbuf = curbuf;
1129
1130 buf_copy_options(buf, BCO_ENTER);
1131 curbuf = buf;
1132#ifdef FEAT_QUICKFIX
1133 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1134 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1135#endif
1136 if (curbuf->b_ml.ml_mfp == NULL)
1137 ml_open(curbuf);
1138 curbuf = save_curbuf;
1139}
1140
1141/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001142 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001143 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001144 */
1145 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001146find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001147{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001148 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001149 buf_T *save_curbuf = curbuf;
1150
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001151 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001152 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001153 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001154 if (buf == NULL)
1155 buf = buflist_findname_exp(name);
1156 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001157 if (buf == NULL)
1158 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001159 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001160 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001161 if (buf == NULL)
1162 return NULL;
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001163 prepare_buffer(buf);
1164
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001165 curbuf = buf;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001166 if (msg)
1167 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001168 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001169 changed_bytes(1, 0);
1170 curbuf = save_curbuf;
1171 }
1172
1173 return buf;
1174}
1175
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001176 static void
1177set_callback(
1178 char_u **cbp,
1179 partial_T **pp,
1180 char_u *callback,
1181 partial_T *partial)
1182{
1183 free_callback(*cbp, *pp);
1184 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001185 {
1186 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001187 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001188 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001189 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001190 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001191 func_ref(*cbp);
1192 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001193 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001194 else
1195 *cbp = NULL;
1196 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001197 if (partial != NULL)
1198 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001199}
1200
Bram Moolenaar187db502016-02-27 14:44:26 +01001201/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001202 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001203 */
1204 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001205channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001206{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001207 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001208
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001209 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001210 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001211 channel->ch_part[part].ch_mode = opt->jo_mode;
1212 if (opt->jo_set & JO_IN_MODE)
1213 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1214 if (opt->jo_set & JO_OUT_MODE)
1215 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1216 if (opt->jo_set & JO_ERR_MODE)
1217 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001218 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001219
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001220 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001221 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001222 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1223 if (opt->jo_set & JO_OUT_TIMEOUT)
1224 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1225 if (opt->jo_set & JO_ERR_TIMEOUT)
1226 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001227 if (opt->jo_set & JO_BLOCK_WRITE)
1228 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001229
1230 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001231 set_callback(&channel->ch_callback, &channel->ch_partial,
1232 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001233 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001234 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1235 &channel->ch_part[PART_OUT].ch_partial,
1236 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001237 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001238 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1239 &channel->ch_part[PART_ERR].ch_partial,
1240 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001241 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001242 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1243 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001244 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001245
1246 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1247 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 buf_T *buf;
1249
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001250 /* writing output to a buffer. Default mode is NL. */
1251 if (!(opt->jo_set & JO_OUT_MODE))
1252 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001253 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001254 {
1255 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1256 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001257 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001258 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001259 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001260 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001261 int msg = TRUE;
1262
1263 if (opt->jo_set2 & JO2_OUT_MSG)
1264 msg = opt->jo_message[PART_OUT];
1265 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001266 }
1267 if (buf != NULL)
1268 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001269 if (opt->jo_set & JO_OUT_MODIFIABLE)
1270 channel->ch_part[PART_OUT].ch_nomodifiable =
1271 !opt->jo_modifiable[PART_OUT];
1272
1273 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001275 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001276 }
1277 else
1278 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001279 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001280 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001281 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001282 // if the buffer was deleted or unloaded resurrect it
1283 if (buf->b_ml.ml_mfp == NULL)
1284 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001285 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001286 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001287 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001288
1289 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1290 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1291 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1292 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001293 buf_T *buf;
1294
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001295 /* writing err to a buffer. Default mode is NL. */
1296 if (!(opt->jo_set & JO_ERR_MODE))
1297 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1298 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001299 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001300 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001301 {
1302 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1303 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001304 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001305 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001306 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001307 {
1308 int msg = TRUE;
1309
1310 if (opt->jo_set2 & JO2_ERR_MSG)
1311 msg = opt->jo_message[PART_ERR];
1312 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1313 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001314 if (buf != NULL)
1315 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001316 if (opt->jo_set & JO_ERR_MODIFIABLE)
1317 channel->ch_part[PART_ERR].ch_nomodifiable =
1318 !opt->jo_modifiable[PART_ERR];
1319 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001322 }
1323 else
1324 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001325 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001326 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001327 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001328 // if the buffer was deleted or unloaded resurrect it
1329 if (buf->b_ml.ml_mfp == NULL)
1330 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001331 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001332 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001333 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001334
1335 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1336 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1337 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001338}
1339
1340/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001341 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001342 */
1343 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001344channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001345 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001346 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001347 char_u *callback,
1348 partial_T *partial,
1349 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001350{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001351 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001352 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1353
1354 if (item != NULL)
1355 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001356 item->cq_partial = partial;
1357 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001358 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001359 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001360 item->cq_callback = callback;
1361 }
1362 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001363 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001364 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001365 func_ref(item->cq_callback);
1366 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001367 item->cq_seq_nr = id;
1368 item->cq_prev = head->cq_prev;
1369 head->cq_prev = item;
1370 item->cq_next = NULL;
1371 if (item->cq_prev == NULL)
1372 head->cq_next = item;
1373 else
1374 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001375 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001376}
1377
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001378 static void
1379write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1380{
1381 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001382 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001383 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001384 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001385
Bram Moolenaar655da312016-05-28 22:22:34 +02001386 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001387 if ((p = alloc(len + 2)) == NULL)
1388 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001389 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001390
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001391 if (channel->ch_write_text_mode)
1392 p[len] = CAR;
1393 else
1394 {
1395 for (i = 0; i < len; ++i)
1396 if (p[i] == NL)
1397 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001398
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001399 p[len] = NL;
1400 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001401 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001402 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001403 vim_free(p);
1404}
1405
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001406/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001407 * Return TRUE if "channel" can be written to.
1408 * Returns FALSE if the input is closed or the write would block.
1409 */
1410 static int
1411can_write_buf_line(channel_T *channel)
1412{
1413 chanpart_T *in_part = &channel->ch_part[PART_IN];
1414
1415 if (in_part->ch_fd == INVALID_FD)
1416 return FALSE; /* pipe was closed */
1417
1418 /* for testing: block every other attempt to write */
1419 if (in_part->ch_block_write == 1)
1420 in_part->ch_block_write = -1;
1421 else if (in_part->ch_block_write == -1)
1422 in_part->ch_block_write = 1;
1423
1424 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1425#ifndef WIN32
1426 {
1427# if defined(HAVE_SELECT)
1428 struct timeval tval;
1429 fd_set wfds;
1430 int ret;
1431
1432 FD_ZERO(&wfds);
1433 FD_SET((int)in_part->ch_fd, &wfds);
1434 tval.tv_sec = 0;
1435 tval.tv_usec = 0;
1436 for (;;)
1437 {
1438 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1439# ifdef EINTR
1440 SOCK_ERRNO;
1441 if (ret == -1 && errno == EINTR)
1442 continue;
1443# endif
1444 if (ret <= 0 || in_part->ch_block_write == 1)
1445 {
1446 if (ret > 0)
1447 ch_log(channel, "FAKED Input not ready for writing");
1448 else
1449 ch_log(channel, "Input not ready for writing");
1450 return FALSE;
1451 }
1452 break;
1453 }
1454# else
1455 struct pollfd fds;
1456
1457 fds.fd = in_part->ch_fd;
1458 fds.events = POLLOUT;
1459 if (poll(&fds, 1, 0) <= 0)
1460 {
1461 ch_log(channel, "Input not ready for writing");
1462 return FALSE;
1463 }
1464 if (in_part->ch_block_write == 1)
1465 {
1466 ch_log(channel, "FAKED Input not ready for writing");
1467 return FALSE;
1468 }
1469# endif
1470 }
1471#endif
1472 return TRUE;
1473}
1474
1475/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001476 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001477 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001478 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001479channel_write_in(channel_T *channel)
1480{
1481 chanpart_T *in_part = &channel->ch_part[PART_IN];
1482 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001483 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001484 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001485
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001486 if (buf == NULL || in_part->ch_buf_append)
1487 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001488 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001489 {
1490 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001491 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001492 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001493 return;
1494 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001495
1496 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1497 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1498 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001499 if (!can_write_buf_line(channel))
1500 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001501 write_buf_line(buf, lnum, channel);
1502 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001503 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504
1505 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001506 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001507 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001508 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001509
Bram Moolenaar014069a2016-03-03 22:51:40 +01001510 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001511 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001512 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001513#if defined(FEAT_TERMINAL)
1514 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001515 if (channel->ch_job != NULL)
1516 term_send_eof(channel);
1517#endif
1518
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001519 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001520 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001521 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001522
1523 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001524 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001525 }
1526 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001527 ch_log(channel, "Still %ld more lines to write",
1528 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001529}
1530
1531/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001532 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001533 */
1534 void
1535channel_buffer_free(buf_T *buf)
1536{
1537 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001538 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001539
1540 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001541 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001542 {
1543 chanpart_T *ch_part = &channel->ch_part[part];
1544
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001545 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001546 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001547 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001548 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001549 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001550 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001551 }
1552}
1553
1554/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001555 * Write any lines waiting to be written to "channel".
1556 */
1557 static void
1558channel_write_input(channel_T *channel)
1559{
1560 chanpart_T *in_part = &channel->ch_part[PART_IN];
1561
1562 if (in_part->ch_writeque.wq_next != NULL)
1563 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1564 else if (in_part->ch_bufref.br_buf != NULL)
1565 {
1566 if (in_part->ch_buf_append)
1567 channel_write_new_lines(in_part->ch_bufref.br_buf);
1568 else
1569 channel_write_in(channel);
1570 }
1571}
1572
1573/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001574 * Write any lines waiting to be written to a channel.
1575 */
1576 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001577channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001578{
1579 channel_T *channel;
1580
1581 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001582 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001583}
1584
1585/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001586 * Write appended lines above the last one in "buf" to the channel.
1587 */
1588 void
1589channel_write_new_lines(buf_T *buf)
1590{
1591 channel_T *channel;
1592 int found_one = FALSE;
1593
1594 /* There could be more than one channel for the buffer, loop over all of
1595 * them. */
1596 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1597 {
1598 chanpart_T *in_part = &channel->ch_part[PART_IN];
1599 linenr_T lnum;
1600 int written = 0;
1601
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001602 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001603 {
1604 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001605 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001606 found_one = TRUE;
1607 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1608 ++lnum)
1609 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001610 if (!can_write_buf_line(channel))
1611 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001612 write_buf_line(buf, lnum, channel);
1613 ++written;
1614 }
1615
1616 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001617 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001618 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001619 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001620 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001621 ch_log(channel, "Still %ld more lines to write",
1622 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001623
1624 in_part->ch_buf_bot = lnum;
1625 }
1626 }
1627 if (!found_one)
1628 buf->b_write_to_channel = FALSE;
1629}
1630
1631/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001632 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001633 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001634 */
1635 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001636invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1637 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001638{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001639 typval_T rettv;
1640 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001641
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001642 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001643 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001644
Bram Moolenaar77073442016-02-13 23:23:53 +01001645 argv[0].v_type = VAR_CHANNEL;
1646 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001647
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001648 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1649 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001650 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001651 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001652}
1653
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001654/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001655 * Return the first node from "channel"/"part" without removing it.
1656 * Returns NULL if there is nothing.
1657 */
1658 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001659channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001660{
1661 readq_T *head = &channel->ch_part[part].ch_head;
1662
1663 return head->rq_next;
1664}
1665
1666/*
1667 * Return a pointer to the first NL in "node".
1668 * Skips over NUL characters.
1669 * Returns NULL if there is no NL.
1670 */
1671 char_u *
1672channel_first_nl(readq_T *node)
1673{
1674 char_u *buffer = node->rq_buffer;
1675 long_u i;
1676
1677 for (i = 0; i < node->rq_buflen; ++i)
1678 if (buffer[i] == NL)
1679 return buffer + i;
1680 return NULL;
1681}
1682
1683/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001684 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001685 * The caller must free it.
1686 * Returns NULL if there is nothing.
1687 */
1688 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001689channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001690{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001691 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001692 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001693 char_u *p;
1694
Bram Moolenaar77073442016-02-13 23:23:53 +01001695 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 if (outlen != NULL)
1698 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001699 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001700 p = node->rq_buffer;
1701 head->rq_next = node->rq_next;
1702 if (node->rq_next == NULL)
1703 head->rq_prev = NULL;
1704 else
1705 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706 vim_free(node);
1707 return p;
1708}
1709
1710/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001711 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001712 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713 */
1714 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001716{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001717 readq_T *head = &channel->ch_part[part].ch_head;
1718 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001719 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001720 char_u *res;
1721 char_u *p;
1722
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001723 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001724 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001726 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001727 if (res == NULL)
1728 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001729 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001730 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001731 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001732 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001733 p += node->rq_buflen;
1734 }
1735 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001736
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001737 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001738 do
1739 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001741 vim_free(p);
1742 } while (p != NULL);
1743
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 if (outlen != NULL)
1745 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001746 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001747 *outlen += len;
1748 return res;
1749 }
1750
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001751 // Turn all NUL into NL, so that the result can be used as a string.
1752 p = res;
1753 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001754 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001755 if (*p == NUL)
1756 *p = NL;
1757#ifdef WIN32
1758 else if (*p == 0x1b)
1759 {
1760 // crush the escape sequence OSC 0/1/2: ESC ]0;
1761 if (p + 3 < res + len
1762 && p[1] == ']'
1763 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1764 && p[3] == ';')
1765 {
1766 // '\a' becomes a NL
1767 while (p < res + (len - 1) && *p != '\a')
1768 ++p;
1769 // BEL is zero width characters, suppress display mistake
1770 // ConPTY (after 10.0.18317) requires advance checking
1771 if (p[-1] == NUL)
1772 p[-1] = 0x07;
1773 }
1774 }
1775#endif
1776 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001777 }
1778
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001779 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001780}
1781
1782/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001783 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001784 * Caller must check these bytes are available.
1785 */
1786 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001787channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001788{
1789 readq_T *head = &channel->ch_part[part].ch_head;
1790 readq_T *node = head->rq_next;
1791 char_u *buf = node->rq_buffer;
1792
1793 mch_memmove(buf, buf + len, node->rq_buflen - len);
1794 node->rq_buflen -= len;
1795}
1796
1797/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001798 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001799 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001800 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801 */
1802 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001803channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001804{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001805 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001806 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001807 readq_T *last_node;
1808 readq_T *n;
1809 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001810 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001811 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001812
Bram Moolenaar77073442016-02-13 23:23:53 +01001813 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001814 return FAIL;
1815
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001816 last_node = node->rq_next;
1817 len = node->rq_buflen + last_node->rq_buflen + 1;
1818 if (want_nl)
1819 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001820 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001821 {
1822 last_node = last_node->rq_next;
1823 len += last_node->rq_buflen;
1824 }
1825
1826 p = newbuf = alloc(len);
1827 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001828 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001829 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001830 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001831 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001832 node->rq_buffer = newbuf;
1833 for (n = node; n != last_node; )
1834 {
1835 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001836 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001837 p += n->rq_buflen;
1838 vim_free(n->rq_buffer);
1839 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001840 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001841
1842 /* dispose of the collapsed nodes and their buffers */
1843 for (n = node->rq_next; n != last_node; )
1844 {
1845 n = n->rq_next;
1846 vim_free(n->rq_prev);
1847 }
1848 node->rq_next = last_node->rq_next;
1849 if (last_node->rq_next == NULL)
1850 head->rq_prev = node;
1851 else
1852 last_node->rq_next->rq_prev = node;
1853 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001854 return OK;
1855}
1856
1857/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001859 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001860 * Returns OK or FAIL.
1861 */
1862 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001863channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001864 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001865{
1866 readq_T *node;
1867 readq_T *head = &channel->ch_part[part].ch_head;
1868 char_u *p;
1869 int i;
1870
1871 node = (readq_T *)alloc(sizeof(readq_T));
1872 if (node == NULL)
1873 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001874 /* A NUL is added at the end, because netbeans code expects that.
1875 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001876 node->rq_buffer = alloc(len + 1);
1877 if (node->rq_buffer == NULL)
1878 {
1879 vim_free(node);
1880 return FAIL; /* out of memory */
1881 }
1882
1883 if (channel->ch_part[part].ch_mode == MODE_NL)
1884 {
1885 /* Drop any CR before a NL. */
1886 p = node->rq_buffer;
1887 for (i = 0; i < len; ++i)
1888 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1889 *p++ = buf[i];
1890 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001891 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001892 }
1893 else
1894 {
1895 mch_memmove(node->rq_buffer, buf, len);
1896 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001897 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001898 }
1899
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001900 if (prepend)
1901 {
1902 /* preend node to the head of the queue */
1903 node->rq_next = head->rq_next;
1904 node->rq_prev = NULL;
1905 if (head->rq_next == NULL)
1906 head->rq_prev = node;
1907 else
1908 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001909 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001911 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001912 {
1913 /* append node to the tail of the queue */
1914 node->rq_next = NULL;
1915 node->rq_prev = head->rq_prev;
1916 if (head->rq_prev == NULL)
1917 head->rq_next = node;
1918 else
1919 head->rq_prev->rq_next = node;
1920 head->rq_prev = node;
1921 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001922
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001923 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001924 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001925 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001926 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001927 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001928 fprintf(log_fd, "'\n");
1929 }
1930 return OK;
1931}
1932
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001933/*
1934 * Try to fill the buffer of "reader".
1935 * Returns FALSE when nothing was added.
1936 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001937 static int
1938channel_fill(js_read_T *reader)
1939{
1940 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001941 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001942 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001943 int keeplen;
1944 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001945 char_u *p;
1946
1947 if (next == NULL)
1948 return FALSE;
1949
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001950 keeplen = reader->js_end - reader->js_buf;
1951 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001952 {
1953 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001954 addlen = (int)STRLEN(next);
1955 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001956 if (p == NULL)
1957 {
1958 vim_free(next);
1959 return FALSE;
1960 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001961 mch_memmove(p, reader->js_buf, keeplen);
1962 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001963 vim_free(next);
1964 next = p;
1965 }
1966
1967 vim_free(reader->js_buf);
1968 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001969 return TRUE;
1970}
1971
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001972/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001973 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001974 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001975 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001976 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001977 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001978channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001979{
1980 js_read_T reader;
1981 typval_T listtv;
1982 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001983 chanpart_T *chanpart = &channel->ch_part[part];
1984 jsonq_T *head = &chanpart->ch_json_head;
1985 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001986 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001987
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001988 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001989 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001990
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001991 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001992 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001993 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001994 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001995 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001996
1997 /* When a message is incomplete we wait for a short while for more to
1998 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001999 * or list will make us hang.
2000 * Do not generate error messages, they will be written in a channel log. */
2001 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002002 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002003 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002004 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002005 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002006 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002007 /* Only accept the response when it is a list with at least two
2008 * items. */
2009 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002010 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002011 if (listtv.v_type != VAR_LIST)
2012 ch_error(channel, "Did not receive a list, discarding");
2013 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002014 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002015 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002017 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002018 else
2019 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002020 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2021 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002022 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002023 else
2024 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002025 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002026 item->jq_value = alloc_tv();
2027 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002028 {
2029 vim_free(item);
2030 clear_tv(&listtv);
2031 }
2032 else
2033 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002034 *item->jq_value = listtv;
2035 item->jq_prev = head->jq_prev;
2036 head->jq_prev = item;
2037 item->jq_next = NULL;
2038 if (item->jq_prev == NULL)
2039 head->jq_next = item;
2040 else
2041 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002042 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002043 }
2044 }
2045 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002046
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002047 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002048 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002049 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002050 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002051 size_t buflen = STRLEN(reader.js_buf);
2052
2053 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002054 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002055 /* First time encountering incomplete message or after receiving
2056 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002057 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002058 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002059 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002060 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002061 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002062#ifdef WIN32
2063 chanpart->ch_deadline = GetTickCount() + 100L;
2064#else
2065 gettimeofday(&chanpart->ch_deadline, NULL);
2066 chanpart->ch_deadline.tv_usec += 100 * 1000;
2067 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2068 {
2069 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2070 ++chanpart->ch_deadline.tv_sec;
2071 }
2072#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002073 }
2074 else
2075 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002076 int timeout;
2077#ifdef WIN32
2078 timeout = GetTickCount() > chanpart->ch_deadline;
2079#else
2080 {
2081 struct timeval now_tv;
2082
2083 gettimeofday(&now_tv, NULL);
2084 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2085 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2086 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2087 }
2088#endif
2089 if (timeout)
2090 {
2091 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002092 chanpart->ch_wait_len = 0;
2093 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002094 }
2095 else
2096 {
2097 reader.js_used = 0;
2098 ch_log(channel, "still waiting on incomplete message");
2099 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002100 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002101 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002102
2103 if (status == FAIL)
2104 {
2105 ch_error(channel, "Decoding failed - discarding input");
2106 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002107 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002108 }
2109 else if (reader.js_buf[reader.js_used] != NUL)
2110 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002111 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002112 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002113 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2114 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002115 ret = status == MAYBE ? FALSE: TRUE;
2116 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002117 else
2118 ret = FALSE;
2119
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002120 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002121 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002122}
2123
2124/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002125 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002126 */
2127 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002128remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002129{
Bram Moolenaar77073442016-02-13 23:23:53 +01002130 if (node->cq_prev == NULL)
2131 head->cq_next = node->cq_next;
2132 else
2133 node->cq_prev->cq_next = node->cq_next;
2134 if (node->cq_next == NULL)
2135 head->cq_prev = node->cq_prev;
2136 else
2137 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002138}
2139
2140/*
2141 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002142 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002143 */
2144 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002145remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002146{
Bram Moolenaar77073442016-02-13 23:23:53 +01002147 if (node->jq_prev == NULL)
2148 head->jq_next = node->jq_next;
2149 else
2150 node->jq_prev->jq_next = node->jq_next;
2151 if (node->jq_next == NULL)
2152 head->jq_prev = node->jq_prev;
2153 else
2154 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002155 vim_free(node);
2156}
2157
2158/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002159 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002160 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002161 * When "id" is zero or negative jut get the first message. But not the one
2162 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002163 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002164 * Return OK when found and return the value in "rettv".
2165 * Return FAIL otherwise.
2166 */
2167 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002168channel_get_json(
2169 channel_T *channel,
2170 ch_part_T part,
2171 int id,
2172 int without_callback,
2173 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002174{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002175 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002176 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002177
Bram Moolenaar77073442016-02-13 23:23:53 +01002178 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002179 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002180 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002181 typval_T *tv = &l->lv_first->li_tv;
2182
Bram Moolenaar958dc692016-12-01 15:34:12 +01002183 if ((without_callback || !item->jq_no_callback)
2184 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002185 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002186 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002187 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002188 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002189 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002190 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002191 ch_log(channel, "Getting JSON message %ld",
2192 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002193 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002194 return OK;
2195 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002196 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002197 }
2198 return FAIL;
2199}
2200
Bram Moolenaar958dc692016-12-01 15:34:12 +01002201/*
2202 * Put back "rettv" into the JSON queue, there was no callback for it.
2203 * Takes over the values in "rettv".
2204 */
2205 static void
2206channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2207{
2208 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2209 jsonq_T *item = head->jq_next;
2210 jsonq_T *newitem;
2211
2212 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2213 /* last item was pushed back, append to the end */
2214 item = NULL;
2215 else while (item != NULL && item->jq_no_callback)
2216 /* append after the last item that was pushed back */
2217 item = item->jq_next;
2218
2219 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2220 if (newitem == NULL)
2221 clear_tv(rettv);
2222 else
2223 {
2224 newitem->jq_value = alloc_tv();
2225 if (newitem->jq_value == NULL)
2226 {
2227 vim_free(newitem);
2228 clear_tv(rettv);
2229 }
2230 else
2231 {
2232 newitem->jq_no_callback = FALSE;
2233 *newitem->jq_value = *rettv;
2234 if (item == NULL)
2235 {
2236 /* append to the end */
2237 newitem->jq_prev = head->jq_prev;
2238 head->jq_prev = newitem;
2239 newitem->jq_next = NULL;
2240 if (newitem->jq_prev == NULL)
2241 head->jq_next = newitem;
2242 else
2243 newitem->jq_prev->jq_next = newitem;
2244 }
2245 else
2246 {
2247 /* append after "item" */
2248 newitem->jq_prev = item;
2249 newitem->jq_next = item->jq_next;
2250 item->jq_next = newitem;
2251 if (newitem->jq_next == NULL)
2252 head->jq_prev = newitem;
2253 else
2254 newitem->jq_next->jq_prev = newitem;
2255 }
2256 }
2257 }
2258}
2259
Bram Moolenaarece61b02016-02-20 21:39:05 +01002260#define CH_JSON_MAX_ARGS 4
2261
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002262/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002263 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 * "argv[0]" is the command string.
2265 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002266 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002267 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002268channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002269{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002270 char_u *cmd = argv[0].vval.v_string;
2271 char_u *arg;
2272 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002273
Bram Moolenaarece61b02016-02-20 21:39:05 +01002274 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002275 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002276 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002277 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002278 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002279 return;
2280 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002281 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002282 if (arg == NULL)
2283 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002284
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002285 if (STRCMP(cmd, "ex") == 0)
2286 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002287 int save_called_emsg = called_emsg;
2288
2289 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002290 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002291 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002292 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002293 --emsg_silent;
2294 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002295 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002296 (char *)get_vim_var_str(VV_ERRMSG));
2297 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002298 }
2299 else if (STRCMP(cmd, "normal") == 0)
2300 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002301 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002302
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002303 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002304 ea.arg = arg;
2305 ea.addr_count = 0;
2306 ea.forceit = TRUE; /* no mapping */
2307 ex_normal(&ea);
2308 }
2309 else if (STRCMP(cmd, "redraw") == 0)
2310 {
2311 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002312
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002313 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002314 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002315 ex_redraw(&ea);
2316 showruler(FALSE);
2317 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002318 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002319 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002320 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002321 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002322 int is_call = cmd[0] == 'c';
2323 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002324
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 if (argv[id_idx].v_type != VAR_UNKNOWN
2326 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002327 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002328 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002329 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331 }
2332 else if (is_call && argv[2].v_type != VAR_LIST)
2333 {
2334 ch_error(channel, "third argument for call must be a list");
2335 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002336 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002337 }
2338 else
2339 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002340 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002341 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002342 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002343 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002344
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002345 /* Don't pollute the display with errors. */
2346 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002347 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002348 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002349 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002350 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002351 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002352 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002353 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002354 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002355 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2356 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002357 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002358
2359 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002360 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002361 int id = argv[id_idx].vval.v_number;
2362
Bram Moolenaar55fab432016-02-07 16:53:13 +01002363 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002364 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002365 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002366 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002367 /* If evaluation failed or the result can't be encoded
2368 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002369 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002370 err_tv.v_type = VAR_STRING;
2371 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002372 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002373 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002374 if (json != NULL)
2375 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002376 channel_send(channel,
2377 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002378 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002379 vim_free(json);
2380 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002381 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002382 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002383 if (tv == &res_tv)
2384 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002385 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002386 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002387 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002388 }
2389 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002390 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002391 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002392 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002393 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002394}
2395
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002396/*
2397 * Invoke the callback at "cbhead".
2398 * Does not redraw but sets channel_need_redraw.
2399 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002400 static void
2401invoke_one_time_callback(
2402 channel_T *channel,
2403 cbq_T *cbhead,
2404 cbq_T *item,
2405 typval_T *argv)
2406{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002407 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002408 (char *)item->cq_callback);
2409 /* Remove the item from the list first, if the callback
2410 * invokes ch_close() the list will be cleared. */
2411 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002412 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002413 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002414 vim_free(item);
2415}
2416
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002417 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002418append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002419{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002420 bufref_T save_curbuf = {NULL, 0, 0};
2421 win_T *save_curwin = NULL;
2422 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002423 linenr_T lnum = buffer->b_ml.ml_line_count;
2424 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002425 chanpart_T *ch_part = &channel->ch_part[part];
2426 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002427 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002428
2429 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2430 {
2431 if (!ch_part->ch_nomod_error)
2432 {
2433 ch_error(channel, "Buffer is not modifiable, cannot append");
2434 ch_part->ch_nomod_error = TRUE;
2435 }
2436 return;
2437 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002438
2439 /* If the buffer is also used as input insert above the last
2440 * line. Don't write these lines. */
2441 if (save_write_to)
2442 {
2443 --lnum;
2444 buffer->b_write_to_channel = FALSE;
2445 }
2446
2447 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002448 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002449
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002450 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002451
2452 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2453 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2454
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002455 u_sync(TRUE);
2456 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002457 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002458
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002459 if (empty)
2460 {
2461 /* The buffer is empty, replace the first (dummy) line. */
2462 ml_replace(lnum, msg, TRUE);
2463 lnum = 0;
2464 }
2465 else
2466 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002467 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002468
2469 /* Restore curbuf/curwin/curtab */
2470 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2471
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002472 if (ch_part->ch_nomodifiable)
2473 buffer->b_p_ma = FALSE;
2474 else
2475 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002476
2477 if (buffer->b_nwindows > 0)
2478 {
2479 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002480
2481 FOR_ALL_WINDOWS(wp)
2482 {
2483 if (wp->w_buffer == buffer
2484 && (save_write_to
2485 ? wp->w_cursor.lnum == lnum + 1
2486 : (wp->w_cursor.lnum == lnum
2487 && wp->w_cursor.col == 0)))
2488 {
2489 ++wp->w_cursor.lnum;
2490 save_curwin = curwin;
2491 curwin = wp;
2492 curbuf = curwin->w_buffer;
2493 scroll_cursor_bot(0, FALSE);
2494 curwin = save_curwin;
2495 curbuf = curwin->w_buffer;
2496 }
2497 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002498 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002499 channel_need_redraw = TRUE;
2500 }
2501
2502 if (save_write_to)
2503 {
2504 channel_T *ch;
2505
2506 /* Find channels reading from this buffer and adjust their
2507 * next-to-read line number. */
2508 buffer->b_write_to_channel = TRUE;
2509 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2510 {
2511 chanpart_T *in_part = &ch->ch_part[PART_IN];
2512
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002513 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002514 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2515 }
2516 }
2517}
2518
Bram Moolenaar437905c2016-04-26 19:01:05 +02002519 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002520drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002521{
2522 char_u *msg;
2523
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002524 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002525 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002526 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002527 vim_free(msg);
2528 }
2529}
2530
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002531/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002532 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002533 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002534 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002535 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002536 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002537may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002538{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002539 char_u *msg = NULL;
2540 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002541 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002542 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002543 chanpart_T *ch_part = &channel->ch_part[part];
2544 ch_mode_T ch_mode = ch_part->ch_mode;
2545 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002546 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002547 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002548 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002549 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002550 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002551
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002552 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002553 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002554 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002555
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002556 /* Use a message-specific callback, part callback or channel callback */
2557 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2558 if (cbitem->cq_seq_nr == 0)
2559 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002560 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002561 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002562 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002563 partial = cbitem->cq_partial;
2564 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002565 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002566 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002567 callback = ch_part->ch_callback;
2568 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002569 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002570 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002571 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002572 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002573 partial = channel->ch_partial;
2574 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002575
Bram Moolenaarec68a992016-10-03 21:37:41 +02002576 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002577 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2578 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002579 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002580 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002581 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002582 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002583 buffer = NULL;
2584 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002585
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002586 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002587 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002588 listitem_T *item;
2589 int argc = 0;
2590
Bram Moolenaard7ece102016-02-02 23:23:02 +01002591 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002592 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002593 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002594 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002595 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002596 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002597 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002598 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002599
Bram Moolenaarece61b02016-02-20 21:39:05 +01002600 for (item = listtv->vval.v_list->lv_first;
2601 item != NULL && argc < CH_JSON_MAX_ARGS;
2602 item = item->li_next)
2603 argv[argc++] = item->li_tv;
2604 while (argc < CH_JSON_MAX_ARGS)
2605 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002606
Bram Moolenaarece61b02016-02-20 21:39:05 +01002607 if (argv[0].v_type == VAR_STRING)
2608 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002609 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002610 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002611 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002612 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002613 }
2614
Bram Moolenaarece61b02016-02-20 21:39:05 +01002615 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002616 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002617 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002618 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002619 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002620 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002621 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002622 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002623 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002624 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002625 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002626 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002627 return FALSE;
2628 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002629 else
2630 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002631 /* If there is no callback or buffer drop the message. */
2632 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002633 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002634 /* If there is a close callback it may use ch_read() to get the
2635 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002636 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002637 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002638 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002639 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002640
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002641 if (ch_mode == MODE_NL)
2642 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002643 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002644 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002645 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002646
2647 /* See if we have a message ending in NL in the first buffer. If
2648 * not try to concatenate the first and the second buffer. */
2649 while (TRUE)
2650 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002651 node = channel_peek(channel, part);
2652 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002653 if (nl != NULL)
2654 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002655 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002656 {
2657 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2658 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002659 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002660 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002661 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002662 buf = node->rq_buffer;
2663
Bram Moolenaarec68a992016-10-03 21:37:41 +02002664 if (nl == NULL)
2665 {
2666 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002667 char_u *new_buf;
2668
2669 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2670 if (new_buf == NULL)
2671 /* This might fail over and over again, should the message
2672 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002673 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002674 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002675 node->rq_buffer = buf;
2676 nl = buf + node->rq_buflen++;
2677 *nl = NUL;
2678 }
2679
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002680 /* Convert NUL to NL, the internal representation. */
2681 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2682 if (*p == NUL)
2683 *p = NL;
2684
2685 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002686 {
2687 /* get the whole buffer, drop the NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002688 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002689 *nl = NUL;
2690 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002691 else
2692 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002693 /* Copy the message into allocated memory (excluding the NL)
2694 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002695 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002696 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002697 }
2698 }
2699 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002700 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002701 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002702 * get everything we have.
2703 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002704 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002705 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002706
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002707 if (msg == NULL)
2708 return FALSE; /* out of memory (and avoids Coverity warning) */
2709
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002710 argv[1].v_type = VAR_STRING;
2711 argv[1].vval.v_string = msg;
2712 }
2713
Bram Moolenaara07fec92016-02-05 21:04:08 +01002714 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002715 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002716 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002717
Bram Moolenaar958dc692016-12-01 15:34:12 +01002718 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002719 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002720 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002721 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002722 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002723 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002724 break;
2725 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002726 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002727 {
2728 if (channel->ch_drop_never)
2729 {
2730 /* message must be read with ch_read() */
2731 channel_push_json(channel, part, listtv);
2732 listtv = NULL;
2733 }
2734 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002735 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002736 seq_nr);
2737 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002738 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002739 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002740 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002741 if (buffer != NULL)
2742 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002743 if (msg == NULL)
2744 /* JSON or JS mode: re-encode the message. */
2745 msg = json_encode(listtv, ch_mode);
2746 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002747 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002748#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002749 if (buffer->b_term != NULL)
2750 write_to_term(buffer, msg, channel);
2751 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002752#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002753 append_to_buffer(buffer, msg, channel, part);
2754 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002755 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002756
Bram Moolenaar187db502016-02-27 14:44:26 +01002757 if (callback != NULL)
2758 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002759 if (cbitem != NULL)
2760 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2761 else
2762 {
2763 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002764 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002765 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002766 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002767 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002768 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002769 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002770 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002771 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002772
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002773 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002774 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002775 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002776
2777 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002778}
2779
Bram Moolenaar113e1072019-01-20 15:30:40 +01002780#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002781/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002782 * Return TRUE when channel "channel" is open for writing to.
2783 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002784 */
2785 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002786channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002787{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002788 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002789 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002790}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002791#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002792
2793/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002794 * Return TRUE when channel "channel" is open for reading or writing.
2795 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002796 */
2797 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002798channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002799{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002800 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002801 || channel->CH_IN_FD != INVALID_FD
2802 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002803 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002804}
2805
2806/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002807 * Return TRUE if "channel" has JSON or other typeahead.
2808 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002809 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002810channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002811{
2812 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2813
2814 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2815 {
2816 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2817 jsonq_T *item = head->jq_next;
2818
2819 return item != NULL;
2820 }
2821 return channel_peek(channel, part) != NULL;
2822}
2823
2824/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002825 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002826 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002827 */
2828 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002829channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002830{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002831 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002832 int has_readahead = FALSE;
2833
Bram Moolenaar77073442016-02-13 23:23:53 +01002834 if (channel == NULL)
2835 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002836 if (req_part == PART_OUT)
2837 {
2838 if (channel->CH_OUT_FD != INVALID_FD)
2839 return "open";
2840 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002841 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002842 }
2843 else if (req_part == PART_ERR)
2844 {
2845 if (channel->CH_ERR_FD != INVALID_FD)
2846 return "open";
2847 if (channel_has_readahead(channel, PART_ERR))
2848 has_readahead = TRUE;
2849 }
2850 else
2851 {
2852 if (channel_is_open(channel))
2853 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002854 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002855 if (channel_has_readahead(channel, part))
2856 {
2857 has_readahead = TRUE;
2858 break;
2859 }
2860 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002861
2862 if (has_readahead)
2863 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002864 return "closed";
2865}
2866
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002867 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002868channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002869{
2870 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002871 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002872 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002873 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002874 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002875
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002876 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002877 STRCAT(namebuf, "_");
2878 tail = STRLEN(namebuf);
2879
2880 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002881 if (chanpart->ch_fd != INVALID_FD)
2882 status = "open";
2883 else if (channel_has_readahead(channel, part))
2884 status = "buffered";
2885 else
2886 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002887 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002888
2889 STRCPY(namebuf + tail, "mode");
2890 switch (chanpart->ch_mode)
2891 {
2892 case MODE_NL: s = "NL"; break;
2893 case MODE_RAW: s = "RAW"; break;
2894 case MODE_JSON: s = "JSON"; break;
2895 case MODE_JS: s = "JS"; break;
2896 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002897 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002898
2899 STRCPY(namebuf + tail, "io");
2900 if (part == PART_SOCK)
2901 s = "socket";
2902 else switch (chanpart->ch_io)
2903 {
2904 case JIO_NULL: s = "null"; break;
2905 case JIO_PIPE: s = "pipe"; break;
2906 case JIO_FILE: s = "file"; break;
2907 case JIO_BUFFER: s = "buffer"; break;
2908 case JIO_OUT: s = "out"; break;
2909 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002910 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002911
2912 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002913 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002914}
2915
2916 void
2917channel_info(channel_T *channel, dict_T *dict)
2918{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002919 dict_add_number(dict, "id", channel->ch_id);
2920 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002921
2922 if (channel->ch_hostname != NULL)
2923 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002924 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2925 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002926 channel_part_info(channel, dict, "sock", PART_SOCK);
2927 }
2928 else
2929 {
2930 channel_part_info(channel, dict, "out", PART_OUT);
2931 channel_part_info(channel, dict, "err", PART_ERR);
2932 channel_part_info(channel, dict, "in", PART_IN);
2933 }
2934}
2935
Bram Moolenaar77073442016-02-13 23:23:53 +01002936/*
2937 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002938 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002939 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002940 */
2941 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002942channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002943{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002944 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002945
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002946#ifdef FEAT_GUI
2947 channel_gui_unregister(channel);
2948#endif
2949
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002950 ch_close_part(channel, PART_SOCK);
2951 ch_close_part(channel, PART_IN);
2952 ch_close_part(channel, PART_OUT);
2953 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002954
Bram Moolenaara9f02812017-08-05 17:40:38 +02002955 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002956 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002957 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002958
Bram Moolenaara9f02812017-08-05 17:40:38 +02002959 /* Invoke callbacks and flush buffers before the close callback. */
2960 if (channel->ch_close_cb != NULL)
2961 ch_log(channel,
2962 "Invoking callbacks and flushing buffers before closing");
2963 for (part = PART_SOCK; part < PART_IN; ++part)
2964 {
2965 if (channel->ch_close_cb != NULL
2966 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2967 {
2968 /* Increment the refcount to avoid the channel being freed
2969 * halfway. */
2970 ++channel->ch_refcount;
2971 if (channel->ch_close_cb == NULL)
2972 ch_log(channel, "flushing %s buffers before closing",
2973 part_names[part]);
2974 while (may_invoke_callback(channel, part))
2975 ;
2976 --channel->ch_refcount;
2977 }
2978 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002979
Bram Moolenaara9f02812017-08-05 17:40:38 +02002980 if (channel->ch_close_cb != NULL)
2981 {
2982 typval_T argv[1];
2983 typval_T rettv;
2984 int dummy;
2985
2986 /* Increment the refcount to avoid the channel being freed
2987 * halfway. */
2988 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002989 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002990 (char *)channel->ch_close_cb);
2991 argv[0].v_type = VAR_CHANNEL;
2992 argv[0].vval.v_channel = channel;
2993 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002994 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002995 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002996 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002997 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002998
Bram Moolenaara9f02812017-08-05 17:40:38 +02002999 /* the callback is only called once */
3000 free_callback(channel->ch_close_cb, channel->ch_close_partial);
3001 channel->ch_close_cb = NULL;
3002 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003003
Bram Moolenaara9f02812017-08-05 17:40:38 +02003004 if (channel_need_redraw)
3005 {
3006 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003007 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003008 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003009
Bram Moolenaara9f02812017-08-05 17:40:38 +02003010 if (!channel->ch_drop_never)
3011 /* any remaining messages are useless now */
3012 for (part = PART_SOCK; part < PART_IN; ++part)
3013 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003014
3015 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003016 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003017 }
3018
3019 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003020
3021#ifdef FEAT_TERMINAL
3022 term_channel_closed(channel);
3023#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003024}
3025
Bram Moolenaard04a0202016-01-26 23:30:18 +01003026/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003027 * Close the "in" part channel "channel".
3028 */
3029 void
3030channel_close_in(channel_T *channel)
3031{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003032 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003033}
3034
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003035 static void
3036remove_from_writeque(writeq_T *wq, writeq_T *entry)
3037{
3038 ga_clear(&entry->wq_ga);
3039 wq->wq_next = entry->wq_next;
3040 if (wq->wq_next == NULL)
3041 wq->wq_prev = NULL;
3042 else
3043 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003044 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003045}
3046
Bram Moolenaar0874a832016-09-01 15:11:51 +02003047/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003048 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003049 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003050 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003051channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003052{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003053 chanpart_T *ch_part = &channel->ch_part[part];
3054 jsonq_T *json_head = &ch_part->ch_json_head;
3055 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003056
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003057 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003058 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003059
3060 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003061 {
3062 cbq_T *node = cb_head->cq_next;
3063
3064 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003065 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003066 vim_free(node);
3067 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003068
3069 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003070 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003071 free_tv(json_head->jq_next->jq_value);
3072 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003073 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003074
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003075 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3076 ch_part->ch_callback = NULL;
3077 ch_part->ch_partial = NULL;
3078
3079 while (ch_part->ch_writeque.wq_next != NULL)
3080 remove_from_writeque(&ch_part->ch_writeque,
3081 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003082}
3083
3084/*
3085 * Clear all the read buffers on "channel".
3086 */
3087 void
3088channel_clear(channel_T *channel)
3089{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003090 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003091 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003092 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003093 channel_clear_one(channel, PART_OUT);
3094 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003095 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003096 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003097 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003098 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003099 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003100 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003101 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003102}
3103
Bram Moolenaar77073442016-02-13 23:23:53 +01003104#if defined(EXITFREE) || defined(PROTO)
3105 void
3106channel_free_all(void)
3107{
3108 channel_T *channel;
3109
Bram Moolenaard6051b52016-02-28 15:49:03 +01003110 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003111 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3112 channel_clear(channel);
3113}
3114#endif
3115
3116
Bram Moolenaar715d2852016-04-30 17:06:31 +02003117/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003118#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003119
3120/* Buffer size for reading incoming messages. */
3121#define MAXMSGSIZE 4096
3122
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003123#if defined(HAVE_SELECT)
3124/*
3125 * Add write fds where we are waiting for writing to be possible.
3126 */
3127 static int
3128channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3129{
3130 int maxfd = maxfd_arg;
3131 channel_T *ch;
3132
3133 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3134 {
3135 chanpart_T *in_part = &ch->ch_part[PART_IN];
3136
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003137 if (in_part->ch_fd != INVALID_FD
3138 && (in_part->ch_bufref.br_buf != NULL
3139 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003140 {
3141 FD_SET((int)in_part->ch_fd, wfds);
3142 if ((int)in_part->ch_fd >= maxfd)
3143 maxfd = (int)in_part->ch_fd + 1;
3144 }
3145 }
3146 return maxfd;
3147}
3148#else
3149/*
3150 * Add write fds where we are waiting for writing to be possible.
3151 */
3152 static int
3153channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3154{
3155 int nfd = nfd_in;
3156 channel_T *ch;
3157
3158 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3159 {
3160 chanpart_T *in_part = &ch->ch_part[PART_IN];
3161
Bram Moolenaar683b7962017-08-19 15:51:59 +02003162 if (in_part->ch_fd != INVALID_FD
3163 && (in_part->ch_bufref.br_buf != NULL
3164 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003165 {
3166 in_part->ch_poll_idx = nfd;
3167 fds[nfd].fd = in_part->ch_fd;
3168 fds[nfd].events = POLLOUT;
3169 ++nfd;
3170 }
3171 else
3172 in_part->ch_poll_idx = -1;
3173 }
3174 return nfd;
3175}
3176#endif
3177
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003178typedef enum {
3179 CW_READY,
3180 CW_NOT_READY,
3181 CW_ERROR
3182} channel_wait_result;
3183
Bram Moolenaard04a0202016-01-26 23:30:18 +01003184/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003185 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003186 * Return CW_READY when there is something to read.
3187 * Return CW_NOT_READY when there is nothing to read.
3188 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003189 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003190 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003191channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003192{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003193 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003194 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003195
Bram Moolenaard8070362016-02-15 21:56:54 +01003196# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003197 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003198 {
3199 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003200 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003201 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003202 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003203
3204 /* reading from a pipe, not a socket */
3205 while (TRUE)
3206 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003207 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3208
3209 if (r && nread > 0)
3210 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003211
3212 if (channel->ch_named_pipe)
3213 {
3214 DisconnectNamedPipe((HANDLE)fd);
3215 ConnectNamedPipe((HANDLE)fd, NULL);
3216 }
3217 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003218 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003219
3220 /* perhaps write some buffer lines */
3221 channel_write_any_lines();
3222
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003223 sleep_time = deadline - GetTickCount();
3224 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003225 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003226 /* Wait for a little while. Very short at first, up to 10 msec
3227 * after looping a few times. */
3228 if (sleep_time > delay)
3229 sleep_time = delay;
3230 Sleep(sleep_time);
3231 delay = delay * 2;
3232 if (delay > 10)
3233 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003234 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003235 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003236 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003237#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003238 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003239#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003240 struct timeval tval;
3241 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003242 fd_set wfds;
3243 int ret;
3244 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003245
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003246 tval.tv_sec = timeout / 1000;
3247 tval.tv_usec = (timeout % 1000) * 1000;
3248 for (;;)
3249 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003250 FD_ZERO(&rfds);
3251 FD_SET((int)fd, &rfds);
3252
3253 /* Write lines to a pipe when a pipe can be written to. Need to
3254 * set this every time, some buffers may be done. */
3255 maxfd = (int)fd + 1;
3256 FD_ZERO(&wfds);
3257 maxfd = channel_fill_wfds(maxfd, &wfds);
3258
3259 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003260# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003261 SOCK_ERRNO;
3262 if (ret == -1 && errno == EINTR)
3263 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003264# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003265 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003266 {
3267 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003268 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003269 channel_write_any_lines();
3270 continue;
3271 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003272 break;
3273 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003274#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003275 for (;;)
3276 {
3277 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3278 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003279
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003280 fds[0].fd = fd;
3281 fds[0].events = POLLIN;
3282 nfd = channel_fill_poll_write(nfd, fds);
3283 if (poll(fds, nfd, timeout) > 0)
3284 {
3285 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003286 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003287 channel_write_any_lines();
3288 continue;
3289 }
3290 break;
3291 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003292#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003293 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003294 return CW_NOT_READY;
3295}
3296
3297 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003298ch_close_part_on_error(
3299 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003300{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003301 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003302
3303 if (is_err)
3304 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003305 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003306 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003307 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003308
3309 /* Queue a "DETACH" netbeans message in the command queue in order to
3310 * terminate the netbeans session later. Do not end the session here
3311 * directly as we may be running in the context of a call to
3312 * netbeans_parse_messages():
3313 * netbeans_parse_messages
3314 * -> autocmd triggered while processing the netbeans cmd
3315 * -> ui_breakcheck
3316 * -> gui event loop or select loop
3317 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003318 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003319 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003320 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003321 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003322 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3323
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003324 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003325 * close the channel yet, there may be something to read on another part.
3326 * When stdout and stderr use the same FD we get the error only on one of
3327 * them, also close the other. */
3328 if (part == PART_OUT || part == PART_ERR)
3329 {
3330 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3331
3332 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3333 ch_close_part(channel, other);
3334 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003335 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003336
3337#ifdef FEAT_GUI
3338 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003339 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003340#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003341}
3342
3343 static void
3344channel_close_now(channel_T *channel)
3345{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003346 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003347 if (channel->ch_nb_close_cb != NULL)
3348 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003349 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003350}
3351
3352/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003353 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003354 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003355 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003356 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003357 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003358channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003359{
3360 static char_u *buf = NULL;
3361 int len = 0;
3362 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003363 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003364 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003365
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003366 fd = channel->ch_part[part].ch_fd;
3367 if (fd == INVALID_FD)
3368 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003369 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003370 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003371 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003372 }
3373 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003374
3375 /* Allocate a buffer to read into. */
3376 if (buf == NULL)
3377 {
3378 buf = alloc(MAXMSGSIZE);
3379 if (buf == NULL)
3380 return; /* out of memory! */
3381 }
3382
3383 /* Keep on reading for as long as there is something to read.
3384 * Use select() or poll() to avoid blocking on a message that is exactly
3385 * MAXMSGSIZE long. */
3386 for (;;)
3387 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003388 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003389 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003390 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003391 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003392 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003393 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003394 if (len <= 0)
3395 break; /* error or nothing more to read */
3396
3397 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003398 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003399 readlen += len;
3400 if (len < MAXMSGSIZE)
3401 break; /* did read everything that's available */
3402 }
3403
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003404 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003405 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003406 {
3407 if (!channel->ch_keep_open)
3408 ch_close_part_on_error(channel, part, (len < 0), func);
3409 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003410#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003411 else if (CH_HAS_GUI && gtk_main_level() > 0)
3412 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003413 gtk_main_quit();
3414#endif
3415}
3416
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003417/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003418 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003419 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003420 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003421 * Returns what was read in allocated memory.
3422 * Returns NULL in case of error or timeout.
3423 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003424 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003425channel_read_block(
3426 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003427{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003428 char_u *buf;
3429 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003430 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003431 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003432 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003433 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003434
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003435 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003436 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003437
3438 while (TRUE)
3439 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003440 node = channel_peek(channel, part);
3441 if (node != NULL)
3442 {
3443 if (mode == MODE_RAW || (mode == MODE_NL
3444 && channel_first_nl(node) != NULL))
3445 /* got a complete message */
3446 break;
3447 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3448 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003449 /* If not blocking or nothing more is coming then return what we
3450 * have. */
3451 if (raw || fd == INVALID_FD)
3452 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003453 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003454
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003455 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003456 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003457 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003458 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003459 {
3460 ch_log(channel, "Timed out");
3461 return NULL;
3462 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003463 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003464 }
3465
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003466 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003467 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003468 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003469 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003470 }
3471 else
3472 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003473 char_u *p;
3474
3475 buf = node->rq_buffer;
3476 nl = channel_first_nl(node);
3477
3478 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003479 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003480 if (*p == NUL)
3481 *p = NL;
3482
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003483 if (nl == NULL)
3484 {
3485 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003486 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003487 }
3488 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003489 {
3490 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003491 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003492 *nl = NUL;
3493 }
3494 else
3495 {
3496 /* Copy the message into allocated memory and remove it from the
3497 * buffer. */
3498 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003499 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003500 }
3501 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003502 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003503 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003504 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003505}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003506
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003507/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003508 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003509 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003510 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003511 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003512 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003513 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003514channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003515 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003516 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003517 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003518 int id,
3519 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003520{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003521 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003522 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003523 int timeout;
3524 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003525
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003526 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003527 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003528 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003529 for (;;)
3530 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003531 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003532
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003533 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003534 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003535 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003536 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003537 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003538 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003539
Bram Moolenaard7ece102016-02-02 23:23:02 +01003540 if (!more)
3541 {
3542 /* Handle any other messages in the queue. If done some more
3543 * messages may have arrived. */
3544 if (channel_parse_messages())
3545 continue;
3546
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003547 /* Wait for up to the timeout. If there was an incomplete message
3548 * use the deadline for that. */
3549 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003550 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003551 {
3552#ifdef WIN32
3553 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3554#else
3555 {
3556 struct timeval now_tv;
3557
3558 gettimeofday(&now_tv, NULL);
3559 timeout = (chanpart->ch_deadline.tv_sec
3560 - now_tv.tv_sec) * 1000
3561 + (chanpart->ch_deadline.tv_usec
3562 - now_tv.tv_usec) / 1000
3563 + 1;
3564 }
3565#endif
3566 if (timeout < 0)
3567 {
3568 /* Something went wrong, channel_parse_json() didn't
3569 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003570 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003571 timeout = timeout_arg;
3572 }
3573 else if (timeout > timeout_arg)
3574 timeout = timeout_arg;
3575 }
3576 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003577 if (fd == INVALID_FD
3578 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003579 {
3580 if (timeout == timeout_arg)
3581 {
3582 if (fd != INVALID_FD)
3583 ch_log(channel, "Timed out");
3584 break;
3585 }
3586 }
3587 else
3588 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003589 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003590 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003591 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003592 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003593}
3594
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003595/*
3596 * Common for ch_read() and ch_readraw().
3597 */
3598 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003599common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003600{
3601 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003602 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003603 jobopt_T opt;
3604 int mode;
3605 int timeout;
3606 int id = -1;
3607 typval_T *listtv = NULL;
3608
3609 /* return an empty string by default */
3610 rettv->v_type = VAR_STRING;
3611 rettv->vval.v_string = NULL;
3612
3613 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003614 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003615 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003616 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003617
Bram Moolenaar437905c2016-04-26 19:01:05 +02003618 if (opt.jo_set & JO_PART)
3619 part = opt.jo_part;
3620 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003621 if (channel != NULL)
3622 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003623 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003624 part = channel_part_read(channel);
3625 mode = channel_get_mode(channel, part);
3626 timeout = channel_get_timeout(channel, part);
3627 if (opt.jo_set & JO_TIMEOUT)
3628 timeout = opt.jo_timeout;
3629
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003630 if (blob)
3631 {
3632 int outlen = 0;
3633 char_u *p = channel_read_block(channel, part,
3634 timeout, TRUE, &outlen);
3635 if (p != NULL)
3636 {
3637 blob_T *b = blob_alloc();
3638
3639 if (b != NULL)
3640 {
3641 b->bv_ga.ga_len = outlen;
3642 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3643 blob_free(b);
3644 else
3645 {
3646 memcpy(b->bv_ga.ga_data, p, outlen);
3647 rettv_blob_set(rettv, b);
3648 }
3649 }
3650 vim_free(p);
3651 }
3652 }
3653 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003654 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003655 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003656 else
3657 {
3658 if (opt.jo_set & JO_ID)
3659 id = opt.jo_id;
3660 channel_read_json_block(channel, part, timeout, id, &listtv);
3661 if (listtv != NULL)
3662 {
3663 *rettv = *listtv;
3664 vim_free(listtv);
3665 }
3666 else
3667 {
3668 rettv->v_type = VAR_SPECIAL;
3669 rettv->vval.v_number = VVAL_NONE;
3670 }
3671 }
3672 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003673
3674theend:
3675 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003676}
3677
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003678# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3679 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003680/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003681 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003682 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003683 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003684 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003685channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003686{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003687 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003688 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003689
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003690 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003691 for (channel = first_channel; channel != NULL;
3692 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003693 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003694 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003695 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003696 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003697 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003698 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003699 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003700 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003701 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003702}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003703# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003704
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003705# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003706/*
3707 * Check the channels for anything that is ready to be read.
3708 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003709 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003710 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003711 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003712channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003713{
3714 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003715 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003716 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003717
3718 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3719 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003720 if (only_keep_open && !channel->ch_keep_open)
3721 continue;
3722
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003723 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003724 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003725 {
3726 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003727 if (fd != INVALID_FD)
3728 {
3729 int r = channel_wait(channel, fd, 0);
3730
3731 if (r == CW_READY)
3732 channel_read(channel, part, "channel_handle_events");
3733 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003734 ch_close_part_on_error(channel, part, TRUE,
3735 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003736 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003737 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003738 }
3739}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003740# endif
3741
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003742# if defined(FEAT_GUI) || defined(PROTO)
3743/*
3744 * Return TRUE when there is any channel with a keep_open flag.
3745 */
3746 int
3747channel_any_keep_open()
3748{
3749 channel_T *channel;
3750
3751 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3752 if (channel->ch_keep_open)
3753 return TRUE;
3754 return FALSE;
3755}
3756# endif
3757
Bram Moolenaard04a0202016-01-26 23:30:18 +01003758/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003759 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003760 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003761 */
3762 void
3763channel_set_nonblock(channel_T *channel, ch_part_T part)
3764{
3765 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003766 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003767
3768 if (fd != INVALID_FD)
3769 {
3770#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003771 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003772
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003773 ioctlsocket(fd, FIONBIO, &val);
3774#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003775 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003776#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003777 ch_part->ch_nonblocking = TRUE;
3778 }
3779}
3780
3781/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003782 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003783 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003784 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003785 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003786 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003787channel_send(
3788 channel_T *channel,
3789 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003790 char_u *buf_arg,
3791 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003792 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003793{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003794 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003795 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003796 chanpart_T *ch_part = &channel->ch_part[part];
3797 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003798
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003799 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003800 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003801 {
3802 if (!channel->ch_error && fun != NULL)
3803 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003804 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003805 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003806 }
3807 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003808 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003809 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003810
Bram Moolenaar0b146882018-09-06 16:27:24 +02003811 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3812 channel_set_nonblock(channel, part);
3813
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003814 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003815 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003816 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003817 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003818 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003819 fprintf(log_fd, "'\n");
3820 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003821 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003822 }
3823
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003824 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003825 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003826 writeq_T *wq = &ch_part->ch_writeque;
3827 char_u *buf;
3828 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003829
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003830 if (wq->wq_next != NULL)
3831 {
3832 /* first write what was queued */
3833 buf = wq->wq_next->wq_ga.ga_data;
3834 len = wq->wq_next->wq_ga.ga_len;
3835 did_use_queue = TRUE;
3836 }
3837 else
3838 {
3839 if (len_arg == 0)
3840 /* nothing to write, called from channel_select_check() */
3841 return OK;
3842 buf = buf_arg;
3843 len = len_arg;
3844 }
3845
3846 if (part == PART_SOCK)
3847 res = sock_write(fd, (char *)buf, len);
3848 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003849 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003850 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar31faed62019-01-22 23:01:40 +01003851#ifdef WIN32
3852 if (channel->ch_named_pipe && res < 0)
3853 {
3854 DisconnectNamedPipe((HANDLE)fd);
3855 ConnectNamedPipe((HANDLE)fd, NULL);
3856 }
3857#endif
3858 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003859 if (res < 0 && (errno == EWOULDBLOCK
3860#ifdef EAGAIN
3861 || errno == EAGAIN
3862#endif
3863 ))
3864 res = 0; /* nothing got written */
3865
3866 if (res >= 0 && ch_part->ch_nonblocking)
3867 {
3868 writeq_T *entry = wq->wq_next;
3869
3870 if (did_use_queue)
3871 ch_log(channel, "Sent %d bytes now", res);
3872 if (res == len)
3873 {
3874 /* Wrote all the buf[len] bytes. */
3875 if (entry != NULL)
3876 {
3877 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003878 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003879 continue;
3880 }
3881 if (did_use_queue)
3882 ch_log(channel, "Write queue empty");
3883 }
3884 else
3885 {
3886 /* Wrote only buf[res] bytes, can't write more now. */
3887 if (entry != NULL)
3888 {
3889 if (res > 0)
3890 {
3891 /* Remove the bytes that were written. */
3892 mch_memmove(entry->wq_ga.ga_data,
3893 (char *)entry->wq_ga.ga_data + res,
3894 len - res);
3895 entry->wq_ga.ga_len -= res;
3896 }
3897 buf = buf_arg;
3898 len = len_arg;
3899 }
3900 else
3901 {
3902 buf += res;
3903 len -= res;
3904 }
3905 ch_log(channel, "Adding %d bytes to the write queue", len);
3906
3907 /* Append the not written bytes of the argument to the write
3908 * buffer. Limit entries to 4000 bytes. */
3909 if (wq->wq_prev != NULL
3910 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3911 {
3912 writeq_T *last = wq->wq_prev;
3913
3914 /* append to the last entry */
3915 if (ga_grow(&last->wq_ga, len) == OK)
3916 {
3917 mch_memmove((char *)last->wq_ga.ga_data
3918 + last->wq_ga.ga_len,
3919 buf, len);
3920 last->wq_ga.ga_len += len;
3921 }
3922 }
3923 else
3924 {
3925 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3926
3927 if (last != NULL)
3928 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003929 last->wq_prev = wq->wq_prev;
3930 last->wq_next = NULL;
3931 if (wq->wq_prev == NULL)
3932 wq->wq_next = last;
3933 else
3934 wq->wq_prev->wq_next = last;
3935 wq->wq_prev = last;
3936 ga_init2(&last->wq_ga, 1, 1000);
3937 if (ga_grow(&last->wq_ga, len) == OK)
3938 {
3939 mch_memmove(last->wq_ga.ga_data, buf, len);
3940 last->wq_ga.ga_len = len;
3941 }
3942 }
3943 }
3944 }
3945 }
3946 else if (res != len)
3947 {
3948 if (!channel->ch_error && fun != NULL)
3949 {
3950 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003951 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003952 }
3953 channel->ch_error = TRUE;
3954 return FAIL;
3955 }
3956
3957 channel->ch_error = FALSE;
3958 return OK;
3959 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003960}
3961
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003962/*
3963 * Common for "ch_sendexpr()" and "ch_sendraw()".
3964 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003965 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003966 * Otherwise returns NULL.
3967 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003968 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003969send_common(
3970 typval_T *argvars,
3971 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003972 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003973 int id,
3974 int eval,
3975 jobopt_T *opt,
3976 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003977 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003978{
3979 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003980 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003981
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003982 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003983 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003984 if (channel == NULL)
3985 return NULL;
3986 part_send = channel_part_send(channel);
3987 *part_read = channel_part_read(channel);
3988
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003989 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003990 return NULL;
3991
3992 /* Set the callback. An empty callback means no callback and not reading
3993 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3994 * allowed. */
3995 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3996 {
3997 if (eval)
3998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003999 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004000 return NULL;
4001 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02004002 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004003 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004004 }
4005
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004006 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004007 && opt->jo_callback == NULL)
4008 return channel;
4009 return NULL;
4010}
4011
4012/*
4013 * common for "ch_evalexpr()" and "ch_sendexpr()"
4014 */
4015 void
4016ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4017{
4018 char_u *text;
4019 typval_T *listtv;
4020 channel_T *channel;
4021 int id;
4022 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004023 ch_part_T part_send;
4024 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004025 jobopt_T opt;
4026 int timeout;
4027
4028 /* return an empty string by default */
4029 rettv->v_type = VAR_STRING;
4030 rettv->vval.v_string = NULL;
4031
Bram Moolenaar437905c2016-04-26 19:01:05 +02004032 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004033 if (channel == NULL)
4034 return;
4035 part_send = channel_part_send(channel);
4036
4037 ch_mode = channel_get_mode(channel, part_send);
4038 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004040 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004041 return;
4042 }
4043
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004044 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004045 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004046 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004047 if (text == NULL)
4048 return;
4049
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004050 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004051 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4052 vim_free(text);
4053 if (channel != NULL && eval)
4054 {
4055 if (opt.jo_set & JO_TIMEOUT)
4056 timeout = opt.jo_timeout;
4057 else
4058 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004059 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4060 == OK)
4061 {
4062 list_T *list = listtv->vval.v_list;
4063
4064 /* Move the item from the list and then change the type to
4065 * avoid the value being freed. */
4066 *rettv = list->lv_last->li_tv;
4067 list->lv_last->li_tv.v_type = VAR_NUMBER;
4068 free_tv(listtv);
4069 }
4070 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004071 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004072}
4073
4074/*
4075 * common for "ch_evalraw()" and "ch_sendraw()"
4076 */
4077 void
4078ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4079{
4080 char_u buf[NUMBUFLEN];
4081 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004082 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004083 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004084 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 jobopt_T opt;
4086 int timeout;
4087
4088 /* return an empty string by default */
4089 rettv->v_type = VAR_STRING;
4090 rettv->vval.v_string = NULL;
4091
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004092 if (argvars[1].v_type == VAR_BLOB)
4093 {
4094 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4095 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4096 }
4097 else
4098 {
4099 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004100 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004101 }
4102 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004103 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4104 if (channel != NULL && eval)
4105 {
4106 if (opt.jo_set & JO_TIMEOUT)
4107 timeout = opt.jo_timeout;
4108 else
4109 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004110 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004111 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004112 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004113 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004114}
4115
Bram Moolenaarf3360612017-10-01 16:21:31 +02004116# define KEEP_OPEN_TIME 20 /* msec */
4117
Bram Moolenaard04a0202016-01-26 23:30:18 +01004118# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004119/*
4120 * Add open channels to the poll struct.
4121 * Return the adjusted struct index.
4122 * The type of "fds" is hidden to avoid problems with the function proto.
4123 */
4124 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004125channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004126{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004127 int nfd = nfd_in;
4128 channel_T *channel;
4129 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004130 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004131
Bram Moolenaar77073442016-02-13 23:23:53 +01004132 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004133 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004134 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004135 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004136 chanpart_T *ch_part = &channel->ch_part[part];
4137
4138 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004139 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004140 if (channel->ch_keep_open)
4141 {
4142 /* For unknown reason poll() returns immediately for a
4143 * keep-open channel. Instead of adding it to the fds add
4144 * a short timeout and check, like polling. */
4145 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4146 *towait = KEEP_OPEN_TIME;
4147 }
4148 else
4149 {
4150 ch_part->ch_poll_idx = nfd;
4151 fds[nfd].fd = ch_part->ch_fd;
4152 fds[nfd].events = POLLIN;
4153 nfd++;
4154 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004155 }
4156 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004157 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004158 }
4159 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004160
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004161 nfd = channel_fill_poll_write(nfd, fds);
4162
Bram Moolenaare0874f82016-01-24 20:36:41 +01004163 return nfd;
4164}
4165
4166/*
4167 * The type of "fds" is hidden to avoid problems with the function proto.
4168 */
4169 int
4170channel_poll_check(int ret_in, void *fds_in)
4171{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004172 int ret = ret_in;
4173 channel_T *channel;
4174 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004175 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004176 int idx;
4177 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004178
Bram Moolenaar77073442016-02-13 23:23:53 +01004179 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004180 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004181 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004182 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004183 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004184
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004185 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004186 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004187 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004188 --ret;
4189 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004190 else if (channel->ch_part[part].ch_fd != INVALID_FD
4191 && channel->ch_keep_open)
4192 {
4193 /* polling a keep-open channel */
4194 channel_read(channel, part, "channel_poll_check_keep_open");
4195 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004196 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004197
4198 in_part = &channel->ch_part[PART_IN];
4199 idx = in_part->ch_poll_idx;
4200 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4201 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004202 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004203 --ret;
4204 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004205 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004206
4207 return ret;
4208}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004209# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004210
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004211# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004212
Bram Moolenaare0874f82016-01-24 20:36:41 +01004213/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004214 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004215 */
4216 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004217channel_select_setup(
4218 int maxfd_in,
4219 void *rfds_in,
4220 void *wfds_in,
4221 struct timeval *tv,
4222 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004223{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004224 int maxfd = maxfd_in;
4225 channel_T *channel;
4226 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004227 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004228 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004229
Bram Moolenaar77073442016-02-13 23:23:53 +01004230 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004231 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004232 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004233 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004234 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004235
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004236 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004237 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004238 if (channel->ch_keep_open)
4239 {
4240 /* For unknown reason select() returns immediately for a
4241 * keep-open channel. Instead of adding it to the rfds add
4242 * a short timeout and check, like polling. */
4243 if (*tvp == NULL || tv->tv_sec > 0
4244 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4245 {
4246 *tvp = tv;
4247 tv->tv_sec = 0;
4248 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4249 }
4250 }
4251 else
4252 {
4253 FD_SET((int)fd, rfds);
4254 if (maxfd < (int)fd)
4255 maxfd = (int)fd;
4256 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004257 }
4258 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004259 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004260
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004261 maxfd = channel_fill_wfds(maxfd, wfds);
4262
Bram Moolenaare0874f82016-01-24 20:36:41 +01004263 return maxfd;
4264}
4265
4266/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004267 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004268 */
4269 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004270channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004271{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004272 int ret = ret_in;
4273 channel_T *channel;
4274 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004275 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004276 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004277 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004278
Bram Moolenaar77073442016-02-13 23:23:53 +01004279 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004280 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004281 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004282 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004283 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004284
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004285 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004286 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004287 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004288 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004289 --ret;
4290 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004291 else if (fd != INVALID_FD && channel->ch_keep_open)
4292 {
4293 /* polling a keep-open channel */
4294 channel_read(channel, part, "channel_select_check_keep_open");
4295 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004296 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004297
4298 in_part = &channel->ch_part[PART_IN];
4299 if (ret > 0 && in_part->ch_fd != INVALID_FD
4300 && FD_ISSET(in_part->ch_fd, wfds))
4301 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004302 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004303 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004304 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004305 --ret;
4306 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004307 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004308
4309 return ret;
4310}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004311# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004312
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004313/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004314 * Execute queued up commands.
4315 * Invoked from the main loop when it's safe to execute received commands.
4316 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004317 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004318 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004319channel_parse_messages(void)
4320{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004321 channel_T *channel = first_channel;
4322 int ret = FALSE;
4323 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004324 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004325#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004326 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004327
4328 ELAPSED_INIT(start_tv);
4329#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004330
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004331 ++safe_to_invoke_callback;
4332
Bram Moolenaard0b65022016-03-06 21:50:33 +01004333 /* Only do this message when another message was given, otherwise we get
4334 * lots of them. */
4335 if (did_log_msg)
4336 {
4337 ch_log(NULL, "looking for messages on channels");
4338 did_log_msg = FALSE;
4339 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004340 while (channel != NULL)
4341 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004342 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004343 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004344 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004345 channel_close_now(channel);
4346 /* channel may have been freed, start over */
4347 channel = first_channel;
4348 continue;
4349 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004350 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004351 {
4352 channel_free(channel);
4353 /* channel has been freed, start over */
4354 channel = first_channel;
4355 continue;
4356 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004357 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004358 {
4359 /* channel is no longer useful, free it */
4360 channel_free(channel);
4361 channel = first_channel;
4362 part = PART_SOCK;
4363 continue;
4364 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004365 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004366 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004367 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004368 /* Increase the refcount, in case the handler causes the channel
4369 * to be unreferenced or closed. */
4370 ++channel->ch_refcount;
4371 r = may_invoke_callback(channel, part);
4372 if (r == OK)
4373 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004374 if (channel_unref(channel) || (r == OK
4375#ifdef ELAPSED_FUNC
4376 /* Limit the time we loop here to 100 msec, otherwise
4377 * Vim becomes unresponsive when the callback takes
4378 * more than a bit of time. */
4379 && ELAPSED_FUNC(start_tv) < 100L
4380#endif
4381 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004382 {
4383 /* channel was freed or something was done, start over */
4384 channel = first_channel;
4385 part = PART_SOCK;
4386 continue;
4387 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004388 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004389 if (part < PART_ERR)
4390 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004391 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004392 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004393 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004394 part = PART_SOCK;
4395 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004396 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004397
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004398 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004399 {
4400 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004401 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004402 }
4403
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004404 --safe_to_invoke_callback;
4405
Bram Moolenaard7ece102016-02-02 23:23:02 +01004406 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004407}
4408
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004409/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004410 * Return TRUE if any channel has readahead. That means we should not block on
4411 * waiting for input.
4412 */
4413 int
4414channel_any_readahead(void)
4415{
4416 channel_T *channel = first_channel;
4417 ch_part_T part = PART_SOCK;
4418
4419 while (channel != NULL)
4420 {
4421 if (channel_has_readahead(channel, part))
4422 return TRUE;
4423 if (part < PART_ERR)
4424 ++part;
4425 else
4426 {
4427 channel = channel->ch_next;
4428 part = PART_SOCK;
4429 }
4430 }
4431 return FALSE;
4432}
4433
4434/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004435 * Mark references to lists used in channels.
4436 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004437 int
4438set_ref_in_channel(int copyID)
4439{
Bram Moolenaar77073442016-02-13 23:23:53 +01004440 int abort = FALSE;
4441 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004442 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004443
Bram Moolenaar77073442016-02-13 23:23:53 +01004444 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004445 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004446 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004447 tv.v_type = VAR_CHANNEL;
4448 tv.vval.v_channel = channel;
4449 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004450 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004451 return abort;
4452}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004453
4454/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004455 * Return the "part" to write to for "channel".
4456 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004457 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004458channel_part_send(channel_T *channel)
4459{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004460 if (channel->CH_SOCK_FD == INVALID_FD)
4461 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004462 return PART_SOCK;
4463}
4464
4465/*
4466 * Return the default "part" to read from for "channel".
4467 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004468 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004469channel_part_read(channel_T *channel)
4470{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004471 if (channel->CH_SOCK_FD == INVALID_FD)
4472 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004473 return PART_SOCK;
4474}
4475
4476/*
4477 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004478 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004479 */
4480 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004481channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004482{
Bram Moolenaar77073442016-02-13 23:23:53 +01004483 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004484 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004485 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004486}
4487
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004488/*
4489 * Return the timeout of "channel"/"part"
4490 */
4491 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004492channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004493{
4494 return channel->ch_part[part].ch_timeout;
4495}
4496
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004497 static int
4498handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4499{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004500 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004501
4502 opt->jo_set |= jo;
4503 if (STRCMP(val, "nl") == 0)
4504 *modep = MODE_NL;
4505 else if (STRCMP(val, "raw") == 0)
4506 *modep = MODE_RAW;
4507 else if (STRCMP(val, "js") == 0)
4508 *modep = MODE_JS;
4509 else if (STRCMP(val, "json") == 0)
4510 *modep = MODE_JSON;
4511 else
4512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004513 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004514 return FAIL;
4515 }
4516 return OK;
4517}
4518
4519 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004520handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004521{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004522 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004523
4524 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4525 if (STRCMP(val, "null") == 0)
4526 opt->jo_io[part] = JIO_NULL;
4527 else if (STRCMP(val, "pipe") == 0)
4528 opt->jo_io[part] = JIO_PIPE;
4529 else if (STRCMP(val, "file") == 0)
4530 opt->jo_io[part] = JIO_FILE;
4531 else if (STRCMP(val, "buffer") == 0)
4532 opt->jo_io[part] = JIO_BUFFER;
4533 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4534 opt->jo_io[part] = JIO_OUT;
4535 else
4536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004537 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004538 return FAIL;
4539 }
4540 return OK;
4541}
4542
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004543/*
4544 * Clear a jobopt_T before using it.
4545 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004546 void
4547clear_job_options(jobopt_T *opt)
4548{
4549 vim_memset(opt, 0, sizeof(jobopt_T));
4550}
4551
4552/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004553 * Free any members of a jobopt_T.
4554 */
4555 void
4556free_job_options(jobopt_T *opt)
4557{
4558 if (opt->jo_partial != NULL)
4559 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004560 else if (opt->jo_callback != NULL)
4561 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004562 if (opt->jo_out_partial != NULL)
4563 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004564 else if (opt->jo_out_cb != NULL)
4565 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004566 if (opt->jo_err_partial != NULL)
4567 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004568 else if (opt->jo_err_cb != NULL)
4569 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004570 if (opt->jo_close_partial != NULL)
4571 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004572 else if (opt->jo_close_cb != NULL)
4573 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004574 if (opt->jo_exit_partial != NULL)
4575 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004576 else if (opt->jo_exit_cb != NULL)
4577 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004578 if (opt->jo_env != NULL)
4579 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004580}
4581
4582/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004583 * Get the PART_ number from the first character of an option name.
4584 */
4585 static int
4586part_from_char(int c)
4587{
4588 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4589}
4590
4591/*
4592 * Get the option entries from the dict in "tv", parse them and put the result
4593 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004594 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004595 * If an option value is invalid return FAIL.
4596 */
4597 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004598get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004599{
4600 typval_T *item;
4601 char_u *val;
4602 dict_T *dict;
4603 int todo;
4604 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004605 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004606
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004607 if (tv->v_type == VAR_UNKNOWN)
4608 return OK;
4609 if (tv->v_type != VAR_DICT)
4610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004611 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004612 return FAIL;
4613 }
4614 dict = tv->vval.v_dict;
4615 if (dict == NULL)
4616 return OK;
4617
4618 todo = (int)dict->dv_hashtab.ht_used;
4619 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4620 if (!HASHITEM_EMPTY(hi))
4621 {
4622 item = &dict_lookup(hi)->di_tv;
4623
4624 if (STRCMP(hi->hi_key, "mode") == 0)
4625 {
4626 if (!(supported & JO_MODE))
4627 break;
4628 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4629 return FAIL;
4630 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004631 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004632 {
4633 if (!(supported & JO_IN_MODE))
4634 break;
4635 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4636 == FAIL)
4637 return FAIL;
4638 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004639 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004640 {
4641 if (!(supported & JO_OUT_MODE))
4642 break;
4643 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4644 == FAIL)
4645 return FAIL;
4646 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004647 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004648 {
4649 if (!(supported & JO_ERR_MODE))
4650 break;
4651 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4652 == FAIL)
4653 return FAIL;
4654 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004655 else if (STRCMP(hi->hi_key, "noblock") == 0)
4656 {
4657 if (!(supported & JO_MODE))
4658 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004659 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004660 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004661 else if (STRCMP(hi->hi_key, "in_io") == 0
4662 || STRCMP(hi->hi_key, "out_io") == 0
4663 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004664 {
4665 if (!(supported & JO_OUT_IO))
4666 break;
4667 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4668 return FAIL;
4669 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004670 else if (STRCMP(hi->hi_key, "in_name") == 0
4671 || STRCMP(hi->hi_key, "out_name") == 0
4672 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004673 {
4674 part = part_from_char(*hi->hi_key);
4675
4676 if (!(supported & JO_OUT_IO))
4677 break;
4678 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4679 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004680 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004681 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004682 else if (STRCMP(hi->hi_key, "pty") == 0)
4683 {
4684 if (!(supported & JO_MODE))
4685 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004686 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004687 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004688 else if (STRCMP(hi->hi_key, "in_buf") == 0
4689 || STRCMP(hi->hi_key, "out_buf") == 0
4690 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004691 {
4692 part = part_from_char(*hi->hi_key);
4693
4694 if (!(supported & JO_OUT_IO))
4695 break;
4696 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004697 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004698 if (opt->jo_io_buf[part] <= 0)
4699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004700 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004701 return FAIL;
4702 }
4703 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004705 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004706 return FAIL;
4707 }
4708 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004709 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4710 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4711 {
4712 part = part_from_char(*hi->hi_key);
4713
4714 if (!(supported & JO_OUT_IO))
4715 break;
4716 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004717 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004718 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004719 else if (STRCMP(hi->hi_key, "out_msg") == 0
4720 || STRCMP(hi->hi_key, "err_msg") == 0)
4721 {
4722 part = part_from_char(*hi->hi_key);
4723
4724 if (!(supported & JO_OUT_IO))
4725 break;
4726 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004727 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004728 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004729 else if (STRCMP(hi->hi_key, "in_top") == 0
4730 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004731 {
4732 linenr_T *lp;
4733
4734 if (!(supported & JO_OUT_IO))
4735 break;
4736 if (hi->hi_key[3] == 't')
4737 {
4738 lp = &opt->jo_in_top;
4739 opt->jo_set |= JO_IN_TOP;
4740 }
4741 else
4742 {
4743 lp = &opt->jo_in_bot;
4744 opt->jo_set |= JO_IN_BOT;
4745 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004746 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004747 if (*lp < 0)
4748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004749 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004750 return FAIL;
4751 }
4752 }
4753 else if (STRCMP(hi->hi_key, "channel") == 0)
4754 {
4755 if (!(supported & JO_OUT_IO))
4756 break;
4757 opt->jo_set |= JO_CHANNEL;
4758 if (item->v_type != VAR_CHANNEL)
4759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004760 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004761 return FAIL;
4762 }
4763 opt->jo_channel = item->vval.v_channel;
4764 }
4765 else if (STRCMP(hi->hi_key, "callback") == 0)
4766 {
4767 if (!(supported & JO_CALLBACK))
4768 break;
4769 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004770 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004771 if (opt->jo_callback == NULL)
4772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004773 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004774 return FAIL;
4775 }
4776 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004777 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004778 {
4779 if (!(supported & JO_OUT_CALLBACK))
4780 break;
4781 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004782 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004783 if (opt->jo_out_cb == NULL)
4784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004785 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004786 return FAIL;
4787 }
4788 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004789 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004790 {
4791 if (!(supported & JO_ERR_CALLBACK))
4792 break;
4793 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004794 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004795 if (opt->jo_err_cb == NULL)
4796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004797 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004798 return FAIL;
4799 }
4800 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004801 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004802 {
4803 if (!(supported & JO_CLOSE_CALLBACK))
4804 break;
4805 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004806 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004807 if (opt->jo_close_cb == NULL)
4808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004809 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004810 return FAIL;
4811 }
4812 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004813 else if (STRCMP(hi->hi_key, "drop") == 0)
4814 {
4815 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004816 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004817
4818 if (STRCMP(val, "never") == 0)
4819 never = TRUE;
4820 else if (STRCMP(val, "auto") != 0)
4821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004822 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004823 return FAIL;
4824 }
4825 opt->jo_drop_never = never;
4826 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004827 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4828 {
4829 if (!(supported & JO_EXIT_CB))
4830 break;
4831 opt->jo_set |= JO_EXIT_CB;
4832 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4833 if (opt->jo_exit_cb == NULL)
4834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004835 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004836 return FAIL;
4837 }
4838 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004839#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004840 else if (STRCMP(hi->hi_key, "term_name") == 0)
4841 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004842 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004843 break;
4844 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004846 if (opt->jo_term_name == NULL)
4847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004848 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004849 return FAIL;
4850 }
4851 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004852 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4853 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004854 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004855 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004856 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004857 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004860 return FAIL;
4861 }
4862 opt->jo_set2 |= JO2_TERM_FINISH;
4863 opt->jo_term_finish = *val;
4864 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004865 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4866 {
4867 char_u *p;
4868
4869 if (!(supported2 & JO2_TERM_OPENCMD))
4870 break;
4871 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004872 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004873 if (p != NULL)
4874 {
4875 /* Must have %d and no other %. */
4876 p = vim_strchr(p, '%');
4877 if (p != NULL && (p[1] != 'd'
4878 || vim_strchr(p + 2, '%') != NULL))
4879 p = NULL;
4880 }
4881 if (p == NULL)
4882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004883 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004884 return FAIL;
4885 }
4886 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004887 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4888 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004889 char_u *p;
4890
4891 if (!(supported2 & JO2_EOF_CHARS))
4892 break;
4893 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004894 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004895 if (p == NULL)
4896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004897 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004898 return FAIL;
4899 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004900 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004901 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4902 {
4903 if (!(supported2 & JO2_TERM_ROWS))
4904 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004905 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004906 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004907 }
4908 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4909 {
4910 if (!(supported2 & JO2_TERM_COLS))
4911 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004912 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004913 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004914 }
4915 else if (STRCMP(hi->hi_key, "vertical") == 0)
4916 {
4917 if (!(supported2 & JO2_VERTICAL))
4918 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004919 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004920 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004921 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004922 else if (STRCMP(hi->hi_key, "curwin") == 0)
4923 {
4924 if (!(supported2 & JO2_CURWIN))
4925 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004926 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004927 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004928 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004929 else if (STRCMP(hi->hi_key, "hidden") == 0)
4930 {
4931 if (!(supported2 & JO2_HIDDEN))
4932 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004933 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004934 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004935 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004936 else if (STRCMP(hi->hi_key, "norestore") == 0)
4937 {
4938 if (!(supported2 & JO2_NORESTORE))
4939 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004940 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004941 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004942 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004943 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4944 {
4945 if (!(supported2 & JO2_TERM_KILL))
4946 break;
4947 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004948 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004949 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004950 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004951 {
4952 char_u *p;
4953
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004954 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004955 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004956 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004957 p = tv_get_string_chk(item);
4958 if (p == NULL)
4959 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004960 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004961 return FAIL;
4962 }
4963 // Allow empty string, "winpty", "conpty".
4964 if (!(*p == NUL || STRCMP(p, "winpty") == 0
4965 || STRCMP(p, "conpty") == 0))
4966 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004967 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004968 return FAIL;
4969 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004970 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004971 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004972# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4973 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004975 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004976 listitem_T *li;
4977 long_u rgb[16];
4978
4979 if (!(supported2 & JO2_ANSI_COLORS))
4980 break;
4981
4982 if (item == NULL || item->v_type != VAR_LIST
4983 || item->vval.v_list == NULL)
4984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004985 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004986 return FAIL;
4987 }
4988
4989 li = item->vval.v_list->lv_first;
4990 for (; li != NULL && n < 16; li = li->li_next, n++)
4991 {
4992 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004993 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004994
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004995 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004996 if (color_name == NULL)
4997 return FAIL;
4998
4999 guicolor = GUI_GET_COLOR(color_name);
5000 if (guicolor == INVALCOLOR)
5001 return FAIL;
5002
5003 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5004 }
5005
5006 if (n != 16 || li != NULL)
5007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005008 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005009 return FAIL;
5010 }
5011
5012 opt->jo_set2 |= JO2_ANSI_COLORS;
5013 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5014 }
5015# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005016#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005017 else if (STRCMP(hi->hi_key, "env") == 0)
5018 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005019 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005020 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005021 if (item->v_type != VAR_DICT)
5022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005023 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005024 return FAIL;
5025 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005026 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005027 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005028 if (opt->jo_env != NULL)
5029 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005030 }
5031 else if (STRCMP(hi->hi_key, "cwd") == 0)
5032 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005033 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005034 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005035 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005036 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005037#ifndef WIN32 // Win32 directories don't have the concept of "executable"
5038 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5039#endif
5040 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005041 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005042 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005043 return FAIL;
5044 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005045 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005046 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005047 else if (STRCMP(hi->hi_key, "waittime") == 0)
5048 {
5049 if (!(supported & JO_WAITTIME))
5050 break;
5051 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005052 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005053 }
5054 else if (STRCMP(hi->hi_key, "timeout") == 0)
5055 {
5056 if (!(supported & JO_TIMEOUT))
5057 break;
5058 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005059 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005060 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005061 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005062 {
5063 if (!(supported & JO_OUT_TIMEOUT))
5064 break;
5065 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005066 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005067 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005068 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005069 {
5070 if (!(supported & JO_ERR_TIMEOUT))
5071 break;
5072 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005073 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005074 }
5075 else if (STRCMP(hi->hi_key, "part") == 0)
5076 {
5077 if (!(supported & JO_PART))
5078 break;
5079 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005080 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005081 if (STRCMP(val, "err") == 0)
5082 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005083 else if (STRCMP(val, "out") == 0)
5084 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005085 else
5086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005087 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005088 return FAIL;
5089 }
5090 }
5091 else if (STRCMP(hi->hi_key, "id") == 0)
5092 {
5093 if (!(supported & JO_ID))
5094 break;
5095 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005096 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005097 }
5098 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5099 {
5100 if (!(supported & JO_STOPONEXIT))
5101 break;
5102 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005103 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005104 opt->jo_soe_buf);
5105 if (opt->jo_stoponexit == NULL)
5106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005107 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005108 return FAIL;
5109 }
5110 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005111 else if (STRCMP(hi->hi_key, "block_write") == 0)
5112 {
5113 if (!(supported & JO_BLOCK_WRITE))
5114 break;
5115 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005116 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005117 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005118 else
5119 break;
5120 --todo;
5121 }
5122 if (todo > 0)
5123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005124 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005125 return FAIL;
5126 }
5127
5128 return OK;
5129}
5130
5131/*
5132 * Get the channel from the argument.
5133 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005134 * When "check_open" is TRUE check that the channel can be used.
5135 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005136 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005137 */
5138 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005139get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005140{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005141 channel_T *channel = NULL;
5142 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005143
5144 if (tv->v_type == VAR_JOB)
5145 {
5146 if (tv->vval.v_job != NULL)
5147 channel = tv->vval.v_job->jv_channel;
5148 }
5149 else if (tv->v_type == VAR_CHANNEL)
5150 {
5151 channel = tv->vval.v_channel;
5152 }
5153 else
5154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005155 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005156 return NULL;
5157 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005158 if (channel != NULL && reading)
5159 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005160 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005161
Bram Moolenaar437905c2016-04-26 19:01:05 +02005162 if (check_open && (channel == NULL || (!channel_is_open(channel)
5163 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005165 emsg(_("E906: not an open channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005166 return NULL;
5167 }
5168 return channel;
5169}
5170
5171static job_T *first_job = NULL;
5172
5173 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005174job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005175{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005176 int i;
5177
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005178 ch_log(job->jv_channel, "Freeing job");
5179 if (job->jv_channel != NULL)
5180 {
5181 /* The link from the channel to the job doesn't count as a reference,
5182 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005183 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005184 * NULL the reference. We don't set ch_job_killed, unreferencing the
5185 * job doesn't mean it stops running. */
5186 job->jv_channel->ch_job = NULL;
5187 channel_unref(job->jv_channel);
5188 }
5189 mch_clear_job(job);
5190
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005191 vim_free(job->jv_tty_in);
5192 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005193 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005194#ifdef UNIX
5195 vim_free(job->jv_termsig);
5196#endif
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005197#ifdef WIN3264
5198 vim_free(job->jv_tty_type);
5199#endif
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005200 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005201 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005202 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005203 for (i = 0; job->jv_argv[i] != NULL; i++)
5204 vim_free(job->jv_argv[i]);
5205 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005206 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005207}
5208
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005209/*
5210 * Remove "job" from the list of jobs.
5211 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005212 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005213job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005214{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005215 if (job->jv_next != NULL)
5216 job->jv_next->jv_prev = job->jv_prev;
5217 if (job->jv_prev == NULL)
5218 first_job = job->jv_next;
5219 else
5220 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005221}
5222
5223 static void
5224job_free_job(job_T *job)
5225{
5226 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005227 vim_free(job);
5228}
5229
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005230 static void
5231job_free(job_T *job)
5232{
5233 if (!in_free_unref_items)
5234 {
5235 job_free_contents(job);
5236 job_free_job(job);
5237 }
5238}
5239
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005240job_T *jobs_to_free = NULL;
5241
5242/*
5243 * Put "job" in a list to be freed later, when it's no longer referenced.
5244 */
5245 static void
5246job_free_later(job_T *job)
5247{
5248 job_unlink(job);
5249 job->jv_next = jobs_to_free;
5250 jobs_to_free = job;
5251}
5252
5253 static void
5254free_jobs_to_free_later(void)
5255{
5256 job_T *job;
5257
5258 while (jobs_to_free != NULL)
5259 {
5260 job = jobs_to_free;
5261 jobs_to_free = job->jv_next;
5262 job_free_contents(job);
5263 vim_free(job);
5264 }
5265}
5266
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005267#if defined(EXITFREE) || defined(PROTO)
5268 void
5269job_free_all(void)
5270{
5271 while (first_job != NULL)
5272 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005273 free_jobs_to_free_later();
5274
5275# ifdef FEAT_TERMINAL
5276 free_unused_terminals();
5277# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005278}
5279#endif
5280
5281/*
5282 * Return TRUE if we need to check if the process of "job" has ended.
5283 */
5284 static int
5285job_need_end_check(job_T *job)
5286{
5287 return job->jv_status == JOB_STARTED
5288 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5289}
5290
5291/*
5292 * Return TRUE if the channel of "job" is still useful.
5293 */
5294 static int
5295job_channel_still_useful(job_T *job)
5296{
5297 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5298}
5299
5300/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005301 * Return TRUE if the channel of "job" is closeable.
5302 */
5303 static int
5304job_channel_can_close(job_T *job)
5305{
5306 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5307}
5308
5309/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005310 * Return TRUE if the job should not be freed yet. Do not free the job when
5311 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5312 * or when the associated channel will do something with the job output.
5313 */
5314 static int
5315job_still_useful(job_T *job)
5316{
5317 return job_need_end_check(job) || job_channel_still_useful(job);
5318}
5319
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005320#if defined(GUI_MAY_FORK) || defined(PROTO)
5321/*
5322 * Return TRUE when there is any running job that we care about.
5323 */
5324 int
5325job_any_running()
5326{
5327 job_T *job;
5328
5329 for (job = first_job; job != NULL; job = job->jv_next)
5330 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005331 {
5332 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005333 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005334 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005335 return FALSE;
5336}
5337#endif
5338
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005339#if !defined(USE_ARGV) || defined(PROTO)
5340/*
5341 * Escape one argument for an external command.
5342 * Returns the escaped string in allocated memory. NULL when out of memory.
5343 */
5344 static char_u *
5345win32_escape_arg(char_u *arg)
5346{
5347 int slen, dlen;
5348 int escaping = 0;
5349 int i;
5350 char_u *s, *d;
5351 char_u *escaped_arg;
5352 int has_spaces = FALSE;
5353
5354 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005355 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005356 dlen = slen;
5357 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5358 {
5359 if (*s == '"' || *s == '\\')
5360 ++dlen;
5361 if (*s == ' ' || *s == '\t')
5362 has_spaces = TRUE;
5363 }
5364
5365 if (has_spaces)
5366 dlen += 2;
5367
5368 if (dlen == slen)
5369 return vim_strsave(arg);
5370
5371 /* Allocate memory for the result and fill it. */
5372 escaped_arg = alloc(dlen + 1);
5373 if (escaped_arg == NULL)
5374 return NULL;
5375 memset(escaped_arg, 0, dlen+1);
5376
5377 d = escaped_arg;
5378
5379 if (has_spaces)
5380 *d++ = '"';
5381
5382 for (s = arg; *s != NUL;)
5383 {
5384 switch (*s)
5385 {
5386 case '"':
5387 for (i = 0; i < escaping; i++)
5388 *d++ = '\\';
5389 escaping = 0;
5390 *d++ = '\\';
5391 *d++ = *s++;
5392 break;
5393 case '\\':
5394 escaping++;
5395 *d++ = *s++;
5396 break;
5397 default:
5398 escaping = 0;
5399 MB_COPY_CHAR(s, d);
5400 break;
5401 }
5402 }
5403
5404 /* add terminating quote and finish with a NUL */
5405 if (has_spaces)
5406 {
5407 for (i = 0; i < escaping; i++)
5408 *d++ = '\\';
5409 *d++ = '"';
5410 }
5411 *d = NUL;
5412
5413 return escaped_arg;
5414}
5415
5416/*
5417 * Build a command line from a list, taking care of escaping.
5418 * The result is put in gap->ga_data.
5419 * Returns FAIL when out of memory.
5420 */
5421 int
5422win32_build_cmd(list_T *l, garray_T *gap)
5423{
5424 listitem_T *li;
5425 char_u *s;
5426
5427 for (li = l->lv_first; li != NULL; li = li->li_next)
5428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005429 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005430 if (s == NULL)
5431 return FAIL;
5432 s = win32_escape_arg(s);
5433 if (s == NULL)
5434 return FAIL;
5435 ga_concat(gap, s);
5436 vim_free(s);
5437 if (li->li_next != NULL)
5438 ga_append(gap, ' ');
5439 }
5440 return OK;
5441}
5442#endif
5443
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005444/*
5445 * NOTE: Must call job_cleanup() only once right after the status of "job"
5446 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5447 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005448 * If the job is no longer used it will be removed from the list of jobs, and
5449 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005450 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005451 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005452job_cleanup(job_T *job)
5453{
5454 if (job->jv_status != JOB_ENDED)
5455 return;
5456
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005457 /* Ready to cleanup the job. */
5458 job->jv_status = JOB_FINISHED;
5459
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005460 /* When only channel-in is kept open, close explicitly. */
5461 if (job->jv_channel != NULL)
5462 ch_close_part(job->jv_channel, PART_IN);
5463
Bram Moolenaar97792de2016-10-15 18:36:49 +02005464 if (job->jv_exit_cb != NULL)
5465 {
5466 typval_T argv[3];
5467 typval_T rettv;
5468 int dummy;
5469
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005470 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005471 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005472 ++job->jv_refcount;
5473 argv[0].v_type = VAR_JOB;
5474 argv[0].vval.v_job = job;
5475 argv[1].v_type = VAR_NUMBER;
5476 argv[1].vval.v_number = job->jv_exitval;
5477 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5478 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5479 job->jv_exit_partial, NULL);
5480 clear_tv(&rettv);
5481 --job->jv_refcount;
5482 channel_need_redraw = TRUE;
5483 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005484
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005485 if (job->jv_channel != NULL
5486 && job->jv_channel->ch_anonymous_pipe && !job->jv_channel->ch_killing)
5487 {
5488 ++safe_to_invoke_callback;
5489 channel_free_contents(job->jv_channel);
5490 job->jv_channel->ch_job = NULL;
5491 job->jv_channel = NULL;
5492 --safe_to_invoke_callback;
5493 }
5494
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005495 // Do not free the job in case the close callback of the associated channel
5496 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005497 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005498 // The job was already unreferenced and the associated channel was
5499 // detached, now that it ended it can be freed. However, a caller might
5500 // still use it, thus free it a bit later.
5501 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005502}
5503
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005504/*
5505 * Mark references in jobs that are still useful.
5506 */
5507 int
5508set_ref_in_job(int copyID)
5509{
5510 int abort = FALSE;
5511 job_T *job;
5512 typval_T tv;
5513
5514 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005515 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005516 {
5517 tv.v_type = VAR_JOB;
5518 tv.vval.v_job = job;
5519 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5520 }
5521 return abort;
5522}
5523
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005524/*
5525 * Dereference "job". Note that after this "job" may have been freed.
5526 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005527 void
5528job_unref(job_T *job)
5529{
5530 if (job != NULL && --job->jv_refcount <= 0)
5531 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005532 /* Do not free the job if there is a channel where the close callback
5533 * may get the job info. */
5534 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005535 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005536 /* Do not free the job when it has not ended yet and there is a
5537 * "stoponexit" flag or an exit callback. */
5538 if (!job_need_end_check(job))
5539 {
5540 job_free(job);
5541 }
5542 else if (job->jv_channel != NULL)
5543 {
5544 /* Do remove the link to the channel, otherwise it hangs
5545 * around until Vim exits. See job_free() for refcount. */
5546 ch_log(job->jv_channel, "detaching channel from job");
5547 job->jv_channel->ch_job = NULL;
5548 channel_unref(job->jv_channel);
5549 job->jv_channel = NULL;
5550 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005551 }
5552 }
5553}
5554
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005555 int
5556free_unused_jobs_contents(int copyID, int mask)
5557{
5558 int did_free = FALSE;
5559 job_T *job;
5560
5561 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005562 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005563 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005564 {
5565 /* Free the channel and ordinary items it contains, but don't
5566 * recurse into Lists, Dictionaries etc. */
5567 job_free_contents(job);
5568 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005569 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005570 return did_free;
5571}
5572
5573 void
5574free_unused_jobs(int copyID, int mask)
5575{
5576 job_T *job;
5577 job_T *job_next;
5578
5579 for (job = first_job; job != NULL; job = job_next)
5580 {
5581 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005582 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005583 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005584 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005585 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005586 job_free_job(job);
5587 }
5588 }
5589}
5590
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005591/*
5592 * Allocate a job. Sets the refcount to one and sets options default.
5593 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005594 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005595job_alloc(void)
5596{
5597 job_T *job;
5598
5599 job = (job_T *)alloc_clear(sizeof(job_T));
5600 if (job != NULL)
5601 {
5602 job->jv_refcount = 1;
5603 job->jv_stoponexit = vim_strsave((char_u *)"term");
5604
5605 if (first_job != NULL)
5606 {
5607 first_job->jv_prev = job;
5608 job->jv_next = first_job;
5609 }
5610 first_job = job;
5611 }
5612 return job;
5613}
5614
5615 void
5616job_set_options(job_T *job, jobopt_T *opt)
5617{
5618 if (opt->jo_set & JO_STOPONEXIT)
5619 {
5620 vim_free(job->jv_stoponexit);
5621 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5622 job->jv_stoponexit = NULL;
5623 else
5624 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5625 }
5626 if (opt->jo_set & JO_EXIT_CB)
5627 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005628 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005629 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005630 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005631 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005632 job->jv_exit_partial = NULL;
5633 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005634 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005635 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005636 job->jv_exit_partial = opt->jo_exit_partial;
5637 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005638 {
5639 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005640 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005641 }
5642 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005643 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005644 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005645 func_ref(job->jv_exit_cb);
5646 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005647 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005648 }
5649}
5650
5651/*
5652 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5653 */
5654 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005655job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005656{
5657 job_T *job;
5658
5659 for (job = first_job; job != NULL; job = job->jv_next)
5660 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005661 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005662}
5663
5664/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005665 * Return TRUE when there is any job that has an exit callback and might exit,
5666 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005667 */
5668 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005669has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005670{
5671 job_T *job;
5672
5673 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005674 /* Only should check if the channel has been closed, if the channel is
5675 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005676 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5677 || (job->jv_status == JOB_FINISHED
5678 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005679 return TRUE;
5680 return FALSE;
5681}
5682
Bram Moolenaar97792de2016-10-15 18:36:49 +02005683#define MAX_CHECK_ENDED 8
5684
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005685/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005686 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005687 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005688 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005689 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005690job_check_ended(void)
5691{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005692 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005693 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005694
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005695 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005696 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005697 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005698
Bram Moolenaar97792de2016-10-15 18:36:49 +02005699 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005700 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005701 // NOTE: mch_detect_ended_job() must only return a job of which the
5702 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005703 job_T *job = mch_detect_ended_job(first_job);
5704
5705 if (job == NULL)
5706 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005707 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005708 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005709 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005710
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005711 // Actually free jobs that were cleaned up.
5712 free_jobs_to_free_later();
5713
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005714 if (channel_need_redraw)
5715 {
5716 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005717 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005718 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005719 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005720}
5721
5722/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005723 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005724 * "argv_arg" is only for Unix.
5725 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005726 * The returned job has a refcount of one.
5727 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005728 */
5729 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005730job_start(
5731 typval_T *argvars,
5732 char **argv_arg,
5733 jobopt_T *opt_arg,
5734 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005735{
5736 job_T *job;
5737 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005738 char **argv = NULL;
5739 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005740#if defined(UNIX)
5741# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005742 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005743#else
5744 garray_T ga;
5745#endif
5746 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005747 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005748
5749 job = job_alloc();
5750 if (job == NULL)
5751 return NULL;
5752
5753 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005754#ifndef USE_ARGV
5755 ga_init2(&ga, (int)sizeof(char*), 20);
5756#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005757
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005758 if (opt_arg != NULL)
5759 opt = *opt_arg;
5760 else
5761 {
5762 /* Default mode is NL. */
5763 clear_job_options(&opt);
5764 opt.jo_mode = MODE_NL;
5765 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005766 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5767 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5768 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005769 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005770 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005771
5772 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005773 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005774 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5775 && opt.jo_io[part] == JIO_FILE
5776 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5777 || *opt.jo_io_name[part] == NUL))
5778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005779 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005780 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005781 }
5782
5783 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5784 {
5785 buf_T *buf = NULL;
5786
5787 /* check that we can find the buffer before starting the job */
5788 if (opt.jo_set & JO_IN_BUF)
5789 {
5790 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5791 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005792 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005793 }
5794 else if (!(opt.jo_set & JO_IN_NAME))
5795 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005796 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005797 }
5798 else
5799 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5800 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005801 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005802 if (buf->b_ml.ml_mfp == NULL)
5803 {
5804 char_u numbuf[NUMBUFLEN];
5805 char_u *s;
5806
5807 if (opt.jo_set & JO_IN_BUF)
5808 {
5809 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5810 s = numbuf;
5811 }
5812 else
5813 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005814 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005815 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005816 }
5817 job->jv_in_buf = buf;
5818 }
5819
5820 job_set_options(job, &opt);
5821
Bram Moolenaar13568252018-03-16 20:46:58 +01005822#ifdef USE_ARGV
5823 if (argv_arg != NULL)
5824 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005825 /* Make a copy of argv_arg for job->jv_argv. */
5826 for (i = 0; argv_arg[i] != NULL; i++)
5827 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005828 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005829 if (argv == NULL)
5830 goto theend;
5831 for (i = 0; i < argc; i++)
5832 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5833 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005834 }
5835 else
5836#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005837 if (argvars[0].v_type == VAR_STRING)
5838 {
5839 /* Command is a string. */
5840 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005841 if (cmd == NULL || *cmd == NUL)
5842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005843 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005844 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005845 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005846
5847 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005848 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005849 }
5850 else if (argvars[0].v_type != VAR_LIST
5851 || argvars[0].vval.v_list == NULL
5852 || argvars[0].vval.v_list->lv_len < 1)
5853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005854 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005855 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005856 }
5857 else
5858 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005859 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005860
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005861 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005862 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005863#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005864 if (win32_build_cmd(l, &ga) == FAIL)
5865 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005866 cmd = ga.ga_data;
5867#endif
5868 }
5869
Bram Moolenaar20608922018-04-21 22:30:08 +02005870 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005871 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005872
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005873#ifdef USE_ARGV
5874 if (ch_log_active())
5875 {
5876 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005877
5878 ga_init2(&ga, (int)sizeof(char), 200);
5879 for (i = 0; i < argc; ++i)
5880 {
5881 if (i > 0)
5882 ga_concat(&ga, (char_u *)" ");
5883 ga_concat(&ga, (char_u *)argv[i]);
5884 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005885 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005886 ga_clear(&ga);
5887 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005888 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005889#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005890 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005891 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005892#endif
5893
5894 /* If the channel is reading from a buffer, write lines now. */
5895 if (job->jv_channel != NULL)
5896 channel_write_in(job->jv_channel);
5897
5898theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005899#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005900 vim_free(ga.ga_data);
5901#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005902 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005903 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005904 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005905 return job;
5906}
5907
5908/*
5909 * Get the status of "job" and invoke the exit callback when needed.
5910 * The returned string is not allocated.
5911 */
5912 char *
5913job_status(job_T *job)
5914{
5915 char *result;
5916
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005917 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005918 /* No need to check, dead is dead. */
5919 result = "dead";
5920 else if (job->jv_status == JOB_FAILED)
5921 result = "fail";
5922 else
5923 {
5924 result = mch_job_status(job);
5925 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005926 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005927 }
5928 return result;
5929}
5930
Bram Moolenaar8950a562016-03-12 15:22:55 +01005931/*
5932 * Implementation of job_info().
5933 */
5934 void
5935job_info(job_T *job, dict_T *dict)
5936{
5937 dictitem_T *item;
5938 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005939 list_T *l;
5940 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005941
Bram Moolenaare0be1672018-07-08 16:50:37 +02005942 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005943
5944 item = dictitem_alloc((char_u *)"channel");
5945 if (item == NULL)
5946 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005947 item->di_tv.v_type = VAR_CHANNEL;
5948 item->di_tv.vval.v_channel = job->jv_channel;
5949 if (job->jv_channel != NULL)
5950 ++job->jv_channel->ch_refcount;
5951 if (dict_add(dict, item) == FAIL)
5952 dictitem_free(item);
5953
5954#ifdef UNIX
5955 nr = job->jv_pid;
5956#else
5957 nr = job->jv_proc_info.dwProcessId;
5958#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005959 dict_add_number(dict, "process", nr);
5960 dict_add_string(dict, "tty_in", job->jv_tty_in);
5961 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005962
Bram Moolenaare0be1672018-07-08 16:50:37 +02005963 dict_add_number(dict, "exitval", job->jv_exitval);
5964 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5965 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005966#ifdef UNIX
5967 dict_add_string(dict, "termsig", job->jv_termsig);
5968#endif
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005969#ifdef WIN3264
5970 dict_add_string(dict, "tty_type", job->jv_tty_type);
5971#endif
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005972
5973 l = list_alloc();
5974 if (l != NULL)
5975 {
5976 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005977 if (job->jv_argv != NULL)
5978 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005979 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005980 }
5981}
5982
5983/*
5984 * Implementation of job_info() to return info for all jobs.
5985 */
5986 void
5987job_info_all(list_T *l)
5988{
5989 job_T *job;
5990 typval_T tv;
5991
5992 for (job = first_job; job != NULL; job = job->jv_next)
5993 {
5994 tv.v_type = VAR_JOB;
5995 tv.vval.v_job = job;
5996
5997 if (list_append_tv(l, &tv) != OK)
5998 return;
5999 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01006000}
6001
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006002/*
6003 * Send a signal to "job". Implements job_stop().
6004 * When "type" is not NULL use this for the type.
6005 * Otherwise use argvars[1] for the type.
6006 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006007 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006008job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006009{
6010 char_u *arg;
6011
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006012 if (type != NULL)
6013 arg = (char_u *)type;
6014 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006015 arg = (char_u *)"";
6016 else
6017 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006018 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006019 if (arg == NULL)
6020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006021 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006022 return 0;
6023 }
6024 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006025 if (job->jv_status == JOB_FAILED)
6026 {
6027 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6028 return 0;
6029 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006030 if (job->jv_status == JOB_ENDED)
6031 {
6032 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6033 return 0;
6034 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006035 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006036 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006037 return 0;
6038
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006039 /* Assume that only "kill" will kill the job. */
6040 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006041 job->jv_channel->ch_job_killed = TRUE;
6042
6043 /* We don't try freeing the job, obviously the caller still has a
6044 * reference to it. */
6045 return 1;
6046}
6047
Bram Moolenaarf2732452018-06-03 14:47:35 +02006048 void
6049invoke_prompt_callback(void)
6050{
6051 typval_T rettv;
6052 int dummy;
6053 typval_T argv[2];
6054 char_u *text;
6055 char_u *prompt;
6056 linenr_T lnum = curbuf->b_ml.ml_line_count;
6057
6058 // Add a new line for the prompt before invoking the callback, so that
6059 // text can always be inserted above the last line.
6060 ml_append(lnum, (char_u *)"", 0, FALSE);
6061 curwin->w_cursor.lnum = lnum + 1;
6062 curwin->w_cursor.col = 0;
6063
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006064 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006065 return;
6066 text = ml_get(lnum);
6067 prompt = prompt_text();
6068 if (STRLEN(text) >= STRLEN(prompt))
6069 text += STRLEN(prompt);
6070 argv[0].v_type = VAR_STRING;
6071 argv[0].vval.v_string = vim_strsave(text);
6072 argv[1].v_type = VAR_UNKNOWN;
6073
6074 call_func(curbuf->b_prompt_callback,
6075 (int)STRLEN(curbuf->b_prompt_callback),
6076 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
6077 curbuf->b_prompt_partial, NULL);
6078 clear_tv(&argv[0]);
6079 clear_tv(&rettv);
6080}
6081
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006082/*
6083 * Return TRUE when the interrupt callback was invoked.
6084 */
6085 int
6086invoke_prompt_interrupt(void)
6087{
6088 typval_T rettv;
6089 int dummy;
6090 typval_T argv[1];
6091
6092 if (curbuf->b_prompt_interrupt == NULL
6093 || *curbuf->b_prompt_interrupt == NUL)
6094 return FALSE;
6095 argv[0].v_type = VAR_UNKNOWN;
6096
6097 got_int = FALSE; // don't skip executing commands
6098 call_func(curbuf->b_prompt_interrupt,
6099 (int)STRLEN(curbuf->b_prompt_interrupt),
6100 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
6101 curbuf->b_prompt_int_partial, NULL);
6102 clear_tv(&rettv);
6103 return TRUE;
6104}
6105
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006106#endif /* FEAT_JOB_CHANNEL */