blob: e42c9575f94bf17ecfe6f7e6fa81da9699ebd029 [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 Moolenaar4f974752019-02-17 17:44:42 +010023#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +010024/* 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 Moolenaar5843f5f2019-08-20 20:13:45 +020058static ch_mode_T channel_get_mode(channel_T *channel, ch_part_T part);
59static int channel_get_timeout(channel_T *channel, ch_part_T part);
60static ch_part_T channel_part_send(channel_T *channel);
61static ch_part_T channel_part_read(channel_T *channel);
62static void free_job_options(jobopt_T *opt);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020063
Bram Moolenaar187db502016-02-27 14:44:26 +010064/* Whether a redraw is needed for appending a line to a buffer. */
65static int channel_need_redraw = FALSE;
66
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020067/* Whether we are inside channel_parse_messages() or another situation where it
68 * is safe to invoke callbacks. */
69static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010070
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020071static char *part_names[] = {"sock", "out", "err", "in"};
72
Bram Moolenaar4f974752019-02-17 17:44:42 +010073#ifdef MSWIN
Bram Moolenaard8070362016-02-15 21:56:54 +010074 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010075fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010076{
77 HANDLE h = (HANDLE)fd;
78 DWORD nread;
79
80 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
81 return -1;
82 return (int)nread;
83}
84
85 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010086fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010087{
Bram Moolenaar24058382019-01-24 23:11:49 +010088 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010090 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010091 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010092
Bram Moolenaar24058382019-01-24 23:11:49 +010093 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010094 {
Bram Moolenaar24058382019-01-24 23:11:49 +010095 if (todo > MAX_NAMED_PIPE_SIZE)
96 size = MAX_NAMED_PIPE_SIZE;
97 else
Bram Moolenaardec01202019-01-28 20:04:24 +010098 size = (DWORD)todo;
Bram Moolenaar65240682019-02-10 22:23:26 +010099 // If the pipe overflows while the job does not read the data,
100 // WriteFile() will block forever. This abandons the write.
Bram Moolenaar24058382019-01-24 23:11:49 +0100101 memset(&ov, 0, sizeof(ov));
Bram Moolenaar65240682019-02-10 22:23:26 +0100102 nwrite = 0;
Bram Moolenaar24058382019-01-24 23:11:49 +0100103 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
104 {
105 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100106
Bram Moolenaar24058382019-01-24 23:11:49 +0100107 if (err != ERROR_IO_PENDING)
108 return -1;
109 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
110 return -1;
111 FlushFileBuffers(h);
112 }
Bram Moolenaar65240682019-02-10 22:23:26 +0100113 else if (nwrite == 0)
114 // WriteFile() returns TRUE but did not write anything. This causes
115 // a hang, so bail out.
116 break;
Bram Moolenaar24058382019-01-24 23:11:49 +0100117 todo -= nwrite;
118 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100119 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100120 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100121}
122
123 static void
124fd_close(sock_T fd)
125{
126 HANDLE h = (HANDLE)fd;
127
128 CloseHandle(h);
129}
130#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100131
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100132/* Log file opened with ch_logfile(). */
133static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100134#ifdef FEAT_RELTIME
135static proftime_T log_start;
136#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137
138 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100139ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100140{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100141 FILE *file = NULL;
142
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100143 if (log_fd != NULL)
144 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100145
146 if (*fname != NUL)
147 {
148 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
149 if (file == NULL)
150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100151 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100152 return;
153 }
154 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100155 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100156
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100157 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100158 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100159 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100160#ifdef FEAT_RELTIME
161 profile_start(&log_start);
162#endif
163 }
164}
165
166 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200167ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168{
169 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100170}
171
172 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200173ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100174{
175 if (log_fd != NULL)
176 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100177#ifdef FEAT_RELTIME
178 proftime_T log_now;
179
180 profile_start(&log_now);
181 profile_sub(&log_now, &log_start);
182 fprintf(log_fd, "%s ", profile_msg(&log_now));
183#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100184 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200185 {
186 if (part < PART_COUNT)
187 fprintf(log_fd, "%son %d(%s): ",
188 what, ch->ch_id, part_names[part]);
189 else
190 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
191 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 else
193 fprintf(log_fd, "%s: ", what);
194 }
195}
196
Bram Moolenaar8caa43d2019-02-20 22:45:06 +0100197#ifndef PROTO // prototype is in proto.h
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100198 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200199ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100200{
201 if (log_fd != NULL)
202 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200203 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100204
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200205 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200206 va_start(ap, fmt);
207 vfprintf(log_fd, fmt, ap);
208 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100209 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 fflush(log_fd);
Bram Moolenaar101e9922019-09-25 21:43:11 +0200211 did_repeated_msg = 0;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100212 }
213}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200214#endif
215
216 static void
217ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200218#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
219 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200220#endif
221 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100222
223 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200224ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100225{
226 if (log_fd != NULL)
227 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200228 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200230 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200231 va_start(ap, fmt);
232 vfprintf(log_fd, fmt, ap);
233 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100234 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100235 fflush(log_fd);
Bram Moolenaar101e9922019-09-25 21:43:11 +0200236 did_repeated_msg = 0;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100237 }
238}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100239
Bram Moolenaar4f974752019-02-17 17:44:42 +0100240#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100241# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100242# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100243
244 static char *
245strerror_win32(int eno)
246{
247 static LPVOID msgbuf = NULL;
248 char_u *ptr;
249
250 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200251 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200253 msgbuf = NULL;
254 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100255 FormatMessage(
256 FORMAT_MESSAGE_ALLOCATE_BUFFER |
257 FORMAT_MESSAGE_FROM_SYSTEM |
258 FORMAT_MESSAGE_IGNORE_INSERTS,
259 NULL,
260 eno,
261 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
262 (LPTSTR) &msgbuf,
263 0,
264 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200265 if (msgbuf != NULL)
266 /* chomp \r or \n */
267 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
268 switch (*ptr)
269 {
270 case '\r':
271 STRMOVE(ptr, ptr + 1);
272 ptr--;
273 break;
274 case '\n':
275 if (*(ptr + 1) == '\0')
276 *ptr = '\0';
277 else
278 *ptr = ' ';
279 break;
280 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100281 return msgbuf;
282}
283#endif
284
Bram Moolenaar77073442016-02-13 23:23:53 +0100285/*
286 * The list of all allocated channels.
287 */
288static channel_T *first_channel = NULL;
289static int next_ch_id = 0;
290
291/*
292 * Allocate a new channel. The refcount is set to 1.
293 * The channel isn't actually used until it is opened.
294 * Returns NULL if out of memory.
295 */
296 channel_T *
297add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100298{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200299 ch_part_T part;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200300 channel_T *channel = ALLOC_CLEAR_ONE(channel_T);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100301
Bram Moolenaar77073442016-02-13 23:23:53 +0100302 if (channel == NULL)
303 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100304
Bram Moolenaar77073442016-02-13 23:23:53 +0100305 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100306 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100307
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200308 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100309 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100310 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100311#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#endif
314#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100315 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100316#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100318 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100319
Bram Moolenaar77073442016-02-13 23:23:53 +0100320 if (first_channel != NULL)
321 {
322 first_channel->ch_prev = channel;
323 channel->ch_next = first_channel;
324 }
325 first_channel = channel;
326
327 channel->ch_refcount = 1;
328 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100329}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100330
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200331 int
332has_any_channel(void)
333{
334 return first_channel != NULL;
335}
336
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100337/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100338 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100339 * Return TRUE if "channel" has a callback and the associated job wasn't
340 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100341 */
342 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100343channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100344{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100345 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100346 int has_out_msg;
347 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348
349 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100350 if (channel->ch_job_killed && channel->ch_job == NULL)
351 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100352
353 /* If there is a close callback it may still need to be invoked. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200354 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100355 return TRUE;
356
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200357 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200358 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200359 return TRUE;
360
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100361 /* If there is no callback then nobody can get readahead. If the fd is
362 * closed and there is no readahead then the callback won't be called. */
363 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100364 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
365 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
367 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
368 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
369 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
370 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
371 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200372 return (channel->ch_callback.cb_name != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100373 || has_out_msg || has_err_msg))
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200374 || ((channel->ch_part[PART_OUT].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200375 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
376 && has_out_msg)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200377 || ((channel->ch_part[PART_ERR].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200378 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
379 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100380}
381
382/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200383 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
384 */
385 static int
386channel_can_close(channel_T *channel)
387{
388 return channel->ch_to_be_closed == 0;
389}
390
391/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200392 * Close a channel and free all its resources.
393 */
394 static void
395channel_free_contents(channel_T *channel)
396{
397 channel_close(channel, TRUE);
398 channel_clear(channel);
399 ch_log(channel, "Freeing channel");
400}
401
402 static void
403channel_free_channel(channel_T *channel)
404{
405 if (channel->ch_next != NULL)
406 channel->ch_next->ch_prev = channel->ch_prev;
407 if (channel->ch_prev == NULL)
408 first_channel = channel->ch_next;
409 else
410 channel->ch_prev->ch_next = channel->ch_next;
411 vim_free(channel);
412}
413
414 static void
415channel_free(channel_T *channel)
416{
417 if (!in_free_unref_items)
418 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200419 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200420 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200421 else
422 {
423 channel_free_contents(channel);
424 channel_free_channel(channel);
425 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200426 }
427}
428
429/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100430 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100431 * possible, there is no callback to be invoked or the associated job was
432 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100435 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436channel_may_free(channel_T *channel)
437{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100438 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100439 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100441 return TRUE;
442 }
443 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100444}
445
446/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100447 * Decrement the reference count on "channel" and maybe free it when it goes
448 * down to zero. Don't free it if there is a pending action.
449 * Returns TRUE when the channel is no longer referenced.
450 */
451 int
452channel_unref(channel_T *channel)
453{
454 if (channel != NULL && --channel->ch_refcount <= 0)
455 return channel_may_free(channel);
456 return FALSE;
457}
458
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200459 int
460free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100461{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200462 int did_free = FALSE;
463 channel_T *ch;
464
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200465 /* This is invoked from the garbage collector, which only runs at a safe
466 * point. */
467 ++safe_to_invoke_callback;
468
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200469 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200470 if (!channel_still_useful(ch)
471 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200472 {
473 /* Free the channel and ordinary items it contains, but don't
474 * recurse into Lists, Dictionaries etc. */
475 channel_free_contents(ch);
476 did_free = TRUE;
477 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200478
479 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200480 return did_free;
481}
482
483 void
484free_unused_channels(int copyID, int mask)
485{
486 channel_T *ch;
487 channel_T *ch_next;
488
489 for (ch = first_channel; ch != NULL; ch = ch_next)
490 {
491 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200492 if (!channel_still_useful(ch)
493 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200494 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200495 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200496 channel_free_channel(ch);
497 }
498 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100499}
500
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100502
Bram Moolenaarea781452019-09-04 18:53:12 +0200503# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
504/*
505 * Lookup the channel from the socket. Set "partp" to the fd index.
506 * Returns NULL when the socket isn't found.
507 */
508 static channel_T *
509channel_fd2channel(sock_T fd, ch_part_T *partp)
510{
511 channel_T *channel;
512 ch_part_T part;
513
514 if (fd != INVALID_FD)
515 for (channel = first_channel; channel != NULL;
516 channel = channel->ch_next)
517 {
518 for (part = PART_SOCK; part < PART_IN; ++part)
519 if (channel->ch_part[part].ch_fd == fd)
520 {
521 *partp = part;
522 return channel;
523 }
524 }
525 return NULL;
526}
527
Bram Moolenaar77073442016-02-13 23:23:53 +0100528 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100529channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100530{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100531 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200532 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100533
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100534 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100535 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200536 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100537 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100539}
Bram Moolenaarea781452019-09-04 18:53:12 +0200540# endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100541
Bram Moolenaare0874f82016-01-24 20:36:41 +0100542/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100543 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100544 */
Bram Moolenaarea781452019-09-04 18:53:12 +0200545# ifdef FEAT_GUI_X11
Bram Moolenaard04a0202016-01-26 23:30:18 +0100546 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200547messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200548 int *unused1 UNUSED,
549 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100550{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100551 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100552}
Bram Moolenaarea781452019-09-04 18:53:12 +0200553# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100554
Bram Moolenaarea781452019-09-04 18:53:12 +0200555# ifdef FEAT_GUI_GTK
556# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100557 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200558messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200559 GIOCondition unused2 UNUSED,
560 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100561{
562 channel_read_fd(GPOINTER_TO_INT(clientData));
563 return TRUE; /* Return FALSE instead in case the event source is to
564 * be removed after this function returns. */
565}
Bram Moolenaarea781452019-09-04 18:53:12 +0200566# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100567 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200568messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200569 gint unused1 UNUSED,
570 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100571{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100572 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100573}
Bram Moolenaarea781452019-09-04 18:53:12 +0200574# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100575# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100576
577 static void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200578channel_gui_register_one(channel_T *channel, ch_part_T part UNUSED)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100579{
Bram Moolenaarde279892016-03-11 22:19:44 +0100580 if (!CH_HAS_GUI)
581 return;
582
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200583 /* gets stuck in handling events for a not connected channel */
584 if (channel->ch_keep_open)
585 return;
586
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100587# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200588 /* Tell notifier we are interested in being called when there is input on
589 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100590 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200591 {
592 ch_log(channel, "Registering part %s with fd %d",
593 part_names[part], channel->ch_part[part].ch_fd);
594
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100595 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100596 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100597 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100598 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200599 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100600 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200601 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100602# else
603# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200604 /* Tell gdk we are interested in being called when there is input on the
605 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100606 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100607 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200608 ch_log(channel, "Registering part %s with fd %d",
609 part_names[part], channel->ch_part[part].ch_fd);
610# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100611 GIOChannel *chnnl = g_io_channel_unix_new(
612 (gint)channel->ch_part[part].ch_fd);
613
614 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
615 chnnl,
616 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200617 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100618 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
619
620 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100621# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100622 channel->ch_part[part].ch_inputHandler = gdk_input_add(
623 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100624 (GdkInputCondition)
625 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200626 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100627 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100628# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200629 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100630# endif
631# endif
632}
633
Bram Moolenaarde279892016-03-11 22:19:44 +0100634 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100635channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100636{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100637 if (channel->CH_SOCK_FD != INVALID_FD)
638 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200639 if (channel->CH_OUT_FD != INVALID_FD
640 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100641 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200642 if (channel->CH_ERR_FD != INVALID_FD
643 && channel->CH_ERR_FD != channel->CH_SOCK_FD
644 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100645 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646}
647
648/*
649 * Register any of our file descriptors with the GUI event handling system.
650 * Called when the GUI has started.
651 */
652 void
653channel_gui_register_all(void)
654{
Bram Moolenaar77073442016-02-13 23:23:53 +0100655 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100656
Bram Moolenaar77073442016-02-13 23:23:53 +0100657 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100658 channel_gui_register(channel);
659}
660
661 static void
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200662channel_gui_unregister_one(channel_T *channel UNUSED, ch_part_T part UNUSED)
Bram Moolenaarde279892016-03-11 22:19:44 +0100663{
664# ifdef FEAT_GUI_X11
665 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
666 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200667 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100668 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
669 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
670 }
671# else
672# ifdef FEAT_GUI_GTK
673 if (channel->ch_part[part].ch_inputHandler != 0)
674 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200675 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100676# if GTK_CHECK_VERSION(3,0,0)
677 g_source_remove(channel->ch_part[part].ch_inputHandler);
678# else
679 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
680# endif
681 channel->ch_part[part].ch_inputHandler = 0;
682 }
683# endif
684# endif
685}
686
687 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100688channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100689{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200690 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100691
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100692 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100693 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100694}
695
Bram Moolenaarea781452019-09-04 18:53:12 +0200696#endif // FEAT_GUI
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100698static char *e_cannot_connect = N_("E902: Cannot connect to port");
699
Bram Moolenaard04a0202016-01-26 23:30:18 +0100700/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100701 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100702 * "waittime" is the time in msec to wait for the connection.
703 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100704 * Returns the channel for success.
705 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100706 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100707 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100708channel_open(
709 char *hostname,
710 int port_in,
711 int waittime,
712 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100713{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100714 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100715 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100716 struct hostent *host;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100717#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100718 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100719 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100720#else
721 int port = port_in;
722#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100723 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100724 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100725
Bram Moolenaar4f974752019-02-17 17:44:42 +0100726#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100727 channel_init_winsock();
728#endif
729
Bram Moolenaar77073442016-02-13 23:23:53 +0100730 channel = add_channel();
731 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100732 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100733 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100734 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100735 }
736
737 /* Get the server internet address and put into addr structure */
738 /* fill in the socket address structure and connect to server */
739 vim_memset((char *)&server, 0, sizeof(server));
740 server.sin_family = AF_INET;
741 server.sin_port = htons(port);
742 if ((host = gethostbyname(hostname)) == NULL)
743 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100744 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200745 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100746 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100747 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100748 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100749 {
750 char *p;
751
Bram Moolenaar2e324952018-04-14 14:37:07 +0200752 /* When using host->h_addr_list[0] directly ubsan warns for it to not
753 * be aligned. First copy the pointer to avoid that. */
754 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100755 memcpy((char *)&server.sin_addr, p, host->h_length);
756 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100757
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100758 /* On Mac and Solaris a zero timeout almost never works. At least wait
759 * one millisecond. Let's do it for all systems, because we don't know why
760 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100761 if (waittime == 0)
762 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763
764 /*
765 * For Unix we need to call connect() again after connect() failed.
766 * On Win32 one time is sufficient.
767 */
768 while (TRUE)
769 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100770 long elapsed_msec = 0;
771 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100772
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100773 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100774 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100775 sd = socket(AF_INET, SOCK_STREAM, 0);
776 if (sd == -1)
777 {
778 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200779 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100780 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100781 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100782 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100783
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 if (waittime >= 0)
785 {
786 /* Make connect() non-blocking. */
787 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +0100788#ifdef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100789 ioctlsocket(sd, FIONBIO, &val) < 0
790#else
791 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
792#endif
793 )
794 {
795 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200796 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100797 "channel_open: Connect failed with errno %d", errno);
798 sock_close(sd);
799 channel_free(channel);
800 return NULL;
801 }
802 }
803
804 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200805 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
807
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 if (ret == 0)
809 /* The connection could be established. */
810 break;
811
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100812 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100813 if (waittime < 0 || (errno != EWOULDBLOCK
814 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100815#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100816 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100817#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100818 ))
819 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200820 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100821 "channel_open: Connect failed with errno %d", errno);
822 PERROR(_(e_cannot_connect));
823 sock_close(sd);
824 channel_free(channel);
825 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100826 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100827
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100828 /* Limit the waittime to 50 msec. If it doesn't work within this
829 * time we close the socket and try creating it again. */
830 waitnow = waittime > 50 ? 50 : waittime;
831
Bram Moolenaar045a2842016-03-08 22:33:07 +0100832 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100833 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100834#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100835 if (errno != ECONNREFUSED)
836#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100837 {
838 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100839 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100840 fd_set wfds;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100841#ifndef MSWIN
Bram Moolenaard42119f2016-02-28 20:51:49 +0100842 int so_error = 0;
843 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100844 struct timeval start_tv;
845 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100846#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100847 FD_ZERO(&rfds);
848 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100849 FD_ZERO(&wfds);
850 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100851
Bram Moolenaar562ca712016-03-09 21:50:05 +0100852 tv.tv_sec = waitnow / 1000;
853 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100854#ifndef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100855 gettimeofday(&start_tv, NULL);
856#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200857 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100858 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100859 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100860
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100861 if (ret < 0)
862 {
863 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200864 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100865 "channel_open: Connect failed with errno %d", errno);
866 PERROR(_(e_cannot_connect));
867 sock_close(sd);
868 channel_free(channel);
869 return NULL;
870 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100871
Bram Moolenaar4f974752019-02-17 17:44:42 +0100872#ifdef MSWIN
Bram Moolenaar562ca712016-03-09 21:50:05 +0100873 /* On Win32: select() is expected to work and wait for up to
874 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875 if (FD_ISSET(sd, &wfds))
876 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100877 elapsed_msec = waitnow;
878 if (waittime > 1 && elapsed_msec < waittime)
879 {
880 waittime -= elapsed_msec;
881 continue;
882 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100883#else
884 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100885 * After putting the socket in non-blocking mode, connect() will
886 * return EINPROGRESS, select() will not wait (as if writing is
887 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100888 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100889 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100890 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100891 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100892 {
893 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100894 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100895 if (ret < 0 || (so_error != 0
896 && so_error != EWOULDBLOCK
897 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100898# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100899 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100900# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100901 ))
902 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200903 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100904 "channel_open: Connect failed with errno %d",
905 so_error);
906 PERROR(_(e_cannot_connect));
907 sock_close(sd);
908 channel_free(channel);
909 return NULL;
910 }
911 }
912
Bram Moolenaar045a2842016-03-08 22:33:07 +0100913 if (FD_ISSET(sd, &wfds) && so_error == 0)
914 /* Did not detect an error, connection is established. */
915 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100916
Bram Moolenaar045a2842016-03-08 22:33:07 +0100917 gettimeofday(&end_tv, NULL);
918 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
919 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100920#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100921 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100922
Bram Moolenaar4f974752019-02-17 17:44:42 +0100923#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100924 if (waittime > 1 && elapsed_msec < waittime)
925 {
926 /* The port isn't ready but we also didn't get an error.
927 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100928 * yet. Select() may return early, wait until the remaining
929 * "waitnow" and try again. */
930 waitnow -= elapsed_msec;
931 waittime -= elapsed_msec;
932 if (waitnow > 0)
933 {
934 mch_delay((long)waitnow, TRUE);
935 ui_breakcheck();
936 waittime -= waitnow;
937 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100938 if (!got_int)
939 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100940 if (waittime <= 0)
941 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100942 waittime = 1;
943 continue;
944 }
945 /* we were interrupted, behave as if timed out */
946 }
947#endif
948
949 /* We timed out. */
950 ch_error(channel, "Connection timed out");
951 sock_close(sd);
952 channel_free(channel);
953 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100954 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100955
Bram Moolenaar045a2842016-03-08 22:33:07 +0100956 ch_log(channel, "Connection made");
957
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100958 if (waittime >= 0)
959 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100960#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100961 val = 0;
962 ioctlsocket(sd, FIONBIO, &val);
963#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100964 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100965#endif
966 }
967
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100968 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100969 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100970 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
971 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200972 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100973
974#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100975 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100976#endif
977
Bram Moolenaar77073442016-02-13 23:23:53 +0100978 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100979}
980
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100981/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +0200982 * Copy callback from "src" to "dest", incrementing the refcounts.
983 */
984 static void
985copy_callback(callback_T *dest, callback_T *src)
986{
987 dest->cb_partial = src->cb_partial;
988 if (dest->cb_partial != NULL)
989 {
990 dest->cb_name = src->cb_name;
991 dest->cb_free_name = FALSE;
992 ++dest->cb_partial->pt_refcount;
993 }
994 else
995 {
996 dest->cb_name = vim_strsave(src->cb_name);
997 dest->cb_free_name = TRUE;
998 func_ref(src->cb_name);
999 }
1000}
1001
1002 static void
1003free_set_callback(callback_T *cbp, callback_T *callback)
1004{
1005 free_callback(cbp);
1006
1007 if (callback->cb_name != NULL && *callback->cb_name != NUL)
1008 copy_callback(cbp, callback);
1009 else
1010 cbp->cb_name = NULL;
1011}
1012
1013/*
1014 * Prepare buffer "buf" for writing channel output to.
1015 */
1016 static void
1017prepare_buffer(buf_T *buf)
1018{
1019 buf_T *save_curbuf = curbuf;
1020
1021 buf_copy_options(buf, BCO_ENTER);
1022 curbuf = buf;
1023#ifdef FEAT_QUICKFIX
1024 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1025 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1026#endif
1027 if (curbuf->b_ml.ml_mfp == NULL)
1028 ml_open(curbuf);
1029 curbuf = save_curbuf;
1030}
1031
1032/*
1033 * Find a buffer matching "name" or create a new one.
1034 * Returns NULL if there is something very wrong (error already reported).
1035 */
1036 static buf_T *
Bram Moolenaar261f3462019-09-07 15:45:32 +02001037channel_find_buffer(char_u *name, int err, int msg)
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02001038{
1039 buf_T *buf = NULL;
1040 buf_T *save_curbuf = curbuf;
1041
1042 if (name != NULL && *name != NUL)
1043 {
1044 buf = buflist_findname(name);
1045 if (buf == NULL)
1046 buf = buflist_findname_exp(name);
1047 }
1048 if (buf == NULL)
1049 {
1050 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
1051 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
1052 if (buf == NULL)
1053 return NULL;
1054 prepare_buffer(buf);
1055
1056 curbuf = buf;
1057 if (msg)
1058 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1059 : "Reading from channel output..."), TRUE);
1060 changed_bytes(1, 0);
1061 curbuf = save_curbuf;
1062 }
1063
1064 return buf;
1065}
1066
1067/*
1068 * Set various properties from an "opt" argument.
1069 */
1070 static void
1071channel_set_options(channel_T *channel, jobopt_T *opt)
1072{
1073 ch_part_T part;
1074
1075 if (opt->jo_set & JO_MODE)
1076 for (part = PART_SOCK; part < PART_COUNT; ++part)
1077 channel->ch_part[part].ch_mode = opt->jo_mode;
1078 if (opt->jo_set & JO_IN_MODE)
1079 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1080 if (opt->jo_set & JO_OUT_MODE)
1081 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1082 if (opt->jo_set & JO_ERR_MODE)
1083 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
1084 channel->ch_nonblock = opt->jo_noblock;
1085
1086 if (opt->jo_set & JO_TIMEOUT)
1087 for (part = PART_SOCK; part < PART_COUNT; ++part)
1088 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1089 if (opt->jo_set & JO_OUT_TIMEOUT)
1090 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1091 if (opt->jo_set & JO_ERR_TIMEOUT)
1092 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1093 if (opt->jo_set & JO_BLOCK_WRITE)
1094 channel->ch_part[PART_IN].ch_block_write = 1;
1095
1096 if (opt->jo_set & JO_CALLBACK)
1097 free_set_callback(&channel->ch_callback, &opt->jo_callback);
1098 if (opt->jo_set & JO_OUT_CALLBACK)
1099 free_set_callback(&channel->ch_part[PART_OUT].ch_callback,
1100 &opt->jo_out_cb);
1101 if (opt->jo_set & JO_ERR_CALLBACK)
1102 free_set_callback(&channel->ch_part[PART_ERR].ch_callback,
1103 &opt->jo_err_cb);
1104 if (opt->jo_set & JO_CLOSE_CALLBACK)
1105 free_set_callback(&channel->ch_close_cb, &opt->jo_close_cb);
1106 channel->ch_drop_never = opt->jo_drop_never;
1107
1108 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1109 {
1110 buf_T *buf;
1111
1112 /* writing output to a buffer. Default mode is NL. */
1113 if (!(opt->jo_set & JO_OUT_MODE))
1114 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
1115 if (opt->jo_set & JO_OUT_BUF)
1116 {
1117 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1118 if (buf == NULL)
1119 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1120 }
1121 else
1122 {
1123 int msg = TRUE;
1124
1125 if (opt->jo_set2 & JO2_OUT_MSG)
1126 msg = opt->jo_message[PART_OUT];
Bram Moolenaar261f3462019-09-07 15:45:32 +02001127 buf = channel_find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02001128 }
1129 if (buf != NULL)
1130 {
1131 if (opt->jo_set & JO_OUT_MODIFIABLE)
1132 channel->ch_part[PART_OUT].ch_nomodifiable =
1133 !opt->jo_modifiable[PART_OUT];
1134
1135 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1136 {
1137 emsg(_(e_modifiable));
1138 }
1139 else
1140 {
1141 ch_log(channel, "writing out to buffer '%s'",
1142 (char *)buf->b_ffname);
1143 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1144 // if the buffer was deleted or unloaded resurrect it
1145 if (buf->b_ml.ml_mfp == NULL)
1146 prepare_buffer(buf);
1147 }
1148 }
1149 }
1150
1151 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1152 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1153 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1154 {
1155 buf_T *buf;
1156
1157 /* writing err to a buffer. Default mode is NL. */
1158 if (!(opt->jo_set & JO_ERR_MODE))
1159 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1160 if (opt->jo_io[PART_ERR] == JIO_OUT)
1161 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1162 else if (opt->jo_set & JO_ERR_BUF)
1163 {
1164 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1165 if (buf == NULL)
1166 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1167 }
1168 else
1169 {
1170 int msg = TRUE;
1171
1172 if (opt->jo_set2 & JO2_ERR_MSG)
1173 msg = opt->jo_message[PART_ERR];
Bram Moolenaar261f3462019-09-07 15:45:32 +02001174 buf = channel_find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02001175 }
1176 if (buf != NULL)
1177 {
1178 if (opt->jo_set & JO_ERR_MODIFIABLE)
1179 channel->ch_part[PART_ERR].ch_nomodifiable =
1180 !opt->jo_modifiable[PART_ERR];
1181 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1182 {
1183 emsg(_(e_modifiable));
1184 }
1185 else
1186 {
1187 ch_log(channel, "writing err to buffer '%s'",
1188 (char *)buf->b_ffname);
1189 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1190 // if the buffer was deleted or unloaded resurrect it
1191 if (buf->b_ml.ml_mfp == NULL)
1192 prepare_buffer(buf);
1193 }
1194 }
1195 }
1196
1197 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1198 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1199 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
1200}
1201
1202/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001203 * Implements ch_open().
1204 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001205 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001206channel_open_func(typval_T *argvars)
1207{
1208 char_u *address;
1209 char_u *p;
1210 char *rest;
1211 int port;
1212 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001213 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001214
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001215 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001216 if (argvars[1].v_type != VAR_UNKNOWN
1217 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
1218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001220 return NULL;
1221 }
1222
1223 /* parse address */
1224 p = vim_strchr(address, ':');
1225 if (p == NULL)
1226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001228 return NULL;
1229 }
1230 *p++ = NUL;
1231 port = strtol((char *)p, &rest, 10);
1232 if (*address == NUL || port <= 0 || *rest != NUL)
1233 {
1234 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001236 return NULL;
1237 }
1238
1239 /* parse options */
1240 clear_job_options(&opt);
1241 opt.jo_mode = MODE_JSON;
1242 opt.jo_timeout = 2000;
1243 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02001244 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001245 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001246 if (opt.jo_timeout < 0)
1247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001249 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001250 }
1251
1252 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1253 if (channel != NULL)
1254 {
1255 opt.jo_set = JO_ALL;
1256 channel_set_options(channel, &opt);
1257 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001258theend:
1259 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001260 return channel;
1261}
1262
Bram Moolenaarde279892016-03-11 22:19:44 +01001263 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001264ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001265{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001266 sock_T *fd = &channel->ch_part[part].ch_fd;
1267
Bram Moolenaarde279892016-03-11 22:19:44 +01001268 if (*fd != INVALID_FD)
1269 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001270 if (part == PART_SOCK)
1271 sock_close(*fd);
1272 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001273 {
1274 /* When using a pty the same FD is set on multiple parts, only
1275 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001276 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1277 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1278 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001279 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001280#ifdef MSWIN
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001281 if (channel->ch_named_pipe)
1282 DisconnectNamedPipe((HANDLE)fd);
1283#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001284 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001285 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001286 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001287 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001288
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001289 /* channel is closed, may want to end the job if it was the last */
1290 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001291 }
1292}
1293
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001294 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001295channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001296{
Bram Moolenaarde279892016-03-11 22:19:44 +01001297 if (in != INVALID_FD)
1298 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001299 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001300 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001301# if defined(UNIX)
1302 /* Do not end the job when all output channels are closed, wait until
1303 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001304 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001305 channel->ch_to_be_closed |= (1U << PART_IN);
1306# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001307 }
1308 if (out != INVALID_FD)
1309 {
1310# if defined(FEAT_GUI)
1311 channel_gui_unregister_one(channel, PART_OUT);
1312# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001313 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001314 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001315 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001316# if defined(FEAT_GUI)
1317 channel_gui_register_one(channel, PART_OUT);
1318# endif
1319 }
1320 if (err != INVALID_FD)
1321 {
1322# if defined(FEAT_GUI)
1323 channel_gui_unregister_one(channel, PART_ERR);
1324# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001325 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001326 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001327 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001328# if defined(FEAT_GUI)
1329 channel_gui_register_one(channel, PART_ERR);
1330# endif
1331 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001332}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001333
Bram Moolenaard6051b52016-02-28 15:49:03 +01001334/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001335 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001336 * This does not keep a refcount, when the job is freed ch_job is cleared.
1337 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001338 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001339channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001340{
Bram Moolenaar77073442016-02-13 23:23:53 +01001341 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001342
1343 channel_set_options(channel, options);
1344
1345 if (job->jv_in_buf != NULL)
1346 {
1347 chanpart_T *in_part = &channel->ch_part[PART_IN];
1348
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001349 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001350 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001351 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001352 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001353 {
1354 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1355 {
1356 /* Special mode: send last-but-one line when appending a line
1357 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001358 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001359 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001360 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001361 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001362 }
1363 else
1364 in_part->ch_buf_top = options->jo_in_top;
1365 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001366 else
1367 in_part->ch_buf_top = 1;
1368 if (options->jo_set & JO_IN_BOT)
1369 in_part->ch_buf_bot = options->jo_in_bot;
1370 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001371 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001372 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001373}
1374
1375/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001376 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001377 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001378 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001379channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001380 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001381 ch_part_T part,
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001382 callback_T *callback,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001383 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001384{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001385 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001386 cbq_T *item = ALLOC_ONE(cbq_T);
Bram Moolenaara07fec92016-02-05 21:04:08 +01001387
1388 if (item != NULL)
1389 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001390 copy_callback(&item->cq_callback, callback);
Bram Moolenaar77073442016-02-13 23:23:53 +01001391 item->cq_seq_nr = id;
1392 item->cq_prev = head->cq_prev;
1393 head->cq_prev = item;
1394 item->cq_next = NULL;
1395 if (item->cq_prev == NULL)
1396 head->cq_next = item;
1397 else
1398 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001399 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001400}
1401
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001402 static void
1403write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1404{
1405 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001406 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001407 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001408 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001409
Bram Moolenaar655da312016-05-28 22:22:34 +02001410 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411 if ((p = alloc(len + 2)) == NULL)
1412 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001413 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001414
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001415 if (channel->ch_write_text_mode)
1416 p[len] = CAR;
1417 else
1418 {
1419 for (i = 0; i < len; ++i)
1420 if (p[i] == NL)
1421 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001422
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001423 p[len] = NL;
1424 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001425 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001426 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001427 vim_free(p);
1428}
1429
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001430/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001431 * Return TRUE if "channel" can be written to.
1432 * Returns FALSE if the input is closed or the write would block.
1433 */
1434 static int
1435can_write_buf_line(channel_T *channel)
1436{
1437 chanpart_T *in_part = &channel->ch_part[PART_IN];
1438
1439 if (in_part->ch_fd == INVALID_FD)
1440 return FALSE; /* pipe was closed */
1441
1442 /* for testing: block every other attempt to write */
1443 if (in_part->ch_block_write == 1)
1444 in_part->ch_block_write = -1;
1445 else if (in_part->ch_block_write == -1)
1446 in_part->ch_block_write = 1;
1447
1448 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001449#ifndef MSWIN
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001450 {
1451# if defined(HAVE_SELECT)
1452 struct timeval tval;
1453 fd_set wfds;
1454 int ret;
1455
1456 FD_ZERO(&wfds);
1457 FD_SET((int)in_part->ch_fd, &wfds);
1458 tval.tv_sec = 0;
1459 tval.tv_usec = 0;
1460 for (;;)
1461 {
1462 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1463# ifdef EINTR
1464 SOCK_ERRNO;
1465 if (ret == -1 && errno == EINTR)
1466 continue;
1467# endif
1468 if (ret <= 0 || in_part->ch_block_write == 1)
1469 {
1470 if (ret > 0)
1471 ch_log(channel, "FAKED Input not ready for writing");
1472 else
1473 ch_log(channel, "Input not ready for writing");
1474 return FALSE;
1475 }
1476 break;
1477 }
1478# else
1479 struct pollfd fds;
1480
1481 fds.fd = in_part->ch_fd;
1482 fds.events = POLLOUT;
1483 if (poll(&fds, 1, 0) <= 0)
1484 {
1485 ch_log(channel, "Input not ready for writing");
1486 return FALSE;
1487 }
1488 if (in_part->ch_block_write == 1)
1489 {
1490 ch_log(channel, "FAKED Input not ready for writing");
1491 return FALSE;
1492 }
1493# endif
1494 }
1495#endif
1496 return TRUE;
1497}
1498
1499/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001500 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001501 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001502 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001503channel_write_in(channel_T *channel)
1504{
1505 chanpart_T *in_part = &channel->ch_part[PART_IN];
1506 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001507 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001508 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001509
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001510 if (buf == NULL || in_part->ch_buf_append)
1511 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001512 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001513 {
1514 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001515 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001516 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001517 return;
1518 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001519
1520 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1521 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1522 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001523 if (!can_write_buf_line(channel))
1524 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001525 write_buf_line(buf, lnum, channel);
1526 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001527 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001528
1529 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001530 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001531 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001532 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001533
Bram Moolenaar014069a2016-03-03 22:51:40 +01001534 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001535 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001536 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001537#if defined(FEAT_TERMINAL)
1538 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001539 if (channel->ch_job != NULL)
1540 term_send_eof(channel);
1541#endif
1542
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001543 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001544 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001545 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001546
1547 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001548 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001549 }
1550 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001551 ch_log(channel, "Still %ld more lines to write",
1552 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001553}
1554
1555/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001556 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001557 */
1558 void
1559channel_buffer_free(buf_T *buf)
1560{
1561 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001562 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001563
1564 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001565 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001566 {
1567 chanpart_T *ch_part = &channel->ch_part[part];
1568
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001569 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001570 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001571 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001572 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001573 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001574 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001575 }
1576}
1577
1578/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001579 * Write any lines waiting to be written to "channel".
1580 */
1581 static void
1582channel_write_input(channel_T *channel)
1583{
1584 chanpart_T *in_part = &channel->ch_part[PART_IN];
1585
1586 if (in_part->ch_writeque.wq_next != NULL)
1587 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1588 else if (in_part->ch_bufref.br_buf != NULL)
1589 {
1590 if (in_part->ch_buf_append)
1591 channel_write_new_lines(in_part->ch_bufref.br_buf);
1592 else
1593 channel_write_in(channel);
1594 }
1595}
1596
1597/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001598 * Write any lines waiting to be written to a channel.
1599 */
1600 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001601channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001602{
1603 channel_T *channel;
1604
1605 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001606 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001607}
1608
1609/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001610 * Write appended lines above the last one in "buf" to the channel.
1611 */
1612 void
1613channel_write_new_lines(buf_T *buf)
1614{
1615 channel_T *channel;
1616 int found_one = FALSE;
1617
1618 /* There could be more than one channel for the buffer, loop over all of
1619 * them. */
1620 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1621 {
1622 chanpart_T *in_part = &channel->ch_part[PART_IN];
1623 linenr_T lnum;
1624 int written = 0;
1625
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001626 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001627 {
1628 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001629 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001630 found_one = TRUE;
1631 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1632 ++lnum)
1633 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001634 if (!can_write_buf_line(channel))
1635 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001636 write_buf_line(buf, lnum, channel);
1637 ++written;
1638 }
1639
1640 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001641 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001642 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001643 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001644 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001645 ch_log(channel, "Still %ld more lines to write",
1646 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001647
1648 in_part->ch_buf_bot = lnum;
1649 }
1650 }
1651 if (!found_one)
1652 buf->b_write_to_channel = FALSE;
1653}
1654
1655/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001656 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001657 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001658 */
1659 static void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001660invoke_callback(channel_T *channel, callback_T *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001661{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001662 typval_T rettv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001663
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001664 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001665 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001666
Bram Moolenaar77073442016-02-13 23:23:53 +01001667 argv[0].v_type = VAR_CHANNEL;
1668 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001669
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001670 call_callback(callback, -1, &rettv, 2, argv);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001671 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001672 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001673}
1674
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001675/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001676 * Return the first node from "channel"/"part" without removing it.
1677 * Returns NULL if there is nothing.
1678 */
1679 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001680channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001681{
1682 readq_T *head = &channel->ch_part[part].ch_head;
1683
1684 return head->rq_next;
1685}
1686
1687/*
1688 * Return a pointer to the first NL in "node".
1689 * Skips over NUL characters.
1690 * Returns NULL if there is no NL.
1691 */
1692 char_u *
1693channel_first_nl(readq_T *node)
1694{
1695 char_u *buffer = node->rq_buffer;
1696 long_u i;
1697
1698 for (i = 0; i < node->rq_buflen; ++i)
1699 if (buffer[i] == NL)
1700 return buffer + i;
1701 return NULL;
1702}
1703
1704/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001705 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706 * The caller must free it.
1707 * Returns NULL if there is nothing.
1708 */
1709 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001711{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001712 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001713 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001714 char_u *p;
1715
Bram Moolenaar77073442016-02-13 23:23:53 +01001716 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001717 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001718 if (outlen != NULL)
1719 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001720 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001721 p = node->rq_buffer;
1722 head->rq_next = node->rq_next;
1723 if (node->rq_next == NULL)
1724 head->rq_prev = NULL;
1725 else
1726 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001727 vim_free(node);
1728 return p;
1729}
1730
1731/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001732 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001733 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001734 */
1735 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001737{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001738 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001739 readq_T *node;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001740 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001741 char_u *res;
1742 char_u *p;
1743
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001744 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001745 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001746 len += node->rq_buflen;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001747 res = alloc(len + 1);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001748 if (res == NULL)
1749 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001750 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001751 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001752 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001753 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001754 p += node->rq_buflen;
1755 }
1756 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001757
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001758 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001759 do
1760 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001761 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001762 vim_free(p);
1763 } while (p != NULL);
1764
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001765 if (outlen != NULL)
1766 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001767 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001768 *outlen += len;
1769 return res;
1770 }
1771
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001772 // Turn all NUL into NL, so that the result can be used as a string.
1773 p = res;
1774 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001775 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001776 if (*p == NUL)
1777 *p = NL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001778#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001779 else if (*p == 0x1b)
1780 {
1781 // crush the escape sequence OSC 0/1/2: ESC ]0;
1782 if (p + 3 < res + len
1783 && p[1] == ']'
1784 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1785 && p[3] == ';')
1786 {
1787 // '\a' becomes a NL
1788 while (p < res + (len - 1) && *p != '\a')
1789 ++p;
1790 // BEL is zero width characters, suppress display mistake
1791 // ConPTY (after 10.0.18317) requires advance checking
1792 if (p[-1] == NUL)
1793 p[-1] = 0x07;
1794 }
1795 }
1796#endif
1797 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001798 }
1799
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001800 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801}
1802
1803/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001804 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001805 * Caller must check these bytes are available.
1806 */
1807 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001808channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001809{
1810 readq_T *head = &channel->ch_part[part].ch_head;
1811 readq_T *node = head->rq_next;
1812 char_u *buf = node->rq_buffer;
1813
1814 mch_memmove(buf, buf + len, node->rq_buflen - len);
1815 node->rq_buflen -= len;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001816 node->rq_buffer[node->rq_buflen] = NUL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001817}
1818
1819/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001820 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001821 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001822 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001823 */
1824 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001825channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001826{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001827 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001828 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001829 readq_T *last_node;
1830 readq_T *n;
1831 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001832 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001833 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001834
Bram Moolenaar77073442016-02-13 23:23:53 +01001835 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836 return FAIL;
1837
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001838 last_node = node->rq_next;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001839 len = node->rq_buflen + last_node->rq_buflen;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001840 if (want_nl)
1841 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001842 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001843 {
1844 last_node = last_node->rq_next;
1845 len += last_node->rq_buflen;
1846 }
1847
Bram Moolenaar772153f2019-03-04 12:09:49 +01001848 p = newbuf = alloc(len + 1);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001849 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001850 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001851 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001852 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001853 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001854 node->rq_buffer = newbuf;
1855 for (n = node; n != last_node; )
1856 {
1857 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001858 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001859 p += n->rq_buflen;
1860 vim_free(n->rq_buffer);
1861 }
Bram Moolenaar772153f2019-03-04 12:09:49 +01001862 *p = NUL;
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001863 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001864
1865 /* dispose of the collapsed nodes and their buffers */
1866 for (n = node->rq_next; n != last_node; )
1867 {
1868 n = n->rq_next;
1869 vim_free(n->rq_prev);
1870 }
1871 node->rq_next = last_node->rq_next;
1872 if (last_node->rq_next == NULL)
1873 head->rq_prev = node;
1874 else
1875 last_node->rq_next->rq_prev = node;
1876 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001877 return OK;
1878}
1879
1880/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001881 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001882 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001883 * Returns OK or FAIL.
1884 */
1885 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001886channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001887 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001888{
1889 readq_T *node;
1890 readq_T *head = &channel->ch_part[part].ch_head;
1891 char_u *p;
1892 int i;
1893
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001894 node = ALLOC_ONE(readq_T);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001895 if (node == NULL)
1896 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001897 /* A NUL is added at the end, because netbeans code expects that.
1898 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001899 node->rq_buffer = alloc(len + 1);
1900 if (node->rq_buffer == NULL)
1901 {
1902 vim_free(node);
1903 return FAIL; /* out of memory */
1904 }
1905
1906 if (channel->ch_part[part].ch_mode == MODE_NL)
1907 {
1908 /* Drop any CR before a NL. */
1909 p = node->rq_buffer;
1910 for (i = 0; i < len; ++i)
1911 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1912 *p++ = buf[i];
1913 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001914 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001915 }
1916 else
1917 {
1918 mch_memmove(node->rq_buffer, buf, len);
1919 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001920 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001921 }
1922
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001923 if (prepend)
1924 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001925 // prepend node to the head of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001926 node->rq_next = head->rq_next;
1927 node->rq_prev = NULL;
1928 if (head->rq_next == NULL)
1929 head->rq_prev = node;
1930 else
1931 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001932 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001933 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001934 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001935 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001936 // append node to the tail of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001937 node->rq_next = NULL;
1938 node->rq_prev = head->rq_prev;
1939 if (head->rq_prev == NULL)
1940 head->rq_next = node;
1941 else
1942 head->rq_prev->rq_next = node;
1943 head->rq_prev = node;
1944 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001945
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001946 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001947 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001948 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001949 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001950 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001951 fprintf(log_fd, "'\n");
1952 }
1953 return OK;
1954}
1955
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001956/*
1957 * Try to fill the buffer of "reader".
1958 * Returns FALSE when nothing was added.
1959 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001960 static int
1961channel_fill(js_read_T *reader)
1962{
1963 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001964 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001965 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001966 int keeplen;
1967 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001968 char_u *p;
1969
1970 if (next == NULL)
1971 return FALSE;
1972
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001973 keeplen = reader->js_end - reader->js_buf;
1974 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001975 {
1976 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001977 addlen = (int)STRLEN(next);
1978 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001979 if (p == NULL)
1980 {
1981 vim_free(next);
1982 return FALSE;
1983 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001984 mch_memmove(p, reader->js_buf, keeplen);
1985 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001986 vim_free(next);
1987 next = p;
1988 }
1989
1990 vim_free(reader->js_buf);
1991 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001992 return TRUE;
1993}
1994
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001995/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001996 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001997 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001998 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001999 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002000 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002001channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002002{
2003 js_read_T reader;
2004 typval_T listtv;
2005 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002006 chanpart_T *chanpart = &channel->ch_part[part];
2007 jsonq_T *head = &chanpart->ch_json_head;
2008 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002009 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002010
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002011 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002012 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002013
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002014 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002015 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002016 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01002017 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002018 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002019
2020 /* When a message is incomplete we wait for a short while for more to
2021 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002022 * or list will make us hang.
2023 * Do not generate error messages, they will be written in a channel log. */
2024 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002025 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002026 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002027 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002028 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002029 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002030 /* Only accept the response when it is a list with at least two
2031 * items. */
2032 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002033 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002034 if (listtv.v_type != VAR_LIST)
2035 ch_error(channel, "Did not receive a list, discarding");
2036 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002037 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002038 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002039 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002040 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041 else
2042 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002043 item = ALLOC_ONE(jsonq_T);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002044 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002045 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002046 else
2047 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002048 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002049 item->jq_value = alloc_tv();
2050 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002051 {
2052 vim_free(item);
2053 clear_tv(&listtv);
2054 }
2055 else
2056 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002057 *item->jq_value = listtv;
2058 item->jq_prev = head->jq_prev;
2059 head->jq_prev = item;
2060 item->jq_next = NULL;
2061 if (item->jq_prev == NULL)
2062 head->jq_next = item;
2063 else
2064 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002065 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066 }
2067 }
2068 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002069
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002070 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002071 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002072 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002073 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002074 size_t buflen = STRLEN(reader.js_buf);
2075
2076 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002077 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002078 /* First time encountering incomplete message or after receiving
2079 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002080 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002081 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002082 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002083 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002084 chanpart->ch_wait_len = buflen;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002085#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002086 chanpart->ch_deadline = GetTickCount() + 100L;
2087#else
2088 gettimeofday(&chanpart->ch_deadline, NULL);
2089 chanpart->ch_deadline.tv_usec += 100 * 1000;
2090 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2091 {
2092 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2093 ++chanpart->ch_deadline.tv_sec;
2094 }
2095#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002096 }
2097 else
2098 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002099 int timeout;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002100#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002101 timeout = GetTickCount() > chanpart->ch_deadline;
2102#else
2103 {
2104 struct timeval now_tv;
2105
2106 gettimeofday(&now_tv, NULL);
2107 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2108 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2109 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2110 }
2111#endif
2112 if (timeout)
2113 {
2114 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002115 chanpart->ch_wait_len = 0;
2116 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002117 }
2118 else
2119 {
2120 reader.js_used = 0;
2121 ch_log(channel, "still waiting on incomplete message");
2122 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002123 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002124 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002125
2126 if (status == FAIL)
2127 {
2128 ch_error(channel, "Decoding failed - discarding input");
2129 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002130 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002131 }
2132 else if (reader.js_buf[reader.js_used] != NUL)
2133 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002134 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002135 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002136 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2137 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002138 ret = status == MAYBE ? FALSE: TRUE;
2139 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002140 else
2141 ret = FALSE;
2142
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002143 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002144 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002145}
2146
2147/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002148 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002149 */
2150 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002151remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002152{
Bram Moolenaar77073442016-02-13 23:23:53 +01002153 if (node->cq_prev == NULL)
2154 head->cq_next = node->cq_next;
2155 else
2156 node->cq_prev->cq_next = node->cq_next;
2157 if (node->cq_next == NULL)
2158 head->cq_prev = node->cq_prev;
2159 else
2160 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002161}
2162
2163/*
2164 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002165 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002166 */
2167 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002168remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002169{
Bram Moolenaar77073442016-02-13 23:23:53 +01002170 if (node->jq_prev == NULL)
2171 head->jq_next = node->jq_next;
2172 else
2173 node->jq_prev->jq_next = node->jq_next;
2174 if (node->jq_next == NULL)
2175 head->jq_prev = node->jq_prev;
2176 else
2177 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002178 vim_free(node);
2179}
2180
2181/*
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002182 * Add "id" to the list of JSON message IDs we are waiting on.
2183 */
2184 static void
2185channel_add_block_id(chanpart_T *chanpart, int id)
2186{
2187 garray_T *gap = &chanpart->ch_block_ids;
2188
2189 if (gap->ga_growsize == 0)
2190 ga_init2(gap, (int)sizeof(int), 10);
2191 if (ga_grow(gap, 1) == OK)
2192 {
2193 ((int *)gap->ga_data)[gap->ga_len] = id;
2194 ++gap->ga_len;
2195 }
2196}
2197
2198/*
2199 * Remove "id" from the list of JSON message IDs we are waiting on.
2200 */
2201 static void
2202channel_remove_block_id(chanpart_T *chanpart, int id)
2203{
2204 garray_T *gap = &chanpart->ch_block_ids;
2205 int i;
2206
2207 for (i = 0; i < gap->ga_len; ++i)
2208 if (((int *)gap->ga_data)[i] == id)
2209 {
2210 --gap->ga_len;
2211 if (i < gap->ga_len)
2212 {
2213 int *p = ((int *)gap->ga_data) + i;
2214
2215 mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int));
2216 }
2217 return;
2218 }
2219 siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id);
2220}
2221
2222/*
2223 * Return TRUE if "id" is in the list of JSON message IDs we are waiting on.
2224 */
2225 static int
2226channel_has_block_id(chanpart_T *chanpart, int id)
2227{
2228 garray_T *gap = &chanpart->ch_block_ids;
2229 int i;
2230
2231 for (i = 0; i < gap->ga_len; ++i)
2232 if (((int *)gap->ga_data)[i] == id)
2233 return TRUE;
2234 return FALSE;
2235}
2236
2237/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002238 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002239 * When "id" is positive it must match the first number in the list.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002240 * When "id" is zero or negative jut get the first message. But not one
2241 * in the ch_block_ids list.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002242 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002243 * Return OK when found and return the value in "rettv".
2244 * Return FAIL otherwise.
2245 */
2246 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002247channel_get_json(
2248 channel_T *channel,
2249 ch_part_T part,
2250 int id,
2251 int without_callback,
2252 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002253{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002254 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002255 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002256
Bram Moolenaar77073442016-02-13 23:23:53 +01002257 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002258 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002259 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002260 typval_T *tv = &l->lv_first->li_tv;
2261
Bram Moolenaar958dc692016-12-01 15:34:12 +01002262 if ((without_callback || !item->jq_no_callback)
2263 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002264 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002265 || tv->vval.v_number == 0
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002266 || !channel_has_block_id(
2267 &channel->ch_part[part], tv->vval.v_number)))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002268 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002269 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002270 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002271 ch_log(channel, "Getting JSON message %ld",
2272 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002273 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002274 return OK;
2275 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002276 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002277 }
2278 return FAIL;
2279}
2280
Bram Moolenaar958dc692016-12-01 15:34:12 +01002281/*
2282 * Put back "rettv" into the JSON queue, there was no callback for it.
2283 * Takes over the values in "rettv".
2284 */
2285 static void
2286channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2287{
2288 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2289 jsonq_T *item = head->jq_next;
2290 jsonq_T *newitem;
2291
2292 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2293 /* last item was pushed back, append to the end */
2294 item = NULL;
2295 else while (item != NULL && item->jq_no_callback)
2296 /* append after the last item that was pushed back */
2297 item = item->jq_next;
2298
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002299 newitem = ALLOC_ONE(jsonq_T);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002300 if (newitem == NULL)
2301 clear_tv(rettv);
2302 else
2303 {
2304 newitem->jq_value = alloc_tv();
2305 if (newitem->jq_value == NULL)
2306 {
2307 vim_free(newitem);
2308 clear_tv(rettv);
2309 }
2310 else
2311 {
2312 newitem->jq_no_callback = FALSE;
2313 *newitem->jq_value = *rettv;
2314 if (item == NULL)
2315 {
2316 /* append to the end */
2317 newitem->jq_prev = head->jq_prev;
2318 head->jq_prev = newitem;
2319 newitem->jq_next = NULL;
2320 if (newitem->jq_prev == NULL)
2321 head->jq_next = newitem;
2322 else
2323 newitem->jq_prev->jq_next = newitem;
2324 }
2325 else
2326 {
2327 /* append after "item" */
2328 newitem->jq_prev = item;
2329 newitem->jq_next = item->jq_next;
2330 item->jq_next = newitem;
2331 if (newitem->jq_next == NULL)
2332 head->jq_prev = newitem;
2333 else
2334 newitem->jq_next->jq_prev = newitem;
2335 }
2336 }
2337 }
2338}
2339
Bram Moolenaarece61b02016-02-20 21:39:05 +01002340#define CH_JSON_MAX_ARGS 4
2341
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002342/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002343 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002344 * "argv[0]" is the command string.
2345 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002346 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002347 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002348channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002349{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002350 char_u *cmd = argv[0].vval.v_string;
2351 char_u *arg;
2352 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002353
Bram Moolenaarece61b02016-02-20 21:39:05 +01002354 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002355 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002356 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002357 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002358 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002359 return;
2360 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002361 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002362 if (arg == NULL)
2363 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002364
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002365 if (STRCMP(cmd, "ex") == 0)
2366 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002367 int save_called_emsg = called_emsg;
2368
2369 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002370 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002371 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002372 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002373 --emsg_silent;
2374 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002375 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002376 (char *)get_vim_var_str(VV_ERRMSG));
2377 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002378 }
2379 else if (STRCMP(cmd, "normal") == 0)
2380 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002381 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002382
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002383 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002384 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002385 ea.arg = arg;
2386 ea.addr_count = 0;
2387 ea.forceit = TRUE; /* no mapping */
2388 ex_normal(&ea);
2389 }
2390 else if (STRCMP(cmd, "redraw") == 0)
2391 {
2392 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002393
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002394 ch_log(channel, "redraw");
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002395 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002396 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002397 ex_redraw(&ea);
2398 showruler(FALSE);
2399 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002400 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002401 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002402 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002403 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 int is_call = cmd[0] == 'c';
2405 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002406
Bram Moolenaarece61b02016-02-20 21:39:05 +01002407 if (argv[id_idx].v_type != VAR_UNKNOWN
2408 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002409 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002410 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002411 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002412 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002413 }
2414 else if (is_call && argv[2].v_type != VAR_LIST)
2415 {
2416 ch_error(channel, "third argument for call must be a list");
2417 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002418 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002419 }
2420 else
2421 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002422 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002423 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002424 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002425 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002426
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002427 /* Don't pollute the display with errors. */
2428 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002429 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002430 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002431 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002432 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002433 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002434 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002435 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002436 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002437 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2438 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002439 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002440
2441 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002442 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002443 int id = argv[id_idx].vval.v_number;
2444
Bram Moolenaar55fab432016-02-07 16:53:13 +01002445 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002446 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002447 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002448 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002449 /* If evaluation failed or the result can't be encoded
2450 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002451 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002452 err_tv.v_type = VAR_STRING;
2453 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002454 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002455 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002456 if (json != NULL)
2457 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002458 channel_send(channel,
2459 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002460 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002461 vim_free(json);
2462 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002463 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002464 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002465 if (tv == &res_tv)
2466 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002467 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002468 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002469 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002470 }
2471 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002472 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002473 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002474 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002475 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002476}
2477
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002478/*
2479 * Invoke the callback at "cbhead".
2480 * Does not redraw but sets channel_need_redraw.
2481 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002482 static void
2483invoke_one_time_callback(
2484 channel_T *channel,
2485 cbq_T *cbhead,
2486 cbq_T *item,
2487 typval_T *argv)
2488{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002489 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002490 (char *)item->cq_callback.cb_name);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002491 /* Remove the item from the list first, if the callback
2492 * invokes ch_close() the list will be cleared. */
2493 remove_cb_node(cbhead, item);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002494 invoke_callback(channel, &item->cq_callback, argv);
2495 free_callback(&item->cq_callback);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002496 vim_free(item);
2497}
2498
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002499 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002500append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002501{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002502 bufref_T save_curbuf = {NULL, 0, 0};
2503 win_T *save_curwin = NULL;
2504 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002505 linenr_T lnum = buffer->b_ml.ml_line_count;
2506 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002507 chanpart_T *ch_part = &channel->ch_part[part];
2508 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002509 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002510
2511 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2512 {
2513 if (!ch_part->ch_nomod_error)
2514 {
2515 ch_error(channel, "Buffer is not modifiable, cannot append");
2516 ch_part->ch_nomod_error = TRUE;
2517 }
2518 return;
2519 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002520
2521 /* If the buffer is also used as input insert above the last
2522 * line. Don't write these lines. */
2523 if (save_write_to)
2524 {
2525 --lnum;
2526 buffer->b_write_to_channel = FALSE;
2527 }
2528
2529 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002530 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002531
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002532 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002533
2534 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2535 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2536
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002537 u_sync(TRUE);
2538 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002539 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002540
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002541 if (empty)
2542 {
2543 /* The buffer is empty, replace the first (dummy) line. */
2544 ml_replace(lnum, msg, TRUE);
2545 lnum = 0;
2546 }
2547 else
2548 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002549 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002550
2551 /* Restore curbuf/curwin/curtab */
2552 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2553
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002554 if (ch_part->ch_nomodifiable)
2555 buffer->b_p_ma = FALSE;
2556 else
2557 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002558
2559 if (buffer->b_nwindows > 0)
2560 {
2561 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002562
2563 FOR_ALL_WINDOWS(wp)
2564 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002565 if (wp->w_buffer == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002566 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002567 int move_cursor = save_write_to
2568 ? wp->w_cursor.lnum == lnum + 1
2569 : (wp->w_cursor.lnum == lnum
2570 && wp->w_cursor.col == 0);
2571
2572 // If the cursor is at or above the new line, move it one line
2573 // down. If the topline is outdated update it now.
2574 if (move_cursor || wp->w_topline > buffer->b_ml.ml_line_count)
2575 {
2576 if (move_cursor)
2577 ++wp->w_cursor.lnum;
2578 save_curwin = curwin;
2579 curwin = wp;
2580 curbuf = curwin->w_buffer;
2581 scroll_cursor_bot(0, FALSE);
2582 curwin = save_curwin;
2583 curbuf = curwin->w_buffer;
2584 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002585 }
2586 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002587 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002588 channel_need_redraw = TRUE;
2589 }
2590
2591 if (save_write_to)
2592 {
2593 channel_T *ch;
2594
2595 /* Find channels reading from this buffer and adjust their
2596 * next-to-read line number. */
2597 buffer->b_write_to_channel = TRUE;
2598 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2599 {
2600 chanpart_T *in_part = &ch->ch_part[PART_IN];
2601
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002602 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002603 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2604 }
2605 }
2606}
2607
Bram Moolenaar437905c2016-04-26 19:01:05 +02002608 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002609drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002610{
2611 char_u *msg;
2612
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002613 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002614 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002615 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002616 vim_free(msg);
2617 }
2618}
2619
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002620/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002621 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002622 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002623 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002624 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002625 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002626may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002627{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002628 char_u *msg = NULL;
2629 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002630 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002631 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002632 chanpart_T *ch_part = &channel->ch_part[part];
2633 ch_mode_T ch_mode = ch_part->ch_mode;
2634 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002635 cbq_T *cbitem;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002636 callback_T *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002637 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002638 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002639
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002640 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002641 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002642 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002643
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002644 /* Use a message-specific callback, part callback or channel callback */
2645 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2646 if (cbitem->cq_seq_nr == 0)
2647 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002648 if (cbitem != NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002649 callback = &cbitem->cq_callback;
2650 else if (ch_part->ch_callback.cb_name != NULL)
2651 callback = &ch_part->ch_callback;
2652 else if (channel->ch_callback.cb_name != NULL)
2653 callback = &channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002654
Bram Moolenaarec68a992016-10-03 21:37:41 +02002655 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002656 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2657 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002658 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002659 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002660 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002661 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002662 buffer = NULL;
2663 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002664
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002665 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002666 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002667 listitem_T *item;
2668 int argc = 0;
2669
Bram Moolenaard7ece102016-02-02 23:23:02 +01002670 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002671 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002672 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002673 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002675 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002676 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002677 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002678
Bram Moolenaarece61b02016-02-20 21:39:05 +01002679 for (item = listtv->vval.v_list->lv_first;
2680 item != NULL && argc < CH_JSON_MAX_ARGS;
2681 item = item->li_next)
2682 argv[argc++] = item->li_tv;
2683 while (argc < CH_JSON_MAX_ARGS)
2684 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002685
Bram Moolenaarece61b02016-02-20 21:39:05 +01002686 if (argv[0].v_type == VAR_STRING)
2687 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002688 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002689 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002690 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002691 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002692 }
2693
Bram Moolenaarece61b02016-02-20 21:39:05 +01002694 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002695 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002696 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002697 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002698 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002699 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002700 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002701 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002702 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002703 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002704 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002705 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002706 return FALSE;
2707 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002708 else
2709 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002710 /* If there is no callback or buffer drop the message. */
2711 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002712 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002713 /* If there is a close callback it may use ch_read() to get the
2714 * messages. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002715 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002716 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002717 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002718 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002719
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002720 if (ch_mode == MODE_NL)
2721 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002722 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002723 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002724 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002725
2726 /* See if we have a message ending in NL in the first buffer. If
2727 * not try to concatenate the first and the second buffer. */
2728 while (TRUE)
2729 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002730 node = channel_peek(channel, part);
2731 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002732 if (nl != NULL)
2733 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002734 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002735 {
2736 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2737 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002738 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002739 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002740 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002741 buf = node->rq_buffer;
2742
Bram Moolenaar772153f2019-03-04 12:09:49 +01002743 // Convert NUL to NL, the internal representation.
2744 for (p = buf; (nl == NULL || p < nl)
2745 && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002746 if (*p == NUL)
2747 *p = NL;
2748
Bram Moolenaar772153f2019-03-04 12:09:49 +01002749 if (nl == NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002750 {
Bram Moolenaar772153f2019-03-04 12:09:49 +01002751 // get the whole buffer, drop the NL
2752 msg = channel_get(channel, part, NULL);
2753 }
2754 else if (nl + 1 == buf + node->rq_buflen)
2755 {
2756 // get the whole buffer
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002757 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002758 *nl = NUL;
2759 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002760 else
2761 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002762 /* Copy the message into allocated memory (excluding the NL)
2763 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002764 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002765 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002766 }
2767 }
2768 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002769 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002770 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002771 * get everything we have.
2772 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002773 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002774 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002775
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002776 if (msg == NULL)
2777 return FALSE; /* out of memory (and avoids Coverity warning) */
2778
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002779 argv[1].v_type = VAR_STRING;
2780 argv[1].vval.v_string = msg;
2781 }
2782
Bram Moolenaara07fec92016-02-05 21:04:08 +01002783 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002784 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002785 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002786
Bram Moolenaar958dc692016-12-01 15:34:12 +01002787 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002788 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002789 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002790 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002791 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002792 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002793 break;
2794 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002795 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002796 {
2797 if (channel->ch_drop_never)
2798 {
2799 /* message must be read with ch_read() */
2800 channel_push_json(channel, part, listtv);
2801 listtv = NULL;
2802 }
2803 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002804 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002805 seq_nr);
2806 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002807 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002808 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002809 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002810 if (buffer != NULL)
2811 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002812 if (msg == NULL)
2813 /* JSON or JS mode: re-encode the message. */
2814 msg = json_encode(listtv, ch_mode);
2815 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002816 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002817#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002818 if (buffer->b_term != NULL)
2819 write_to_term(buffer, msg, channel);
2820 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002821#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002822 append_to_buffer(buffer, msg, channel, part);
2823 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002824 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002825
Bram Moolenaar187db502016-02-27 14:44:26 +01002826 if (callback != NULL)
2827 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002828 if (cbitem != NULL)
2829 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2830 else
2831 {
2832 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002833 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002834 (char *)callback->cb_name);
2835 invoke_callback(channel, callback, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002836 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002837 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002838 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002839 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002840 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002841
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002842 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002843 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002844 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002845
2846 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002847}
2848
Bram Moolenaar113e1072019-01-20 15:30:40 +01002849#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002850/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002851 * Return TRUE when channel "channel" is open for writing to.
2852 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002853 */
2854 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002855channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002856{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002857 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002858 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002859}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002860#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002861
2862/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002863 * Return TRUE when channel "channel" is open for reading or writing.
2864 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002865 */
2866 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002867channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002868{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002869 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002870 || channel->CH_IN_FD != INVALID_FD
2871 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002872 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002873}
2874
2875/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002876 * Return TRUE if "channel" has JSON or other typeahead.
2877 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002878 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002879channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002880{
2881 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2882
2883 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2884 {
2885 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002886
Bram Moolenaar4340fc92019-06-28 22:06:49 +02002887 if (head->jq_next == NULL)
2888 // Parse json from readahead, there might be a complete message to
2889 // process.
2890 channel_parse_json(channel, part);
2891
2892 return head->jq_next != NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002893 }
2894 return channel_peek(channel, part) != NULL;
2895}
2896
2897/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002898 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002899 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002900 */
2901 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002902channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002903{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002904 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002905 int has_readahead = FALSE;
2906
Bram Moolenaar77073442016-02-13 23:23:53 +01002907 if (channel == NULL)
2908 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002909 if (req_part == PART_OUT)
2910 {
2911 if (channel->CH_OUT_FD != INVALID_FD)
2912 return "open";
2913 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002914 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002915 }
2916 else if (req_part == PART_ERR)
2917 {
2918 if (channel->CH_ERR_FD != INVALID_FD)
2919 return "open";
2920 if (channel_has_readahead(channel, PART_ERR))
2921 has_readahead = TRUE;
2922 }
2923 else
2924 {
2925 if (channel_is_open(channel))
2926 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002927 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002928 if (channel_has_readahead(channel, part))
2929 {
2930 has_readahead = TRUE;
2931 break;
2932 }
2933 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002934
2935 if (has_readahead)
2936 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002937 return "closed";
2938}
2939
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002940 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002941channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002942{
2943 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002944 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002945 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002946 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002947 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002948
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002949 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002950 STRCAT(namebuf, "_");
2951 tail = STRLEN(namebuf);
2952
2953 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002954 if (chanpart->ch_fd != INVALID_FD)
2955 status = "open";
2956 else if (channel_has_readahead(channel, part))
2957 status = "buffered";
2958 else
2959 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002960 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002961
2962 STRCPY(namebuf + tail, "mode");
2963 switch (chanpart->ch_mode)
2964 {
2965 case MODE_NL: s = "NL"; break;
2966 case MODE_RAW: s = "RAW"; break;
2967 case MODE_JSON: s = "JSON"; break;
2968 case MODE_JS: s = "JS"; break;
2969 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002970 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002971
2972 STRCPY(namebuf + tail, "io");
2973 if (part == PART_SOCK)
2974 s = "socket";
2975 else switch (chanpart->ch_io)
2976 {
2977 case JIO_NULL: s = "null"; break;
2978 case JIO_PIPE: s = "pipe"; break;
2979 case JIO_FILE: s = "file"; break;
2980 case JIO_BUFFER: s = "buffer"; break;
2981 case JIO_OUT: s = "out"; break;
2982 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002983 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002984
2985 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002986 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002987}
2988
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002989 static void
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002990channel_info(channel_T *channel, dict_T *dict)
2991{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002992 dict_add_number(dict, "id", channel->ch_id);
2993 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002994
2995 if (channel->ch_hostname != NULL)
2996 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002997 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2998 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002999 channel_part_info(channel, dict, "sock", PART_SOCK);
3000 }
3001 else
3002 {
3003 channel_part_info(channel, dict, "out", PART_OUT);
3004 channel_part_info(channel, dict, "err", PART_ERR);
3005 channel_part_info(channel, dict, "in", PART_IN);
3006 }
3007}
3008
Bram Moolenaar77073442016-02-13 23:23:53 +01003009/*
3010 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003011 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01003012 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003013 */
3014 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01003015channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003016{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003017 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003018
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003019#ifdef FEAT_GUI
3020 channel_gui_unregister(channel);
3021#endif
3022
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003023 ch_close_part(channel, PART_SOCK);
3024 ch_close_part(channel, PART_IN);
3025 ch_close_part(channel, PART_OUT);
3026 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003027
Bram Moolenaara9f02812017-08-05 17:40:38 +02003028 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003029 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02003030 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003031
Bram Moolenaara9f02812017-08-05 17:40:38 +02003032 /* Invoke callbacks and flush buffers before the close callback. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003033 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003034 ch_log(channel,
3035 "Invoking callbacks and flushing buffers before closing");
3036 for (part = PART_SOCK; part < PART_IN; ++part)
3037 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003038 if (channel->ch_close_cb.cb_name != NULL
Bram Moolenaara9f02812017-08-05 17:40:38 +02003039 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3040 {
3041 /* Increment the refcount to avoid the channel being freed
3042 * halfway. */
3043 ++channel->ch_refcount;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003044 if (channel->ch_close_cb.cb_name == NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003045 ch_log(channel, "flushing %s buffers before closing",
3046 part_names[part]);
3047 while (may_invoke_callback(channel, part))
3048 ;
3049 --channel->ch_refcount;
3050 }
3051 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003052
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003053 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003054 {
3055 typval_T argv[1];
3056 typval_T rettv;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003057
3058 /* Increment the refcount to avoid the channel being freed
3059 * halfway. */
3060 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003061 ch_log(channel, "Invoking close callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003062 (char *)channel->ch_close_cb.cb_name);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003063 argv[0].v_type = VAR_CHANNEL;
3064 argv[0].vval.v_channel = channel;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003065 call_callback(&channel->ch_close_cb, -1, &rettv, 1, argv);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003066 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003067 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003068
Bram Moolenaara9f02812017-08-05 17:40:38 +02003069 /* the callback is only called once */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003070 free_callback(&channel->ch_close_cb);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003071
Bram Moolenaara9f02812017-08-05 17:40:38 +02003072 if (channel_need_redraw)
3073 {
3074 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003075 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003076 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003077
Bram Moolenaara9f02812017-08-05 17:40:38 +02003078 if (!channel->ch_drop_never)
3079 /* any remaining messages are useless now */
3080 for (part = PART_SOCK; part < PART_IN; ++part)
3081 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003082
3083 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003084 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003085 }
3086
3087 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003088
3089#ifdef FEAT_TERMINAL
3090 term_channel_closed(channel);
3091#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003092}
3093
Bram Moolenaard04a0202016-01-26 23:30:18 +01003094/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003095 * Close the "in" part channel "channel".
3096 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003097 static void
Bram Moolenaar0874a832016-09-01 15:11:51 +02003098channel_close_in(channel_T *channel)
3099{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003100 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003101}
3102
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003103 static void
3104remove_from_writeque(writeq_T *wq, writeq_T *entry)
3105{
3106 ga_clear(&entry->wq_ga);
3107 wq->wq_next = entry->wq_next;
3108 if (wq->wq_next == NULL)
3109 wq->wq_prev = NULL;
3110 else
3111 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003112 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003113}
3114
Bram Moolenaar0874a832016-09-01 15:11:51 +02003115/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003116 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003117 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003118 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003119channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003120{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003121 chanpart_T *ch_part = &channel->ch_part[part];
3122 jsonq_T *json_head = &ch_part->ch_json_head;
3123 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003124
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003125 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003126 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003127
3128 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003129 {
3130 cbq_T *node = cb_head->cq_next;
3131
3132 remove_cb_node(cb_head, node);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003133 free_callback(&node->cq_callback);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003134 vim_free(node);
3135 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003136
3137 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003138 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003139 free_tv(json_head->jq_next->jq_value);
3140 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003141 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003142
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003143 free_callback(&ch_part->ch_callback);
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003144 ga_clear(&ch_part->ch_block_ids);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003145
3146 while (ch_part->ch_writeque.wq_next != NULL)
3147 remove_from_writeque(&ch_part->ch_writeque,
3148 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003149}
3150
3151/*
3152 * Clear all the read buffers on "channel".
3153 */
3154 void
3155channel_clear(channel_T *channel)
3156{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003157 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003158 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003159 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003160 channel_clear_one(channel, PART_OUT);
3161 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003162 channel_clear_one(channel, PART_IN);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003163 free_callback(&channel->ch_callback);
3164 free_callback(&channel->ch_close_cb);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003165}
3166
Bram Moolenaar77073442016-02-13 23:23:53 +01003167#if defined(EXITFREE) || defined(PROTO)
3168 void
3169channel_free_all(void)
3170{
3171 channel_T *channel;
3172
Bram Moolenaard6051b52016-02-28 15:49:03 +01003173 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003174 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3175 channel_clear(channel);
3176}
3177#endif
3178
3179
Bram Moolenaar715d2852016-04-30 17:06:31 +02003180/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003181#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003182
3183/* Buffer size for reading incoming messages. */
3184#define MAXMSGSIZE 4096
3185
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003186#if defined(HAVE_SELECT)
3187/*
3188 * Add write fds where we are waiting for writing to be possible.
3189 */
3190 static int
3191channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3192{
3193 int maxfd = maxfd_arg;
3194 channel_T *ch;
3195
3196 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3197 {
3198 chanpart_T *in_part = &ch->ch_part[PART_IN];
3199
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003200 if (in_part->ch_fd != INVALID_FD
3201 && (in_part->ch_bufref.br_buf != NULL
3202 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003203 {
3204 FD_SET((int)in_part->ch_fd, wfds);
3205 if ((int)in_part->ch_fd >= maxfd)
3206 maxfd = (int)in_part->ch_fd + 1;
3207 }
3208 }
3209 return maxfd;
3210}
3211#else
3212/*
3213 * Add write fds where we are waiting for writing to be possible.
3214 */
3215 static int
3216channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3217{
3218 int nfd = nfd_in;
3219 channel_T *ch;
3220
3221 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3222 {
3223 chanpart_T *in_part = &ch->ch_part[PART_IN];
3224
Bram Moolenaar683b7962017-08-19 15:51:59 +02003225 if (in_part->ch_fd != INVALID_FD
3226 && (in_part->ch_bufref.br_buf != NULL
3227 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003228 {
3229 in_part->ch_poll_idx = nfd;
3230 fds[nfd].fd = in_part->ch_fd;
3231 fds[nfd].events = POLLOUT;
3232 ++nfd;
3233 }
3234 else
3235 in_part->ch_poll_idx = -1;
3236 }
3237 return nfd;
3238}
3239#endif
3240
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003241typedef enum {
3242 CW_READY,
3243 CW_NOT_READY,
3244 CW_ERROR
3245} channel_wait_result;
3246
Bram Moolenaard04a0202016-01-26 23:30:18 +01003247/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003248 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003249 * Return CW_READY when there is something to read.
3250 * Return CW_NOT_READY when there is nothing to read.
3251 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003252 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003253 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003254channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003255{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003256 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003257 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003258
Bram Moolenaar4f974752019-02-17 17:44:42 +01003259# ifdef MSWIN
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003260 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003261 {
3262 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003263 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003264 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003265 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003266
3267 /* reading from a pipe, not a socket */
3268 while (TRUE)
3269 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003270 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3271
3272 if (r && nread > 0)
3273 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003274
3275 if (channel->ch_named_pipe)
3276 {
3277 DisconnectNamedPipe((HANDLE)fd);
3278 ConnectNamedPipe((HANDLE)fd, NULL);
3279 }
3280 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003281 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003282
3283 /* perhaps write some buffer lines */
3284 channel_write_any_lines();
3285
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003286 sleep_time = deadline - GetTickCount();
3287 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003288 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003289 /* Wait for a little while. Very short at first, up to 10 msec
3290 * after looping a few times. */
3291 if (sleep_time > delay)
3292 sleep_time = delay;
3293 Sleep(sleep_time);
3294 delay = delay * 2;
3295 if (delay > 10)
3296 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003297 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003298 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003299 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003300#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003301 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003302#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003303 struct timeval tval;
3304 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003305 fd_set wfds;
3306 int ret;
3307 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003308
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003309 tval.tv_sec = timeout / 1000;
3310 tval.tv_usec = (timeout % 1000) * 1000;
3311 for (;;)
3312 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003313 FD_ZERO(&rfds);
3314 FD_SET((int)fd, &rfds);
3315
3316 /* Write lines to a pipe when a pipe can be written to. Need to
3317 * set this every time, some buffers may be done. */
3318 maxfd = (int)fd + 1;
3319 FD_ZERO(&wfds);
3320 maxfd = channel_fill_wfds(maxfd, &wfds);
3321
3322 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003323# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003324 SOCK_ERRNO;
3325 if (ret == -1 && errno == EINTR)
3326 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003327# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003328 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003329 {
3330 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003331 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003332 channel_write_any_lines();
3333 continue;
3334 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003335 break;
3336 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003337#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003338 for (;;)
3339 {
3340 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3341 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003342
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003343 fds[0].fd = fd;
3344 fds[0].events = POLLIN;
3345 nfd = channel_fill_poll_write(nfd, fds);
3346 if (poll(fds, nfd, timeout) > 0)
3347 {
3348 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003349 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003350 channel_write_any_lines();
3351 continue;
3352 }
3353 break;
3354 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003355#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003356 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003357 return CW_NOT_READY;
3358}
3359
3360 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003361ch_close_part_on_error(
3362 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003363{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003364 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003365
3366 if (is_err)
3367 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003368 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003369 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003370 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003371
3372 /* Queue a "DETACH" netbeans message in the command queue in order to
3373 * terminate the netbeans session later. Do not end the session here
3374 * directly as we may be running in the context of a call to
3375 * netbeans_parse_messages():
3376 * netbeans_parse_messages
3377 * -> autocmd triggered while processing the netbeans cmd
3378 * -> ui_breakcheck
3379 * -> gui event loop or select loop
3380 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003381 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003382 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003383 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003384 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003385 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3386
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003387 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003388 * close the channel yet, there may be something to read on another part.
3389 * When stdout and stderr use the same FD we get the error only on one of
3390 * them, also close the other. */
3391 if (part == PART_OUT || part == PART_ERR)
3392 {
3393 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3394
3395 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3396 ch_close_part(channel, other);
3397 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003398 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003399
3400#ifdef FEAT_GUI
3401 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003402 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003403#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003404}
3405
3406 static void
3407channel_close_now(channel_T *channel)
3408{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003409 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003410 if (channel->ch_nb_close_cb != NULL)
3411 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003412 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003413}
3414
3415/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003416 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003417 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003418 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003419 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003420 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003421channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003422{
3423 static char_u *buf = NULL;
3424 int len = 0;
3425 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003426 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003427 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003428
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003429 fd = channel->ch_part[part].ch_fd;
3430 if (fd == INVALID_FD)
3431 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003432 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003433 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003434 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003435 }
3436 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003437
3438 /* Allocate a buffer to read into. */
3439 if (buf == NULL)
3440 {
3441 buf = alloc(MAXMSGSIZE);
3442 if (buf == NULL)
3443 return; /* out of memory! */
3444 }
3445
3446 /* Keep on reading for as long as there is something to read.
3447 * Use select() or poll() to avoid blocking on a message that is exactly
3448 * MAXMSGSIZE long. */
3449 for (;;)
3450 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003451 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003452 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003453 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003454 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003455 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003456 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003457 if (len <= 0)
3458 break; /* error or nothing more to read */
3459
3460 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003461 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003462 readlen += len;
3463 if (len < MAXMSGSIZE)
3464 break; /* did read everything that's available */
3465 }
3466
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003467 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003468 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003469 {
3470 if (!channel->ch_keep_open)
3471 ch_close_part_on_error(channel, part, (len < 0), func);
3472 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003473#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003474 else if (CH_HAS_GUI && gtk_main_level() > 0)
3475 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003476 gtk_main_quit();
3477#endif
3478}
3479
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003480/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003481 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003482 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003483 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003484 * Does not trigger timers or handle messages.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003485 * Returns what was read in allocated memory.
3486 * Returns NULL in case of error or timeout.
3487 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003488 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003489channel_read_block(
3490 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003491{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003492 char_u *buf;
3493 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003494 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003495 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003496 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003497 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003498
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003499 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003500 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003501
3502 while (TRUE)
3503 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003504 node = channel_peek(channel, part);
3505 if (node != NULL)
3506 {
3507 if (mode == MODE_RAW || (mode == MODE_NL
3508 && channel_first_nl(node) != NULL))
3509 /* got a complete message */
3510 break;
3511 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3512 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003513 /* If not blocking or nothing more is coming then return what we
3514 * have. */
3515 if (raw || fd == INVALID_FD)
3516 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003517 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003518
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003519 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003520 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003521 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003522 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003523 {
3524 ch_log(channel, "Timed out");
3525 return NULL;
3526 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003527 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003528 }
3529
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003530 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003531 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003532 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003533 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003534 }
3535 else
3536 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003537 char_u *p;
3538
3539 buf = node->rq_buffer;
3540 nl = channel_first_nl(node);
3541
3542 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003543 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003544 if (*p == NUL)
3545 *p = NL;
3546
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003547 if (nl == NULL)
3548 {
3549 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003550 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003551 }
3552 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003553 {
3554 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003555 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003556 *nl = NUL;
3557 }
3558 else
3559 {
3560 /* Copy the message into allocated memory and remove it from the
3561 * buffer. */
3562 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003563 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003564 }
3565 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003566 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003567 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003568 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003569}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003570
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003571static int channel_blocking_wait = 0;
3572
3573/*
3574 * Return TRUE if in a blocking wait that might trigger callbacks.
3575 */
3576 int
3577channel_in_blocking_wait(void)
3578{
3579 return channel_blocking_wait > 0;
3580}
3581
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003582/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003583 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003584 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003585 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003586 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003587 * In corner cases this can be called recursively, that is why ch_block_ids is
3588 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003589 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003590 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003591channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003592 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003593 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003594 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003595 int id,
3596 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003597{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003598 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003599 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003600 int timeout;
3601 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003602 int retval = FAIL;
Bram Moolenaard7ece102016-02-02 23:23:02 +01003603
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003604 ch_log(channel, "Blocking read JSON for id %d", id);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003605 ++channel_blocking_wait;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003606
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003607 if (id >= 0)
3608 channel_add_block_id(chanpart, id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003609
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003610 for (;;)
3611 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003612 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003613
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003614 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003615 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003616 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003617 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003618 retval = OK;
3619 break;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003620 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003621
Bram Moolenaard7ece102016-02-02 23:23:02 +01003622 if (!more)
3623 {
3624 /* Handle any other messages in the queue. If done some more
3625 * messages may have arrived. */
3626 if (channel_parse_messages())
3627 continue;
3628
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003629 /* Wait for up to the timeout. If there was an incomplete message
3630 * use the deadline for that. */
3631 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003632 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003633 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003634#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003635 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3636#else
3637 {
3638 struct timeval now_tv;
3639
3640 gettimeofday(&now_tv, NULL);
3641 timeout = (chanpart->ch_deadline.tv_sec
3642 - now_tv.tv_sec) * 1000
3643 + (chanpart->ch_deadline.tv_usec
3644 - now_tv.tv_usec) / 1000
3645 + 1;
3646 }
3647#endif
3648 if (timeout < 0)
3649 {
3650 /* Something went wrong, channel_parse_json() didn't
3651 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003652 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003653 timeout = timeout_arg;
3654 }
3655 else if (timeout > timeout_arg)
3656 timeout = timeout_arg;
3657 }
3658 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003659 if (fd == INVALID_FD
3660 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003661 {
3662 if (timeout == timeout_arg)
3663 {
3664 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003665 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003666 break;
3667 }
3668 }
3669 else
3670 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003671 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003672 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003673 if (id >= 0)
3674 channel_remove_block_id(chanpart, id);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003675 --channel_blocking_wait;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003676
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003677 return retval;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003678}
3679
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003680/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003681 * Get the channel from the argument.
3682 * Returns NULL if the handle is invalid.
3683 * When "check_open" is TRUE check that the channel can be used.
3684 * When "reading" is TRUE "check_open" considers typeahead useful.
3685 * "part" is used to check typeahead, when PART_COUNT use the default part.
3686 */
3687 static channel_T *
3688get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3689{
3690 channel_T *channel = NULL;
3691 int has_readahead = FALSE;
3692
3693 if (tv->v_type == VAR_JOB)
3694 {
3695 if (tv->vval.v_job != NULL)
3696 channel = tv->vval.v_job->jv_channel;
3697 }
3698 else if (tv->v_type == VAR_CHANNEL)
3699 {
3700 channel = tv->vval.v_channel;
3701 }
3702 else
3703 {
3704 semsg(_(e_invarg2), tv_get_string(tv));
3705 return NULL;
3706 }
3707 if (channel != NULL && reading)
3708 has_readahead = channel_has_readahead(channel,
3709 part != PART_COUNT ? part : channel_part_read(channel));
3710
3711 if (check_open && (channel == NULL || (!channel_is_open(channel)
3712 && !(reading && has_readahead))))
3713 {
3714 emsg(_("E906: not an open channel"));
3715 return NULL;
3716 }
3717 return channel;
3718}
3719
3720/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003721 * Common for ch_read() and ch_readraw().
3722 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003723 static void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003724common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003725{
3726 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003727 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003728 jobopt_T opt;
3729 int mode;
3730 int timeout;
3731 int id = -1;
3732 typval_T *listtv = NULL;
3733
3734 /* return an empty string by default */
3735 rettv->v_type = VAR_STRING;
3736 rettv->vval.v_string = NULL;
3737
3738 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003739 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003740 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003741 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003742
Bram Moolenaar437905c2016-04-26 19:01:05 +02003743 if (opt.jo_set & JO_PART)
3744 part = opt.jo_part;
3745 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003746 if (channel != NULL)
3747 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003748 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003749 part = channel_part_read(channel);
3750 mode = channel_get_mode(channel, part);
3751 timeout = channel_get_timeout(channel, part);
3752 if (opt.jo_set & JO_TIMEOUT)
3753 timeout = opt.jo_timeout;
3754
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003755 if (blob)
3756 {
3757 int outlen = 0;
3758 char_u *p = channel_read_block(channel, part,
3759 timeout, TRUE, &outlen);
3760 if (p != NULL)
3761 {
3762 blob_T *b = blob_alloc();
3763
3764 if (b != NULL)
3765 {
3766 b->bv_ga.ga_len = outlen;
3767 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3768 blob_free(b);
3769 else
3770 {
3771 memcpy(b->bv_ga.ga_data, p, outlen);
3772 rettv_blob_set(rettv, b);
3773 }
3774 }
3775 vim_free(p);
3776 }
3777 }
3778 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003779 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003780 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003781 else
3782 {
3783 if (opt.jo_set & JO_ID)
3784 id = opt.jo_id;
3785 channel_read_json_block(channel, part, timeout, id, &listtv);
3786 if (listtv != NULL)
3787 {
3788 *rettv = *listtv;
3789 vim_free(listtv);
3790 }
3791 else
3792 {
3793 rettv->v_type = VAR_SPECIAL;
3794 rettv->vval.v_number = VVAL_NONE;
3795 }
3796 }
3797 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003798
3799theend:
3800 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003801}
3802
Bram Moolenaar4f974752019-02-17 17:44:42 +01003803# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003804/*
3805 * Check the channels for anything that is ready to be read.
3806 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003807 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003808 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003809 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003810channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003811{
3812 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003813 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003814 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003815
3816 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3817 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003818 if (only_keep_open && !channel->ch_keep_open)
3819 continue;
3820
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003821 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003822 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003823 {
3824 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003825 if (fd != INVALID_FD)
3826 {
3827 int r = channel_wait(channel, fd, 0);
3828
3829 if (r == CW_READY)
3830 channel_read(channel, part, "channel_handle_events");
3831 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003832 ch_close_part_on_error(channel, part, TRUE,
3833 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003834 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003835 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003836 }
3837}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003838# endif
3839
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003840# if defined(FEAT_GUI) || defined(PROTO)
3841/*
3842 * Return TRUE when there is any channel with a keep_open flag.
3843 */
3844 int
3845channel_any_keep_open()
3846{
3847 channel_T *channel;
3848
3849 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3850 if (channel->ch_keep_open)
3851 return TRUE;
3852 return FALSE;
3853}
3854# endif
3855
Bram Moolenaard04a0202016-01-26 23:30:18 +01003856/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003857 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003858 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003859 */
3860 void
3861channel_set_nonblock(channel_T *channel, ch_part_T part)
3862{
3863 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003864 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003865
3866 if (fd != INVALID_FD)
3867 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003868#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003869 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003870
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003871 ioctlsocket(fd, FIONBIO, &val);
3872#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003873 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003874#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003875 ch_part->ch_nonblocking = TRUE;
3876 }
3877}
3878
3879/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003880 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003881 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003882 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003883 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003884 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003885channel_send(
3886 channel_T *channel,
3887 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003888 char_u *buf_arg,
3889 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003890 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003891{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003892 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003893 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003894 chanpart_T *ch_part = &channel->ch_part[part];
3895 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003896
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003897 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003898 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003899 {
3900 if (!channel->ch_error && fun != NULL)
3901 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003902 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003903 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003904 }
3905 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003906 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003907 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003908
Bram Moolenaar0b146882018-09-06 16:27:24 +02003909 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3910 channel_set_nonblock(channel, part);
3911
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003912 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003913 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003914 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003915 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003916 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003917 fprintf(log_fd, "'\n");
3918 fflush(log_fd);
Bram Moolenaar101e9922019-09-25 21:43:11 +02003919 did_repeated_msg = 0;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003920 }
3921
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003922 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003923 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003924 writeq_T *wq = &ch_part->ch_writeque;
3925 char_u *buf;
3926 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003927
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003928 if (wq->wq_next != NULL)
3929 {
3930 /* first write what was queued */
3931 buf = wq->wq_next->wq_ga.ga_data;
3932 len = wq->wq_next->wq_ga.ga_len;
3933 did_use_queue = TRUE;
3934 }
3935 else
3936 {
3937 if (len_arg == 0)
3938 /* nothing to write, called from channel_select_check() */
3939 return OK;
3940 buf = buf_arg;
3941 len = len_arg;
3942 }
3943
3944 if (part == PART_SOCK)
3945 res = sock_write(fd, (char *)buf, len);
3946 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003947 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003948 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003949#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003950 if (channel->ch_named_pipe && res < 0)
3951 {
3952 DisconnectNamedPipe((HANDLE)fd);
3953 ConnectNamedPipe((HANDLE)fd, NULL);
3954 }
3955#endif
3956 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003957 if (res < 0 && (errno == EWOULDBLOCK
3958#ifdef EAGAIN
3959 || errno == EAGAIN
3960#endif
3961 ))
3962 res = 0; /* nothing got written */
3963
3964 if (res >= 0 && ch_part->ch_nonblocking)
3965 {
3966 writeq_T *entry = wq->wq_next;
3967
3968 if (did_use_queue)
3969 ch_log(channel, "Sent %d bytes now", res);
3970 if (res == len)
3971 {
3972 /* Wrote all the buf[len] bytes. */
3973 if (entry != NULL)
3974 {
3975 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003976 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003977 continue;
3978 }
3979 if (did_use_queue)
3980 ch_log(channel, "Write queue empty");
3981 }
3982 else
3983 {
3984 /* Wrote only buf[res] bytes, can't write more now. */
3985 if (entry != NULL)
3986 {
3987 if (res > 0)
3988 {
3989 /* Remove the bytes that were written. */
3990 mch_memmove(entry->wq_ga.ga_data,
3991 (char *)entry->wq_ga.ga_data + res,
3992 len - res);
3993 entry->wq_ga.ga_len -= res;
3994 }
3995 buf = buf_arg;
3996 len = len_arg;
3997 }
3998 else
3999 {
4000 buf += res;
4001 len -= res;
4002 }
4003 ch_log(channel, "Adding %d bytes to the write queue", len);
4004
4005 /* Append the not written bytes of the argument to the write
4006 * buffer. Limit entries to 4000 bytes. */
4007 if (wq->wq_prev != NULL
4008 && wq->wq_prev->wq_ga.ga_len + len < 4000)
4009 {
4010 writeq_T *last = wq->wq_prev;
4011
4012 /* append to the last entry */
Bram Moolenaar0d071552019-07-08 22:04:03 +02004013 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004014 {
4015 mch_memmove((char *)last->wq_ga.ga_data
4016 + last->wq_ga.ga_len,
4017 buf, len);
4018 last->wq_ga.ga_len += len;
4019 }
4020 }
4021 else
4022 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004023 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004024
4025 if (last != NULL)
4026 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004027 last->wq_prev = wq->wq_prev;
4028 last->wq_next = NULL;
4029 if (wq->wq_prev == NULL)
4030 wq->wq_next = last;
4031 else
4032 wq->wq_prev->wq_next = last;
4033 wq->wq_prev = last;
4034 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004035 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004036 {
4037 mch_memmove(last->wq_ga.ga_data, buf, len);
4038 last->wq_ga.ga_len = len;
4039 }
4040 }
4041 }
4042 }
4043 }
4044 else if (res != len)
4045 {
4046 if (!channel->ch_error && fun != NULL)
4047 {
4048 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004049 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004050 }
4051 channel->ch_error = TRUE;
4052 return FAIL;
4053 }
4054
4055 channel->ch_error = FALSE;
4056 return OK;
4057 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004058}
4059
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004060/*
4061 * Common for "ch_sendexpr()" and "ch_sendraw()".
4062 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004063 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 * Otherwise returns NULL.
4065 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004066 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004067send_common(
4068 typval_T *argvars,
4069 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004070 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004071 int id,
4072 int eval,
4073 jobopt_T *opt,
4074 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004075 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076{
4077 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004078 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004079
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004080 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004081 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004082 if (channel == NULL)
4083 return NULL;
4084 part_send = channel_part_send(channel);
4085 *part_read = channel_part_read(channel);
4086
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004087 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 return NULL;
4089
4090 /* Set the callback. An empty callback means no callback and not reading
4091 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4092 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004093 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004094 {
4095 if (eval)
4096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004097 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004098 return NULL;
4099 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004100 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004101 }
4102
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004103 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004104 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004105 return channel;
4106 return NULL;
4107}
4108
4109/*
4110 * common for "ch_evalexpr()" and "ch_sendexpr()"
4111 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004112 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4114{
4115 char_u *text;
4116 typval_T *listtv;
4117 channel_T *channel;
4118 int id;
4119 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004120 ch_part_T part_send;
4121 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004122 jobopt_T opt;
4123 int timeout;
4124
4125 /* return an empty string by default */
4126 rettv->v_type = VAR_STRING;
4127 rettv->vval.v_string = NULL;
4128
Bram Moolenaar437905c2016-04-26 19:01:05 +02004129 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004130 if (channel == NULL)
4131 return;
4132 part_send = channel_part_send(channel);
4133
4134 ch_mode = channel_get_mode(channel, part_send);
4135 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004137 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004138 return;
4139 }
4140
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004141 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004142 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004143 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004144 if (text == NULL)
4145 return;
4146
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004147 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004148 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4149 vim_free(text);
4150 if (channel != NULL && eval)
4151 {
4152 if (opt.jo_set & JO_TIMEOUT)
4153 timeout = opt.jo_timeout;
4154 else
4155 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4157 == OK)
4158 {
4159 list_T *list = listtv->vval.v_list;
4160
4161 /* Move the item from the list and then change the type to
4162 * avoid the value being freed. */
4163 *rettv = list->lv_last->li_tv;
4164 list->lv_last->li_tv.v_type = VAR_NUMBER;
4165 free_tv(listtv);
4166 }
4167 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004168 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004169}
4170
4171/*
4172 * common for "ch_evalraw()" and "ch_sendraw()"
4173 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004174 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004175ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4176{
4177 char_u buf[NUMBUFLEN];
4178 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004179 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004180 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004181 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004182 jobopt_T opt;
4183 int timeout;
4184
4185 /* return an empty string by default */
4186 rettv->v_type = VAR_STRING;
4187 rettv->vval.v_string = NULL;
4188
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004189 if (argvars[1].v_type == VAR_BLOB)
4190 {
4191 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4192 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4193 }
4194 else
4195 {
4196 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004197 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004198 }
4199 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4201 if (channel != NULL && eval)
4202 {
4203 if (opt.jo_set & JO_TIMEOUT)
4204 timeout = opt.jo_timeout;
4205 else
4206 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004207 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004208 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004209 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004210 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004211}
4212
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004213#define KEEP_OPEN_TIME 20 /* msec */
Bram Moolenaarf3360612017-10-01 16:21:31 +02004214
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004215#if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004216/*
4217 * Add open channels to the poll struct.
4218 * Return the adjusted struct index.
4219 * The type of "fds" is hidden to avoid problems with the function proto.
4220 */
4221 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004222channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004223{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004224 int nfd = nfd_in;
4225 channel_T *channel;
4226 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004227 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004228
Bram Moolenaar77073442016-02-13 23:23:53 +01004229 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004230 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004231 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004232 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004233 chanpart_T *ch_part = &channel->ch_part[part];
4234
4235 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004236 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004237 if (channel->ch_keep_open)
4238 {
4239 /* For unknown reason poll() returns immediately for a
4240 * keep-open channel. Instead of adding it to the fds add
4241 * a short timeout and check, like polling. */
4242 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4243 *towait = KEEP_OPEN_TIME;
4244 }
4245 else
4246 {
4247 ch_part->ch_poll_idx = nfd;
4248 fds[nfd].fd = ch_part->ch_fd;
4249 fds[nfd].events = POLLIN;
4250 nfd++;
4251 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004252 }
4253 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004254 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004255 }
4256 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004257
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004258 nfd = channel_fill_poll_write(nfd, fds);
4259
Bram Moolenaare0874f82016-01-24 20:36:41 +01004260 return nfd;
4261}
4262
4263/*
4264 * The type of "fds" is hidden to avoid problems with the function proto.
4265 */
4266 int
4267channel_poll_check(int ret_in, void *fds_in)
4268{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004269 int ret = ret_in;
4270 channel_T *channel;
4271 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004272 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004273 int idx;
4274 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004275
Bram Moolenaar77073442016-02-13 23:23:53 +01004276 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004277 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004278 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004279 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004280 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004281
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004282 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004283 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004284 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004285 --ret;
4286 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004287 else if (channel->ch_part[part].ch_fd != INVALID_FD
4288 && channel->ch_keep_open)
4289 {
4290 /* polling a keep-open channel */
4291 channel_read(channel, part, "channel_poll_check_keep_open");
4292 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004293 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004294
4295 in_part = &channel->ch_part[PART_IN];
4296 idx = in_part->ch_poll_idx;
4297 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4298 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004299 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004300 --ret;
4301 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004302 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004303
4304 return ret;
4305}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004306#endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004307
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004308#if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004309
Bram Moolenaare0874f82016-01-24 20:36:41 +01004310/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004311 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004312 */
4313 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004314channel_select_setup(
4315 int maxfd_in,
4316 void *rfds_in,
4317 void *wfds_in,
4318 struct timeval *tv,
4319 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004320{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004321 int maxfd = maxfd_in;
4322 channel_T *channel;
4323 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004324 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004325 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004326
Bram Moolenaar77073442016-02-13 23:23:53 +01004327 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004328 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004329 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004330 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004331 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004332
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004333 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004334 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004335 if (channel->ch_keep_open)
4336 {
4337 /* For unknown reason select() returns immediately for a
4338 * keep-open channel. Instead of adding it to the rfds add
4339 * a short timeout and check, like polling. */
4340 if (*tvp == NULL || tv->tv_sec > 0
4341 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4342 {
4343 *tvp = tv;
4344 tv->tv_sec = 0;
4345 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4346 }
4347 }
4348 else
4349 {
4350 FD_SET((int)fd, rfds);
4351 if (maxfd < (int)fd)
4352 maxfd = (int)fd;
4353 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004354 }
4355 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004356 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004357
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004358 maxfd = channel_fill_wfds(maxfd, wfds);
4359
Bram Moolenaare0874f82016-01-24 20:36:41 +01004360 return maxfd;
4361}
4362
4363/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004364 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004365 */
4366 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004367channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004368{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004369 int ret = ret_in;
4370 channel_T *channel;
4371 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004372 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004373 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004374 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004375
Bram Moolenaar77073442016-02-13 23:23:53 +01004376 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004377 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004378 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004379 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004380 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004381
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004382 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004383 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004384 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004385 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004386 --ret;
4387 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004388 else if (fd != INVALID_FD && channel->ch_keep_open)
4389 {
4390 /* polling a keep-open channel */
4391 channel_read(channel, part, "channel_select_check_keep_open");
4392 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004393 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004394
4395 in_part = &channel->ch_part[PART_IN];
4396 if (ret > 0 && in_part->ch_fd != INVALID_FD
4397 && FD_ISSET(in_part->ch_fd, wfds))
4398 {
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004399 // Clear the flag first, ch_fd may change in channel_write_input().
Bram Moolenaar3c518402017-09-08 20:47:00 +02004400 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004401 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004402 --ret;
4403 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004404 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004405
4406 return ret;
4407}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004408#endif // !MSWIN && HAVE_SELECT
Bram Moolenaare0874f82016-01-24 20:36:41 +01004409
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004410/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004411 * Execute queued up commands.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004412 * Invoked from the main loop when it's safe to execute received commands,
4413 * and during a blocking wait for ch_evalexpr().
Bram Moolenaard7ece102016-02-02 23:23:02 +01004414 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004415 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004416 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004417channel_parse_messages(void)
4418{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004419 channel_T *channel = first_channel;
4420 int ret = FALSE;
4421 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004422 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004423#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004424 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004425
4426 ELAPSED_INIT(start_tv);
4427#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004428
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004429 ++safe_to_invoke_callback;
4430
Bram Moolenaard0b65022016-03-06 21:50:33 +01004431 /* Only do this message when another message was given, otherwise we get
4432 * lots of them. */
Bram Moolenaar101e9922019-09-25 21:43:11 +02004433 if ((did_repeated_msg & REPEATED_MSG_LOOKING) == 0)
Bram Moolenaard0b65022016-03-06 21:50:33 +01004434 {
4435 ch_log(NULL, "looking for messages on channels");
Bram Moolenaar101e9922019-09-25 21:43:11 +02004436 // now we should also give the message for SafeState
4437 did_repeated_msg = REPEATED_MSG_LOOKING;
Bram Moolenaard0b65022016-03-06 21:50:33 +01004438 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004439 while (channel != NULL)
4440 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004441 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004442 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004443 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004444 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004445 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004446 channel = first_channel;
4447 continue;
4448 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004449 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004450 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004451 if (channel->ch_killing)
4452 {
4453 channel_free_contents(channel);
4454 channel->ch_job->jv_channel = NULL;
4455 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004456 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004457 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004458 channel = first_channel;
4459 continue;
4460 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004461 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004462 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004463 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004464 channel_free(channel);
4465 channel = first_channel;
4466 part = PART_SOCK;
4467 continue;
4468 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004469 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004470 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004471 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004472 /* Increase the refcount, in case the handler causes the channel
4473 * to be unreferenced or closed. */
4474 ++channel->ch_refcount;
4475 r = may_invoke_callback(channel, part);
4476 if (r == OK)
4477 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004478 if (channel_unref(channel) || (r == OK
4479#ifdef ELAPSED_FUNC
4480 /* Limit the time we loop here to 100 msec, otherwise
4481 * Vim becomes unresponsive when the callback takes
4482 * more than a bit of time. */
4483 && ELAPSED_FUNC(start_tv) < 100L
4484#endif
4485 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004486 {
4487 /* channel was freed or something was done, start over */
4488 channel = first_channel;
4489 part = PART_SOCK;
4490 continue;
4491 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004492 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004493 if (part < PART_ERR)
4494 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004495 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004496 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004497 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004498 part = PART_SOCK;
4499 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004500 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004501
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004502 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004503 {
4504 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004505 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004506 }
4507
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004508 --safe_to_invoke_callback;
4509
Bram Moolenaard7ece102016-02-02 23:23:02 +01004510 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004511}
4512
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004513/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004514 * Return TRUE if any channel has readahead. That means we should not block on
4515 * waiting for input.
4516 */
4517 int
4518channel_any_readahead(void)
4519{
4520 channel_T *channel = first_channel;
4521 ch_part_T part = PART_SOCK;
4522
4523 while (channel != NULL)
4524 {
4525 if (channel_has_readahead(channel, part))
4526 return TRUE;
4527 if (part < PART_ERR)
4528 ++part;
4529 else
4530 {
4531 channel = channel->ch_next;
4532 part = PART_SOCK;
4533 }
4534 }
4535 return FALSE;
4536}
4537
4538/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004539 * Mark references to lists used in channels.
4540 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004541 int
4542set_ref_in_channel(int copyID)
4543{
Bram Moolenaar77073442016-02-13 23:23:53 +01004544 int abort = FALSE;
4545 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004546 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004547
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004548 for (channel = first_channel; !abort && channel != NULL;
4549 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004550 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004551 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004552 tv.v_type = VAR_CHANNEL;
4553 tv.vval.v_channel = channel;
4554 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004555 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004556 return abort;
4557}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004558
4559/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004560 * Return the "part" to write to for "channel".
4561 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004562 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004563channel_part_send(channel_T *channel)
4564{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004565 if (channel->CH_SOCK_FD == INVALID_FD)
4566 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004567 return PART_SOCK;
4568}
4569
4570/*
4571 * Return the default "part" to read from for "channel".
4572 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004573 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004574channel_part_read(channel_T *channel)
4575{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004576 if (channel->CH_SOCK_FD == INVALID_FD)
4577 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004578 return PART_SOCK;
4579}
4580
4581/*
4582 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004583 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004584 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004585 static ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004586channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004587{
Bram Moolenaar77073442016-02-13 23:23:53 +01004588 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004589 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004590 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004591}
4592
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004593/*
4594 * Return the timeout of "channel"/"part"
4595 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004596 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004597channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004598{
4599 return channel->ch_part[part].ch_timeout;
4600}
4601
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004602 static int
4603handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4604{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004605 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004606
4607 opt->jo_set |= jo;
4608 if (STRCMP(val, "nl") == 0)
4609 *modep = MODE_NL;
4610 else if (STRCMP(val, "raw") == 0)
4611 *modep = MODE_RAW;
4612 else if (STRCMP(val, "js") == 0)
4613 *modep = MODE_JS;
4614 else if (STRCMP(val, "json") == 0)
4615 *modep = MODE_JSON;
4616 else
4617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004618 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004619 return FAIL;
4620 }
4621 return OK;
4622}
4623
4624 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004625handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004626{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004627 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004628
4629 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4630 if (STRCMP(val, "null") == 0)
4631 opt->jo_io[part] = JIO_NULL;
4632 else if (STRCMP(val, "pipe") == 0)
4633 opt->jo_io[part] = JIO_PIPE;
4634 else if (STRCMP(val, "file") == 0)
4635 opt->jo_io[part] = JIO_FILE;
4636 else if (STRCMP(val, "buffer") == 0)
4637 opt->jo_io[part] = JIO_BUFFER;
4638 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4639 opt->jo_io[part] = JIO_OUT;
4640 else
4641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004642 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004643 return FAIL;
4644 }
4645 return OK;
4646}
4647
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004648/*
4649 * Clear a jobopt_T before using it.
4650 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004651 void
4652clear_job_options(jobopt_T *opt)
4653{
4654 vim_memset(opt, 0, sizeof(jobopt_T));
4655}
4656
4657/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004658 * Free any members of a jobopt_T.
4659 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004660 static void
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004661free_job_options(jobopt_T *opt)
4662{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004663 if (opt->jo_callback.cb_partial != NULL)
4664 partial_unref(opt->jo_callback.cb_partial);
4665 else if (opt->jo_callback.cb_name != NULL)
4666 func_unref(opt->jo_callback.cb_name);
4667 if (opt->jo_out_cb.cb_partial != NULL)
4668 partial_unref(opt->jo_out_cb.cb_partial);
4669 else if (opt->jo_out_cb.cb_name != NULL)
4670 func_unref(opt->jo_out_cb.cb_name);
4671 if (opt->jo_err_cb.cb_partial != NULL)
4672 partial_unref(opt->jo_err_cb.cb_partial);
4673 else if (opt->jo_err_cb.cb_name != NULL)
4674 func_unref(opt->jo_err_cb.cb_name);
4675 if (opt->jo_close_cb.cb_partial != NULL)
4676 partial_unref(opt->jo_close_cb.cb_partial);
4677 else if (opt->jo_close_cb.cb_name != NULL)
4678 func_unref(opt->jo_close_cb.cb_name);
4679 if (opt->jo_exit_cb.cb_partial != NULL)
4680 partial_unref(opt->jo_exit_cb.cb_partial);
4681 else if (opt->jo_exit_cb.cb_name != NULL)
4682 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004683 if (opt->jo_env != NULL)
4684 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004685}
4686
4687/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004688 * Get the PART_ number from the first character of an option name.
4689 */
4690 static int
4691part_from_char(int c)
4692{
4693 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4694}
4695
4696/*
4697 * Get the option entries from the dict in "tv", parse them and put the result
4698 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004699 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004700 * If an option value is invalid return FAIL.
4701 */
4702 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004703get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004704{
4705 typval_T *item;
4706 char_u *val;
4707 dict_T *dict;
4708 int todo;
4709 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004710 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004711
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004712 if (tv->v_type == VAR_UNKNOWN)
4713 return OK;
4714 if (tv->v_type != VAR_DICT)
4715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004717 return FAIL;
4718 }
4719 dict = tv->vval.v_dict;
4720 if (dict == NULL)
4721 return OK;
4722
4723 todo = (int)dict->dv_hashtab.ht_used;
4724 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4725 if (!HASHITEM_EMPTY(hi))
4726 {
4727 item = &dict_lookup(hi)->di_tv;
4728
4729 if (STRCMP(hi->hi_key, "mode") == 0)
4730 {
4731 if (!(supported & JO_MODE))
4732 break;
4733 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4734 return FAIL;
4735 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004736 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004737 {
4738 if (!(supported & JO_IN_MODE))
4739 break;
4740 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4741 == FAIL)
4742 return FAIL;
4743 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004744 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004745 {
4746 if (!(supported & JO_OUT_MODE))
4747 break;
4748 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4749 == FAIL)
4750 return FAIL;
4751 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004752 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004753 {
4754 if (!(supported & JO_ERR_MODE))
4755 break;
4756 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4757 == FAIL)
4758 return FAIL;
4759 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004760 else if (STRCMP(hi->hi_key, "noblock") == 0)
4761 {
4762 if (!(supported & JO_MODE))
4763 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004764 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004765 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004766 else if (STRCMP(hi->hi_key, "in_io") == 0
4767 || STRCMP(hi->hi_key, "out_io") == 0
4768 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004769 {
4770 if (!(supported & JO_OUT_IO))
4771 break;
4772 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4773 return FAIL;
4774 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004775 else if (STRCMP(hi->hi_key, "in_name") == 0
4776 || STRCMP(hi->hi_key, "out_name") == 0
4777 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004778 {
4779 part = part_from_char(*hi->hi_key);
4780
4781 if (!(supported & JO_OUT_IO))
4782 break;
4783 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4784 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004785 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004786 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004787 else if (STRCMP(hi->hi_key, "pty") == 0)
4788 {
4789 if (!(supported & JO_MODE))
4790 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004791 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004792 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004793 else if (STRCMP(hi->hi_key, "in_buf") == 0
4794 || STRCMP(hi->hi_key, "out_buf") == 0
4795 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004796 {
4797 part = part_from_char(*hi->hi_key);
4798
4799 if (!(supported & JO_OUT_IO))
4800 break;
4801 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004802 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004803 if (opt->jo_io_buf[part] <= 0)
4804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004805 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004806 return FAIL;
4807 }
4808 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004810 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004811 return FAIL;
4812 }
4813 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004814 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4815 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4816 {
4817 part = part_from_char(*hi->hi_key);
4818
4819 if (!(supported & JO_OUT_IO))
4820 break;
4821 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004822 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004823 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004824 else if (STRCMP(hi->hi_key, "out_msg") == 0
4825 || STRCMP(hi->hi_key, "err_msg") == 0)
4826 {
4827 part = part_from_char(*hi->hi_key);
4828
4829 if (!(supported & JO_OUT_IO))
4830 break;
4831 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004832 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004833 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004834 else if (STRCMP(hi->hi_key, "in_top") == 0
4835 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004836 {
4837 linenr_T *lp;
4838
4839 if (!(supported & JO_OUT_IO))
4840 break;
4841 if (hi->hi_key[3] == 't')
4842 {
4843 lp = &opt->jo_in_top;
4844 opt->jo_set |= JO_IN_TOP;
4845 }
4846 else
4847 {
4848 lp = &opt->jo_in_bot;
4849 opt->jo_set |= JO_IN_BOT;
4850 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004851 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004852 if (*lp < 0)
4853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004854 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004855 return FAIL;
4856 }
4857 }
4858 else if (STRCMP(hi->hi_key, "channel") == 0)
4859 {
4860 if (!(supported & JO_OUT_IO))
4861 break;
4862 opt->jo_set |= JO_CHANNEL;
4863 if (item->v_type != VAR_CHANNEL)
4864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004865 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004866 return FAIL;
4867 }
4868 opt->jo_channel = item->vval.v_channel;
4869 }
4870 else if (STRCMP(hi->hi_key, "callback") == 0)
4871 {
4872 if (!(supported & JO_CALLBACK))
4873 break;
4874 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004875 opt->jo_callback = get_callback(item);
4876 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004878 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004879 return FAIL;
4880 }
4881 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004882 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004883 {
4884 if (!(supported & JO_OUT_CALLBACK))
4885 break;
4886 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004887 opt->jo_out_cb = get_callback(item);
4888 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004890 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004891 return FAIL;
4892 }
4893 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004894 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004895 {
4896 if (!(supported & JO_ERR_CALLBACK))
4897 break;
4898 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004899 opt->jo_err_cb = get_callback(item);
4900 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004902 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004903 return FAIL;
4904 }
4905 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004906 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004907 {
4908 if (!(supported & JO_CLOSE_CALLBACK))
4909 break;
4910 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004911 opt->jo_close_cb = get_callback(item);
4912 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004914 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004915 return FAIL;
4916 }
4917 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004918 else if (STRCMP(hi->hi_key, "drop") == 0)
4919 {
4920 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004921 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004922
4923 if (STRCMP(val, "never") == 0)
4924 never = TRUE;
4925 else if (STRCMP(val, "auto") != 0)
4926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004927 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004928 return FAIL;
4929 }
4930 opt->jo_drop_never = never;
4931 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004932 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4933 {
4934 if (!(supported & JO_EXIT_CB))
4935 break;
4936 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004937 opt->jo_exit_cb = get_callback(item);
4938 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004940 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004941 return FAIL;
4942 }
4943 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004944#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004945 else if (STRCMP(hi->hi_key, "term_name") == 0)
4946 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004947 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004948 break;
4949 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004950 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004951 if (opt->jo_term_name == NULL)
4952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004953 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004954 return FAIL;
4955 }
4956 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004957 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4958 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004959 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004960 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004961 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004962 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004964 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004965 return FAIL;
4966 }
4967 opt->jo_set2 |= JO2_TERM_FINISH;
4968 opt->jo_term_finish = *val;
4969 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004970 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4971 {
4972 char_u *p;
4973
4974 if (!(supported2 & JO2_TERM_OPENCMD))
4975 break;
4976 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004977 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004978 if (p != NULL)
4979 {
4980 /* Must have %d and no other %. */
4981 p = vim_strchr(p, '%');
4982 if (p != NULL && (p[1] != 'd'
4983 || vim_strchr(p + 2, '%') != NULL))
4984 p = NULL;
4985 }
4986 if (p == NULL)
4987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004988 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004989 return FAIL;
4990 }
4991 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004992 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4993 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004994 char_u *p;
4995
4996 if (!(supported2 & JO2_EOF_CHARS))
4997 break;
4998 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004999 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02005000 if (p == NULL)
5001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005002 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02005003 return FAIL;
5004 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02005005 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005006 else if (STRCMP(hi->hi_key, "term_rows") == 0)
5007 {
5008 if (!(supported2 & JO2_TERM_ROWS))
5009 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005010 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005011 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005012 }
5013 else if (STRCMP(hi->hi_key, "term_cols") == 0)
5014 {
5015 if (!(supported2 & JO2_TERM_COLS))
5016 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005017 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005018 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005019 }
5020 else if (STRCMP(hi->hi_key, "vertical") == 0)
5021 {
5022 if (!(supported2 & JO2_VERTICAL))
5023 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005024 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005025 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005026 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005027 else if (STRCMP(hi->hi_key, "curwin") == 0)
5028 {
5029 if (!(supported2 & JO2_CURWIN))
5030 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005031 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005032 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005033 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005034 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5035 {
5036 int nr;
5037
5038 if (!(supported2 & JO2_CURWIN))
5039 break;
5040 opt->jo_set2 |= JO2_BUFNR;
5041 nr = tv_get_number(item);
5042 if (nr <= 0)
5043 {
5044 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5045 return FAIL;
5046 }
5047 opt->jo_bufnr_buf = buflist_findnr(nr);
5048 if (opt->jo_bufnr_buf == NULL)
5049 {
5050 semsg(_(e_nobufnr), (long)nr);
5051 return FAIL;
5052 }
5053 if (opt->jo_bufnr_buf->b_nwindows == 0
5054 || opt->jo_bufnr_buf->b_term == NULL)
5055 {
5056 semsg(_(e_invarg2), "bufnr");
5057 return FAIL;
5058 }
5059 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005060 else if (STRCMP(hi->hi_key, "hidden") == 0)
5061 {
5062 if (!(supported2 & JO2_HIDDEN))
5063 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005064 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005065 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005066 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005067 else if (STRCMP(hi->hi_key, "norestore") == 0)
5068 {
5069 if (!(supported2 & JO2_NORESTORE))
5070 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005071 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005072 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005073 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005074 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5075 {
5076 if (!(supported2 & JO2_TERM_KILL))
5077 break;
5078 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005079 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005080 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005081 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005082 {
5083 char_u *p;
5084
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005085 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005086 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005087 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005088 p = tv_get_string_chk(item);
5089 if (p == NULL)
5090 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005091 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005092 return FAIL;
5093 }
5094 // Allow empty string, "winpty", "conpty".
5095 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5096 || STRCMP(p, "conpty") == 0))
5097 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005098 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005099 return FAIL;
5100 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005101 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005102 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005103# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5104 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5105 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005106 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005107 listitem_T *li;
5108 long_u rgb[16];
5109
5110 if (!(supported2 & JO2_ANSI_COLORS))
5111 break;
5112
5113 if (item == NULL || item->v_type != VAR_LIST
5114 || item->vval.v_list == NULL)
5115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005116 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005117 return FAIL;
5118 }
5119
5120 li = item->vval.v_list->lv_first;
5121 for (; li != NULL && n < 16; li = li->li_next, n++)
5122 {
5123 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005124 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005125
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005126 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005127 if (color_name == NULL)
5128 return FAIL;
5129
5130 guicolor = GUI_GET_COLOR(color_name);
5131 if (guicolor == INVALCOLOR)
5132 return FAIL;
5133
5134 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5135 }
5136
5137 if (n != 16 || li != NULL)
5138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005139 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005140 return FAIL;
5141 }
5142
5143 opt->jo_set2 |= JO2_ANSI_COLORS;
5144 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5145 }
5146# endif
Bram Moolenaard2842ea2019-09-26 23:08:54 +02005147 else if (STRCMP(hi->hi_key, "term_api") == 0)
5148 {
5149 if (!(supported2 & JO2_TERM_API))
5150 break;
5151 opt->jo_set2 |= JO2_TERM_API;
5152 opt->jo_term_api = tv_get_string_buf_chk(item,
5153 opt->jo_term_api_buf);
5154 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005155#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005156 else if (STRCMP(hi->hi_key, "env") == 0)
5157 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005158 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005159 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005160 if (item->v_type != VAR_DICT)
5161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005162 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005163 return FAIL;
5164 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005165 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005166 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005167 if (opt->jo_env != NULL)
5168 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005169 }
5170 else if (STRCMP(hi->hi_key, "cwd") == 0)
5171 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005172 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005173 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005174 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005175 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005176#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005177 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5178#endif
5179 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005181 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005182 return FAIL;
5183 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005184 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005185 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005186 else if (STRCMP(hi->hi_key, "waittime") == 0)
5187 {
5188 if (!(supported & JO_WAITTIME))
5189 break;
5190 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005192 }
5193 else if (STRCMP(hi->hi_key, "timeout") == 0)
5194 {
5195 if (!(supported & JO_TIMEOUT))
5196 break;
5197 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005198 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005199 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005200 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005201 {
5202 if (!(supported & JO_OUT_TIMEOUT))
5203 break;
5204 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005205 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005206 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005207 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005208 {
5209 if (!(supported & JO_ERR_TIMEOUT))
5210 break;
5211 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005212 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 }
5214 else if (STRCMP(hi->hi_key, "part") == 0)
5215 {
5216 if (!(supported & JO_PART))
5217 break;
5218 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005219 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005220 if (STRCMP(val, "err") == 0)
5221 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005222 else if (STRCMP(val, "out") == 0)
5223 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005224 else
5225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005226 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005227 return FAIL;
5228 }
5229 }
5230 else if (STRCMP(hi->hi_key, "id") == 0)
5231 {
5232 if (!(supported & JO_ID))
5233 break;
5234 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005235 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005236 }
5237 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5238 {
5239 if (!(supported & JO_STOPONEXIT))
5240 break;
5241 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005242 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005243 opt->jo_soe_buf);
5244 if (opt->jo_stoponexit == NULL)
5245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005246 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005247 return FAIL;
5248 }
5249 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005250 else if (STRCMP(hi->hi_key, "block_write") == 0)
5251 {
5252 if (!(supported & JO_BLOCK_WRITE))
5253 break;
5254 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005255 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005256 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005257 else
5258 break;
5259 --todo;
5260 }
5261 if (todo > 0)
5262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005263 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005264 return FAIL;
5265 }
5266
5267 return OK;
5268}
5269
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005270static job_T *first_job = NULL;
5271
5272 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005273job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005274{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005275 int i;
5276
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005277 ch_log(job->jv_channel, "Freeing job");
5278 if (job->jv_channel != NULL)
5279 {
5280 /* The link from the channel to the job doesn't count as a reference,
5281 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005282 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005283 * NULL the reference. We don't set ch_job_killed, unreferencing the
5284 * job doesn't mean it stops running. */
5285 job->jv_channel->ch_job = NULL;
5286 channel_unref(job->jv_channel);
5287 }
5288 mch_clear_job(job);
5289
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005290 vim_free(job->jv_tty_in);
5291 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005292 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005293#ifdef UNIX
5294 vim_free(job->jv_termsig);
5295#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005296#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005297 vim_free(job->jv_tty_type);
5298#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005299 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005300 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005301 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005302 for (i = 0; job->jv_argv[i] != NULL; i++)
5303 vim_free(job->jv_argv[i]);
5304 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005305 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005306}
5307
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005308/*
5309 * Remove "job" from the list of jobs.
5310 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005311 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005312job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005313{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005314 if (job->jv_next != NULL)
5315 job->jv_next->jv_prev = job->jv_prev;
5316 if (job->jv_prev == NULL)
5317 first_job = job->jv_next;
5318 else
5319 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005320}
5321
5322 static void
5323job_free_job(job_T *job)
5324{
5325 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005326 vim_free(job);
5327}
5328
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005329 static void
5330job_free(job_T *job)
5331{
5332 if (!in_free_unref_items)
5333 {
5334 job_free_contents(job);
5335 job_free_job(job);
5336 }
5337}
5338
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005339static job_T *jobs_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005340
5341/*
5342 * Put "job" in a list to be freed later, when it's no longer referenced.
5343 */
5344 static void
5345job_free_later(job_T *job)
5346{
5347 job_unlink(job);
5348 job->jv_next = jobs_to_free;
5349 jobs_to_free = job;
5350}
5351
5352 static void
5353free_jobs_to_free_later(void)
5354{
5355 job_T *job;
5356
5357 while (jobs_to_free != NULL)
5358 {
5359 job = jobs_to_free;
5360 jobs_to_free = job->jv_next;
5361 job_free_contents(job);
5362 vim_free(job);
5363 }
5364}
5365
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005366#if defined(EXITFREE) || defined(PROTO)
5367 void
5368job_free_all(void)
5369{
5370 while (first_job != NULL)
5371 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005372 free_jobs_to_free_later();
5373
5374# ifdef FEAT_TERMINAL
5375 free_unused_terminals();
5376# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005377}
5378#endif
5379
5380/*
5381 * Return TRUE if we need to check if the process of "job" has ended.
5382 */
5383 static int
5384job_need_end_check(job_T *job)
5385{
5386 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005387 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005388}
5389
5390/*
5391 * Return TRUE if the channel of "job" is still useful.
5392 */
5393 static int
5394job_channel_still_useful(job_T *job)
5395{
5396 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5397}
5398
5399/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005400 * Return TRUE if the channel of "job" is closeable.
5401 */
5402 static int
5403job_channel_can_close(job_T *job)
5404{
5405 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5406}
5407
5408/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005409 * Return TRUE if the job should not be freed yet. Do not free the job when
5410 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5411 * or when the associated channel will do something with the job output.
5412 */
5413 static int
5414job_still_useful(job_T *job)
5415{
5416 return job_need_end_check(job) || job_channel_still_useful(job);
5417}
5418
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005419#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005420/*
5421 * Return TRUE when there is any running job that we care about.
5422 */
5423 int
5424job_any_running()
5425{
5426 job_T *job;
5427
5428 for (job = first_job; job != NULL; job = job->jv_next)
5429 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005430 {
5431 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005432 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005433 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005434 return FALSE;
5435}
5436#endif
5437
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005438#if !defined(USE_ARGV) || defined(PROTO)
5439/*
5440 * Escape one argument for an external command.
5441 * Returns the escaped string in allocated memory. NULL when out of memory.
5442 */
5443 static char_u *
5444win32_escape_arg(char_u *arg)
5445{
5446 int slen, dlen;
5447 int escaping = 0;
5448 int i;
5449 char_u *s, *d;
5450 char_u *escaped_arg;
5451 int has_spaces = FALSE;
5452
5453 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005454 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005455 dlen = slen;
5456 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5457 {
5458 if (*s == '"' || *s == '\\')
5459 ++dlen;
5460 if (*s == ' ' || *s == '\t')
5461 has_spaces = TRUE;
5462 }
5463
5464 if (has_spaces)
5465 dlen += 2;
5466
5467 if (dlen == slen)
5468 return vim_strsave(arg);
5469
5470 /* Allocate memory for the result and fill it. */
5471 escaped_arg = alloc(dlen + 1);
5472 if (escaped_arg == NULL)
5473 return NULL;
5474 memset(escaped_arg, 0, dlen+1);
5475
5476 d = escaped_arg;
5477
5478 if (has_spaces)
5479 *d++ = '"';
5480
5481 for (s = arg; *s != NUL;)
5482 {
5483 switch (*s)
5484 {
5485 case '"':
5486 for (i = 0; i < escaping; i++)
5487 *d++ = '\\';
5488 escaping = 0;
5489 *d++ = '\\';
5490 *d++ = *s++;
5491 break;
5492 case '\\':
5493 escaping++;
5494 *d++ = *s++;
5495 break;
5496 default:
5497 escaping = 0;
5498 MB_COPY_CHAR(s, d);
5499 break;
5500 }
5501 }
5502
5503 /* add terminating quote and finish with a NUL */
5504 if (has_spaces)
5505 {
5506 for (i = 0; i < escaping; i++)
5507 *d++ = '\\';
5508 *d++ = '"';
5509 }
5510 *d = NUL;
5511
5512 return escaped_arg;
5513}
5514
5515/*
5516 * Build a command line from a list, taking care of escaping.
5517 * The result is put in gap->ga_data.
5518 * Returns FAIL when out of memory.
5519 */
5520 int
5521win32_build_cmd(list_T *l, garray_T *gap)
5522{
5523 listitem_T *li;
5524 char_u *s;
5525
5526 for (li = l->lv_first; li != NULL; li = li->li_next)
5527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005528 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005529 if (s == NULL)
5530 return FAIL;
5531 s = win32_escape_arg(s);
5532 if (s == NULL)
5533 return FAIL;
5534 ga_concat(gap, s);
5535 vim_free(s);
5536 if (li->li_next != NULL)
5537 ga_append(gap, ' ');
5538 }
5539 return OK;
5540}
5541#endif
5542
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005543/*
5544 * NOTE: Must call job_cleanup() only once right after the status of "job"
5545 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5546 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005547 * If the job is no longer used it will be removed from the list of jobs, and
5548 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005549 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005550 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005551job_cleanup(job_T *job)
5552{
5553 if (job->jv_status != JOB_ENDED)
5554 return;
5555
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005556 /* Ready to cleanup the job. */
5557 job->jv_status = JOB_FINISHED;
5558
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005559 /* When only channel-in is kept open, close explicitly. */
5560 if (job->jv_channel != NULL)
5561 ch_close_part(job->jv_channel, PART_IN);
5562
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005563 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005564 {
5565 typval_T argv[3];
5566 typval_T rettv;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005567
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005568 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005569 ch_log(job->jv_channel, "Invoking exit callback %s",
5570 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005571 ++job->jv_refcount;
5572 argv[0].v_type = VAR_JOB;
5573 argv[0].vval.v_job = job;
5574 argv[1].v_type = VAR_NUMBER;
5575 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005576 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005577 clear_tv(&rettv);
5578 --job->jv_refcount;
5579 channel_need_redraw = TRUE;
5580 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005581
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005582 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5583 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005584
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005585 // Do not free the job in case the close callback of the associated channel
5586 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005587 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005588 // The job was already unreferenced and the associated channel was
5589 // detached, now that it ended it can be freed. However, a caller might
5590 // still use it, thus free it a bit later.
5591 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005592}
5593
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005594/*
5595 * Mark references in jobs that are still useful.
5596 */
5597 int
5598set_ref_in_job(int copyID)
5599{
5600 int abort = FALSE;
5601 job_T *job;
5602 typval_T tv;
5603
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005604 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005605 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005606 {
5607 tv.v_type = VAR_JOB;
5608 tv.vval.v_job = job;
5609 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5610 }
5611 return abort;
5612}
5613
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005614/*
5615 * Dereference "job". Note that after this "job" may have been freed.
5616 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005617 void
5618job_unref(job_T *job)
5619{
5620 if (job != NULL && --job->jv_refcount <= 0)
5621 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005622 /* Do not free the job if there is a channel where the close callback
5623 * may get the job info. */
5624 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005625 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005626 /* Do not free the job when it has not ended yet and there is a
5627 * "stoponexit" flag or an exit callback. */
5628 if (!job_need_end_check(job))
5629 {
5630 job_free(job);
5631 }
5632 else if (job->jv_channel != NULL)
5633 {
5634 /* Do remove the link to the channel, otherwise it hangs
5635 * around until Vim exits. See job_free() for refcount. */
5636 ch_log(job->jv_channel, "detaching channel from job");
5637 job->jv_channel->ch_job = NULL;
5638 channel_unref(job->jv_channel);
5639 job->jv_channel = NULL;
5640 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005641 }
5642 }
5643}
5644
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005645 int
5646free_unused_jobs_contents(int copyID, int mask)
5647{
5648 int did_free = FALSE;
5649 job_T *job;
5650
5651 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005652 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005653 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005654 {
5655 /* Free the channel and ordinary items it contains, but don't
5656 * recurse into Lists, Dictionaries etc. */
5657 job_free_contents(job);
5658 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005659 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005660 return did_free;
5661}
5662
5663 void
5664free_unused_jobs(int copyID, int mask)
5665{
5666 job_T *job;
5667 job_T *job_next;
5668
5669 for (job = first_job; job != NULL; job = job_next)
5670 {
5671 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005672 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005673 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005674 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005675 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005676 job_free_job(job);
5677 }
5678 }
5679}
5680
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005681/*
5682 * Allocate a job. Sets the refcount to one and sets options default.
5683 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005684 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005685job_alloc(void)
5686{
5687 job_T *job;
5688
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005689 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005690 if (job != NULL)
5691 {
5692 job->jv_refcount = 1;
5693 job->jv_stoponexit = vim_strsave((char_u *)"term");
5694
5695 if (first_job != NULL)
5696 {
5697 first_job->jv_prev = job;
5698 job->jv_next = first_job;
5699 }
5700 first_job = job;
5701 }
5702 return job;
5703}
5704
5705 void
5706job_set_options(job_T *job, jobopt_T *opt)
5707{
5708 if (opt->jo_set & JO_STOPONEXIT)
5709 {
5710 vim_free(job->jv_stoponexit);
5711 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5712 job->jv_stoponexit = NULL;
5713 else
5714 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5715 }
5716 if (opt->jo_set & JO_EXIT_CB)
5717 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005718 free_callback(&job->jv_exit_cb);
5719 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005720 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005721 job->jv_exit_cb.cb_name = NULL;
5722 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005723 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005724 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005725 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005726 }
5727}
5728
5729/*
5730 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5731 */
5732 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005733job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005734{
5735 job_T *job;
5736
5737 for (job = first_job; job != NULL; job = job->jv_next)
5738 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005739 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005740}
5741
5742/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005743 * Return TRUE when there is any job that has an exit callback and might exit,
5744 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005745 */
5746 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005747has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005748{
5749 job_T *job;
5750
5751 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005752 /* Only should check if the channel has been closed, if the channel is
5753 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005754 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5755 || (job->jv_status == JOB_FINISHED
5756 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005757 return TRUE;
5758 return FALSE;
5759}
5760
Bram Moolenaar97792de2016-10-15 18:36:49 +02005761#define MAX_CHECK_ENDED 8
5762
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005763/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005764 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005765 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005766 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005767 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005768job_check_ended(void)
5769{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005770 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005771 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005772
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005773 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005774 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005775 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005776
Bram Moolenaar97792de2016-10-15 18:36:49 +02005777 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005778 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005779 // NOTE: mch_detect_ended_job() must only return a job of which the
5780 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005781 job_T *job = mch_detect_ended_job(first_job);
5782
5783 if (job == NULL)
5784 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005785 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005786 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005787 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005788
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005789 // Actually free jobs that were cleaned up.
5790 free_jobs_to_free_later();
5791
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005792 if (channel_need_redraw)
5793 {
5794 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005795 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005796 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005797 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005798}
5799
5800/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005801 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005802 * "argv_arg" is only for Unix.
5803 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005804 * The returned job has a refcount of one.
5805 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005806 */
5807 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005808job_start(
5809 typval_T *argvars,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02005810 char **argv_arg UNUSED,
Bram Moolenaar493359e2018-06-12 20:25:52 +02005811 jobopt_T *opt_arg,
5812 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005813{
5814 job_T *job;
5815 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005816 char **argv = NULL;
5817 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005818#if defined(UNIX)
5819# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005820 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005821#else
5822 garray_T ga;
5823#endif
5824 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005825 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005826
5827 job = job_alloc();
5828 if (job == NULL)
5829 return NULL;
5830
5831 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005832#ifndef USE_ARGV
5833 ga_init2(&ga, (int)sizeof(char*), 20);
5834#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005835
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005836 if (opt_arg != NULL)
5837 opt = *opt_arg;
5838 else
5839 {
5840 /* Default mode is NL. */
5841 clear_job_options(&opt);
5842 opt.jo_mode = MODE_NL;
5843 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005844 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5845 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5846 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005847 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005848 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005849
5850 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005851 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005852 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5853 && opt.jo_io[part] == JIO_FILE
5854 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5855 || *opt.jo_io_name[part] == NUL))
5856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005857 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005858 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005859 }
5860
5861 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5862 {
5863 buf_T *buf = NULL;
5864
5865 /* check that we can find the buffer before starting the job */
5866 if (opt.jo_set & JO_IN_BUF)
5867 {
5868 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5869 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005870 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005871 }
5872 else if (!(opt.jo_set & JO_IN_NAME))
5873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005874 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005875 }
5876 else
5877 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5878 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005879 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005880 if (buf->b_ml.ml_mfp == NULL)
5881 {
5882 char_u numbuf[NUMBUFLEN];
5883 char_u *s;
5884
5885 if (opt.jo_set & JO_IN_BUF)
5886 {
5887 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5888 s = numbuf;
5889 }
5890 else
5891 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005892 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005893 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005894 }
5895 job->jv_in_buf = buf;
5896 }
5897
5898 job_set_options(job, &opt);
5899
Bram Moolenaar13568252018-03-16 20:46:58 +01005900#ifdef USE_ARGV
5901 if (argv_arg != NULL)
5902 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005903 /* Make a copy of argv_arg for job->jv_argv. */
5904 for (i = 0; argv_arg[i] != NULL; i++)
5905 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005906 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005907 if (argv == NULL)
5908 goto theend;
5909 for (i = 0; i < argc; i++)
5910 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5911 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005912 }
5913 else
5914#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005915 if (argvars[0].v_type == VAR_STRING)
5916 {
5917 /* Command is a string. */
5918 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005919 if (cmd == NULL || *cmd == NUL)
5920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005921 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005922 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005923 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005924
5925 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005926 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005927 }
5928 else if (argvars[0].v_type != VAR_LIST
5929 || argvars[0].vval.v_list == NULL
5930 || argvars[0].vval.v_list->lv_len < 1)
5931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005932 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005933 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005934 }
5935 else
5936 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005937 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005938
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005939 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005940 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005941#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005942 if (win32_build_cmd(l, &ga) == FAIL)
5943 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005944 cmd = ga.ga_data;
5945#endif
5946 }
5947
Bram Moolenaar20608922018-04-21 22:30:08 +02005948 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005949 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005950
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005951#ifdef USE_ARGV
5952 if (ch_log_active())
5953 {
5954 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005955
5956 ga_init2(&ga, (int)sizeof(char), 200);
5957 for (i = 0; i < argc; ++i)
5958 {
5959 if (i > 0)
5960 ga_concat(&ga, (char_u *)" ");
5961 ga_concat(&ga, (char_u *)argv[i]);
5962 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005963 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005964 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005965 ga_clear(&ga);
5966 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005967 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005968#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005969 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005970 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005971#endif
5972
5973 /* If the channel is reading from a buffer, write lines now. */
5974 if (job->jv_channel != NULL)
5975 channel_write_in(job->jv_channel);
5976
5977theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005978#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005979 vim_free(ga.ga_data);
5980#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005981 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005982 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005983 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005984 return job;
5985}
5986
5987/*
5988 * Get the status of "job" and invoke the exit callback when needed.
5989 * The returned string is not allocated.
5990 */
5991 char *
5992job_status(job_T *job)
5993{
5994 char *result;
5995
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005996 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005997 /* No need to check, dead is dead. */
5998 result = "dead";
5999 else if (job->jv_status == JOB_FAILED)
6000 result = "fail";
6001 else
6002 {
6003 result = mch_job_status(job);
6004 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02006005 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006006 }
6007 return result;
6008}
6009
Bram Moolenaar8950a562016-03-12 15:22:55 +01006010/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006011 * Send a signal to "job". Implements job_stop().
6012 * When "type" is not NULL use this for the type.
6013 * Otherwise use argvars[1] for the type.
6014 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006015 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006016job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006017{
6018 char_u *arg;
6019
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006020 if (type != NULL)
6021 arg = (char_u *)type;
6022 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006023 arg = (char_u *)"";
6024 else
6025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006026 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006027 if (arg == NULL)
6028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006029 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006030 return 0;
6031 }
6032 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006033 if (job->jv_status == JOB_FAILED)
6034 {
6035 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6036 return 0;
6037 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006038 if (job->jv_status == JOB_ENDED)
6039 {
6040 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6041 return 0;
6042 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006043 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006044 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006045 return 0;
6046
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006047 /* Assume that only "kill" will kill the job. */
6048 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006049 job->jv_channel->ch_job_killed = TRUE;
6050
6051 /* We don't try freeing the job, obviously the caller still has a
6052 * reference to it. */
6053 return 1;
6054}
6055
Bram Moolenaarf2732452018-06-03 14:47:35 +02006056 void
6057invoke_prompt_callback(void)
6058{
6059 typval_T rettv;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006060 typval_T argv[2];
6061 char_u *text;
6062 char_u *prompt;
6063 linenr_T lnum = curbuf->b_ml.ml_line_count;
6064
6065 // Add a new line for the prompt before invoking the callback, so that
6066 // text can always be inserted above the last line.
6067 ml_append(lnum, (char_u *)"", 0, FALSE);
6068 curwin->w_cursor.lnum = lnum + 1;
6069 curwin->w_cursor.col = 0;
6070
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006071 if (curbuf->b_prompt_callback.cb_name == NULL
6072 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006073 return;
6074 text = ml_get(lnum);
6075 prompt = prompt_text();
6076 if (STRLEN(text) >= STRLEN(prompt))
6077 text += STRLEN(prompt);
6078 argv[0].v_type = VAR_STRING;
6079 argv[0].vval.v_string = vim_strsave(text);
6080 argv[1].v_type = VAR_UNKNOWN;
6081
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006082 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006083 clear_tv(&argv[0]);
6084 clear_tv(&rettv);
6085}
6086
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006087/*
6088 * Return TRUE when the interrupt callback was invoked.
6089 */
6090 int
6091invoke_prompt_interrupt(void)
6092{
6093 typval_T rettv;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006094 typval_T argv[1];
6095
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006096 if (curbuf->b_prompt_interrupt.cb_name == NULL
6097 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006098 return FALSE;
6099 argv[0].v_type = VAR_UNKNOWN;
6100
6101 got_int = FALSE; // don't skip executing commands
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006102 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006103 clear_tv(&rettv);
6104 return TRUE;
6105}
6106
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006107/*
6108 * "prompt_setcallback({buffer}, {callback})" function
6109 */
6110 void
6111f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6112{
6113 buf_T *buf;
6114 callback_T callback;
6115
6116 if (check_secure())
6117 return;
6118 buf = tv_get_buf(&argvars[0], FALSE);
6119 if (buf == NULL)
6120 return;
6121
6122 callback = get_callback(&argvars[1]);
6123 if (callback.cb_name == NULL)
6124 return;
6125
6126 free_callback(&buf->b_prompt_callback);
6127 set_callback(&buf->b_prompt_callback, &callback);
6128}
6129
6130/*
6131 * "prompt_setinterrupt({buffer}, {callback})" function
6132 */
6133 void
6134f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6135{
6136 buf_T *buf;
6137 callback_T callback;
6138
6139 if (check_secure())
6140 return;
6141 buf = tv_get_buf(&argvars[0], FALSE);
6142 if (buf == NULL)
6143 return;
6144
6145 callback = get_callback(&argvars[1]);
6146 if (callback.cb_name == NULL)
6147 return;
6148
6149 free_callback(&buf->b_prompt_interrupt);
6150 set_callback(&buf->b_prompt_interrupt, &callback);
6151}
6152
6153/*
6154 * "prompt_setprompt({buffer}, {text})" function
6155 */
6156 void
6157f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6158{
6159 buf_T *buf;
6160 char_u *text;
6161
6162 if (check_secure())
6163 return;
6164 buf = tv_get_buf(&argvars[0], FALSE);
6165 if (buf == NULL)
6166 return;
6167
6168 text = tv_get_string(&argvars[1]);
6169 vim_free(buf->b_prompt_text);
6170 buf->b_prompt_text = vim_strsave(text);
6171}
6172
6173/*
6174 * "ch_canread()" function
6175 */
6176 void
6177f_ch_canread(typval_T *argvars, typval_T *rettv)
6178{
6179 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6180
6181 rettv->vval.v_number = 0;
6182 if (channel != NULL)
6183 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6184 || channel_has_readahead(channel, PART_OUT)
6185 || channel_has_readahead(channel, PART_ERR);
6186}
6187
6188/*
6189 * "ch_close()" function
6190 */
6191 void
6192f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6193{
6194 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6195
6196 if (channel != NULL)
6197 {
6198 channel_close(channel, FALSE);
6199 channel_clear(channel);
6200 }
6201}
6202
6203/*
6204 * "ch_close()" function
6205 */
6206 void
6207f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6208{
6209 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6210
6211 if (channel != NULL)
6212 channel_close_in(channel);
6213}
6214
6215/*
6216 * "ch_getbufnr()" function
6217 */
6218 void
6219f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6220{
6221 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6222
6223 rettv->vval.v_number = -1;
6224 if (channel != NULL)
6225 {
6226 char_u *what = tv_get_string(&argvars[1]);
6227 int part;
6228
6229 if (STRCMP(what, "err") == 0)
6230 part = PART_ERR;
6231 else if (STRCMP(what, "out") == 0)
6232 part = PART_OUT;
6233 else if (STRCMP(what, "in") == 0)
6234 part = PART_IN;
6235 else
6236 part = PART_SOCK;
6237 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6238 rettv->vval.v_number =
6239 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6240 }
6241}
6242
6243/*
6244 * "ch_getjob()" function
6245 */
6246 void
6247f_ch_getjob(typval_T *argvars, typval_T *rettv)
6248{
6249 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6250
6251 if (channel != NULL)
6252 {
6253 rettv->v_type = VAR_JOB;
6254 rettv->vval.v_job = channel->ch_job;
6255 if (channel->ch_job != NULL)
6256 ++channel->ch_job->jv_refcount;
6257 }
6258}
6259
6260/*
6261 * "ch_info()" function
6262 */
6263 void
6264f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6265{
6266 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6267
6268 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6269 channel_info(channel, rettv->vval.v_dict);
6270}
6271
6272/*
6273 * "ch_log()" function
6274 */
6275 void
6276f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6277{
6278 char_u *msg = tv_get_string(&argvars[0]);
6279 channel_T *channel = NULL;
6280
6281 if (argvars[1].v_type != VAR_UNKNOWN)
6282 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6283
6284 ch_log(channel, "%s", msg);
6285}
6286
6287/*
6288 * "ch_logfile()" function
6289 */
6290 void
6291f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6292{
6293 char_u *fname;
6294 char_u *opt = (char_u *)"";
6295 char_u buf[NUMBUFLEN];
6296
6297 /* Don't open a file in restricted mode. */
6298 if (check_restricted() || check_secure())
6299 return;
6300 fname = tv_get_string(&argvars[0]);
6301 if (argvars[1].v_type == VAR_STRING)
6302 opt = tv_get_string_buf(&argvars[1], buf);
6303 ch_logfile(fname, opt);
6304}
6305
6306/*
6307 * "ch_open()" function
6308 */
6309 void
6310f_ch_open(typval_T *argvars, typval_T *rettv)
6311{
6312 rettv->v_type = VAR_CHANNEL;
6313 if (check_restricted() || check_secure())
6314 return;
6315 rettv->vval.v_channel = channel_open_func(argvars);
6316}
6317
6318/*
6319 * "ch_read()" function
6320 */
6321 void
6322f_ch_read(typval_T *argvars, typval_T *rettv)
6323{
6324 common_channel_read(argvars, rettv, FALSE, FALSE);
6325}
6326
6327/*
6328 * "ch_readblob()" function
6329 */
6330 void
6331f_ch_readblob(typval_T *argvars, typval_T *rettv)
6332{
6333 common_channel_read(argvars, rettv, TRUE, TRUE);
6334}
6335
6336/*
6337 * "ch_readraw()" function
6338 */
6339 void
6340f_ch_readraw(typval_T *argvars, typval_T *rettv)
6341{
6342 common_channel_read(argvars, rettv, TRUE, FALSE);
6343}
6344
6345/*
6346 * "ch_evalexpr()" function
6347 */
6348 void
6349f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6350{
6351 ch_expr_common(argvars, rettv, TRUE);
6352}
6353
6354/*
6355 * "ch_sendexpr()" function
6356 */
6357 void
6358f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6359{
6360 ch_expr_common(argvars, rettv, FALSE);
6361}
6362
6363/*
6364 * "ch_evalraw()" function
6365 */
6366 void
6367f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6368{
6369 ch_raw_common(argvars, rettv, TRUE);
6370}
6371
6372/*
6373 * "ch_sendraw()" function
6374 */
6375 void
6376f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6377{
6378 ch_raw_common(argvars, rettv, FALSE);
6379}
6380
6381/*
6382 * "ch_setoptions()" function
6383 */
6384 void
6385f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6386{
6387 channel_T *channel;
6388 jobopt_T opt;
6389
6390 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6391 if (channel == NULL)
6392 return;
6393 clear_job_options(&opt);
6394 if (get_job_options(&argvars[1], &opt,
6395 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6396 channel_set_options(channel, &opt);
6397 free_job_options(&opt);
6398}
6399
6400/*
6401 * "ch_status()" function
6402 */
6403 void
6404f_ch_status(typval_T *argvars, typval_T *rettv)
6405{
6406 channel_T *channel;
6407 jobopt_T opt;
6408 int part = -1;
6409
6410 /* return an empty string by default */
6411 rettv->v_type = VAR_STRING;
6412 rettv->vval.v_string = NULL;
6413
6414 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6415
6416 if (argvars[1].v_type != VAR_UNKNOWN)
6417 {
6418 clear_job_options(&opt);
6419 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6420 && (opt.jo_set & JO_PART))
6421 part = opt.jo_part;
6422 }
6423
6424 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6425}
6426
6427/*
6428 * Get the job from the argument.
6429 * Returns NULL if the job is invalid.
6430 */
6431 static job_T *
6432get_job_arg(typval_T *tv)
6433{
6434 job_T *job;
6435
6436 if (tv->v_type != VAR_JOB)
6437 {
6438 semsg(_(e_invarg2), tv_get_string(tv));
6439 return NULL;
6440 }
6441 job = tv->vval.v_job;
6442
6443 if (job == NULL)
6444 emsg(_("E916: not a valid job"));
6445 return job;
6446}
6447
6448/*
6449 * "job_getchannel()" function
6450 */
6451 void
6452f_job_getchannel(typval_T *argvars, typval_T *rettv)
6453{
6454 job_T *job = get_job_arg(&argvars[0]);
6455
6456 if (job != NULL)
6457 {
6458 rettv->v_type = VAR_CHANNEL;
6459 rettv->vval.v_channel = job->jv_channel;
6460 if (job->jv_channel != NULL)
6461 ++job->jv_channel->ch_refcount;
6462 }
6463}
6464
6465/*
6466 * Implementation of job_info().
6467 */
6468 static void
6469job_info(job_T *job, dict_T *dict)
6470{
6471 dictitem_T *item;
6472 varnumber_T nr;
6473 list_T *l;
6474 int i;
6475
6476 dict_add_string(dict, "status", (char_u *)job_status(job));
6477
6478 item = dictitem_alloc((char_u *)"channel");
6479 if (item == NULL)
6480 return;
6481 item->di_tv.v_type = VAR_CHANNEL;
6482 item->di_tv.vval.v_channel = job->jv_channel;
6483 if (job->jv_channel != NULL)
6484 ++job->jv_channel->ch_refcount;
6485 if (dict_add(dict, item) == FAIL)
6486 dictitem_free(item);
6487
6488#ifdef UNIX
6489 nr = job->jv_pid;
6490#else
6491 nr = job->jv_proc_info.dwProcessId;
6492#endif
6493 dict_add_number(dict, "process", nr);
6494 dict_add_string(dict, "tty_in", job->jv_tty_in);
6495 dict_add_string(dict, "tty_out", job->jv_tty_out);
6496
6497 dict_add_number(dict, "exitval", job->jv_exitval);
6498 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6499 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6500#ifdef UNIX
6501 dict_add_string(dict, "termsig", job->jv_termsig);
6502#endif
6503#ifdef MSWIN
6504 dict_add_string(dict, "tty_type", job->jv_tty_type);
6505#endif
6506
6507 l = list_alloc();
6508 if (l != NULL)
6509 {
6510 dict_add_list(dict, "cmd", l);
6511 if (job->jv_argv != NULL)
6512 for (i = 0; job->jv_argv[i] != NULL; i++)
6513 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6514 }
6515}
6516
6517/*
6518 * Implementation of job_info() to return info for all jobs.
6519 */
6520 static void
6521job_info_all(list_T *l)
6522{
6523 job_T *job;
6524 typval_T tv;
6525
6526 for (job = first_job; job != NULL; job = job->jv_next)
6527 {
6528 tv.v_type = VAR_JOB;
6529 tv.vval.v_job = job;
6530
6531 if (list_append_tv(l, &tv) != OK)
6532 return;
6533 }
6534}
6535
6536/*
6537 * "job_info()" function
6538 */
6539 void
6540f_job_info(typval_T *argvars, typval_T *rettv)
6541{
6542 if (argvars[0].v_type != VAR_UNKNOWN)
6543 {
6544 job_T *job = get_job_arg(&argvars[0]);
6545
6546 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6547 job_info(job, rettv->vval.v_dict);
6548 }
6549 else if (rettv_list_alloc(rettv) == OK)
6550 job_info_all(rettv->vval.v_list);
6551}
6552
6553/*
6554 * "job_setoptions()" function
6555 */
6556 void
6557f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6558{
6559 job_T *job = get_job_arg(&argvars[0]);
6560 jobopt_T opt;
6561
6562 if (job == NULL)
6563 return;
6564 clear_job_options(&opt);
6565 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6566 job_set_options(job, &opt);
6567 free_job_options(&opt);
6568}
6569
6570/*
6571 * "job_start()" function
6572 */
6573 void
6574f_job_start(typval_T *argvars, typval_T *rettv)
6575{
6576 rettv->v_type = VAR_JOB;
6577 if (check_restricted() || check_secure())
6578 return;
6579 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6580}
6581
6582/*
6583 * "job_status()" function
6584 */
6585 void
6586f_job_status(typval_T *argvars, typval_T *rettv)
6587{
6588 job_T *job = get_job_arg(&argvars[0]);
6589
6590 if (job != NULL)
6591 {
6592 rettv->v_type = VAR_STRING;
6593 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6594 }
6595}
6596
6597/*
6598 * "job_stop()" function
6599 */
6600 void
6601f_job_stop(typval_T *argvars, typval_T *rettv)
6602{
6603 job_T *job = get_job_arg(&argvars[0]);
6604
6605 if (job != NULL)
6606 rettv->vval.v_number = job_stop(job, argvars, NULL);
6607}
6608
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006609#endif /* FEAT_JOB_CHANNEL */