blob: d728c77d9ffdae0deb0915e3fe74770d644e57f9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaard04a0202016-01-26 23:30:18 +010023#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
Bram Moolenaar24058382019-01-24 23:11:49 +010083 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010084 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010085 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010086 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010087
Bram Moolenaar24058382019-01-24 23:11:49 +010088 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 {
Bram Moolenaar24058382019-01-24 23:11:49 +010090 if (todo > MAX_NAMED_PIPE_SIZE)
91 size = MAX_NAMED_PIPE_SIZE;
92 else
Bram Moolenaardec01202019-01-28 20:04:24 +010093 size = (DWORD)todo;
Bram 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 Moolenaar4ac2e8d2018-04-08 12:38:26 +0200194#ifndef PROTO /* prototype is in vim.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 Moolenaar4d919d72016-02-05 22:36:41 +0100237#ifdef _WIN32
238# 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 Moolenaar7b3ca762016-02-14 19:13:43 +0100297 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(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. */
351 if (channel->ch_close_cb != NULL)
352 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 Moolenaarfdd6ce42016-02-28 22:21:38 +0100369 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100370 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200371 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200372 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
373 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200374 || ((channel->ch_part[PART_ERR].ch_callback != 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 Moolenaarf12d9832016-01-29 21:11:25 +0100690#ifdef WIN32
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 Moolenaarf12d9832016-01-29 21:11:25 +0100699#ifdef WIN32
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 (
761#ifdef _WIN32
762 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 Moolenaar045a2842016-03-08 22:33:07 +0100807#ifndef WIN32
808 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 Moolenaare081e212016-02-28 22:33:46 +0100814#ifndef WIN32
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 Moolenaar81661fb2016-02-18 22:23:34 +0100827#ifndef WIN32
828 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 Moolenaare081e212016-02-28 22:33:46 +0100845#ifdef WIN32
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
896#ifndef WIN32
897 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 Moolenaar4d919d72016-02-05 22:36:41 +0100933#ifdef _WIN32
934 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/*
955 * Implements ch_open().
956 */
957 channel_T *
958channel_open_func(typval_T *argvars)
959{
960 char_u *address;
961 char_u *p;
962 char *rest;
963 int port;
964 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200965 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100966
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100967 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100968 if (argvars[1].v_type != VAR_UNKNOWN
969 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100971 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100972 return NULL;
973 }
974
975 /* parse address */
976 p = vim_strchr(address, ':');
977 if (p == NULL)
978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100980 return NULL;
981 }
982 *p++ = NUL;
983 port = strtol((char *)p, &rest, 10);
984 if (*address == NUL || port <= 0 || *rest != NUL)
985 {
986 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100987 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100988 return NULL;
989 }
990
991 /* parse options */
992 clear_job_options(&opt);
993 opt.jo_mode = MODE_JSON;
994 opt.jo_timeout = 2000;
995 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200996 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200997 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100998 if (opt.jo_timeout < 0)
999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001000 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001001 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001002 }
1003
1004 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1005 if (channel != NULL)
1006 {
1007 opt.jo_set = JO_ALL;
1008 channel_set_options(channel, &opt);
1009 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001010theend:
1011 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001012 return channel;
1013}
1014
Bram Moolenaarde279892016-03-11 22:19:44 +01001015 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001016ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001017{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001018 sock_T *fd = &channel->ch_part[part].ch_fd;
1019
Bram Moolenaarde279892016-03-11 22:19:44 +01001020 if (*fd != INVALID_FD)
1021 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001022 if (part == PART_SOCK)
1023 sock_close(*fd);
1024 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001025 {
1026 /* When using a pty the same FD is set on multiple parts, only
1027 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001028 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1029 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1030 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001031 {
1032#ifdef WIN32
1033 if (channel->ch_named_pipe)
1034 DisconnectNamedPipe((HANDLE)fd);
1035#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001036 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001037 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001038 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001039 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001040
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001041 /* channel is closed, may want to end the job if it was the last */
1042 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001043 }
1044}
1045
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001046 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001047channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001048{
Bram Moolenaarde279892016-03-11 22:19:44 +01001049 if (in != INVALID_FD)
1050 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001051 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001052 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001053# if defined(UNIX)
1054 /* Do not end the job when all output channels are closed, wait until
1055 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001056 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001057 channel->ch_to_be_closed |= (1U << PART_IN);
1058# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001059 }
1060 if (out != INVALID_FD)
1061 {
1062# if defined(FEAT_GUI)
1063 channel_gui_unregister_one(channel, PART_OUT);
1064# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001065 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001066 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001067 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001068# if defined(FEAT_GUI)
1069 channel_gui_register_one(channel, PART_OUT);
1070# endif
1071 }
1072 if (err != INVALID_FD)
1073 {
1074# if defined(FEAT_GUI)
1075 channel_gui_unregister_one(channel, PART_ERR);
1076# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001077 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001078 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001079 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001080# if defined(FEAT_GUI)
1081 channel_gui_register_one(channel, PART_ERR);
1082# endif
1083 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001084}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001085
Bram Moolenaard6051b52016-02-28 15:49:03 +01001086/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001087 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001088 * This does not keep a refcount, when the job is freed ch_job is cleared.
1089 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001090 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001091channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001092{
Bram Moolenaar77073442016-02-13 23:23:53 +01001093 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001094
1095 channel_set_options(channel, options);
1096
1097 if (job->jv_in_buf != NULL)
1098 {
1099 chanpart_T *in_part = &channel->ch_part[PART_IN];
1100
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001101 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001102 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001103 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001104 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001105 {
1106 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1107 {
1108 /* Special mode: send last-but-one line when appending a line
1109 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001110 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001111 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001112 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001113 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001114 }
1115 else
1116 in_part->ch_buf_top = options->jo_in_top;
1117 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001118 else
1119 in_part->ch_buf_top = 1;
1120 if (options->jo_set & JO_IN_BOT)
1121 in_part->ch_buf_bot = options->jo_in_bot;
1122 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001123 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001124 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001125}
1126
1127/*
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001128 * Prepare buffer "buf" for writing channel output to.
1129 */
1130 static void
1131prepare_buffer(buf_T *buf)
1132{
1133 buf_T *save_curbuf = curbuf;
1134
1135 buf_copy_options(buf, BCO_ENTER);
1136 curbuf = buf;
1137#ifdef FEAT_QUICKFIX
1138 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1139 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1140#endif
1141 if (curbuf->b_ml.ml_mfp == NULL)
1142 ml_open(curbuf);
1143 curbuf = save_curbuf;
1144}
1145
1146/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001147 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001148 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001149 */
1150 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001151find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001152{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001153 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001154 buf_T *save_curbuf = curbuf;
1155
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001156 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001157 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001158 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001159 if (buf == NULL)
1160 buf = buflist_findname_exp(name);
1161 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001162 if (buf == NULL)
1163 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001164 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001165 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001166 if (buf == NULL)
1167 return NULL;
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001168 prepare_buffer(buf);
1169
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001170 curbuf = buf;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001171 if (msg)
1172 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001173 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001174 changed_bytes(1, 0);
1175 curbuf = save_curbuf;
1176 }
1177
1178 return buf;
1179}
1180
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001181 static void
1182set_callback(
1183 char_u **cbp,
1184 partial_T **pp,
1185 char_u *callback,
1186 partial_T *partial)
1187{
1188 free_callback(*cbp, *pp);
1189 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001190 {
1191 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001192 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001193 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001194 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001195 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001196 func_ref(*cbp);
1197 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001198 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001199 else
1200 *cbp = NULL;
1201 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001202 if (partial != NULL)
1203 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001204}
1205
Bram Moolenaar187db502016-02-27 14:44:26 +01001206/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001207 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001208 */
1209 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001210channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001211{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001212 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001213
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001214 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001215 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001216 channel->ch_part[part].ch_mode = opt->jo_mode;
1217 if (opt->jo_set & JO_IN_MODE)
1218 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1219 if (opt->jo_set & JO_OUT_MODE)
1220 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1221 if (opt->jo_set & JO_ERR_MODE)
1222 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001223 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001224
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001225 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001226 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001227 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1228 if (opt->jo_set & JO_OUT_TIMEOUT)
1229 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1230 if (opt->jo_set & JO_ERR_TIMEOUT)
1231 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001232 if (opt->jo_set & JO_BLOCK_WRITE)
1233 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001234
1235 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001236 set_callback(&channel->ch_callback, &channel->ch_partial,
1237 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001238 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001239 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1240 &channel->ch_part[PART_OUT].ch_partial,
1241 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001242 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001243 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1244 &channel->ch_part[PART_ERR].ch_partial,
1245 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001246 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001247 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1248 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001249 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001250
1251 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1252 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001253 buf_T *buf;
1254
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001255 /* writing output to a buffer. Default mode is NL. */
1256 if (!(opt->jo_set & JO_OUT_MODE))
1257 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001258 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001259 {
1260 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1261 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001262 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001263 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001264 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001265 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001266 int msg = TRUE;
1267
1268 if (opt->jo_set2 & JO2_OUT_MSG)
1269 msg = opt->jo_message[PART_OUT];
1270 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001271 }
1272 if (buf != NULL)
1273 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001274 if (opt->jo_set & JO_OUT_MODIFIABLE)
1275 channel->ch_part[PART_OUT].ch_nomodifiable =
1276 !opt->jo_modifiable[PART_OUT];
1277
1278 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1279 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001280 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001281 }
1282 else
1283 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001284 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001285 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001286 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001287 // if the buffer was deleted or unloaded resurrect it
1288 if (buf->b_ml.ml_mfp == NULL)
1289 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001290 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001291 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001292 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001293
1294 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1295 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1296 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1297 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001298 buf_T *buf;
1299
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001300 /* writing err to a buffer. Default mode is NL. */
1301 if (!(opt->jo_set & JO_ERR_MODE))
1302 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1303 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001304 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001305 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001306 {
1307 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1308 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001309 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001310 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001311 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001312 {
1313 int msg = TRUE;
1314
1315 if (opt->jo_set2 & JO2_ERR_MSG)
1316 msg = opt->jo_message[PART_ERR];
1317 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1318 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001319 if (buf != NULL)
1320 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001321 if (opt->jo_set & JO_ERR_MODIFIABLE)
1322 channel->ch_part[PART_ERR].ch_nomodifiable =
1323 !opt->jo_modifiable[PART_ERR];
1324 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001326 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001327 }
1328 else
1329 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001330 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001331 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001332 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001333 // if the buffer was deleted or unloaded resurrect it
1334 if (buf->b_ml.ml_mfp == NULL)
1335 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001336 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001337 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001338 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001339
1340 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1341 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1342 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001343}
1344
1345/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001346 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001347 */
1348 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001349channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001350 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001351 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001352 char_u *callback,
1353 partial_T *partial,
1354 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001355{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001356 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001357 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1358
1359 if (item != NULL)
1360 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001361 item->cq_partial = partial;
1362 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001363 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001364 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001365 item->cq_callback = callback;
1366 }
1367 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001368 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001369 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001370 func_ref(item->cq_callback);
1371 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001372 item->cq_seq_nr = id;
1373 item->cq_prev = head->cq_prev;
1374 head->cq_prev = item;
1375 item->cq_next = NULL;
1376 if (item->cq_prev == NULL)
1377 head->cq_next = item;
1378 else
1379 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001380 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001381}
1382
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001383 static void
1384write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1385{
1386 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001387 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001388 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001389 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001390
Bram Moolenaar655da312016-05-28 22:22:34 +02001391 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001392 if ((p = alloc(len + 2)) == NULL)
1393 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001394 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001395
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001396 if (channel->ch_write_text_mode)
1397 p[len] = CAR;
1398 else
1399 {
1400 for (i = 0; i < len; ++i)
1401 if (p[i] == NL)
1402 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001403
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001404 p[len] = NL;
1405 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001406 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001407 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001408 vim_free(p);
1409}
1410
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001411/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001412 * Return TRUE if "channel" can be written to.
1413 * Returns FALSE if the input is closed or the write would block.
1414 */
1415 static int
1416can_write_buf_line(channel_T *channel)
1417{
1418 chanpart_T *in_part = &channel->ch_part[PART_IN];
1419
1420 if (in_part->ch_fd == INVALID_FD)
1421 return FALSE; /* pipe was closed */
1422
1423 /* for testing: block every other attempt to write */
1424 if (in_part->ch_block_write == 1)
1425 in_part->ch_block_write = -1;
1426 else if (in_part->ch_block_write == -1)
1427 in_part->ch_block_write = 1;
1428
1429 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1430#ifndef WIN32
1431 {
1432# if defined(HAVE_SELECT)
1433 struct timeval tval;
1434 fd_set wfds;
1435 int ret;
1436
1437 FD_ZERO(&wfds);
1438 FD_SET((int)in_part->ch_fd, &wfds);
1439 tval.tv_sec = 0;
1440 tval.tv_usec = 0;
1441 for (;;)
1442 {
1443 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1444# ifdef EINTR
1445 SOCK_ERRNO;
1446 if (ret == -1 && errno == EINTR)
1447 continue;
1448# endif
1449 if (ret <= 0 || in_part->ch_block_write == 1)
1450 {
1451 if (ret > 0)
1452 ch_log(channel, "FAKED Input not ready for writing");
1453 else
1454 ch_log(channel, "Input not ready for writing");
1455 return FALSE;
1456 }
1457 break;
1458 }
1459# else
1460 struct pollfd fds;
1461
1462 fds.fd = in_part->ch_fd;
1463 fds.events = POLLOUT;
1464 if (poll(&fds, 1, 0) <= 0)
1465 {
1466 ch_log(channel, "Input not ready for writing");
1467 return FALSE;
1468 }
1469 if (in_part->ch_block_write == 1)
1470 {
1471 ch_log(channel, "FAKED Input not ready for writing");
1472 return FALSE;
1473 }
1474# endif
1475 }
1476#endif
1477 return TRUE;
1478}
1479
1480/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001481 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001482 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001483 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001484channel_write_in(channel_T *channel)
1485{
1486 chanpart_T *in_part = &channel->ch_part[PART_IN];
1487 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001488 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001489 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001490
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001491 if (buf == NULL || in_part->ch_buf_append)
1492 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001493 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001494 {
1495 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001496 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001497 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001498 return;
1499 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001500
1501 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1502 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1503 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 if (!can_write_buf_line(channel))
1505 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506 write_buf_line(buf, lnum, channel);
1507 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001508 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001509
1510 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001511 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001512 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001513 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001514
Bram Moolenaar014069a2016-03-03 22:51:40 +01001515 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001516 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001517 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001518#if defined(FEAT_TERMINAL)
1519 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001520 if (channel->ch_job != NULL)
1521 term_send_eof(channel);
1522#endif
1523
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001524 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001525 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001526 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001527
1528 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001529 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001530 }
1531 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001532 ch_log(channel, "Still %ld more lines to write",
1533 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001534}
1535
1536/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001537 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001538 */
1539 void
1540channel_buffer_free(buf_T *buf)
1541{
1542 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001543 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001544
1545 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001546 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001547 {
1548 chanpart_T *ch_part = &channel->ch_part[part];
1549
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001550 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001551 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001552 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001553 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001554 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001555 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001556 }
1557}
1558
1559/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001560 * Write any lines waiting to be written to "channel".
1561 */
1562 static void
1563channel_write_input(channel_T *channel)
1564{
1565 chanpart_T *in_part = &channel->ch_part[PART_IN];
1566
1567 if (in_part->ch_writeque.wq_next != NULL)
1568 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1569 else if (in_part->ch_bufref.br_buf != NULL)
1570 {
1571 if (in_part->ch_buf_append)
1572 channel_write_new_lines(in_part->ch_bufref.br_buf);
1573 else
1574 channel_write_in(channel);
1575 }
1576}
1577
1578/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001579 * Write any lines waiting to be written to a channel.
1580 */
1581 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001582channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001583{
1584 channel_T *channel;
1585
1586 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001587 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001588}
1589
1590/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001591 * Write appended lines above the last one in "buf" to the channel.
1592 */
1593 void
1594channel_write_new_lines(buf_T *buf)
1595{
1596 channel_T *channel;
1597 int found_one = FALSE;
1598
1599 /* There could be more than one channel for the buffer, loop over all of
1600 * them. */
1601 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1602 {
1603 chanpart_T *in_part = &channel->ch_part[PART_IN];
1604 linenr_T lnum;
1605 int written = 0;
1606
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001607 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001608 {
1609 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001610 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001611 found_one = TRUE;
1612 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1613 ++lnum)
1614 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001615 if (!can_write_buf_line(channel))
1616 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001617 write_buf_line(buf, lnum, channel);
1618 ++written;
1619 }
1620
1621 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001622 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001623 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001624 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001625 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001626 ch_log(channel, "Still %ld more lines to write",
1627 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001628
1629 in_part->ch_buf_bot = lnum;
1630 }
1631 }
1632 if (!found_one)
1633 buf->b_write_to_channel = FALSE;
1634}
1635
1636/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001637 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001638 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001639 */
1640 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001641invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1642 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001643{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001644 typval_T rettv;
1645 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001646
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001647 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001648 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001649
Bram Moolenaar77073442016-02-13 23:23:53 +01001650 argv[0].v_type = VAR_CHANNEL;
1651 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001652
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001653 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1654 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001655 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001656 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001657}
1658
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001659/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001660 * Return the first node from "channel"/"part" without removing it.
1661 * Returns NULL if there is nothing.
1662 */
1663 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001664channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001665{
1666 readq_T *head = &channel->ch_part[part].ch_head;
1667
1668 return head->rq_next;
1669}
1670
1671/*
1672 * Return a pointer to the first NL in "node".
1673 * Skips over NUL characters.
1674 * Returns NULL if there is no NL.
1675 */
1676 char_u *
1677channel_first_nl(readq_T *node)
1678{
1679 char_u *buffer = node->rq_buffer;
1680 long_u i;
1681
1682 for (i = 0; i < node->rq_buflen; ++i)
1683 if (buffer[i] == NL)
1684 return buffer + i;
1685 return NULL;
1686}
1687
1688/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001689 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001690 * The caller must free it.
1691 * Returns NULL if there is nothing.
1692 */
1693 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001696 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001697 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001698 char_u *p;
1699
Bram Moolenaar77073442016-02-13 23:23:53 +01001700 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 if (outlen != NULL)
1703 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001704 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001705 p = node->rq_buffer;
1706 head->rq_next = node->rq_next;
1707 if (node->rq_next == NULL)
1708 head->rq_prev = NULL;
1709 else
1710 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001711 vim_free(node);
1712 return p;
1713}
1714
1715/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001716 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001717 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001718 */
1719 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001722 readq_T *head = &channel->ch_part[part].ch_head;
1723 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001724 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001725 char_u *res;
1726 char_u *p;
1727
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001728 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001729 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001730 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001731 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001732 if (res == NULL)
1733 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001735 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001736 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001737 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001738 p += node->rq_buflen;
1739 }
1740 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001741
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001742 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001743 do
1744 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001745 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001746 vim_free(p);
1747 } while (p != NULL);
1748
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001749 if (outlen != NULL)
1750 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001751 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 *outlen += len;
1753 return res;
1754 }
1755
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001756 // Turn all NUL into NL, so that the result can be used as a string.
1757 p = res;
1758 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001759 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001760 if (*p == NUL)
1761 *p = NL;
1762#ifdef WIN32
1763 else if (*p == 0x1b)
1764 {
1765 // crush the escape sequence OSC 0/1/2: ESC ]0;
1766 if (p + 3 < res + len
1767 && p[1] == ']'
1768 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1769 && p[3] == ';')
1770 {
1771 // '\a' becomes a NL
1772 while (p < res + (len - 1) && *p != '\a')
1773 ++p;
1774 // BEL is zero width characters, suppress display mistake
1775 // ConPTY (after 10.0.18317) requires advance checking
1776 if (p[-1] == NUL)
1777 p[-1] = 0x07;
1778 }
1779 }
1780#endif
1781 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001782 }
1783
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001784 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001785}
1786
1787/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001788 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001789 * Caller must check these bytes are available.
1790 */
1791 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001792channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001793{
1794 readq_T *head = &channel->ch_part[part].ch_head;
1795 readq_T *node = head->rq_next;
1796 char_u *buf = node->rq_buffer;
1797
1798 mch_memmove(buf, buf + len, node->rq_buflen - len);
1799 node->rq_buflen -= len;
1800}
1801
1802/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001803 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001804 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001805 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001806 */
1807 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001808channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001809{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001810 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001811 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001812 readq_T *last_node;
1813 readq_T *n;
1814 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001815 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001816 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001817
Bram Moolenaar77073442016-02-13 23:23:53 +01001818 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001819 return FAIL;
1820
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001821 last_node = node->rq_next;
1822 len = node->rq_buflen + last_node->rq_buflen + 1;
1823 if (want_nl)
1824 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001825 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001826 {
1827 last_node = last_node->rq_next;
1828 len += last_node->rq_buflen;
1829 }
1830
1831 p = newbuf = alloc(len);
1832 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001833 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001834 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001835 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001836 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001837 node->rq_buffer = newbuf;
1838 for (n = node; n != last_node; )
1839 {
1840 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001841 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001842 p += n->rq_buflen;
1843 vim_free(n->rq_buffer);
1844 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001845 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001846
1847 /* dispose of the collapsed nodes and their buffers */
1848 for (n = node->rq_next; n != last_node; )
1849 {
1850 n = n->rq_next;
1851 vim_free(n->rq_prev);
1852 }
1853 node->rq_next = last_node->rq_next;
1854 if (last_node->rq_next == NULL)
1855 head->rq_prev = node;
1856 else
1857 last_node->rq_next->rq_prev = node;
1858 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001859 return OK;
1860}
1861
1862/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001864 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001865 * Returns OK or FAIL.
1866 */
1867 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001868channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001869 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870{
1871 readq_T *node;
1872 readq_T *head = &channel->ch_part[part].ch_head;
1873 char_u *p;
1874 int i;
1875
1876 node = (readq_T *)alloc(sizeof(readq_T));
1877 if (node == NULL)
1878 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001879 /* A NUL is added at the end, because netbeans code expects that.
1880 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001881 node->rq_buffer = alloc(len + 1);
1882 if (node->rq_buffer == NULL)
1883 {
1884 vim_free(node);
1885 return FAIL; /* out of memory */
1886 }
1887
1888 if (channel->ch_part[part].ch_mode == MODE_NL)
1889 {
1890 /* Drop any CR before a NL. */
1891 p = node->rq_buffer;
1892 for (i = 0; i < len; ++i)
1893 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1894 *p++ = buf[i];
1895 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001896 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001897 }
1898 else
1899 {
1900 mch_memmove(node->rq_buffer, buf, len);
1901 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001902 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001903 }
1904
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001905 if (prepend)
1906 {
1907 /* preend node to the head of the queue */
1908 node->rq_next = head->rq_next;
1909 node->rq_prev = NULL;
1910 if (head->rq_next == NULL)
1911 head->rq_prev = node;
1912 else
1913 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001914 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001915 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001916 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001917 {
1918 /* append node to the tail of the queue */
1919 node->rq_next = NULL;
1920 node->rq_prev = head->rq_prev;
1921 if (head->rq_prev == NULL)
1922 head->rq_next = node;
1923 else
1924 head->rq_prev->rq_next = node;
1925 head->rq_prev = node;
1926 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001927
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001928 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001929 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001930 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001931 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001932 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001933 fprintf(log_fd, "'\n");
1934 }
1935 return OK;
1936}
1937
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001938/*
1939 * Try to fill the buffer of "reader".
1940 * Returns FALSE when nothing was added.
1941 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001942 static int
1943channel_fill(js_read_T *reader)
1944{
1945 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001946 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001947 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001948 int keeplen;
1949 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001950 char_u *p;
1951
1952 if (next == NULL)
1953 return FALSE;
1954
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001955 keeplen = reader->js_end - reader->js_buf;
1956 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001957 {
1958 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001959 addlen = (int)STRLEN(next);
1960 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001961 if (p == NULL)
1962 {
1963 vim_free(next);
1964 return FALSE;
1965 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001966 mch_memmove(p, reader->js_buf, keeplen);
1967 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001968 vim_free(next);
1969 next = p;
1970 }
1971
1972 vim_free(reader->js_buf);
1973 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001974 return TRUE;
1975}
1976
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001977/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001979 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001980 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001981 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001982 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001983channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001984{
1985 js_read_T reader;
1986 typval_T listtv;
1987 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001988 chanpart_T *chanpart = &channel->ch_part[part];
1989 jsonq_T *head = &chanpart->ch_json_head;
1990 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001991 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001992
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001993 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001994 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001995
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001996 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001997 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001998 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001999 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002000 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002001
2002 /* When a message is incomplete we wait for a short while for more to
2003 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002004 * or list will make us hang.
2005 * Do not generate error messages, they will be written in a channel log. */
2006 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002007 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002008 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002009 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002010 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002011 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002012 /* Only accept the response when it is a list with at least two
2013 * items. */
2014 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002015 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002016 if (listtv.v_type != VAR_LIST)
2017 ch_error(channel, "Did not receive a list, discarding");
2018 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002019 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002020 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002021 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002022 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002023 else
2024 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002025 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2026 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002027 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002028 else
2029 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002030 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002031 item->jq_value = alloc_tv();
2032 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002033 {
2034 vim_free(item);
2035 clear_tv(&listtv);
2036 }
2037 else
2038 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002039 *item->jq_value = listtv;
2040 item->jq_prev = head->jq_prev;
2041 head->jq_prev = item;
2042 item->jq_next = NULL;
2043 if (item->jq_prev == NULL)
2044 head->jq_next = item;
2045 else
2046 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002047 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002048 }
2049 }
2050 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002051
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002052 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002053 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002054 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002055 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002056 size_t buflen = STRLEN(reader.js_buf);
2057
2058 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002059 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002060 /* First time encountering incomplete message or after receiving
2061 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002062 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002063 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002064 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002065 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002066 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002067#ifdef WIN32
2068 chanpart->ch_deadline = GetTickCount() + 100L;
2069#else
2070 gettimeofday(&chanpart->ch_deadline, NULL);
2071 chanpart->ch_deadline.tv_usec += 100 * 1000;
2072 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2073 {
2074 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2075 ++chanpart->ch_deadline.tv_sec;
2076 }
2077#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002078 }
2079 else
2080 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002081 int timeout;
2082#ifdef WIN32
2083 timeout = GetTickCount() > chanpart->ch_deadline;
2084#else
2085 {
2086 struct timeval now_tv;
2087
2088 gettimeofday(&now_tv, NULL);
2089 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2090 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2091 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2092 }
2093#endif
2094 if (timeout)
2095 {
2096 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002097 chanpart->ch_wait_len = 0;
2098 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002099 }
2100 else
2101 {
2102 reader.js_used = 0;
2103 ch_log(channel, "still waiting on incomplete message");
2104 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002105 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002106 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002107
2108 if (status == FAIL)
2109 {
2110 ch_error(channel, "Decoding failed - discarding input");
2111 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002112 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002113 }
2114 else if (reader.js_buf[reader.js_used] != NUL)
2115 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002116 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002117 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002118 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2119 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002120 ret = status == MAYBE ? FALSE: TRUE;
2121 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002122 else
2123 ret = FALSE;
2124
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002125 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002126 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002127}
2128
2129/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002130 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002131 */
2132 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002133remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002134{
Bram Moolenaar77073442016-02-13 23:23:53 +01002135 if (node->cq_prev == NULL)
2136 head->cq_next = node->cq_next;
2137 else
2138 node->cq_prev->cq_next = node->cq_next;
2139 if (node->cq_next == NULL)
2140 head->cq_prev = node->cq_prev;
2141 else
2142 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002143}
2144
2145/*
2146 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002147 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002148 */
2149 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002150remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002151{
Bram Moolenaar77073442016-02-13 23:23:53 +01002152 if (node->jq_prev == NULL)
2153 head->jq_next = node->jq_next;
2154 else
2155 node->jq_prev->jq_next = node->jq_next;
2156 if (node->jq_next == NULL)
2157 head->jq_prev = node->jq_prev;
2158 else
2159 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002160 vim_free(node);
2161}
2162
2163/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002164 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002165 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002166 * When "id" is zero or negative jut get the first message. But not the one
2167 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002168 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002169 * Return OK when found and return the value in "rettv".
2170 * Return FAIL otherwise.
2171 */
2172 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002173channel_get_json(
2174 channel_T *channel,
2175 ch_part_T part,
2176 int id,
2177 int without_callback,
2178 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002179{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002180 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002181 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002182
Bram Moolenaar77073442016-02-13 23:23:53 +01002183 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002184 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002185 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002186 typval_T *tv = &l->lv_first->li_tv;
2187
Bram Moolenaar958dc692016-12-01 15:34:12 +01002188 if ((without_callback || !item->jq_no_callback)
2189 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002190 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002191 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002192 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002193 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002194 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002195 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002196 ch_log(channel, "Getting JSON message %ld",
2197 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002198 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002199 return OK;
2200 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002201 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002202 }
2203 return FAIL;
2204}
2205
Bram Moolenaar958dc692016-12-01 15:34:12 +01002206/*
2207 * Put back "rettv" into the JSON queue, there was no callback for it.
2208 * Takes over the values in "rettv".
2209 */
2210 static void
2211channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2212{
2213 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2214 jsonq_T *item = head->jq_next;
2215 jsonq_T *newitem;
2216
2217 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2218 /* last item was pushed back, append to the end */
2219 item = NULL;
2220 else while (item != NULL && item->jq_no_callback)
2221 /* append after the last item that was pushed back */
2222 item = item->jq_next;
2223
2224 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2225 if (newitem == NULL)
2226 clear_tv(rettv);
2227 else
2228 {
2229 newitem->jq_value = alloc_tv();
2230 if (newitem->jq_value == NULL)
2231 {
2232 vim_free(newitem);
2233 clear_tv(rettv);
2234 }
2235 else
2236 {
2237 newitem->jq_no_callback = FALSE;
2238 *newitem->jq_value = *rettv;
2239 if (item == NULL)
2240 {
2241 /* append to the end */
2242 newitem->jq_prev = head->jq_prev;
2243 head->jq_prev = newitem;
2244 newitem->jq_next = NULL;
2245 if (newitem->jq_prev == NULL)
2246 head->jq_next = newitem;
2247 else
2248 newitem->jq_prev->jq_next = newitem;
2249 }
2250 else
2251 {
2252 /* append after "item" */
2253 newitem->jq_prev = item;
2254 newitem->jq_next = item->jq_next;
2255 item->jq_next = newitem;
2256 if (newitem->jq_next == NULL)
2257 head->jq_prev = newitem;
2258 else
2259 newitem->jq_next->jq_prev = newitem;
2260 }
2261 }
2262 }
2263}
2264
Bram Moolenaarece61b02016-02-20 21:39:05 +01002265#define CH_JSON_MAX_ARGS 4
2266
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002267/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002268 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002269 * "argv[0]" is the command string.
2270 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002271 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002272 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002273channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002274{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002275 char_u *cmd = argv[0].vval.v_string;
2276 char_u *arg;
2277 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002278
Bram Moolenaarece61b02016-02-20 21:39:05 +01002279 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002280 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002281 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002282 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002283 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002284 return;
2285 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002286 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002287 if (arg == NULL)
2288 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002289
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002290 if (STRCMP(cmd, "ex") == 0)
2291 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002292 int save_called_emsg = called_emsg;
2293
2294 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002295 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002296 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002297 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002298 --emsg_silent;
2299 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002300 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002301 (char *)get_vim_var_str(VV_ERRMSG));
2302 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002303 }
2304 else if (STRCMP(cmd, "normal") == 0)
2305 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002306 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002307
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002308 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002309 ea.arg = arg;
2310 ea.addr_count = 0;
2311 ea.forceit = TRUE; /* no mapping */
2312 ex_normal(&ea);
2313 }
2314 else if (STRCMP(cmd, "redraw") == 0)
2315 {
2316 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002317
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002318 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002319 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002320 ex_redraw(&ea);
2321 showruler(FALSE);
2322 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002323 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002324 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002326 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002327 int is_call = cmd[0] == 'c';
2328 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002329
Bram Moolenaarece61b02016-02-20 21:39:05 +01002330 if (argv[id_idx].v_type != VAR_UNKNOWN
2331 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002332 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002333 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002334 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002335 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002336 }
2337 else if (is_call && argv[2].v_type != VAR_LIST)
2338 {
2339 ch_error(channel, "third argument for call must be a list");
2340 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002341 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002342 }
2343 else
2344 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002345 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002346 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002347 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002348 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002349
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002350 /* Don't pollute the display with errors. */
2351 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002352 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002353 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002354 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002355 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002356 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002357 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002358 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002359 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002360 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2361 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002362 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002363
2364 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002365 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002366 int id = argv[id_idx].vval.v_number;
2367
Bram Moolenaar55fab432016-02-07 16:53:13 +01002368 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002369 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002370 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002371 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002372 /* If evaluation failed or the result can't be encoded
2373 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002374 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002375 err_tv.v_type = VAR_STRING;
2376 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002377 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002378 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002379 if (json != NULL)
2380 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002381 channel_send(channel,
2382 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002383 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002384 vim_free(json);
2385 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002386 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002387 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002388 if (tv == &res_tv)
2389 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002390 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002391 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002392 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002393 }
2394 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002395 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002396 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002397 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002398 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002399}
2400
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002401/*
2402 * Invoke the callback at "cbhead".
2403 * Does not redraw but sets channel_need_redraw.
2404 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002405 static void
2406invoke_one_time_callback(
2407 channel_T *channel,
2408 cbq_T *cbhead,
2409 cbq_T *item,
2410 typval_T *argv)
2411{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002412 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002413 (char *)item->cq_callback);
2414 /* Remove the item from the list first, if the callback
2415 * invokes ch_close() the list will be cleared. */
2416 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002417 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002418 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002419 vim_free(item);
2420}
2421
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002422 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002423append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002424{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002425 bufref_T save_curbuf = {NULL, 0, 0};
2426 win_T *save_curwin = NULL;
2427 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002428 linenr_T lnum = buffer->b_ml.ml_line_count;
2429 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002430 chanpart_T *ch_part = &channel->ch_part[part];
2431 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002432 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002433
2434 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2435 {
2436 if (!ch_part->ch_nomod_error)
2437 {
2438 ch_error(channel, "Buffer is not modifiable, cannot append");
2439 ch_part->ch_nomod_error = TRUE;
2440 }
2441 return;
2442 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002443
2444 /* If the buffer is also used as input insert above the last
2445 * line. Don't write these lines. */
2446 if (save_write_to)
2447 {
2448 --lnum;
2449 buffer->b_write_to_channel = FALSE;
2450 }
2451
2452 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002453 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002454
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002455 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002456
2457 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2458 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2459
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002460 u_sync(TRUE);
2461 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002462 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002463
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002464 if (empty)
2465 {
2466 /* The buffer is empty, replace the first (dummy) line. */
2467 ml_replace(lnum, msg, TRUE);
2468 lnum = 0;
2469 }
2470 else
2471 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002472 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002473
2474 /* Restore curbuf/curwin/curtab */
2475 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2476
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002477 if (ch_part->ch_nomodifiable)
2478 buffer->b_p_ma = FALSE;
2479 else
2480 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002481
2482 if (buffer->b_nwindows > 0)
2483 {
2484 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002485
2486 FOR_ALL_WINDOWS(wp)
2487 {
2488 if (wp->w_buffer == buffer
2489 && (save_write_to
2490 ? wp->w_cursor.lnum == lnum + 1
2491 : (wp->w_cursor.lnum == lnum
2492 && wp->w_cursor.col == 0)))
2493 {
2494 ++wp->w_cursor.lnum;
2495 save_curwin = curwin;
2496 curwin = wp;
2497 curbuf = curwin->w_buffer;
2498 scroll_cursor_bot(0, FALSE);
2499 curwin = save_curwin;
2500 curbuf = curwin->w_buffer;
2501 }
2502 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002503 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002504 channel_need_redraw = TRUE;
2505 }
2506
2507 if (save_write_to)
2508 {
2509 channel_T *ch;
2510
2511 /* Find channels reading from this buffer and adjust their
2512 * next-to-read line number. */
2513 buffer->b_write_to_channel = TRUE;
2514 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2515 {
2516 chanpart_T *in_part = &ch->ch_part[PART_IN];
2517
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002518 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002519 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2520 }
2521 }
2522}
2523
Bram Moolenaar437905c2016-04-26 19:01:05 +02002524 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002525drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002526{
2527 char_u *msg;
2528
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002529 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002530 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002531 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002532 vim_free(msg);
2533 }
2534}
2535
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002536/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002537 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002538 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002539 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002540 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002541 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002542may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002543{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002544 char_u *msg = NULL;
2545 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002546 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002547 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002548 chanpart_T *ch_part = &channel->ch_part[part];
2549 ch_mode_T ch_mode = ch_part->ch_mode;
2550 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002551 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002552 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002553 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002554 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002555 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002556
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002557 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002558 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002559 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002560
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002561 /* Use a message-specific callback, part callback or channel callback */
2562 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2563 if (cbitem->cq_seq_nr == 0)
2564 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002565 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002566 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002567 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002568 partial = cbitem->cq_partial;
2569 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002570 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002571 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002572 callback = ch_part->ch_callback;
2573 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002574 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002575 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002576 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002577 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002578 partial = channel->ch_partial;
2579 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002580
Bram Moolenaarec68a992016-10-03 21:37:41 +02002581 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002582 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2583 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002584 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002585 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002586 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002587 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002588 buffer = NULL;
2589 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002590
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002591 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002592 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002593 listitem_T *item;
2594 int argc = 0;
2595
Bram Moolenaard7ece102016-02-02 23:23:02 +01002596 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002597 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002598 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002599 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002600 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002601 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002602 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002603 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002604
Bram Moolenaarece61b02016-02-20 21:39:05 +01002605 for (item = listtv->vval.v_list->lv_first;
2606 item != NULL && argc < CH_JSON_MAX_ARGS;
2607 item = item->li_next)
2608 argv[argc++] = item->li_tv;
2609 while (argc < CH_JSON_MAX_ARGS)
2610 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002611
Bram Moolenaarece61b02016-02-20 21:39:05 +01002612 if (argv[0].v_type == VAR_STRING)
2613 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002614 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002615 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002616 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002617 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002618 }
2619
Bram Moolenaarece61b02016-02-20 21:39:05 +01002620 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002621 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002622 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002623 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002624 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002625 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002626 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002627 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002628 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002629 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002630 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002631 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002632 return FALSE;
2633 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002634 else
2635 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002636 /* If there is no callback or buffer drop the message. */
2637 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002638 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002639 /* If there is a close callback it may use ch_read() to get the
2640 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002641 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002642 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002643 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002644 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002645
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002646 if (ch_mode == MODE_NL)
2647 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002648 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002649 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002650 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002651
2652 /* See if we have a message ending in NL in the first buffer. If
2653 * not try to concatenate the first and the second buffer. */
2654 while (TRUE)
2655 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002656 node = channel_peek(channel, part);
2657 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002658 if (nl != NULL)
2659 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002660 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002661 {
2662 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2663 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002664 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002665 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002666 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002667 buf = node->rq_buffer;
2668
Bram Moolenaarec68a992016-10-03 21:37:41 +02002669 if (nl == NULL)
2670 {
2671 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002672 char_u *new_buf;
2673
2674 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2675 if (new_buf == NULL)
2676 /* This might fail over and over again, should the message
2677 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002678 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002679 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002680 node->rq_buffer = buf;
2681 nl = buf + node->rq_buflen++;
2682 *nl = NUL;
2683 }
2684
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002685 /* Convert NUL to NL, the internal representation. */
2686 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2687 if (*p == NUL)
2688 *p = NL;
2689
2690 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002691 {
2692 /* get the whole buffer, drop the NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002693 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002694 *nl = NUL;
2695 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002696 else
2697 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002698 /* Copy the message into allocated memory (excluding the NL)
2699 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002700 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002701 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002702 }
2703 }
2704 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002705 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002706 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002707 * get everything we have.
2708 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002709 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002710 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002711
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002712 if (msg == NULL)
2713 return FALSE; /* out of memory (and avoids Coverity warning) */
2714
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002715 argv[1].v_type = VAR_STRING;
2716 argv[1].vval.v_string = msg;
2717 }
2718
Bram Moolenaara07fec92016-02-05 21:04:08 +01002719 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002720 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002721 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002722
Bram Moolenaar958dc692016-12-01 15:34:12 +01002723 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002724 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002725 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002726 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002727 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002728 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002729 break;
2730 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002731 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002732 {
2733 if (channel->ch_drop_never)
2734 {
2735 /* message must be read with ch_read() */
2736 channel_push_json(channel, part, listtv);
2737 listtv = NULL;
2738 }
2739 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002740 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002741 seq_nr);
2742 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002743 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002744 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002745 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002746 if (buffer != NULL)
2747 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002748 if (msg == NULL)
2749 /* JSON or JS mode: re-encode the message. */
2750 msg = json_encode(listtv, ch_mode);
2751 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002752 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002753#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002754 if (buffer->b_term != NULL)
2755 write_to_term(buffer, msg, channel);
2756 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002757#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002758 append_to_buffer(buffer, msg, channel, part);
2759 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002760 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002761
Bram Moolenaar187db502016-02-27 14:44:26 +01002762 if (callback != NULL)
2763 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002764 if (cbitem != NULL)
2765 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2766 else
2767 {
2768 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002769 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002770 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002771 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002772 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002773 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002774 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002775 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002776 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002777
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002778 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002779 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002780 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002781
2782 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002783}
2784
Bram Moolenaar113e1072019-01-20 15:30:40 +01002785#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002786/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002787 * Return TRUE when channel "channel" is open for writing to.
2788 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002789 */
2790 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002791channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002792{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002793 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002794 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002795}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002796#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002797
2798/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002799 * Return TRUE when channel "channel" is open for reading or writing.
2800 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002801 */
2802 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002803channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002804{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002805 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002806 || channel->CH_IN_FD != INVALID_FD
2807 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002808 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002809}
2810
2811/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002812 * Return TRUE if "channel" has JSON or other typeahead.
2813 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002814 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002815channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002816{
2817 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2818
2819 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2820 {
2821 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2822 jsonq_T *item = head->jq_next;
2823
2824 return item != NULL;
2825 }
2826 return channel_peek(channel, part) != NULL;
2827}
2828
2829/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002830 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002831 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002832 */
2833 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002834channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002835{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002836 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002837 int has_readahead = FALSE;
2838
Bram Moolenaar77073442016-02-13 23:23:53 +01002839 if (channel == NULL)
2840 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002841 if (req_part == PART_OUT)
2842 {
2843 if (channel->CH_OUT_FD != INVALID_FD)
2844 return "open";
2845 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002846 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002847 }
2848 else if (req_part == PART_ERR)
2849 {
2850 if (channel->CH_ERR_FD != INVALID_FD)
2851 return "open";
2852 if (channel_has_readahead(channel, PART_ERR))
2853 has_readahead = TRUE;
2854 }
2855 else
2856 {
2857 if (channel_is_open(channel))
2858 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002859 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002860 if (channel_has_readahead(channel, part))
2861 {
2862 has_readahead = TRUE;
2863 break;
2864 }
2865 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002866
2867 if (has_readahead)
2868 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002869 return "closed";
2870}
2871
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002872 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002873channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002874{
2875 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002876 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002877 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002878 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002879 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002880
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002881 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002882 STRCAT(namebuf, "_");
2883 tail = STRLEN(namebuf);
2884
2885 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002886 if (chanpart->ch_fd != INVALID_FD)
2887 status = "open";
2888 else if (channel_has_readahead(channel, part))
2889 status = "buffered";
2890 else
2891 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002892 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002893
2894 STRCPY(namebuf + tail, "mode");
2895 switch (chanpart->ch_mode)
2896 {
2897 case MODE_NL: s = "NL"; break;
2898 case MODE_RAW: s = "RAW"; break;
2899 case MODE_JSON: s = "JSON"; break;
2900 case MODE_JS: s = "JS"; break;
2901 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002902 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002903
2904 STRCPY(namebuf + tail, "io");
2905 if (part == PART_SOCK)
2906 s = "socket";
2907 else switch (chanpart->ch_io)
2908 {
2909 case JIO_NULL: s = "null"; break;
2910 case JIO_PIPE: s = "pipe"; break;
2911 case JIO_FILE: s = "file"; break;
2912 case JIO_BUFFER: s = "buffer"; break;
2913 case JIO_OUT: s = "out"; break;
2914 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002915 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002916
2917 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002918 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002919}
2920
2921 void
2922channel_info(channel_T *channel, dict_T *dict)
2923{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002924 dict_add_number(dict, "id", channel->ch_id);
2925 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002926
2927 if (channel->ch_hostname != NULL)
2928 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002929 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2930 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002931 channel_part_info(channel, dict, "sock", PART_SOCK);
2932 }
2933 else
2934 {
2935 channel_part_info(channel, dict, "out", PART_OUT);
2936 channel_part_info(channel, dict, "err", PART_ERR);
2937 channel_part_info(channel, dict, "in", PART_IN);
2938 }
2939}
2940
Bram Moolenaar77073442016-02-13 23:23:53 +01002941/*
2942 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002943 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002944 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002945 */
2946 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002947channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002948{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002949 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002950
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002951#ifdef FEAT_GUI
2952 channel_gui_unregister(channel);
2953#endif
2954
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002955 ch_close_part(channel, PART_SOCK);
2956 ch_close_part(channel, PART_IN);
2957 ch_close_part(channel, PART_OUT);
2958 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002959
Bram Moolenaara9f02812017-08-05 17:40:38 +02002960 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002961 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002962 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002963
Bram Moolenaara9f02812017-08-05 17:40:38 +02002964 /* Invoke callbacks and flush buffers before the close callback. */
2965 if (channel->ch_close_cb != NULL)
2966 ch_log(channel,
2967 "Invoking callbacks and flushing buffers before closing");
2968 for (part = PART_SOCK; part < PART_IN; ++part)
2969 {
2970 if (channel->ch_close_cb != NULL
2971 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2972 {
2973 /* Increment the refcount to avoid the channel being freed
2974 * halfway. */
2975 ++channel->ch_refcount;
2976 if (channel->ch_close_cb == NULL)
2977 ch_log(channel, "flushing %s buffers before closing",
2978 part_names[part]);
2979 while (may_invoke_callback(channel, part))
2980 ;
2981 --channel->ch_refcount;
2982 }
2983 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002984
Bram Moolenaara9f02812017-08-05 17:40:38 +02002985 if (channel->ch_close_cb != NULL)
2986 {
2987 typval_T argv[1];
2988 typval_T rettv;
2989 int dummy;
2990
2991 /* Increment the refcount to avoid the channel being freed
2992 * halfway. */
2993 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002994 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002995 (char *)channel->ch_close_cb);
2996 argv[0].v_type = VAR_CHANNEL;
2997 argv[0].vval.v_channel = channel;
2998 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002999 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003000 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003001 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003002 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003003
Bram Moolenaara9f02812017-08-05 17:40:38 +02003004 /* the callback is only called once */
3005 free_callback(channel->ch_close_cb, channel->ch_close_partial);
3006 channel->ch_close_cb = NULL;
3007 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003008
Bram Moolenaara9f02812017-08-05 17:40:38 +02003009 if (channel_need_redraw)
3010 {
3011 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003012 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003013 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003014
Bram Moolenaara9f02812017-08-05 17:40:38 +02003015 if (!channel->ch_drop_never)
3016 /* any remaining messages are useless now */
3017 for (part = PART_SOCK; part < PART_IN; ++part)
3018 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003019
3020 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003021 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003022 }
3023
3024 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003025
3026#ifdef FEAT_TERMINAL
3027 term_channel_closed(channel);
3028#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003029}
3030
Bram Moolenaard04a0202016-01-26 23:30:18 +01003031/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003032 * Close the "in" part channel "channel".
3033 */
3034 void
3035channel_close_in(channel_T *channel)
3036{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003037 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003038}
3039
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003040 static void
3041remove_from_writeque(writeq_T *wq, writeq_T *entry)
3042{
3043 ga_clear(&entry->wq_ga);
3044 wq->wq_next = entry->wq_next;
3045 if (wq->wq_next == NULL)
3046 wq->wq_prev = NULL;
3047 else
3048 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003049 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003050}
3051
Bram Moolenaar0874a832016-09-01 15:11:51 +02003052/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003053 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003054 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003055 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003056channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003057{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003058 chanpart_T *ch_part = &channel->ch_part[part];
3059 jsonq_T *json_head = &ch_part->ch_json_head;
3060 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003061
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003062 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003063 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003064
3065 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003066 {
3067 cbq_T *node = cb_head->cq_next;
3068
3069 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003070 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003071 vim_free(node);
3072 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003073
3074 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003075 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003076 free_tv(json_head->jq_next->jq_value);
3077 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003078 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003079
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003080 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3081 ch_part->ch_callback = NULL;
3082 ch_part->ch_partial = NULL;
3083
3084 while (ch_part->ch_writeque.wq_next != NULL)
3085 remove_from_writeque(&ch_part->ch_writeque,
3086 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003087}
3088
3089/*
3090 * Clear all the read buffers on "channel".
3091 */
3092 void
3093channel_clear(channel_T *channel)
3094{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003095 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003096 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003097 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003098 channel_clear_one(channel, PART_OUT);
3099 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003100 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003101 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003102 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003103 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003104 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003105 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003106 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003107}
3108
Bram Moolenaar77073442016-02-13 23:23:53 +01003109#if defined(EXITFREE) || defined(PROTO)
3110 void
3111channel_free_all(void)
3112{
3113 channel_T *channel;
3114
Bram Moolenaard6051b52016-02-28 15:49:03 +01003115 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003116 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3117 channel_clear(channel);
3118}
3119#endif
3120
3121
Bram Moolenaar715d2852016-04-30 17:06:31 +02003122/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003123#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003124
3125/* Buffer size for reading incoming messages. */
3126#define MAXMSGSIZE 4096
3127
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003128#if defined(HAVE_SELECT)
3129/*
3130 * Add write fds where we are waiting for writing to be possible.
3131 */
3132 static int
3133channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3134{
3135 int maxfd = maxfd_arg;
3136 channel_T *ch;
3137
3138 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3139 {
3140 chanpart_T *in_part = &ch->ch_part[PART_IN];
3141
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003142 if (in_part->ch_fd != INVALID_FD
3143 && (in_part->ch_bufref.br_buf != NULL
3144 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003145 {
3146 FD_SET((int)in_part->ch_fd, wfds);
3147 if ((int)in_part->ch_fd >= maxfd)
3148 maxfd = (int)in_part->ch_fd + 1;
3149 }
3150 }
3151 return maxfd;
3152}
3153#else
3154/*
3155 * Add write fds where we are waiting for writing to be possible.
3156 */
3157 static int
3158channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3159{
3160 int nfd = nfd_in;
3161 channel_T *ch;
3162
3163 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3164 {
3165 chanpart_T *in_part = &ch->ch_part[PART_IN];
3166
Bram Moolenaar683b7962017-08-19 15:51:59 +02003167 if (in_part->ch_fd != INVALID_FD
3168 && (in_part->ch_bufref.br_buf != NULL
3169 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003170 {
3171 in_part->ch_poll_idx = nfd;
3172 fds[nfd].fd = in_part->ch_fd;
3173 fds[nfd].events = POLLOUT;
3174 ++nfd;
3175 }
3176 else
3177 in_part->ch_poll_idx = -1;
3178 }
3179 return nfd;
3180}
3181#endif
3182
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003183typedef enum {
3184 CW_READY,
3185 CW_NOT_READY,
3186 CW_ERROR
3187} channel_wait_result;
3188
Bram Moolenaard04a0202016-01-26 23:30:18 +01003189/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003190 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003191 * Return CW_READY when there is something to read.
3192 * Return CW_NOT_READY when there is nothing to read.
3193 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003194 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003195 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003196channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003197{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003198 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003199 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003200
Bram Moolenaard8070362016-02-15 21:56:54 +01003201# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003202 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003203 {
3204 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003205 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003206 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003207 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003208
3209 /* reading from a pipe, not a socket */
3210 while (TRUE)
3211 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003212 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3213
3214 if (r && nread > 0)
3215 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003216
3217 if (channel->ch_named_pipe)
3218 {
3219 DisconnectNamedPipe((HANDLE)fd);
3220 ConnectNamedPipe((HANDLE)fd, NULL);
3221 }
3222 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003223 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003224
3225 /* perhaps write some buffer lines */
3226 channel_write_any_lines();
3227
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003228 sleep_time = deadline - GetTickCount();
3229 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003230 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003231 /* Wait for a little while. Very short at first, up to 10 msec
3232 * after looping a few times. */
3233 if (sleep_time > delay)
3234 sleep_time = delay;
3235 Sleep(sleep_time);
3236 delay = delay * 2;
3237 if (delay > 10)
3238 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003239 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003240 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003241 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003242#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003243 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003244#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003245 struct timeval tval;
3246 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003247 fd_set wfds;
3248 int ret;
3249 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003250
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003251 tval.tv_sec = timeout / 1000;
3252 tval.tv_usec = (timeout % 1000) * 1000;
3253 for (;;)
3254 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003255 FD_ZERO(&rfds);
3256 FD_SET((int)fd, &rfds);
3257
3258 /* Write lines to a pipe when a pipe can be written to. Need to
3259 * set this every time, some buffers may be done. */
3260 maxfd = (int)fd + 1;
3261 FD_ZERO(&wfds);
3262 maxfd = channel_fill_wfds(maxfd, &wfds);
3263
3264 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003265# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003266 SOCK_ERRNO;
3267 if (ret == -1 && errno == EINTR)
3268 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003269# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003270 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003271 {
3272 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003273 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003274 channel_write_any_lines();
3275 continue;
3276 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003277 break;
3278 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003279#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003280 for (;;)
3281 {
3282 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3283 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003284
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003285 fds[0].fd = fd;
3286 fds[0].events = POLLIN;
3287 nfd = channel_fill_poll_write(nfd, fds);
3288 if (poll(fds, nfd, timeout) > 0)
3289 {
3290 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003291 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003292 channel_write_any_lines();
3293 continue;
3294 }
3295 break;
3296 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003297#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003298 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003299 return CW_NOT_READY;
3300}
3301
3302 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003303ch_close_part_on_error(
3304 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003305{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003306 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003307
3308 if (is_err)
3309 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003310 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003311 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003312 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003313
3314 /* Queue a "DETACH" netbeans message in the command queue in order to
3315 * terminate the netbeans session later. Do not end the session here
3316 * directly as we may be running in the context of a call to
3317 * netbeans_parse_messages():
3318 * netbeans_parse_messages
3319 * -> autocmd triggered while processing the netbeans cmd
3320 * -> ui_breakcheck
3321 * -> gui event loop or select loop
3322 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003323 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003324 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003325 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003326 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003327 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3328
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003329 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003330 * close the channel yet, there may be something to read on another part.
3331 * When stdout and stderr use the same FD we get the error only on one of
3332 * them, also close the other. */
3333 if (part == PART_OUT || part == PART_ERR)
3334 {
3335 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3336
3337 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3338 ch_close_part(channel, other);
3339 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003340 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003341
3342#ifdef FEAT_GUI
3343 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003344 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003345#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003346}
3347
3348 static void
3349channel_close_now(channel_T *channel)
3350{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003351 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003352 if (channel->ch_nb_close_cb != NULL)
3353 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003354 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003355}
3356
3357/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003358 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003359 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003360 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003361 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003362 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003363channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003364{
3365 static char_u *buf = NULL;
3366 int len = 0;
3367 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003368 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003369 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003370
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003371 fd = channel->ch_part[part].ch_fd;
3372 if (fd == INVALID_FD)
3373 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003374 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003375 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003376 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003377 }
3378 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003379
3380 /* Allocate a buffer to read into. */
3381 if (buf == NULL)
3382 {
3383 buf = alloc(MAXMSGSIZE);
3384 if (buf == NULL)
3385 return; /* out of memory! */
3386 }
3387
3388 /* Keep on reading for as long as there is something to read.
3389 * Use select() or poll() to avoid blocking on a message that is exactly
3390 * MAXMSGSIZE long. */
3391 for (;;)
3392 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003393 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003394 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003395 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003396 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003397 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003398 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003399 if (len <= 0)
3400 break; /* error or nothing more to read */
3401
3402 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003403 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003404 readlen += len;
3405 if (len < MAXMSGSIZE)
3406 break; /* did read everything that's available */
3407 }
3408
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003409 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003410 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003411 {
3412 if (!channel->ch_keep_open)
3413 ch_close_part_on_error(channel, part, (len < 0), func);
3414 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003415#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003416 else if (CH_HAS_GUI && gtk_main_level() > 0)
3417 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003418 gtk_main_quit();
3419#endif
3420}
3421
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003422/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003423 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003424 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003425 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003426 * Returns what was read in allocated memory.
3427 * Returns NULL in case of error or timeout.
3428 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003429 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003430channel_read_block(
3431 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003432{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003433 char_u *buf;
3434 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003435 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003436 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003437 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003438 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003439
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003440 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003441 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003442
3443 while (TRUE)
3444 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003445 node = channel_peek(channel, part);
3446 if (node != NULL)
3447 {
3448 if (mode == MODE_RAW || (mode == MODE_NL
3449 && channel_first_nl(node) != NULL))
3450 /* got a complete message */
3451 break;
3452 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3453 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003454 /* If not blocking or nothing more is coming then return what we
3455 * have. */
3456 if (raw || fd == INVALID_FD)
3457 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003458 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003459
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003460 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003461 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003462 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003463 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003464 {
3465 ch_log(channel, "Timed out");
3466 return NULL;
3467 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003468 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003469 }
3470
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003471 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003472 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003473 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003474 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003475 }
3476 else
3477 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003478 char_u *p;
3479
3480 buf = node->rq_buffer;
3481 nl = channel_first_nl(node);
3482
3483 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003484 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003485 if (*p == NUL)
3486 *p = NL;
3487
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003488 if (nl == NULL)
3489 {
3490 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003491 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003492 }
3493 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003494 {
3495 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003496 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003497 *nl = NUL;
3498 }
3499 else
3500 {
3501 /* Copy the message into allocated memory and remove it from the
3502 * buffer. */
3503 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003504 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003505 }
3506 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003507 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003508 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003509 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003510}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003511
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003512/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003513 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003514 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003515 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003516 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003517 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003518 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003519channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003520 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003521 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003522 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003523 int id,
3524 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003525{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003526 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003527 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003528 int timeout;
3529 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003530
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003531 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003532 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003533 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003534 for (;;)
3535 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003536 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003537
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003538 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003539 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003540 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003541 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003542 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003543 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003544
Bram Moolenaard7ece102016-02-02 23:23:02 +01003545 if (!more)
3546 {
3547 /* Handle any other messages in the queue. If done some more
3548 * messages may have arrived. */
3549 if (channel_parse_messages())
3550 continue;
3551
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003552 /* Wait for up to the timeout. If there was an incomplete message
3553 * use the deadline for that. */
3554 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003555 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003556 {
3557#ifdef WIN32
3558 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3559#else
3560 {
3561 struct timeval now_tv;
3562
3563 gettimeofday(&now_tv, NULL);
3564 timeout = (chanpart->ch_deadline.tv_sec
3565 - now_tv.tv_sec) * 1000
3566 + (chanpart->ch_deadline.tv_usec
3567 - now_tv.tv_usec) / 1000
3568 + 1;
3569 }
3570#endif
3571 if (timeout < 0)
3572 {
3573 /* Something went wrong, channel_parse_json() didn't
3574 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003575 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003576 timeout = timeout_arg;
3577 }
3578 else if (timeout > timeout_arg)
3579 timeout = timeout_arg;
3580 }
3581 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003582 if (fd == INVALID_FD
3583 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003584 {
3585 if (timeout == timeout_arg)
3586 {
3587 if (fd != INVALID_FD)
3588 ch_log(channel, "Timed out");
3589 break;
3590 }
3591 }
3592 else
3593 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003594 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003595 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003596 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003597 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003598}
3599
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003600/*
3601 * Common for ch_read() and ch_readraw().
3602 */
3603 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003604common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003605{
3606 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003607 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003608 jobopt_T opt;
3609 int mode;
3610 int timeout;
3611 int id = -1;
3612 typval_T *listtv = NULL;
3613
3614 /* return an empty string by default */
3615 rettv->v_type = VAR_STRING;
3616 rettv->vval.v_string = NULL;
3617
3618 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003619 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003620 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003621 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003622
Bram Moolenaar437905c2016-04-26 19:01:05 +02003623 if (opt.jo_set & JO_PART)
3624 part = opt.jo_part;
3625 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003626 if (channel != NULL)
3627 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003628 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003629 part = channel_part_read(channel);
3630 mode = channel_get_mode(channel, part);
3631 timeout = channel_get_timeout(channel, part);
3632 if (opt.jo_set & JO_TIMEOUT)
3633 timeout = opt.jo_timeout;
3634
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003635 if (blob)
3636 {
3637 int outlen = 0;
3638 char_u *p = channel_read_block(channel, part,
3639 timeout, TRUE, &outlen);
3640 if (p != NULL)
3641 {
3642 blob_T *b = blob_alloc();
3643
3644 if (b != NULL)
3645 {
3646 b->bv_ga.ga_len = outlen;
3647 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3648 blob_free(b);
3649 else
3650 {
3651 memcpy(b->bv_ga.ga_data, p, outlen);
3652 rettv_blob_set(rettv, b);
3653 }
3654 }
3655 vim_free(p);
3656 }
3657 }
3658 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003659 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003660 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003661 else
3662 {
3663 if (opt.jo_set & JO_ID)
3664 id = opt.jo_id;
3665 channel_read_json_block(channel, part, timeout, id, &listtv);
3666 if (listtv != NULL)
3667 {
3668 *rettv = *listtv;
3669 vim_free(listtv);
3670 }
3671 else
3672 {
3673 rettv->v_type = VAR_SPECIAL;
3674 rettv->vval.v_number = VVAL_NONE;
3675 }
3676 }
3677 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003678
3679theend:
3680 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003681}
3682
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003683# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3684 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003685/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003686 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003687 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003688 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003689 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003690channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003691{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003692 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003693 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003694
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003695 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003696 for (channel = first_channel; channel != NULL;
3697 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003698 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003699 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003700 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003701 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003702 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003703 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003704 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003705 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003706 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003707}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003708# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003709
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003710# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003711/*
3712 * Check the channels for anything that is ready to be read.
3713 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003714 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003715 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003716 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003717channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003718{
3719 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003720 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003721 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003722
3723 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3724 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003725 if (only_keep_open && !channel->ch_keep_open)
3726 continue;
3727
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003728 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003729 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003730 {
3731 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003732 if (fd != INVALID_FD)
3733 {
3734 int r = channel_wait(channel, fd, 0);
3735
3736 if (r == CW_READY)
3737 channel_read(channel, part, "channel_handle_events");
3738 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003739 ch_close_part_on_error(channel, part, TRUE,
3740 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003741 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003742 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003743 }
3744}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003745# endif
3746
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003747# if defined(FEAT_GUI) || defined(PROTO)
3748/*
3749 * Return TRUE when there is any channel with a keep_open flag.
3750 */
3751 int
3752channel_any_keep_open()
3753{
3754 channel_T *channel;
3755
3756 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3757 if (channel->ch_keep_open)
3758 return TRUE;
3759 return FALSE;
3760}
3761# endif
3762
Bram Moolenaard04a0202016-01-26 23:30:18 +01003763/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003764 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003765 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003766 */
3767 void
3768channel_set_nonblock(channel_T *channel, ch_part_T part)
3769{
3770 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003771 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003772
3773 if (fd != INVALID_FD)
3774 {
3775#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003776 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003777
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003778 ioctlsocket(fd, FIONBIO, &val);
3779#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003780 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003781#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003782 ch_part->ch_nonblocking = TRUE;
3783 }
3784}
3785
3786/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003787 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003788 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003789 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003790 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003791 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003792channel_send(
3793 channel_T *channel,
3794 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003795 char_u *buf_arg,
3796 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003797 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003798{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003799 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003800 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003801 chanpart_T *ch_part = &channel->ch_part[part];
3802 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003803
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003804 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003805 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003806 {
3807 if (!channel->ch_error && fun != NULL)
3808 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003809 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003810 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003811 }
3812 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003813 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003814 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003815
Bram Moolenaar0b146882018-09-06 16:27:24 +02003816 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3817 channel_set_nonblock(channel, part);
3818
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003819 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003820 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003821 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003822 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003823 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003824 fprintf(log_fd, "'\n");
3825 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003826 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003827 }
3828
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003829 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003830 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003831 writeq_T *wq = &ch_part->ch_writeque;
3832 char_u *buf;
3833 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003834
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003835 if (wq->wq_next != NULL)
3836 {
3837 /* first write what was queued */
3838 buf = wq->wq_next->wq_ga.ga_data;
3839 len = wq->wq_next->wq_ga.ga_len;
3840 did_use_queue = TRUE;
3841 }
3842 else
3843 {
3844 if (len_arg == 0)
3845 /* nothing to write, called from channel_select_check() */
3846 return OK;
3847 buf = buf_arg;
3848 len = len_arg;
3849 }
3850
3851 if (part == PART_SOCK)
3852 res = sock_write(fd, (char *)buf, len);
3853 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003854 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003855 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar31faed62019-01-22 23:01:40 +01003856#ifdef WIN32
3857 if (channel->ch_named_pipe && res < 0)
3858 {
3859 DisconnectNamedPipe((HANDLE)fd);
3860 ConnectNamedPipe((HANDLE)fd, NULL);
3861 }
3862#endif
3863 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003864 if (res < 0 && (errno == EWOULDBLOCK
3865#ifdef EAGAIN
3866 || errno == EAGAIN
3867#endif
3868 ))
3869 res = 0; /* nothing got written */
3870
3871 if (res >= 0 && ch_part->ch_nonblocking)
3872 {
3873 writeq_T *entry = wq->wq_next;
3874
3875 if (did_use_queue)
3876 ch_log(channel, "Sent %d bytes now", res);
3877 if (res == len)
3878 {
3879 /* Wrote all the buf[len] bytes. */
3880 if (entry != NULL)
3881 {
3882 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003883 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003884 continue;
3885 }
3886 if (did_use_queue)
3887 ch_log(channel, "Write queue empty");
3888 }
3889 else
3890 {
3891 /* Wrote only buf[res] bytes, can't write more now. */
3892 if (entry != NULL)
3893 {
3894 if (res > 0)
3895 {
3896 /* Remove the bytes that were written. */
3897 mch_memmove(entry->wq_ga.ga_data,
3898 (char *)entry->wq_ga.ga_data + res,
3899 len - res);
3900 entry->wq_ga.ga_len -= res;
3901 }
3902 buf = buf_arg;
3903 len = len_arg;
3904 }
3905 else
3906 {
3907 buf += res;
3908 len -= res;
3909 }
3910 ch_log(channel, "Adding %d bytes to the write queue", len);
3911
3912 /* Append the not written bytes of the argument to the write
3913 * buffer. Limit entries to 4000 bytes. */
3914 if (wq->wq_prev != NULL
3915 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3916 {
3917 writeq_T *last = wq->wq_prev;
3918
3919 /* append to the last entry */
3920 if (ga_grow(&last->wq_ga, len) == OK)
3921 {
3922 mch_memmove((char *)last->wq_ga.ga_data
3923 + last->wq_ga.ga_len,
3924 buf, len);
3925 last->wq_ga.ga_len += len;
3926 }
3927 }
3928 else
3929 {
3930 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3931
3932 if (last != NULL)
3933 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003934 last->wq_prev = wq->wq_prev;
3935 last->wq_next = NULL;
3936 if (wq->wq_prev == NULL)
3937 wq->wq_next = last;
3938 else
3939 wq->wq_prev->wq_next = last;
3940 wq->wq_prev = last;
3941 ga_init2(&last->wq_ga, 1, 1000);
3942 if (ga_grow(&last->wq_ga, len) == OK)
3943 {
3944 mch_memmove(last->wq_ga.ga_data, buf, len);
3945 last->wq_ga.ga_len = len;
3946 }
3947 }
3948 }
3949 }
3950 }
3951 else if (res != len)
3952 {
3953 if (!channel->ch_error && fun != NULL)
3954 {
3955 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003957 }
3958 channel->ch_error = TRUE;
3959 return FAIL;
3960 }
3961
3962 channel->ch_error = FALSE;
3963 return OK;
3964 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003965}
3966
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003967/*
3968 * Common for "ch_sendexpr()" and "ch_sendraw()".
3969 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003970 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003971 * Otherwise returns NULL.
3972 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003973 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003974send_common(
3975 typval_T *argvars,
3976 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003977 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003978 int id,
3979 int eval,
3980 jobopt_T *opt,
3981 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003982 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003983{
3984 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003985 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003986
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003987 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003988 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003989 if (channel == NULL)
3990 return NULL;
3991 part_send = channel_part_send(channel);
3992 *part_read = channel_part_read(channel);
3993
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003994 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003995 return NULL;
3996
3997 /* Set the callback. An empty callback means no callback and not reading
3998 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3999 * allowed. */
4000 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
4001 {
4002 if (eval)
4003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004004 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004005 return NULL;
4006 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02004007 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004008 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004009 }
4010
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004011 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004012 && opt->jo_callback == NULL)
4013 return channel;
4014 return NULL;
4015}
4016
4017/*
4018 * common for "ch_evalexpr()" and "ch_sendexpr()"
4019 */
4020 void
4021ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4022{
4023 char_u *text;
4024 typval_T *listtv;
4025 channel_T *channel;
4026 int id;
4027 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004028 ch_part_T part_send;
4029 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004030 jobopt_T opt;
4031 int timeout;
4032
4033 /* return an empty string by default */
4034 rettv->v_type = VAR_STRING;
4035 rettv->vval.v_string = NULL;
4036
Bram Moolenaar437905c2016-04-26 19:01:05 +02004037 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004038 if (channel == NULL)
4039 return;
4040 part_send = channel_part_send(channel);
4041
4042 ch_mode = channel_get_mode(channel, part_send);
4043 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004045 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004046 return;
4047 }
4048
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004049 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004050 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004051 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004052 if (text == NULL)
4053 return;
4054
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004055 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004056 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4057 vim_free(text);
4058 if (channel != NULL && eval)
4059 {
4060 if (opt.jo_set & JO_TIMEOUT)
4061 timeout = opt.jo_timeout;
4062 else
4063 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4065 == OK)
4066 {
4067 list_T *list = listtv->vval.v_list;
4068
4069 /* Move the item from the list and then change the type to
4070 * avoid the value being freed. */
4071 *rettv = list->lv_last->li_tv;
4072 list->lv_last->li_tv.v_type = VAR_NUMBER;
4073 free_tv(listtv);
4074 }
4075 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004076 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004077}
4078
4079/*
4080 * common for "ch_evalraw()" and "ch_sendraw()"
4081 */
4082 void
4083ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4084{
4085 char_u buf[NUMBUFLEN];
4086 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004087 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004089 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004090 jobopt_T opt;
4091 int timeout;
4092
4093 /* return an empty string by default */
4094 rettv->v_type = VAR_STRING;
4095 rettv->vval.v_string = NULL;
4096
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004097 if (argvars[1].v_type == VAR_BLOB)
4098 {
4099 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4100 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4101 }
4102 else
4103 {
4104 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004105 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004106 }
4107 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004108 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4109 if (channel != NULL && eval)
4110 {
4111 if (opt.jo_set & JO_TIMEOUT)
4112 timeout = opt.jo_timeout;
4113 else
4114 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004115 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004116 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004117 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004118 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004119}
4120
Bram Moolenaarf3360612017-10-01 16:21:31 +02004121# define KEEP_OPEN_TIME 20 /* msec */
4122
Bram Moolenaard04a0202016-01-26 23:30:18 +01004123# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004124/*
4125 * Add open channels to the poll struct.
4126 * Return the adjusted struct index.
4127 * The type of "fds" is hidden to avoid problems with the function proto.
4128 */
4129 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004130channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004131{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004132 int nfd = nfd_in;
4133 channel_T *channel;
4134 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004135 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004136
Bram Moolenaar77073442016-02-13 23:23:53 +01004137 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004138 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004139 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004140 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004141 chanpart_T *ch_part = &channel->ch_part[part];
4142
4143 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004144 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004145 if (channel->ch_keep_open)
4146 {
4147 /* For unknown reason poll() returns immediately for a
4148 * keep-open channel. Instead of adding it to the fds add
4149 * a short timeout and check, like polling. */
4150 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4151 *towait = KEEP_OPEN_TIME;
4152 }
4153 else
4154 {
4155 ch_part->ch_poll_idx = nfd;
4156 fds[nfd].fd = ch_part->ch_fd;
4157 fds[nfd].events = POLLIN;
4158 nfd++;
4159 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004160 }
4161 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004162 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004163 }
4164 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004165
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004166 nfd = channel_fill_poll_write(nfd, fds);
4167
Bram Moolenaare0874f82016-01-24 20:36:41 +01004168 return nfd;
4169}
4170
4171/*
4172 * The type of "fds" is hidden to avoid problems with the function proto.
4173 */
4174 int
4175channel_poll_check(int ret_in, void *fds_in)
4176{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004177 int ret = ret_in;
4178 channel_T *channel;
4179 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004180 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004181 int idx;
4182 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004183
Bram Moolenaar77073442016-02-13 23:23:53 +01004184 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004185 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004186 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004187 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004188 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004189
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004190 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004191 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004192 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004193 --ret;
4194 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004195 else if (channel->ch_part[part].ch_fd != INVALID_FD
4196 && channel->ch_keep_open)
4197 {
4198 /* polling a keep-open channel */
4199 channel_read(channel, part, "channel_poll_check_keep_open");
4200 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004201 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004202
4203 in_part = &channel->ch_part[PART_IN];
4204 idx = in_part->ch_poll_idx;
4205 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4206 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004207 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004208 --ret;
4209 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004210 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004211
4212 return ret;
4213}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004214# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004215
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004216# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004217
Bram Moolenaare0874f82016-01-24 20:36:41 +01004218/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004219 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004220 */
4221 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004222channel_select_setup(
4223 int maxfd_in,
4224 void *rfds_in,
4225 void *wfds_in,
4226 struct timeval *tv,
4227 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004228{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004229 int maxfd = maxfd_in;
4230 channel_T *channel;
4231 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004232 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004233 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004234
Bram Moolenaar77073442016-02-13 23:23:53 +01004235 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004236 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004237 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004238 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004239 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004240
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004241 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004242 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004243 if (channel->ch_keep_open)
4244 {
4245 /* For unknown reason select() returns immediately for a
4246 * keep-open channel. Instead of adding it to the rfds add
4247 * a short timeout and check, like polling. */
4248 if (*tvp == NULL || tv->tv_sec > 0
4249 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4250 {
4251 *tvp = tv;
4252 tv->tv_sec = 0;
4253 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4254 }
4255 }
4256 else
4257 {
4258 FD_SET((int)fd, rfds);
4259 if (maxfd < (int)fd)
4260 maxfd = (int)fd;
4261 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004262 }
4263 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004264 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004265
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004266 maxfd = channel_fill_wfds(maxfd, wfds);
4267
Bram Moolenaare0874f82016-01-24 20:36:41 +01004268 return maxfd;
4269}
4270
4271/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004272 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004273 */
4274 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004275channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004276{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004277 int ret = ret_in;
4278 channel_T *channel;
4279 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004280 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004281 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004282 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004283
Bram Moolenaar77073442016-02-13 23:23:53 +01004284 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004285 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004286 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004287 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004288 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004289
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004290 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004291 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004292 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004293 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004294 --ret;
4295 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004296 else if (fd != INVALID_FD && channel->ch_keep_open)
4297 {
4298 /* polling a keep-open channel */
4299 channel_read(channel, part, "channel_select_check_keep_open");
4300 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004301 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004302
4303 in_part = &channel->ch_part[PART_IN];
4304 if (ret > 0 && in_part->ch_fd != INVALID_FD
4305 && FD_ISSET(in_part->ch_fd, wfds))
4306 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004307 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004308 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004309 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004310 --ret;
4311 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004312 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004313
4314 return ret;
4315}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004316# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004317
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004318/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004319 * Execute queued up commands.
4320 * Invoked from the main loop when it's safe to execute received commands.
4321 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004322 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004323 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004324channel_parse_messages(void)
4325{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004326 channel_T *channel = first_channel;
4327 int ret = FALSE;
4328 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004329 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004330#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004331 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004332
4333 ELAPSED_INIT(start_tv);
4334#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004335
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004336 ++safe_to_invoke_callback;
4337
Bram Moolenaard0b65022016-03-06 21:50:33 +01004338 /* Only do this message when another message was given, otherwise we get
4339 * lots of them. */
4340 if (did_log_msg)
4341 {
4342 ch_log(NULL, "looking for messages on channels");
4343 did_log_msg = FALSE;
4344 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004345 while (channel != NULL)
4346 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004347 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004348 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004349 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004350 channel_close_now(channel);
4351 /* channel may have been freed, start over */
4352 channel = first_channel;
4353 continue;
4354 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004355 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004356 {
4357 channel_free(channel);
4358 /* channel has been freed, start over */
4359 channel = first_channel;
4360 continue;
4361 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004362 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004363 {
4364 /* channel is no longer useful, free it */
4365 channel_free(channel);
4366 channel = first_channel;
4367 part = PART_SOCK;
4368 continue;
4369 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004370 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004371 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004372 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004373 /* Increase the refcount, in case the handler causes the channel
4374 * to be unreferenced or closed. */
4375 ++channel->ch_refcount;
4376 r = may_invoke_callback(channel, part);
4377 if (r == OK)
4378 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004379 if (channel_unref(channel) || (r == OK
4380#ifdef ELAPSED_FUNC
4381 /* Limit the time we loop here to 100 msec, otherwise
4382 * Vim becomes unresponsive when the callback takes
4383 * more than a bit of time. */
4384 && ELAPSED_FUNC(start_tv) < 100L
4385#endif
4386 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004387 {
4388 /* channel was freed or something was done, start over */
4389 channel = first_channel;
4390 part = PART_SOCK;
4391 continue;
4392 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004393 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004394 if (part < PART_ERR)
4395 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004396 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004397 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004398 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004399 part = PART_SOCK;
4400 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004401 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004402
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004403 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004404 {
4405 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004406 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004407 }
4408
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004409 --safe_to_invoke_callback;
4410
Bram Moolenaard7ece102016-02-02 23:23:02 +01004411 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004412}
4413
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004414/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004415 * Return TRUE if any channel has readahead. That means we should not block on
4416 * waiting for input.
4417 */
4418 int
4419channel_any_readahead(void)
4420{
4421 channel_T *channel = first_channel;
4422 ch_part_T part = PART_SOCK;
4423
4424 while (channel != NULL)
4425 {
4426 if (channel_has_readahead(channel, part))
4427 return TRUE;
4428 if (part < PART_ERR)
4429 ++part;
4430 else
4431 {
4432 channel = channel->ch_next;
4433 part = PART_SOCK;
4434 }
4435 }
4436 return FALSE;
4437}
4438
4439/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004440 * Mark references to lists used in channels.
4441 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004442 int
4443set_ref_in_channel(int copyID)
4444{
Bram Moolenaar77073442016-02-13 23:23:53 +01004445 int abort = FALSE;
4446 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004447 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004448
Bram Moolenaar77073442016-02-13 23:23:53 +01004449 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004450 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004451 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004452 tv.v_type = VAR_CHANNEL;
4453 tv.vval.v_channel = channel;
4454 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004455 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004456 return abort;
4457}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004458
4459/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004460 * Return the "part" to write to for "channel".
4461 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004462 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004463channel_part_send(channel_T *channel)
4464{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004465 if (channel->CH_SOCK_FD == INVALID_FD)
4466 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004467 return PART_SOCK;
4468}
4469
4470/*
4471 * Return the default "part" to read from for "channel".
4472 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004473 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004474channel_part_read(channel_T *channel)
4475{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004476 if (channel->CH_SOCK_FD == INVALID_FD)
4477 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004478 return PART_SOCK;
4479}
4480
4481/*
4482 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004483 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004484 */
4485 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004486channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004487{
Bram Moolenaar77073442016-02-13 23:23:53 +01004488 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004489 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004490 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004491}
4492
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004493/*
4494 * Return the timeout of "channel"/"part"
4495 */
4496 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004497channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004498{
4499 return channel->ch_part[part].ch_timeout;
4500}
4501
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004502 static int
4503handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4504{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004505 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004506
4507 opt->jo_set |= jo;
4508 if (STRCMP(val, "nl") == 0)
4509 *modep = MODE_NL;
4510 else if (STRCMP(val, "raw") == 0)
4511 *modep = MODE_RAW;
4512 else if (STRCMP(val, "js") == 0)
4513 *modep = MODE_JS;
4514 else if (STRCMP(val, "json") == 0)
4515 *modep = MODE_JSON;
4516 else
4517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004518 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004519 return FAIL;
4520 }
4521 return OK;
4522}
4523
4524 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004525handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004526{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004527 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004528
4529 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4530 if (STRCMP(val, "null") == 0)
4531 opt->jo_io[part] = JIO_NULL;
4532 else if (STRCMP(val, "pipe") == 0)
4533 opt->jo_io[part] = JIO_PIPE;
4534 else if (STRCMP(val, "file") == 0)
4535 opt->jo_io[part] = JIO_FILE;
4536 else if (STRCMP(val, "buffer") == 0)
4537 opt->jo_io[part] = JIO_BUFFER;
4538 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4539 opt->jo_io[part] = JIO_OUT;
4540 else
4541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004542 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004543 return FAIL;
4544 }
4545 return OK;
4546}
4547
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004548/*
4549 * Clear a jobopt_T before using it.
4550 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 void
4552clear_job_options(jobopt_T *opt)
4553{
4554 vim_memset(opt, 0, sizeof(jobopt_T));
4555}
4556
4557/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004558 * Free any members of a jobopt_T.
4559 */
4560 void
4561free_job_options(jobopt_T *opt)
4562{
4563 if (opt->jo_partial != NULL)
4564 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004565 else if (opt->jo_callback != NULL)
4566 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004567 if (opt->jo_out_partial != NULL)
4568 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004569 else if (opt->jo_out_cb != NULL)
4570 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004571 if (opt->jo_err_partial != NULL)
4572 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004573 else if (opt->jo_err_cb != NULL)
4574 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004575 if (opt->jo_close_partial != NULL)
4576 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004577 else if (opt->jo_close_cb != NULL)
4578 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004579 if (opt->jo_exit_partial != NULL)
4580 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004581 else if (opt->jo_exit_cb != NULL)
4582 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004583 if (opt->jo_env != NULL)
4584 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004585}
4586
4587/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004588 * Get the PART_ number from the first character of an option name.
4589 */
4590 static int
4591part_from_char(int c)
4592{
4593 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4594}
4595
4596/*
4597 * Get the option entries from the dict in "tv", parse them and put the result
4598 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004599 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004600 * If an option value is invalid return FAIL.
4601 */
4602 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004603get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004604{
4605 typval_T *item;
4606 char_u *val;
4607 dict_T *dict;
4608 int todo;
4609 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004610 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004611
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004612 if (tv->v_type == VAR_UNKNOWN)
4613 return OK;
4614 if (tv->v_type != VAR_DICT)
4615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004616 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004617 return FAIL;
4618 }
4619 dict = tv->vval.v_dict;
4620 if (dict == NULL)
4621 return OK;
4622
4623 todo = (int)dict->dv_hashtab.ht_used;
4624 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4625 if (!HASHITEM_EMPTY(hi))
4626 {
4627 item = &dict_lookup(hi)->di_tv;
4628
4629 if (STRCMP(hi->hi_key, "mode") == 0)
4630 {
4631 if (!(supported & JO_MODE))
4632 break;
4633 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4634 return FAIL;
4635 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004636 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004637 {
4638 if (!(supported & JO_IN_MODE))
4639 break;
4640 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4641 == FAIL)
4642 return FAIL;
4643 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004644 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004645 {
4646 if (!(supported & JO_OUT_MODE))
4647 break;
4648 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4649 == FAIL)
4650 return FAIL;
4651 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004652 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004653 {
4654 if (!(supported & JO_ERR_MODE))
4655 break;
4656 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4657 == FAIL)
4658 return FAIL;
4659 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004660 else if (STRCMP(hi->hi_key, "noblock") == 0)
4661 {
4662 if (!(supported & JO_MODE))
4663 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004664 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004665 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004666 else if (STRCMP(hi->hi_key, "in_io") == 0
4667 || STRCMP(hi->hi_key, "out_io") == 0
4668 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004669 {
4670 if (!(supported & JO_OUT_IO))
4671 break;
4672 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4673 return FAIL;
4674 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004675 else if (STRCMP(hi->hi_key, "in_name") == 0
4676 || STRCMP(hi->hi_key, "out_name") == 0
4677 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004678 {
4679 part = part_from_char(*hi->hi_key);
4680
4681 if (!(supported & JO_OUT_IO))
4682 break;
4683 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4684 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004685 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004686 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004687 else if (STRCMP(hi->hi_key, "pty") == 0)
4688 {
4689 if (!(supported & JO_MODE))
4690 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004691 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004692 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004693 else if (STRCMP(hi->hi_key, "in_buf") == 0
4694 || STRCMP(hi->hi_key, "out_buf") == 0
4695 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004696 {
4697 part = part_from_char(*hi->hi_key);
4698
4699 if (!(supported & JO_OUT_IO))
4700 break;
4701 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004702 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004703 if (opt->jo_io_buf[part] <= 0)
4704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004705 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004706 return FAIL;
4707 }
4708 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004710 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004711 return FAIL;
4712 }
4713 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004714 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4715 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4716 {
4717 part = part_from_char(*hi->hi_key);
4718
4719 if (!(supported & JO_OUT_IO))
4720 break;
4721 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004722 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004723 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004724 else if (STRCMP(hi->hi_key, "out_msg") == 0
4725 || STRCMP(hi->hi_key, "err_msg") == 0)
4726 {
4727 part = part_from_char(*hi->hi_key);
4728
4729 if (!(supported & JO_OUT_IO))
4730 break;
4731 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004732 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004733 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004734 else if (STRCMP(hi->hi_key, "in_top") == 0
4735 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004736 {
4737 linenr_T *lp;
4738
4739 if (!(supported & JO_OUT_IO))
4740 break;
4741 if (hi->hi_key[3] == 't')
4742 {
4743 lp = &opt->jo_in_top;
4744 opt->jo_set |= JO_IN_TOP;
4745 }
4746 else
4747 {
4748 lp = &opt->jo_in_bot;
4749 opt->jo_set |= JO_IN_BOT;
4750 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004751 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004752 if (*lp < 0)
4753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004754 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004755 return FAIL;
4756 }
4757 }
4758 else if (STRCMP(hi->hi_key, "channel") == 0)
4759 {
4760 if (!(supported & JO_OUT_IO))
4761 break;
4762 opt->jo_set |= JO_CHANNEL;
4763 if (item->v_type != VAR_CHANNEL)
4764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004765 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004766 return FAIL;
4767 }
4768 opt->jo_channel = item->vval.v_channel;
4769 }
4770 else if (STRCMP(hi->hi_key, "callback") == 0)
4771 {
4772 if (!(supported & JO_CALLBACK))
4773 break;
4774 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004775 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004776 if (opt->jo_callback == NULL)
4777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004778 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004779 return FAIL;
4780 }
4781 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004782 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004783 {
4784 if (!(supported & JO_OUT_CALLBACK))
4785 break;
4786 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004787 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004788 if (opt->jo_out_cb == NULL)
4789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004790 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004791 return FAIL;
4792 }
4793 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004794 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004795 {
4796 if (!(supported & JO_ERR_CALLBACK))
4797 break;
4798 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004799 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004800 if (opt->jo_err_cb == NULL)
4801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004802 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004803 return FAIL;
4804 }
4805 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004806 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004807 {
4808 if (!(supported & JO_CLOSE_CALLBACK))
4809 break;
4810 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004811 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004812 if (opt->jo_close_cb == NULL)
4813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004814 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004815 return FAIL;
4816 }
4817 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004818 else if (STRCMP(hi->hi_key, "drop") == 0)
4819 {
4820 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004821 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004822
4823 if (STRCMP(val, "never") == 0)
4824 never = TRUE;
4825 else if (STRCMP(val, "auto") != 0)
4826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004827 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004828 return FAIL;
4829 }
4830 opt->jo_drop_never = never;
4831 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004832 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4833 {
4834 if (!(supported & JO_EXIT_CB))
4835 break;
4836 opt->jo_set |= JO_EXIT_CB;
4837 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4838 if (opt->jo_exit_cb == NULL)
4839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004840 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004841 return FAIL;
4842 }
4843 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004844#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004845 else if (STRCMP(hi->hi_key, "term_name") == 0)
4846 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004847 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004848 break;
4849 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004850 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004851 if (opt->jo_term_name == NULL)
4852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004853 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004854 return FAIL;
4855 }
4856 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004857 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4858 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004859 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004860 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004861 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004862 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004864 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004865 return FAIL;
4866 }
4867 opt->jo_set2 |= JO2_TERM_FINISH;
4868 opt->jo_term_finish = *val;
4869 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004870 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4871 {
4872 char_u *p;
4873
4874 if (!(supported2 & JO2_TERM_OPENCMD))
4875 break;
4876 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004877 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004878 if (p != NULL)
4879 {
4880 /* Must have %d and no other %. */
4881 p = vim_strchr(p, '%');
4882 if (p != NULL && (p[1] != 'd'
4883 || vim_strchr(p + 2, '%') != NULL))
4884 p = NULL;
4885 }
4886 if (p == NULL)
4887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004888 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004889 return FAIL;
4890 }
4891 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004892 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4893 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004894 char_u *p;
4895
4896 if (!(supported2 & JO2_EOF_CHARS))
4897 break;
4898 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004899 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004900 if (p == NULL)
4901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004902 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004903 return FAIL;
4904 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004905 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004906 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4907 {
4908 if (!(supported2 & JO2_TERM_ROWS))
4909 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004910 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004911 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004912 }
4913 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4914 {
4915 if (!(supported2 & JO2_TERM_COLS))
4916 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004917 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004918 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004919 }
4920 else if (STRCMP(hi->hi_key, "vertical") == 0)
4921 {
4922 if (!(supported2 & JO2_VERTICAL))
4923 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004924 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004925 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004926 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004927 else if (STRCMP(hi->hi_key, "curwin") == 0)
4928 {
4929 if (!(supported2 & JO2_CURWIN))
4930 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004931 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004932 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004933 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004934 else if (STRCMP(hi->hi_key, "hidden") == 0)
4935 {
4936 if (!(supported2 & JO2_HIDDEN))
4937 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004938 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004940 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004941 else if (STRCMP(hi->hi_key, "norestore") == 0)
4942 {
4943 if (!(supported2 & JO2_NORESTORE))
4944 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004945 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004947 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004948 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4949 {
4950 if (!(supported2 & JO2_TERM_KILL))
4951 break;
4952 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004953 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004954 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004955 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004956 {
4957 char_u *p;
4958
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004959 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004960 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004961 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004962 p = tv_get_string_chk(item);
4963 if (p == NULL)
4964 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004965 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004966 return FAIL;
4967 }
4968 // Allow empty string, "winpty", "conpty".
4969 if (!(*p == NUL || STRCMP(p, "winpty") == 0
4970 || STRCMP(p, "conpty") == 0))
4971 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004972 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004973 return FAIL;
4974 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01004975 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004976 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004977# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4978 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4979 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004980 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004981 listitem_T *li;
4982 long_u rgb[16];
4983
4984 if (!(supported2 & JO2_ANSI_COLORS))
4985 break;
4986
4987 if (item == NULL || item->v_type != VAR_LIST
4988 || item->vval.v_list == NULL)
4989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004990 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004991 return FAIL;
4992 }
4993
4994 li = item->vval.v_list->lv_first;
4995 for (; li != NULL && n < 16; li = li->li_next, n++)
4996 {
4997 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004998 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004999
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005000 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005001 if (color_name == NULL)
5002 return FAIL;
5003
5004 guicolor = GUI_GET_COLOR(color_name);
5005 if (guicolor == INVALCOLOR)
5006 return FAIL;
5007
5008 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5009 }
5010
5011 if (n != 16 || li != NULL)
5012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005013 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005014 return FAIL;
5015 }
5016
5017 opt->jo_set2 |= JO2_ANSI_COLORS;
5018 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5019 }
5020# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005021#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005022 else if (STRCMP(hi->hi_key, "env") == 0)
5023 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005024 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005025 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005026 if (item->v_type != VAR_DICT)
5027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005028 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005029 return FAIL;
5030 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005031 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005032 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005033 if (opt->jo_env != NULL)
5034 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005035 }
5036 else if (STRCMP(hi->hi_key, "cwd") == 0)
5037 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005038 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005039 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005040 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005041 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005042#ifndef WIN32 // Win32 directories don't have the concept of "executable"
5043 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5044#endif
5045 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005047 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005048 return FAIL;
5049 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005050 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005051 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005052 else if (STRCMP(hi->hi_key, "waittime") == 0)
5053 {
5054 if (!(supported & JO_WAITTIME))
5055 break;
5056 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005057 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005058 }
5059 else if (STRCMP(hi->hi_key, "timeout") == 0)
5060 {
5061 if (!(supported & JO_TIMEOUT))
5062 break;
5063 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005064 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005065 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005066 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005067 {
5068 if (!(supported & JO_OUT_TIMEOUT))
5069 break;
5070 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005071 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005072 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005073 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005074 {
5075 if (!(supported & JO_ERR_TIMEOUT))
5076 break;
5077 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005078 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005079 }
5080 else if (STRCMP(hi->hi_key, "part") == 0)
5081 {
5082 if (!(supported & JO_PART))
5083 break;
5084 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005085 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005086 if (STRCMP(val, "err") == 0)
5087 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005088 else if (STRCMP(val, "out") == 0)
5089 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005090 else
5091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005092 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005093 return FAIL;
5094 }
5095 }
5096 else if (STRCMP(hi->hi_key, "id") == 0)
5097 {
5098 if (!(supported & JO_ID))
5099 break;
5100 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005101 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005102 }
5103 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5104 {
5105 if (!(supported & JO_STOPONEXIT))
5106 break;
5107 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005108 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005109 opt->jo_soe_buf);
5110 if (opt->jo_stoponexit == NULL)
5111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005112 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005113 return FAIL;
5114 }
5115 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005116 else if (STRCMP(hi->hi_key, "block_write") == 0)
5117 {
5118 if (!(supported & JO_BLOCK_WRITE))
5119 break;
5120 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005121 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005122 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005123 else
5124 break;
5125 --todo;
5126 }
5127 if (todo > 0)
5128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005129 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005130 return FAIL;
5131 }
5132
5133 return OK;
5134}
5135
5136/*
5137 * Get the channel from the argument.
5138 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005139 * When "check_open" is TRUE check that the channel can be used.
5140 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005141 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005142 */
5143 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005144get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005145{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005146 channel_T *channel = NULL;
5147 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005148
5149 if (tv->v_type == VAR_JOB)
5150 {
5151 if (tv->vval.v_job != NULL)
5152 channel = tv->vval.v_job->jv_channel;
5153 }
5154 else if (tv->v_type == VAR_CHANNEL)
5155 {
5156 channel = tv->vval.v_channel;
5157 }
5158 else
5159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005160 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005161 return NULL;
5162 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005163 if (channel != NULL && reading)
5164 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005165 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005166
Bram Moolenaar437905c2016-04-26 19:01:05 +02005167 if (check_open && (channel == NULL || (!channel_is_open(channel)
5168 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005170 emsg(_("E906: not an open channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005171 return NULL;
5172 }
5173 return channel;
5174}
5175
5176static job_T *first_job = NULL;
5177
5178 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005179job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005180{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005181 int i;
5182
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005183 ch_log(job->jv_channel, "Freeing job");
5184 if (job->jv_channel != NULL)
5185 {
5186 /* The link from the channel to the job doesn't count as a reference,
5187 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005188 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005189 * NULL the reference. We don't set ch_job_killed, unreferencing the
5190 * job doesn't mean it stops running. */
5191 job->jv_channel->ch_job = NULL;
5192 channel_unref(job->jv_channel);
5193 }
5194 mch_clear_job(job);
5195
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005196 vim_free(job->jv_tty_in);
5197 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005198 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005199#ifdef UNIX
5200 vim_free(job->jv_termsig);
5201#endif
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005202#ifdef WIN3264
5203 vim_free(job->jv_tty_type);
5204#endif
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005205 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005206 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005207 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005208 for (i = 0; job->jv_argv[i] != NULL; i++)
5209 vim_free(job->jv_argv[i]);
5210 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005211 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005212}
5213
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005214/*
5215 * Remove "job" from the list of jobs.
5216 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005217 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005218job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005219{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005220 if (job->jv_next != NULL)
5221 job->jv_next->jv_prev = job->jv_prev;
5222 if (job->jv_prev == NULL)
5223 first_job = job->jv_next;
5224 else
5225 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005226}
5227
5228 static void
5229job_free_job(job_T *job)
5230{
5231 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005232 vim_free(job);
5233}
5234
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005235 static void
5236job_free(job_T *job)
5237{
5238 if (!in_free_unref_items)
5239 {
5240 job_free_contents(job);
5241 job_free_job(job);
5242 }
5243}
5244
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005245job_T *jobs_to_free = NULL;
5246
5247/*
5248 * Put "job" in a list to be freed later, when it's no longer referenced.
5249 */
5250 static void
5251job_free_later(job_T *job)
5252{
5253 job_unlink(job);
5254 job->jv_next = jobs_to_free;
5255 jobs_to_free = job;
5256}
5257
5258 static void
5259free_jobs_to_free_later(void)
5260{
5261 job_T *job;
5262
5263 while (jobs_to_free != NULL)
5264 {
5265 job = jobs_to_free;
5266 jobs_to_free = job->jv_next;
5267 job_free_contents(job);
5268 vim_free(job);
5269 }
5270}
5271
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005272#if defined(EXITFREE) || defined(PROTO)
5273 void
5274job_free_all(void)
5275{
5276 while (first_job != NULL)
5277 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005278 free_jobs_to_free_later();
5279
5280# ifdef FEAT_TERMINAL
5281 free_unused_terminals();
5282# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005283}
5284#endif
5285
5286/*
5287 * Return TRUE if we need to check if the process of "job" has ended.
5288 */
5289 static int
5290job_need_end_check(job_T *job)
5291{
5292 return job->jv_status == JOB_STARTED
5293 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5294}
5295
5296/*
5297 * Return TRUE if the channel of "job" is still useful.
5298 */
5299 static int
5300job_channel_still_useful(job_T *job)
5301{
5302 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5303}
5304
5305/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005306 * Return TRUE if the channel of "job" is closeable.
5307 */
5308 static int
5309job_channel_can_close(job_T *job)
5310{
5311 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5312}
5313
5314/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005315 * Return TRUE if the job should not be freed yet. Do not free the job when
5316 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5317 * or when the associated channel will do something with the job output.
5318 */
5319 static int
5320job_still_useful(job_T *job)
5321{
5322 return job_need_end_check(job) || job_channel_still_useful(job);
5323}
5324
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005325#if defined(GUI_MAY_FORK) || defined(PROTO)
5326/*
5327 * Return TRUE when there is any running job that we care about.
5328 */
5329 int
5330job_any_running()
5331{
5332 job_T *job;
5333
5334 for (job = first_job; job != NULL; job = job->jv_next)
5335 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005336 {
5337 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005338 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005339 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005340 return FALSE;
5341}
5342#endif
5343
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005344#if !defined(USE_ARGV) || defined(PROTO)
5345/*
5346 * Escape one argument for an external command.
5347 * Returns the escaped string in allocated memory. NULL when out of memory.
5348 */
5349 static char_u *
5350win32_escape_arg(char_u *arg)
5351{
5352 int slen, dlen;
5353 int escaping = 0;
5354 int i;
5355 char_u *s, *d;
5356 char_u *escaped_arg;
5357 int has_spaces = FALSE;
5358
5359 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005360 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005361 dlen = slen;
5362 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5363 {
5364 if (*s == '"' || *s == '\\')
5365 ++dlen;
5366 if (*s == ' ' || *s == '\t')
5367 has_spaces = TRUE;
5368 }
5369
5370 if (has_spaces)
5371 dlen += 2;
5372
5373 if (dlen == slen)
5374 return vim_strsave(arg);
5375
5376 /* Allocate memory for the result and fill it. */
5377 escaped_arg = alloc(dlen + 1);
5378 if (escaped_arg == NULL)
5379 return NULL;
5380 memset(escaped_arg, 0, dlen+1);
5381
5382 d = escaped_arg;
5383
5384 if (has_spaces)
5385 *d++ = '"';
5386
5387 for (s = arg; *s != NUL;)
5388 {
5389 switch (*s)
5390 {
5391 case '"':
5392 for (i = 0; i < escaping; i++)
5393 *d++ = '\\';
5394 escaping = 0;
5395 *d++ = '\\';
5396 *d++ = *s++;
5397 break;
5398 case '\\':
5399 escaping++;
5400 *d++ = *s++;
5401 break;
5402 default:
5403 escaping = 0;
5404 MB_COPY_CHAR(s, d);
5405 break;
5406 }
5407 }
5408
5409 /* add terminating quote and finish with a NUL */
5410 if (has_spaces)
5411 {
5412 for (i = 0; i < escaping; i++)
5413 *d++ = '\\';
5414 *d++ = '"';
5415 }
5416 *d = NUL;
5417
5418 return escaped_arg;
5419}
5420
5421/*
5422 * Build a command line from a list, taking care of escaping.
5423 * The result is put in gap->ga_data.
5424 * Returns FAIL when out of memory.
5425 */
5426 int
5427win32_build_cmd(list_T *l, garray_T *gap)
5428{
5429 listitem_T *li;
5430 char_u *s;
5431
5432 for (li = l->lv_first; li != NULL; li = li->li_next)
5433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005434 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005435 if (s == NULL)
5436 return FAIL;
5437 s = win32_escape_arg(s);
5438 if (s == NULL)
5439 return FAIL;
5440 ga_concat(gap, s);
5441 vim_free(s);
5442 if (li->li_next != NULL)
5443 ga_append(gap, ' ');
5444 }
5445 return OK;
5446}
5447#endif
5448
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005449/*
5450 * NOTE: Must call job_cleanup() only once right after the status of "job"
5451 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5452 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005453 * If the job is no longer used it will be removed from the list of jobs, and
5454 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005455 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005456 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005457job_cleanup(job_T *job)
5458{
5459 if (job->jv_status != JOB_ENDED)
5460 return;
5461
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005462 /* Ready to cleanup the job. */
5463 job->jv_status = JOB_FINISHED;
5464
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005465 /* When only channel-in is kept open, close explicitly. */
5466 if (job->jv_channel != NULL)
5467 ch_close_part(job->jv_channel, PART_IN);
5468
Bram Moolenaar97792de2016-10-15 18:36:49 +02005469 if (job->jv_exit_cb != NULL)
5470 {
5471 typval_T argv[3];
5472 typval_T rettv;
5473 int dummy;
5474
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005475 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005476 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005477 ++job->jv_refcount;
5478 argv[0].v_type = VAR_JOB;
5479 argv[0].vval.v_job = job;
5480 argv[1].v_type = VAR_NUMBER;
5481 argv[1].vval.v_number = job->jv_exitval;
5482 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5483 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5484 job->jv_exit_partial, NULL);
5485 clear_tv(&rettv);
5486 --job->jv_refcount;
5487 channel_need_redraw = TRUE;
5488 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005489
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005490 if (job->jv_channel != NULL
5491 && job->jv_channel->ch_anonymous_pipe && !job->jv_channel->ch_killing)
5492 {
5493 ++safe_to_invoke_callback;
5494 channel_free_contents(job->jv_channel);
5495 job->jv_channel->ch_job = NULL;
5496 job->jv_channel = NULL;
5497 --safe_to_invoke_callback;
5498 }
5499
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005500 // Do not free the job in case the close callback of the associated channel
5501 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005502 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005503 // The job was already unreferenced and the associated channel was
5504 // detached, now that it ended it can be freed. However, a caller might
5505 // still use it, thus free it a bit later.
5506 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005507}
5508
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005509/*
5510 * Mark references in jobs that are still useful.
5511 */
5512 int
5513set_ref_in_job(int copyID)
5514{
5515 int abort = FALSE;
5516 job_T *job;
5517 typval_T tv;
5518
5519 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005520 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005521 {
5522 tv.v_type = VAR_JOB;
5523 tv.vval.v_job = job;
5524 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5525 }
5526 return abort;
5527}
5528
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005529/*
5530 * Dereference "job". Note that after this "job" may have been freed.
5531 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005532 void
5533job_unref(job_T *job)
5534{
5535 if (job != NULL && --job->jv_refcount <= 0)
5536 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005537 /* Do not free the job if there is a channel where the close callback
5538 * may get the job info. */
5539 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005540 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005541 /* Do not free the job when it has not ended yet and there is a
5542 * "stoponexit" flag or an exit callback. */
5543 if (!job_need_end_check(job))
5544 {
5545 job_free(job);
5546 }
5547 else if (job->jv_channel != NULL)
5548 {
5549 /* Do remove the link to the channel, otherwise it hangs
5550 * around until Vim exits. See job_free() for refcount. */
5551 ch_log(job->jv_channel, "detaching channel from job");
5552 job->jv_channel->ch_job = NULL;
5553 channel_unref(job->jv_channel);
5554 job->jv_channel = NULL;
5555 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005556 }
5557 }
5558}
5559
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005560 int
5561free_unused_jobs_contents(int copyID, int mask)
5562{
5563 int did_free = FALSE;
5564 job_T *job;
5565
5566 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005567 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005568 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005569 {
5570 /* Free the channel and ordinary items it contains, but don't
5571 * recurse into Lists, Dictionaries etc. */
5572 job_free_contents(job);
5573 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005574 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005575 return did_free;
5576}
5577
5578 void
5579free_unused_jobs(int copyID, int mask)
5580{
5581 job_T *job;
5582 job_T *job_next;
5583
5584 for (job = first_job; job != NULL; job = job_next)
5585 {
5586 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005587 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005588 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005589 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005590 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005591 job_free_job(job);
5592 }
5593 }
5594}
5595
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005596/*
5597 * Allocate a job. Sets the refcount to one and sets options default.
5598 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005599 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005600job_alloc(void)
5601{
5602 job_T *job;
5603
5604 job = (job_T *)alloc_clear(sizeof(job_T));
5605 if (job != NULL)
5606 {
5607 job->jv_refcount = 1;
5608 job->jv_stoponexit = vim_strsave((char_u *)"term");
5609
5610 if (first_job != NULL)
5611 {
5612 first_job->jv_prev = job;
5613 job->jv_next = first_job;
5614 }
5615 first_job = job;
5616 }
5617 return job;
5618}
5619
5620 void
5621job_set_options(job_T *job, jobopt_T *opt)
5622{
5623 if (opt->jo_set & JO_STOPONEXIT)
5624 {
5625 vim_free(job->jv_stoponexit);
5626 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5627 job->jv_stoponexit = NULL;
5628 else
5629 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5630 }
5631 if (opt->jo_set & JO_EXIT_CB)
5632 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005633 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005634 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005635 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005636 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005637 job->jv_exit_partial = NULL;
5638 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005639 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005640 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005641 job->jv_exit_partial = opt->jo_exit_partial;
5642 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005643 {
5644 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005645 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005646 }
5647 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005648 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005649 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005650 func_ref(job->jv_exit_cb);
5651 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005652 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005653 }
5654}
5655
5656/*
5657 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5658 */
5659 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005660job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005661{
5662 job_T *job;
5663
5664 for (job = first_job; job != NULL; job = job->jv_next)
5665 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005666 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005667}
5668
5669/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005670 * Return TRUE when there is any job that has an exit callback and might exit,
5671 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005672 */
5673 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005674has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005675{
5676 job_T *job;
5677
5678 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005679 /* Only should check if the channel has been closed, if the channel is
5680 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005681 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5682 || (job->jv_status == JOB_FINISHED
5683 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005684 return TRUE;
5685 return FALSE;
5686}
5687
Bram Moolenaar97792de2016-10-15 18:36:49 +02005688#define MAX_CHECK_ENDED 8
5689
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005690/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005691 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005692 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005693 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005694 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005695job_check_ended(void)
5696{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005697 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005698 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005699
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005700 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005701 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005702 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005703
Bram Moolenaar97792de2016-10-15 18:36:49 +02005704 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005705 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005706 // NOTE: mch_detect_ended_job() must only return a job of which the
5707 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005708 job_T *job = mch_detect_ended_job(first_job);
5709
5710 if (job == NULL)
5711 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005712 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005713 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005714 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005715
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005716 // Actually free jobs that were cleaned up.
5717 free_jobs_to_free_later();
5718
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005719 if (channel_need_redraw)
5720 {
5721 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005722 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005723 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005724 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005725}
5726
5727/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005728 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005729 * "argv_arg" is only for Unix.
5730 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005731 * The returned job has a refcount of one.
5732 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005733 */
5734 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005735job_start(
5736 typval_T *argvars,
5737 char **argv_arg,
5738 jobopt_T *opt_arg,
5739 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005740{
5741 job_T *job;
5742 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005743 char **argv = NULL;
5744 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005745#if defined(UNIX)
5746# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005747 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005748#else
5749 garray_T ga;
5750#endif
5751 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005752 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005753
5754 job = job_alloc();
5755 if (job == NULL)
5756 return NULL;
5757
5758 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005759#ifndef USE_ARGV
5760 ga_init2(&ga, (int)sizeof(char*), 20);
5761#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005762
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005763 if (opt_arg != NULL)
5764 opt = *opt_arg;
5765 else
5766 {
5767 /* Default mode is NL. */
5768 clear_job_options(&opt);
5769 opt.jo_mode = MODE_NL;
5770 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005771 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5772 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5773 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005774 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005775 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005776
5777 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005778 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005779 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5780 && opt.jo_io[part] == JIO_FILE
5781 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5782 || *opt.jo_io_name[part] == NUL))
5783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005784 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005785 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005786 }
5787
5788 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5789 {
5790 buf_T *buf = NULL;
5791
5792 /* check that we can find the buffer before starting the job */
5793 if (opt.jo_set & JO_IN_BUF)
5794 {
5795 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5796 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005797 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005798 }
5799 else if (!(opt.jo_set & JO_IN_NAME))
5800 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005801 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005802 }
5803 else
5804 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5805 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005806 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005807 if (buf->b_ml.ml_mfp == NULL)
5808 {
5809 char_u numbuf[NUMBUFLEN];
5810 char_u *s;
5811
5812 if (opt.jo_set & JO_IN_BUF)
5813 {
5814 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5815 s = numbuf;
5816 }
5817 else
5818 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005819 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005820 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005821 }
5822 job->jv_in_buf = buf;
5823 }
5824
5825 job_set_options(job, &opt);
5826
Bram Moolenaar13568252018-03-16 20:46:58 +01005827#ifdef USE_ARGV
5828 if (argv_arg != NULL)
5829 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005830 /* Make a copy of argv_arg for job->jv_argv. */
5831 for (i = 0; argv_arg[i] != NULL; i++)
5832 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005833 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005834 if (argv == NULL)
5835 goto theend;
5836 for (i = 0; i < argc; i++)
5837 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5838 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005839 }
5840 else
5841#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005842 if (argvars[0].v_type == VAR_STRING)
5843 {
5844 /* Command is a string. */
5845 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005846 if (cmd == NULL || *cmd == NUL)
5847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005848 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005849 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005850 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005851
5852 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005853 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005854 }
5855 else if (argvars[0].v_type != VAR_LIST
5856 || argvars[0].vval.v_list == NULL
5857 || argvars[0].vval.v_list->lv_len < 1)
5858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005859 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005860 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005861 }
5862 else
5863 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005864 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005865
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005866 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005867 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005868#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005869 if (win32_build_cmd(l, &ga) == FAIL)
5870 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005871 cmd = ga.ga_data;
5872#endif
5873 }
5874
Bram Moolenaar20608922018-04-21 22:30:08 +02005875 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005876 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005877
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005878#ifdef USE_ARGV
5879 if (ch_log_active())
5880 {
5881 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005882
5883 ga_init2(&ga, (int)sizeof(char), 200);
5884 for (i = 0; i < argc; ++i)
5885 {
5886 if (i > 0)
5887 ga_concat(&ga, (char_u *)" ");
5888 ga_concat(&ga, (char_u *)argv[i]);
5889 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005890 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005891 ga_clear(&ga);
5892 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005893 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005894#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005895 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005896 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005897#endif
5898
5899 /* If the channel is reading from a buffer, write lines now. */
5900 if (job->jv_channel != NULL)
5901 channel_write_in(job->jv_channel);
5902
5903theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005904#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005905 vim_free(ga.ga_data);
5906#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005907 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005908 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005909 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005910 return job;
5911}
5912
5913/*
5914 * Get the status of "job" and invoke the exit callback when needed.
5915 * The returned string is not allocated.
5916 */
5917 char *
5918job_status(job_T *job)
5919{
5920 char *result;
5921
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005922 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005923 /* No need to check, dead is dead. */
5924 result = "dead";
5925 else if (job->jv_status == JOB_FAILED)
5926 result = "fail";
5927 else
5928 {
5929 result = mch_job_status(job);
5930 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005931 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005932 }
5933 return result;
5934}
5935
Bram Moolenaar8950a562016-03-12 15:22:55 +01005936/*
5937 * Implementation of job_info().
5938 */
5939 void
5940job_info(job_T *job, dict_T *dict)
5941{
5942 dictitem_T *item;
5943 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005944 list_T *l;
5945 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005946
Bram Moolenaare0be1672018-07-08 16:50:37 +02005947 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005948
5949 item = dictitem_alloc((char_u *)"channel");
5950 if (item == NULL)
5951 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005952 item->di_tv.v_type = VAR_CHANNEL;
5953 item->di_tv.vval.v_channel = job->jv_channel;
5954 if (job->jv_channel != NULL)
5955 ++job->jv_channel->ch_refcount;
5956 if (dict_add(dict, item) == FAIL)
5957 dictitem_free(item);
5958
5959#ifdef UNIX
5960 nr = job->jv_pid;
5961#else
5962 nr = job->jv_proc_info.dwProcessId;
5963#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005964 dict_add_number(dict, "process", nr);
5965 dict_add_string(dict, "tty_in", job->jv_tty_in);
5966 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005967
Bram Moolenaare0be1672018-07-08 16:50:37 +02005968 dict_add_number(dict, "exitval", job->jv_exitval);
5969 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5970 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005971#ifdef UNIX
5972 dict_add_string(dict, "termsig", job->jv_termsig);
5973#endif
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005974#ifdef WIN3264
5975 dict_add_string(dict, "tty_type", job->jv_tty_type);
5976#endif
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005977
5978 l = list_alloc();
5979 if (l != NULL)
5980 {
5981 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005982 if (job->jv_argv != NULL)
5983 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005984 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005985 }
5986}
5987
5988/*
5989 * Implementation of job_info() to return info for all jobs.
5990 */
5991 void
5992job_info_all(list_T *l)
5993{
5994 job_T *job;
5995 typval_T tv;
5996
5997 for (job = first_job; job != NULL; job = job->jv_next)
5998 {
5999 tv.v_type = VAR_JOB;
6000 tv.vval.v_job = job;
6001
6002 if (list_append_tv(l, &tv) != OK)
6003 return;
6004 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01006005}
6006
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006007/*
6008 * Send a signal to "job". Implements job_stop().
6009 * When "type" is not NULL use this for the type.
6010 * Otherwise use argvars[1] for the type.
6011 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006012 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006013job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006014{
6015 char_u *arg;
6016
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006017 if (type != NULL)
6018 arg = (char_u *)type;
6019 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006020 arg = (char_u *)"";
6021 else
6022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006023 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006024 if (arg == NULL)
6025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006026 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006027 return 0;
6028 }
6029 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006030 if (job->jv_status == JOB_FAILED)
6031 {
6032 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6033 return 0;
6034 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006035 if (job->jv_status == JOB_ENDED)
6036 {
6037 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6038 return 0;
6039 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006040 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006041 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006042 return 0;
6043
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006044 /* Assume that only "kill" will kill the job. */
6045 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006046 job->jv_channel->ch_job_killed = TRUE;
6047
6048 /* We don't try freeing the job, obviously the caller still has a
6049 * reference to it. */
6050 return 1;
6051}
6052
Bram Moolenaarf2732452018-06-03 14:47:35 +02006053 void
6054invoke_prompt_callback(void)
6055{
6056 typval_T rettv;
6057 int dummy;
6058 typval_T argv[2];
6059 char_u *text;
6060 char_u *prompt;
6061 linenr_T lnum = curbuf->b_ml.ml_line_count;
6062
6063 // Add a new line for the prompt before invoking the callback, so that
6064 // text can always be inserted above the last line.
6065 ml_append(lnum, (char_u *)"", 0, FALSE);
6066 curwin->w_cursor.lnum = lnum + 1;
6067 curwin->w_cursor.col = 0;
6068
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006069 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006070 return;
6071 text = ml_get(lnum);
6072 prompt = prompt_text();
6073 if (STRLEN(text) >= STRLEN(prompt))
6074 text += STRLEN(prompt);
6075 argv[0].v_type = VAR_STRING;
6076 argv[0].vval.v_string = vim_strsave(text);
6077 argv[1].v_type = VAR_UNKNOWN;
6078
6079 call_func(curbuf->b_prompt_callback,
6080 (int)STRLEN(curbuf->b_prompt_callback),
6081 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
6082 curbuf->b_prompt_partial, NULL);
6083 clear_tv(&argv[0]);
6084 clear_tv(&rettv);
6085}
6086
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006087/*
6088 * Return TRUE when the interrupt callback was invoked.
6089 */
6090 int
6091invoke_prompt_interrupt(void)
6092{
6093 typval_T rettv;
6094 int dummy;
6095 typval_T argv[1];
6096
6097 if (curbuf->b_prompt_interrupt == NULL
6098 || *curbuf->b_prompt_interrupt == NUL)
6099 return FALSE;
6100 argv[0].v_type = VAR_UNKNOWN;
6101
6102 got_int = FALSE; // don't skip executing commands
6103 call_func(curbuf->b_prompt_interrupt,
6104 (int)STRLEN(curbuf->b_prompt_interrupt),
6105 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
6106 curbuf->b_prompt_int_partial, NULL);
6107 clear_tv(&rettv);
6108 return TRUE;
6109}
6110
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006111#endif /* FEAT_JOB_CHANNEL */