blob: 6351c892c3c16784e7691738606ed30a78b511c5 [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 Moolenaarb2658a12016-04-26 17:16:24 +020058
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaar4f974752019-02-17 17:44:42 +010068#ifdef MSWIN
Bram Moolenaard8070362016-02-15 21:56:54 +010069 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
Bram Moolenaar24058382019-01-24 23:11:49 +010083 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010084 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010085 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010086 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010087
Bram Moolenaar24058382019-01-24 23:11:49 +010088 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 {
Bram Moolenaar24058382019-01-24 23:11:49 +010090 if (todo > MAX_NAMED_PIPE_SIZE)
91 size = MAX_NAMED_PIPE_SIZE;
92 else
Bram Moolenaardec01202019-01-28 20:04:24 +010093 size = (DWORD)todo;
Bram Moolenaar65240682019-02-10 22:23:26 +010094 // If the pipe overflows while the job does not read the data,
95 // WriteFile() will block forever. This abandons the write.
Bram Moolenaar24058382019-01-24 23:11:49 +010096 memset(&ov, 0, sizeof(ov));
Bram Moolenaar65240682019-02-10 22:23:26 +010097 nwrite = 0;
Bram Moolenaar24058382019-01-24 23:11:49 +010098 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
99 {
100 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100101
Bram Moolenaar24058382019-01-24 23:11:49 +0100102 if (err != ERROR_IO_PENDING)
103 return -1;
104 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
105 return -1;
106 FlushFileBuffers(h);
107 }
Bram Moolenaar65240682019-02-10 22:23:26 +0100108 else if (nwrite == 0)
109 // WriteFile() returns TRUE but did not write anything. This causes
110 // a hang, so bail out.
111 break;
Bram Moolenaar24058382019-01-24 23:11:49 +0100112 todo -= nwrite;
113 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100114 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100115 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100116}
117
118 static void
119fd_close(sock_T fd)
120{
121 HANDLE h = (HANDLE)fd;
122
123 CloseHandle(h);
124}
125#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100126
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127/* Log file opened with ch_logfile(). */
128static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100129#ifdef FEAT_RELTIME
130static proftime_T log_start;
131#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100132
133 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100134ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100136 FILE *file = NULL;
137
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138 if (log_fd != NULL)
139 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100140
141 if (*fname != NUL)
142 {
143 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
144 if (file == NULL)
145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100146 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100147 return;
148 }
149 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100150 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100151
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100152 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100153 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100155#ifdef FEAT_RELTIME
156 profile_start(&log_start);
157#endif
158 }
159}
160
161 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200162ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163{
164 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100165}
166
167 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200168ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169{
170 if (log_fd != NULL)
171 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172#ifdef FEAT_RELTIME
173 proftime_T log_now;
174
175 profile_start(&log_now);
176 profile_sub(&log_now, &log_start);
177 fprintf(log_fd, "%s ", profile_msg(&log_now));
178#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200180 {
181 if (part < PART_COUNT)
182 fprintf(log_fd, "%son %d(%s): ",
183 what, ch->ch_id, part_names[part]);
184 else
185 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
186 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100187 else
188 fprintf(log_fd, "%s: ", what);
189 }
190}
191
Bram Moolenaard0b65022016-03-06 21:50:33 +0100192static int did_log_msg = TRUE;
193
Bram Moolenaar8caa43d2019-02-20 22:45:06 +0100194#ifndef PROTO // prototype is in proto.h
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100195 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200196ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197{
198 if (log_fd != NULL)
199 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200200 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200202 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200203 va_start(ap, fmt);
204 vfprintf(log_fd, fmt, ap);
205 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100206 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100207 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100208 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100209 }
210}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200211#endif
212
213 static void
214ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200215#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
216 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200217#endif
218 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219
220 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200221ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100222{
223 if (log_fd != NULL)
224 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200225 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100226
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200227 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200228 va_start(ap, fmt);
229 vfprintf(log_fd, fmt, ap);
230 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100231 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100233 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 }
235}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100236
Bram Moolenaar4f974752019-02-17 17:44:42 +0100237#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100238# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100239# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100240
241 static char *
242strerror_win32(int eno)
243{
244 static LPVOID msgbuf = NULL;
245 char_u *ptr;
246
247 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200248 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100249 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200250 msgbuf = NULL;
251 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252 FormatMessage(
253 FORMAT_MESSAGE_ALLOCATE_BUFFER |
254 FORMAT_MESSAGE_FROM_SYSTEM |
255 FORMAT_MESSAGE_IGNORE_INSERTS,
256 NULL,
257 eno,
258 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
259 (LPTSTR) &msgbuf,
260 0,
261 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200262 if (msgbuf != NULL)
263 /* chomp \r or \n */
264 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
265 switch (*ptr)
266 {
267 case '\r':
268 STRMOVE(ptr, ptr + 1);
269 ptr--;
270 break;
271 case '\n':
272 if (*(ptr + 1) == '\0')
273 *ptr = '\0';
274 else
275 *ptr = ' ';
276 break;
277 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100278 return msgbuf;
279}
280#endif
281
Bram Moolenaar77073442016-02-13 23:23:53 +0100282/*
283 * The list of all allocated channels.
284 */
285static channel_T *first_channel = NULL;
286static int next_ch_id = 0;
287
288/*
289 * Allocate a new channel. The refcount is set to 1.
290 * The channel isn't actually used until it is opened.
291 * Returns NULL if out of memory.
292 */
293 channel_T *
294add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100295{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200296 ch_part_T part;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200297 channel_T *channel = ALLOC_CLEAR_ONE(channel_T);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100298
Bram Moolenaar77073442016-02-13 23:23:53 +0100299 if (channel == NULL)
300 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100301
Bram Moolenaar77073442016-02-13 23:23:53 +0100302 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100303 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100304
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200305 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100306 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100307 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100309 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100310#endif
311#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100315 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100316
Bram Moolenaar77073442016-02-13 23:23:53 +0100317 if (first_channel != NULL)
318 {
319 first_channel->ch_prev = channel;
320 channel->ch_next = first_channel;
321 }
322 first_channel = channel;
323
324 channel->ch_refcount = 1;
325 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100326}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100327
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200328 int
329has_any_channel(void)
330{
331 return first_channel != NULL;
332}
333
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100334/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100335 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100336 * Return TRUE if "channel" has a callback and the associated job wasn't
337 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100338 */
339 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100340channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100341{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100342 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 int has_out_msg;
344 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100345
346 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100347 if (channel->ch_job_killed && channel->ch_job == NULL)
348 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100349
350 /* If there is a close callback it may still need to be invoked. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200351 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100352 return TRUE;
353
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200354 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200355 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200356 return TRUE;
357
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 /* If there is no callback then nobody can get readahead. If the fd is
359 * closed and there is no readahead then the callback won't be called. */
360 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100361 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
362 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100363 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
364 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
365 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
366 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
367 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
368 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200369 return (channel->ch_callback.cb_name != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100370 || has_out_msg || has_err_msg))
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200371 || ((channel->ch_part[PART_OUT].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200372 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
373 && has_out_msg)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200374 || ((channel->ch_part[PART_ERR].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200375 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
376 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377}
378
379/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200380 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
381 */
382 static int
383channel_can_close(channel_T *channel)
384{
385 return channel->ch_to_be_closed == 0;
386}
387
388/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200389 * Close a channel and free all its resources.
390 */
391 static void
392channel_free_contents(channel_T *channel)
393{
394 channel_close(channel, TRUE);
395 channel_clear(channel);
396 ch_log(channel, "Freeing channel");
397}
398
399 static void
400channel_free_channel(channel_T *channel)
401{
402 if (channel->ch_next != NULL)
403 channel->ch_next->ch_prev = channel->ch_prev;
404 if (channel->ch_prev == NULL)
405 first_channel = channel->ch_next;
406 else
407 channel->ch_prev->ch_next = channel->ch_next;
408 vim_free(channel);
409}
410
411 static void
412channel_free(channel_T *channel)
413{
414 if (!in_free_unref_items)
415 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200416 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200417 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200418 else
419 {
420 channel_free_contents(channel);
421 channel_free_channel(channel);
422 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200423 }
424}
425
426/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100427 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100428 * possible, there is no callback to be invoked or the associated job was
429 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100430 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100431 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100432 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100433channel_may_free(channel_T *channel)
434{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100435 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100436 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100437 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100438 return TRUE;
439 }
440 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100441}
442
443/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100444 * Decrement the reference count on "channel" and maybe free it when it goes
445 * down to zero. Don't free it if there is a pending action.
446 * Returns TRUE when the channel is no longer referenced.
447 */
448 int
449channel_unref(channel_T *channel)
450{
451 if (channel != NULL && --channel->ch_refcount <= 0)
452 return channel_may_free(channel);
453 return FALSE;
454}
455
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200456 int
457free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100458{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200459 int did_free = FALSE;
460 channel_T *ch;
461
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200462 /* This is invoked from the garbage collector, which only runs at a safe
463 * point. */
464 ++safe_to_invoke_callback;
465
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200467 if (!channel_still_useful(ch)
468 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200469 {
470 /* Free the channel and ordinary items it contains, but don't
471 * recurse into Lists, Dictionaries etc. */
472 channel_free_contents(ch);
473 did_free = TRUE;
474 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200475
476 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200477 return did_free;
478}
479
480 void
481free_unused_channels(int copyID, int mask)
482{
483 channel_T *ch;
484 channel_T *ch_next;
485
486 for (ch = first_channel; ch != NULL; ch = ch_next)
487 {
488 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200489 if (!channel_still_useful(ch)
490 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200491 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200492 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200493 channel_free_channel(ch);
494 }
495 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
497
Bram Moolenaard04a0202016-01-26 23:30:18 +0100498#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100499
500#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
501 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100502channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100503{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200505 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100506
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100507 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100508 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200509 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100510 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200511 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100512}
513#endif
514
Bram Moolenaare0874f82016-01-24 20:36:41 +0100515/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100516 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100518#ifdef FEAT_GUI_X11
519 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200520messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200521 int *unused1 UNUSED,
522 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100523{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100524 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100525}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100526#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100527
Bram Moolenaard04a0202016-01-26 23:30:18 +0100528#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100529# if GTK_CHECK_VERSION(3,0,0)
530 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200531messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200532 GIOCondition unused2 UNUSED,
533 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100534{
535 channel_read_fd(GPOINTER_TO_INT(clientData));
536 return TRUE; /* Return FALSE instead in case the event source is to
537 * be removed after this function returns. */
538}
539# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100540 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200541messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200542 gint unused1 UNUSED,
543 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100544{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100545 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100546}
Bram Moolenaar98921892016-02-23 17:14:37 +0100547# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100548#endif
549
550 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200551channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100552{
Bram Moolenaarde279892016-03-11 22:19:44 +0100553 if (!CH_HAS_GUI)
554 return;
555
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200556 /* gets stuck in handling events for a not connected channel */
557 if (channel->ch_keep_open)
558 return;
559
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200561 /* Tell notifier we are interested in being called when there is input on
562 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100563 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200564 {
565 ch_log(channel, "Registering part %s with fd %d",
566 part_names[part], channel->ch_part[part].ch_fd);
567
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100569 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100571 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200572 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100573 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200574 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100575# else
576# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200577 /* Tell gdk we are interested in being called when there is input on the
578 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100579 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100580 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200581 ch_log(channel, "Registering part %s with fd %d",
582 part_names[part], channel->ch_part[part].ch_fd);
583# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100584 GIOChannel *chnnl = g_io_channel_unix_new(
585 (gint)channel->ch_part[part].ch_fd);
586
587 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
588 chnnl,
589 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200590 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100591 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
592
593 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100594# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100595 channel->ch_part[part].ch_inputHandler = gdk_input_add(
596 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100597 (GdkInputCondition)
598 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200599 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100600 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100601# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200602 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100603# endif
604# endif
605}
606
Bram Moolenaarde279892016-03-11 22:19:44 +0100607 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100608channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100609{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100610 if (channel->CH_SOCK_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200612 if (channel->CH_OUT_FD != INVALID_FD
613 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100614 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200615 if (channel->CH_ERR_FD != INVALID_FD
616 && channel->CH_ERR_FD != channel->CH_SOCK_FD
617 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100619}
620
621/*
622 * Register any of our file descriptors with the GUI event handling system.
623 * Called when the GUI has started.
624 */
625 void
626channel_gui_register_all(void)
627{
Bram Moolenaar77073442016-02-13 23:23:53 +0100628 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100629
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100631 channel_gui_register(channel);
632}
633
634 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200635channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100636{
637# ifdef FEAT_GUI_X11
638 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
639 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200640 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100641 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
642 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
643 }
644# else
645# ifdef FEAT_GUI_GTK
646 if (channel->ch_part[part].ch_inputHandler != 0)
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# if GTK_CHECK_VERSION(3,0,0)
650 g_source_remove(channel->ch_part[part].ch_inputHandler);
651# else
652 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
653# endif
654 channel->ch_part[part].ch_inputHandler = 0;
655 }
656# endif
657# endif
658}
659
660 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100661channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100662{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200663 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100664
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100665 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100666 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667}
668
669#endif
670
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100671static char *e_cannot_connect = N_("E902: Cannot connect to port");
672
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100674 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100675 * "waittime" is the time in msec to wait for the connection.
676 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 * Returns the channel for success.
678 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100680 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100681channel_open(
682 char *hostname,
683 int port_in,
684 int waittime,
685 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100688 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100689 struct hostent *host;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100690#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100692 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693#else
694 int port = port_in;
695#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100696 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100697 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100698
Bram Moolenaar4f974752019-02-17 17:44:42 +0100699#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100700 channel_init_winsock();
701#endif
702
Bram Moolenaar77073442016-02-13 23:23:53 +0100703 channel = add_channel();
704 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100705 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100706 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100707 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100708 }
709
710 /* Get the server internet address and put into addr structure */
711 /* fill in the socket address structure and connect to server */
712 vim_memset((char *)&server, 0, sizeof(server));
713 server.sin_family = AF_INET;
714 server.sin_port = htons(port);
715 if ((host = gethostbyname(hostname)) == NULL)
716 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100717 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200718 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100719 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100720 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100722 {
723 char *p;
724
Bram Moolenaar2e324952018-04-14 14:37:07 +0200725 /* When using host->h_addr_list[0] directly ubsan warns for it to not
726 * be aligned. First copy the pointer to avoid that. */
727 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100728 memcpy((char *)&server.sin_addr, p, host->h_length);
729 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100730
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100731 /* On Mac and Solaris a zero timeout almost never works. At least wait
732 * one millisecond. Let's do it for all systems, because we don't know why
733 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100734 if (waittime == 0)
735 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100736
737 /*
738 * For Unix we need to call connect() again after connect() failed.
739 * On Win32 one time is sufficient.
740 */
741 while (TRUE)
742 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100743 long elapsed_msec = 0;
744 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100745
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100746 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100747 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100748 sd = socket(AF_INET, SOCK_STREAM, 0);
749 if (sd == -1)
750 {
751 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200752 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100753 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100754 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100755 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100756
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100757 if (waittime >= 0)
758 {
759 /* Make connect() non-blocking. */
760 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +0100761#ifdef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100762 ioctlsocket(sd, FIONBIO, &val) < 0
763#else
764 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
765#endif
766 )
767 {
768 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200769 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 "channel_open: Connect failed with errno %d", errno);
771 sock_close(sd);
772 channel_free(channel);
773 return NULL;
774 }
775 }
776
777 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200778 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100779 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
780
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781 if (ret == 0)
782 /* The connection could be established. */
783 break;
784
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100785 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100786 if (waittime < 0 || (errno != EWOULDBLOCK
787 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100788#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100790#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 ))
792 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200793 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100794 "channel_open: Connect failed with errno %d", errno);
795 PERROR(_(e_cannot_connect));
796 sock_close(sd);
797 channel_free(channel);
798 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100799 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100800
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100801 /* Limit the waittime to 50 msec. If it doesn't work within this
802 * time we close the socket and try creating it again. */
803 waitnow = waittime > 50 ? 50 : waittime;
804
Bram Moolenaar045a2842016-03-08 22:33:07 +0100805 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100806 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100807#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 if (errno != ECONNREFUSED)
809#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100810 {
811 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100812 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100813 fd_set wfds;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100814#ifndef MSWIN
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 int so_error = 0;
816 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100817 struct timeval start_tv;
818 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100819#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100820 FD_ZERO(&rfds);
821 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100822 FD_ZERO(&wfds);
823 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100824
Bram Moolenaar562ca712016-03-09 21:50:05 +0100825 tv.tv_sec = waitnow / 1000;
826 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100827#ifndef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100828 gettimeofday(&start_tv, NULL);
829#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200830 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100831 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100832 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100833
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100834 if (ret < 0)
835 {
836 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200837 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100838 "channel_open: Connect failed with errno %d", errno);
839 PERROR(_(e_cannot_connect));
840 sock_close(sd);
841 channel_free(channel);
842 return NULL;
843 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100844
Bram Moolenaar4f974752019-02-17 17:44:42 +0100845#ifdef MSWIN
Bram Moolenaar562ca712016-03-09 21:50:05 +0100846 /* On Win32: select() is expected to work and wait for up to
847 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100848 if (FD_ISSET(sd, &wfds))
849 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100850 elapsed_msec = waitnow;
851 if (waittime > 1 && elapsed_msec < waittime)
852 {
853 waittime -= elapsed_msec;
854 continue;
855 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100856#else
857 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100858 * After putting the socket in non-blocking mode, connect() will
859 * return EINPROGRESS, select() will not wait (as if writing is
860 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100861 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100862 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100864 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100865 {
866 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100867 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100868 if (ret < 0 || (so_error != 0
869 && so_error != EWOULDBLOCK
870 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100871# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100872 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100873# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100874 ))
875 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200876 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100877 "channel_open: Connect failed with errno %d",
878 so_error);
879 PERROR(_(e_cannot_connect));
880 sock_close(sd);
881 channel_free(channel);
882 return NULL;
883 }
884 }
885
Bram Moolenaar045a2842016-03-08 22:33:07 +0100886 if (FD_ISSET(sd, &wfds) && so_error == 0)
887 /* Did not detect an error, connection is established. */
888 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100889
Bram Moolenaar045a2842016-03-08 22:33:07 +0100890 gettimeofday(&end_tv, NULL);
891 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
892 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100893#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100894 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100895
Bram Moolenaar4f974752019-02-17 17:44:42 +0100896#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100897 if (waittime > 1 && elapsed_msec < waittime)
898 {
899 /* The port isn't ready but we also didn't get an error.
900 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100901 * yet. Select() may return early, wait until the remaining
902 * "waitnow" and try again. */
903 waitnow -= elapsed_msec;
904 waittime -= elapsed_msec;
905 if (waitnow > 0)
906 {
907 mch_delay((long)waitnow, TRUE);
908 ui_breakcheck();
909 waittime -= waitnow;
910 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100911 if (!got_int)
912 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100913 if (waittime <= 0)
914 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100915 waittime = 1;
916 continue;
917 }
918 /* we were interrupted, behave as if timed out */
919 }
920#endif
921
922 /* We timed out. */
923 ch_error(channel, "Connection timed out");
924 sock_close(sd);
925 channel_free(channel);
926 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100927 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100928
Bram Moolenaar045a2842016-03-08 22:33:07 +0100929 ch_log(channel, "Connection made");
930
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100931 if (waittime >= 0)
932 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100933#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100934 val = 0;
935 ioctlsocket(sd, FIONBIO, &val);
936#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100937 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100938#endif
939 }
940
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100941 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100942 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100943 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
944 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200945 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100946
947#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100948 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100949#endif
950
Bram Moolenaar77073442016-02-13 23:23:53 +0100951 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100952}
953
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100954/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +0200955 * Copy callback from "src" to "dest", incrementing the refcounts.
956 */
957 static void
958copy_callback(callback_T *dest, callback_T *src)
959{
960 dest->cb_partial = src->cb_partial;
961 if (dest->cb_partial != NULL)
962 {
963 dest->cb_name = src->cb_name;
964 dest->cb_free_name = FALSE;
965 ++dest->cb_partial->pt_refcount;
966 }
967 else
968 {
969 dest->cb_name = vim_strsave(src->cb_name);
970 dest->cb_free_name = TRUE;
971 func_ref(src->cb_name);
972 }
973}
974
975 static void
976free_set_callback(callback_T *cbp, callback_T *callback)
977{
978 free_callback(cbp);
979
980 if (callback->cb_name != NULL && *callback->cb_name != NUL)
981 copy_callback(cbp, callback);
982 else
983 cbp->cb_name = NULL;
984}
985
986/*
987 * Prepare buffer "buf" for writing channel output to.
988 */
989 static void
990prepare_buffer(buf_T *buf)
991{
992 buf_T *save_curbuf = curbuf;
993
994 buf_copy_options(buf, BCO_ENTER);
995 curbuf = buf;
996#ifdef FEAT_QUICKFIX
997 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
998 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
999#endif
1000 if (curbuf->b_ml.ml_mfp == NULL)
1001 ml_open(curbuf);
1002 curbuf = save_curbuf;
1003}
1004
1005/*
1006 * Find a buffer matching "name" or create a new one.
1007 * Returns NULL if there is something very wrong (error already reported).
1008 */
1009 static buf_T *
1010find_buffer(char_u *name, int err, int msg)
1011{
1012 buf_T *buf = NULL;
1013 buf_T *save_curbuf = curbuf;
1014
1015 if (name != NULL && *name != NUL)
1016 {
1017 buf = buflist_findname(name);
1018 if (buf == NULL)
1019 buf = buflist_findname_exp(name);
1020 }
1021 if (buf == NULL)
1022 {
1023 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
1024 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
1025 if (buf == NULL)
1026 return NULL;
1027 prepare_buffer(buf);
1028
1029 curbuf = buf;
1030 if (msg)
1031 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1032 : "Reading from channel output..."), TRUE);
1033 changed_bytes(1, 0);
1034 curbuf = save_curbuf;
1035 }
1036
1037 return buf;
1038}
1039
1040/*
1041 * Set various properties from an "opt" argument.
1042 */
1043 static void
1044channel_set_options(channel_T *channel, jobopt_T *opt)
1045{
1046 ch_part_T part;
1047
1048 if (opt->jo_set & JO_MODE)
1049 for (part = PART_SOCK; part < PART_COUNT; ++part)
1050 channel->ch_part[part].ch_mode = opt->jo_mode;
1051 if (opt->jo_set & JO_IN_MODE)
1052 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1053 if (opt->jo_set & JO_OUT_MODE)
1054 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1055 if (opt->jo_set & JO_ERR_MODE)
1056 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
1057 channel->ch_nonblock = opt->jo_noblock;
1058
1059 if (opt->jo_set & JO_TIMEOUT)
1060 for (part = PART_SOCK; part < PART_COUNT; ++part)
1061 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1062 if (opt->jo_set & JO_OUT_TIMEOUT)
1063 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1064 if (opt->jo_set & JO_ERR_TIMEOUT)
1065 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1066 if (opt->jo_set & JO_BLOCK_WRITE)
1067 channel->ch_part[PART_IN].ch_block_write = 1;
1068
1069 if (opt->jo_set & JO_CALLBACK)
1070 free_set_callback(&channel->ch_callback, &opt->jo_callback);
1071 if (opt->jo_set & JO_OUT_CALLBACK)
1072 free_set_callback(&channel->ch_part[PART_OUT].ch_callback,
1073 &opt->jo_out_cb);
1074 if (opt->jo_set & JO_ERR_CALLBACK)
1075 free_set_callback(&channel->ch_part[PART_ERR].ch_callback,
1076 &opt->jo_err_cb);
1077 if (opt->jo_set & JO_CLOSE_CALLBACK)
1078 free_set_callback(&channel->ch_close_cb, &opt->jo_close_cb);
1079 channel->ch_drop_never = opt->jo_drop_never;
1080
1081 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1082 {
1083 buf_T *buf;
1084
1085 /* writing output to a buffer. Default mode is NL. */
1086 if (!(opt->jo_set & JO_OUT_MODE))
1087 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
1088 if (opt->jo_set & JO_OUT_BUF)
1089 {
1090 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1091 if (buf == NULL)
1092 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1093 }
1094 else
1095 {
1096 int msg = TRUE;
1097
1098 if (opt->jo_set2 & JO2_OUT_MSG)
1099 msg = opt->jo_message[PART_OUT];
1100 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
1101 }
1102 if (buf != NULL)
1103 {
1104 if (opt->jo_set & JO_OUT_MODIFIABLE)
1105 channel->ch_part[PART_OUT].ch_nomodifiable =
1106 !opt->jo_modifiable[PART_OUT];
1107
1108 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1109 {
1110 emsg(_(e_modifiable));
1111 }
1112 else
1113 {
1114 ch_log(channel, "writing out to buffer '%s'",
1115 (char *)buf->b_ffname);
1116 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1117 // if the buffer was deleted or unloaded resurrect it
1118 if (buf->b_ml.ml_mfp == NULL)
1119 prepare_buffer(buf);
1120 }
1121 }
1122 }
1123
1124 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1125 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1126 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1127 {
1128 buf_T *buf;
1129
1130 /* writing err to a buffer. Default mode is NL. */
1131 if (!(opt->jo_set & JO_ERR_MODE))
1132 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1133 if (opt->jo_io[PART_ERR] == JIO_OUT)
1134 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1135 else if (opt->jo_set & JO_ERR_BUF)
1136 {
1137 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1138 if (buf == NULL)
1139 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1140 }
1141 else
1142 {
1143 int msg = TRUE;
1144
1145 if (opt->jo_set2 & JO2_ERR_MSG)
1146 msg = opt->jo_message[PART_ERR];
1147 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1148 }
1149 if (buf != NULL)
1150 {
1151 if (opt->jo_set & JO_ERR_MODIFIABLE)
1152 channel->ch_part[PART_ERR].ch_nomodifiable =
1153 !opt->jo_modifiable[PART_ERR];
1154 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1155 {
1156 emsg(_(e_modifiable));
1157 }
1158 else
1159 {
1160 ch_log(channel, "writing err to buffer '%s'",
1161 (char *)buf->b_ffname);
1162 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1163 // if the buffer was deleted or unloaded resurrect it
1164 if (buf->b_ml.ml_mfp == NULL)
1165 prepare_buffer(buf);
1166 }
1167 }
1168 }
1169
1170 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1171 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1172 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
1173}
1174
1175/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001176 * Implements ch_open().
1177 */
1178 channel_T *
1179channel_open_func(typval_T *argvars)
1180{
1181 char_u *address;
1182 char_u *p;
1183 char *rest;
1184 int port;
1185 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001186 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001187
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001188 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001189 if (argvars[1].v_type != VAR_UNKNOWN
1190 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
1191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001193 return NULL;
1194 }
1195
1196 /* parse address */
1197 p = vim_strchr(address, ':');
1198 if (p == NULL)
1199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001201 return NULL;
1202 }
1203 *p++ = NUL;
1204 port = strtol((char *)p, &rest, 10);
1205 if (*address == NUL || port <= 0 || *rest != NUL)
1206 {
1207 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001209 return NULL;
1210 }
1211
1212 /* parse options */
1213 clear_job_options(&opt);
1214 opt.jo_mode = MODE_JSON;
1215 opt.jo_timeout = 2000;
1216 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02001217 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001218 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001219 if (opt.jo_timeout < 0)
1220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001222 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001223 }
1224
1225 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1226 if (channel != NULL)
1227 {
1228 opt.jo_set = JO_ALL;
1229 channel_set_options(channel, &opt);
1230 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001231theend:
1232 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001233 return channel;
1234}
1235
Bram Moolenaarde279892016-03-11 22:19:44 +01001236 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001237ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001238{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001239 sock_T *fd = &channel->ch_part[part].ch_fd;
1240
Bram Moolenaarde279892016-03-11 22:19:44 +01001241 if (*fd != INVALID_FD)
1242 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001243 if (part == PART_SOCK)
1244 sock_close(*fd);
1245 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001246 {
1247 /* When using a pty the same FD is set on multiple parts, only
1248 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001249 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1250 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1251 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001252 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001253#ifdef MSWIN
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001254 if (channel->ch_named_pipe)
1255 DisconnectNamedPipe((HANDLE)fd);
1256#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001257 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001258 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001259 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001260 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001261
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001262 /* channel is closed, may want to end the job if it was the last */
1263 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001264 }
1265}
1266
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001267 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001268channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001269{
Bram Moolenaarde279892016-03-11 22:19:44 +01001270 if (in != INVALID_FD)
1271 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001272 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001273 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001274# if defined(UNIX)
1275 /* Do not end the job when all output channels are closed, wait until
1276 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001277 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001278 channel->ch_to_be_closed |= (1U << PART_IN);
1279# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001280 }
1281 if (out != INVALID_FD)
1282 {
1283# if defined(FEAT_GUI)
1284 channel_gui_unregister_one(channel, PART_OUT);
1285# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001286 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001287 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001288 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001289# if defined(FEAT_GUI)
1290 channel_gui_register_one(channel, PART_OUT);
1291# endif
1292 }
1293 if (err != INVALID_FD)
1294 {
1295# if defined(FEAT_GUI)
1296 channel_gui_unregister_one(channel, PART_ERR);
1297# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001298 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001299 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001300 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001301# if defined(FEAT_GUI)
1302 channel_gui_register_one(channel, PART_ERR);
1303# endif
1304 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001305}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001306
Bram Moolenaard6051b52016-02-28 15:49:03 +01001307/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001308 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001309 * This does not keep a refcount, when the job is freed ch_job is cleared.
1310 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001311 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001312channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001313{
Bram Moolenaar77073442016-02-13 23:23:53 +01001314 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001315
1316 channel_set_options(channel, options);
1317
1318 if (job->jv_in_buf != NULL)
1319 {
1320 chanpart_T *in_part = &channel->ch_part[PART_IN];
1321
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001322 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001323 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001324 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001325 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001326 {
1327 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1328 {
1329 /* Special mode: send last-but-one line when appending a line
1330 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001331 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001332 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001333 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001334 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001335 }
1336 else
1337 in_part->ch_buf_top = options->jo_in_top;
1338 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001339 else
1340 in_part->ch_buf_top = 1;
1341 if (options->jo_set & JO_IN_BOT)
1342 in_part->ch_buf_bot = options->jo_in_bot;
1343 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001344 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001345 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001346}
1347
1348/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001349 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001350 */
1351 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001352channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001353 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001354 ch_part_T part,
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001355 callback_T *callback,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001356 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001357{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001358 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001359 cbq_T *item = ALLOC_ONE(cbq_T);
Bram Moolenaara07fec92016-02-05 21:04:08 +01001360
1361 if (item != NULL)
1362 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001363 copy_callback(&item->cq_callback, callback);
Bram Moolenaar77073442016-02-13 23:23:53 +01001364 item->cq_seq_nr = id;
1365 item->cq_prev = head->cq_prev;
1366 head->cq_prev = item;
1367 item->cq_next = NULL;
1368 if (item->cq_prev == NULL)
1369 head->cq_next = item;
1370 else
1371 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001372 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001373}
1374
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375 static void
1376write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1377{
1378 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001379 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001380 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001381 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001382
Bram Moolenaar655da312016-05-28 22:22:34 +02001383 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001384 if ((p = alloc(len + 2)) == NULL)
1385 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001386 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001387
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001388 if (channel->ch_write_text_mode)
1389 p[len] = CAR;
1390 else
1391 {
1392 for (i = 0; i < len; ++i)
1393 if (p[i] == NL)
1394 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001395
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001396 p[len] = NL;
1397 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001398 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001399 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001400 vim_free(p);
1401}
1402
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001403/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001404 * Return TRUE if "channel" can be written to.
1405 * Returns FALSE if the input is closed or the write would block.
1406 */
1407 static int
1408can_write_buf_line(channel_T *channel)
1409{
1410 chanpart_T *in_part = &channel->ch_part[PART_IN];
1411
1412 if (in_part->ch_fd == INVALID_FD)
1413 return FALSE; /* pipe was closed */
1414
1415 /* for testing: block every other attempt to write */
1416 if (in_part->ch_block_write == 1)
1417 in_part->ch_block_write = -1;
1418 else if (in_part->ch_block_write == -1)
1419 in_part->ch_block_write = 1;
1420
1421 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001422#ifndef MSWIN
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001423 {
1424# if defined(HAVE_SELECT)
1425 struct timeval tval;
1426 fd_set wfds;
1427 int ret;
1428
1429 FD_ZERO(&wfds);
1430 FD_SET((int)in_part->ch_fd, &wfds);
1431 tval.tv_sec = 0;
1432 tval.tv_usec = 0;
1433 for (;;)
1434 {
1435 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1436# ifdef EINTR
1437 SOCK_ERRNO;
1438 if (ret == -1 && errno == EINTR)
1439 continue;
1440# endif
1441 if (ret <= 0 || in_part->ch_block_write == 1)
1442 {
1443 if (ret > 0)
1444 ch_log(channel, "FAKED Input not ready for writing");
1445 else
1446 ch_log(channel, "Input not ready for writing");
1447 return FALSE;
1448 }
1449 break;
1450 }
1451# else
1452 struct pollfd fds;
1453
1454 fds.fd = in_part->ch_fd;
1455 fds.events = POLLOUT;
1456 if (poll(&fds, 1, 0) <= 0)
1457 {
1458 ch_log(channel, "Input not ready for writing");
1459 return FALSE;
1460 }
1461 if (in_part->ch_block_write == 1)
1462 {
1463 ch_log(channel, "FAKED Input not ready for writing");
1464 return FALSE;
1465 }
1466# endif
1467 }
1468#endif
1469 return TRUE;
1470}
1471
1472/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001473 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001474 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001475 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001476channel_write_in(channel_T *channel)
1477{
1478 chanpart_T *in_part = &channel->ch_part[PART_IN];
1479 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001480 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001481 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001482
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001483 if (buf == NULL || in_part->ch_buf_append)
1484 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001485 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001486 {
1487 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001488 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001489 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001490 return;
1491 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001492
1493 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1494 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1495 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001496 if (!can_write_buf_line(channel))
1497 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001498 write_buf_line(buf, lnum, channel);
1499 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001500 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001501
1502 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001503 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001505 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506
Bram Moolenaar014069a2016-03-03 22:51:40 +01001507 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001508 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001509 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001510#if defined(FEAT_TERMINAL)
1511 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001512 if (channel->ch_job != NULL)
1513 term_send_eof(channel);
1514#endif
1515
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001516 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001517 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001518 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001519
1520 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001521 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001522 }
1523 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001524 ch_log(channel, "Still %ld more lines to write",
1525 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001526}
1527
1528/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001529 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001530 */
1531 void
1532channel_buffer_free(buf_T *buf)
1533{
1534 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001535 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001536
1537 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001538 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001539 {
1540 chanpart_T *ch_part = &channel->ch_part[part];
1541
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001542 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001543 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001544 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001545 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001546 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001547 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001548 }
1549}
1550
1551/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001552 * Write any lines waiting to be written to "channel".
1553 */
1554 static void
1555channel_write_input(channel_T *channel)
1556{
1557 chanpart_T *in_part = &channel->ch_part[PART_IN];
1558
1559 if (in_part->ch_writeque.wq_next != NULL)
1560 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1561 else if (in_part->ch_bufref.br_buf != NULL)
1562 {
1563 if (in_part->ch_buf_append)
1564 channel_write_new_lines(in_part->ch_bufref.br_buf);
1565 else
1566 channel_write_in(channel);
1567 }
1568}
1569
1570/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001571 * Write any lines waiting to be written to a channel.
1572 */
1573 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001574channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001575{
1576 channel_T *channel;
1577
1578 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001579 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001580}
1581
1582/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001583 * Write appended lines above the last one in "buf" to the channel.
1584 */
1585 void
1586channel_write_new_lines(buf_T *buf)
1587{
1588 channel_T *channel;
1589 int found_one = FALSE;
1590
1591 /* There could be more than one channel for the buffer, loop over all of
1592 * them. */
1593 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1594 {
1595 chanpart_T *in_part = &channel->ch_part[PART_IN];
1596 linenr_T lnum;
1597 int written = 0;
1598
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001599 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001600 {
1601 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001602 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001603 found_one = TRUE;
1604 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1605 ++lnum)
1606 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001607 if (!can_write_buf_line(channel))
1608 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001609 write_buf_line(buf, lnum, channel);
1610 ++written;
1611 }
1612
1613 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001614 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001615 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001616 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001617 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001618 ch_log(channel, "Still %ld more lines to write",
1619 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001620
1621 in_part->ch_buf_bot = lnum;
1622 }
1623 }
1624 if (!found_one)
1625 buf->b_write_to_channel = FALSE;
1626}
1627
1628/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001629 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001630 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001631 */
1632 static void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001633invoke_callback(channel_T *channel, callback_T *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001634{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001635 typval_T rettv;
1636 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001637
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001638 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001640
Bram Moolenaar77073442016-02-13 23:23:53 +01001641 argv[0].v_type = VAR_CHANNEL;
1642 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001643
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001644 call_callback(callback, -1, &rettv, 2, argv, NULL,
1645 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001646 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001647 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001648}
1649
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001650/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001651 * Return the first node from "channel"/"part" without removing it.
1652 * Returns NULL if there is nothing.
1653 */
1654 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001655channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001656{
1657 readq_T *head = &channel->ch_part[part].ch_head;
1658
1659 return head->rq_next;
1660}
1661
1662/*
1663 * Return a pointer to the first NL in "node".
1664 * Skips over NUL characters.
1665 * Returns NULL if there is no NL.
1666 */
1667 char_u *
1668channel_first_nl(readq_T *node)
1669{
1670 char_u *buffer = node->rq_buffer;
1671 long_u i;
1672
1673 for (i = 0; i < node->rq_buflen; ++i)
1674 if (buffer[i] == NL)
1675 return buffer + i;
1676 return NULL;
1677}
1678
1679/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001680 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681 * The caller must free it.
1682 * Returns NULL if there is nothing.
1683 */
1684 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001685channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001687 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001688 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689 char_u *p;
1690
Bram Moolenaar77073442016-02-13 23:23:53 +01001691 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001692 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001693 if (outlen != NULL)
1694 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001696 p = node->rq_buffer;
1697 head->rq_next = node->rq_next;
1698 if (node->rq_next == NULL)
1699 head->rq_prev = NULL;
1700 else
1701 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 vim_free(node);
1703 return p;
1704}
1705
1706/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001708 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001709 */
1710 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001711channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001712{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001713 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001714 readq_T *node;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001715 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001716 char_u *res;
1717 char_u *p;
1718
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001719 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001720 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001721 len += node->rq_buflen;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001722 res = alloc(len + 1);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001723 if (res == NULL)
1724 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 p = res;
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 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001728 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001729 p += node->rq_buflen;
1730 }
1731 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001732
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001733 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001734 do
1735 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001737 vim_free(p);
1738 } while (p != NULL);
1739
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 if (outlen != NULL)
1741 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001742 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 *outlen += len;
1744 return res;
1745 }
1746
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001747 // Turn all NUL into NL, so that the result can be used as a string.
1748 p = res;
1749 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001750 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001751 if (*p == NUL)
1752 *p = NL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001753#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001754 else if (*p == 0x1b)
1755 {
1756 // crush the escape sequence OSC 0/1/2: ESC ]0;
1757 if (p + 3 < res + len
1758 && p[1] == ']'
1759 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1760 && p[3] == ';')
1761 {
1762 // '\a' becomes a NL
1763 while (p < res + (len - 1) && *p != '\a')
1764 ++p;
1765 // BEL is zero width characters, suppress display mistake
1766 // ConPTY (after 10.0.18317) requires advance checking
1767 if (p[-1] == NUL)
1768 p[-1] = 0x07;
1769 }
1770 }
1771#endif
1772 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001773 }
1774
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001775 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001776}
1777
1778/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001779 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001780 * Caller must check these bytes are available.
1781 */
1782 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001783channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001784{
1785 readq_T *head = &channel->ch_part[part].ch_head;
1786 readq_T *node = head->rq_next;
1787 char_u *buf = node->rq_buffer;
1788
1789 mch_memmove(buf, buf + len, node->rq_buflen - len);
1790 node->rq_buflen -= len;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001791 node->rq_buffer[node->rq_buflen] = NUL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001792}
1793
1794/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001795 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001797 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001798 */
1799 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001800channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001803 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001804 readq_T *last_node;
1805 readq_T *n;
1806 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001807 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001808 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001809
Bram Moolenaar77073442016-02-13 23:23:53 +01001810 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001811 return FAIL;
1812
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001813 last_node = node->rq_next;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001814 len = node->rq_buflen + last_node->rq_buflen;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001815 if (want_nl)
1816 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001817 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001818 {
1819 last_node = last_node->rq_next;
1820 len += last_node->rq_buflen;
1821 }
1822
Bram Moolenaar772153f2019-03-04 12:09:49 +01001823 p = newbuf = alloc(len + 1);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001825 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001826 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001827 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001828 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001829 node->rq_buffer = newbuf;
1830 for (n = node; n != last_node; )
1831 {
1832 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001833 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001834 p += n->rq_buflen;
1835 vim_free(n->rq_buffer);
1836 }
Bram Moolenaar772153f2019-03-04 12:09:49 +01001837 *p = NUL;
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001838 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001839
1840 /* dispose of the collapsed nodes and their buffers */
1841 for (n = node->rq_next; n != last_node; )
1842 {
1843 n = n->rq_next;
1844 vim_free(n->rq_prev);
1845 }
1846 node->rq_next = last_node->rq_next;
1847 if (last_node->rq_next == NULL)
1848 head->rq_prev = node;
1849 else
1850 last_node->rq_next->rq_prev = node;
1851 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001852 return OK;
1853}
1854
1855/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001856 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 * Returns OK or FAIL.
1859 */
1860 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001861channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863{
1864 readq_T *node;
1865 readq_T *head = &channel->ch_part[part].ch_head;
1866 char_u *p;
1867 int i;
1868
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001869 node = ALLOC_ONE(readq_T);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870 if (node == NULL)
1871 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001872 /* A NUL is added at the end, because netbeans code expects that.
1873 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001874 node->rq_buffer = alloc(len + 1);
1875 if (node->rq_buffer == NULL)
1876 {
1877 vim_free(node);
1878 return FAIL; /* out of memory */
1879 }
1880
1881 if (channel->ch_part[part].ch_mode == MODE_NL)
1882 {
1883 /* Drop any CR before a NL. */
1884 p = node->rq_buffer;
1885 for (i = 0; i < len; ++i)
1886 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1887 *p++ = buf[i];
1888 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001889 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890 }
1891 else
1892 {
1893 mch_memmove(node->rq_buffer, buf, len);
1894 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001895 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001896 }
1897
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001898 if (prepend)
1899 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001900 // prepend node to the head of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001901 node->rq_next = head->rq_next;
1902 node->rq_prev = NULL;
1903 if (head->rq_next == NULL)
1904 head->rq_prev = node;
1905 else
1906 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001907 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001908 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001909 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001911 // append node to the tail of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001912 node->rq_next = NULL;
1913 node->rq_prev = head->rq_prev;
1914 if (head->rq_prev == NULL)
1915 head->rq_next = node;
1916 else
1917 head->rq_prev->rq_next = node;
1918 head->rq_prev = node;
1919 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001920
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001921 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001922 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001923 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001924 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001925 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001926 fprintf(log_fd, "'\n");
1927 }
1928 return OK;
1929}
1930
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001931/*
1932 * Try to fill the buffer of "reader".
1933 * Returns FALSE when nothing was added.
1934 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001935 static int
1936channel_fill(js_read_T *reader)
1937{
1938 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001939 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001941 int keeplen;
1942 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001943 char_u *p;
1944
1945 if (next == NULL)
1946 return FALSE;
1947
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001948 keeplen = reader->js_end - reader->js_buf;
1949 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001950 {
1951 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001952 addlen = (int)STRLEN(next);
1953 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001954 if (p == NULL)
1955 {
1956 vim_free(next);
1957 return FALSE;
1958 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001959 mch_memmove(p, reader->js_buf, keeplen);
1960 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001961 vim_free(next);
1962 next = p;
1963 }
1964
1965 vim_free(reader->js_buf);
1966 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001967 return TRUE;
1968}
1969
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001970/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001971 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001972 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001973 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001974 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001975 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001976channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001977{
1978 js_read_T reader;
1979 typval_T listtv;
1980 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001981 chanpart_T *chanpart = &channel->ch_part[part];
1982 jsonq_T *head = &chanpart->ch_json_head;
1983 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001984 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001985
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001986 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001987 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001988
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001989 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001990 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001991 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001992 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001993 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994
1995 /* When a message is incomplete we wait for a short while for more to
1996 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001997 * or list will make us hang.
1998 * Do not generate error messages, they will be written in a channel log. */
1999 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002000 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002001 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002002 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002003 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002004 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002005 /* Only accept the response when it is a list with at least two
2006 * items. */
2007 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002008 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002009 if (listtv.v_type != VAR_LIST)
2010 ch_error(channel, "Did not receive a list, discarding");
2011 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002012 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002013 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002014 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002015 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 else
2017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002018 item = ALLOC_ONE(jsonq_T);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002019 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002020 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002021 else
2022 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002023 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002024 item->jq_value = alloc_tv();
2025 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002026 {
2027 vim_free(item);
2028 clear_tv(&listtv);
2029 }
2030 else
2031 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 *item->jq_value = listtv;
2033 item->jq_prev = head->jq_prev;
2034 head->jq_prev = item;
2035 item->jq_next = NULL;
2036 if (item->jq_prev == NULL)
2037 head->jq_next = item;
2038 else
2039 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002040 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041 }
2042 }
2043 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002044
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002045 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002046 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002047 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002048 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002049 size_t buflen = STRLEN(reader.js_buf);
2050
2051 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002052 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002053 /* First time encountering incomplete message or after receiving
2054 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002055 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002056 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002057 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002058 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002059 chanpart->ch_wait_len = buflen;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002060#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002061 chanpart->ch_deadline = GetTickCount() + 100L;
2062#else
2063 gettimeofday(&chanpart->ch_deadline, NULL);
2064 chanpart->ch_deadline.tv_usec += 100 * 1000;
2065 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2066 {
2067 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2068 ++chanpart->ch_deadline.tv_sec;
2069 }
2070#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002071 }
2072 else
2073 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002074 int timeout;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002075#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002076 timeout = GetTickCount() > chanpart->ch_deadline;
2077#else
2078 {
2079 struct timeval now_tv;
2080
2081 gettimeofday(&now_tv, NULL);
2082 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2083 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2084 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2085 }
2086#endif
2087 if (timeout)
2088 {
2089 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002090 chanpart->ch_wait_len = 0;
2091 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002092 }
2093 else
2094 {
2095 reader.js_used = 0;
2096 ch_log(channel, "still waiting on incomplete message");
2097 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002098 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002099 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002100
2101 if (status == FAIL)
2102 {
2103 ch_error(channel, "Decoding failed - discarding input");
2104 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002105 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002106 }
2107 else if (reader.js_buf[reader.js_used] != NUL)
2108 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002109 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002110 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002111 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2112 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002113 ret = status == MAYBE ? FALSE: TRUE;
2114 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002115 else
2116 ret = FALSE;
2117
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002118 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002119 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002120}
2121
2122/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002123 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002124 */
2125 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002126remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002127{
Bram Moolenaar77073442016-02-13 23:23:53 +01002128 if (node->cq_prev == NULL)
2129 head->cq_next = node->cq_next;
2130 else
2131 node->cq_prev->cq_next = node->cq_next;
2132 if (node->cq_next == NULL)
2133 head->cq_prev = node->cq_prev;
2134 else
2135 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002136}
2137
2138/*
2139 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002140 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002141 */
2142 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002143remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002144{
Bram Moolenaar77073442016-02-13 23:23:53 +01002145 if (node->jq_prev == NULL)
2146 head->jq_next = node->jq_next;
2147 else
2148 node->jq_prev->jq_next = node->jq_next;
2149 if (node->jq_next == NULL)
2150 head->jq_prev = node->jq_prev;
2151 else
2152 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002153 vim_free(node);
2154}
2155
2156/*
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002157 * Add "id" to the list of JSON message IDs we are waiting on.
2158 */
2159 static void
2160channel_add_block_id(chanpart_T *chanpart, int id)
2161{
2162 garray_T *gap = &chanpart->ch_block_ids;
2163
2164 if (gap->ga_growsize == 0)
2165 ga_init2(gap, (int)sizeof(int), 10);
2166 if (ga_grow(gap, 1) == OK)
2167 {
2168 ((int *)gap->ga_data)[gap->ga_len] = id;
2169 ++gap->ga_len;
2170 }
2171}
2172
2173/*
2174 * Remove "id" from the list of JSON message IDs we are waiting on.
2175 */
2176 static void
2177channel_remove_block_id(chanpart_T *chanpart, int id)
2178{
2179 garray_T *gap = &chanpart->ch_block_ids;
2180 int i;
2181
2182 for (i = 0; i < gap->ga_len; ++i)
2183 if (((int *)gap->ga_data)[i] == id)
2184 {
2185 --gap->ga_len;
2186 if (i < gap->ga_len)
2187 {
2188 int *p = ((int *)gap->ga_data) + i;
2189
2190 mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int));
2191 }
2192 return;
2193 }
2194 siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id);
2195}
2196
2197/*
2198 * Return TRUE if "id" is in the list of JSON message IDs we are waiting on.
2199 */
2200 static int
2201channel_has_block_id(chanpart_T *chanpart, int id)
2202{
2203 garray_T *gap = &chanpart->ch_block_ids;
2204 int i;
2205
2206 for (i = 0; i < gap->ga_len; ++i)
2207 if (((int *)gap->ga_data)[i] == id)
2208 return TRUE;
2209 return FALSE;
2210}
2211
2212/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002213 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002214 * When "id" is positive it must match the first number in the list.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002215 * When "id" is zero or negative jut get the first message. But not one
2216 * in the ch_block_ids list.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002217 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002218 * Return OK when found and return the value in "rettv".
2219 * Return FAIL otherwise.
2220 */
2221 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002222channel_get_json(
2223 channel_T *channel,
2224 ch_part_T part,
2225 int id,
2226 int without_callback,
2227 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002228{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002229 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002230 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002231
Bram Moolenaar77073442016-02-13 23:23:53 +01002232 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002233 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002234 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002235 typval_T *tv = &l->lv_first->li_tv;
2236
Bram Moolenaar958dc692016-12-01 15:34:12 +01002237 if ((without_callback || !item->jq_no_callback)
2238 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002239 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002240 || tv->vval.v_number == 0
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002241 || !channel_has_block_id(
2242 &channel->ch_part[part], tv->vval.v_number)))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002243 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002244 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002245 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002246 ch_log(channel, "Getting JSON message %ld",
2247 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002248 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002249 return OK;
2250 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002251 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002252 }
2253 return FAIL;
2254}
2255
Bram Moolenaar958dc692016-12-01 15:34:12 +01002256/*
2257 * Put back "rettv" into the JSON queue, there was no callback for it.
2258 * Takes over the values in "rettv".
2259 */
2260 static void
2261channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2262{
2263 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2264 jsonq_T *item = head->jq_next;
2265 jsonq_T *newitem;
2266
2267 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2268 /* last item was pushed back, append to the end */
2269 item = NULL;
2270 else while (item != NULL && item->jq_no_callback)
2271 /* append after the last item that was pushed back */
2272 item = item->jq_next;
2273
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002274 newitem = ALLOC_ONE(jsonq_T);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002275 if (newitem == NULL)
2276 clear_tv(rettv);
2277 else
2278 {
2279 newitem->jq_value = alloc_tv();
2280 if (newitem->jq_value == NULL)
2281 {
2282 vim_free(newitem);
2283 clear_tv(rettv);
2284 }
2285 else
2286 {
2287 newitem->jq_no_callback = FALSE;
2288 *newitem->jq_value = *rettv;
2289 if (item == NULL)
2290 {
2291 /* append to the end */
2292 newitem->jq_prev = head->jq_prev;
2293 head->jq_prev = newitem;
2294 newitem->jq_next = NULL;
2295 if (newitem->jq_prev == NULL)
2296 head->jq_next = newitem;
2297 else
2298 newitem->jq_prev->jq_next = newitem;
2299 }
2300 else
2301 {
2302 /* append after "item" */
2303 newitem->jq_prev = item;
2304 newitem->jq_next = item->jq_next;
2305 item->jq_next = newitem;
2306 if (newitem->jq_next == NULL)
2307 head->jq_prev = newitem;
2308 else
2309 newitem->jq_next->jq_prev = newitem;
2310 }
2311 }
2312 }
2313}
2314
Bram Moolenaarece61b02016-02-20 21:39:05 +01002315#define CH_JSON_MAX_ARGS 4
2316
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002317/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002318 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002319 * "argv[0]" is the command string.
2320 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002321 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002322 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002323channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002324{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 char_u *cmd = argv[0].vval.v_string;
2326 char_u *arg;
2327 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002328
Bram Moolenaarece61b02016-02-20 21:39:05 +01002329 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002330 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002332 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002334 return;
2335 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002336 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002337 if (arg == NULL)
2338 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002339
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002340 if (STRCMP(cmd, "ex") == 0)
2341 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002342 int save_called_emsg = called_emsg;
2343
2344 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002345 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002346 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002347 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002348 --emsg_silent;
2349 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002350 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002351 (char *)get_vim_var_str(VV_ERRMSG));
2352 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002353 }
2354 else if (STRCMP(cmd, "normal") == 0)
2355 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002356 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002357
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002358 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002359 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002360 ea.arg = arg;
2361 ea.addr_count = 0;
2362 ea.forceit = TRUE; /* no mapping */
2363 ex_normal(&ea);
2364 }
2365 else if (STRCMP(cmd, "redraw") == 0)
2366 {
2367 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002368
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002369 ch_log(channel, "redraw");
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002370 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002371 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002372 ex_redraw(&ea);
2373 showruler(FALSE);
2374 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002375 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002376 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002377 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002378 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002379 int is_call = cmd[0] == 'c';
2380 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002381
Bram Moolenaarece61b02016-02-20 21:39:05 +01002382 if (argv[id_idx].v_type != VAR_UNKNOWN
2383 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002384 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002385 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002386 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002387 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002388 }
2389 else if (is_call && argv[2].v_type != VAR_LIST)
2390 {
2391 ch_error(channel, "third argument for call must be a list");
2392 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002393 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002394 }
2395 else
2396 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002397 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002398 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002399 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002400 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002401
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002402 /* Don't pollute the display with errors. */
2403 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002405 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002406 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002407 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002408 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002409 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002410 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002411 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002412 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2413 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002414 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002415
2416 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002417 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002418 int id = argv[id_idx].vval.v_number;
2419
Bram Moolenaar55fab432016-02-07 16:53:13 +01002420 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002421 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002422 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002423 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002424 /* If evaluation failed or the result can't be encoded
2425 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002426 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002427 err_tv.v_type = VAR_STRING;
2428 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002429 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002430 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002431 if (json != NULL)
2432 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002433 channel_send(channel,
2434 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002435 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002436 vim_free(json);
2437 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002438 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002439 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002440 if (tv == &res_tv)
2441 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002442 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002443 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002444 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002445 }
2446 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002447 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002448 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002449 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002450 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002451}
2452
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002453/*
2454 * Invoke the callback at "cbhead".
2455 * Does not redraw but sets channel_need_redraw.
2456 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002457 static void
2458invoke_one_time_callback(
2459 channel_T *channel,
2460 cbq_T *cbhead,
2461 cbq_T *item,
2462 typval_T *argv)
2463{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002464 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002465 (char *)item->cq_callback.cb_name);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002466 /* Remove the item from the list first, if the callback
2467 * invokes ch_close() the list will be cleared. */
2468 remove_cb_node(cbhead, item);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002469 invoke_callback(channel, &item->cq_callback, argv);
2470 free_callback(&item->cq_callback);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002471 vim_free(item);
2472}
2473
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002474 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002475append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002476{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002477 bufref_T save_curbuf = {NULL, 0, 0};
2478 win_T *save_curwin = NULL;
2479 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002480 linenr_T lnum = buffer->b_ml.ml_line_count;
2481 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002482 chanpart_T *ch_part = &channel->ch_part[part];
2483 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002484 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002485
2486 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2487 {
2488 if (!ch_part->ch_nomod_error)
2489 {
2490 ch_error(channel, "Buffer is not modifiable, cannot append");
2491 ch_part->ch_nomod_error = TRUE;
2492 }
2493 return;
2494 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002495
2496 /* If the buffer is also used as input insert above the last
2497 * line. Don't write these lines. */
2498 if (save_write_to)
2499 {
2500 --lnum;
2501 buffer->b_write_to_channel = FALSE;
2502 }
2503
2504 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002505 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002506
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002507 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002508
2509 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2510 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2511
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002512 u_sync(TRUE);
2513 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002514 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002515
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002516 if (empty)
2517 {
2518 /* The buffer is empty, replace the first (dummy) line. */
2519 ml_replace(lnum, msg, TRUE);
2520 lnum = 0;
2521 }
2522 else
2523 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002524 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002525
2526 /* Restore curbuf/curwin/curtab */
2527 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2528
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002529 if (ch_part->ch_nomodifiable)
2530 buffer->b_p_ma = FALSE;
2531 else
2532 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002533
2534 if (buffer->b_nwindows > 0)
2535 {
2536 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002537
2538 FOR_ALL_WINDOWS(wp)
2539 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002540 if (wp->w_buffer == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002541 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002542 int move_cursor = save_write_to
2543 ? wp->w_cursor.lnum == lnum + 1
2544 : (wp->w_cursor.lnum == lnum
2545 && wp->w_cursor.col == 0);
2546
2547 // If the cursor is at or above the new line, move it one line
2548 // down. If the topline is outdated update it now.
2549 if (move_cursor || wp->w_topline > buffer->b_ml.ml_line_count)
2550 {
2551 if (move_cursor)
2552 ++wp->w_cursor.lnum;
2553 save_curwin = curwin;
2554 curwin = wp;
2555 curbuf = curwin->w_buffer;
2556 scroll_cursor_bot(0, FALSE);
2557 curwin = save_curwin;
2558 curbuf = curwin->w_buffer;
2559 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002560 }
2561 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002562 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002563 channel_need_redraw = TRUE;
2564 }
2565
2566 if (save_write_to)
2567 {
2568 channel_T *ch;
2569
2570 /* Find channels reading from this buffer and adjust their
2571 * next-to-read line number. */
2572 buffer->b_write_to_channel = TRUE;
2573 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2574 {
2575 chanpart_T *in_part = &ch->ch_part[PART_IN];
2576
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002577 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002578 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2579 }
2580 }
2581}
2582
Bram Moolenaar437905c2016-04-26 19:01:05 +02002583 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002584drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002585{
2586 char_u *msg;
2587
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002588 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002589 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002590 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002591 vim_free(msg);
2592 }
2593}
2594
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002595/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002596 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002597 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002598 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002599 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002600 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002601may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002602{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002603 char_u *msg = NULL;
2604 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002605 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002606 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002607 chanpart_T *ch_part = &channel->ch_part[part];
2608 ch_mode_T ch_mode = ch_part->ch_mode;
2609 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002610 cbq_T *cbitem;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002611 callback_T *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002612 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002613 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002614
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002615 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002616 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002617 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002618
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002619 /* Use a message-specific callback, part callback or channel callback */
2620 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2621 if (cbitem->cq_seq_nr == 0)
2622 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002623 if (cbitem != NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002624 callback = &cbitem->cq_callback;
2625 else if (ch_part->ch_callback.cb_name != NULL)
2626 callback = &ch_part->ch_callback;
2627 else if (channel->ch_callback.cb_name != NULL)
2628 callback = &channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002629
Bram Moolenaarec68a992016-10-03 21:37:41 +02002630 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002631 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2632 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002633 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002634 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002635 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002636 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002637 buffer = NULL;
2638 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002639
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002640 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002641 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002642 listitem_T *item;
2643 int argc = 0;
2644
Bram Moolenaard7ece102016-02-02 23:23:02 +01002645 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002646 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002647 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002648 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002649 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002650 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002651 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002652 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002653
Bram Moolenaarece61b02016-02-20 21:39:05 +01002654 for (item = listtv->vval.v_list->lv_first;
2655 item != NULL && argc < CH_JSON_MAX_ARGS;
2656 item = item->li_next)
2657 argv[argc++] = item->li_tv;
2658 while (argc < CH_JSON_MAX_ARGS)
2659 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002660
Bram Moolenaarece61b02016-02-20 21:39:05 +01002661 if (argv[0].v_type == VAR_STRING)
2662 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002663 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002664 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002665 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002666 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002667 }
2668
Bram Moolenaarece61b02016-02-20 21:39:05 +01002669 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002670 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002671 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002672 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002673 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002674 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002675 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002676 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002677 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002678 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002679 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002680 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002681 return FALSE;
2682 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002683 else
2684 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002685 /* If there is no callback or buffer drop the message. */
2686 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002687 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002688 /* If there is a close callback it may use ch_read() to get the
2689 * messages. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002690 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002691 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002692 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002693 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002694
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002695 if (ch_mode == MODE_NL)
2696 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002697 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002698 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002699 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002700
2701 /* See if we have a message ending in NL in the first buffer. If
2702 * not try to concatenate the first and the second buffer. */
2703 while (TRUE)
2704 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002705 node = channel_peek(channel, part);
2706 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002707 if (nl != NULL)
2708 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002709 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002710 {
2711 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2712 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002713 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002714 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002715 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002716 buf = node->rq_buffer;
2717
Bram Moolenaar772153f2019-03-04 12:09:49 +01002718 // Convert NUL to NL, the internal representation.
2719 for (p = buf; (nl == NULL || p < nl)
2720 && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002721 if (*p == NUL)
2722 *p = NL;
2723
Bram Moolenaar772153f2019-03-04 12:09:49 +01002724 if (nl == NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002725 {
Bram Moolenaar772153f2019-03-04 12:09:49 +01002726 // get the whole buffer, drop the NL
2727 msg = channel_get(channel, part, NULL);
2728 }
2729 else if (nl + 1 == buf + node->rq_buflen)
2730 {
2731 // get the whole buffer
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002732 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002733 *nl = NUL;
2734 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002735 else
2736 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002737 /* Copy the message into allocated memory (excluding the NL)
2738 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002739 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002740 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002741 }
2742 }
2743 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002744 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002745 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002746 * get everything we have.
2747 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002748 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002749 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002750
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002751 if (msg == NULL)
2752 return FALSE; /* out of memory (and avoids Coverity warning) */
2753
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002754 argv[1].v_type = VAR_STRING;
2755 argv[1].vval.v_string = msg;
2756 }
2757
Bram Moolenaara07fec92016-02-05 21:04:08 +01002758 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002759 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002760 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002761
Bram Moolenaar958dc692016-12-01 15:34:12 +01002762 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002763 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002764 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002765 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002766 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002767 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002768 break;
2769 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002770 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002771 {
2772 if (channel->ch_drop_never)
2773 {
2774 /* message must be read with ch_read() */
2775 channel_push_json(channel, part, listtv);
2776 listtv = NULL;
2777 }
2778 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002779 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002780 seq_nr);
2781 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002782 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002783 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002784 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002785 if (buffer != NULL)
2786 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002787 if (msg == NULL)
2788 /* JSON or JS mode: re-encode the message. */
2789 msg = json_encode(listtv, ch_mode);
2790 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002791 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002792#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002793 if (buffer->b_term != NULL)
2794 write_to_term(buffer, msg, channel);
2795 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002796#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002797 append_to_buffer(buffer, msg, channel, part);
2798 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002799 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002800
Bram Moolenaar187db502016-02-27 14:44:26 +01002801 if (callback != NULL)
2802 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002803 if (cbitem != NULL)
2804 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2805 else
2806 {
2807 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002808 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002809 (char *)callback->cb_name);
2810 invoke_callback(channel, callback, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002811 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002812 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002813 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002814 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002815 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002816
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002817 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002818 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002819 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002820
2821 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002822}
2823
Bram Moolenaar113e1072019-01-20 15:30:40 +01002824#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002825/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002826 * Return TRUE when channel "channel" is open for writing to.
2827 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002828 */
2829 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002830channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002831{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002832 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002833 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002834}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002835#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002836
2837/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002838 * Return TRUE when channel "channel" is open for reading or writing.
2839 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002840 */
2841 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002842channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002843{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002844 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002845 || channel->CH_IN_FD != INVALID_FD
2846 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002847 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002848}
2849
2850/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002851 * Return TRUE if "channel" has JSON or other typeahead.
2852 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002853 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002854channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002855{
2856 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2857
2858 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2859 {
2860 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002861
Bram Moolenaar4340fc92019-06-28 22:06:49 +02002862 if (head->jq_next == NULL)
2863 // Parse json from readahead, there might be a complete message to
2864 // process.
2865 channel_parse_json(channel, part);
2866
2867 return head->jq_next != NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002868 }
2869 return channel_peek(channel, part) != NULL;
2870}
2871
2872/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002873 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002874 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002875 */
2876 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002877channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002878{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002879 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002880 int has_readahead = FALSE;
2881
Bram Moolenaar77073442016-02-13 23:23:53 +01002882 if (channel == NULL)
2883 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002884 if (req_part == PART_OUT)
2885 {
2886 if (channel->CH_OUT_FD != INVALID_FD)
2887 return "open";
2888 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002889 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002890 }
2891 else if (req_part == PART_ERR)
2892 {
2893 if (channel->CH_ERR_FD != INVALID_FD)
2894 return "open";
2895 if (channel_has_readahead(channel, PART_ERR))
2896 has_readahead = TRUE;
2897 }
2898 else
2899 {
2900 if (channel_is_open(channel))
2901 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002902 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002903 if (channel_has_readahead(channel, part))
2904 {
2905 has_readahead = TRUE;
2906 break;
2907 }
2908 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002909
2910 if (has_readahead)
2911 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002912 return "closed";
2913}
2914
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002915 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002916channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002917{
2918 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002919 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002920 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002921 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002922 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002923
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002924 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002925 STRCAT(namebuf, "_");
2926 tail = STRLEN(namebuf);
2927
2928 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002929 if (chanpart->ch_fd != INVALID_FD)
2930 status = "open";
2931 else if (channel_has_readahead(channel, part))
2932 status = "buffered";
2933 else
2934 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002935 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002936
2937 STRCPY(namebuf + tail, "mode");
2938 switch (chanpart->ch_mode)
2939 {
2940 case MODE_NL: s = "NL"; break;
2941 case MODE_RAW: s = "RAW"; break;
2942 case MODE_JSON: s = "JSON"; break;
2943 case MODE_JS: s = "JS"; break;
2944 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002945 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002946
2947 STRCPY(namebuf + tail, "io");
2948 if (part == PART_SOCK)
2949 s = "socket";
2950 else switch (chanpart->ch_io)
2951 {
2952 case JIO_NULL: s = "null"; break;
2953 case JIO_PIPE: s = "pipe"; break;
2954 case JIO_FILE: s = "file"; break;
2955 case JIO_BUFFER: s = "buffer"; break;
2956 case JIO_OUT: s = "out"; break;
2957 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002958 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002959
2960 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002961 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002962}
2963
2964 void
2965channel_info(channel_T *channel, dict_T *dict)
2966{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002967 dict_add_number(dict, "id", channel->ch_id);
2968 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002969
2970 if (channel->ch_hostname != NULL)
2971 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002972 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2973 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002974 channel_part_info(channel, dict, "sock", PART_SOCK);
2975 }
2976 else
2977 {
2978 channel_part_info(channel, dict, "out", PART_OUT);
2979 channel_part_info(channel, dict, "err", PART_ERR);
2980 channel_part_info(channel, dict, "in", PART_IN);
2981 }
2982}
2983
Bram Moolenaar77073442016-02-13 23:23:53 +01002984/*
2985 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002986 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002987 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002988 */
2989 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002990channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002991{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002992 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002993
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002994#ifdef FEAT_GUI
2995 channel_gui_unregister(channel);
2996#endif
2997
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002998 ch_close_part(channel, PART_SOCK);
2999 ch_close_part(channel, PART_IN);
3000 ch_close_part(channel, PART_OUT);
3001 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003002
Bram Moolenaara9f02812017-08-05 17:40:38 +02003003 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003004 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02003005 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003006
Bram Moolenaara9f02812017-08-05 17:40:38 +02003007 /* Invoke callbacks and flush buffers before the close callback. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003008 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003009 ch_log(channel,
3010 "Invoking callbacks and flushing buffers before closing");
3011 for (part = PART_SOCK; part < PART_IN; ++part)
3012 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003013 if (channel->ch_close_cb.cb_name != NULL
Bram Moolenaara9f02812017-08-05 17:40:38 +02003014 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3015 {
3016 /* Increment the refcount to avoid the channel being freed
3017 * halfway. */
3018 ++channel->ch_refcount;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003019 if (channel->ch_close_cb.cb_name == NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003020 ch_log(channel, "flushing %s buffers before closing",
3021 part_names[part]);
3022 while (may_invoke_callback(channel, part))
3023 ;
3024 --channel->ch_refcount;
3025 }
3026 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003027
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003028 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003029 {
3030 typval_T argv[1];
3031 typval_T rettv;
3032 int dummy;
3033
3034 /* Increment the refcount to avoid the channel being freed
3035 * halfway. */
3036 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003037 ch_log(channel, "Invoking close callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003038 (char *)channel->ch_close_cb.cb_name);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003039 argv[0].v_type = VAR_CHANNEL;
3040 argv[0].vval.v_channel = channel;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003041 call_callback(&channel->ch_close_cb, -1,
3042 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003043 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003044 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003045
Bram Moolenaara9f02812017-08-05 17:40:38 +02003046 /* the callback is only called once */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003047 free_callback(&channel->ch_close_cb);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003048
Bram Moolenaara9f02812017-08-05 17:40:38 +02003049 if (channel_need_redraw)
3050 {
3051 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003052 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003053 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003054
Bram Moolenaara9f02812017-08-05 17:40:38 +02003055 if (!channel->ch_drop_never)
3056 /* any remaining messages are useless now */
3057 for (part = PART_SOCK; part < PART_IN; ++part)
3058 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003059
3060 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003061 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003062 }
3063
3064 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003065
3066#ifdef FEAT_TERMINAL
3067 term_channel_closed(channel);
3068#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003069}
3070
Bram Moolenaard04a0202016-01-26 23:30:18 +01003071/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003072 * Close the "in" part channel "channel".
3073 */
3074 void
3075channel_close_in(channel_T *channel)
3076{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003077 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003078}
3079
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003080 static void
3081remove_from_writeque(writeq_T *wq, writeq_T *entry)
3082{
3083 ga_clear(&entry->wq_ga);
3084 wq->wq_next = entry->wq_next;
3085 if (wq->wq_next == NULL)
3086 wq->wq_prev = NULL;
3087 else
3088 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003089 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003090}
3091
Bram Moolenaar0874a832016-09-01 15:11:51 +02003092/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003093 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003094 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003095 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003096channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003097{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003098 chanpart_T *ch_part = &channel->ch_part[part];
3099 jsonq_T *json_head = &ch_part->ch_json_head;
3100 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003101
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003102 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003103 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003104
3105 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003106 {
3107 cbq_T *node = cb_head->cq_next;
3108
3109 remove_cb_node(cb_head, node);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003110 free_callback(&node->cq_callback);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003111 vim_free(node);
3112 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003113
3114 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003115 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003116 free_tv(json_head->jq_next->jq_value);
3117 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003118 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003119
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003120 free_callback(&ch_part->ch_callback);
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003121 ga_clear(&ch_part->ch_block_ids);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003122
3123 while (ch_part->ch_writeque.wq_next != NULL)
3124 remove_from_writeque(&ch_part->ch_writeque,
3125 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003126}
3127
3128/*
3129 * Clear all the read buffers on "channel".
3130 */
3131 void
3132channel_clear(channel_T *channel)
3133{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003134 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003135 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003136 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003137 channel_clear_one(channel, PART_OUT);
3138 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003139 channel_clear_one(channel, PART_IN);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003140 free_callback(&channel->ch_callback);
3141 free_callback(&channel->ch_close_cb);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003142}
3143
Bram Moolenaar77073442016-02-13 23:23:53 +01003144#if defined(EXITFREE) || defined(PROTO)
3145 void
3146channel_free_all(void)
3147{
3148 channel_T *channel;
3149
Bram Moolenaard6051b52016-02-28 15:49:03 +01003150 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003151 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3152 channel_clear(channel);
3153}
3154#endif
3155
3156
Bram Moolenaar715d2852016-04-30 17:06:31 +02003157/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003158#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003159
3160/* Buffer size for reading incoming messages. */
3161#define MAXMSGSIZE 4096
3162
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003163#if defined(HAVE_SELECT)
3164/*
3165 * Add write fds where we are waiting for writing to be possible.
3166 */
3167 static int
3168channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3169{
3170 int maxfd = maxfd_arg;
3171 channel_T *ch;
3172
3173 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3174 {
3175 chanpart_T *in_part = &ch->ch_part[PART_IN];
3176
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003177 if (in_part->ch_fd != INVALID_FD
3178 && (in_part->ch_bufref.br_buf != NULL
3179 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003180 {
3181 FD_SET((int)in_part->ch_fd, wfds);
3182 if ((int)in_part->ch_fd >= maxfd)
3183 maxfd = (int)in_part->ch_fd + 1;
3184 }
3185 }
3186 return maxfd;
3187}
3188#else
3189/*
3190 * Add write fds where we are waiting for writing to be possible.
3191 */
3192 static int
3193channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3194{
3195 int nfd = nfd_in;
3196 channel_T *ch;
3197
3198 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3199 {
3200 chanpart_T *in_part = &ch->ch_part[PART_IN];
3201
Bram Moolenaar683b7962017-08-19 15:51:59 +02003202 if (in_part->ch_fd != INVALID_FD
3203 && (in_part->ch_bufref.br_buf != NULL
3204 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003205 {
3206 in_part->ch_poll_idx = nfd;
3207 fds[nfd].fd = in_part->ch_fd;
3208 fds[nfd].events = POLLOUT;
3209 ++nfd;
3210 }
3211 else
3212 in_part->ch_poll_idx = -1;
3213 }
3214 return nfd;
3215}
3216#endif
3217
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003218typedef enum {
3219 CW_READY,
3220 CW_NOT_READY,
3221 CW_ERROR
3222} channel_wait_result;
3223
Bram Moolenaard04a0202016-01-26 23:30:18 +01003224/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003225 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003226 * Return CW_READY when there is something to read.
3227 * Return CW_NOT_READY when there is nothing to read.
3228 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003229 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003230 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003231channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003232{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003233 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003234 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003235
Bram Moolenaar4f974752019-02-17 17:44:42 +01003236# ifdef MSWIN
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003237 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003238 {
3239 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003240 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003241 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003242 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003243
3244 /* reading from a pipe, not a socket */
3245 while (TRUE)
3246 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003247 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3248
3249 if (r && nread > 0)
3250 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003251
3252 if (channel->ch_named_pipe)
3253 {
3254 DisconnectNamedPipe((HANDLE)fd);
3255 ConnectNamedPipe((HANDLE)fd, NULL);
3256 }
3257 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003258 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003259
3260 /* perhaps write some buffer lines */
3261 channel_write_any_lines();
3262
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003263 sleep_time = deadline - GetTickCount();
3264 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003265 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003266 /* Wait for a little while. Very short at first, up to 10 msec
3267 * after looping a few times. */
3268 if (sleep_time > delay)
3269 sleep_time = delay;
3270 Sleep(sleep_time);
3271 delay = delay * 2;
3272 if (delay > 10)
3273 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003274 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003275 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003276 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003277#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003278 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003279#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003280 struct timeval tval;
3281 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003282 fd_set wfds;
3283 int ret;
3284 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003285
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003286 tval.tv_sec = timeout / 1000;
3287 tval.tv_usec = (timeout % 1000) * 1000;
3288 for (;;)
3289 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003290 FD_ZERO(&rfds);
3291 FD_SET((int)fd, &rfds);
3292
3293 /* Write lines to a pipe when a pipe can be written to. Need to
3294 * set this every time, some buffers may be done. */
3295 maxfd = (int)fd + 1;
3296 FD_ZERO(&wfds);
3297 maxfd = channel_fill_wfds(maxfd, &wfds);
3298
3299 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003300# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003301 SOCK_ERRNO;
3302 if (ret == -1 && errno == EINTR)
3303 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003304# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003305 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003306 {
3307 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003308 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003309 channel_write_any_lines();
3310 continue;
3311 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003312 break;
3313 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003314#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003315 for (;;)
3316 {
3317 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3318 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003319
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003320 fds[0].fd = fd;
3321 fds[0].events = POLLIN;
3322 nfd = channel_fill_poll_write(nfd, fds);
3323 if (poll(fds, nfd, timeout) > 0)
3324 {
3325 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003326 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003327 channel_write_any_lines();
3328 continue;
3329 }
3330 break;
3331 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003332#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003333 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003334 return CW_NOT_READY;
3335}
3336
3337 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003338ch_close_part_on_error(
3339 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003340{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003341 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003342
3343 if (is_err)
3344 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003345 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003346 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003347 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003348
3349 /* Queue a "DETACH" netbeans message in the command queue in order to
3350 * terminate the netbeans session later. Do not end the session here
3351 * directly as we may be running in the context of a call to
3352 * netbeans_parse_messages():
3353 * netbeans_parse_messages
3354 * -> autocmd triggered while processing the netbeans cmd
3355 * -> ui_breakcheck
3356 * -> gui event loop or select loop
3357 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003358 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003359 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003360 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003361 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003362 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3363
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003364 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003365 * close the channel yet, there may be something to read on another part.
3366 * When stdout and stderr use the same FD we get the error only on one of
3367 * them, also close the other. */
3368 if (part == PART_OUT || part == PART_ERR)
3369 {
3370 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3371
3372 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3373 ch_close_part(channel, other);
3374 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003375 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003376
3377#ifdef FEAT_GUI
3378 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003379 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003380#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003381}
3382
3383 static void
3384channel_close_now(channel_T *channel)
3385{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003386 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003387 if (channel->ch_nb_close_cb != NULL)
3388 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003389 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003390}
3391
3392/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003393 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003394 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003395 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003396 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003397 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003398channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003399{
3400 static char_u *buf = NULL;
3401 int len = 0;
3402 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003403 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003404 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003405
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003406 fd = channel->ch_part[part].ch_fd;
3407 if (fd == INVALID_FD)
3408 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003409 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003410 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003411 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003412 }
3413 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003414
3415 /* Allocate a buffer to read into. */
3416 if (buf == NULL)
3417 {
3418 buf = alloc(MAXMSGSIZE);
3419 if (buf == NULL)
3420 return; /* out of memory! */
3421 }
3422
3423 /* Keep on reading for as long as there is something to read.
3424 * Use select() or poll() to avoid blocking on a message that is exactly
3425 * MAXMSGSIZE long. */
3426 for (;;)
3427 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003428 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003429 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003430 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003431 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003432 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003433 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003434 if (len <= 0)
3435 break; /* error or nothing more to read */
3436
3437 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003438 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003439 readlen += len;
3440 if (len < MAXMSGSIZE)
3441 break; /* did read everything that's available */
3442 }
3443
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003444 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003445 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003446 {
3447 if (!channel->ch_keep_open)
3448 ch_close_part_on_error(channel, part, (len < 0), func);
3449 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003450#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003451 else if (CH_HAS_GUI && gtk_main_level() > 0)
3452 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003453 gtk_main_quit();
3454#endif
3455}
3456
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003457/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003458 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003459 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003460 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003461 * Returns what was read in allocated memory.
3462 * Returns NULL in case of error or timeout.
3463 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003464 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003465channel_read_block(
3466 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003467{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003468 char_u *buf;
3469 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003470 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003471 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003472 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003473 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003474
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003475 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003476 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003477
3478 while (TRUE)
3479 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003480 node = channel_peek(channel, part);
3481 if (node != NULL)
3482 {
3483 if (mode == MODE_RAW || (mode == MODE_NL
3484 && channel_first_nl(node) != NULL))
3485 /* got a complete message */
3486 break;
3487 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3488 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003489 /* If not blocking or nothing more is coming then return what we
3490 * have. */
3491 if (raw || fd == INVALID_FD)
3492 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003493 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003494
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003495 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003496 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003497 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003498 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003499 {
3500 ch_log(channel, "Timed out");
3501 return NULL;
3502 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003503 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003504 }
3505
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003506 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003507 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003508 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003509 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003510 }
3511 else
3512 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003513 char_u *p;
3514
3515 buf = node->rq_buffer;
3516 nl = channel_first_nl(node);
3517
3518 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003519 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003520 if (*p == NUL)
3521 *p = NL;
3522
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003523 if (nl == NULL)
3524 {
3525 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003526 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003527 }
3528 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003529 {
3530 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003531 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003532 *nl = NUL;
3533 }
3534 else
3535 {
3536 /* Copy the message into allocated memory and remove it from the
3537 * buffer. */
3538 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003539 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003540 }
3541 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003542 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003543 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003544 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003545}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003546
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003547/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003548 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003549 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003550 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003551 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003552 * In corner cases this can be called recursively, that is why ch_block_ids is
3553 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003554 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003555 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003556channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003557 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003558 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003559 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003560 int id,
3561 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003562{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003563 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003564 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003565 int timeout;
3566 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003567
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003568 ch_log(channel, "Blocking read JSON for id %d", id);
3569 if (id >= 0)
3570 channel_add_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003571 for (;;)
3572 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003573 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003574
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003575 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003576 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003577 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003578 if (id >= 0)
3579 channel_remove_block_id(chanpart, id);
3580 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003581 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003582 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003583
Bram Moolenaard7ece102016-02-02 23:23:02 +01003584 if (!more)
3585 {
3586 /* Handle any other messages in the queue. If done some more
3587 * messages may have arrived. */
3588 if (channel_parse_messages())
3589 continue;
3590
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003591 /* Wait for up to the timeout. If there was an incomplete message
3592 * use the deadline for that. */
3593 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003594 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003595 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003596#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003597 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3598#else
3599 {
3600 struct timeval now_tv;
3601
3602 gettimeofday(&now_tv, NULL);
3603 timeout = (chanpart->ch_deadline.tv_sec
3604 - now_tv.tv_sec) * 1000
3605 + (chanpart->ch_deadline.tv_usec
3606 - now_tv.tv_usec) / 1000
3607 + 1;
3608 }
3609#endif
3610 if (timeout < 0)
3611 {
3612 /* Something went wrong, channel_parse_json() didn't
3613 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003614 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003615 timeout = timeout_arg;
3616 }
3617 else if (timeout > timeout_arg)
3618 timeout = timeout_arg;
3619 }
3620 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003621 if (fd == INVALID_FD
3622 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003623 {
3624 if (timeout == timeout_arg)
3625 {
3626 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003627 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003628 break;
3629 }
3630 }
3631 else
3632 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003633 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003634 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003635 if (id >= 0)
3636 channel_remove_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003637 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003638}
3639
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003640/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003641 * Get the channel from the argument.
3642 * Returns NULL if the handle is invalid.
3643 * When "check_open" is TRUE check that the channel can be used.
3644 * When "reading" is TRUE "check_open" considers typeahead useful.
3645 * "part" is used to check typeahead, when PART_COUNT use the default part.
3646 */
3647 static channel_T *
3648get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3649{
3650 channel_T *channel = NULL;
3651 int has_readahead = FALSE;
3652
3653 if (tv->v_type == VAR_JOB)
3654 {
3655 if (tv->vval.v_job != NULL)
3656 channel = tv->vval.v_job->jv_channel;
3657 }
3658 else if (tv->v_type == VAR_CHANNEL)
3659 {
3660 channel = tv->vval.v_channel;
3661 }
3662 else
3663 {
3664 semsg(_(e_invarg2), tv_get_string(tv));
3665 return NULL;
3666 }
3667 if (channel != NULL && reading)
3668 has_readahead = channel_has_readahead(channel,
3669 part != PART_COUNT ? part : channel_part_read(channel));
3670
3671 if (check_open && (channel == NULL || (!channel_is_open(channel)
3672 && !(reading && has_readahead))))
3673 {
3674 emsg(_("E906: not an open channel"));
3675 return NULL;
3676 }
3677 return channel;
3678}
3679
3680/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003681 * Common for ch_read() and ch_readraw().
3682 */
3683 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003684common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003685{
3686 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003687 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003688 jobopt_T opt;
3689 int mode;
3690 int timeout;
3691 int id = -1;
3692 typval_T *listtv = NULL;
3693
3694 /* return an empty string by default */
3695 rettv->v_type = VAR_STRING;
3696 rettv->vval.v_string = NULL;
3697
3698 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003699 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003700 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003701 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003702
Bram Moolenaar437905c2016-04-26 19:01:05 +02003703 if (opt.jo_set & JO_PART)
3704 part = opt.jo_part;
3705 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003706 if (channel != NULL)
3707 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003708 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003709 part = channel_part_read(channel);
3710 mode = channel_get_mode(channel, part);
3711 timeout = channel_get_timeout(channel, part);
3712 if (opt.jo_set & JO_TIMEOUT)
3713 timeout = opt.jo_timeout;
3714
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003715 if (blob)
3716 {
3717 int outlen = 0;
3718 char_u *p = channel_read_block(channel, part,
3719 timeout, TRUE, &outlen);
3720 if (p != NULL)
3721 {
3722 blob_T *b = blob_alloc();
3723
3724 if (b != NULL)
3725 {
3726 b->bv_ga.ga_len = outlen;
3727 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3728 blob_free(b);
3729 else
3730 {
3731 memcpy(b->bv_ga.ga_data, p, outlen);
3732 rettv_blob_set(rettv, b);
3733 }
3734 }
3735 vim_free(p);
3736 }
3737 }
3738 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003739 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003740 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003741 else
3742 {
3743 if (opt.jo_set & JO_ID)
3744 id = opt.jo_id;
3745 channel_read_json_block(channel, part, timeout, id, &listtv);
3746 if (listtv != NULL)
3747 {
3748 *rettv = *listtv;
3749 vim_free(listtv);
3750 }
3751 else
3752 {
3753 rettv->v_type = VAR_SPECIAL;
3754 rettv->vval.v_number = VVAL_NONE;
3755 }
3756 }
3757 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003758
3759theend:
3760 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003761}
3762
Bram Moolenaar4f974752019-02-17 17:44:42 +01003763# if defined(MSWIN) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003764 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003765/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003766 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003767 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003768 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003769 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003770channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003771{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003772 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003773 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003774
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003775 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003776 for (channel = first_channel; channel != NULL;
3777 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003778 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003779 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003780 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003781 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003782 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003783 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003784 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003785 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003786 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003787}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003788# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003789
Bram Moolenaar4f974752019-02-17 17:44:42 +01003790# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003791/*
3792 * Check the channels for anything that is ready to be read.
3793 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003794 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003795 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003796 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003797channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003798{
3799 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003800 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003801 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003802
3803 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3804 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003805 if (only_keep_open && !channel->ch_keep_open)
3806 continue;
3807
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003808 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003809 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003810 {
3811 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003812 if (fd != INVALID_FD)
3813 {
3814 int r = channel_wait(channel, fd, 0);
3815
3816 if (r == CW_READY)
3817 channel_read(channel, part, "channel_handle_events");
3818 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003819 ch_close_part_on_error(channel, part, TRUE,
3820 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003821 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003822 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003823 }
3824}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003825# endif
3826
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003827# if defined(FEAT_GUI) || defined(PROTO)
3828/*
3829 * Return TRUE when there is any channel with a keep_open flag.
3830 */
3831 int
3832channel_any_keep_open()
3833{
3834 channel_T *channel;
3835
3836 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3837 if (channel->ch_keep_open)
3838 return TRUE;
3839 return FALSE;
3840}
3841# endif
3842
Bram Moolenaard04a0202016-01-26 23:30:18 +01003843/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003844 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003845 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003846 */
3847 void
3848channel_set_nonblock(channel_T *channel, ch_part_T part)
3849{
3850 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003851 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003852
3853 if (fd != INVALID_FD)
3854 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003855#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003856 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003857
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003858 ioctlsocket(fd, FIONBIO, &val);
3859#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003860 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003861#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003862 ch_part->ch_nonblocking = TRUE;
3863 }
3864}
3865
3866/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003867 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003868 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003869 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003870 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003871 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003872channel_send(
3873 channel_T *channel,
3874 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003875 char_u *buf_arg,
3876 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003877 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003878{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003879 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003880 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003881 chanpart_T *ch_part = &channel->ch_part[part];
3882 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003883
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003884 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003885 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003886 {
3887 if (!channel->ch_error && fun != NULL)
3888 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003889 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003890 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003891 }
3892 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003893 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003894 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003895
Bram Moolenaar0b146882018-09-06 16:27:24 +02003896 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3897 channel_set_nonblock(channel, part);
3898
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003899 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003900 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003901 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003902 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003903 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003904 fprintf(log_fd, "'\n");
3905 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003906 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003907 }
3908
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003909 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003910 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003911 writeq_T *wq = &ch_part->ch_writeque;
3912 char_u *buf;
3913 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003914
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003915 if (wq->wq_next != NULL)
3916 {
3917 /* first write what was queued */
3918 buf = wq->wq_next->wq_ga.ga_data;
3919 len = wq->wq_next->wq_ga.ga_len;
3920 did_use_queue = TRUE;
3921 }
3922 else
3923 {
3924 if (len_arg == 0)
3925 /* nothing to write, called from channel_select_check() */
3926 return OK;
3927 buf = buf_arg;
3928 len = len_arg;
3929 }
3930
3931 if (part == PART_SOCK)
3932 res = sock_write(fd, (char *)buf, len);
3933 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003934 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003935 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003936#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003937 if (channel->ch_named_pipe && res < 0)
3938 {
3939 DisconnectNamedPipe((HANDLE)fd);
3940 ConnectNamedPipe((HANDLE)fd, NULL);
3941 }
3942#endif
3943 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003944 if (res < 0 && (errno == EWOULDBLOCK
3945#ifdef EAGAIN
3946 || errno == EAGAIN
3947#endif
3948 ))
3949 res = 0; /* nothing got written */
3950
3951 if (res >= 0 && ch_part->ch_nonblocking)
3952 {
3953 writeq_T *entry = wq->wq_next;
3954
3955 if (did_use_queue)
3956 ch_log(channel, "Sent %d bytes now", res);
3957 if (res == len)
3958 {
3959 /* Wrote all the buf[len] bytes. */
3960 if (entry != NULL)
3961 {
3962 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003963 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003964 continue;
3965 }
3966 if (did_use_queue)
3967 ch_log(channel, "Write queue empty");
3968 }
3969 else
3970 {
3971 /* Wrote only buf[res] bytes, can't write more now. */
3972 if (entry != NULL)
3973 {
3974 if (res > 0)
3975 {
3976 /* Remove the bytes that were written. */
3977 mch_memmove(entry->wq_ga.ga_data,
3978 (char *)entry->wq_ga.ga_data + res,
3979 len - res);
3980 entry->wq_ga.ga_len -= res;
3981 }
3982 buf = buf_arg;
3983 len = len_arg;
3984 }
3985 else
3986 {
3987 buf += res;
3988 len -= res;
3989 }
3990 ch_log(channel, "Adding %d bytes to the write queue", len);
3991
3992 /* Append the not written bytes of the argument to the write
3993 * buffer. Limit entries to 4000 bytes. */
3994 if (wq->wq_prev != NULL
3995 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3996 {
3997 writeq_T *last = wq->wq_prev;
3998
3999 /* append to the last entry */
Bram Moolenaar0d071552019-07-08 22:04:03 +02004000 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004001 {
4002 mch_memmove((char *)last->wq_ga.ga_data
4003 + last->wq_ga.ga_len,
4004 buf, len);
4005 last->wq_ga.ga_len += len;
4006 }
4007 }
4008 else
4009 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004010 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004011
4012 if (last != NULL)
4013 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004014 last->wq_prev = wq->wq_prev;
4015 last->wq_next = NULL;
4016 if (wq->wq_prev == NULL)
4017 wq->wq_next = last;
4018 else
4019 wq->wq_prev->wq_next = last;
4020 wq->wq_prev = last;
4021 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004022 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004023 {
4024 mch_memmove(last->wq_ga.ga_data, buf, len);
4025 last->wq_ga.ga_len = len;
4026 }
4027 }
4028 }
4029 }
4030 }
4031 else if (res != len)
4032 {
4033 if (!channel->ch_error && fun != NULL)
4034 {
4035 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004036 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004037 }
4038 channel->ch_error = TRUE;
4039 return FAIL;
4040 }
4041
4042 channel->ch_error = FALSE;
4043 return OK;
4044 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004045}
4046
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004047/*
4048 * Common for "ch_sendexpr()" and "ch_sendraw()".
4049 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004050 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004051 * Otherwise returns NULL.
4052 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004053 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004054send_common(
4055 typval_T *argvars,
4056 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004057 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004058 int id,
4059 int eval,
4060 jobopt_T *opt,
4061 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004062 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004063{
4064 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004065 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004066
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004067 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004068 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004069 if (channel == NULL)
4070 return NULL;
4071 part_send = channel_part_send(channel);
4072 *part_read = channel_part_read(channel);
4073
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004074 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004075 return NULL;
4076
4077 /* Set the callback. An empty callback means no callback and not reading
4078 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4079 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004080 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004081 {
4082 if (eval)
4083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004084 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 return NULL;
4086 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004087 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 }
4089
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004090 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004091 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004092 return channel;
4093 return NULL;
4094}
4095
4096/*
4097 * common for "ch_evalexpr()" and "ch_sendexpr()"
4098 */
4099 void
4100ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4101{
4102 char_u *text;
4103 typval_T *listtv;
4104 channel_T *channel;
4105 int id;
4106 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004107 ch_part_T part_send;
4108 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004109 jobopt_T opt;
4110 int timeout;
4111
4112 /* return an empty string by default */
4113 rettv->v_type = VAR_STRING;
4114 rettv->vval.v_string = NULL;
4115
Bram Moolenaar437905c2016-04-26 19:01:05 +02004116 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004117 if (channel == NULL)
4118 return;
4119 part_send = channel_part_send(channel);
4120
4121 ch_mode = channel_get_mode(channel, part_send);
4122 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004124 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004125 return;
4126 }
4127
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004128 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004129 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004130 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004131 if (text == NULL)
4132 return;
4133
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004134 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004135 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4136 vim_free(text);
4137 if (channel != NULL && eval)
4138 {
4139 if (opt.jo_set & JO_TIMEOUT)
4140 timeout = opt.jo_timeout;
4141 else
4142 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004143 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4144 == OK)
4145 {
4146 list_T *list = listtv->vval.v_list;
4147
4148 /* Move the item from the list and then change the type to
4149 * avoid the value being freed. */
4150 *rettv = list->lv_last->li_tv;
4151 list->lv_last->li_tv.v_type = VAR_NUMBER;
4152 free_tv(listtv);
4153 }
4154 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004155 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156}
4157
4158/*
4159 * common for "ch_evalraw()" and "ch_sendraw()"
4160 */
4161 void
4162ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4163{
4164 char_u buf[NUMBUFLEN];
4165 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004166 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004167 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004168 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004169 jobopt_T opt;
4170 int timeout;
4171
4172 /* return an empty string by default */
4173 rettv->v_type = VAR_STRING;
4174 rettv->vval.v_string = NULL;
4175
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004176 if (argvars[1].v_type == VAR_BLOB)
4177 {
4178 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4179 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4180 }
4181 else
4182 {
4183 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004184 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004185 }
4186 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004187 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4188 if (channel != NULL && eval)
4189 {
4190 if (opt.jo_set & JO_TIMEOUT)
4191 timeout = opt.jo_timeout;
4192 else
4193 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004194 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004195 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004196 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004197 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004198}
4199
Bram Moolenaarf3360612017-10-01 16:21:31 +02004200# define KEEP_OPEN_TIME 20 /* msec */
4201
Bram Moolenaard04a0202016-01-26 23:30:18 +01004202# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004203/*
4204 * Add open channels to the poll struct.
4205 * Return the adjusted struct index.
4206 * The type of "fds" is hidden to avoid problems with the function proto.
4207 */
4208 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004209channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004210{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004211 int nfd = nfd_in;
4212 channel_T *channel;
4213 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004214 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004215
Bram Moolenaar77073442016-02-13 23:23:53 +01004216 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004217 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004218 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004219 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004220 chanpart_T *ch_part = &channel->ch_part[part];
4221
4222 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004223 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004224 if (channel->ch_keep_open)
4225 {
4226 /* For unknown reason poll() returns immediately for a
4227 * keep-open channel. Instead of adding it to the fds add
4228 * a short timeout and check, like polling. */
4229 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4230 *towait = KEEP_OPEN_TIME;
4231 }
4232 else
4233 {
4234 ch_part->ch_poll_idx = nfd;
4235 fds[nfd].fd = ch_part->ch_fd;
4236 fds[nfd].events = POLLIN;
4237 nfd++;
4238 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004239 }
4240 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004241 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004242 }
4243 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004244
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004245 nfd = channel_fill_poll_write(nfd, fds);
4246
Bram Moolenaare0874f82016-01-24 20:36:41 +01004247 return nfd;
4248}
4249
4250/*
4251 * The type of "fds" is hidden to avoid problems with the function proto.
4252 */
4253 int
4254channel_poll_check(int ret_in, void *fds_in)
4255{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004256 int ret = ret_in;
4257 channel_T *channel;
4258 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004259 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004260 int idx;
4261 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004262
Bram Moolenaar77073442016-02-13 23:23:53 +01004263 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004264 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004265 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004266 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004267 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004268
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004269 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004270 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004271 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004272 --ret;
4273 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004274 else if (channel->ch_part[part].ch_fd != INVALID_FD
4275 && channel->ch_keep_open)
4276 {
4277 /* polling a keep-open channel */
4278 channel_read(channel, part, "channel_poll_check_keep_open");
4279 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004280 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004281
4282 in_part = &channel->ch_part[PART_IN];
4283 idx = in_part->ch_poll_idx;
4284 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4285 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004286 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004287 --ret;
4288 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004289 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004290
4291 return ret;
4292}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004293# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004294
Bram Moolenaar4f974752019-02-17 17:44:42 +01004295# if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004296
Bram Moolenaare0874f82016-01-24 20:36:41 +01004297/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004298 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004299 */
4300 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004301channel_select_setup(
4302 int maxfd_in,
4303 void *rfds_in,
4304 void *wfds_in,
4305 struct timeval *tv,
4306 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004307{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004308 int maxfd = maxfd_in;
4309 channel_T *channel;
4310 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004311 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004312 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004313
Bram Moolenaar77073442016-02-13 23:23:53 +01004314 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004315 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004316 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004317 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004318 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004319
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004320 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004321 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004322 if (channel->ch_keep_open)
4323 {
4324 /* For unknown reason select() returns immediately for a
4325 * keep-open channel. Instead of adding it to the rfds add
4326 * a short timeout and check, like polling. */
4327 if (*tvp == NULL || tv->tv_sec > 0
4328 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4329 {
4330 *tvp = tv;
4331 tv->tv_sec = 0;
4332 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4333 }
4334 }
4335 else
4336 {
4337 FD_SET((int)fd, rfds);
4338 if (maxfd < (int)fd)
4339 maxfd = (int)fd;
4340 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004341 }
4342 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004343 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004344
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004345 maxfd = channel_fill_wfds(maxfd, wfds);
4346
Bram Moolenaare0874f82016-01-24 20:36:41 +01004347 return maxfd;
4348}
4349
4350/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004351 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004352 */
4353 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004354channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004355{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004356 int ret = ret_in;
4357 channel_T *channel;
4358 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004359 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004360 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004361 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004362
Bram Moolenaar77073442016-02-13 23:23:53 +01004363 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004364 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004365 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004366 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004367 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004368
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004369 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004370 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004371 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004372 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004373 --ret;
4374 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004375 else if (fd != INVALID_FD && channel->ch_keep_open)
4376 {
4377 /* polling a keep-open channel */
4378 channel_read(channel, part, "channel_select_check_keep_open");
4379 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004380 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004381
4382 in_part = &channel->ch_part[PART_IN];
4383 if (ret > 0 && in_part->ch_fd != INVALID_FD
4384 && FD_ISSET(in_part->ch_fd, wfds))
4385 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004386 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004387 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004388 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004389 --ret;
4390 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004391 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004392
4393 return ret;
4394}
Bram Moolenaar4f974752019-02-17 17:44:42 +01004395# endif /* !MSWIN && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004396
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004397/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004398 * Execute queued up commands.
4399 * Invoked from the main loop when it's safe to execute received commands.
4400 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004401 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004402 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004403channel_parse_messages(void)
4404{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004405 channel_T *channel = first_channel;
4406 int ret = FALSE;
4407 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004408 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004409#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004410 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004411
4412 ELAPSED_INIT(start_tv);
4413#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004414
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004415 ++safe_to_invoke_callback;
4416
Bram Moolenaard0b65022016-03-06 21:50:33 +01004417 /* Only do this message when another message was given, otherwise we get
4418 * lots of them. */
4419 if (did_log_msg)
4420 {
4421 ch_log(NULL, "looking for messages on channels");
4422 did_log_msg = FALSE;
4423 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004424 while (channel != NULL)
4425 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004426 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004427 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004428 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004429 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004430 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004431 channel = first_channel;
4432 continue;
4433 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004434 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004435 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004436 if (channel->ch_killing)
4437 {
4438 channel_free_contents(channel);
4439 channel->ch_job->jv_channel = NULL;
4440 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004441 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004442 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004443 channel = first_channel;
4444 continue;
4445 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004446 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004447 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004448 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004449 channel_free(channel);
4450 channel = first_channel;
4451 part = PART_SOCK;
4452 continue;
4453 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004454 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004455 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004456 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004457 /* Increase the refcount, in case the handler causes the channel
4458 * to be unreferenced or closed. */
4459 ++channel->ch_refcount;
4460 r = may_invoke_callback(channel, part);
4461 if (r == OK)
4462 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004463 if (channel_unref(channel) || (r == OK
4464#ifdef ELAPSED_FUNC
4465 /* Limit the time we loop here to 100 msec, otherwise
4466 * Vim becomes unresponsive when the callback takes
4467 * more than a bit of time. */
4468 && ELAPSED_FUNC(start_tv) < 100L
4469#endif
4470 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004471 {
4472 /* channel was freed or something was done, start over */
4473 channel = first_channel;
4474 part = PART_SOCK;
4475 continue;
4476 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004477 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004478 if (part < PART_ERR)
4479 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004480 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004481 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004482 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004483 part = PART_SOCK;
4484 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004485 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004486
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004487 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004488 {
4489 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004490 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004491 }
4492
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004493 --safe_to_invoke_callback;
4494
Bram Moolenaard7ece102016-02-02 23:23:02 +01004495 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004496}
4497
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004498/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004499 * Return TRUE if any channel has readahead. That means we should not block on
4500 * waiting for input.
4501 */
4502 int
4503channel_any_readahead(void)
4504{
4505 channel_T *channel = first_channel;
4506 ch_part_T part = PART_SOCK;
4507
4508 while (channel != NULL)
4509 {
4510 if (channel_has_readahead(channel, part))
4511 return TRUE;
4512 if (part < PART_ERR)
4513 ++part;
4514 else
4515 {
4516 channel = channel->ch_next;
4517 part = PART_SOCK;
4518 }
4519 }
4520 return FALSE;
4521}
4522
4523/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004524 * Mark references to lists used in channels.
4525 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004526 int
4527set_ref_in_channel(int copyID)
4528{
Bram Moolenaar77073442016-02-13 23:23:53 +01004529 int abort = FALSE;
4530 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004531 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004532
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004533 for (channel = first_channel; !abort && channel != NULL;
4534 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004535 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004536 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004537 tv.v_type = VAR_CHANNEL;
4538 tv.vval.v_channel = channel;
4539 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004540 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004541 return abort;
4542}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004543
4544/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004545 * Return the "part" to write to for "channel".
4546 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004547 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004548channel_part_send(channel_T *channel)
4549{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004550 if (channel->CH_SOCK_FD == INVALID_FD)
4551 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004552 return PART_SOCK;
4553}
4554
4555/*
4556 * Return the default "part" to read from for "channel".
4557 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004558 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004559channel_part_read(channel_T *channel)
4560{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004561 if (channel->CH_SOCK_FD == INVALID_FD)
4562 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004563 return PART_SOCK;
4564}
4565
4566/*
4567 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004568 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004569 */
4570 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004571channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004572{
Bram Moolenaar77073442016-02-13 23:23:53 +01004573 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004574 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004575 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004576}
4577
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004578/*
4579 * Return the timeout of "channel"/"part"
4580 */
4581 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004582channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004583{
4584 return channel->ch_part[part].ch_timeout;
4585}
4586
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004587 static int
4588handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4589{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004590 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004591
4592 opt->jo_set |= jo;
4593 if (STRCMP(val, "nl") == 0)
4594 *modep = MODE_NL;
4595 else if (STRCMP(val, "raw") == 0)
4596 *modep = MODE_RAW;
4597 else if (STRCMP(val, "js") == 0)
4598 *modep = MODE_JS;
4599 else if (STRCMP(val, "json") == 0)
4600 *modep = MODE_JSON;
4601 else
4602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004603 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004604 return FAIL;
4605 }
4606 return OK;
4607}
4608
4609 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004610handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004611{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004612 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004613
4614 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4615 if (STRCMP(val, "null") == 0)
4616 opt->jo_io[part] = JIO_NULL;
4617 else if (STRCMP(val, "pipe") == 0)
4618 opt->jo_io[part] = JIO_PIPE;
4619 else if (STRCMP(val, "file") == 0)
4620 opt->jo_io[part] = JIO_FILE;
4621 else if (STRCMP(val, "buffer") == 0)
4622 opt->jo_io[part] = JIO_BUFFER;
4623 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4624 opt->jo_io[part] = JIO_OUT;
4625 else
4626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004627 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004628 return FAIL;
4629 }
4630 return OK;
4631}
4632
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004633/*
4634 * Clear a jobopt_T before using it.
4635 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004636 void
4637clear_job_options(jobopt_T *opt)
4638{
4639 vim_memset(opt, 0, sizeof(jobopt_T));
4640}
4641
4642/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004643 * Free any members of a jobopt_T.
4644 */
4645 void
4646free_job_options(jobopt_T *opt)
4647{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004648 if (opt->jo_callback.cb_partial != NULL)
4649 partial_unref(opt->jo_callback.cb_partial);
4650 else if (opt->jo_callback.cb_name != NULL)
4651 func_unref(opt->jo_callback.cb_name);
4652 if (opt->jo_out_cb.cb_partial != NULL)
4653 partial_unref(opt->jo_out_cb.cb_partial);
4654 else if (opt->jo_out_cb.cb_name != NULL)
4655 func_unref(opt->jo_out_cb.cb_name);
4656 if (opt->jo_err_cb.cb_partial != NULL)
4657 partial_unref(opt->jo_err_cb.cb_partial);
4658 else if (opt->jo_err_cb.cb_name != NULL)
4659 func_unref(opt->jo_err_cb.cb_name);
4660 if (opt->jo_close_cb.cb_partial != NULL)
4661 partial_unref(opt->jo_close_cb.cb_partial);
4662 else if (opt->jo_close_cb.cb_name != NULL)
4663 func_unref(opt->jo_close_cb.cb_name);
4664 if (opt->jo_exit_cb.cb_partial != NULL)
4665 partial_unref(opt->jo_exit_cb.cb_partial);
4666 else if (opt->jo_exit_cb.cb_name != NULL)
4667 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004668 if (opt->jo_env != NULL)
4669 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004670}
4671
4672/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004673 * Get the PART_ number from the first character of an option name.
4674 */
4675 static int
4676part_from_char(int c)
4677{
4678 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4679}
4680
4681/*
4682 * Get the option entries from the dict in "tv", parse them and put the result
4683 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004684 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004685 * If an option value is invalid return FAIL.
4686 */
4687 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004688get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004689{
4690 typval_T *item;
4691 char_u *val;
4692 dict_T *dict;
4693 int todo;
4694 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004695 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004696
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004697 if (tv->v_type == VAR_UNKNOWN)
4698 return OK;
4699 if (tv->v_type != VAR_DICT)
4700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004701 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004702 return FAIL;
4703 }
4704 dict = tv->vval.v_dict;
4705 if (dict == NULL)
4706 return OK;
4707
4708 todo = (int)dict->dv_hashtab.ht_used;
4709 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4710 if (!HASHITEM_EMPTY(hi))
4711 {
4712 item = &dict_lookup(hi)->di_tv;
4713
4714 if (STRCMP(hi->hi_key, "mode") == 0)
4715 {
4716 if (!(supported & JO_MODE))
4717 break;
4718 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4719 return FAIL;
4720 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004721 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004722 {
4723 if (!(supported & JO_IN_MODE))
4724 break;
4725 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4726 == FAIL)
4727 return FAIL;
4728 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004729 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004730 {
4731 if (!(supported & JO_OUT_MODE))
4732 break;
4733 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4734 == FAIL)
4735 return FAIL;
4736 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004737 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004738 {
4739 if (!(supported & JO_ERR_MODE))
4740 break;
4741 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4742 == FAIL)
4743 return FAIL;
4744 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004745 else if (STRCMP(hi->hi_key, "noblock") == 0)
4746 {
4747 if (!(supported & JO_MODE))
4748 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004749 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004750 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004751 else if (STRCMP(hi->hi_key, "in_io") == 0
4752 || STRCMP(hi->hi_key, "out_io") == 0
4753 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004754 {
4755 if (!(supported & JO_OUT_IO))
4756 break;
4757 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4758 return FAIL;
4759 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004760 else if (STRCMP(hi->hi_key, "in_name") == 0
4761 || STRCMP(hi->hi_key, "out_name") == 0
4762 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 {
4764 part = part_from_char(*hi->hi_key);
4765
4766 if (!(supported & JO_OUT_IO))
4767 break;
4768 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4769 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004770 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004771 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004772 else if (STRCMP(hi->hi_key, "pty") == 0)
4773 {
4774 if (!(supported & JO_MODE))
4775 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004776 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004777 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004778 else if (STRCMP(hi->hi_key, "in_buf") == 0
4779 || STRCMP(hi->hi_key, "out_buf") == 0
4780 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004781 {
4782 part = part_from_char(*hi->hi_key);
4783
4784 if (!(supported & JO_OUT_IO))
4785 break;
4786 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004787 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004788 if (opt->jo_io_buf[part] <= 0)
4789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004790 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004791 return FAIL;
4792 }
4793 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4794 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004795 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004796 return FAIL;
4797 }
4798 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004799 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4800 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4801 {
4802 part = part_from_char(*hi->hi_key);
4803
4804 if (!(supported & JO_OUT_IO))
4805 break;
4806 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004807 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004808 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004809 else if (STRCMP(hi->hi_key, "out_msg") == 0
4810 || STRCMP(hi->hi_key, "err_msg") == 0)
4811 {
4812 part = part_from_char(*hi->hi_key);
4813
4814 if (!(supported & JO_OUT_IO))
4815 break;
4816 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004817 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004818 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004819 else if (STRCMP(hi->hi_key, "in_top") == 0
4820 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004821 {
4822 linenr_T *lp;
4823
4824 if (!(supported & JO_OUT_IO))
4825 break;
4826 if (hi->hi_key[3] == 't')
4827 {
4828 lp = &opt->jo_in_top;
4829 opt->jo_set |= JO_IN_TOP;
4830 }
4831 else
4832 {
4833 lp = &opt->jo_in_bot;
4834 opt->jo_set |= JO_IN_BOT;
4835 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004836 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004837 if (*lp < 0)
4838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004840 return FAIL;
4841 }
4842 }
4843 else if (STRCMP(hi->hi_key, "channel") == 0)
4844 {
4845 if (!(supported & JO_OUT_IO))
4846 break;
4847 opt->jo_set |= JO_CHANNEL;
4848 if (item->v_type != VAR_CHANNEL)
4849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004850 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004851 return FAIL;
4852 }
4853 opt->jo_channel = item->vval.v_channel;
4854 }
4855 else if (STRCMP(hi->hi_key, "callback") == 0)
4856 {
4857 if (!(supported & JO_CALLBACK))
4858 break;
4859 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004860 opt->jo_callback = get_callback(item);
4861 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004863 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004864 return FAIL;
4865 }
4866 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004867 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004868 {
4869 if (!(supported & JO_OUT_CALLBACK))
4870 break;
4871 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004872 opt->jo_out_cb = get_callback(item);
4873 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004874 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004875 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004876 return FAIL;
4877 }
4878 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004879 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004880 {
4881 if (!(supported & JO_ERR_CALLBACK))
4882 break;
4883 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004884 opt->jo_err_cb = get_callback(item);
4885 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004887 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004888 return FAIL;
4889 }
4890 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004891 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004892 {
4893 if (!(supported & JO_CLOSE_CALLBACK))
4894 break;
4895 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004896 opt->jo_close_cb = get_callback(item);
4897 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004899 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004900 return FAIL;
4901 }
4902 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004903 else if (STRCMP(hi->hi_key, "drop") == 0)
4904 {
4905 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004906 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004907
4908 if (STRCMP(val, "never") == 0)
4909 never = TRUE;
4910 else if (STRCMP(val, "auto") != 0)
4911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004912 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004913 return FAIL;
4914 }
4915 opt->jo_drop_never = never;
4916 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004917 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4918 {
4919 if (!(supported & JO_EXIT_CB))
4920 break;
4921 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004922 opt->jo_exit_cb = get_callback(item);
4923 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004925 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004926 return FAIL;
4927 }
4928 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004929#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004930 else if (STRCMP(hi->hi_key, "term_name") == 0)
4931 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004932 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004933 break;
4934 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004935 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004936 if (opt->jo_term_name == NULL)
4937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004938 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004939 return FAIL;
4940 }
4941 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004942 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4943 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004944 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004945 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004947 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004949 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004950 return FAIL;
4951 }
4952 opt->jo_set2 |= JO2_TERM_FINISH;
4953 opt->jo_term_finish = *val;
4954 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004955 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4956 {
4957 char_u *p;
4958
4959 if (!(supported2 & JO2_TERM_OPENCMD))
4960 break;
4961 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004963 if (p != NULL)
4964 {
4965 /* Must have %d and no other %. */
4966 p = vim_strchr(p, '%');
4967 if (p != NULL && (p[1] != 'd'
4968 || vim_strchr(p + 2, '%') != NULL))
4969 p = NULL;
4970 }
4971 if (p == NULL)
4972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004973 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004974 return FAIL;
4975 }
4976 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004977 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4978 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004979 char_u *p;
4980
4981 if (!(supported2 & JO2_EOF_CHARS))
4982 break;
4983 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004984 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004985 if (p == NULL)
4986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004987 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004988 return FAIL;
4989 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004990 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004991 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4992 {
4993 if (!(supported2 & JO2_TERM_ROWS))
4994 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004995 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004996 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004997 }
4998 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4999 {
5000 if (!(supported2 & JO2_TERM_COLS))
5001 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005002 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005003 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005004 }
5005 else if (STRCMP(hi->hi_key, "vertical") == 0)
5006 {
5007 if (!(supported2 & JO2_VERTICAL))
5008 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005009 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005010 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005011 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005012 else if (STRCMP(hi->hi_key, "curwin") == 0)
5013 {
5014 if (!(supported2 & JO2_CURWIN))
5015 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005016 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005017 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005018 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005019 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5020 {
5021 int nr;
5022
5023 if (!(supported2 & JO2_CURWIN))
5024 break;
5025 opt->jo_set2 |= JO2_BUFNR;
5026 nr = tv_get_number(item);
5027 if (nr <= 0)
5028 {
5029 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5030 return FAIL;
5031 }
5032 opt->jo_bufnr_buf = buflist_findnr(nr);
5033 if (opt->jo_bufnr_buf == NULL)
5034 {
5035 semsg(_(e_nobufnr), (long)nr);
5036 return FAIL;
5037 }
5038 if (opt->jo_bufnr_buf->b_nwindows == 0
5039 || opt->jo_bufnr_buf->b_term == NULL)
5040 {
5041 semsg(_(e_invarg2), "bufnr");
5042 return FAIL;
5043 }
5044 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005045 else if (STRCMP(hi->hi_key, "hidden") == 0)
5046 {
5047 if (!(supported2 & JO2_HIDDEN))
5048 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005049 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005050 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005051 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005052 else if (STRCMP(hi->hi_key, "norestore") == 0)
5053 {
5054 if (!(supported2 & JO2_NORESTORE))
5055 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005056 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005057 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005058 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005059 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5060 {
5061 if (!(supported2 & JO2_TERM_KILL))
5062 break;
5063 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005064 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005065 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005066 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005067 {
5068 char_u *p;
5069
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005070 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005071 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005072 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005073 p = tv_get_string_chk(item);
5074 if (p == NULL)
5075 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005076 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005077 return FAIL;
5078 }
5079 // Allow empty string, "winpty", "conpty".
5080 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5081 || STRCMP(p, "conpty") == 0))
5082 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005083 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005084 return FAIL;
5085 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005086 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005087 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005088# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5089 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5090 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005091 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005092 listitem_T *li;
5093 long_u rgb[16];
5094
5095 if (!(supported2 & JO2_ANSI_COLORS))
5096 break;
5097
5098 if (item == NULL || item->v_type != VAR_LIST
5099 || item->vval.v_list == NULL)
5100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005101 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005102 return FAIL;
5103 }
5104
5105 li = item->vval.v_list->lv_first;
5106 for (; li != NULL && n < 16; li = li->li_next, n++)
5107 {
5108 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005109 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005111 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005112 if (color_name == NULL)
5113 return FAIL;
5114
5115 guicolor = GUI_GET_COLOR(color_name);
5116 if (guicolor == INVALCOLOR)
5117 return FAIL;
5118
5119 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5120 }
5121
5122 if (n != 16 || li != NULL)
5123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005124 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005125 return FAIL;
5126 }
5127
5128 opt->jo_set2 |= JO2_ANSI_COLORS;
5129 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5130 }
5131# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005132#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005133 else if (STRCMP(hi->hi_key, "env") == 0)
5134 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005135 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005136 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005137 if (item->v_type != VAR_DICT)
5138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005139 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005140 return FAIL;
5141 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005142 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005143 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005144 if (opt->jo_env != NULL)
5145 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005146 }
5147 else if (STRCMP(hi->hi_key, "cwd") == 0)
5148 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005149 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005150 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005151 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005152 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005153#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005154 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5155#endif
5156 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005158 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005159 return FAIL;
5160 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005161 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005162 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005163 else if (STRCMP(hi->hi_key, "waittime") == 0)
5164 {
5165 if (!(supported & JO_WAITTIME))
5166 break;
5167 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005168 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005169 }
5170 else if (STRCMP(hi->hi_key, "timeout") == 0)
5171 {
5172 if (!(supported & JO_TIMEOUT))
5173 break;
5174 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005175 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005176 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005177 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005178 {
5179 if (!(supported & JO_OUT_TIMEOUT))
5180 break;
5181 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005182 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005183 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005184 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005185 {
5186 if (!(supported & JO_ERR_TIMEOUT))
5187 break;
5188 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005189 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005190 }
5191 else if (STRCMP(hi->hi_key, "part") == 0)
5192 {
5193 if (!(supported & JO_PART))
5194 break;
5195 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005196 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005197 if (STRCMP(val, "err") == 0)
5198 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005199 else if (STRCMP(val, "out") == 0)
5200 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005201 else
5202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005203 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005204 return FAIL;
5205 }
5206 }
5207 else if (STRCMP(hi->hi_key, "id") == 0)
5208 {
5209 if (!(supported & JO_ID))
5210 break;
5211 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005212 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 }
5214 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5215 {
5216 if (!(supported & JO_STOPONEXIT))
5217 break;
5218 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005219 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005220 opt->jo_soe_buf);
5221 if (opt->jo_stoponexit == NULL)
5222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005223 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005224 return FAIL;
5225 }
5226 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005227 else if (STRCMP(hi->hi_key, "block_write") == 0)
5228 {
5229 if (!(supported & JO_BLOCK_WRITE))
5230 break;
5231 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005232 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005233 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005234 else
5235 break;
5236 --todo;
5237 }
5238 if (todo > 0)
5239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005240 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005241 return FAIL;
5242 }
5243
5244 return OK;
5245}
5246
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005247static job_T *first_job = NULL;
5248
5249 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005250job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005251{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005252 int i;
5253
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005254 ch_log(job->jv_channel, "Freeing job");
5255 if (job->jv_channel != NULL)
5256 {
5257 /* The link from the channel to the job doesn't count as a reference,
5258 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005259 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005260 * NULL the reference. We don't set ch_job_killed, unreferencing the
5261 * job doesn't mean it stops running. */
5262 job->jv_channel->ch_job = NULL;
5263 channel_unref(job->jv_channel);
5264 }
5265 mch_clear_job(job);
5266
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005267 vim_free(job->jv_tty_in);
5268 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005269 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005270#ifdef UNIX
5271 vim_free(job->jv_termsig);
5272#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005273#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005274 vim_free(job->jv_tty_type);
5275#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005276 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005277 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005278 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005279 for (i = 0; job->jv_argv[i] != NULL; i++)
5280 vim_free(job->jv_argv[i]);
5281 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005282 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005283}
5284
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005285/*
5286 * Remove "job" from the list of jobs.
5287 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005288 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005289job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005290{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005291 if (job->jv_next != NULL)
5292 job->jv_next->jv_prev = job->jv_prev;
5293 if (job->jv_prev == NULL)
5294 first_job = job->jv_next;
5295 else
5296 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005297}
5298
5299 static void
5300job_free_job(job_T *job)
5301{
5302 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005303 vim_free(job);
5304}
5305
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005306 static void
5307job_free(job_T *job)
5308{
5309 if (!in_free_unref_items)
5310 {
5311 job_free_contents(job);
5312 job_free_job(job);
5313 }
5314}
5315
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005316job_T *jobs_to_free = NULL;
5317
5318/*
5319 * Put "job" in a list to be freed later, when it's no longer referenced.
5320 */
5321 static void
5322job_free_later(job_T *job)
5323{
5324 job_unlink(job);
5325 job->jv_next = jobs_to_free;
5326 jobs_to_free = job;
5327}
5328
5329 static void
5330free_jobs_to_free_later(void)
5331{
5332 job_T *job;
5333
5334 while (jobs_to_free != NULL)
5335 {
5336 job = jobs_to_free;
5337 jobs_to_free = job->jv_next;
5338 job_free_contents(job);
5339 vim_free(job);
5340 }
5341}
5342
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005343#if defined(EXITFREE) || defined(PROTO)
5344 void
5345job_free_all(void)
5346{
5347 while (first_job != NULL)
5348 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005349 free_jobs_to_free_later();
5350
5351# ifdef FEAT_TERMINAL
5352 free_unused_terminals();
5353# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005354}
5355#endif
5356
5357/*
5358 * Return TRUE if we need to check if the process of "job" has ended.
5359 */
5360 static int
5361job_need_end_check(job_T *job)
5362{
5363 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005364 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005365}
5366
5367/*
5368 * Return TRUE if the channel of "job" is still useful.
5369 */
5370 static int
5371job_channel_still_useful(job_T *job)
5372{
5373 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5374}
5375
5376/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005377 * Return TRUE if the channel of "job" is closeable.
5378 */
5379 static int
5380job_channel_can_close(job_T *job)
5381{
5382 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5383}
5384
5385/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005386 * Return TRUE if the job should not be freed yet. Do not free the job when
5387 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5388 * or when the associated channel will do something with the job output.
5389 */
5390 static int
5391job_still_useful(job_T *job)
5392{
5393 return job_need_end_check(job) || job_channel_still_useful(job);
5394}
5395
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005396#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005397/*
5398 * Return TRUE when there is any running job that we care about.
5399 */
5400 int
5401job_any_running()
5402{
5403 job_T *job;
5404
5405 for (job = first_job; job != NULL; job = job->jv_next)
5406 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005407 {
5408 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005409 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005410 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005411 return FALSE;
5412}
5413#endif
5414
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005415#if !defined(USE_ARGV) || defined(PROTO)
5416/*
5417 * Escape one argument for an external command.
5418 * Returns the escaped string in allocated memory. NULL when out of memory.
5419 */
5420 static char_u *
5421win32_escape_arg(char_u *arg)
5422{
5423 int slen, dlen;
5424 int escaping = 0;
5425 int i;
5426 char_u *s, *d;
5427 char_u *escaped_arg;
5428 int has_spaces = FALSE;
5429
5430 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005431 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005432 dlen = slen;
5433 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5434 {
5435 if (*s == '"' || *s == '\\')
5436 ++dlen;
5437 if (*s == ' ' || *s == '\t')
5438 has_spaces = TRUE;
5439 }
5440
5441 if (has_spaces)
5442 dlen += 2;
5443
5444 if (dlen == slen)
5445 return vim_strsave(arg);
5446
5447 /* Allocate memory for the result and fill it. */
5448 escaped_arg = alloc(dlen + 1);
5449 if (escaped_arg == NULL)
5450 return NULL;
5451 memset(escaped_arg, 0, dlen+1);
5452
5453 d = escaped_arg;
5454
5455 if (has_spaces)
5456 *d++ = '"';
5457
5458 for (s = arg; *s != NUL;)
5459 {
5460 switch (*s)
5461 {
5462 case '"':
5463 for (i = 0; i < escaping; i++)
5464 *d++ = '\\';
5465 escaping = 0;
5466 *d++ = '\\';
5467 *d++ = *s++;
5468 break;
5469 case '\\':
5470 escaping++;
5471 *d++ = *s++;
5472 break;
5473 default:
5474 escaping = 0;
5475 MB_COPY_CHAR(s, d);
5476 break;
5477 }
5478 }
5479
5480 /* add terminating quote and finish with a NUL */
5481 if (has_spaces)
5482 {
5483 for (i = 0; i < escaping; i++)
5484 *d++ = '\\';
5485 *d++ = '"';
5486 }
5487 *d = NUL;
5488
5489 return escaped_arg;
5490}
5491
5492/*
5493 * Build a command line from a list, taking care of escaping.
5494 * The result is put in gap->ga_data.
5495 * Returns FAIL when out of memory.
5496 */
5497 int
5498win32_build_cmd(list_T *l, garray_T *gap)
5499{
5500 listitem_T *li;
5501 char_u *s;
5502
5503 for (li = l->lv_first; li != NULL; li = li->li_next)
5504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005505 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005506 if (s == NULL)
5507 return FAIL;
5508 s = win32_escape_arg(s);
5509 if (s == NULL)
5510 return FAIL;
5511 ga_concat(gap, s);
5512 vim_free(s);
5513 if (li->li_next != NULL)
5514 ga_append(gap, ' ');
5515 }
5516 return OK;
5517}
5518#endif
5519
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005520/*
5521 * NOTE: Must call job_cleanup() only once right after the status of "job"
5522 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5523 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005524 * If the job is no longer used it will be removed from the list of jobs, and
5525 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005526 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005527 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005528job_cleanup(job_T *job)
5529{
5530 if (job->jv_status != JOB_ENDED)
5531 return;
5532
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005533 /* Ready to cleanup the job. */
5534 job->jv_status = JOB_FINISHED;
5535
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005536 /* When only channel-in is kept open, close explicitly. */
5537 if (job->jv_channel != NULL)
5538 ch_close_part(job->jv_channel, PART_IN);
5539
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005540 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005541 {
5542 typval_T argv[3];
5543 typval_T rettv;
5544 int dummy;
5545
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005546 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005547 ch_log(job->jv_channel, "Invoking exit callback %s",
5548 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005549 ++job->jv_refcount;
5550 argv[0].v_type = VAR_JOB;
5551 argv[0].vval.v_job = job;
5552 argv[1].v_type = VAR_NUMBER;
5553 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005554 call_callback(&job->jv_exit_cb, -1,
5555 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005556 clear_tv(&rettv);
5557 --job->jv_refcount;
5558 channel_need_redraw = TRUE;
5559 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005560
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005561 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5562 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005563
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005564 // Do not free the job in case the close callback of the associated channel
5565 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005566 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005567 // The job was already unreferenced and the associated channel was
5568 // detached, now that it ended it can be freed. However, a caller might
5569 // still use it, thus free it a bit later.
5570 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005571}
5572
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005573/*
5574 * Mark references in jobs that are still useful.
5575 */
5576 int
5577set_ref_in_job(int copyID)
5578{
5579 int abort = FALSE;
5580 job_T *job;
5581 typval_T tv;
5582
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005583 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005584 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005585 {
5586 tv.v_type = VAR_JOB;
5587 tv.vval.v_job = job;
5588 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5589 }
5590 return abort;
5591}
5592
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005593/*
5594 * Dereference "job". Note that after this "job" may have been freed.
5595 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005596 void
5597job_unref(job_T *job)
5598{
5599 if (job != NULL && --job->jv_refcount <= 0)
5600 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005601 /* Do not free the job if there is a channel where the close callback
5602 * may get the job info. */
5603 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005604 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005605 /* Do not free the job when it has not ended yet and there is a
5606 * "stoponexit" flag or an exit callback. */
5607 if (!job_need_end_check(job))
5608 {
5609 job_free(job);
5610 }
5611 else if (job->jv_channel != NULL)
5612 {
5613 /* Do remove the link to the channel, otherwise it hangs
5614 * around until Vim exits. See job_free() for refcount. */
5615 ch_log(job->jv_channel, "detaching channel from job");
5616 job->jv_channel->ch_job = NULL;
5617 channel_unref(job->jv_channel);
5618 job->jv_channel = NULL;
5619 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005620 }
5621 }
5622}
5623
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005624 int
5625free_unused_jobs_contents(int copyID, int mask)
5626{
5627 int did_free = FALSE;
5628 job_T *job;
5629
5630 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005631 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005632 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005633 {
5634 /* Free the channel and ordinary items it contains, but don't
5635 * recurse into Lists, Dictionaries etc. */
5636 job_free_contents(job);
5637 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005638 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005639 return did_free;
5640}
5641
5642 void
5643free_unused_jobs(int copyID, int mask)
5644{
5645 job_T *job;
5646 job_T *job_next;
5647
5648 for (job = first_job; job != NULL; job = job_next)
5649 {
5650 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005651 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005652 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005653 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005654 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005655 job_free_job(job);
5656 }
5657 }
5658}
5659
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005660/*
5661 * Allocate a job. Sets the refcount to one and sets options default.
5662 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005663 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005664job_alloc(void)
5665{
5666 job_T *job;
5667
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005668 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005669 if (job != NULL)
5670 {
5671 job->jv_refcount = 1;
5672 job->jv_stoponexit = vim_strsave((char_u *)"term");
5673
5674 if (first_job != NULL)
5675 {
5676 first_job->jv_prev = job;
5677 job->jv_next = first_job;
5678 }
5679 first_job = job;
5680 }
5681 return job;
5682}
5683
5684 void
5685job_set_options(job_T *job, jobopt_T *opt)
5686{
5687 if (opt->jo_set & JO_STOPONEXIT)
5688 {
5689 vim_free(job->jv_stoponexit);
5690 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5691 job->jv_stoponexit = NULL;
5692 else
5693 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5694 }
5695 if (opt->jo_set & JO_EXIT_CB)
5696 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005697 free_callback(&job->jv_exit_cb);
5698 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005699 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005700 job->jv_exit_cb.cb_name = NULL;
5701 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005702 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005703 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005704 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005705 }
5706}
5707
5708/*
5709 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5710 */
5711 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005712job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005713{
5714 job_T *job;
5715
5716 for (job = first_job; job != NULL; job = job->jv_next)
5717 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005718 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005719}
5720
5721/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005722 * Return TRUE when there is any job that has an exit callback and might exit,
5723 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005724 */
5725 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005726has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005727{
5728 job_T *job;
5729
5730 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005731 /* Only should check if the channel has been closed, if the channel is
5732 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005733 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5734 || (job->jv_status == JOB_FINISHED
5735 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005736 return TRUE;
5737 return FALSE;
5738}
5739
Bram Moolenaar97792de2016-10-15 18:36:49 +02005740#define MAX_CHECK_ENDED 8
5741
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005742/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005743 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005744 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005745 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005746 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005747job_check_ended(void)
5748{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005749 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005750 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005751
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005752 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005753 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005754 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005755
Bram Moolenaar97792de2016-10-15 18:36:49 +02005756 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005757 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005758 // NOTE: mch_detect_ended_job() must only return a job of which the
5759 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005760 job_T *job = mch_detect_ended_job(first_job);
5761
5762 if (job == NULL)
5763 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005764 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005765 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005766 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005767
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005768 // Actually free jobs that were cleaned up.
5769 free_jobs_to_free_later();
5770
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005771 if (channel_need_redraw)
5772 {
5773 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005774 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005775 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005776 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005777}
5778
5779/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005780 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005781 * "argv_arg" is only for Unix.
5782 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005783 * The returned job has a refcount of one.
5784 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005785 */
5786 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005787job_start(
5788 typval_T *argvars,
5789 char **argv_arg,
5790 jobopt_T *opt_arg,
5791 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005792{
5793 job_T *job;
5794 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005795 char **argv = NULL;
5796 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005797#if defined(UNIX)
5798# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005799 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005800#else
5801 garray_T ga;
5802#endif
5803 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005804 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005805
5806 job = job_alloc();
5807 if (job == NULL)
5808 return NULL;
5809
5810 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005811#ifndef USE_ARGV
5812 ga_init2(&ga, (int)sizeof(char*), 20);
5813#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005814
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005815 if (opt_arg != NULL)
5816 opt = *opt_arg;
5817 else
5818 {
5819 /* Default mode is NL. */
5820 clear_job_options(&opt);
5821 opt.jo_mode = MODE_NL;
5822 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005823 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5824 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5825 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005826 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005827 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005828
5829 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005830 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005831 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5832 && opt.jo_io[part] == JIO_FILE
5833 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5834 || *opt.jo_io_name[part] == NUL))
5835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005836 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005837 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005838 }
5839
5840 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5841 {
5842 buf_T *buf = NULL;
5843
5844 /* check that we can find the buffer before starting the job */
5845 if (opt.jo_set & JO_IN_BUF)
5846 {
5847 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5848 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005849 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005850 }
5851 else if (!(opt.jo_set & JO_IN_NAME))
5852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005853 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005854 }
5855 else
5856 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5857 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005858 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005859 if (buf->b_ml.ml_mfp == NULL)
5860 {
5861 char_u numbuf[NUMBUFLEN];
5862 char_u *s;
5863
5864 if (opt.jo_set & JO_IN_BUF)
5865 {
5866 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5867 s = numbuf;
5868 }
5869 else
5870 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005871 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005872 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005873 }
5874 job->jv_in_buf = buf;
5875 }
5876
5877 job_set_options(job, &opt);
5878
Bram Moolenaar13568252018-03-16 20:46:58 +01005879#ifdef USE_ARGV
5880 if (argv_arg != NULL)
5881 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005882 /* Make a copy of argv_arg for job->jv_argv. */
5883 for (i = 0; argv_arg[i] != NULL; i++)
5884 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005885 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005886 if (argv == NULL)
5887 goto theend;
5888 for (i = 0; i < argc; i++)
5889 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5890 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005891 }
5892 else
5893#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005894 if (argvars[0].v_type == VAR_STRING)
5895 {
5896 /* Command is a string. */
5897 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005898 if (cmd == NULL || *cmd == NUL)
5899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005900 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005901 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005902 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005903
5904 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005905 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005906 }
5907 else if (argvars[0].v_type != VAR_LIST
5908 || argvars[0].vval.v_list == NULL
5909 || argvars[0].vval.v_list->lv_len < 1)
5910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005911 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005912 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005913 }
5914 else
5915 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005916 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005917
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005918 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005919 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005920#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005921 if (win32_build_cmd(l, &ga) == FAIL)
5922 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005923 cmd = ga.ga_data;
5924#endif
5925 }
5926
Bram Moolenaar20608922018-04-21 22:30:08 +02005927 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005928 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005929
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005930#ifdef USE_ARGV
5931 if (ch_log_active())
5932 {
5933 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005934
5935 ga_init2(&ga, (int)sizeof(char), 200);
5936 for (i = 0; i < argc; ++i)
5937 {
5938 if (i > 0)
5939 ga_concat(&ga, (char_u *)" ");
5940 ga_concat(&ga, (char_u *)argv[i]);
5941 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005942 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005943 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005944 ga_clear(&ga);
5945 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005946 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005947#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005948 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005949 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005950#endif
5951
5952 /* If the channel is reading from a buffer, write lines now. */
5953 if (job->jv_channel != NULL)
5954 channel_write_in(job->jv_channel);
5955
5956theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005957#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005958 vim_free(ga.ga_data);
5959#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005960 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005961 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005962 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005963 return job;
5964}
5965
5966/*
5967 * Get the status of "job" and invoke the exit callback when needed.
5968 * The returned string is not allocated.
5969 */
5970 char *
5971job_status(job_T *job)
5972{
5973 char *result;
5974
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005975 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005976 /* No need to check, dead is dead. */
5977 result = "dead";
5978 else if (job->jv_status == JOB_FAILED)
5979 result = "fail";
5980 else
5981 {
5982 result = mch_job_status(job);
5983 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005984 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005985 }
5986 return result;
5987}
5988
Bram Moolenaar8950a562016-03-12 15:22:55 +01005989/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005990 * Send a signal to "job". Implements job_stop().
5991 * When "type" is not NULL use this for the type.
5992 * Otherwise use argvars[1] for the type.
5993 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005994 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005995job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005996{
5997 char_u *arg;
5998
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005999 if (type != NULL)
6000 arg = (char_u *)type;
6001 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006002 arg = (char_u *)"";
6003 else
6004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006005 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006006 if (arg == NULL)
6007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006008 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006009 return 0;
6010 }
6011 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006012 if (job->jv_status == JOB_FAILED)
6013 {
6014 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6015 return 0;
6016 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006017 if (job->jv_status == JOB_ENDED)
6018 {
6019 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6020 return 0;
6021 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006022 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006023 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006024 return 0;
6025
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006026 /* Assume that only "kill" will kill the job. */
6027 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006028 job->jv_channel->ch_job_killed = TRUE;
6029
6030 /* We don't try freeing the job, obviously the caller still has a
6031 * reference to it. */
6032 return 1;
6033}
6034
Bram Moolenaarf2732452018-06-03 14:47:35 +02006035 void
6036invoke_prompt_callback(void)
6037{
6038 typval_T rettv;
6039 int dummy;
6040 typval_T argv[2];
6041 char_u *text;
6042 char_u *prompt;
6043 linenr_T lnum = curbuf->b_ml.ml_line_count;
6044
6045 // Add a new line for the prompt before invoking the callback, so that
6046 // text can always be inserted above the last line.
6047 ml_append(lnum, (char_u *)"", 0, FALSE);
6048 curwin->w_cursor.lnum = lnum + 1;
6049 curwin->w_cursor.col = 0;
6050
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006051 if (curbuf->b_prompt_callback.cb_name == NULL
6052 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006053 return;
6054 text = ml_get(lnum);
6055 prompt = prompt_text();
6056 if (STRLEN(text) >= STRLEN(prompt))
6057 text += STRLEN(prompt);
6058 argv[0].v_type = VAR_STRING;
6059 argv[0].vval.v_string = vim_strsave(text);
6060 argv[1].v_type = VAR_UNKNOWN;
6061
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006062 call_callback(&curbuf->b_prompt_callback, -1,
6063 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
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;
6075 int dummy;
6076 typval_T argv[1];
6077
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006078 if (curbuf->b_prompt_interrupt.cb_name == NULL
6079 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006080 return FALSE;
6081 argv[0].v_type = VAR_UNKNOWN;
6082
6083 got_int = FALSE; // don't skip executing commands
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006084 call_callback(&curbuf->b_prompt_interrupt, -1,
6085 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006086 clear_tv(&rettv);
6087 return TRUE;
6088}
6089
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006090/*
6091 * "prompt_setcallback({buffer}, {callback})" function
6092 */
6093 void
6094f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6095{
6096 buf_T *buf;
6097 callback_T callback;
6098
6099 if (check_secure())
6100 return;
6101 buf = tv_get_buf(&argvars[0], FALSE);
6102 if (buf == NULL)
6103 return;
6104
6105 callback = get_callback(&argvars[1]);
6106 if (callback.cb_name == NULL)
6107 return;
6108
6109 free_callback(&buf->b_prompt_callback);
6110 set_callback(&buf->b_prompt_callback, &callback);
6111}
6112
6113/*
6114 * "prompt_setinterrupt({buffer}, {callback})" function
6115 */
6116 void
6117f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6118{
6119 buf_T *buf;
6120 callback_T callback;
6121
6122 if (check_secure())
6123 return;
6124 buf = tv_get_buf(&argvars[0], FALSE);
6125 if (buf == NULL)
6126 return;
6127
6128 callback = get_callback(&argvars[1]);
6129 if (callback.cb_name == NULL)
6130 return;
6131
6132 free_callback(&buf->b_prompt_interrupt);
6133 set_callback(&buf->b_prompt_interrupt, &callback);
6134}
6135
6136/*
6137 * "prompt_setprompt({buffer}, {text})" function
6138 */
6139 void
6140f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6141{
6142 buf_T *buf;
6143 char_u *text;
6144
6145 if (check_secure())
6146 return;
6147 buf = tv_get_buf(&argvars[0], FALSE);
6148 if (buf == NULL)
6149 return;
6150
6151 text = tv_get_string(&argvars[1]);
6152 vim_free(buf->b_prompt_text);
6153 buf->b_prompt_text = vim_strsave(text);
6154}
6155
6156/*
6157 * "ch_canread()" function
6158 */
6159 void
6160f_ch_canread(typval_T *argvars, typval_T *rettv)
6161{
6162 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6163
6164 rettv->vval.v_number = 0;
6165 if (channel != NULL)
6166 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6167 || channel_has_readahead(channel, PART_OUT)
6168 || channel_has_readahead(channel, PART_ERR);
6169}
6170
6171/*
6172 * "ch_close()" function
6173 */
6174 void
6175f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6176{
6177 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6178
6179 if (channel != NULL)
6180 {
6181 channel_close(channel, FALSE);
6182 channel_clear(channel);
6183 }
6184}
6185
6186/*
6187 * "ch_close()" function
6188 */
6189 void
6190f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6191{
6192 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6193
6194 if (channel != NULL)
6195 channel_close_in(channel);
6196}
6197
6198/*
6199 * "ch_getbufnr()" function
6200 */
6201 void
6202f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6203{
6204 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6205
6206 rettv->vval.v_number = -1;
6207 if (channel != NULL)
6208 {
6209 char_u *what = tv_get_string(&argvars[1]);
6210 int part;
6211
6212 if (STRCMP(what, "err") == 0)
6213 part = PART_ERR;
6214 else if (STRCMP(what, "out") == 0)
6215 part = PART_OUT;
6216 else if (STRCMP(what, "in") == 0)
6217 part = PART_IN;
6218 else
6219 part = PART_SOCK;
6220 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6221 rettv->vval.v_number =
6222 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6223 }
6224}
6225
6226/*
6227 * "ch_getjob()" function
6228 */
6229 void
6230f_ch_getjob(typval_T *argvars, typval_T *rettv)
6231{
6232 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6233
6234 if (channel != NULL)
6235 {
6236 rettv->v_type = VAR_JOB;
6237 rettv->vval.v_job = channel->ch_job;
6238 if (channel->ch_job != NULL)
6239 ++channel->ch_job->jv_refcount;
6240 }
6241}
6242
6243/*
6244 * "ch_info()" function
6245 */
6246 void
6247f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6248{
6249 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6250
6251 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6252 channel_info(channel, rettv->vval.v_dict);
6253}
6254
6255/*
6256 * "ch_log()" function
6257 */
6258 void
6259f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6260{
6261 char_u *msg = tv_get_string(&argvars[0]);
6262 channel_T *channel = NULL;
6263
6264 if (argvars[1].v_type != VAR_UNKNOWN)
6265 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6266
6267 ch_log(channel, "%s", msg);
6268}
6269
6270/*
6271 * "ch_logfile()" function
6272 */
6273 void
6274f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6275{
6276 char_u *fname;
6277 char_u *opt = (char_u *)"";
6278 char_u buf[NUMBUFLEN];
6279
6280 /* Don't open a file in restricted mode. */
6281 if (check_restricted() || check_secure())
6282 return;
6283 fname = tv_get_string(&argvars[0]);
6284 if (argvars[1].v_type == VAR_STRING)
6285 opt = tv_get_string_buf(&argvars[1], buf);
6286 ch_logfile(fname, opt);
6287}
6288
6289/*
6290 * "ch_open()" function
6291 */
6292 void
6293f_ch_open(typval_T *argvars, typval_T *rettv)
6294{
6295 rettv->v_type = VAR_CHANNEL;
6296 if (check_restricted() || check_secure())
6297 return;
6298 rettv->vval.v_channel = channel_open_func(argvars);
6299}
6300
6301/*
6302 * "ch_read()" function
6303 */
6304 void
6305f_ch_read(typval_T *argvars, typval_T *rettv)
6306{
6307 common_channel_read(argvars, rettv, FALSE, FALSE);
6308}
6309
6310/*
6311 * "ch_readblob()" function
6312 */
6313 void
6314f_ch_readblob(typval_T *argvars, typval_T *rettv)
6315{
6316 common_channel_read(argvars, rettv, TRUE, TRUE);
6317}
6318
6319/*
6320 * "ch_readraw()" function
6321 */
6322 void
6323f_ch_readraw(typval_T *argvars, typval_T *rettv)
6324{
6325 common_channel_read(argvars, rettv, TRUE, FALSE);
6326}
6327
6328/*
6329 * "ch_evalexpr()" function
6330 */
6331 void
6332f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6333{
6334 ch_expr_common(argvars, rettv, TRUE);
6335}
6336
6337/*
6338 * "ch_sendexpr()" function
6339 */
6340 void
6341f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6342{
6343 ch_expr_common(argvars, rettv, FALSE);
6344}
6345
6346/*
6347 * "ch_evalraw()" function
6348 */
6349 void
6350f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6351{
6352 ch_raw_common(argvars, rettv, TRUE);
6353}
6354
6355/*
6356 * "ch_sendraw()" function
6357 */
6358 void
6359f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6360{
6361 ch_raw_common(argvars, rettv, FALSE);
6362}
6363
6364/*
6365 * "ch_setoptions()" function
6366 */
6367 void
6368f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6369{
6370 channel_T *channel;
6371 jobopt_T opt;
6372
6373 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6374 if (channel == NULL)
6375 return;
6376 clear_job_options(&opt);
6377 if (get_job_options(&argvars[1], &opt,
6378 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6379 channel_set_options(channel, &opt);
6380 free_job_options(&opt);
6381}
6382
6383/*
6384 * "ch_status()" function
6385 */
6386 void
6387f_ch_status(typval_T *argvars, typval_T *rettv)
6388{
6389 channel_T *channel;
6390 jobopt_T opt;
6391 int part = -1;
6392
6393 /* return an empty string by default */
6394 rettv->v_type = VAR_STRING;
6395 rettv->vval.v_string = NULL;
6396
6397 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6398
6399 if (argvars[1].v_type != VAR_UNKNOWN)
6400 {
6401 clear_job_options(&opt);
6402 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6403 && (opt.jo_set & JO_PART))
6404 part = opt.jo_part;
6405 }
6406
6407 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6408}
6409
6410/*
6411 * Get the job from the argument.
6412 * Returns NULL if the job is invalid.
6413 */
6414 static job_T *
6415get_job_arg(typval_T *tv)
6416{
6417 job_T *job;
6418
6419 if (tv->v_type != VAR_JOB)
6420 {
6421 semsg(_(e_invarg2), tv_get_string(tv));
6422 return NULL;
6423 }
6424 job = tv->vval.v_job;
6425
6426 if (job == NULL)
6427 emsg(_("E916: not a valid job"));
6428 return job;
6429}
6430
6431/*
6432 * "job_getchannel()" function
6433 */
6434 void
6435f_job_getchannel(typval_T *argvars, typval_T *rettv)
6436{
6437 job_T *job = get_job_arg(&argvars[0]);
6438
6439 if (job != NULL)
6440 {
6441 rettv->v_type = VAR_CHANNEL;
6442 rettv->vval.v_channel = job->jv_channel;
6443 if (job->jv_channel != NULL)
6444 ++job->jv_channel->ch_refcount;
6445 }
6446}
6447
6448/*
6449 * Implementation of job_info().
6450 */
6451 static void
6452job_info(job_T *job, dict_T *dict)
6453{
6454 dictitem_T *item;
6455 varnumber_T nr;
6456 list_T *l;
6457 int i;
6458
6459 dict_add_string(dict, "status", (char_u *)job_status(job));
6460
6461 item = dictitem_alloc((char_u *)"channel");
6462 if (item == NULL)
6463 return;
6464 item->di_tv.v_type = VAR_CHANNEL;
6465 item->di_tv.vval.v_channel = job->jv_channel;
6466 if (job->jv_channel != NULL)
6467 ++job->jv_channel->ch_refcount;
6468 if (dict_add(dict, item) == FAIL)
6469 dictitem_free(item);
6470
6471#ifdef UNIX
6472 nr = job->jv_pid;
6473#else
6474 nr = job->jv_proc_info.dwProcessId;
6475#endif
6476 dict_add_number(dict, "process", nr);
6477 dict_add_string(dict, "tty_in", job->jv_tty_in);
6478 dict_add_string(dict, "tty_out", job->jv_tty_out);
6479
6480 dict_add_number(dict, "exitval", job->jv_exitval);
6481 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6482 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6483#ifdef UNIX
6484 dict_add_string(dict, "termsig", job->jv_termsig);
6485#endif
6486#ifdef MSWIN
6487 dict_add_string(dict, "tty_type", job->jv_tty_type);
6488#endif
6489
6490 l = list_alloc();
6491 if (l != NULL)
6492 {
6493 dict_add_list(dict, "cmd", l);
6494 if (job->jv_argv != NULL)
6495 for (i = 0; job->jv_argv[i] != NULL; i++)
6496 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6497 }
6498}
6499
6500/*
6501 * Implementation of job_info() to return info for all jobs.
6502 */
6503 static void
6504job_info_all(list_T *l)
6505{
6506 job_T *job;
6507 typval_T tv;
6508
6509 for (job = first_job; job != NULL; job = job->jv_next)
6510 {
6511 tv.v_type = VAR_JOB;
6512 tv.vval.v_job = job;
6513
6514 if (list_append_tv(l, &tv) != OK)
6515 return;
6516 }
6517}
6518
6519/*
6520 * "job_info()" function
6521 */
6522 void
6523f_job_info(typval_T *argvars, typval_T *rettv)
6524{
6525 if (argvars[0].v_type != VAR_UNKNOWN)
6526 {
6527 job_T *job = get_job_arg(&argvars[0]);
6528
6529 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6530 job_info(job, rettv->vval.v_dict);
6531 }
6532 else if (rettv_list_alloc(rettv) == OK)
6533 job_info_all(rettv->vval.v_list);
6534}
6535
6536/*
6537 * "job_setoptions()" function
6538 */
6539 void
6540f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6541{
6542 job_T *job = get_job_arg(&argvars[0]);
6543 jobopt_T opt;
6544
6545 if (job == NULL)
6546 return;
6547 clear_job_options(&opt);
6548 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6549 job_set_options(job, &opt);
6550 free_job_options(&opt);
6551}
6552
6553/*
6554 * "job_start()" function
6555 */
6556 void
6557f_job_start(typval_T *argvars, typval_T *rettv)
6558{
6559 rettv->v_type = VAR_JOB;
6560 if (check_restricted() || check_secure())
6561 return;
6562 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6563}
6564
6565/*
6566 * "job_status()" function
6567 */
6568 void
6569f_job_status(typval_T *argvars, typval_T *rettv)
6570{
6571 job_T *job = get_job_arg(&argvars[0]);
6572
6573 if (job != NULL)
6574 {
6575 rettv->v_type = VAR_STRING;
6576 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6577 }
6578}
6579
6580/*
6581 * "job_stop()" function
6582 */
6583 void
6584f_job_stop(typval_T *argvars, typval_T *rettv)
6585{
6586 job_T *job = get_job_arg(&argvars[0]);
6587
6588 if (job != NULL)
6589 rettv->vval.v_number = job_stop(job, argvars, NULL);
6590}
6591
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006592#endif /* FEAT_JOB_CHANNEL */