blob: a023c9fd7706cdb3bb0e7cc1938aa5bb1c5fce7c [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 +020058# if defined(MSWIN) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
59static channel_T *channel_fd2channel(sock_T fd, ch_part_T *partp);
60# endif
61static ch_mode_T channel_get_mode(channel_T *channel, ch_part_T part);
62static int channel_get_timeout(channel_T *channel, ch_part_T part);
63static ch_part_T channel_part_send(channel_T *channel);
64static ch_part_T channel_part_read(channel_T *channel);
65static void free_job_options(jobopt_T *opt);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020066
Bram Moolenaar187db502016-02-27 14:44:26 +010067/* Whether a redraw is needed for appending a line to a buffer. */
68static int channel_need_redraw = FALSE;
69
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020070/* Whether we are inside channel_parse_messages() or another situation where it
71 * is safe to invoke callbacks. */
72static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010073
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020074static char *part_names[] = {"sock", "out", "err", "in"};
75
Bram Moolenaar4f974752019-02-17 17:44:42 +010076#ifdef MSWIN
Bram Moolenaard8070362016-02-15 21:56:54 +010077 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010078fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010079{
80 HANDLE h = (HANDLE)fd;
81 DWORD nread;
82
83 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
84 return -1;
85 return (int)nread;
86}
87
88 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010089fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010090{
Bram Moolenaar24058382019-01-24 23:11:49 +010091 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010092 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010093 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010094 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010095
Bram Moolenaar24058382019-01-24 23:11:49 +010096 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010097 {
Bram Moolenaar24058382019-01-24 23:11:49 +010098 if (todo > MAX_NAMED_PIPE_SIZE)
99 size = MAX_NAMED_PIPE_SIZE;
100 else
Bram Moolenaardec01202019-01-28 20:04:24 +0100101 size = (DWORD)todo;
Bram Moolenaar65240682019-02-10 22:23:26 +0100102 // If the pipe overflows while the job does not read the data,
103 // WriteFile() will block forever. This abandons the write.
Bram Moolenaar24058382019-01-24 23:11:49 +0100104 memset(&ov, 0, sizeof(ov));
Bram Moolenaar65240682019-02-10 22:23:26 +0100105 nwrite = 0;
Bram Moolenaar24058382019-01-24 23:11:49 +0100106 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
107 {
108 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100109
Bram Moolenaar24058382019-01-24 23:11:49 +0100110 if (err != ERROR_IO_PENDING)
111 return -1;
112 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
113 return -1;
114 FlushFileBuffers(h);
115 }
Bram Moolenaar65240682019-02-10 22:23:26 +0100116 else if (nwrite == 0)
117 // WriteFile() returns TRUE but did not write anything. This causes
118 // a hang, so bail out.
119 break;
Bram Moolenaar24058382019-01-24 23:11:49 +0100120 todo -= nwrite;
121 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100122 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100123 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100124}
125
126 static void
127fd_close(sock_T fd)
128{
129 HANDLE h = (HANDLE)fd;
130
131 CloseHandle(h);
132}
133#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100134
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135/* Log file opened with ch_logfile(). */
136static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100137#ifdef FEAT_RELTIME
138static proftime_T log_start;
139#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100140
141 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100142ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100143{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100144 FILE *file = NULL;
145
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100146 if (log_fd != NULL)
147 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100148
149 if (*fname != NUL)
150 {
151 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
152 if (file == NULL)
153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100154 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100155 return;
156 }
157 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100158 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100159
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100160 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163#ifdef FEAT_RELTIME
164 profile_start(&log_start);
165#endif
166 }
167}
168
169 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200170ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100171{
172 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173}
174
175 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200176ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177{
178 if (log_fd != NULL)
179 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100180#ifdef FEAT_RELTIME
181 proftime_T log_now;
182
183 profile_start(&log_now);
184 profile_sub(&log_now, &log_start);
185 fprintf(log_fd, "%s ", profile_msg(&log_now));
186#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100187 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200188 {
189 if (part < PART_COUNT)
190 fprintf(log_fd, "%son %d(%s): ",
191 what, ch->ch_id, part_names[part]);
192 else
193 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
194 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 else
196 fprintf(log_fd, "%s: ", what);
197 }
198}
199
Bram Moolenaard0b65022016-03-06 21:50:33 +0100200static int did_log_msg = TRUE;
201
Bram Moolenaar8caa43d2019-02-20 22:45:06 +0100202#ifndef PROTO // prototype is in proto.h
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100203 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200204ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100205{
206 if (log_fd != NULL)
207 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200208 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100209
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200210 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200211 va_start(ap, fmt);
212 vfprintf(log_fd, fmt, ap);
213 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100214 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100216 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100217 }
218}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200219#endif
220
221 static void
222ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200223#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
224 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200225#endif
226 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227
228 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200229ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100230{
231 if (log_fd != NULL)
232 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200233 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200235 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200236 va_start(ap, fmt);
237 vfprintf(log_fd, fmt, ap);
238 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100239 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100241 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 }
243}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100244
Bram Moolenaar4f974752019-02-17 17:44:42 +0100245#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100246# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100247# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100248
249 static char *
250strerror_win32(int eno)
251{
252 static LPVOID msgbuf = NULL;
253 char_u *ptr;
254
255 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200256 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100257 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200258 msgbuf = NULL;
259 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100260 FormatMessage(
261 FORMAT_MESSAGE_ALLOCATE_BUFFER |
262 FORMAT_MESSAGE_FROM_SYSTEM |
263 FORMAT_MESSAGE_IGNORE_INSERTS,
264 NULL,
265 eno,
266 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
267 (LPTSTR) &msgbuf,
268 0,
269 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200270 if (msgbuf != NULL)
271 /* chomp \r or \n */
272 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
273 switch (*ptr)
274 {
275 case '\r':
276 STRMOVE(ptr, ptr + 1);
277 ptr--;
278 break;
279 case '\n':
280 if (*(ptr + 1) == '\0')
281 *ptr = '\0';
282 else
283 *ptr = ' ';
284 break;
285 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100286 return msgbuf;
287}
288#endif
289
Bram Moolenaar77073442016-02-13 23:23:53 +0100290/*
291 * The list of all allocated channels.
292 */
293static channel_T *first_channel = NULL;
294static int next_ch_id = 0;
295
296/*
297 * Allocate a new channel. The refcount is set to 1.
298 * The channel isn't actually used until it is opened.
299 * Returns NULL if out of memory.
300 */
301 channel_T *
302add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100303{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200304 ch_part_T part;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200305 channel_T *channel = ALLOC_CLEAR_ONE(channel_T);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100306
Bram Moolenaar77073442016-02-13 23:23:53 +0100307 if (channel == NULL)
308 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100309
Bram Moolenaar77073442016-02-13 23:23:53 +0100310 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100311 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100312
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200313 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100314 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100315 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100316#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100318#endif
319#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100320 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100321#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100322 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100323 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100324
Bram Moolenaar77073442016-02-13 23:23:53 +0100325 if (first_channel != NULL)
326 {
327 first_channel->ch_prev = channel;
328 channel->ch_next = first_channel;
329 }
330 first_channel = channel;
331
332 channel->ch_refcount = 1;
333 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100334}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100335
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200336 int
337has_any_channel(void)
338{
339 return first_channel != NULL;
340}
341
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100342/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100343 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100344 * Return TRUE if "channel" has a callback and the associated job wasn't
345 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100346 */
347 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100348channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100349{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100350 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100351 int has_out_msg;
352 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100353
354 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100355 if (channel->ch_job_killed && channel->ch_job == NULL)
356 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100357
358 /* If there is a close callback it may still need to be invoked. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200359 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100360 return TRUE;
361
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200362 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200363 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200364 return TRUE;
365
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 /* If there is no callback then nobody can get readahead. If the fd is
367 * closed and there is no readahead then the callback won't be called. */
368 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100369 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
370 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100371 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
372 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
373 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
374 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
375 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
376 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200377 return (channel->ch_callback.cb_name != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100378 || has_out_msg || has_err_msg))
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200379 || ((channel->ch_part[PART_OUT].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200380 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
381 && has_out_msg)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200382 || ((channel->ch_part[PART_ERR].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200383 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
384 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100385}
386
387/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200388 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
389 */
390 static int
391channel_can_close(channel_T *channel)
392{
393 return channel->ch_to_be_closed == 0;
394}
395
396/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200397 * Close a channel and free all its resources.
398 */
399 static void
400channel_free_contents(channel_T *channel)
401{
402 channel_close(channel, TRUE);
403 channel_clear(channel);
404 ch_log(channel, "Freeing channel");
405}
406
407 static void
408channel_free_channel(channel_T *channel)
409{
410 if (channel->ch_next != NULL)
411 channel->ch_next->ch_prev = channel->ch_prev;
412 if (channel->ch_prev == NULL)
413 first_channel = channel->ch_next;
414 else
415 channel->ch_prev->ch_next = channel->ch_next;
416 vim_free(channel);
417}
418
419 static void
420channel_free(channel_T *channel)
421{
422 if (!in_free_unref_items)
423 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200424 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200426 else
427 {
428 channel_free_contents(channel);
429 channel_free_channel(channel);
430 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200431 }
432}
433
434/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100435 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100436 * possible, there is no callback to be invoked or the associated job was
437 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100438 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100439 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100440 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100441channel_may_free(channel_T *channel)
442{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100443 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100444 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100445 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100446 return TRUE;
447 }
448 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100449}
450
451/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100452 * Decrement the reference count on "channel" and maybe free it when it goes
453 * down to zero. Don't free it if there is a pending action.
454 * Returns TRUE when the channel is no longer referenced.
455 */
456 int
457channel_unref(channel_T *channel)
458{
459 if (channel != NULL && --channel->ch_refcount <= 0)
460 return channel_may_free(channel);
461 return FALSE;
462}
463
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 int
465free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100466{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467 int did_free = FALSE;
468 channel_T *ch;
469
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200470 /* This is invoked from the garbage collector, which only runs at a safe
471 * point. */
472 ++safe_to_invoke_callback;
473
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200474 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200475 if (!channel_still_useful(ch)
476 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200477 {
478 /* Free the channel and ordinary items it contains, but don't
479 * recurse into Lists, Dictionaries etc. */
480 channel_free_contents(ch);
481 did_free = TRUE;
482 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200483
484 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200485 return did_free;
486}
487
488 void
489free_unused_channels(int copyID, int mask)
490{
491 channel_T *ch;
492 channel_T *ch_next;
493
494 for (ch = first_channel; ch != NULL; ch = ch_next)
495 {
496 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200497 if (!channel_still_useful(ch)
498 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200499 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200500 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200501 channel_free_channel(ch);
502 }
503 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100504}
505
Bram Moolenaard04a0202016-01-26 23:30:18 +0100506#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100507
508#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
509 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100510channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100511{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100512 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200513 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100514
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100515 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100516 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200517 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100518 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200519 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100520}
521#endif
522
Bram Moolenaare0874f82016-01-24 20:36:41 +0100523/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100524 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100525 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100526#ifdef FEAT_GUI_X11
527 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200528messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200529 int *unused1 UNUSED,
530 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100531{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100532 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100533}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100534#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100535
Bram Moolenaard04a0202016-01-26 23:30:18 +0100536#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100537# if GTK_CHECK_VERSION(3,0,0)
538 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200539messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200540 GIOCondition unused2 UNUSED,
541 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100542{
543 channel_read_fd(GPOINTER_TO_INT(clientData));
544 return TRUE; /* Return FALSE instead in case the event source is to
545 * be removed after this function returns. */
546}
547# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100548 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200549messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200550 gint unused1 UNUSED,
551 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100552{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100553 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100554}
Bram Moolenaar98921892016-02-23 17:14:37 +0100555# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100556#endif
557
558 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200559channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560{
Bram Moolenaarde279892016-03-11 22:19:44 +0100561 if (!CH_HAS_GUI)
562 return;
563
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200564 /* gets stuck in handling events for a not connected channel */
565 if (channel->ch_keep_open)
566 return;
567
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100568# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200569 /* Tell notifier we are interested in being called when there is input on
570 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100571 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200572 {
573 ch_log(channel, "Registering part %s with fd %d",
574 part_names[part], channel->ch_part[part].ch_fd);
575
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100576 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100577 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100578 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100579 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200580 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100581 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200582 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100583# else
584# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200585 /* Tell gdk we are interested in being called when there is input on the
586 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100587 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100588 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200589 ch_log(channel, "Registering part %s with fd %d",
590 part_names[part], channel->ch_part[part].ch_fd);
591# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100592 GIOChannel *chnnl = g_io_channel_unix_new(
593 (gint)channel->ch_part[part].ch_fd);
594
595 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
596 chnnl,
597 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200598 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100599 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
600
601 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100602# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100603 channel->ch_part[part].ch_inputHandler = gdk_input_add(
604 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100605 (GdkInputCondition)
606 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200607 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100608 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100609# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200610 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100611# endif
612# endif
613}
614
Bram Moolenaarde279892016-03-11 22:19:44 +0100615 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100616channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100617{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 if (channel->CH_SOCK_FD != INVALID_FD)
619 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200620 if (channel->CH_OUT_FD != INVALID_FD
621 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100622 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200623 if (channel->CH_ERR_FD != INVALID_FD
624 && channel->CH_ERR_FD != channel->CH_SOCK_FD
625 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100626 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100627}
628
629/*
630 * Register any of our file descriptors with the GUI event handling system.
631 * Called when the GUI has started.
632 */
633 void
634channel_gui_register_all(void)
635{
Bram Moolenaar77073442016-02-13 23:23:53 +0100636 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100637
Bram Moolenaar77073442016-02-13 23:23:53 +0100638 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100639 channel_gui_register(channel);
640}
641
642 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200643channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100644{
645# ifdef FEAT_GUI_X11
646 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
647 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200648 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100649 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
650 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
651 }
652# else
653# ifdef FEAT_GUI_GTK
654 if (channel->ch_part[part].ch_inputHandler != 0)
655 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200656 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100657# if GTK_CHECK_VERSION(3,0,0)
658 g_source_remove(channel->ch_part[part].ch_inputHandler);
659# else
660 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
661# endif
662 channel->ch_part[part].ch_inputHandler = 0;
663 }
664# endif
665# endif
666}
667
668 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100669channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100670{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200671 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100672
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100673 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100674 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100675}
676
677#endif
678
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100679static char *e_cannot_connect = N_("E902: Cannot connect to port");
680
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100682 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100683 * "waittime" is the time in msec to wait for the connection.
684 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100685 * Returns the channel for success.
686 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100687 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100689channel_open(
690 char *hostname,
691 int port_in,
692 int waittime,
693 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100694{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100695 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100697 struct hostent *host;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100698#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100699 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100700 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100701#else
702 int port = port_in;
703#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100704 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100705 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100706
Bram Moolenaar4f974752019-02-17 17:44:42 +0100707#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100708 channel_init_winsock();
709#endif
710
Bram Moolenaar77073442016-02-13 23:23:53 +0100711 channel = add_channel();
712 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100713 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100714 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100715 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100716 }
717
718 /* Get the server internet address and put into addr structure */
719 /* fill in the socket address structure and connect to server */
720 vim_memset((char *)&server, 0, sizeof(server));
721 server.sin_family = AF_INET;
722 server.sin_port = htons(port);
723 if ((host = gethostbyname(hostname)) == NULL)
724 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100725 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200726 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100727 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100728 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100729 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100730 {
731 char *p;
732
Bram Moolenaar2e324952018-04-14 14:37:07 +0200733 /* When using host->h_addr_list[0] directly ubsan warns for it to not
734 * be aligned. First copy the pointer to avoid that. */
735 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100736 memcpy((char *)&server.sin_addr, p, host->h_length);
737 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100738
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100739 /* On Mac and Solaris a zero timeout almost never works. At least wait
740 * one millisecond. Let's do it for all systems, because we don't know why
741 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100742 if (waittime == 0)
743 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100744
745 /*
746 * For Unix we need to call connect() again after connect() failed.
747 * On Win32 one time is sufficient.
748 */
749 while (TRUE)
750 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100751 long elapsed_msec = 0;
752 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100753
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100754 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100755 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100756 sd = socket(AF_INET, SOCK_STREAM, 0);
757 if (sd == -1)
758 {
759 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200760 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100761 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100762 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100763 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100764
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100765 if (waittime >= 0)
766 {
767 /* Make connect() non-blocking. */
768 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +0100769#ifdef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 ioctlsocket(sd, FIONBIO, &val) < 0
771#else
772 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
773#endif
774 )
775 {
776 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200777 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100778 "channel_open: Connect failed with errno %d", errno);
779 sock_close(sd);
780 channel_free(channel);
781 return NULL;
782 }
783 }
784
785 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200786 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100787 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
788
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 if (ret == 0)
790 /* The connection could be established. */
791 break;
792
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100793 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100794 if (waittime < 0 || (errno != EWOULDBLOCK
795 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100796#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100797 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100798#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100799 ))
800 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200801 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100802 "channel_open: Connect failed with errno %d", errno);
803 PERROR(_(e_cannot_connect));
804 sock_close(sd);
805 channel_free(channel);
806 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100807 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100808
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100809 /* Limit the waittime to 50 msec. If it doesn't work within this
810 * time we close the socket and try creating it again. */
811 waitnow = waittime > 50 ? 50 : waittime;
812
Bram Moolenaar045a2842016-03-08 22:33:07 +0100813 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100814 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100815#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100816 if (errno != ECONNREFUSED)
817#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100818 {
819 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100820 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100821 fd_set wfds;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100822#ifndef MSWIN
Bram Moolenaard42119f2016-02-28 20:51:49 +0100823 int so_error = 0;
824 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100825 struct timeval start_tv;
826 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100827#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100828 FD_ZERO(&rfds);
829 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100830 FD_ZERO(&wfds);
831 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100832
Bram Moolenaar562ca712016-03-09 21:50:05 +0100833 tv.tv_sec = waitnow / 1000;
834 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100835#ifndef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100836 gettimeofday(&start_tv, NULL);
837#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200838 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100839 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100840 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100841
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100842 if (ret < 0)
843 {
844 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200845 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100846 "channel_open: Connect failed with errno %d", errno);
847 PERROR(_(e_cannot_connect));
848 sock_close(sd);
849 channel_free(channel);
850 return NULL;
851 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100852
Bram Moolenaar4f974752019-02-17 17:44:42 +0100853#ifdef MSWIN
Bram Moolenaar562ca712016-03-09 21:50:05 +0100854 /* On Win32: select() is expected to work and wait for up to
855 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100856 if (FD_ISSET(sd, &wfds))
857 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100858 elapsed_msec = waitnow;
859 if (waittime > 1 && elapsed_msec < waittime)
860 {
861 waittime -= elapsed_msec;
862 continue;
863 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100864#else
865 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100866 * After putting the socket in non-blocking mode, connect() will
867 * return EINPROGRESS, select() will not wait (as if writing is
868 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100869 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100870 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100871 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100872 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100873 {
874 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100876 if (ret < 0 || (so_error != 0
877 && so_error != EWOULDBLOCK
878 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100879# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100880 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100881# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100882 ))
883 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200884 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100885 "channel_open: Connect failed with errno %d",
886 so_error);
887 PERROR(_(e_cannot_connect));
888 sock_close(sd);
889 channel_free(channel);
890 return NULL;
891 }
892 }
893
Bram Moolenaar045a2842016-03-08 22:33:07 +0100894 if (FD_ISSET(sd, &wfds) && so_error == 0)
895 /* Did not detect an error, connection is established. */
896 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100897
Bram Moolenaar045a2842016-03-08 22:33:07 +0100898 gettimeofday(&end_tv, NULL);
899 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
900 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100901#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100902 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100903
Bram Moolenaar4f974752019-02-17 17:44:42 +0100904#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100905 if (waittime > 1 && elapsed_msec < waittime)
906 {
907 /* The port isn't ready but we also didn't get an error.
908 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100909 * yet. Select() may return early, wait until the remaining
910 * "waitnow" and try again. */
911 waitnow -= elapsed_msec;
912 waittime -= elapsed_msec;
913 if (waitnow > 0)
914 {
915 mch_delay((long)waitnow, TRUE);
916 ui_breakcheck();
917 waittime -= waitnow;
918 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100919 if (!got_int)
920 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100921 if (waittime <= 0)
922 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100923 waittime = 1;
924 continue;
925 }
926 /* we were interrupted, behave as if timed out */
927 }
928#endif
929
930 /* We timed out. */
931 ch_error(channel, "Connection timed out");
932 sock_close(sd);
933 channel_free(channel);
934 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100935 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100936
Bram Moolenaar045a2842016-03-08 22:33:07 +0100937 ch_log(channel, "Connection made");
938
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100939 if (waittime >= 0)
940 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100941#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100942 val = 0;
943 ioctlsocket(sd, FIONBIO, &val);
944#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100945 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100946#endif
947 }
948
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100949 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100950 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100951 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
952 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200953 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100954
955#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100956 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100957#endif
958
Bram Moolenaar77073442016-02-13 23:23:53 +0100959 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100960}
961
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100962/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +0200963 * Copy callback from "src" to "dest", incrementing the refcounts.
964 */
965 static void
966copy_callback(callback_T *dest, callback_T *src)
967{
968 dest->cb_partial = src->cb_partial;
969 if (dest->cb_partial != NULL)
970 {
971 dest->cb_name = src->cb_name;
972 dest->cb_free_name = FALSE;
973 ++dest->cb_partial->pt_refcount;
974 }
975 else
976 {
977 dest->cb_name = vim_strsave(src->cb_name);
978 dest->cb_free_name = TRUE;
979 func_ref(src->cb_name);
980 }
981}
982
983 static void
984free_set_callback(callback_T *cbp, callback_T *callback)
985{
986 free_callback(cbp);
987
988 if (callback->cb_name != NULL && *callback->cb_name != NUL)
989 copy_callback(cbp, callback);
990 else
991 cbp->cb_name = NULL;
992}
993
994/*
995 * Prepare buffer "buf" for writing channel output to.
996 */
997 static void
998prepare_buffer(buf_T *buf)
999{
1000 buf_T *save_curbuf = curbuf;
1001
1002 buf_copy_options(buf, BCO_ENTER);
1003 curbuf = buf;
1004#ifdef FEAT_QUICKFIX
1005 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1006 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1007#endif
1008 if (curbuf->b_ml.ml_mfp == NULL)
1009 ml_open(curbuf);
1010 curbuf = save_curbuf;
1011}
1012
1013/*
1014 * Find a buffer matching "name" or create a new one.
1015 * Returns NULL if there is something very wrong (error already reported).
1016 */
1017 static buf_T *
1018find_buffer(char_u *name, int err, int msg)
1019{
1020 buf_T *buf = NULL;
1021 buf_T *save_curbuf = curbuf;
1022
1023 if (name != NULL && *name != NUL)
1024 {
1025 buf = buflist_findname(name);
1026 if (buf == NULL)
1027 buf = buflist_findname_exp(name);
1028 }
1029 if (buf == NULL)
1030 {
1031 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
1032 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
1033 if (buf == NULL)
1034 return NULL;
1035 prepare_buffer(buf);
1036
1037 curbuf = buf;
1038 if (msg)
1039 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1040 : "Reading from channel output..."), TRUE);
1041 changed_bytes(1, 0);
1042 curbuf = save_curbuf;
1043 }
1044
1045 return buf;
1046}
1047
1048/*
1049 * Set various properties from an "opt" argument.
1050 */
1051 static void
1052channel_set_options(channel_T *channel, jobopt_T *opt)
1053{
1054 ch_part_T part;
1055
1056 if (opt->jo_set & JO_MODE)
1057 for (part = PART_SOCK; part < PART_COUNT; ++part)
1058 channel->ch_part[part].ch_mode = opt->jo_mode;
1059 if (opt->jo_set & JO_IN_MODE)
1060 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1061 if (opt->jo_set & JO_OUT_MODE)
1062 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1063 if (opt->jo_set & JO_ERR_MODE)
1064 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
1065 channel->ch_nonblock = opt->jo_noblock;
1066
1067 if (opt->jo_set & JO_TIMEOUT)
1068 for (part = PART_SOCK; part < PART_COUNT; ++part)
1069 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1070 if (opt->jo_set & JO_OUT_TIMEOUT)
1071 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1072 if (opt->jo_set & JO_ERR_TIMEOUT)
1073 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1074 if (opt->jo_set & JO_BLOCK_WRITE)
1075 channel->ch_part[PART_IN].ch_block_write = 1;
1076
1077 if (opt->jo_set & JO_CALLBACK)
1078 free_set_callback(&channel->ch_callback, &opt->jo_callback);
1079 if (opt->jo_set & JO_OUT_CALLBACK)
1080 free_set_callback(&channel->ch_part[PART_OUT].ch_callback,
1081 &opt->jo_out_cb);
1082 if (opt->jo_set & JO_ERR_CALLBACK)
1083 free_set_callback(&channel->ch_part[PART_ERR].ch_callback,
1084 &opt->jo_err_cb);
1085 if (opt->jo_set & JO_CLOSE_CALLBACK)
1086 free_set_callback(&channel->ch_close_cb, &opt->jo_close_cb);
1087 channel->ch_drop_never = opt->jo_drop_never;
1088
1089 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1090 {
1091 buf_T *buf;
1092
1093 /* writing output to a buffer. Default mode is NL. */
1094 if (!(opt->jo_set & JO_OUT_MODE))
1095 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
1096 if (opt->jo_set & JO_OUT_BUF)
1097 {
1098 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1099 if (buf == NULL)
1100 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1101 }
1102 else
1103 {
1104 int msg = TRUE;
1105
1106 if (opt->jo_set2 & JO2_OUT_MSG)
1107 msg = opt->jo_message[PART_OUT];
1108 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
1109 }
1110 if (buf != NULL)
1111 {
1112 if (opt->jo_set & JO_OUT_MODIFIABLE)
1113 channel->ch_part[PART_OUT].ch_nomodifiable =
1114 !opt->jo_modifiable[PART_OUT];
1115
1116 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1117 {
1118 emsg(_(e_modifiable));
1119 }
1120 else
1121 {
1122 ch_log(channel, "writing out to buffer '%s'",
1123 (char *)buf->b_ffname);
1124 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1125 // if the buffer was deleted or unloaded resurrect it
1126 if (buf->b_ml.ml_mfp == NULL)
1127 prepare_buffer(buf);
1128 }
1129 }
1130 }
1131
1132 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1133 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1134 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1135 {
1136 buf_T *buf;
1137
1138 /* writing err to a buffer. Default mode is NL. */
1139 if (!(opt->jo_set & JO_ERR_MODE))
1140 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1141 if (opt->jo_io[PART_ERR] == JIO_OUT)
1142 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1143 else if (opt->jo_set & JO_ERR_BUF)
1144 {
1145 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1146 if (buf == NULL)
1147 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1148 }
1149 else
1150 {
1151 int msg = TRUE;
1152
1153 if (opt->jo_set2 & JO2_ERR_MSG)
1154 msg = opt->jo_message[PART_ERR];
1155 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1156 }
1157 if (buf != NULL)
1158 {
1159 if (opt->jo_set & JO_ERR_MODIFIABLE)
1160 channel->ch_part[PART_ERR].ch_nomodifiable =
1161 !opt->jo_modifiable[PART_ERR];
1162 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1163 {
1164 emsg(_(e_modifiable));
1165 }
1166 else
1167 {
1168 ch_log(channel, "writing err to buffer '%s'",
1169 (char *)buf->b_ffname);
1170 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1171 // if the buffer was deleted or unloaded resurrect it
1172 if (buf->b_ml.ml_mfp == NULL)
1173 prepare_buffer(buf);
1174 }
1175 }
1176 }
1177
1178 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1179 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1180 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
1181}
1182
1183/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001184 * Implements ch_open().
1185 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001186 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001187channel_open_func(typval_T *argvars)
1188{
1189 char_u *address;
1190 char_u *p;
1191 char *rest;
1192 int port;
1193 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001194 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001195
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001196 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001197 if (argvars[1].v_type != VAR_UNKNOWN
1198 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
1199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001201 return NULL;
1202 }
1203
1204 /* parse address */
1205 p = vim_strchr(address, ':');
1206 if (p == NULL)
1207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001209 return NULL;
1210 }
1211 *p++ = NUL;
1212 port = strtol((char *)p, &rest, 10);
1213 if (*address == NUL || port <= 0 || *rest != NUL)
1214 {
1215 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001217 return NULL;
1218 }
1219
1220 /* parse options */
1221 clear_job_options(&opt);
1222 opt.jo_mode = MODE_JSON;
1223 opt.jo_timeout = 2000;
1224 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02001225 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001226 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001227 if (opt.jo_timeout < 0)
1228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001230 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001231 }
1232
1233 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1234 if (channel != NULL)
1235 {
1236 opt.jo_set = JO_ALL;
1237 channel_set_options(channel, &opt);
1238 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001239theend:
1240 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001241 return channel;
1242}
1243
Bram Moolenaarde279892016-03-11 22:19:44 +01001244 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001245ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001246{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001247 sock_T *fd = &channel->ch_part[part].ch_fd;
1248
Bram Moolenaarde279892016-03-11 22:19:44 +01001249 if (*fd != INVALID_FD)
1250 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001251 if (part == PART_SOCK)
1252 sock_close(*fd);
1253 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001254 {
1255 /* When using a pty the same FD is set on multiple parts, only
1256 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001257 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1258 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1259 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001260 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001261#ifdef MSWIN
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001262 if (channel->ch_named_pipe)
1263 DisconnectNamedPipe((HANDLE)fd);
1264#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001265 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001266 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001267 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001268 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001269
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001270 /* channel is closed, may want to end the job if it was the last */
1271 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001272 }
1273}
1274
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001275 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001276channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001277{
Bram Moolenaarde279892016-03-11 22:19:44 +01001278 if (in != INVALID_FD)
1279 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001280 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001281 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001282# if defined(UNIX)
1283 /* Do not end the job when all output channels are closed, wait until
1284 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001285 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001286 channel->ch_to_be_closed |= (1U << PART_IN);
1287# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001288 }
1289 if (out != INVALID_FD)
1290 {
1291# if defined(FEAT_GUI)
1292 channel_gui_unregister_one(channel, PART_OUT);
1293# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001294 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001295 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001296 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001297# if defined(FEAT_GUI)
1298 channel_gui_register_one(channel, PART_OUT);
1299# endif
1300 }
1301 if (err != INVALID_FD)
1302 {
1303# if defined(FEAT_GUI)
1304 channel_gui_unregister_one(channel, PART_ERR);
1305# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001306 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001307 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001308 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001309# if defined(FEAT_GUI)
1310 channel_gui_register_one(channel, PART_ERR);
1311# endif
1312 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001313}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001314
Bram Moolenaard6051b52016-02-28 15:49:03 +01001315/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001316 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001317 * This does not keep a refcount, when the job is freed ch_job is cleared.
1318 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001319 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001320channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001321{
Bram Moolenaar77073442016-02-13 23:23:53 +01001322 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001323
1324 channel_set_options(channel, options);
1325
1326 if (job->jv_in_buf != NULL)
1327 {
1328 chanpart_T *in_part = &channel->ch_part[PART_IN];
1329
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001330 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001331 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001332 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001333 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001334 {
1335 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1336 {
1337 /* Special mode: send last-but-one line when appending a line
1338 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001339 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001340 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001341 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001342 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001343 }
1344 else
1345 in_part->ch_buf_top = options->jo_in_top;
1346 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001347 else
1348 in_part->ch_buf_top = 1;
1349 if (options->jo_set & JO_IN_BOT)
1350 in_part->ch_buf_bot = options->jo_in_bot;
1351 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001352 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001353 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001354}
1355
1356/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001357 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001358 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001359 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001360channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001361 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001362 ch_part_T part,
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001363 callback_T *callback,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001364 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001365{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001366 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001367 cbq_T *item = ALLOC_ONE(cbq_T);
Bram Moolenaara07fec92016-02-05 21:04:08 +01001368
1369 if (item != NULL)
1370 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001371 copy_callback(&item->cq_callback, callback);
Bram Moolenaar77073442016-02-13 23:23:53 +01001372 item->cq_seq_nr = id;
1373 item->cq_prev = head->cq_prev;
1374 head->cq_prev = item;
1375 item->cq_next = NULL;
1376 if (item->cq_prev == NULL)
1377 head->cq_next = item;
1378 else
1379 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001380 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001381}
1382
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001383 static void
1384write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1385{
1386 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001387 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001388 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001389 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001390
Bram Moolenaar655da312016-05-28 22:22:34 +02001391 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001392 if ((p = alloc(len + 2)) == NULL)
1393 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001394 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001395
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001396 if (channel->ch_write_text_mode)
1397 p[len] = CAR;
1398 else
1399 {
1400 for (i = 0; i < len; ++i)
1401 if (p[i] == NL)
1402 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001403
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001404 p[len] = NL;
1405 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001406 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001407 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001408 vim_free(p);
1409}
1410
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001411/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001412 * Return TRUE if "channel" can be written to.
1413 * Returns FALSE if the input is closed or the write would block.
1414 */
1415 static int
1416can_write_buf_line(channel_T *channel)
1417{
1418 chanpart_T *in_part = &channel->ch_part[PART_IN];
1419
1420 if (in_part->ch_fd == INVALID_FD)
1421 return FALSE; /* pipe was closed */
1422
1423 /* for testing: block every other attempt to write */
1424 if (in_part->ch_block_write == 1)
1425 in_part->ch_block_write = -1;
1426 else if (in_part->ch_block_write == -1)
1427 in_part->ch_block_write = 1;
1428
1429 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001430#ifndef MSWIN
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001431 {
1432# if defined(HAVE_SELECT)
1433 struct timeval tval;
1434 fd_set wfds;
1435 int ret;
1436
1437 FD_ZERO(&wfds);
1438 FD_SET((int)in_part->ch_fd, &wfds);
1439 tval.tv_sec = 0;
1440 tval.tv_usec = 0;
1441 for (;;)
1442 {
1443 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1444# ifdef EINTR
1445 SOCK_ERRNO;
1446 if (ret == -1 && errno == EINTR)
1447 continue;
1448# endif
1449 if (ret <= 0 || in_part->ch_block_write == 1)
1450 {
1451 if (ret > 0)
1452 ch_log(channel, "FAKED Input not ready for writing");
1453 else
1454 ch_log(channel, "Input not ready for writing");
1455 return FALSE;
1456 }
1457 break;
1458 }
1459# else
1460 struct pollfd fds;
1461
1462 fds.fd = in_part->ch_fd;
1463 fds.events = POLLOUT;
1464 if (poll(&fds, 1, 0) <= 0)
1465 {
1466 ch_log(channel, "Input not ready for writing");
1467 return FALSE;
1468 }
1469 if (in_part->ch_block_write == 1)
1470 {
1471 ch_log(channel, "FAKED Input not ready for writing");
1472 return FALSE;
1473 }
1474# endif
1475 }
1476#endif
1477 return TRUE;
1478}
1479
1480/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001481 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001482 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001483 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001484channel_write_in(channel_T *channel)
1485{
1486 chanpart_T *in_part = &channel->ch_part[PART_IN];
1487 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001488 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001489 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001490
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001491 if (buf == NULL || in_part->ch_buf_append)
1492 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001493 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001494 {
1495 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001496 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001497 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001498 return;
1499 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001500
1501 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1502 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1503 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 if (!can_write_buf_line(channel))
1505 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506 write_buf_line(buf, lnum, channel);
1507 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001508 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001509
1510 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001511 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001512 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001513 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001514
Bram Moolenaar014069a2016-03-03 22:51:40 +01001515 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001516 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001517 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001518#if defined(FEAT_TERMINAL)
1519 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001520 if (channel->ch_job != NULL)
1521 term_send_eof(channel);
1522#endif
1523
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001524 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001525 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001526 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001527
1528 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001529 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001530 }
1531 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001532 ch_log(channel, "Still %ld more lines to write",
1533 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001534}
1535
1536/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001537 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001538 */
1539 void
1540channel_buffer_free(buf_T *buf)
1541{
1542 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001543 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001544
1545 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001546 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001547 {
1548 chanpart_T *ch_part = &channel->ch_part[part];
1549
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001550 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001551 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001552 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001553 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001554 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001555 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001556 }
1557}
1558
1559/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001560 * Write any lines waiting to be written to "channel".
1561 */
1562 static void
1563channel_write_input(channel_T *channel)
1564{
1565 chanpart_T *in_part = &channel->ch_part[PART_IN];
1566
1567 if (in_part->ch_writeque.wq_next != NULL)
1568 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1569 else if (in_part->ch_bufref.br_buf != NULL)
1570 {
1571 if (in_part->ch_buf_append)
1572 channel_write_new_lines(in_part->ch_bufref.br_buf);
1573 else
1574 channel_write_in(channel);
1575 }
1576}
1577
1578/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001579 * Write any lines waiting to be written to a channel.
1580 */
1581 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001582channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001583{
1584 channel_T *channel;
1585
1586 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001587 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001588}
1589
1590/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001591 * Write appended lines above the last one in "buf" to the channel.
1592 */
1593 void
1594channel_write_new_lines(buf_T *buf)
1595{
1596 channel_T *channel;
1597 int found_one = FALSE;
1598
1599 /* There could be more than one channel for the buffer, loop over all of
1600 * them. */
1601 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1602 {
1603 chanpart_T *in_part = &channel->ch_part[PART_IN];
1604 linenr_T lnum;
1605 int written = 0;
1606
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001607 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001608 {
1609 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001610 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001611 found_one = TRUE;
1612 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1613 ++lnum)
1614 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001615 if (!can_write_buf_line(channel))
1616 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001617 write_buf_line(buf, lnum, channel);
1618 ++written;
1619 }
1620
1621 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001622 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001623 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001624 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001625 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001626 ch_log(channel, "Still %ld more lines to write",
1627 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001628
1629 in_part->ch_buf_bot = lnum;
1630 }
1631 }
1632 if (!found_one)
1633 buf->b_write_to_channel = FALSE;
1634}
1635
1636/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001637 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001638 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001639 */
1640 static void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001641invoke_callback(channel_T *channel, callback_T *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001642{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001643 typval_T rettv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001644
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001645 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001647
Bram Moolenaar77073442016-02-13 23:23:53 +01001648 argv[0].v_type = VAR_CHANNEL;
1649 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001650
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001651 call_callback(callback, -1, &rettv, 2, argv);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001652 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001653 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001654}
1655
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001656/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001657 * Return the first node from "channel"/"part" without removing it.
1658 * Returns NULL if there is nothing.
1659 */
1660 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001661channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001662{
1663 readq_T *head = &channel->ch_part[part].ch_head;
1664
1665 return head->rq_next;
1666}
1667
1668/*
1669 * Return a pointer to the first NL in "node".
1670 * Skips over NUL characters.
1671 * Returns NULL if there is no NL.
1672 */
1673 char_u *
1674channel_first_nl(readq_T *node)
1675{
1676 char_u *buffer = node->rq_buffer;
1677 long_u i;
1678
1679 for (i = 0; i < node->rq_buflen; ++i)
1680 if (buffer[i] == NL)
1681 return buffer + i;
1682 return NULL;
1683}
1684
1685/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001686 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001687 * The caller must free it.
1688 * Returns NULL if there is nothing.
1689 */
1690 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001691channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001692{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001693 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001694 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695 char_u *p;
1696
Bram Moolenaar77073442016-02-13 23:23:53 +01001697 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001698 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001699 if (outlen != NULL)
1700 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001702 p = node->rq_buffer;
1703 head->rq_next = node->rq_next;
1704 if (node->rq_next == NULL)
1705 head->rq_prev = NULL;
1706 else
1707 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001708 vim_free(node);
1709 return p;
1710}
1711
1712/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001713 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001714 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001715 */
1716 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001717channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001718{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001719 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001720 readq_T *node;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001721 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001722 char_u *res;
1723 char_u *p;
1724
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001725 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001726 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001727 len += node->rq_buflen;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001728 res = alloc(len + 1);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001729 if (res == NULL)
1730 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001731 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001732 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001733 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001734 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001735 p += node->rq_buflen;
1736 }
1737 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001738
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001739 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001740 do
1741 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001742 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001743 vim_free(p);
1744 } while (p != NULL);
1745
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 if (outlen != NULL)
1747 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001748 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001749 *outlen += len;
1750 return res;
1751 }
1752
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001753 // Turn all NUL into NL, so that the result can be used as a string.
1754 p = res;
1755 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001756 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001757 if (*p == NUL)
1758 *p = NL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001759#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001760 else if (*p == 0x1b)
1761 {
1762 // crush the escape sequence OSC 0/1/2: ESC ]0;
1763 if (p + 3 < res + len
1764 && p[1] == ']'
1765 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1766 && p[3] == ';')
1767 {
1768 // '\a' becomes a NL
1769 while (p < res + (len - 1) && *p != '\a')
1770 ++p;
1771 // BEL is zero width characters, suppress display mistake
1772 // ConPTY (after 10.0.18317) requires advance checking
1773 if (p[-1] == NUL)
1774 p[-1] = 0x07;
1775 }
1776 }
1777#endif
1778 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001779 }
1780
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001781 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001782}
1783
1784/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001785 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001786 * Caller must check these bytes are available.
1787 */
1788 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001789channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001790{
1791 readq_T *head = &channel->ch_part[part].ch_head;
1792 readq_T *node = head->rq_next;
1793 char_u *buf = node->rq_buffer;
1794
1795 mch_memmove(buf, buf + len, node->rq_buflen - len);
1796 node->rq_buflen -= len;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001797 node->rq_buffer[node->rq_buflen] = NUL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001798}
1799
1800/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001801 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001802 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001803 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001804 */
1805 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001806channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001807{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001808 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001809 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001810 readq_T *last_node;
1811 readq_T *n;
1812 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001813 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001814 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001815
Bram Moolenaar77073442016-02-13 23:23:53 +01001816 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001817 return FAIL;
1818
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001819 last_node = node->rq_next;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001820 len = node->rq_buflen + last_node->rq_buflen;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001821 if (want_nl)
1822 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001823 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824 {
1825 last_node = last_node->rq_next;
1826 len += last_node->rq_buflen;
1827 }
1828
Bram Moolenaar772153f2019-03-04 12:09:49 +01001829 p = newbuf = alloc(len + 1);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001830 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001831 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001832 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001833 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001834 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001835 node->rq_buffer = newbuf;
1836 for (n = node; n != last_node; )
1837 {
1838 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001839 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001840 p += n->rq_buflen;
1841 vim_free(n->rq_buffer);
1842 }
Bram Moolenaar772153f2019-03-04 12:09:49 +01001843 *p = NUL;
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001844 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001845
1846 /* dispose of the collapsed nodes and their buffers */
1847 for (n = node->rq_next; n != last_node; )
1848 {
1849 n = n->rq_next;
1850 vim_free(n->rq_prev);
1851 }
1852 node->rq_next = last_node->rq_next;
1853 if (last_node->rq_next == NULL)
1854 head->rq_prev = node;
1855 else
1856 last_node->rq_next->rq_prev = node;
1857 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001858 return OK;
1859}
1860
1861/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001862 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001863 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001864 * Returns OK or FAIL.
1865 */
1866 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001867channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001868 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001869{
1870 readq_T *node;
1871 readq_T *head = &channel->ch_part[part].ch_head;
1872 char_u *p;
1873 int i;
1874
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001875 node = ALLOC_ONE(readq_T);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001876 if (node == NULL)
1877 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001878 /* A NUL is added at the end, because netbeans code expects that.
1879 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001880 node->rq_buffer = alloc(len + 1);
1881 if (node->rq_buffer == NULL)
1882 {
1883 vim_free(node);
1884 return FAIL; /* out of memory */
1885 }
1886
1887 if (channel->ch_part[part].ch_mode == MODE_NL)
1888 {
1889 /* Drop any CR before a NL. */
1890 p = node->rq_buffer;
1891 for (i = 0; i < len; ++i)
1892 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1893 *p++ = buf[i];
1894 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001895 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001896 }
1897 else
1898 {
1899 mch_memmove(node->rq_buffer, buf, len);
1900 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001901 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001902 }
1903
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001904 if (prepend)
1905 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001906 // prepend node to the head of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001907 node->rq_next = head->rq_next;
1908 node->rq_prev = NULL;
1909 if (head->rq_next == NULL)
1910 head->rq_prev = node;
1911 else
1912 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001913 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001914 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001915 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001916 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001917 // append node to the tail of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001918 node->rq_next = NULL;
1919 node->rq_prev = head->rq_prev;
1920 if (head->rq_prev == NULL)
1921 head->rq_next = node;
1922 else
1923 head->rq_prev->rq_next = node;
1924 head->rq_prev = node;
1925 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001926
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001927 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001928 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001929 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001930 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001931 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001932 fprintf(log_fd, "'\n");
1933 }
1934 return OK;
1935}
1936
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001937/*
1938 * Try to fill the buffer of "reader".
1939 * Returns FALSE when nothing was added.
1940 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001941 static int
1942channel_fill(js_read_T *reader)
1943{
1944 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001945 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001946 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001947 int keeplen;
1948 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001949 char_u *p;
1950
1951 if (next == NULL)
1952 return FALSE;
1953
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001954 keeplen = reader->js_end - reader->js_buf;
1955 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001956 {
1957 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001958 addlen = (int)STRLEN(next);
1959 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001960 if (p == NULL)
1961 {
1962 vim_free(next);
1963 return FALSE;
1964 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001965 mch_memmove(p, reader->js_buf, keeplen);
1966 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001967 vim_free(next);
1968 next = p;
1969 }
1970
1971 vim_free(reader->js_buf);
1972 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001973 return TRUE;
1974}
1975
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001976/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001977 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001978 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001979 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001980 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001981 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001982channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001983{
1984 js_read_T reader;
1985 typval_T listtv;
1986 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001987 chanpart_T *chanpart = &channel->ch_part[part];
1988 jsonq_T *head = &chanpart->ch_json_head;
1989 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001990 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001991
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001992 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001993 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001994
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001995 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001996 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001997 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001998 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001999 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002000
2001 /* When a message is incomplete we wait for a short while for more to
2002 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002003 * or list will make us hang.
2004 * Do not generate error messages, they will be written in a channel log. */
2005 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002006 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002007 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002008 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002009 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002010 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002011 /* Only accept the response when it is a list with at least two
2012 * items. */
2013 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002014 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002015 if (listtv.v_type != VAR_LIST)
2016 ch_error(channel, "Did not receive a list, discarding");
2017 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002018 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002019 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002020 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002021 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002022 else
2023 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002024 item = ALLOC_ONE(jsonq_T);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002025 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002026 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002027 else
2028 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002029 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002030 item->jq_value = alloc_tv();
2031 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002032 {
2033 vim_free(item);
2034 clear_tv(&listtv);
2035 }
2036 else
2037 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002038 *item->jq_value = listtv;
2039 item->jq_prev = head->jq_prev;
2040 head->jq_prev = item;
2041 item->jq_next = NULL;
2042 if (item->jq_prev == NULL)
2043 head->jq_next = item;
2044 else
2045 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002046 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047 }
2048 }
2049 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002050
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002051 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002052 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002053 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002054 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002055 size_t buflen = STRLEN(reader.js_buf);
2056
2057 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002058 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002059 /* First time encountering incomplete message or after receiving
2060 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002061 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002062 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002063 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002064 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002065 chanpart->ch_wait_len = buflen;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002066#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002067 chanpart->ch_deadline = GetTickCount() + 100L;
2068#else
2069 gettimeofday(&chanpart->ch_deadline, NULL);
2070 chanpart->ch_deadline.tv_usec += 100 * 1000;
2071 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2072 {
2073 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2074 ++chanpart->ch_deadline.tv_sec;
2075 }
2076#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002077 }
2078 else
2079 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002080 int timeout;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002081#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002082 timeout = GetTickCount() > chanpart->ch_deadline;
2083#else
2084 {
2085 struct timeval now_tv;
2086
2087 gettimeofday(&now_tv, NULL);
2088 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2089 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2090 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2091 }
2092#endif
2093 if (timeout)
2094 {
2095 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002096 chanpart->ch_wait_len = 0;
2097 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002098 }
2099 else
2100 {
2101 reader.js_used = 0;
2102 ch_log(channel, "still waiting on incomplete message");
2103 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002104 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002105 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002106
2107 if (status == FAIL)
2108 {
2109 ch_error(channel, "Decoding failed - discarding input");
2110 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002111 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002112 }
2113 else if (reader.js_buf[reader.js_used] != NUL)
2114 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002115 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002116 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002117 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2118 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002119 ret = status == MAYBE ? FALSE: TRUE;
2120 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002121 else
2122 ret = FALSE;
2123
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002124 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002125 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002126}
2127
2128/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002129 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002130 */
2131 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002132remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002133{
Bram Moolenaar77073442016-02-13 23:23:53 +01002134 if (node->cq_prev == NULL)
2135 head->cq_next = node->cq_next;
2136 else
2137 node->cq_prev->cq_next = node->cq_next;
2138 if (node->cq_next == NULL)
2139 head->cq_prev = node->cq_prev;
2140 else
2141 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002142}
2143
2144/*
2145 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002146 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002147 */
2148 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002149remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002150{
Bram Moolenaar77073442016-02-13 23:23:53 +01002151 if (node->jq_prev == NULL)
2152 head->jq_next = node->jq_next;
2153 else
2154 node->jq_prev->jq_next = node->jq_next;
2155 if (node->jq_next == NULL)
2156 head->jq_prev = node->jq_prev;
2157 else
2158 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002159 vim_free(node);
2160}
2161
2162/*
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002163 * Add "id" to the list of JSON message IDs we are waiting on.
2164 */
2165 static void
2166channel_add_block_id(chanpart_T *chanpart, int id)
2167{
2168 garray_T *gap = &chanpart->ch_block_ids;
2169
2170 if (gap->ga_growsize == 0)
2171 ga_init2(gap, (int)sizeof(int), 10);
2172 if (ga_grow(gap, 1) == OK)
2173 {
2174 ((int *)gap->ga_data)[gap->ga_len] = id;
2175 ++gap->ga_len;
2176 }
2177}
2178
2179/*
2180 * Remove "id" from the list of JSON message IDs we are waiting on.
2181 */
2182 static void
2183channel_remove_block_id(chanpart_T *chanpart, int id)
2184{
2185 garray_T *gap = &chanpart->ch_block_ids;
2186 int i;
2187
2188 for (i = 0; i < gap->ga_len; ++i)
2189 if (((int *)gap->ga_data)[i] == id)
2190 {
2191 --gap->ga_len;
2192 if (i < gap->ga_len)
2193 {
2194 int *p = ((int *)gap->ga_data) + i;
2195
2196 mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int));
2197 }
2198 return;
2199 }
2200 siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id);
2201}
2202
2203/*
2204 * Return TRUE if "id" is in the list of JSON message IDs we are waiting on.
2205 */
2206 static int
2207channel_has_block_id(chanpart_T *chanpart, int id)
2208{
2209 garray_T *gap = &chanpart->ch_block_ids;
2210 int i;
2211
2212 for (i = 0; i < gap->ga_len; ++i)
2213 if (((int *)gap->ga_data)[i] == id)
2214 return TRUE;
2215 return FALSE;
2216}
2217
2218/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002219 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002220 * When "id" is positive it must match the first number in the list.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002221 * When "id" is zero or negative jut get the first message. But not one
2222 * in the ch_block_ids list.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002223 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002224 * Return OK when found and return the value in "rettv".
2225 * Return FAIL otherwise.
2226 */
2227 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002228channel_get_json(
2229 channel_T *channel,
2230 ch_part_T part,
2231 int id,
2232 int without_callback,
2233 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002234{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002235 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002236 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002237
Bram Moolenaar77073442016-02-13 23:23:53 +01002238 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002239 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002240 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002241 typval_T *tv = &l->lv_first->li_tv;
2242
Bram Moolenaar958dc692016-12-01 15:34:12 +01002243 if ((without_callback || !item->jq_no_callback)
2244 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002245 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002246 || tv->vval.v_number == 0
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002247 || !channel_has_block_id(
2248 &channel->ch_part[part], tv->vval.v_number)))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002249 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002250 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002251 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002252 ch_log(channel, "Getting JSON message %ld",
2253 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002254 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002255 return OK;
2256 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002257 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002258 }
2259 return FAIL;
2260}
2261
Bram Moolenaar958dc692016-12-01 15:34:12 +01002262/*
2263 * Put back "rettv" into the JSON queue, there was no callback for it.
2264 * Takes over the values in "rettv".
2265 */
2266 static void
2267channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2268{
2269 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2270 jsonq_T *item = head->jq_next;
2271 jsonq_T *newitem;
2272
2273 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2274 /* last item was pushed back, append to the end */
2275 item = NULL;
2276 else while (item != NULL && item->jq_no_callback)
2277 /* append after the last item that was pushed back */
2278 item = item->jq_next;
2279
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002280 newitem = ALLOC_ONE(jsonq_T);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002281 if (newitem == NULL)
2282 clear_tv(rettv);
2283 else
2284 {
2285 newitem->jq_value = alloc_tv();
2286 if (newitem->jq_value == NULL)
2287 {
2288 vim_free(newitem);
2289 clear_tv(rettv);
2290 }
2291 else
2292 {
2293 newitem->jq_no_callback = FALSE;
2294 *newitem->jq_value = *rettv;
2295 if (item == NULL)
2296 {
2297 /* append to the end */
2298 newitem->jq_prev = head->jq_prev;
2299 head->jq_prev = newitem;
2300 newitem->jq_next = NULL;
2301 if (newitem->jq_prev == NULL)
2302 head->jq_next = newitem;
2303 else
2304 newitem->jq_prev->jq_next = newitem;
2305 }
2306 else
2307 {
2308 /* append after "item" */
2309 newitem->jq_prev = item;
2310 newitem->jq_next = item->jq_next;
2311 item->jq_next = newitem;
2312 if (newitem->jq_next == NULL)
2313 head->jq_prev = newitem;
2314 else
2315 newitem->jq_next->jq_prev = newitem;
2316 }
2317 }
2318 }
2319}
2320
Bram Moolenaarece61b02016-02-20 21:39:05 +01002321#define CH_JSON_MAX_ARGS 4
2322
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002323/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002324 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 * "argv[0]" is the command string.
2326 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002327 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002328 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002329channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002330{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331 char_u *cmd = argv[0].vval.v_string;
2332 char_u *arg;
2333 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002334
Bram Moolenaarece61b02016-02-20 21:39:05 +01002335 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002336 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002337 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002338 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002339 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002340 return;
2341 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002342 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002343 if (arg == NULL)
2344 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002345
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002346 if (STRCMP(cmd, "ex") == 0)
2347 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002348 int save_called_emsg = called_emsg;
2349
2350 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002351 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002352 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002353 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002354 --emsg_silent;
2355 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002356 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002357 (char *)get_vim_var_str(VV_ERRMSG));
2358 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002359 }
2360 else if (STRCMP(cmd, "normal") == 0)
2361 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002362 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002363
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002364 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002365 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002366 ea.arg = arg;
2367 ea.addr_count = 0;
2368 ea.forceit = TRUE; /* no mapping */
2369 ex_normal(&ea);
2370 }
2371 else if (STRCMP(cmd, "redraw") == 0)
2372 {
2373 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002374
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002375 ch_log(channel, "redraw");
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002376 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002377 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002378 ex_redraw(&ea);
2379 showruler(FALSE);
2380 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002381 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002382 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002383 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002384 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002385 int is_call = cmd[0] == 'c';
2386 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002387
Bram Moolenaarece61b02016-02-20 21:39:05 +01002388 if (argv[id_idx].v_type != VAR_UNKNOWN
2389 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002390 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002391 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002392 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002393 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002394 }
2395 else if (is_call && argv[2].v_type != VAR_LIST)
2396 {
2397 ch_error(channel, "third argument for call must be a list");
2398 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002399 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002400 }
2401 else
2402 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002403 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002405 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002406 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002407
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002408 /* Don't pollute the display with errors. */
2409 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002410 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002411 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002412 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002413 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002414 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002415 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002416 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002417 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002418 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2419 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002420 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002421
2422 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002423 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002424 int id = argv[id_idx].vval.v_number;
2425
Bram Moolenaar55fab432016-02-07 16:53:13 +01002426 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002427 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002428 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002429 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002430 /* If evaluation failed or the result can't be encoded
2431 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002432 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002433 err_tv.v_type = VAR_STRING;
2434 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002435 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002436 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002437 if (json != NULL)
2438 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002439 channel_send(channel,
2440 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002441 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002442 vim_free(json);
2443 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002444 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002445 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002446 if (tv == &res_tv)
2447 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002448 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002449 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002450 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002451 }
2452 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002453 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002454 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002455 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002456 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002457}
2458
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002459/*
2460 * Invoke the callback at "cbhead".
2461 * Does not redraw but sets channel_need_redraw.
2462 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002463 static void
2464invoke_one_time_callback(
2465 channel_T *channel,
2466 cbq_T *cbhead,
2467 cbq_T *item,
2468 typval_T *argv)
2469{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002470 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002471 (char *)item->cq_callback.cb_name);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002472 /* Remove the item from the list first, if the callback
2473 * invokes ch_close() the list will be cleared. */
2474 remove_cb_node(cbhead, item);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002475 invoke_callback(channel, &item->cq_callback, argv);
2476 free_callback(&item->cq_callback);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002477 vim_free(item);
2478}
2479
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002480 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002481append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002482{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002483 bufref_T save_curbuf = {NULL, 0, 0};
2484 win_T *save_curwin = NULL;
2485 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002486 linenr_T lnum = buffer->b_ml.ml_line_count;
2487 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002488 chanpart_T *ch_part = &channel->ch_part[part];
2489 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002490 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002491
2492 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2493 {
2494 if (!ch_part->ch_nomod_error)
2495 {
2496 ch_error(channel, "Buffer is not modifiable, cannot append");
2497 ch_part->ch_nomod_error = TRUE;
2498 }
2499 return;
2500 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002501
2502 /* If the buffer is also used as input insert above the last
2503 * line. Don't write these lines. */
2504 if (save_write_to)
2505 {
2506 --lnum;
2507 buffer->b_write_to_channel = FALSE;
2508 }
2509
2510 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002511 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002512
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002513 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002514
2515 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2516 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2517
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002518 u_sync(TRUE);
2519 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002520 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002521
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002522 if (empty)
2523 {
2524 /* The buffer is empty, replace the first (dummy) line. */
2525 ml_replace(lnum, msg, TRUE);
2526 lnum = 0;
2527 }
2528 else
2529 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002530 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002531
2532 /* Restore curbuf/curwin/curtab */
2533 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2534
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002535 if (ch_part->ch_nomodifiable)
2536 buffer->b_p_ma = FALSE;
2537 else
2538 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002539
2540 if (buffer->b_nwindows > 0)
2541 {
2542 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002543
2544 FOR_ALL_WINDOWS(wp)
2545 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002546 if (wp->w_buffer == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002547 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002548 int move_cursor = save_write_to
2549 ? wp->w_cursor.lnum == lnum + 1
2550 : (wp->w_cursor.lnum == lnum
2551 && wp->w_cursor.col == 0);
2552
2553 // If the cursor is at or above the new line, move it one line
2554 // down. If the topline is outdated update it now.
2555 if (move_cursor || wp->w_topline > buffer->b_ml.ml_line_count)
2556 {
2557 if (move_cursor)
2558 ++wp->w_cursor.lnum;
2559 save_curwin = curwin;
2560 curwin = wp;
2561 curbuf = curwin->w_buffer;
2562 scroll_cursor_bot(0, FALSE);
2563 curwin = save_curwin;
2564 curbuf = curwin->w_buffer;
2565 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002566 }
2567 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002568 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002569 channel_need_redraw = TRUE;
2570 }
2571
2572 if (save_write_to)
2573 {
2574 channel_T *ch;
2575
2576 /* Find channels reading from this buffer and adjust their
2577 * next-to-read line number. */
2578 buffer->b_write_to_channel = TRUE;
2579 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2580 {
2581 chanpart_T *in_part = &ch->ch_part[PART_IN];
2582
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002583 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002584 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2585 }
2586 }
2587}
2588
Bram Moolenaar437905c2016-04-26 19:01:05 +02002589 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002590drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002591{
2592 char_u *msg;
2593
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002594 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002595 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002596 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002597 vim_free(msg);
2598 }
2599}
2600
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002601/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002602 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002603 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002604 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002605 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002606 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002607may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002608{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002609 char_u *msg = NULL;
2610 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002611 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002612 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002613 chanpart_T *ch_part = &channel->ch_part[part];
2614 ch_mode_T ch_mode = ch_part->ch_mode;
2615 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002616 cbq_T *cbitem;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002617 callback_T *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002618 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002619 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002620
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002621 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002622 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002623 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002624
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002625 /* Use a message-specific callback, part callback or channel callback */
2626 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2627 if (cbitem->cq_seq_nr == 0)
2628 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002629 if (cbitem != NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002630 callback = &cbitem->cq_callback;
2631 else if (ch_part->ch_callback.cb_name != NULL)
2632 callback = &ch_part->ch_callback;
2633 else if (channel->ch_callback.cb_name != NULL)
2634 callback = &channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002635
Bram Moolenaarec68a992016-10-03 21:37:41 +02002636 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002637 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2638 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002639 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002640 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002641 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002642 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002643 buffer = NULL;
2644 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002645
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002646 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002647 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002648 listitem_T *item;
2649 int argc = 0;
2650
Bram Moolenaard7ece102016-02-02 23:23:02 +01002651 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002652 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002653 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002654 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002655 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002656 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002657 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002658 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002659
Bram Moolenaarece61b02016-02-20 21:39:05 +01002660 for (item = listtv->vval.v_list->lv_first;
2661 item != NULL && argc < CH_JSON_MAX_ARGS;
2662 item = item->li_next)
2663 argv[argc++] = item->li_tv;
2664 while (argc < CH_JSON_MAX_ARGS)
2665 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002666
Bram Moolenaarece61b02016-02-20 21:39:05 +01002667 if (argv[0].v_type == VAR_STRING)
2668 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002669 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002670 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002671 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002672 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002673 }
2674
Bram Moolenaarece61b02016-02-20 21:39:05 +01002675 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002676 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002677 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002678 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002679 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002680 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002681 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002682 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002683 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002684 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002685 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002686 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002687 return FALSE;
2688 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002689 else
2690 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002691 /* If there is no callback or buffer drop the message. */
2692 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002693 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002694 /* If there is a close callback it may use ch_read() to get the
2695 * messages. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002696 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002697 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002698 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002699 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002700
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002701 if (ch_mode == MODE_NL)
2702 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002703 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002704 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002705 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002706
2707 /* See if we have a message ending in NL in the first buffer. If
2708 * not try to concatenate the first and the second buffer. */
2709 while (TRUE)
2710 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002711 node = channel_peek(channel, part);
2712 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002713 if (nl != NULL)
2714 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002715 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002716 {
2717 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2718 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002719 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002720 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002721 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002722 buf = node->rq_buffer;
2723
Bram Moolenaar772153f2019-03-04 12:09:49 +01002724 // Convert NUL to NL, the internal representation.
2725 for (p = buf; (nl == NULL || p < nl)
2726 && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002727 if (*p == NUL)
2728 *p = NL;
2729
Bram Moolenaar772153f2019-03-04 12:09:49 +01002730 if (nl == NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002731 {
Bram Moolenaar772153f2019-03-04 12:09:49 +01002732 // get the whole buffer, drop the NL
2733 msg = channel_get(channel, part, NULL);
2734 }
2735 else if (nl + 1 == buf + node->rq_buflen)
2736 {
2737 // get the whole buffer
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002738 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002739 *nl = NUL;
2740 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002741 else
2742 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002743 /* Copy the message into allocated memory (excluding the NL)
2744 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002745 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002746 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002747 }
2748 }
2749 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002750 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002751 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002752 * get everything we have.
2753 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002754 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002755 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002756
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002757 if (msg == NULL)
2758 return FALSE; /* out of memory (and avoids Coverity warning) */
2759
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002760 argv[1].v_type = VAR_STRING;
2761 argv[1].vval.v_string = msg;
2762 }
2763
Bram Moolenaara07fec92016-02-05 21:04:08 +01002764 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002765 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002766 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002767
Bram Moolenaar958dc692016-12-01 15:34:12 +01002768 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002769 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002770 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002771 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002772 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002773 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002774 break;
2775 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002776 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002777 {
2778 if (channel->ch_drop_never)
2779 {
2780 /* message must be read with ch_read() */
2781 channel_push_json(channel, part, listtv);
2782 listtv = NULL;
2783 }
2784 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002785 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002786 seq_nr);
2787 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002788 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002789 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002790 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002791 if (buffer != NULL)
2792 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002793 if (msg == NULL)
2794 /* JSON or JS mode: re-encode the message. */
2795 msg = json_encode(listtv, ch_mode);
2796 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002797 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002798#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002799 if (buffer->b_term != NULL)
2800 write_to_term(buffer, msg, channel);
2801 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002802#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002803 append_to_buffer(buffer, msg, channel, part);
2804 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002805 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002806
Bram Moolenaar187db502016-02-27 14:44:26 +01002807 if (callback != NULL)
2808 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002809 if (cbitem != NULL)
2810 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2811 else
2812 {
2813 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002814 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002815 (char *)callback->cb_name);
2816 invoke_callback(channel, callback, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002817 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002818 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002819 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002820 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002821 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002822
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002823 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002824 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002825 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002826
2827 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002828}
2829
Bram Moolenaar113e1072019-01-20 15:30:40 +01002830#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002831/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002832 * Return TRUE when channel "channel" is open for writing to.
2833 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002834 */
2835 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002836channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002837{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002838 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002839 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002840}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002841#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002842
2843/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002844 * Return TRUE when channel "channel" is open for reading or writing.
2845 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002846 */
2847 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002848channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002849{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002850 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002851 || channel->CH_IN_FD != INVALID_FD
2852 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002853 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002854}
2855
2856/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002857 * Return TRUE if "channel" has JSON or other typeahead.
2858 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002859 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002860channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002861{
2862 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2863
2864 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2865 {
2866 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002867
Bram Moolenaar4340fc92019-06-28 22:06:49 +02002868 if (head->jq_next == NULL)
2869 // Parse json from readahead, there might be a complete message to
2870 // process.
2871 channel_parse_json(channel, part);
2872
2873 return head->jq_next != NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002874 }
2875 return channel_peek(channel, part) != NULL;
2876}
2877
2878/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002879 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002880 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002881 */
2882 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002883channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002884{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002885 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002886 int has_readahead = FALSE;
2887
Bram Moolenaar77073442016-02-13 23:23:53 +01002888 if (channel == NULL)
2889 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002890 if (req_part == PART_OUT)
2891 {
2892 if (channel->CH_OUT_FD != INVALID_FD)
2893 return "open";
2894 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002895 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002896 }
2897 else if (req_part == PART_ERR)
2898 {
2899 if (channel->CH_ERR_FD != INVALID_FD)
2900 return "open";
2901 if (channel_has_readahead(channel, PART_ERR))
2902 has_readahead = TRUE;
2903 }
2904 else
2905 {
2906 if (channel_is_open(channel))
2907 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002908 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002909 if (channel_has_readahead(channel, part))
2910 {
2911 has_readahead = TRUE;
2912 break;
2913 }
2914 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002915
2916 if (has_readahead)
2917 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002918 return "closed";
2919}
2920
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002921 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002922channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002923{
2924 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002925 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002926 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002927 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002928 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002929
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002930 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002931 STRCAT(namebuf, "_");
2932 tail = STRLEN(namebuf);
2933
2934 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002935 if (chanpart->ch_fd != INVALID_FD)
2936 status = "open";
2937 else if (channel_has_readahead(channel, part))
2938 status = "buffered";
2939 else
2940 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002941 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002942
2943 STRCPY(namebuf + tail, "mode");
2944 switch (chanpart->ch_mode)
2945 {
2946 case MODE_NL: s = "NL"; break;
2947 case MODE_RAW: s = "RAW"; break;
2948 case MODE_JSON: s = "JSON"; break;
2949 case MODE_JS: s = "JS"; break;
2950 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002951 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002952
2953 STRCPY(namebuf + tail, "io");
2954 if (part == PART_SOCK)
2955 s = "socket";
2956 else switch (chanpart->ch_io)
2957 {
2958 case JIO_NULL: s = "null"; break;
2959 case JIO_PIPE: s = "pipe"; break;
2960 case JIO_FILE: s = "file"; break;
2961 case JIO_BUFFER: s = "buffer"; break;
2962 case JIO_OUT: s = "out"; break;
2963 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002964 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002965
2966 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002967 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002968}
2969
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002970 static void
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002971channel_info(channel_T *channel, dict_T *dict)
2972{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002973 dict_add_number(dict, "id", channel->ch_id);
2974 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002975
2976 if (channel->ch_hostname != NULL)
2977 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002978 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2979 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002980 channel_part_info(channel, dict, "sock", PART_SOCK);
2981 }
2982 else
2983 {
2984 channel_part_info(channel, dict, "out", PART_OUT);
2985 channel_part_info(channel, dict, "err", PART_ERR);
2986 channel_part_info(channel, dict, "in", PART_IN);
2987 }
2988}
2989
Bram Moolenaar77073442016-02-13 23:23:53 +01002990/*
2991 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002992 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002993 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002994 */
2995 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002996channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002997{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002998 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002999
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003000#ifdef FEAT_GUI
3001 channel_gui_unregister(channel);
3002#endif
3003
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003004 ch_close_part(channel, PART_SOCK);
3005 ch_close_part(channel, PART_IN);
3006 ch_close_part(channel, PART_OUT);
3007 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003008
Bram Moolenaara9f02812017-08-05 17:40:38 +02003009 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003010 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02003011 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003012
Bram Moolenaara9f02812017-08-05 17:40:38 +02003013 /* Invoke callbacks and flush buffers before the close callback. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003014 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003015 ch_log(channel,
3016 "Invoking callbacks and flushing buffers before closing");
3017 for (part = PART_SOCK; part < PART_IN; ++part)
3018 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003019 if (channel->ch_close_cb.cb_name != NULL
Bram Moolenaara9f02812017-08-05 17:40:38 +02003020 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3021 {
3022 /* Increment the refcount to avoid the channel being freed
3023 * halfway. */
3024 ++channel->ch_refcount;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003025 if (channel->ch_close_cb.cb_name == NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003026 ch_log(channel, "flushing %s buffers before closing",
3027 part_names[part]);
3028 while (may_invoke_callback(channel, part))
3029 ;
3030 --channel->ch_refcount;
3031 }
3032 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003033
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003034 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003035 {
3036 typval_T argv[1];
3037 typval_T rettv;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003038
3039 /* Increment the refcount to avoid the channel being freed
3040 * halfway. */
3041 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003042 ch_log(channel, "Invoking close callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003043 (char *)channel->ch_close_cb.cb_name);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003044 argv[0].v_type = VAR_CHANNEL;
3045 argv[0].vval.v_channel = channel;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003046 call_callback(&channel->ch_close_cb, -1, &rettv, 1, argv);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003047 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003048 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003049
Bram Moolenaara9f02812017-08-05 17:40:38 +02003050 /* the callback is only called once */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003051 free_callback(&channel->ch_close_cb);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003052
Bram Moolenaara9f02812017-08-05 17:40:38 +02003053 if (channel_need_redraw)
3054 {
3055 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003056 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003057 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003058
Bram Moolenaara9f02812017-08-05 17:40:38 +02003059 if (!channel->ch_drop_never)
3060 /* any remaining messages are useless now */
3061 for (part = PART_SOCK; part < PART_IN; ++part)
3062 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003063
3064 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003065 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003066 }
3067
3068 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003069
3070#ifdef FEAT_TERMINAL
3071 term_channel_closed(channel);
3072#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003073}
3074
Bram Moolenaard04a0202016-01-26 23:30:18 +01003075/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003076 * Close the "in" part channel "channel".
3077 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003078 static void
Bram Moolenaar0874a832016-09-01 15:11:51 +02003079channel_close_in(channel_T *channel)
3080{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003081 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003082}
3083
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003084 static void
3085remove_from_writeque(writeq_T *wq, writeq_T *entry)
3086{
3087 ga_clear(&entry->wq_ga);
3088 wq->wq_next = entry->wq_next;
3089 if (wq->wq_next == NULL)
3090 wq->wq_prev = NULL;
3091 else
3092 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003093 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003094}
3095
Bram Moolenaar0874a832016-09-01 15:11:51 +02003096/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003097 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003098 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003099 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003100channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003101{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003102 chanpart_T *ch_part = &channel->ch_part[part];
3103 jsonq_T *json_head = &ch_part->ch_json_head;
3104 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003105
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003106 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003107 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003108
3109 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003110 {
3111 cbq_T *node = cb_head->cq_next;
3112
3113 remove_cb_node(cb_head, node);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003114 free_callback(&node->cq_callback);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003115 vim_free(node);
3116 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003117
3118 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003119 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003120 free_tv(json_head->jq_next->jq_value);
3121 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003122 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003123
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003124 free_callback(&ch_part->ch_callback);
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003125 ga_clear(&ch_part->ch_block_ids);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003126
3127 while (ch_part->ch_writeque.wq_next != NULL)
3128 remove_from_writeque(&ch_part->ch_writeque,
3129 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003130}
3131
3132/*
3133 * Clear all the read buffers on "channel".
3134 */
3135 void
3136channel_clear(channel_T *channel)
3137{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003138 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003139 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003140 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003141 channel_clear_one(channel, PART_OUT);
3142 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003143 channel_clear_one(channel, PART_IN);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003144 free_callback(&channel->ch_callback);
3145 free_callback(&channel->ch_close_cb);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003146}
3147
Bram Moolenaar77073442016-02-13 23:23:53 +01003148#if defined(EXITFREE) || defined(PROTO)
3149 void
3150channel_free_all(void)
3151{
3152 channel_T *channel;
3153
Bram Moolenaard6051b52016-02-28 15:49:03 +01003154 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003155 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3156 channel_clear(channel);
3157}
3158#endif
3159
3160
Bram Moolenaar715d2852016-04-30 17:06:31 +02003161/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003162#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003163
3164/* Buffer size for reading incoming messages. */
3165#define MAXMSGSIZE 4096
3166
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003167#if defined(HAVE_SELECT)
3168/*
3169 * Add write fds where we are waiting for writing to be possible.
3170 */
3171 static int
3172channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3173{
3174 int maxfd = maxfd_arg;
3175 channel_T *ch;
3176
3177 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3178 {
3179 chanpart_T *in_part = &ch->ch_part[PART_IN];
3180
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003181 if (in_part->ch_fd != INVALID_FD
3182 && (in_part->ch_bufref.br_buf != NULL
3183 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003184 {
3185 FD_SET((int)in_part->ch_fd, wfds);
3186 if ((int)in_part->ch_fd >= maxfd)
3187 maxfd = (int)in_part->ch_fd + 1;
3188 }
3189 }
3190 return maxfd;
3191}
3192#else
3193/*
3194 * Add write fds where we are waiting for writing to be possible.
3195 */
3196 static int
3197channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3198{
3199 int nfd = nfd_in;
3200 channel_T *ch;
3201
3202 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3203 {
3204 chanpart_T *in_part = &ch->ch_part[PART_IN];
3205
Bram Moolenaar683b7962017-08-19 15:51:59 +02003206 if (in_part->ch_fd != INVALID_FD
3207 && (in_part->ch_bufref.br_buf != NULL
3208 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003209 {
3210 in_part->ch_poll_idx = nfd;
3211 fds[nfd].fd = in_part->ch_fd;
3212 fds[nfd].events = POLLOUT;
3213 ++nfd;
3214 }
3215 else
3216 in_part->ch_poll_idx = -1;
3217 }
3218 return nfd;
3219}
3220#endif
3221
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003222typedef enum {
3223 CW_READY,
3224 CW_NOT_READY,
3225 CW_ERROR
3226} channel_wait_result;
3227
Bram Moolenaard04a0202016-01-26 23:30:18 +01003228/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003229 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003230 * Return CW_READY when there is something to read.
3231 * Return CW_NOT_READY when there is nothing to read.
3232 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003233 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003234 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003235channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003236{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003237 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003238 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003239
Bram Moolenaar4f974752019-02-17 17:44:42 +01003240# ifdef MSWIN
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003241 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003242 {
3243 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003244 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003245 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003246 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003247
3248 /* reading from a pipe, not a socket */
3249 while (TRUE)
3250 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003251 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3252
3253 if (r && nread > 0)
3254 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003255
3256 if (channel->ch_named_pipe)
3257 {
3258 DisconnectNamedPipe((HANDLE)fd);
3259 ConnectNamedPipe((HANDLE)fd, NULL);
3260 }
3261 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003262 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003263
3264 /* perhaps write some buffer lines */
3265 channel_write_any_lines();
3266
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003267 sleep_time = deadline - GetTickCount();
3268 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003269 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003270 /* Wait for a little while. Very short at first, up to 10 msec
3271 * after looping a few times. */
3272 if (sleep_time > delay)
3273 sleep_time = delay;
3274 Sleep(sleep_time);
3275 delay = delay * 2;
3276 if (delay > 10)
3277 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003278 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003279 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003280 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003281#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003282 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003283#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003284 struct timeval tval;
3285 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003286 fd_set wfds;
3287 int ret;
3288 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003289
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003290 tval.tv_sec = timeout / 1000;
3291 tval.tv_usec = (timeout % 1000) * 1000;
3292 for (;;)
3293 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003294 FD_ZERO(&rfds);
3295 FD_SET((int)fd, &rfds);
3296
3297 /* Write lines to a pipe when a pipe can be written to. Need to
3298 * set this every time, some buffers may be done. */
3299 maxfd = (int)fd + 1;
3300 FD_ZERO(&wfds);
3301 maxfd = channel_fill_wfds(maxfd, &wfds);
3302
3303 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003304# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003305 SOCK_ERRNO;
3306 if (ret == -1 && errno == EINTR)
3307 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003308# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003309 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003310 {
3311 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003312 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003313 channel_write_any_lines();
3314 continue;
3315 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003316 break;
3317 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003318#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003319 for (;;)
3320 {
3321 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3322 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003323
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003324 fds[0].fd = fd;
3325 fds[0].events = POLLIN;
3326 nfd = channel_fill_poll_write(nfd, fds);
3327 if (poll(fds, nfd, timeout) > 0)
3328 {
3329 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003330 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003331 channel_write_any_lines();
3332 continue;
3333 }
3334 break;
3335 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003336#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003337 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003338 return CW_NOT_READY;
3339}
3340
3341 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003342ch_close_part_on_error(
3343 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003344{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003345 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003346
3347 if (is_err)
3348 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003349 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003350 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003351 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003352
3353 /* Queue a "DETACH" netbeans message in the command queue in order to
3354 * terminate the netbeans session later. Do not end the session here
3355 * directly as we may be running in the context of a call to
3356 * netbeans_parse_messages():
3357 * netbeans_parse_messages
3358 * -> autocmd triggered while processing the netbeans cmd
3359 * -> ui_breakcheck
3360 * -> gui event loop or select loop
3361 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003362 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003363 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003364 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003365 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003366 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3367
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003368 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003369 * close the channel yet, there may be something to read on another part.
3370 * When stdout and stderr use the same FD we get the error only on one of
3371 * them, also close the other. */
3372 if (part == PART_OUT || part == PART_ERR)
3373 {
3374 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3375
3376 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3377 ch_close_part(channel, other);
3378 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003379 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003380
3381#ifdef FEAT_GUI
3382 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003383 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003384#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003385}
3386
3387 static void
3388channel_close_now(channel_T *channel)
3389{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003390 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003391 if (channel->ch_nb_close_cb != NULL)
3392 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003393 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003394}
3395
3396/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003397 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003398 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003399 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003400 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003401 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003402channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003403{
3404 static char_u *buf = NULL;
3405 int len = 0;
3406 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003407 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003408 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003409
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003410 fd = channel->ch_part[part].ch_fd;
3411 if (fd == INVALID_FD)
3412 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003413 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003414 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003415 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003416 }
3417 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003418
3419 /* Allocate a buffer to read into. */
3420 if (buf == NULL)
3421 {
3422 buf = alloc(MAXMSGSIZE);
3423 if (buf == NULL)
3424 return; /* out of memory! */
3425 }
3426
3427 /* Keep on reading for as long as there is something to read.
3428 * Use select() or poll() to avoid blocking on a message that is exactly
3429 * MAXMSGSIZE long. */
3430 for (;;)
3431 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003432 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003433 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003434 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003435 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003436 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003437 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003438 if (len <= 0)
3439 break; /* error or nothing more to read */
3440
3441 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003442 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003443 readlen += len;
3444 if (len < MAXMSGSIZE)
3445 break; /* did read everything that's available */
3446 }
3447
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003448 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003449 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003450 {
3451 if (!channel->ch_keep_open)
3452 ch_close_part_on_error(channel, part, (len < 0), func);
3453 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003454#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003455 else if (CH_HAS_GUI && gtk_main_level() > 0)
3456 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003457 gtk_main_quit();
3458#endif
3459}
3460
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003461/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003462 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003463 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003464 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003465 * Returns what was read in allocated memory.
3466 * Returns NULL in case of error or timeout.
3467 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003468 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003469channel_read_block(
3470 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003471{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003472 char_u *buf;
3473 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003474 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003475 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003476 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003477 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003478
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003479 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003480 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003481
3482 while (TRUE)
3483 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003484 node = channel_peek(channel, part);
3485 if (node != NULL)
3486 {
3487 if (mode == MODE_RAW || (mode == MODE_NL
3488 && channel_first_nl(node) != NULL))
3489 /* got a complete message */
3490 break;
3491 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3492 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003493 /* If not blocking or nothing more is coming then return what we
3494 * have. */
3495 if (raw || fd == INVALID_FD)
3496 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003497 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003498
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003499 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003500 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003501 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003502 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003503 {
3504 ch_log(channel, "Timed out");
3505 return NULL;
3506 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003507 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003508 }
3509
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003510 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003511 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003512 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003513 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003514 }
3515 else
3516 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003517 char_u *p;
3518
3519 buf = node->rq_buffer;
3520 nl = channel_first_nl(node);
3521
3522 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003523 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003524 if (*p == NUL)
3525 *p = NL;
3526
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003527 if (nl == NULL)
3528 {
3529 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003530 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003531 }
3532 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003533 {
3534 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003535 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003536 *nl = NUL;
3537 }
3538 else
3539 {
3540 /* Copy the message into allocated memory and remove it from the
3541 * buffer. */
3542 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003543 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003544 }
3545 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003546 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003547 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003548 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003549}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003550
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003551/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003552 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003553 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003554 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003555 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003556 * In corner cases this can be called recursively, that is why ch_block_ids is
3557 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003558 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003559 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003560channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003561 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003562 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003563 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003564 int id,
3565 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003566{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003567 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003568 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003569 int timeout;
3570 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003571
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003572 ch_log(channel, "Blocking read JSON for id %d", id);
3573 if (id >= 0)
3574 channel_add_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003575 for (;;)
3576 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003577 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003578
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003579 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003580 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003581 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003582 if (id >= 0)
3583 channel_remove_block_id(chanpart, id);
3584 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003585 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003586 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003587
Bram Moolenaard7ece102016-02-02 23:23:02 +01003588 if (!more)
3589 {
3590 /* Handle any other messages in the queue. If done some more
3591 * messages may have arrived. */
3592 if (channel_parse_messages())
3593 continue;
3594
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003595 /* Wait for up to the timeout. If there was an incomplete message
3596 * use the deadline for that. */
3597 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003598 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003599 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003600#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003601 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3602#else
3603 {
3604 struct timeval now_tv;
3605
3606 gettimeofday(&now_tv, NULL);
3607 timeout = (chanpart->ch_deadline.tv_sec
3608 - now_tv.tv_sec) * 1000
3609 + (chanpart->ch_deadline.tv_usec
3610 - now_tv.tv_usec) / 1000
3611 + 1;
3612 }
3613#endif
3614 if (timeout < 0)
3615 {
3616 /* Something went wrong, channel_parse_json() didn't
3617 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003618 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003619 timeout = timeout_arg;
3620 }
3621 else if (timeout > timeout_arg)
3622 timeout = timeout_arg;
3623 }
3624 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003625 if (fd == INVALID_FD
3626 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003627 {
3628 if (timeout == timeout_arg)
3629 {
3630 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003631 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003632 break;
3633 }
3634 }
3635 else
3636 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003637 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003638 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003639 if (id >= 0)
3640 channel_remove_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003641 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003642}
3643
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003644/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003645 * Get the channel from the argument.
3646 * Returns NULL if the handle is invalid.
3647 * When "check_open" is TRUE check that the channel can be used.
3648 * When "reading" is TRUE "check_open" considers typeahead useful.
3649 * "part" is used to check typeahead, when PART_COUNT use the default part.
3650 */
3651 static channel_T *
3652get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3653{
3654 channel_T *channel = NULL;
3655 int has_readahead = FALSE;
3656
3657 if (tv->v_type == VAR_JOB)
3658 {
3659 if (tv->vval.v_job != NULL)
3660 channel = tv->vval.v_job->jv_channel;
3661 }
3662 else if (tv->v_type == VAR_CHANNEL)
3663 {
3664 channel = tv->vval.v_channel;
3665 }
3666 else
3667 {
3668 semsg(_(e_invarg2), tv_get_string(tv));
3669 return NULL;
3670 }
3671 if (channel != NULL && reading)
3672 has_readahead = channel_has_readahead(channel,
3673 part != PART_COUNT ? part : channel_part_read(channel));
3674
3675 if (check_open && (channel == NULL || (!channel_is_open(channel)
3676 && !(reading && has_readahead))))
3677 {
3678 emsg(_("E906: not an open channel"));
3679 return NULL;
3680 }
3681 return channel;
3682}
3683
3684/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003685 * Common for ch_read() and ch_readraw().
3686 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003687 static void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003688common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003689{
3690 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003691 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003692 jobopt_T opt;
3693 int mode;
3694 int timeout;
3695 int id = -1;
3696 typval_T *listtv = NULL;
3697
3698 /* return an empty string by default */
3699 rettv->v_type = VAR_STRING;
3700 rettv->vval.v_string = NULL;
3701
3702 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003703 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003704 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003705 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003706
Bram Moolenaar437905c2016-04-26 19:01:05 +02003707 if (opt.jo_set & JO_PART)
3708 part = opt.jo_part;
3709 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003710 if (channel != NULL)
3711 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003712 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003713 part = channel_part_read(channel);
3714 mode = channel_get_mode(channel, part);
3715 timeout = channel_get_timeout(channel, part);
3716 if (opt.jo_set & JO_TIMEOUT)
3717 timeout = opt.jo_timeout;
3718
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003719 if (blob)
3720 {
3721 int outlen = 0;
3722 char_u *p = channel_read_block(channel, part,
3723 timeout, TRUE, &outlen);
3724 if (p != NULL)
3725 {
3726 blob_T *b = blob_alloc();
3727
3728 if (b != NULL)
3729 {
3730 b->bv_ga.ga_len = outlen;
3731 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3732 blob_free(b);
3733 else
3734 {
3735 memcpy(b->bv_ga.ga_data, p, outlen);
3736 rettv_blob_set(rettv, b);
3737 }
3738 }
3739 vim_free(p);
3740 }
3741 }
3742 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003743 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003744 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003745 else
3746 {
3747 if (opt.jo_set & JO_ID)
3748 id = opt.jo_id;
3749 channel_read_json_block(channel, part, timeout, id, &listtv);
3750 if (listtv != NULL)
3751 {
3752 *rettv = *listtv;
3753 vim_free(listtv);
3754 }
3755 else
3756 {
3757 rettv->v_type = VAR_SPECIAL;
3758 rettv->vval.v_number = VVAL_NONE;
3759 }
3760 }
3761 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003762
3763theend:
3764 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003765}
3766
Bram Moolenaar4f974752019-02-17 17:44:42 +01003767# if defined(MSWIN) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003768 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003769/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003770 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003771 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003772 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003773 static channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003774channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003775{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003776 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003777 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003778
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003779 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003780 for (channel = first_channel; channel != NULL;
3781 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003782 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003783 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003784 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003785 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003786 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003787 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003788 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003789 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003790 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003791}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003792# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003793
Bram Moolenaar4f974752019-02-17 17:44:42 +01003794# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003795/*
3796 * Check the channels for anything that is ready to be read.
3797 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003798 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003799 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003800 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003801channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003802{
3803 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003804 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003805 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003806
3807 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3808 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003809 if (only_keep_open && !channel->ch_keep_open)
3810 continue;
3811
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003812 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003813 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003814 {
3815 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003816 if (fd != INVALID_FD)
3817 {
3818 int r = channel_wait(channel, fd, 0);
3819
3820 if (r == CW_READY)
3821 channel_read(channel, part, "channel_handle_events");
3822 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003823 ch_close_part_on_error(channel, part, TRUE,
3824 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003825 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003826 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003827 }
3828}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003829# endif
3830
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003831# if defined(FEAT_GUI) || defined(PROTO)
3832/*
3833 * Return TRUE when there is any channel with a keep_open flag.
3834 */
3835 int
3836channel_any_keep_open()
3837{
3838 channel_T *channel;
3839
3840 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3841 if (channel->ch_keep_open)
3842 return TRUE;
3843 return FALSE;
3844}
3845# endif
3846
Bram Moolenaard04a0202016-01-26 23:30:18 +01003847/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003848 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003849 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003850 */
3851 void
3852channel_set_nonblock(channel_T *channel, ch_part_T part)
3853{
3854 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003855 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003856
3857 if (fd != INVALID_FD)
3858 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003859#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003860 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003861
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003862 ioctlsocket(fd, FIONBIO, &val);
3863#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003864 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003865#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003866 ch_part->ch_nonblocking = TRUE;
3867 }
3868}
3869
3870/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003871 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003872 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003873 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003874 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003875 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003876channel_send(
3877 channel_T *channel,
3878 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003879 char_u *buf_arg,
3880 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003881 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003882{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003883 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003884 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003885 chanpart_T *ch_part = &channel->ch_part[part];
3886 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003887
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003888 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003889 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003890 {
3891 if (!channel->ch_error && fun != NULL)
3892 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003893 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003894 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003895 }
3896 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003897 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003898 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003899
Bram Moolenaar0b146882018-09-06 16:27:24 +02003900 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3901 channel_set_nonblock(channel, part);
3902
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003903 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003904 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003905 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003906 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003907 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003908 fprintf(log_fd, "'\n");
3909 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003910 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003911 }
3912
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003913 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003914 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003915 writeq_T *wq = &ch_part->ch_writeque;
3916 char_u *buf;
3917 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003918
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003919 if (wq->wq_next != NULL)
3920 {
3921 /* first write what was queued */
3922 buf = wq->wq_next->wq_ga.ga_data;
3923 len = wq->wq_next->wq_ga.ga_len;
3924 did_use_queue = TRUE;
3925 }
3926 else
3927 {
3928 if (len_arg == 0)
3929 /* nothing to write, called from channel_select_check() */
3930 return OK;
3931 buf = buf_arg;
3932 len = len_arg;
3933 }
3934
3935 if (part == PART_SOCK)
3936 res = sock_write(fd, (char *)buf, len);
3937 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003938 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003939 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003940#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003941 if (channel->ch_named_pipe && res < 0)
3942 {
3943 DisconnectNamedPipe((HANDLE)fd);
3944 ConnectNamedPipe((HANDLE)fd, NULL);
3945 }
3946#endif
3947 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003948 if (res < 0 && (errno == EWOULDBLOCK
3949#ifdef EAGAIN
3950 || errno == EAGAIN
3951#endif
3952 ))
3953 res = 0; /* nothing got written */
3954
3955 if (res >= 0 && ch_part->ch_nonblocking)
3956 {
3957 writeq_T *entry = wq->wq_next;
3958
3959 if (did_use_queue)
3960 ch_log(channel, "Sent %d bytes now", res);
3961 if (res == len)
3962 {
3963 /* Wrote all the buf[len] bytes. */
3964 if (entry != NULL)
3965 {
3966 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003967 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003968 continue;
3969 }
3970 if (did_use_queue)
3971 ch_log(channel, "Write queue empty");
3972 }
3973 else
3974 {
3975 /* Wrote only buf[res] bytes, can't write more now. */
3976 if (entry != NULL)
3977 {
3978 if (res > 0)
3979 {
3980 /* Remove the bytes that were written. */
3981 mch_memmove(entry->wq_ga.ga_data,
3982 (char *)entry->wq_ga.ga_data + res,
3983 len - res);
3984 entry->wq_ga.ga_len -= res;
3985 }
3986 buf = buf_arg;
3987 len = len_arg;
3988 }
3989 else
3990 {
3991 buf += res;
3992 len -= res;
3993 }
3994 ch_log(channel, "Adding %d bytes to the write queue", len);
3995
3996 /* Append the not written bytes of the argument to the write
3997 * buffer. Limit entries to 4000 bytes. */
3998 if (wq->wq_prev != NULL
3999 && wq->wq_prev->wq_ga.ga_len + len < 4000)
4000 {
4001 writeq_T *last = wq->wq_prev;
4002
4003 /* append to the last entry */
Bram Moolenaar0d071552019-07-08 22:04:03 +02004004 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004005 {
4006 mch_memmove((char *)last->wq_ga.ga_data
4007 + last->wq_ga.ga_len,
4008 buf, len);
4009 last->wq_ga.ga_len += len;
4010 }
4011 }
4012 else
4013 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004014 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004015
4016 if (last != NULL)
4017 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004018 last->wq_prev = wq->wq_prev;
4019 last->wq_next = NULL;
4020 if (wq->wq_prev == NULL)
4021 wq->wq_next = last;
4022 else
4023 wq->wq_prev->wq_next = last;
4024 wq->wq_prev = last;
4025 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004026 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004027 {
4028 mch_memmove(last->wq_ga.ga_data, buf, len);
4029 last->wq_ga.ga_len = len;
4030 }
4031 }
4032 }
4033 }
4034 }
4035 else if (res != len)
4036 {
4037 if (!channel->ch_error && fun != NULL)
4038 {
4039 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004040 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004041 }
4042 channel->ch_error = TRUE;
4043 return FAIL;
4044 }
4045
4046 channel->ch_error = FALSE;
4047 return OK;
4048 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004049}
4050
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004051/*
4052 * Common for "ch_sendexpr()" and "ch_sendraw()".
4053 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004054 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055 * Otherwise returns NULL.
4056 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004057 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004058send_common(
4059 typval_T *argvars,
4060 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004061 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062 int id,
4063 int eval,
4064 jobopt_T *opt,
4065 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004066 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004067{
4068 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004069 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004071 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004072 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004073 if (channel == NULL)
4074 return NULL;
4075 part_send = channel_part_send(channel);
4076 *part_read = channel_part_read(channel);
4077
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004078 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004079 return NULL;
4080
4081 /* Set the callback. An empty callback means no callback and not reading
4082 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4083 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004084 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 {
4086 if (eval)
4087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004088 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004089 return NULL;
4090 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004091 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004092 }
4093
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004094 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004095 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004096 return channel;
4097 return NULL;
4098}
4099
4100/*
4101 * common for "ch_evalexpr()" and "ch_sendexpr()"
4102 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004103 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004104ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4105{
4106 char_u *text;
4107 typval_T *listtv;
4108 channel_T *channel;
4109 int id;
4110 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004111 ch_part_T part_send;
4112 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113 jobopt_T opt;
4114 int timeout;
4115
4116 /* return an empty string by default */
4117 rettv->v_type = VAR_STRING;
4118 rettv->vval.v_string = NULL;
4119
Bram Moolenaar437905c2016-04-26 19:01:05 +02004120 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004121 if (channel == NULL)
4122 return;
4123 part_send = channel_part_send(channel);
4124
4125 ch_mode = channel_get_mode(channel, part_send);
4126 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004128 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004129 return;
4130 }
4131
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004132 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004133 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004134 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004135 if (text == NULL)
4136 return;
4137
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004138 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004139 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4140 vim_free(text);
4141 if (channel != NULL && eval)
4142 {
4143 if (opt.jo_set & JO_TIMEOUT)
4144 timeout = opt.jo_timeout;
4145 else
4146 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004147 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4148 == OK)
4149 {
4150 list_T *list = listtv->vval.v_list;
4151
4152 /* Move the item from the list and then change the type to
4153 * avoid the value being freed. */
4154 *rettv = list->lv_last->li_tv;
4155 list->lv_last->li_tv.v_type = VAR_NUMBER;
4156 free_tv(listtv);
4157 }
4158 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004159 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004160}
4161
4162/*
4163 * common for "ch_evalraw()" and "ch_sendraw()"
4164 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004165 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004166ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4167{
4168 char_u buf[NUMBUFLEN];
4169 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004170 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004171 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004172 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004173 jobopt_T opt;
4174 int timeout;
4175
4176 /* return an empty string by default */
4177 rettv->v_type = VAR_STRING;
4178 rettv->vval.v_string = NULL;
4179
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004180 if (argvars[1].v_type == VAR_BLOB)
4181 {
4182 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4183 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4184 }
4185 else
4186 {
4187 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004188 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004189 }
4190 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004191 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4192 if (channel != NULL && eval)
4193 {
4194 if (opt.jo_set & JO_TIMEOUT)
4195 timeout = opt.jo_timeout;
4196 else
4197 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004198 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004199 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004201 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004202}
4203
Bram Moolenaarf3360612017-10-01 16:21:31 +02004204# define KEEP_OPEN_TIME 20 /* msec */
4205
Bram Moolenaard04a0202016-01-26 23:30:18 +01004206# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004207/*
4208 * Add open channels to the poll struct.
4209 * Return the adjusted struct index.
4210 * The type of "fds" is hidden to avoid problems with the function proto.
4211 */
4212 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004213channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004214{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004215 int nfd = nfd_in;
4216 channel_T *channel;
4217 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004218 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004219
Bram Moolenaar77073442016-02-13 23:23:53 +01004220 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004221 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004222 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004223 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004224 chanpart_T *ch_part = &channel->ch_part[part];
4225
4226 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004227 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004228 if (channel->ch_keep_open)
4229 {
4230 /* For unknown reason poll() returns immediately for a
4231 * keep-open channel. Instead of adding it to the fds add
4232 * a short timeout and check, like polling. */
4233 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4234 *towait = KEEP_OPEN_TIME;
4235 }
4236 else
4237 {
4238 ch_part->ch_poll_idx = nfd;
4239 fds[nfd].fd = ch_part->ch_fd;
4240 fds[nfd].events = POLLIN;
4241 nfd++;
4242 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004243 }
4244 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004245 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004246 }
4247 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004248
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004249 nfd = channel_fill_poll_write(nfd, fds);
4250
Bram Moolenaare0874f82016-01-24 20:36:41 +01004251 return nfd;
4252}
4253
4254/*
4255 * The type of "fds" is hidden to avoid problems with the function proto.
4256 */
4257 int
4258channel_poll_check(int ret_in, void *fds_in)
4259{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004260 int ret = ret_in;
4261 channel_T *channel;
4262 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004263 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004264 int idx;
4265 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004266
Bram Moolenaar77073442016-02-13 23:23:53 +01004267 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004268 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004269 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004270 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004271 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004272
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004273 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004274 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004275 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004276 --ret;
4277 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004278 else if (channel->ch_part[part].ch_fd != INVALID_FD
4279 && channel->ch_keep_open)
4280 {
4281 /* polling a keep-open channel */
4282 channel_read(channel, part, "channel_poll_check_keep_open");
4283 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004284 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004285
4286 in_part = &channel->ch_part[PART_IN];
4287 idx = in_part->ch_poll_idx;
4288 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4289 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004290 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004291 --ret;
4292 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004293 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004294
4295 return ret;
4296}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004297# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004298
Bram Moolenaar4f974752019-02-17 17:44:42 +01004299# if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004300
Bram Moolenaare0874f82016-01-24 20:36:41 +01004301/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004302 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004303 */
4304 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004305channel_select_setup(
4306 int maxfd_in,
4307 void *rfds_in,
4308 void *wfds_in,
4309 struct timeval *tv,
4310 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004311{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004312 int maxfd = maxfd_in;
4313 channel_T *channel;
4314 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004315 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004316 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004317
Bram Moolenaar77073442016-02-13 23:23:53 +01004318 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004319 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004320 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004321 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004322 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004323
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004324 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004325 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004326 if (channel->ch_keep_open)
4327 {
4328 /* For unknown reason select() returns immediately for a
4329 * keep-open channel. Instead of adding it to the rfds add
4330 * a short timeout and check, like polling. */
4331 if (*tvp == NULL || tv->tv_sec > 0
4332 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4333 {
4334 *tvp = tv;
4335 tv->tv_sec = 0;
4336 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4337 }
4338 }
4339 else
4340 {
4341 FD_SET((int)fd, rfds);
4342 if (maxfd < (int)fd)
4343 maxfd = (int)fd;
4344 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004345 }
4346 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004347 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004348
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004349 maxfd = channel_fill_wfds(maxfd, wfds);
4350
Bram Moolenaare0874f82016-01-24 20:36:41 +01004351 return maxfd;
4352}
4353
4354/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004355 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004356 */
4357 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004358channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004359{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004360 int ret = ret_in;
4361 channel_T *channel;
4362 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004363 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004364 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004365 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004366
Bram Moolenaar77073442016-02-13 23:23:53 +01004367 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004368 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004369 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004370 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004371 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004372
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004373 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004374 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004375 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004376 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004377 --ret;
4378 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004379 else if (fd != INVALID_FD && channel->ch_keep_open)
4380 {
4381 /* polling a keep-open channel */
4382 channel_read(channel, part, "channel_select_check_keep_open");
4383 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004384 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004385
4386 in_part = &channel->ch_part[PART_IN];
4387 if (ret > 0 && in_part->ch_fd != INVALID_FD
4388 && FD_ISSET(in_part->ch_fd, wfds))
4389 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004390 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004391 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004392 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004393 --ret;
4394 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004395 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004396
4397 return ret;
4398}
Bram Moolenaar4f974752019-02-17 17:44:42 +01004399# endif /* !MSWIN && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004400
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004401/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004402 * Execute queued up commands.
4403 * Invoked from the main loop when it's safe to execute received commands.
4404 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004405 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004406 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004407channel_parse_messages(void)
4408{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004409 channel_T *channel = first_channel;
4410 int ret = FALSE;
4411 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004412 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004413#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004414 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004415
4416 ELAPSED_INIT(start_tv);
4417#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004418
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004419 ++safe_to_invoke_callback;
4420
Bram Moolenaard0b65022016-03-06 21:50:33 +01004421 /* Only do this message when another message was given, otherwise we get
4422 * lots of them. */
4423 if (did_log_msg)
4424 {
4425 ch_log(NULL, "looking for messages on channels");
4426 did_log_msg = FALSE;
4427 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004428 while (channel != NULL)
4429 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004430 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004431 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004432 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004433 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004434 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004435 channel = first_channel;
4436 continue;
4437 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004438 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004439 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004440 if (channel->ch_killing)
4441 {
4442 channel_free_contents(channel);
4443 channel->ch_job->jv_channel = NULL;
4444 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004445 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004446 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004447 channel = first_channel;
4448 continue;
4449 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004450 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004451 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004452 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004453 channel_free(channel);
4454 channel = first_channel;
4455 part = PART_SOCK;
4456 continue;
4457 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004458 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004459 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004460 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004461 /* Increase the refcount, in case the handler causes the channel
4462 * to be unreferenced or closed. */
4463 ++channel->ch_refcount;
4464 r = may_invoke_callback(channel, part);
4465 if (r == OK)
4466 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004467 if (channel_unref(channel) || (r == OK
4468#ifdef ELAPSED_FUNC
4469 /* Limit the time we loop here to 100 msec, otherwise
4470 * Vim becomes unresponsive when the callback takes
4471 * more than a bit of time. */
4472 && ELAPSED_FUNC(start_tv) < 100L
4473#endif
4474 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004475 {
4476 /* channel was freed or something was done, start over */
4477 channel = first_channel;
4478 part = PART_SOCK;
4479 continue;
4480 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004481 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004482 if (part < PART_ERR)
4483 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004484 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004485 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004486 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004487 part = PART_SOCK;
4488 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004489 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004490
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004491 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004492 {
4493 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004494 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004495 }
4496
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004497 --safe_to_invoke_callback;
4498
Bram Moolenaard7ece102016-02-02 23:23:02 +01004499 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004500}
4501
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004502/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004503 * Return TRUE if any channel has readahead. That means we should not block on
4504 * waiting for input.
4505 */
4506 int
4507channel_any_readahead(void)
4508{
4509 channel_T *channel = first_channel;
4510 ch_part_T part = PART_SOCK;
4511
4512 while (channel != NULL)
4513 {
4514 if (channel_has_readahead(channel, part))
4515 return TRUE;
4516 if (part < PART_ERR)
4517 ++part;
4518 else
4519 {
4520 channel = channel->ch_next;
4521 part = PART_SOCK;
4522 }
4523 }
4524 return FALSE;
4525}
4526
4527/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004528 * Mark references to lists used in channels.
4529 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004530 int
4531set_ref_in_channel(int copyID)
4532{
Bram Moolenaar77073442016-02-13 23:23:53 +01004533 int abort = FALSE;
4534 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004535 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004536
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004537 for (channel = first_channel; !abort && channel != NULL;
4538 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004539 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004540 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004541 tv.v_type = VAR_CHANNEL;
4542 tv.vval.v_channel = channel;
4543 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004544 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004545 return abort;
4546}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004547
4548/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004549 * Return the "part" to write to for "channel".
4550 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004551 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004552channel_part_send(channel_T *channel)
4553{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004554 if (channel->CH_SOCK_FD == INVALID_FD)
4555 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004556 return PART_SOCK;
4557}
4558
4559/*
4560 * Return the default "part" to read from for "channel".
4561 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004562 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004563channel_part_read(channel_T *channel)
4564{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004565 if (channel->CH_SOCK_FD == INVALID_FD)
4566 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004567 return PART_SOCK;
4568}
4569
4570/*
4571 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004572 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004573 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004574 static ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004575channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004576{
Bram Moolenaar77073442016-02-13 23:23:53 +01004577 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004578 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004579 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004580}
4581
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004582/*
4583 * Return the timeout of "channel"/"part"
4584 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004585 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004586channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004587{
4588 return channel->ch_part[part].ch_timeout;
4589}
4590
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004591 static int
4592handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4593{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004594 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004595
4596 opt->jo_set |= jo;
4597 if (STRCMP(val, "nl") == 0)
4598 *modep = MODE_NL;
4599 else if (STRCMP(val, "raw") == 0)
4600 *modep = MODE_RAW;
4601 else if (STRCMP(val, "js") == 0)
4602 *modep = MODE_JS;
4603 else if (STRCMP(val, "json") == 0)
4604 *modep = MODE_JSON;
4605 else
4606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004607 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004608 return FAIL;
4609 }
4610 return OK;
4611}
4612
4613 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004614handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004615{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004616 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004617
4618 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4619 if (STRCMP(val, "null") == 0)
4620 opt->jo_io[part] = JIO_NULL;
4621 else if (STRCMP(val, "pipe") == 0)
4622 opt->jo_io[part] = JIO_PIPE;
4623 else if (STRCMP(val, "file") == 0)
4624 opt->jo_io[part] = JIO_FILE;
4625 else if (STRCMP(val, "buffer") == 0)
4626 opt->jo_io[part] = JIO_BUFFER;
4627 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4628 opt->jo_io[part] = JIO_OUT;
4629 else
4630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004631 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004632 return FAIL;
4633 }
4634 return OK;
4635}
4636
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004637/*
4638 * Clear a jobopt_T before using it.
4639 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004640 void
4641clear_job_options(jobopt_T *opt)
4642{
4643 vim_memset(opt, 0, sizeof(jobopt_T));
4644}
4645
4646/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004647 * Free any members of a jobopt_T.
4648 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004649 static void
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004650free_job_options(jobopt_T *opt)
4651{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004652 if (opt->jo_callback.cb_partial != NULL)
4653 partial_unref(opt->jo_callback.cb_partial);
4654 else if (opt->jo_callback.cb_name != NULL)
4655 func_unref(opt->jo_callback.cb_name);
4656 if (opt->jo_out_cb.cb_partial != NULL)
4657 partial_unref(opt->jo_out_cb.cb_partial);
4658 else if (opt->jo_out_cb.cb_name != NULL)
4659 func_unref(opt->jo_out_cb.cb_name);
4660 if (opt->jo_err_cb.cb_partial != NULL)
4661 partial_unref(opt->jo_err_cb.cb_partial);
4662 else if (opt->jo_err_cb.cb_name != NULL)
4663 func_unref(opt->jo_err_cb.cb_name);
4664 if (opt->jo_close_cb.cb_partial != NULL)
4665 partial_unref(opt->jo_close_cb.cb_partial);
4666 else if (opt->jo_close_cb.cb_name != NULL)
4667 func_unref(opt->jo_close_cb.cb_name);
4668 if (opt->jo_exit_cb.cb_partial != NULL)
4669 partial_unref(opt->jo_exit_cb.cb_partial);
4670 else if (opt->jo_exit_cb.cb_name != NULL)
4671 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004672 if (opt->jo_env != NULL)
4673 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004674}
4675
4676/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004677 * Get the PART_ number from the first character of an option name.
4678 */
4679 static int
4680part_from_char(int c)
4681{
4682 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4683}
4684
4685/*
4686 * Get the option entries from the dict in "tv", parse them and put the result
4687 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004688 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004689 * If an option value is invalid return FAIL.
4690 */
4691 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004692get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004693{
4694 typval_T *item;
4695 char_u *val;
4696 dict_T *dict;
4697 int todo;
4698 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004699 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004700
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004701 if (tv->v_type == VAR_UNKNOWN)
4702 return OK;
4703 if (tv->v_type != VAR_DICT)
4704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004705 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004706 return FAIL;
4707 }
4708 dict = tv->vval.v_dict;
4709 if (dict == NULL)
4710 return OK;
4711
4712 todo = (int)dict->dv_hashtab.ht_used;
4713 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4714 if (!HASHITEM_EMPTY(hi))
4715 {
4716 item = &dict_lookup(hi)->di_tv;
4717
4718 if (STRCMP(hi->hi_key, "mode") == 0)
4719 {
4720 if (!(supported & JO_MODE))
4721 break;
4722 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4723 return FAIL;
4724 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004725 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004726 {
4727 if (!(supported & JO_IN_MODE))
4728 break;
4729 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4730 == FAIL)
4731 return FAIL;
4732 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004733 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004734 {
4735 if (!(supported & JO_OUT_MODE))
4736 break;
4737 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4738 == FAIL)
4739 return FAIL;
4740 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004741 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004742 {
4743 if (!(supported & JO_ERR_MODE))
4744 break;
4745 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4746 == FAIL)
4747 return FAIL;
4748 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004749 else if (STRCMP(hi->hi_key, "noblock") == 0)
4750 {
4751 if (!(supported & JO_MODE))
4752 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004753 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004754 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004755 else if (STRCMP(hi->hi_key, "in_io") == 0
4756 || STRCMP(hi->hi_key, "out_io") == 0
4757 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004758 {
4759 if (!(supported & JO_OUT_IO))
4760 break;
4761 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4762 return FAIL;
4763 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004764 else if (STRCMP(hi->hi_key, "in_name") == 0
4765 || STRCMP(hi->hi_key, "out_name") == 0
4766 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004767 {
4768 part = part_from_char(*hi->hi_key);
4769
4770 if (!(supported & JO_OUT_IO))
4771 break;
4772 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4773 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004774 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004775 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004776 else if (STRCMP(hi->hi_key, "pty") == 0)
4777 {
4778 if (!(supported & JO_MODE))
4779 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004780 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004781 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004782 else if (STRCMP(hi->hi_key, "in_buf") == 0
4783 || STRCMP(hi->hi_key, "out_buf") == 0
4784 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004785 {
4786 part = part_from_char(*hi->hi_key);
4787
4788 if (!(supported & JO_OUT_IO))
4789 break;
4790 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004791 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004792 if (opt->jo_io_buf[part] <= 0)
4793 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004794 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004795 return FAIL;
4796 }
4797 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004799 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004800 return FAIL;
4801 }
4802 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004803 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4804 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4805 {
4806 part = part_from_char(*hi->hi_key);
4807
4808 if (!(supported & JO_OUT_IO))
4809 break;
4810 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004811 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004812 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004813 else if (STRCMP(hi->hi_key, "out_msg") == 0
4814 || STRCMP(hi->hi_key, "err_msg") == 0)
4815 {
4816 part = part_from_char(*hi->hi_key);
4817
4818 if (!(supported & JO_OUT_IO))
4819 break;
4820 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004821 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004822 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004823 else if (STRCMP(hi->hi_key, "in_top") == 0
4824 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004825 {
4826 linenr_T *lp;
4827
4828 if (!(supported & JO_OUT_IO))
4829 break;
4830 if (hi->hi_key[3] == 't')
4831 {
4832 lp = &opt->jo_in_top;
4833 opt->jo_set |= JO_IN_TOP;
4834 }
4835 else
4836 {
4837 lp = &opt->jo_in_bot;
4838 opt->jo_set |= JO_IN_BOT;
4839 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004840 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004841 if (*lp < 0)
4842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004843 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004844 return FAIL;
4845 }
4846 }
4847 else if (STRCMP(hi->hi_key, "channel") == 0)
4848 {
4849 if (!(supported & JO_OUT_IO))
4850 break;
4851 opt->jo_set |= JO_CHANNEL;
4852 if (item->v_type != VAR_CHANNEL)
4853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004854 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004855 return FAIL;
4856 }
4857 opt->jo_channel = item->vval.v_channel;
4858 }
4859 else if (STRCMP(hi->hi_key, "callback") == 0)
4860 {
4861 if (!(supported & JO_CALLBACK))
4862 break;
4863 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004864 opt->jo_callback = get_callback(item);
4865 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004867 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004868 return FAIL;
4869 }
4870 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004871 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004872 {
4873 if (!(supported & JO_OUT_CALLBACK))
4874 break;
4875 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004876 opt->jo_out_cb = get_callback(item);
4877 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004879 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004880 return FAIL;
4881 }
4882 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004883 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004884 {
4885 if (!(supported & JO_ERR_CALLBACK))
4886 break;
4887 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004888 opt->jo_err_cb = get_callback(item);
4889 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004891 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004892 return FAIL;
4893 }
4894 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004895 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004896 {
4897 if (!(supported & JO_CLOSE_CALLBACK))
4898 break;
4899 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004900 opt->jo_close_cb = get_callback(item);
4901 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004903 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004904 return FAIL;
4905 }
4906 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004907 else if (STRCMP(hi->hi_key, "drop") == 0)
4908 {
4909 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004910 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004911
4912 if (STRCMP(val, "never") == 0)
4913 never = TRUE;
4914 else if (STRCMP(val, "auto") != 0)
4915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004916 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004917 return FAIL;
4918 }
4919 opt->jo_drop_never = never;
4920 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004921 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4922 {
4923 if (!(supported & JO_EXIT_CB))
4924 break;
4925 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004926 opt->jo_exit_cb = get_callback(item);
4927 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004929 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004930 return FAIL;
4931 }
4932 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004933#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004934 else if (STRCMP(hi->hi_key, "term_name") == 0)
4935 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004936 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004937 break;
4938 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004940 if (opt->jo_term_name == NULL)
4941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004942 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004943 return FAIL;
4944 }
4945 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004946 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4947 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004948 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004949 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004950 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004951 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004953 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004954 return FAIL;
4955 }
4956 opt->jo_set2 |= JO2_TERM_FINISH;
4957 opt->jo_term_finish = *val;
4958 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004959 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4960 {
4961 char_u *p;
4962
4963 if (!(supported2 & JO2_TERM_OPENCMD))
4964 break;
4965 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004966 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004967 if (p != NULL)
4968 {
4969 /* Must have %d and no other %. */
4970 p = vim_strchr(p, '%');
4971 if (p != NULL && (p[1] != 'd'
4972 || vim_strchr(p + 2, '%') != NULL))
4973 p = NULL;
4974 }
4975 if (p == NULL)
4976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004977 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004978 return FAIL;
4979 }
4980 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004981 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4982 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004983 char_u *p;
4984
4985 if (!(supported2 & JO2_EOF_CHARS))
4986 break;
4987 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004988 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004989 if (p == NULL)
4990 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004991 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004992 return FAIL;
4993 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004994 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004995 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4996 {
4997 if (!(supported2 & JO2_TERM_ROWS))
4998 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004999 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005000 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005001 }
5002 else if (STRCMP(hi->hi_key, "term_cols") == 0)
5003 {
5004 if (!(supported2 & JO2_TERM_COLS))
5005 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005006 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005007 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005008 }
5009 else if (STRCMP(hi->hi_key, "vertical") == 0)
5010 {
5011 if (!(supported2 & JO2_VERTICAL))
5012 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005013 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005014 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005015 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005016 else if (STRCMP(hi->hi_key, "curwin") == 0)
5017 {
5018 if (!(supported2 & JO2_CURWIN))
5019 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005020 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005021 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005022 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005023 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5024 {
5025 int nr;
5026
5027 if (!(supported2 & JO2_CURWIN))
5028 break;
5029 opt->jo_set2 |= JO2_BUFNR;
5030 nr = tv_get_number(item);
5031 if (nr <= 0)
5032 {
5033 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5034 return FAIL;
5035 }
5036 opt->jo_bufnr_buf = buflist_findnr(nr);
5037 if (opt->jo_bufnr_buf == NULL)
5038 {
5039 semsg(_(e_nobufnr), (long)nr);
5040 return FAIL;
5041 }
5042 if (opt->jo_bufnr_buf->b_nwindows == 0
5043 || opt->jo_bufnr_buf->b_term == NULL)
5044 {
5045 semsg(_(e_invarg2), "bufnr");
5046 return FAIL;
5047 }
5048 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005049 else if (STRCMP(hi->hi_key, "hidden") == 0)
5050 {
5051 if (!(supported2 & JO2_HIDDEN))
5052 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005053 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005054 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005055 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005056 else if (STRCMP(hi->hi_key, "norestore") == 0)
5057 {
5058 if (!(supported2 & JO2_NORESTORE))
5059 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005060 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005061 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005062 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005063 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5064 {
5065 if (!(supported2 & JO2_TERM_KILL))
5066 break;
5067 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005068 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005069 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005070 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005071 {
5072 char_u *p;
5073
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005074 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005075 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005076 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005077 p = tv_get_string_chk(item);
5078 if (p == NULL)
5079 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005080 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005081 return FAIL;
5082 }
5083 // Allow empty string, "winpty", "conpty".
5084 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5085 || STRCMP(p, "conpty") == 0))
5086 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005087 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005088 return FAIL;
5089 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005090 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005091 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005092# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5093 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005095 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005096 listitem_T *li;
5097 long_u rgb[16];
5098
5099 if (!(supported2 & JO2_ANSI_COLORS))
5100 break;
5101
5102 if (item == NULL || item->v_type != VAR_LIST
5103 || item->vval.v_list == NULL)
5104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005105 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005106 return FAIL;
5107 }
5108
5109 li = item->vval.v_list->lv_first;
5110 for (; li != NULL && n < 16; li = li->li_next, n++)
5111 {
5112 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005113 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005115 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005116 if (color_name == NULL)
5117 return FAIL;
5118
5119 guicolor = GUI_GET_COLOR(color_name);
5120 if (guicolor == INVALCOLOR)
5121 return FAIL;
5122
5123 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5124 }
5125
5126 if (n != 16 || li != NULL)
5127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005128 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005129 return FAIL;
5130 }
5131
5132 opt->jo_set2 |= JO2_ANSI_COLORS;
5133 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5134 }
5135# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005136#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005137 else if (STRCMP(hi->hi_key, "env") == 0)
5138 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005139 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005140 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005141 if (item->v_type != VAR_DICT)
5142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005143 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005144 return FAIL;
5145 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005146 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005147 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005148 if (opt->jo_env != NULL)
5149 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005150 }
5151 else if (STRCMP(hi->hi_key, "cwd") == 0)
5152 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005153 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005154 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005155 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005156 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005157#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005158 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5159#endif
5160 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005162 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005163 return FAIL;
5164 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005165 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005166 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005167 else if (STRCMP(hi->hi_key, "waittime") == 0)
5168 {
5169 if (!(supported & JO_WAITTIME))
5170 break;
5171 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005172 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005173 }
5174 else if (STRCMP(hi->hi_key, "timeout") == 0)
5175 {
5176 if (!(supported & JO_TIMEOUT))
5177 break;
5178 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005179 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005180 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005181 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005182 {
5183 if (!(supported & JO_OUT_TIMEOUT))
5184 break;
5185 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005186 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005187 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005188 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005189 {
5190 if (!(supported & JO_ERR_TIMEOUT))
5191 break;
5192 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005193 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005194 }
5195 else if (STRCMP(hi->hi_key, "part") == 0)
5196 {
5197 if (!(supported & JO_PART))
5198 break;
5199 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005200 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005201 if (STRCMP(val, "err") == 0)
5202 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005203 else if (STRCMP(val, "out") == 0)
5204 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005205 else
5206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005207 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005208 return FAIL;
5209 }
5210 }
5211 else if (STRCMP(hi->hi_key, "id") == 0)
5212 {
5213 if (!(supported & JO_ID))
5214 break;
5215 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005216 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005217 }
5218 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5219 {
5220 if (!(supported & JO_STOPONEXIT))
5221 break;
5222 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005223 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005224 opt->jo_soe_buf);
5225 if (opt->jo_stoponexit == NULL)
5226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005227 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005228 return FAIL;
5229 }
5230 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005231 else if (STRCMP(hi->hi_key, "block_write") == 0)
5232 {
5233 if (!(supported & JO_BLOCK_WRITE))
5234 break;
5235 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005236 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005237 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005238 else
5239 break;
5240 --todo;
5241 }
5242 if (todo > 0)
5243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005244 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005245 return FAIL;
5246 }
5247
5248 return OK;
5249}
5250
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005251static job_T *first_job = NULL;
5252
5253 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005254job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005255{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005256 int i;
5257
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005258 ch_log(job->jv_channel, "Freeing job");
5259 if (job->jv_channel != NULL)
5260 {
5261 /* The link from the channel to the job doesn't count as a reference,
5262 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005263 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005264 * NULL the reference. We don't set ch_job_killed, unreferencing the
5265 * job doesn't mean it stops running. */
5266 job->jv_channel->ch_job = NULL;
5267 channel_unref(job->jv_channel);
5268 }
5269 mch_clear_job(job);
5270
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005271 vim_free(job->jv_tty_in);
5272 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005273 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005274#ifdef UNIX
5275 vim_free(job->jv_termsig);
5276#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005277#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005278 vim_free(job->jv_tty_type);
5279#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005280 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005281 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005282 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005283 for (i = 0; job->jv_argv[i] != NULL; i++)
5284 vim_free(job->jv_argv[i]);
5285 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005286 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005287}
5288
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005289/*
5290 * Remove "job" from the list of jobs.
5291 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005292 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005293job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005294{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005295 if (job->jv_next != NULL)
5296 job->jv_next->jv_prev = job->jv_prev;
5297 if (job->jv_prev == NULL)
5298 first_job = job->jv_next;
5299 else
5300 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005301}
5302
5303 static void
5304job_free_job(job_T *job)
5305{
5306 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005307 vim_free(job);
5308}
5309
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005310 static void
5311job_free(job_T *job)
5312{
5313 if (!in_free_unref_items)
5314 {
5315 job_free_contents(job);
5316 job_free_job(job);
5317 }
5318}
5319
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005320static job_T *jobs_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005321
5322/*
5323 * Put "job" in a list to be freed later, when it's no longer referenced.
5324 */
5325 static void
5326job_free_later(job_T *job)
5327{
5328 job_unlink(job);
5329 job->jv_next = jobs_to_free;
5330 jobs_to_free = job;
5331}
5332
5333 static void
5334free_jobs_to_free_later(void)
5335{
5336 job_T *job;
5337
5338 while (jobs_to_free != NULL)
5339 {
5340 job = jobs_to_free;
5341 jobs_to_free = job->jv_next;
5342 job_free_contents(job);
5343 vim_free(job);
5344 }
5345}
5346
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005347#if defined(EXITFREE) || defined(PROTO)
5348 void
5349job_free_all(void)
5350{
5351 while (first_job != NULL)
5352 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005353 free_jobs_to_free_later();
5354
5355# ifdef FEAT_TERMINAL
5356 free_unused_terminals();
5357# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005358}
5359#endif
5360
5361/*
5362 * Return TRUE if we need to check if the process of "job" has ended.
5363 */
5364 static int
5365job_need_end_check(job_T *job)
5366{
5367 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005368 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005369}
5370
5371/*
5372 * Return TRUE if the channel of "job" is still useful.
5373 */
5374 static int
5375job_channel_still_useful(job_T *job)
5376{
5377 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5378}
5379
5380/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005381 * Return TRUE if the channel of "job" is closeable.
5382 */
5383 static int
5384job_channel_can_close(job_T *job)
5385{
5386 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5387}
5388
5389/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005390 * Return TRUE if the job should not be freed yet. Do not free the job when
5391 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5392 * or when the associated channel will do something with the job output.
5393 */
5394 static int
5395job_still_useful(job_T *job)
5396{
5397 return job_need_end_check(job) || job_channel_still_useful(job);
5398}
5399
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005400#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005401/*
5402 * Return TRUE when there is any running job that we care about.
5403 */
5404 int
5405job_any_running()
5406{
5407 job_T *job;
5408
5409 for (job = first_job; job != NULL; job = job->jv_next)
5410 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005411 {
5412 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005413 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005414 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005415 return FALSE;
5416}
5417#endif
5418
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005419#if !defined(USE_ARGV) || defined(PROTO)
5420/*
5421 * Escape one argument for an external command.
5422 * Returns the escaped string in allocated memory. NULL when out of memory.
5423 */
5424 static char_u *
5425win32_escape_arg(char_u *arg)
5426{
5427 int slen, dlen;
5428 int escaping = 0;
5429 int i;
5430 char_u *s, *d;
5431 char_u *escaped_arg;
5432 int has_spaces = FALSE;
5433
5434 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005435 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005436 dlen = slen;
5437 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5438 {
5439 if (*s == '"' || *s == '\\')
5440 ++dlen;
5441 if (*s == ' ' || *s == '\t')
5442 has_spaces = TRUE;
5443 }
5444
5445 if (has_spaces)
5446 dlen += 2;
5447
5448 if (dlen == slen)
5449 return vim_strsave(arg);
5450
5451 /* Allocate memory for the result and fill it. */
5452 escaped_arg = alloc(dlen + 1);
5453 if (escaped_arg == NULL)
5454 return NULL;
5455 memset(escaped_arg, 0, dlen+1);
5456
5457 d = escaped_arg;
5458
5459 if (has_spaces)
5460 *d++ = '"';
5461
5462 for (s = arg; *s != NUL;)
5463 {
5464 switch (*s)
5465 {
5466 case '"':
5467 for (i = 0; i < escaping; i++)
5468 *d++ = '\\';
5469 escaping = 0;
5470 *d++ = '\\';
5471 *d++ = *s++;
5472 break;
5473 case '\\':
5474 escaping++;
5475 *d++ = *s++;
5476 break;
5477 default:
5478 escaping = 0;
5479 MB_COPY_CHAR(s, d);
5480 break;
5481 }
5482 }
5483
5484 /* add terminating quote and finish with a NUL */
5485 if (has_spaces)
5486 {
5487 for (i = 0; i < escaping; i++)
5488 *d++ = '\\';
5489 *d++ = '"';
5490 }
5491 *d = NUL;
5492
5493 return escaped_arg;
5494}
5495
5496/*
5497 * Build a command line from a list, taking care of escaping.
5498 * The result is put in gap->ga_data.
5499 * Returns FAIL when out of memory.
5500 */
5501 int
5502win32_build_cmd(list_T *l, garray_T *gap)
5503{
5504 listitem_T *li;
5505 char_u *s;
5506
5507 for (li = l->lv_first; li != NULL; li = li->li_next)
5508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005509 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005510 if (s == NULL)
5511 return FAIL;
5512 s = win32_escape_arg(s);
5513 if (s == NULL)
5514 return FAIL;
5515 ga_concat(gap, s);
5516 vim_free(s);
5517 if (li->li_next != NULL)
5518 ga_append(gap, ' ');
5519 }
5520 return OK;
5521}
5522#endif
5523
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005524/*
5525 * NOTE: Must call job_cleanup() only once right after the status of "job"
5526 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5527 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005528 * If the job is no longer used it will be removed from the list of jobs, and
5529 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005530 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005531 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005532job_cleanup(job_T *job)
5533{
5534 if (job->jv_status != JOB_ENDED)
5535 return;
5536
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005537 /* Ready to cleanup the job. */
5538 job->jv_status = JOB_FINISHED;
5539
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005540 /* When only channel-in is kept open, close explicitly. */
5541 if (job->jv_channel != NULL)
5542 ch_close_part(job->jv_channel, PART_IN);
5543
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005544 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005545 {
5546 typval_T argv[3];
5547 typval_T rettv;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005548
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005549 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005550 ch_log(job->jv_channel, "Invoking exit callback %s",
5551 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005552 ++job->jv_refcount;
5553 argv[0].v_type = VAR_JOB;
5554 argv[0].vval.v_job = job;
5555 argv[1].v_type = VAR_NUMBER;
5556 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005557 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005558 clear_tv(&rettv);
5559 --job->jv_refcount;
5560 channel_need_redraw = TRUE;
5561 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005562
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005563 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5564 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005565
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005566 // Do not free the job in case the close callback of the associated channel
5567 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005568 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005569 // The job was already unreferenced and the associated channel was
5570 // detached, now that it ended it can be freed. However, a caller might
5571 // still use it, thus free it a bit later.
5572 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005573}
5574
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005575/*
5576 * Mark references in jobs that are still useful.
5577 */
5578 int
5579set_ref_in_job(int copyID)
5580{
5581 int abort = FALSE;
5582 job_T *job;
5583 typval_T tv;
5584
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005585 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005586 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005587 {
5588 tv.v_type = VAR_JOB;
5589 tv.vval.v_job = job;
5590 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5591 }
5592 return abort;
5593}
5594
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005595/*
5596 * Dereference "job". Note that after this "job" may have been freed.
5597 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005598 void
5599job_unref(job_T *job)
5600{
5601 if (job != NULL && --job->jv_refcount <= 0)
5602 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005603 /* Do not free the job if there is a channel where the close callback
5604 * may get the job info. */
5605 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005606 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005607 /* Do not free the job when it has not ended yet and there is a
5608 * "stoponexit" flag or an exit callback. */
5609 if (!job_need_end_check(job))
5610 {
5611 job_free(job);
5612 }
5613 else if (job->jv_channel != NULL)
5614 {
5615 /* Do remove the link to the channel, otherwise it hangs
5616 * around until Vim exits. See job_free() for refcount. */
5617 ch_log(job->jv_channel, "detaching channel from job");
5618 job->jv_channel->ch_job = NULL;
5619 channel_unref(job->jv_channel);
5620 job->jv_channel = NULL;
5621 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005622 }
5623 }
5624}
5625
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005626 int
5627free_unused_jobs_contents(int copyID, int mask)
5628{
5629 int did_free = FALSE;
5630 job_T *job;
5631
5632 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005633 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005634 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005635 {
5636 /* Free the channel and ordinary items it contains, but don't
5637 * recurse into Lists, Dictionaries etc. */
5638 job_free_contents(job);
5639 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005640 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005641 return did_free;
5642}
5643
5644 void
5645free_unused_jobs(int copyID, int mask)
5646{
5647 job_T *job;
5648 job_T *job_next;
5649
5650 for (job = first_job; job != NULL; job = job_next)
5651 {
5652 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005653 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005654 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005655 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005656 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005657 job_free_job(job);
5658 }
5659 }
5660}
5661
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005662/*
5663 * Allocate a job. Sets the refcount to one and sets options default.
5664 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005665 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005666job_alloc(void)
5667{
5668 job_T *job;
5669
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005670 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005671 if (job != NULL)
5672 {
5673 job->jv_refcount = 1;
5674 job->jv_stoponexit = vim_strsave((char_u *)"term");
5675
5676 if (first_job != NULL)
5677 {
5678 first_job->jv_prev = job;
5679 job->jv_next = first_job;
5680 }
5681 first_job = job;
5682 }
5683 return job;
5684}
5685
5686 void
5687job_set_options(job_T *job, jobopt_T *opt)
5688{
5689 if (opt->jo_set & JO_STOPONEXIT)
5690 {
5691 vim_free(job->jv_stoponexit);
5692 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5693 job->jv_stoponexit = NULL;
5694 else
5695 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5696 }
5697 if (opt->jo_set & JO_EXIT_CB)
5698 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005699 free_callback(&job->jv_exit_cb);
5700 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005701 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005702 job->jv_exit_cb.cb_name = NULL;
5703 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005704 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005705 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005706 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005707 }
5708}
5709
5710/*
5711 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5712 */
5713 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005714job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005715{
5716 job_T *job;
5717
5718 for (job = first_job; job != NULL; job = job->jv_next)
5719 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005720 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005721}
5722
5723/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005724 * Return TRUE when there is any job that has an exit callback and might exit,
5725 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005726 */
5727 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005728has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005729{
5730 job_T *job;
5731
5732 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005733 /* Only should check if the channel has been closed, if the channel is
5734 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005735 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5736 || (job->jv_status == JOB_FINISHED
5737 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005738 return TRUE;
5739 return FALSE;
5740}
5741
Bram Moolenaar97792de2016-10-15 18:36:49 +02005742#define MAX_CHECK_ENDED 8
5743
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005744/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005745 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005746 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005747 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005748 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005749job_check_ended(void)
5750{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005751 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005752 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005753
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005754 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005755 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005756 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005757
Bram Moolenaar97792de2016-10-15 18:36:49 +02005758 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005759 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005760 // NOTE: mch_detect_ended_job() must only return a job of which the
5761 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005762 job_T *job = mch_detect_ended_job(first_job);
5763
5764 if (job == NULL)
5765 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005766 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005767 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005768 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005769
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005770 // Actually free jobs that were cleaned up.
5771 free_jobs_to_free_later();
5772
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005773 if (channel_need_redraw)
5774 {
5775 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005776 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005777 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005778 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005779}
5780
5781/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005782 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005783 * "argv_arg" is only for Unix.
5784 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005785 * The returned job has a refcount of one.
5786 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005787 */
5788 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005789job_start(
5790 typval_T *argvars,
5791 char **argv_arg,
5792 jobopt_T *opt_arg,
5793 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005794{
5795 job_T *job;
5796 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005797 char **argv = NULL;
5798 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005799#if defined(UNIX)
5800# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005801 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005802#else
5803 garray_T ga;
5804#endif
5805 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005806 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005807
5808 job = job_alloc();
5809 if (job == NULL)
5810 return NULL;
5811
5812 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005813#ifndef USE_ARGV
5814 ga_init2(&ga, (int)sizeof(char*), 20);
5815#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005816
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005817 if (opt_arg != NULL)
5818 opt = *opt_arg;
5819 else
5820 {
5821 /* Default mode is NL. */
5822 clear_job_options(&opt);
5823 opt.jo_mode = MODE_NL;
5824 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005825 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5826 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5827 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005828 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005829 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005830
5831 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005832 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005833 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5834 && opt.jo_io[part] == JIO_FILE
5835 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5836 || *opt.jo_io_name[part] == NUL))
5837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005838 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005839 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005840 }
5841
5842 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5843 {
5844 buf_T *buf = NULL;
5845
5846 /* check that we can find the buffer before starting the job */
5847 if (opt.jo_set & JO_IN_BUF)
5848 {
5849 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5850 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005851 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005852 }
5853 else if (!(opt.jo_set & JO_IN_NAME))
5854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005855 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005856 }
5857 else
5858 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5859 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005860 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005861 if (buf->b_ml.ml_mfp == NULL)
5862 {
5863 char_u numbuf[NUMBUFLEN];
5864 char_u *s;
5865
5866 if (opt.jo_set & JO_IN_BUF)
5867 {
5868 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5869 s = numbuf;
5870 }
5871 else
5872 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005873 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005874 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005875 }
5876 job->jv_in_buf = buf;
5877 }
5878
5879 job_set_options(job, &opt);
5880
Bram Moolenaar13568252018-03-16 20:46:58 +01005881#ifdef USE_ARGV
5882 if (argv_arg != NULL)
5883 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005884 /* Make a copy of argv_arg for job->jv_argv. */
5885 for (i = 0; argv_arg[i] != NULL; i++)
5886 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005887 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005888 if (argv == NULL)
5889 goto theend;
5890 for (i = 0; i < argc; i++)
5891 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5892 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005893 }
5894 else
5895#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005896 if (argvars[0].v_type == VAR_STRING)
5897 {
5898 /* Command is a string. */
5899 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005900 if (cmd == NULL || *cmd == NUL)
5901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005902 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005903 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005904 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005905
5906 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005907 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005908 }
5909 else if (argvars[0].v_type != VAR_LIST
5910 || argvars[0].vval.v_list == NULL
5911 || argvars[0].vval.v_list->lv_len < 1)
5912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005913 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005914 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005915 }
5916 else
5917 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005918 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005919
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005920 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005921 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005922#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005923 if (win32_build_cmd(l, &ga) == FAIL)
5924 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005925 cmd = ga.ga_data;
5926#endif
5927 }
5928
Bram Moolenaar20608922018-04-21 22:30:08 +02005929 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005930 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005931
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005932#ifdef USE_ARGV
5933 if (ch_log_active())
5934 {
5935 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005936
5937 ga_init2(&ga, (int)sizeof(char), 200);
5938 for (i = 0; i < argc; ++i)
5939 {
5940 if (i > 0)
5941 ga_concat(&ga, (char_u *)" ");
5942 ga_concat(&ga, (char_u *)argv[i]);
5943 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005944 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005945 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005946 ga_clear(&ga);
5947 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005948 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005949#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005950 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005951 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005952#endif
5953
5954 /* If the channel is reading from a buffer, write lines now. */
5955 if (job->jv_channel != NULL)
5956 channel_write_in(job->jv_channel);
5957
5958theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005959#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005960 vim_free(ga.ga_data);
5961#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005962 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005963 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005964 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005965 return job;
5966}
5967
5968/*
5969 * Get the status of "job" and invoke the exit callback when needed.
5970 * The returned string is not allocated.
5971 */
5972 char *
5973job_status(job_T *job)
5974{
5975 char *result;
5976
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005977 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005978 /* No need to check, dead is dead. */
5979 result = "dead";
5980 else if (job->jv_status == JOB_FAILED)
5981 result = "fail";
5982 else
5983 {
5984 result = mch_job_status(job);
5985 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005986 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005987 }
5988 return result;
5989}
5990
Bram Moolenaar8950a562016-03-12 15:22:55 +01005991/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005992 * Send a signal to "job". Implements job_stop().
5993 * When "type" is not NULL use this for the type.
5994 * Otherwise use argvars[1] for the type.
5995 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005996 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005997job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005998{
5999 char_u *arg;
6000
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006001 if (type != NULL)
6002 arg = (char_u *)type;
6003 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006004 arg = (char_u *)"";
6005 else
6006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006007 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006008 if (arg == NULL)
6009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006010 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006011 return 0;
6012 }
6013 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006014 if (job->jv_status == JOB_FAILED)
6015 {
6016 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6017 return 0;
6018 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006019 if (job->jv_status == JOB_ENDED)
6020 {
6021 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6022 return 0;
6023 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006024 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006025 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006026 return 0;
6027
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006028 /* Assume that only "kill" will kill the job. */
6029 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006030 job->jv_channel->ch_job_killed = TRUE;
6031
6032 /* We don't try freeing the job, obviously the caller still has a
6033 * reference to it. */
6034 return 1;
6035}
6036
Bram Moolenaarf2732452018-06-03 14:47:35 +02006037 void
6038invoke_prompt_callback(void)
6039{
6040 typval_T rettv;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006041 typval_T argv[2];
6042 char_u *text;
6043 char_u *prompt;
6044 linenr_T lnum = curbuf->b_ml.ml_line_count;
6045
6046 // Add a new line for the prompt before invoking the callback, so that
6047 // text can always be inserted above the last line.
6048 ml_append(lnum, (char_u *)"", 0, FALSE);
6049 curwin->w_cursor.lnum = lnum + 1;
6050 curwin->w_cursor.col = 0;
6051
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006052 if (curbuf->b_prompt_callback.cb_name == NULL
6053 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006054 return;
6055 text = ml_get(lnum);
6056 prompt = prompt_text();
6057 if (STRLEN(text) >= STRLEN(prompt))
6058 text += STRLEN(prompt);
6059 argv[0].v_type = VAR_STRING;
6060 argv[0].vval.v_string = vim_strsave(text);
6061 argv[1].v_type = VAR_UNKNOWN;
6062
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006063 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006064 clear_tv(&argv[0]);
6065 clear_tv(&rettv);
6066}
6067
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006068/*
6069 * Return TRUE when the interrupt callback was invoked.
6070 */
6071 int
6072invoke_prompt_interrupt(void)
6073{
6074 typval_T rettv;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006075 typval_T argv[1];
6076
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006077 if (curbuf->b_prompt_interrupt.cb_name == NULL
6078 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006079 return FALSE;
6080 argv[0].v_type = VAR_UNKNOWN;
6081
6082 got_int = FALSE; // don't skip executing commands
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006083 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006084 clear_tv(&rettv);
6085 return TRUE;
6086}
6087
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006088/*
6089 * "prompt_setcallback({buffer}, {callback})" function
6090 */
6091 void
6092f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6093{
6094 buf_T *buf;
6095 callback_T callback;
6096
6097 if (check_secure())
6098 return;
6099 buf = tv_get_buf(&argvars[0], FALSE);
6100 if (buf == NULL)
6101 return;
6102
6103 callback = get_callback(&argvars[1]);
6104 if (callback.cb_name == NULL)
6105 return;
6106
6107 free_callback(&buf->b_prompt_callback);
6108 set_callback(&buf->b_prompt_callback, &callback);
6109}
6110
6111/*
6112 * "prompt_setinterrupt({buffer}, {callback})" function
6113 */
6114 void
6115f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6116{
6117 buf_T *buf;
6118 callback_T callback;
6119
6120 if (check_secure())
6121 return;
6122 buf = tv_get_buf(&argvars[0], FALSE);
6123 if (buf == NULL)
6124 return;
6125
6126 callback = get_callback(&argvars[1]);
6127 if (callback.cb_name == NULL)
6128 return;
6129
6130 free_callback(&buf->b_prompt_interrupt);
6131 set_callback(&buf->b_prompt_interrupt, &callback);
6132}
6133
6134/*
6135 * "prompt_setprompt({buffer}, {text})" function
6136 */
6137 void
6138f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6139{
6140 buf_T *buf;
6141 char_u *text;
6142
6143 if (check_secure())
6144 return;
6145 buf = tv_get_buf(&argvars[0], FALSE);
6146 if (buf == NULL)
6147 return;
6148
6149 text = tv_get_string(&argvars[1]);
6150 vim_free(buf->b_prompt_text);
6151 buf->b_prompt_text = vim_strsave(text);
6152}
6153
6154/*
6155 * "ch_canread()" function
6156 */
6157 void
6158f_ch_canread(typval_T *argvars, typval_T *rettv)
6159{
6160 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6161
6162 rettv->vval.v_number = 0;
6163 if (channel != NULL)
6164 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6165 || channel_has_readahead(channel, PART_OUT)
6166 || channel_has_readahead(channel, PART_ERR);
6167}
6168
6169/*
6170 * "ch_close()" function
6171 */
6172 void
6173f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6174{
6175 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6176
6177 if (channel != NULL)
6178 {
6179 channel_close(channel, FALSE);
6180 channel_clear(channel);
6181 }
6182}
6183
6184/*
6185 * "ch_close()" function
6186 */
6187 void
6188f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6189{
6190 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6191
6192 if (channel != NULL)
6193 channel_close_in(channel);
6194}
6195
6196/*
6197 * "ch_getbufnr()" function
6198 */
6199 void
6200f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6201{
6202 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6203
6204 rettv->vval.v_number = -1;
6205 if (channel != NULL)
6206 {
6207 char_u *what = tv_get_string(&argvars[1]);
6208 int part;
6209
6210 if (STRCMP(what, "err") == 0)
6211 part = PART_ERR;
6212 else if (STRCMP(what, "out") == 0)
6213 part = PART_OUT;
6214 else if (STRCMP(what, "in") == 0)
6215 part = PART_IN;
6216 else
6217 part = PART_SOCK;
6218 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6219 rettv->vval.v_number =
6220 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6221 }
6222}
6223
6224/*
6225 * "ch_getjob()" function
6226 */
6227 void
6228f_ch_getjob(typval_T *argvars, typval_T *rettv)
6229{
6230 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6231
6232 if (channel != NULL)
6233 {
6234 rettv->v_type = VAR_JOB;
6235 rettv->vval.v_job = channel->ch_job;
6236 if (channel->ch_job != NULL)
6237 ++channel->ch_job->jv_refcount;
6238 }
6239}
6240
6241/*
6242 * "ch_info()" function
6243 */
6244 void
6245f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6246{
6247 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6248
6249 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6250 channel_info(channel, rettv->vval.v_dict);
6251}
6252
6253/*
6254 * "ch_log()" function
6255 */
6256 void
6257f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6258{
6259 char_u *msg = tv_get_string(&argvars[0]);
6260 channel_T *channel = NULL;
6261
6262 if (argvars[1].v_type != VAR_UNKNOWN)
6263 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6264
6265 ch_log(channel, "%s", msg);
6266}
6267
6268/*
6269 * "ch_logfile()" function
6270 */
6271 void
6272f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6273{
6274 char_u *fname;
6275 char_u *opt = (char_u *)"";
6276 char_u buf[NUMBUFLEN];
6277
6278 /* Don't open a file in restricted mode. */
6279 if (check_restricted() || check_secure())
6280 return;
6281 fname = tv_get_string(&argvars[0]);
6282 if (argvars[1].v_type == VAR_STRING)
6283 opt = tv_get_string_buf(&argvars[1], buf);
6284 ch_logfile(fname, opt);
6285}
6286
6287/*
6288 * "ch_open()" function
6289 */
6290 void
6291f_ch_open(typval_T *argvars, typval_T *rettv)
6292{
6293 rettv->v_type = VAR_CHANNEL;
6294 if (check_restricted() || check_secure())
6295 return;
6296 rettv->vval.v_channel = channel_open_func(argvars);
6297}
6298
6299/*
6300 * "ch_read()" function
6301 */
6302 void
6303f_ch_read(typval_T *argvars, typval_T *rettv)
6304{
6305 common_channel_read(argvars, rettv, FALSE, FALSE);
6306}
6307
6308/*
6309 * "ch_readblob()" function
6310 */
6311 void
6312f_ch_readblob(typval_T *argvars, typval_T *rettv)
6313{
6314 common_channel_read(argvars, rettv, TRUE, TRUE);
6315}
6316
6317/*
6318 * "ch_readraw()" function
6319 */
6320 void
6321f_ch_readraw(typval_T *argvars, typval_T *rettv)
6322{
6323 common_channel_read(argvars, rettv, TRUE, FALSE);
6324}
6325
6326/*
6327 * "ch_evalexpr()" function
6328 */
6329 void
6330f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6331{
6332 ch_expr_common(argvars, rettv, TRUE);
6333}
6334
6335/*
6336 * "ch_sendexpr()" function
6337 */
6338 void
6339f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6340{
6341 ch_expr_common(argvars, rettv, FALSE);
6342}
6343
6344/*
6345 * "ch_evalraw()" function
6346 */
6347 void
6348f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6349{
6350 ch_raw_common(argvars, rettv, TRUE);
6351}
6352
6353/*
6354 * "ch_sendraw()" function
6355 */
6356 void
6357f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6358{
6359 ch_raw_common(argvars, rettv, FALSE);
6360}
6361
6362/*
6363 * "ch_setoptions()" function
6364 */
6365 void
6366f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6367{
6368 channel_T *channel;
6369 jobopt_T opt;
6370
6371 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6372 if (channel == NULL)
6373 return;
6374 clear_job_options(&opt);
6375 if (get_job_options(&argvars[1], &opt,
6376 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6377 channel_set_options(channel, &opt);
6378 free_job_options(&opt);
6379}
6380
6381/*
6382 * "ch_status()" function
6383 */
6384 void
6385f_ch_status(typval_T *argvars, typval_T *rettv)
6386{
6387 channel_T *channel;
6388 jobopt_T opt;
6389 int part = -1;
6390
6391 /* return an empty string by default */
6392 rettv->v_type = VAR_STRING;
6393 rettv->vval.v_string = NULL;
6394
6395 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6396
6397 if (argvars[1].v_type != VAR_UNKNOWN)
6398 {
6399 clear_job_options(&opt);
6400 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6401 && (opt.jo_set & JO_PART))
6402 part = opt.jo_part;
6403 }
6404
6405 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6406}
6407
6408/*
6409 * Get the job from the argument.
6410 * Returns NULL if the job is invalid.
6411 */
6412 static job_T *
6413get_job_arg(typval_T *tv)
6414{
6415 job_T *job;
6416
6417 if (tv->v_type != VAR_JOB)
6418 {
6419 semsg(_(e_invarg2), tv_get_string(tv));
6420 return NULL;
6421 }
6422 job = tv->vval.v_job;
6423
6424 if (job == NULL)
6425 emsg(_("E916: not a valid job"));
6426 return job;
6427}
6428
6429/*
6430 * "job_getchannel()" function
6431 */
6432 void
6433f_job_getchannel(typval_T *argvars, typval_T *rettv)
6434{
6435 job_T *job = get_job_arg(&argvars[0]);
6436
6437 if (job != NULL)
6438 {
6439 rettv->v_type = VAR_CHANNEL;
6440 rettv->vval.v_channel = job->jv_channel;
6441 if (job->jv_channel != NULL)
6442 ++job->jv_channel->ch_refcount;
6443 }
6444}
6445
6446/*
6447 * Implementation of job_info().
6448 */
6449 static void
6450job_info(job_T *job, dict_T *dict)
6451{
6452 dictitem_T *item;
6453 varnumber_T nr;
6454 list_T *l;
6455 int i;
6456
6457 dict_add_string(dict, "status", (char_u *)job_status(job));
6458
6459 item = dictitem_alloc((char_u *)"channel");
6460 if (item == NULL)
6461 return;
6462 item->di_tv.v_type = VAR_CHANNEL;
6463 item->di_tv.vval.v_channel = job->jv_channel;
6464 if (job->jv_channel != NULL)
6465 ++job->jv_channel->ch_refcount;
6466 if (dict_add(dict, item) == FAIL)
6467 dictitem_free(item);
6468
6469#ifdef UNIX
6470 nr = job->jv_pid;
6471#else
6472 nr = job->jv_proc_info.dwProcessId;
6473#endif
6474 dict_add_number(dict, "process", nr);
6475 dict_add_string(dict, "tty_in", job->jv_tty_in);
6476 dict_add_string(dict, "tty_out", job->jv_tty_out);
6477
6478 dict_add_number(dict, "exitval", job->jv_exitval);
6479 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6480 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6481#ifdef UNIX
6482 dict_add_string(dict, "termsig", job->jv_termsig);
6483#endif
6484#ifdef MSWIN
6485 dict_add_string(dict, "tty_type", job->jv_tty_type);
6486#endif
6487
6488 l = list_alloc();
6489 if (l != NULL)
6490 {
6491 dict_add_list(dict, "cmd", l);
6492 if (job->jv_argv != NULL)
6493 for (i = 0; job->jv_argv[i] != NULL; i++)
6494 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6495 }
6496}
6497
6498/*
6499 * Implementation of job_info() to return info for all jobs.
6500 */
6501 static void
6502job_info_all(list_T *l)
6503{
6504 job_T *job;
6505 typval_T tv;
6506
6507 for (job = first_job; job != NULL; job = job->jv_next)
6508 {
6509 tv.v_type = VAR_JOB;
6510 tv.vval.v_job = job;
6511
6512 if (list_append_tv(l, &tv) != OK)
6513 return;
6514 }
6515}
6516
6517/*
6518 * "job_info()" function
6519 */
6520 void
6521f_job_info(typval_T *argvars, typval_T *rettv)
6522{
6523 if (argvars[0].v_type != VAR_UNKNOWN)
6524 {
6525 job_T *job = get_job_arg(&argvars[0]);
6526
6527 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6528 job_info(job, rettv->vval.v_dict);
6529 }
6530 else if (rettv_list_alloc(rettv) == OK)
6531 job_info_all(rettv->vval.v_list);
6532}
6533
6534/*
6535 * "job_setoptions()" function
6536 */
6537 void
6538f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6539{
6540 job_T *job = get_job_arg(&argvars[0]);
6541 jobopt_T opt;
6542
6543 if (job == NULL)
6544 return;
6545 clear_job_options(&opt);
6546 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6547 job_set_options(job, &opt);
6548 free_job_options(&opt);
6549}
6550
6551/*
6552 * "job_start()" function
6553 */
6554 void
6555f_job_start(typval_T *argvars, typval_T *rettv)
6556{
6557 rettv->v_type = VAR_JOB;
6558 if (check_restricted() || check_secure())
6559 return;
6560 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6561}
6562
6563/*
6564 * "job_status()" function
6565 */
6566 void
6567f_job_status(typval_T *argvars, typval_T *rettv)
6568{
6569 job_T *job = get_job_arg(&argvars[0]);
6570
6571 if (job != NULL)
6572 {
6573 rettv->v_type = VAR_STRING;
6574 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6575 }
6576}
6577
6578/*
6579 * "job_stop()" function
6580 */
6581 void
6582f_job_stop(typval_T *argvars, typval_T *rettv)
6583{
6584 job_T *job = get_job_arg(&argvars[0]);
6585
6586 if (job != NULL)
6587 rettv->vval.v_number = job_stop(job, argvars, NULL);
6588}
6589
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006590#endif /* FEAT_JOB_CHANNEL */