blob: 20cf46238c0771873923f5f6d0bff4ed27bf3dbe [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaard04a0202016-01-26 23:30:18 +010023#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
Bram Moolenaar24058382019-01-24 23:11:49 +010083 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010084 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010085 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010086 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010087
Bram Moolenaar24058382019-01-24 23:11:49 +010088 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 {
Bram Moolenaar24058382019-01-24 23:11:49 +010090 if (todo > MAX_NAMED_PIPE_SIZE)
91 size = MAX_NAMED_PIPE_SIZE;
92 else
Bram Moolenaardec01202019-01-28 20:04:24 +010093 size = (DWORD)todo;
Bram Moolenaar24058382019-01-24 23:11:49 +010094 // If the pipe overflows while the job does not read the data, WriteFile
95 // will block forever. This abandons the write.
96 memset(&ov, 0, sizeof(ov));
97 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
98 {
99 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100100
Bram Moolenaar24058382019-01-24 23:11:49 +0100101 if (err != ERROR_IO_PENDING)
102 return -1;
103 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
104 return -1;
105 FlushFileBuffers(h);
106 }
107 todo -= nwrite;
108 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100109 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100110 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100111}
112
113 static void
114fd_close(sock_T fd)
115{
116 HANDLE h = (HANDLE)fd;
117
118 CloseHandle(h);
119}
120#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100121
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100122/* Log file opened with ch_logfile(). */
123static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100124#ifdef FEAT_RELTIME
125static proftime_T log_start;
126#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127
128 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100129ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100130{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100131 FILE *file = NULL;
132
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100133 if (log_fd != NULL)
134 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100135
136 if (*fname != NUL)
137 {
138 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
139 if (file == NULL)
140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100141 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100142 return;
143 }
144 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100145 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100146
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100147 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100148 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100149 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100150#ifdef FEAT_RELTIME
151 profile_start(&log_start);
152#endif
153 }
154}
155
156 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200157ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100158{
159 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100160}
161
162 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200163ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164{
165 if (log_fd != NULL)
166 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100167#ifdef FEAT_RELTIME
168 proftime_T log_now;
169
170 profile_start(&log_now);
171 profile_sub(&log_now, &log_start);
172 fprintf(log_fd, "%s ", profile_msg(&log_now));
173#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100174 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200175 {
176 if (part < PART_COUNT)
177 fprintf(log_fd, "%son %d(%s): ",
178 what, ch->ch_id, part_names[part]);
179 else
180 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
181 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 else
183 fprintf(log_fd, "%s: ", what);
184 }
185}
186
Bram Moolenaard0b65022016-03-06 21:50:33 +0100187static int did_log_msg = TRUE;
188
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200189#ifndef PROTO /* prototype is in vim.h */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100190 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200191ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192{
193 if (log_fd != NULL)
194 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200195 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100196
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200197 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200198 va_start(ap, fmt);
199 vfprintf(log_fd, fmt, ap);
200 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100201 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100203 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100204 }
205}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200206#endif
207
208 static void
209ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200210#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
211 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200212#endif
213 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214
215 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200216ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100217{
218 if (log_fd != NULL)
219 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200220 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200222 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200223 va_start(ap, fmt);
224 vfprintf(log_fd, fmt, ap);
225 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100226 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100228 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 }
230}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100231
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100232#ifdef _WIN32
233# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100234# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100235
236 static char *
237strerror_win32(int eno)
238{
239 static LPVOID msgbuf = NULL;
240 char_u *ptr;
241
242 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200243 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100244 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200245 msgbuf = NULL;
246 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100247 FormatMessage(
248 FORMAT_MESSAGE_ALLOCATE_BUFFER |
249 FORMAT_MESSAGE_FROM_SYSTEM |
250 FORMAT_MESSAGE_IGNORE_INSERTS,
251 NULL,
252 eno,
253 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
254 (LPTSTR) &msgbuf,
255 0,
256 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200257 if (msgbuf != NULL)
258 /* chomp \r or \n */
259 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
260 switch (*ptr)
261 {
262 case '\r':
263 STRMOVE(ptr, ptr + 1);
264 ptr--;
265 break;
266 case '\n':
267 if (*(ptr + 1) == '\0')
268 *ptr = '\0';
269 else
270 *ptr = ' ';
271 break;
272 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100273 return msgbuf;
274}
275#endif
276
Bram Moolenaar77073442016-02-13 23:23:53 +0100277/*
278 * The list of all allocated channels.
279 */
280static channel_T *first_channel = NULL;
281static int next_ch_id = 0;
282
283/*
284 * Allocate a new channel. The refcount is set to 1.
285 * The channel isn't actually used until it is opened.
286 * Returns NULL if out of memory.
287 */
288 channel_T *
289add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100290{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200291 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100292 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100293
Bram Moolenaar77073442016-02-13 23:23:53 +0100294 if (channel == NULL)
295 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100296
Bram Moolenaar77073442016-02-13 23:23:53 +0100297 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100298 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100299
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200300 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100301 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100302 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100303#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100304 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100305#endif
306#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100307 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100309 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100310 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100311
Bram Moolenaar77073442016-02-13 23:23:53 +0100312 if (first_channel != NULL)
313 {
314 first_channel->ch_prev = channel;
315 channel->ch_next = first_channel;
316 }
317 first_channel = channel;
318
319 channel->ch_refcount = 1;
320 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100321}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100322
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200323 int
324has_any_channel(void)
325{
326 return first_channel != NULL;
327}
328
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100329/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100330 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100331 * Return TRUE if "channel" has a callback and the associated job wasn't
332 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100333 */
334 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100335channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100336{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100337 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100338 int has_out_msg;
339 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100340
341 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100342 if (channel->ch_job_killed && channel->ch_job == NULL)
343 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344
345 /* If there is a close callback it may still need to be invoked. */
346 if (channel->ch_close_cb != NULL)
347 return TRUE;
348
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200349 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200350 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200351 return TRUE;
352
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100353 /* If there is no callback then nobody can get readahead. If the fd is
354 * closed and there is no readahead then the callback won't be called. */
355 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100356 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
357 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
359 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
360 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
361 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
362 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
363 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100365 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200366 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200367 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
368 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200369 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200370 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
371 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100372}
373
374/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200375 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
376 */
377 static int
378channel_can_close(channel_T *channel)
379{
380 return channel->ch_to_be_closed == 0;
381}
382
383/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200384 * Close a channel and free all its resources.
385 */
386 static void
387channel_free_contents(channel_T *channel)
388{
389 channel_close(channel, TRUE);
390 channel_clear(channel);
391 ch_log(channel, "Freeing channel");
392}
393
394 static void
395channel_free_channel(channel_T *channel)
396{
397 if (channel->ch_next != NULL)
398 channel->ch_next->ch_prev = channel->ch_prev;
399 if (channel->ch_prev == NULL)
400 first_channel = channel->ch_next;
401 else
402 channel->ch_prev->ch_next = channel->ch_next;
403 vim_free(channel);
404}
405
406 static void
407channel_free(channel_T *channel)
408{
409 if (!in_free_unref_items)
410 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200411 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200412 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200413 else
414 {
415 channel_free_contents(channel);
416 channel_free_channel(channel);
417 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200418 }
419}
420
421/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100422 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100423 * possible, there is no callback to be invoked or the associated job was
424 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100425 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100426 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100427 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100428channel_may_free(channel_T *channel)
429{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100430 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100431 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100432 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 return TRUE;
434 }
435 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436}
437
438/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100439 * Decrement the reference count on "channel" and maybe free it when it goes
440 * down to zero. Don't free it if there is a pending action.
441 * Returns TRUE when the channel is no longer referenced.
442 */
443 int
444channel_unref(channel_T *channel)
445{
446 if (channel != NULL && --channel->ch_refcount <= 0)
447 return channel_may_free(channel);
448 return FALSE;
449}
450
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200451 int
452free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100453{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200454 int did_free = FALSE;
455 channel_T *ch;
456
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200457 /* This is invoked from the garbage collector, which only runs at a safe
458 * point. */
459 ++safe_to_invoke_callback;
460
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200461 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200462 if (!channel_still_useful(ch)
463 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 {
465 /* Free the channel and ordinary items it contains, but don't
466 * recurse into Lists, Dictionaries etc. */
467 channel_free_contents(ch);
468 did_free = TRUE;
469 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200470
471 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200472 return did_free;
473}
474
475 void
476free_unused_channels(int copyID, int mask)
477{
478 channel_T *ch;
479 channel_T *ch_next;
480
481 for (ch = first_channel; ch != NULL; ch = ch_next)
482 {
483 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200484 if (!channel_still_useful(ch)
485 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200486 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200487 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200488 channel_free_channel(ch);
489 }
490 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100491}
492
Bram Moolenaard04a0202016-01-26 23:30:18 +0100493#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100494
495#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
496 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100497channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100498{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100499 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200500 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100501
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100502 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100503 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200504 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100505 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200506 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100507}
508#endif
509
Bram Moolenaare0874f82016-01-24 20:36:41 +0100510/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100511 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513#ifdef FEAT_GUI_X11
514 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200515messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200516 int *unused1 UNUSED,
517 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100519 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100521#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100524# if GTK_CHECK_VERSION(3,0,0)
525 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200526messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527 GIOCondition unused2 UNUSED,
528 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100529{
530 channel_read_fd(GPOINTER_TO_INT(clientData));
531 return TRUE; /* Return FALSE instead in case the event source is to
532 * be removed after this function returns. */
533}
534# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200536messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200537 gint unused1 UNUSED,
538 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100539{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100540 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100541}
Bram Moolenaar98921892016-02-23 17:14:37 +0100542# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100543#endif
544
545 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200546channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100547{
Bram Moolenaarde279892016-03-11 22:19:44 +0100548 if (!CH_HAS_GUI)
549 return;
550
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200551 /* gets stuck in handling events for a not connected channel */
552 if (channel->ch_keep_open)
553 return;
554
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100555# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200556 /* Tell notifier we are interested in being called when there is input on
557 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100558 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200559 {
560 ch_log(channel, "Registering part %s with fd %d",
561 part_names[part], channel->ch_part[part].ch_fd);
562
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100563 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100564 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100565 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100566 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200567 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100568 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200569 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100570# else
571# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200572 /* Tell gdk we are interested in being called when there is input on the
573 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100574 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100575 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200576 ch_log(channel, "Registering part %s with fd %d",
577 part_names[part], channel->ch_part[part].ch_fd);
578# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100579 GIOChannel *chnnl = g_io_channel_unix_new(
580 (gint)channel->ch_part[part].ch_fd);
581
582 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
583 chnnl,
584 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200585 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100586 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
587
588 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100589# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100590 channel->ch_part[part].ch_inputHandler = gdk_input_add(
591 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100592 (GdkInputCondition)
593 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200594 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100595 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100596# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200597 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100598# endif
599# endif
600}
601
Bram Moolenaarde279892016-03-11 22:19:44 +0100602 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100603channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100604{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100605 if (channel->CH_SOCK_FD != INVALID_FD)
606 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200607 if (channel->CH_OUT_FD != INVALID_FD
608 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100609 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200610 if (channel->CH_ERR_FD != INVALID_FD
611 && channel->CH_ERR_FD != channel->CH_SOCK_FD
612 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100613 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100614}
615
616/*
617 * Register any of our file descriptors with the GUI event handling system.
618 * Called when the GUI has started.
619 */
620 void
621channel_gui_register_all(void)
622{
Bram Moolenaar77073442016-02-13 23:23:53 +0100623 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100624
Bram Moolenaar77073442016-02-13 23:23:53 +0100625 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100626 channel_gui_register(channel);
627}
628
629 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200630channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100631{
632# ifdef FEAT_GUI_X11
633 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
634 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200635 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100636 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
637 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
638 }
639# else
640# ifdef FEAT_GUI_GTK
641 if (channel->ch_part[part].ch_inputHandler != 0)
642 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200643 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100644# if GTK_CHECK_VERSION(3,0,0)
645 g_source_remove(channel->ch_part[part].ch_inputHandler);
646# else
647 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
648# endif
649 channel->ch_part[part].ch_inputHandler = 0;
650 }
651# endif
652# endif
653}
654
655 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100656channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100657{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200658 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100659
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100660 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100661 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662}
663
664#endif
665
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100666static char *e_cannot_connect = N_("E902: Cannot connect to port");
667
Bram Moolenaard04a0202016-01-26 23:30:18 +0100668/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100669 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100670 * "waittime" is the time in msec to wait for the connection.
671 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100672 * Returns the channel for success.
673 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100675 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100676channel_open(
677 char *hostname,
678 int port_in,
679 int waittime,
680 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100683 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100685#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100687 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100688#else
689 int port = port_in;
690#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100691 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100692 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100694#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100695 channel_init_winsock();
696#endif
697
Bram Moolenaar77073442016-02-13 23:23:53 +0100698 channel = add_channel();
699 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100700 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100701 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100702 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100703 }
704
705 /* Get the server internet address and put into addr structure */
706 /* fill in the socket address structure and connect to server */
707 vim_memset((char *)&server, 0, sizeof(server));
708 server.sin_family = AF_INET;
709 server.sin_port = htons(port);
710 if ((host = gethostbyname(hostname)) == NULL)
711 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100712 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200713 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100714 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100715 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100716 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100717 {
718 char *p;
719
Bram Moolenaar2e324952018-04-14 14:37:07 +0200720 /* When using host->h_addr_list[0] directly ubsan warns for it to not
721 * be aligned. First copy the pointer to avoid that. */
722 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100723 memcpy((char *)&server.sin_addr, p, host->h_length);
724 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100725
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100726 /* On Mac and Solaris a zero timeout almost never works. At least wait
727 * one millisecond. Let's do it for all systems, because we don't know why
728 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100729 if (waittime == 0)
730 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731
732 /*
733 * For Unix we need to call connect() again after connect() failed.
734 * On Win32 one time is sufficient.
735 */
736 while (TRUE)
737 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100738 long elapsed_msec = 0;
739 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100740
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100741 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100742 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100743 sd = socket(AF_INET, SOCK_STREAM, 0);
744 if (sd == -1)
745 {
746 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200747 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100748 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100749 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100750 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100751
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100752 if (waittime >= 0)
753 {
754 /* Make connect() non-blocking. */
755 if (
756#ifdef _WIN32
757 ioctlsocket(sd, FIONBIO, &val) < 0
758#else
759 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
760#endif
761 )
762 {
763 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200764 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100765 "channel_open: Connect failed with errno %d", errno);
766 sock_close(sd);
767 channel_free(channel);
768 return NULL;
769 }
770 }
771
772 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200773 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100774 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
775
Bram Moolenaar045a2842016-03-08 22:33:07 +0100776 if (ret == 0)
777 /* The connection could be established. */
778 break;
779
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100780 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781 if (waittime < 0 || (errno != EWOULDBLOCK
782 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100783#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100784 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100785#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100786 ))
787 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200788 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 "channel_open: Connect failed with errno %d", errno);
790 PERROR(_(e_cannot_connect));
791 sock_close(sd);
792 channel_free(channel);
793 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100794 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100795
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100796 /* Limit the waittime to 50 msec. If it doesn't work within this
797 * time we close the socket and try creating it again. */
798 waitnow = waittime > 50 ? 50 : waittime;
799
Bram Moolenaar045a2842016-03-08 22:33:07 +0100800 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100801 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100802#ifndef WIN32
803 if (errno != ECONNREFUSED)
804#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100805 {
806 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100807 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100809#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100810 int so_error = 0;
811 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100812 struct timeval start_tv;
813 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100814#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100815 FD_ZERO(&rfds);
816 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100817 FD_ZERO(&wfds);
818 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100819
Bram Moolenaar562ca712016-03-09 21:50:05 +0100820 tv.tv_sec = waitnow / 1000;
821 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100822#ifndef WIN32
823 gettimeofday(&start_tv, NULL);
824#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200825 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100826 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100827 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100828
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100829 if (ret < 0)
830 {
831 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200832 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100833 "channel_open: Connect failed with errno %d", errno);
834 PERROR(_(e_cannot_connect));
835 sock_close(sd);
836 channel_free(channel);
837 return NULL;
838 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100839
Bram Moolenaare081e212016-02-28 22:33:46 +0100840#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100841 /* On Win32: select() is expected to work and wait for up to
842 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100843 if (FD_ISSET(sd, &wfds))
844 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100845 elapsed_msec = waitnow;
846 if (waittime > 1 && elapsed_msec < waittime)
847 {
848 waittime -= elapsed_msec;
849 continue;
850 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100851#else
852 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100853 * After putting the socket in non-blocking mode, connect() will
854 * return EINPROGRESS, select() will not wait (as if writing is
855 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100856 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100857 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100858 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100859 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100860 {
861 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100862 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 if (ret < 0 || (so_error != 0
864 && so_error != EWOULDBLOCK
865 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100866# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100867 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100868# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100869 ))
870 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200871 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100872 "channel_open: Connect failed with errno %d",
873 so_error);
874 PERROR(_(e_cannot_connect));
875 sock_close(sd);
876 channel_free(channel);
877 return NULL;
878 }
879 }
880
Bram Moolenaar045a2842016-03-08 22:33:07 +0100881 if (FD_ISSET(sd, &wfds) && so_error == 0)
882 /* Did not detect an error, connection is established. */
883 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100884
Bram Moolenaar045a2842016-03-08 22:33:07 +0100885 gettimeofday(&end_tv, NULL);
886 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
887 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100888#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100889 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100890
891#ifndef WIN32
892 if (waittime > 1 && elapsed_msec < waittime)
893 {
894 /* The port isn't ready but we also didn't get an error.
895 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100896 * yet. Select() may return early, wait until the remaining
897 * "waitnow" and try again. */
898 waitnow -= elapsed_msec;
899 waittime -= elapsed_msec;
900 if (waitnow > 0)
901 {
902 mch_delay((long)waitnow, TRUE);
903 ui_breakcheck();
904 waittime -= waitnow;
905 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100906 if (!got_int)
907 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100908 if (waittime <= 0)
909 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100910 waittime = 1;
911 continue;
912 }
913 /* we were interrupted, behave as if timed out */
914 }
915#endif
916
917 /* We timed out. */
918 ch_error(channel, "Connection timed out");
919 sock_close(sd);
920 channel_free(channel);
921 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100922 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100923
Bram Moolenaar045a2842016-03-08 22:33:07 +0100924 ch_log(channel, "Connection made");
925
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100926 if (waittime >= 0)
927 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100928#ifdef _WIN32
929 val = 0;
930 ioctlsocket(sd, FIONBIO, &val);
931#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100932 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100933#endif
934 }
935
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100936 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100937 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100938 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
939 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200940 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100941
942#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100943 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100944#endif
945
Bram Moolenaar77073442016-02-13 23:23:53 +0100946 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100947}
948
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100949/*
950 * Implements ch_open().
951 */
952 channel_T *
953channel_open_func(typval_T *argvars)
954{
955 char_u *address;
956 char_u *p;
957 char *rest;
958 int port;
959 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200960 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100961
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100962 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100963 if (argvars[1].v_type != VAR_UNKNOWN
964 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100966 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100967 return NULL;
968 }
969
970 /* parse address */
971 p = vim_strchr(address, ':');
972 if (p == NULL)
973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100974 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100975 return NULL;
976 }
977 *p++ = NUL;
978 port = strtol((char *)p, &rest, 10);
979 if (*address == NUL || port <= 0 || *rest != NUL)
980 {
981 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100982 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100983 return NULL;
984 }
985
986 /* parse options */
987 clear_job_options(&opt);
988 opt.jo_mode = MODE_JSON;
989 opt.jo_timeout = 2000;
990 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200991 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200992 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100993 if (opt.jo_timeout < 0)
994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100995 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200996 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100997 }
998
999 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1000 if (channel != NULL)
1001 {
1002 opt.jo_set = JO_ALL;
1003 channel_set_options(channel, &opt);
1004 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001005theend:
1006 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001007 return channel;
1008}
1009
Bram Moolenaarde279892016-03-11 22:19:44 +01001010 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001011ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001012{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001013 sock_T *fd = &channel->ch_part[part].ch_fd;
1014
Bram Moolenaarde279892016-03-11 22:19:44 +01001015 if (*fd != INVALID_FD)
1016 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001017 if (part == PART_SOCK)
1018 sock_close(*fd);
1019 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001020 {
1021 /* When using a pty the same FD is set on multiple parts, only
1022 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001023 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1024 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1025 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001026 {
1027#ifdef WIN32
1028 if (channel->ch_named_pipe)
1029 DisconnectNamedPipe((HANDLE)fd);
1030#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001031 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001032 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001033 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001034 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001035
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001036 /* channel is closed, may want to end the job if it was the last */
1037 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001038 }
1039}
1040
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001041 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001042channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001043{
Bram Moolenaarde279892016-03-11 22:19:44 +01001044 if (in != INVALID_FD)
1045 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001046 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001047 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001048# if defined(UNIX)
1049 /* Do not end the job when all output channels are closed, wait until
1050 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001051 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001052 channel->ch_to_be_closed |= (1U << PART_IN);
1053# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001054 }
1055 if (out != INVALID_FD)
1056 {
1057# if defined(FEAT_GUI)
1058 channel_gui_unregister_one(channel, PART_OUT);
1059# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001060 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001061 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001062 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001063# if defined(FEAT_GUI)
1064 channel_gui_register_one(channel, PART_OUT);
1065# endif
1066 }
1067 if (err != INVALID_FD)
1068 {
1069# if defined(FEAT_GUI)
1070 channel_gui_unregister_one(channel, PART_ERR);
1071# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001072 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001073 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001074 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001075# if defined(FEAT_GUI)
1076 channel_gui_register_one(channel, PART_ERR);
1077# endif
1078 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001079}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001080
Bram Moolenaard6051b52016-02-28 15:49:03 +01001081/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001082 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001083 * This does not keep a refcount, when the job is freed ch_job is cleared.
1084 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001085 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001086channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001087{
Bram Moolenaar77073442016-02-13 23:23:53 +01001088 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001089
1090 channel_set_options(channel, options);
1091
1092 if (job->jv_in_buf != NULL)
1093 {
1094 chanpart_T *in_part = &channel->ch_part[PART_IN];
1095
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001096 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001097 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001098 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001099 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001100 {
1101 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1102 {
1103 /* Special mode: send last-but-one line when appending a line
1104 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001105 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001106 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001107 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001108 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001109 }
1110 else
1111 in_part->ch_buf_top = options->jo_in_top;
1112 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001113 else
1114 in_part->ch_buf_top = 1;
1115 if (options->jo_set & JO_IN_BOT)
1116 in_part->ch_buf_bot = options->jo_in_bot;
1117 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001118 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001119 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001120}
1121
1122/*
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001123 * Prepare buffer "buf" for writing channel output to.
1124 */
1125 static void
1126prepare_buffer(buf_T *buf)
1127{
1128 buf_T *save_curbuf = curbuf;
1129
1130 buf_copy_options(buf, BCO_ENTER);
1131 curbuf = buf;
1132#ifdef FEAT_QUICKFIX
1133 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1134 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1135#endif
1136 if (curbuf->b_ml.ml_mfp == NULL)
1137 ml_open(curbuf);
1138 curbuf = save_curbuf;
1139}
1140
1141/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001142 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001143 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001144 */
1145 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001146find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001147{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001148 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001149 buf_T *save_curbuf = curbuf;
1150
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001151 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001152 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001153 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001154 if (buf == NULL)
1155 buf = buflist_findname_exp(name);
1156 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001157 if (buf == NULL)
1158 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001159 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001160 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001161 if (buf == NULL)
1162 return NULL;
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001163 prepare_buffer(buf);
1164
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001165 curbuf = buf;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001166 if (msg)
1167 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001168 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001169 changed_bytes(1, 0);
1170 curbuf = save_curbuf;
1171 }
1172
1173 return buf;
1174}
1175
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001176 static void
1177set_callback(
1178 char_u **cbp,
1179 partial_T **pp,
1180 char_u *callback,
1181 partial_T *partial)
1182{
1183 free_callback(*cbp, *pp);
1184 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001185 {
1186 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001187 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001188 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001189 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001190 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001191 func_ref(*cbp);
1192 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001193 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001194 else
1195 *cbp = NULL;
1196 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001197 if (partial != NULL)
1198 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001199}
1200
Bram Moolenaar187db502016-02-27 14:44:26 +01001201/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001202 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001203 */
1204 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001205channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001206{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001207 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001208
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001209 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001210 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001211 channel->ch_part[part].ch_mode = opt->jo_mode;
1212 if (opt->jo_set & JO_IN_MODE)
1213 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1214 if (opt->jo_set & JO_OUT_MODE)
1215 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1216 if (opt->jo_set & JO_ERR_MODE)
1217 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001218 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001219
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001220 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001221 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001222 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1223 if (opt->jo_set & JO_OUT_TIMEOUT)
1224 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1225 if (opt->jo_set & JO_ERR_TIMEOUT)
1226 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001227 if (opt->jo_set & JO_BLOCK_WRITE)
1228 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001229
1230 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001231 set_callback(&channel->ch_callback, &channel->ch_partial,
1232 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001233 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001234 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1235 &channel->ch_part[PART_OUT].ch_partial,
1236 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001237 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001238 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1239 &channel->ch_part[PART_ERR].ch_partial,
1240 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001241 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001242 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1243 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001244 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001245
1246 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1247 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 buf_T *buf;
1249
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001250 /* writing output to a buffer. Default mode is NL. */
1251 if (!(opt->jo_set & JO_OUT_MODE))
1252 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001253 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001254 {
1255 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1256 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001257 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001258 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001259 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001260 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001261 int msg = TRUE;
1262
1263 if (opt->jo_set2 & JO2_OUT_MSG)
1264 msg = opt->jo_message[PART_OUT];
1265 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001266 }
1267 if (buf != NULL)
1268 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001269 if (opt->jo_set & JO_OUT_MODIFIABLE)
1270 channel->ch_part[PART_OUT].ch_nomodifiable =
1271 !opt->jo_modifiable[PART_OUT];
1272
1273 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001275 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001276 }
1277 else
1278 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001279 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001280 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001281 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001282 // if the buffer was deleted or unloaded resurrect it
1283 if (buf->b_ml.ml_mfp == NULL)
1284 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001285 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001286 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001287 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001288
1289 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1290 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1291 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1292 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001293 buf_T *buf;
1294
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001295 /* writing err to a buffer. Default mode is NL. */
1296 if (!(opt->jo_set & JO_ERR_MODE))
1297 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1298 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001299 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001300 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001301 {
1302 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1303 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001304 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001305 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001306 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001307 {
1308 int msg = TRUE;
1309
1310 if (opt->jo_set2 & JO2_ERR_MSG)
1311 msg = opt->jo_message[PART_ERR];
1312 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1313 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001314 if (buf != NULL)
1315 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001316 if (opt->jo_set & JO_ERR_MODIFIABLE)
1317 channel->ch_part[PART_ERR].ch_nomodifiable =
1318 !opt->jo_modifiable[PART_ERR];
1319 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001322 }
1323 else
1324 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001325 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001326 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001327 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001328 // if the buffer was deleted or unloaded resurrect it
1329 if (buf->b_ml.ml_mfp == NULL)
1330 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001331 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001332 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001333 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001334
1335 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1336 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1337 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001338}
1339
1340/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001341 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001342 */
1343 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001344channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001345 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001346 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001347 char_u *callback,
1348 partial_T *partial,
1349 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001350{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001351 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001352 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1353
1354 if (item != NULL)
1355 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001356 item->cq_partial = partial;
1357 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001358 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001359 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001360 item->cq_callback = callback;
1361 }
1362 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001363 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001364 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001365 func_ref(item->cq_callback);
1366 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001367 item->cq_seq_nr = id;
1368 item->cq_prev = head->cq_prev;
1369 head->cq_prev = item;
1370 item->cq_next = NULL;
1371 if (item->cq_prev == NULL)
1372 head->cq_next = item;
1373 else
1374 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001375 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001376}
1377
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001378 static void
1379write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1380{
1381 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001382 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001383 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001384 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001385
Bram Moolenaar655da312016-05-28 22:22:34 +02001386 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001387 if ((p = alloc(len + 2)) == NULL)
1388 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001389 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001390
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001391 if (channel->ch_write_text_mode)
1392 p[len] = CAR;
1393 else
1394 {
1395 for (i = 0; i < len; ++i)
1396 if (p[i] == NL)
1397 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001398
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001399 p[len] = NL;
1400 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001401 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001402 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001403 vim_free(p);
1404}
1405
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001406/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001407 * Return TRUE if "channel" can be written to.
1408 * Returns FALSE if the input is closed or the write would block.
1409 */
1410 static int
1411can_write_buf_line(channel_T *channel)
1412{
1413 chanpart_T *in_part = &channel->ch_part[PART_IN];
1414
1415 if (in_part->ch_fd == INVALID_FD)
1416 return FALSE; /* pipe was closed */
1417
1418 /* for testing: block every other attempt to write */
1419 if (in_part->ch_block_write == 1)
1420 in_part->ch_block_write = -1;
1421 else if (in_part->ch_block_write == -1)
1422 in_part->ch_block_write = 1;
1423
1424 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1425#ifndef WIN32
1426 {
1427# if defined(HAVE_SELECT)
1428 struct timeval tval;
1429 fd_set wfds;
1430 int ret;
1431
1432 FD_ZERO(&wfds);
1433 FD_SET((int)in_part->ch_fd, &wfds);
1434 tval.tv_sec = 0;
1435 tval.tv_usec = 0;
1436 for (;;)
1437 {
1438 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1439# ifdef EINTR
1440 SOCK_ERRNO;
1441 if (ret == -1 && errno == EINTR)
1442 continue;
1443# endif
1444 if (ret <= 0 || in_part->ch_block_write == 1)
1445 {
1446 if (ret > 0)
1447 ch_log(channel, "FAKED Input not ready for writing");
1448 else
1449 ch_log(channel, "Input not ready for writing");
1450 return FALSE;
1451 }
1452 break;
1453 }
1454# else
1455 struct pollfd fds;
1456
1457 fds.fd = in_part->ch_fd;
1458 fds.events = POLLOUT;
1459 if (poll(&fds, 1, 0) <= 0)
1460 {
1461 ch_log(channel, "Input not ready for writing");
1462 return FALSE;
1463 }
1464 if (in_part->ch_block_write == 1)
1465 {
1466 ch_log(channel, "FAKED Input not ready for writing");
1467 return FALSE;
1468 }
1469# endif
1470 }
1471#endif
1472 return TRUE;
1473}
1474
1475/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001476 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001477 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001478 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001479channel_write_in(channel_T *channel)
1480{
1481 chanpart_T *in_part = &channel->ch_part[PART_IN];
1482 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001483 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001484 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001485
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001486 if (buf == NULL || in_part->ch_buf_append)
1487 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001488 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001489 {
1490 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001491 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001492 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001493 return;
1494 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001495
1496 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1497 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1498 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001499 if (!can_write_buf_line(channel))
1500 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001501 write_buf_line(buf, lnum, channel);
1502 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001503 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504
1505 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001506 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001507 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001508 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001509
Bram Moolenaar014069a2016-03-03 22:51:40 +01001510 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001511 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001512 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001513#if defined(FEAT_TERMINAL)
1514 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001515 if (channel->ch_job != NULL)
1516 term_send_eof(channel);
1517#endif
1518
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001519 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001520 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001521 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001522
1523 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001524 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001525 }
1526 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001527 ch_log(channel, "Still %ld more lines to write",
1528 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001529}
1530
1531/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001532 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001533 */
1534 void
1535channel_buffer_free(buf_T *buf)
1536{
1537 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001538 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001539
1540 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001541 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001542 {
1543 chanpart_T *ch_part = &channel->ch_part[part];
1544
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001545 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001546 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001547 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001548 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001549 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001550 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001551 }
1552}
1553
1554/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001555 * Write any lines waiting to be written to "channel".
1556 */
1557 static void
1558channel_write_input(channel_T *channel)
1559{
1560 chanpart_T *in_part = &channel->ch_part[PART_IN];
1561
1562 if (in_part->ch_writeque.wq_next != NULL)
1563 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1564 else if (in_part->ch_bufref.br_buf != NULL)
1565 {
1566 if (in_part->ch_buf_append)
1567 channel_write_new_lines(in_part->ch_bufref.br_buf);
1568 else
1569 channel_write_in(channel);
1570 }
1571}
1572
1573/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001574 * Write any lines waiting to be written to a channel.
1575 */
1576 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001577channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001578{
1579 channel_T *channel;
1580
1581 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001582 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001583}
1584
1585/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001586 * Write appended lines above the last one in "buf" to the channel.
1587 */
1588 void
1589channel_write_new_lines(buf_T *buf)
1590{
1591 channel_T *channel;
1592 int found_one = FALSE;
1593
1594 /* There could be more than one channel for the buffer, loop over all of
1595 * them. */
1596 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1597 {
1598 chanpart_T *in_part = &channel->ch_part[PART_IN];
1599 linenr_T lnum;
1600 int written = 0;
1601
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001602 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001603 {
1604 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001605 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001606 found_one = TRUE;
1607 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1608 ++lnum)
1609 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001610 if (!can_write_buf_line(channel))
1611 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001612 write_buf_line(buf, lnum, channel);
1613 ++written;
1614 }
1615
1616 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001617 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001618 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001619 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001620 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001621 ch_log(channel, "Still %ld more lines to write",
1622 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001623
1624 in_part->ch_buf_bot = lnum;
1625 }
1626 }
1627 if (!found_one)
1628 buf->b_write_to_channel = FALSE;
1629}
1630
1631/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001632 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001633 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001634 */
1635 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001636invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1637 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001638{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001639 typval_T rettv;
1640 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001641
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001642 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001643 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001644
Bram Moolenaar77073442016-02-13 23:23:53 +01001645 argv[0].v_type = VAR_CHANNEL;
1646 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001647
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001648 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1649 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001650 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001651 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001652}
1653
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001654/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001655 * Return the first node from "channel"/"part" without removing it.
1656 * Returns NULL if there is nothing.
1657 */
1658 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001659channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001660{
1661 readq_T *head = &channel->ch_part[part].ch_head;
1662
1663 return head->rq_next;
1664}
1665
1666/*
1667 * Return a pointer to the first NL in "node".
1668 * Skips over NUL characters.
1669 * Returns NULL if there is no NL.
1670 */
1671 char_u *
1672channel_first_nl(readq_T *node)
1673{
1674 char_u *buffer = node->rq_buffer;
1675 long_u i;
1676
1677 for (i = 0; i < node->rq_buflen; ++i)
1678 if (buffer[i] == NL)
1679 return buffer + i;
1680 return NULL;
1681}
1682
1683/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001684 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001685 * The caller must free it.
1686 * Returns NULL if there is nothing.
1687 */
1688 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001689channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001690{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001691 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001692 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001693 char_u *p;
1694
Bram Moolenaar77073442016-02-13 23:23:53 +01001695 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 if (outlen != NULL)
1698 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001699 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001700 p = node->rq_buffer;
1701 head->rq_next = node->rq_next;
1702 if (node->rq_next == NULL)
1703 head->rq_prev = NULL;
1704 else
1705 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706 vim_free(node);
1707 return p;
1708}
1709
1710/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001711 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001712 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713 */
1714 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001716{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001717 readq_T *head = &channel->ch_part[part].ch_head;
1718 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001719 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001720 char_u *res;
1721 char_u *p;
1722
1723 /* If there is only one buffer just get that one. */
1724 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001725 return channel_get(channel, part, outlen);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001726
1727 /* Concatenate everything into one buffer. */
1728 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001729 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001730 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001731 if (res == NULL)
1732 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001733 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001734 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001735 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001736 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001737 p += node->rq_buflen;
1738 }
1739 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001740
1741 /* Free all buffers */
1742 do
1743 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001745 vim_free(p);
1746 } while (p != NULL);
1747
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001748 if (outlen != NULL)
1749 {
1750 *outlen += len;
1751 return res;
1752 }
1753
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001754 /* turn all NUL into NL */
1755 while (len > 0)
1756 {
1757 --len;
1758 if (res[len] == NUL)
1759 res[len] = NL;
1760 }
1761
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001762 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001763}
1764
1765/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001766 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001767 * Caller must check these bytes are available.
1768 */
1769 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001770channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001771{
1772 readq_T *head = &channel->ch_part[part].ch_head;
1773 readq_T *node = head->rq_next;
1774 char_u *buf = node->rq_buffer;
1775
1776 mch_memmove(buf, buf + len, node->rq_buflen - len);
1777 node->rq_buflen -= len;
1778}
1779
1780/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001781 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001782 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001783 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001784 */
1785 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001786channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001787{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001788 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001789 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001790 readq_T *last_node;
1791 readq_T *n;
1792 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001793 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001794 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001795
Bram Moolenaar77073442016-02-13 23:23:53 +01001796 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001797 return FAIL;
1798
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001799 last_node = node->rq_next;
1800 len = node->rq_buflen + last_node->rq_buflen + 1;
1801 if (want_nl)
1802 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001803 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001804 {
1805 last_node = last_node->rq_next;
1806 len += last_node->rq_buflen;
1807 }
1808
1809 p = newbuf = alloc(len);
1810 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001811 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001812 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001813 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001814 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001815 node->rq_buffer = newbuf;
1816 for (n = node; n != last_node; )
1817 {
1818 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001819 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001820 p += n->rq_buflen;
1821 vim_free(n->rq_buffer);
1822 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001823 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824
1825 /* dispose of the collapsed nodes and their buffers */
1826 for (n = node->rq_next; n != last_node; )
1827 {
1828 n = n->rq_next;
1829 vim_free(n->rq_prev);
1830 }
1831 node->rq_next = last_node->rq_next;
1832 if (last_node->rq_next == NULL)
1833 head->rq_prev = node;
1834 else
1835 last_node->rq_next->rq_prev = node;
1836 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001837 return OK;
1838}
1839
1840/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001841 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001842 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843 * Returns OK or FAIL.
1844 */
1845 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001846channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001847 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001848{
1849 readq_T *node;
1850 readq_T *head = &channel->ch_part[part].ch_head;
1851 char_u *p;
1852 int i;
1853
1854 node = (readq_T *)alloc(sizeof(readq_T));
1855 if (node == NULL)
1856 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001857 /* A NUL is added at the end, because netbeans code expects that.
1858 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001859 node->rq_buffer = alloc(len + 1);
1860 if (node->rq_buffer == NULL)
1861 {
1862 vim_free(node);
1863 return FAIL; /* out of memory */
1864 }
1865
1866 if (channel->ch_part[part].ch_mode == MODE_NL)
1867 {
1868 /* Drop any CR before a NL. */
1869 p = node->rq_buffer;
1870 for (i = 0; i < len; ++i)
1871 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1872 *p++ = buf[i];
1873 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001874 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001875 }
1876 else
1877 {
1878 mch_memmove(node->rq_buffer, buf, len);
1879 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001880 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001881 }
1882
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001883 if (prepend)
1884 {
1885 /* preend node to the head of the queue */
1886 node->rq_next = head->rq_next;
1887 node->rq_prev = NULL;
1888 if (head->rq_next == NULL)
1889 head->rq_prev = node;
1890 else
1891 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001892 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001893 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001894 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001895 {
1896 /* append node to the tail of the queue */
1897 node->rq_next = NULL;
1898 node->rq_prev = head->rq_prev;
1899 if (head->rq_prev == NULL)
1900 head->rq_next = node;
1901 else
1902 head->rq_prev->rq_next = node;
1903 head->rq_prev = node;
1904 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001905
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001906 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001907 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001908 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001909 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001910 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001911 fprintf(log_fd, "'\n");
1912 }
1913 return OK;
1914}
1915
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001916/*
1917 * Try to fill the buffer of "reader".
1918 * Returns FALSE when nothing was added.
1919 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001920 static int
1921channel_fill(js_read_T *reader)
1922{
1923 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001924 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001925 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001926 int keeplen;
1927 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001928 char_u *p;
1929
1930 if (next == NULL)
1931 return FALSE;
1932
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001933 keeplen = reader->js_end - reader->js_buf;
1934 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001935 {
1936 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001937 addlen = (int)STRLEN(next);
1938 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001939 if (p == NULL)
1940 {
1941 vim_free(next);
1942 return FALSE;
1943 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001944 mch_memmove(p, reader->js_buf, keeplen);
1945 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001946 vim_free(next);
1947 next = p;
1948 }
1949
1950 vim_free(reader->js_buf);
1951 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001952 return TRUE;
1953}
1954
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001955/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001956 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001957 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001958 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001959 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001960 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001961channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001962{
1963 js_read_T reader;
1964 typval_T listtv;
1965 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001966 chanpart_T *chanpart = &channel->ch_part[part];
1967 jsonq_T *head = &chanpart->ch_json_head;
1968 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001969 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001970
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001971 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001972 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001973
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001974 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001975 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001976 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001977 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001978 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001979
1980 /* When a message is incomplete we wait for a short while for more to
1981 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001982 * or list will make us hang.
1983 * Do not generate error messages, they will be written in a channel log. */
1984 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001985 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001986 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001987 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001988 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001989 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001990 /* Only accept the response when it is a list with at least two
1991 * items. */
1992 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001993 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994 if (listtv.v_type != VAR_LIST)
1995 ch_error(channel, "Did not receive a list, discarding");
1996 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001997 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001998 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001999 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002000 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002001 else
2002 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002003 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2004 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002005 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002006 else
2007 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002008 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002009 item->jq_value = alloc_tv();
2010 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002011 {
2012 vim_free(item);
2013 clear_tv(&listtv);
2014 }
2015 else
2016 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002017 *item->jq_value = listtv;
2018 item->jq_prev = head->jq_prev;
2019 head->jq_prev = item;
2020 item->jq_next = NULL;
2021 if (item->jq_prev == NULL)
2022 head->jq_next = item;
2023 else
2024 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002025 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002026 }
2027 }
2028 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002029
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002030 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002031 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002032 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002033 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002034 size_t buflen = STRLEN(reader.js_buf);
2035
2036 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002037 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002038 /* First time encountering incomplete message or after receiving
2039 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002040 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002041 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002042 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002043 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002044 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002045#ifdef WIN32
2046 chanpart->ch_deadline = GetTickCount() + 100L;
2047#else
2048 gettimeofday(&chanpart->ch_deadline, NULL);
2049 chanpart->ch_deadline.tv_usec += 100 * 1000;
2050 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2051 {
2052 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2053 ++chanpart->ch_deadline.tv_sec;
2054 }
2055#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002056 }
2057 else
2058 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002059 int timeout;
2060#ifdef WIN32
2061 timeout = GetTickCount() > chanpart->ch_deadline;
2062#else
2063 {
2064 struct timeval now_tv;
2065
2066 gettimeofday(&now_tv, NULL);
2067 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2068 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2069 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2070 }
2071#endif
2072 if (timeout)
2073 {
2074 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002075 chanpart->ch_wait_len = 0;
2076 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002077 }
2078 else
2079 {
2080 reader.js_used = 0;
2081 ch_log(channel, "still waiting on incomplete message");
2082 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002083 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002084 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002085
2086 if (status == FAIL)
2087 {
2088 ch_error(channel, "Decoding failed - discarding input");
2089 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002090 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002091 }
2092 else if (reader.js_buf[reader.js_used] != NUL)
2093 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002094 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002095 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002096 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2097 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002098 ret = status == MAYBE ? FALSE: TRUE;
2099 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002100 else
2101 ret = FALSE;
2102
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002103 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002104 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002105}
2106
2107/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002108 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002109 */
2110 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002111remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002112{
Bram Moolenaar77073442016-02-13 23:23:53 +01002113 if (node->cq_prev == NULL)
2114 head->cq_next = node->cq_next;
2115 else
2116 node->cq_prev->cq_next = node->cq_next;
2117 if (node->cq_next == NULL)
2118 head->cq_prev = node->cq_prev;
2119 else
2120 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002121}
2122
2123/*
2124 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002125 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002126 */
2127 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002128remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002129{
Bram Moolenaar77073442016-02-13 23:23:53 +01002130 if (node->jq_prev == NULL)
2131 head->jq_next = node->jq_next;
2132 else
2133 node->jq_prev->jq_next = node->jq_next;
2134 if (node->jq_next == NULL)
2135 head->jq_prev = node->jq_prev;
2136 else
2137 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002138 vim_free(node);
2139}
2140
2141/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002142 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002143 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002144 * When "id" is zero or negative jut get the first message. But not the one
2145 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002146 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002147 * Return OK when found and return the value in "rettv".
2148 * Return FAIL otherwise.
2149 */
2150 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002151channel_get_json(
2152 channel_T *channel,
2153 ch_part_T part,
2154 int id,
2155 int without_callback,
2156 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002157{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002158 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002159 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002160
Bram Moolenaar77073442016-02-13 23:23:53 +01002161 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002162 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002163 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002164 typval_T *tv = &l->lv_first->li_tv;
2165
Bram Moolenaar958dc692016-12-01 15:34:12 +01002166 if ((without_callback || !item->jq_no_callback)
2167 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002168 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002169 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002170 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002171 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002172 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002173 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002174 ch_log(channel, "Getting JSON message %ld",
2175 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002176 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002177 return OK;
2178 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002179 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002180 }
2181 return FAIL;
2182}
2183
Bram Moolenaar958dc692016-12-01 15:34:12 +01002184/*
2185 * Put back "rettv" into the JSON queue, there was no callback for it.
2186 * Takes over the values in "rettv".
2187 */
2188 static void
2189channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2190{
2191 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2192 jsonq_T *item = head->jq_next;
2193 jsonq_T *newitem;
2194
2195 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2196 /* last item was pushed back, append to the end */
2197 item = NULL;
2198 else while (item != NULL && item->jq_no_callback)
2199 /* append after the last item that was pushed back */
2200 item = item->jq_next;
2201
2202 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2203 if (newitem == NULL)
2204 clear_tv(rettv);
2205 else
2206 {
2207 newitem->jq_value = alloc_tv();
2208 if (newitem->jq_value == NULL)
2209 {
2210 vim_free(newitem);
2211 clear_tv(rettv);
2212 }
2213 else
2214 {
2215 newitem->jq_no_callback = FALSE;
2216 *newitem->jq_value = *rettv;
2217 if (item == NULL)
2218 {
2219 /* append to the end */
2220 newitem->jq_prev = head->jq_prev;
2221 head->jq_prev = newitem;
2222 newitem->jq_next = NULL;
2223 if (newitem->jq_prev == NULL)
2224 head->jq_next = newitem;
2225 else
2226 newitem->jq_prev->jq_next = newitem;
2227 }
2228 else
2229 {
2230 /* append after "item" */
2231 newitem->jq_prev = item;
2232 newitem->jq_next = item->jq_next;
2233 item->jq_next = newitem;
2234 if (newitem->jq_next == NULL)
2235 head->jq_prev = newitem;
2236 else
2237 newitem->jq_next->jq_prev = newitem;
2238 }
2239 }
2240 }
2241}
2242
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243#define CH_JSON_MAX_ARGS 4
2244
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002245/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002246 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002247 * "argv[0]" is the command string.
2248 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002249 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002250 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002251channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002252{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002253 char_u *cmd = argv[0].vval.v_string;
2254 char_u *arg;
2255 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002256
Bram Moolenaarece61b02016-02-20 21:39:05 +01002257 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002258 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002259 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002260 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002261 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002262 return;
2263 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002265 if (arg == NULL)
2266 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002267
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002268 if (STRCMP(cmd, "ex") == 0)
2269 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002270 int save_called_emsg = called_emsg;
2271
2272 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002273 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002274 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002275 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002276 --emsg_silent;
2277 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002278 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002279 (char *)get_vim_var_str(VV_ERRMSG));
2280 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002281 }
2282 else if (STRCMP(cmd, "normal") == 0)
2283 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002284 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002285
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002286 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002287 ea.arg = arg;
2288 ea.addr_count = 0;
2289 ea.forceit = TRUE; /* no mapping */
2290 ex_normal(&ea);
2291 }
2292 else if (STRCMP(cmd, "redraw") == 0)
2293 {
2294 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002295
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002296 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002297 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002298 ex_redraw(&ea);
2299 showruler(FALSE);
2300 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002301 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002302 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002303 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002304 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002305 int is_call = cmd[0] == 'c';
2306 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002307
Bram Moolenaarece61b02016-02-20 21:39:05 +01002308 if (argv[id_idx].v_type != VAR_UNKNOWN
2309 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002310 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002311 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002312 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002313 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002314 }
2315 else if (is_call && argv[2].v_type != VAR_LIST)
2316 {
2317 ch_error(channel, "third argument for call must be a list");
2318 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002319 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002320 }
2321 else
2322 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002323 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002324 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002325 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002326 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002327
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002328 /* Don't pollute the display with errors. */
2329 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002330 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002331 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002332 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002333 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002334 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002335 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002336 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002337 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002338 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2339 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002340 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002341
2342 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002343 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002344 int id = argv[id_idx].vval.v_number;
2345
Bram Moolenaar55fab432016-02-07 16:53:13 +01002346 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002347 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002348 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002349 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002350 /* If evaluation failed or the result can't be encoded
2351 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002352 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002353 err_tv.v_type = VAR_STRING;
2354 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002355 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002356 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002357 if (json != NULL)
2358 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002359 channel_send(channel,
2360 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002361 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002362 vim_free(json);
2363 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002364 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002365 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002366 if (tv == &res_tv)
2367 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002368 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002369 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002370 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002371 }
2372 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002373 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002374 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002375 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002376 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002377}
2378
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002379/*
2380 * Invoke the callback at "cbhead".
2381 * Does not redraw but sets channel_need_redraw.
2382 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002383 static void
2384invoke_one_time_callback(
2385 channel_T *channel,
2386 cbq_T *cbhead,
2387 cbq_T *item,
2388 typval_T *argv)
2389{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002390 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002391 (char *)item->cq_callback);
2392 /* Remove the item from the list first, if the callback
2393 * invokes ch_close() the list will be cleared. */
2394 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002395 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002396 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002397 vim_free(item);
2398}
2399
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002400 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002401append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002402{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002403 bufref_T save_curbuf = {NULL, 0, 0};
2404 win_T *save_curwin = NULL;
2405 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002406 linenr_T lnum = buffer->b_ml.ml_line_count;
2407 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002408 chanpart_T *ch_part = &channel->ch_part[part];
2409 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002410 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002411
2412 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2413 {
2414 if (!ch_part->ch_nomod_error)
2415 {
2416 ch_error(channel, "Buffer is not modifiable, cannot append");
2417 ch_part->ch_nomod_error = TRUE;
2418 }
2419 return;
2420 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002421
2422 /* If the buffer is also used as input insert above the last
2423 * line. Don't write these lines. */
2424 if (save_write_to)
2425 {
2426 --lnum;
2427 buffer->b_write_to_channel = FALSE;
2428 }
2429
2430 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002431 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002432
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002433 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002434
2435 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2436 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2437
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002438 u_sync(TRUE);
2439 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002440 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002441
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002442 if (empty)
2443 {
2444 /* The buffer is empty, replace the first (dummy) line. */
2445 ml_replace(lnum, msg, TRUE);
2446 lnum = 0;
2447 }
2448 else
2449 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002450 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002451
2452 /* Restore curbuf/curwin/curtab */
2453 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2454
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002455 if (ch_part->ch_nomodifiable)
2456 buffer->b_p_ma = FALSE;
2457 else
2458 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002459
2460 if (buffer->b_nwindows > 0)
2461 {
2462 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002463
2464 FOR_ALL_WINDOWS(wp)
2465 {
2466 if (wp->w_buffer == buffer
2467 && (save_write_to
2468 ? wp->w_cursor.lnum == lnum + 1
2469 : (wp->w_cursor.lnum == lnum
2470 && wp->w_cursor.col == 0)))
2471 {
2472 ++wp->w_cursor.lnum;
2473 save_curwin = curwin;
2474 curwin = wp;
2475 curbuf = curwin->w_buffer;
2476 scroll_cursor_bot(0, FALSE);
2477 curwin = save_curwin;
2478 curbuf = curwin->w_buffer;
2479 }
2480 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002481 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002482 channel_need_redraw = TRUE;
2483 }
2484
2485 if (save_write_to)
2486 {
2487 channel_T *ch;
2488
2489 /* Find channels reading from this buffer and adjust their
2490 * next-to-read line number. */
2491 buffer->b_write_to_channel = TRUE;
2492 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2493 {
2494 chanpart_T *in_part = &ch->ch_part[PART_IN];
2495
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002496 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002497 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2498 }
2499 }
2500}
2501
Bram Moolenaar437905c2016-04-26 19:01:05 +02002502 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002503drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002504{
2505 char_u *msg;
2506
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002507 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002508 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002509 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002510 vim_free(msg);
2511 }
2512}
2513
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002514/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002515 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002516 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002517 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002518 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002519 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002520may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002521{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002522 char_u *msg = NULL;
2523 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002524 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002525 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002526 chanpart_T *ch_part = &channel->ch_part[part];
2527 ch_mode_T ch_mode = ch_part->ch_mode;
2528 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002529 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002530 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002531 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002532 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002533 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002534
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002535 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002536 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002537 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002538
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002539 /* Use a message-specific callback, part callback or channel callback */
2540 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2541 if (cbitem->cq_seq_nr == 0)
2542 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002543 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002544 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002545 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002546 partial = cbitem->cq_partial;
2547 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002548 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002549 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002550 callback = ch_part->ch_callback;
2551 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002552 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002553 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002554 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002555 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002556 partial = channel->ch_partial;
2557 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002558
Bram Moolenaarec68a992016-10-03 21:37:41 +02002559 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002560 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2561 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002562 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002563 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002564 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002565 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002566 buffer = NULL;
2567 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002568
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002569 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002570 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002571 listitem_T *item;
2572 int argc = 0;
2573
Bram Moolenaard7ece102016-02-02 23:23:02 +01002574 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002575 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002576 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002577 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002578 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002579 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002580 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002581 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002582
Bram Moolenaarece61b02016-02-20 21:39:05 +01002583 for (item = listtv->vval.v_list->lv_first;
2584 item != NULL && argc < CH_JSON_MAX_ARGS;
2585 item = item->li_next)
2586 argv[argc++] = item->li_tv;
2587 while (argc < CH_JSON_MAX_ARGS)
2588 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002589
Bram Moolenaarece61b02016-02-20 21:39:05 +01002590 if (argv[0].v_type == VAR_STRING)
2591 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002592 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002593 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002594 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002595 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002596 }
2597
Bram Moolenaarece61b02016-02-20 21:39:05 +01002598 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002599 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002600 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002601 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002602 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002603 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002604 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002605 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002606 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002607 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002608 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002609 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002610 return FALSE;
2611 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002612 else
2613 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002614 /* If there is no callback or buffer drop the message. */
2615 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002616 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002617 /* If there is a close callback it may use ch_read() to get the
2618 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002619 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002620 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002621 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002622 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002623
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002624 if (ch_mode == MODE_NL)
2625 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002626 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002627 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002628 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002629
2630 /* See if we have a message ending in NL in the first buffer. If
2631 * not try to concatenate the first and the second buffer. */
2632 while (TRUE)
2633 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002634 node = channel_peek(channel, part);
2635 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002636 if (nl != NULL)
2637 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002638 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002639 {
2640 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2641 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002642 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002643 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002644 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002645 buf = node->rq_buffer;
2646
Bram Moolenaarec68a992016-10-03 21:37:41 +02002647 if (nl == NULL)
2648 {
2649 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002650 char_u *new_buf;
2651
2652 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2653 if (new_buf == NULL)
2654 /* This might fail over and over again, should the message
2655 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002656 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002657 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002658 node->rq_buffer = buf;
2659 nl = buf + node->rq_buflen++;
2660 *nl = NUL;
2661 }
2662
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002663 /* Convert NUL to NL, the internal representation. */
2664 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2665 if (*p == NUL)
2666 *p = NL;
2667
2668 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002669 {
2670 /* get the whole buffer, drop the NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002671 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002672 *nl = NUL;
2673 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002674 else
2675 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002676 /* Copy the message into allocated memory (excluding the NL)
2677 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002678 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002679 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002680 }
2681 }
2682 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002683 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002684 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002685 * get everything we have.
2686 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002687 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002688 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002689
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002690 if (msg == NULL)
2691 return FALSE; /* out of memory (and avoids Coverity warning) */
2692
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002693 argv[1].v_type = VAR_STRING;
2694 argv[1].vval.v_string = msg;
2695 }
2696
Bram Moolenaara07fec92016-02-05 21:04:08 +01002697 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002698 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002699 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002700
Bram Moolenaar958dc692016-12-01 15:34:12 +01002701 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002702 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002703 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002704 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002705 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002706 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002707 break;
2708 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002709 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002710 {
2711 if (channel->ch_drop_never)
2712 {
2713 /* message must be read with ch_read() */
2714 channel_push_json(channel, part, listtv);
2715 listtv = NULL;
2716 }
2717 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002718 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002719 seq_nr);
2720 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002721 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002722 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002723 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002724 if (buffer != NULL)
2725 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002726 if (msg == NULL)
2727 /* JSON or JS mode: re-encode the message. */
2728 msg = json_encode(listtv, ch_mode);
2729 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002730 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002731#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002732 if (buffer->b_term != NULL)
2733 write_to_term(buffer, msg, channel);
2734 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002735#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002736 append_to_buffer(buffer, msg, channel, part);
2737 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002738 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002739
Bram Moolenaar187db502016-02-27 14:44:26 +01002740 if (callback != NULL)
2741 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002742 if (cbitem != NULL)
2743 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2744 else
2745 {
2746 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002747 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002748 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002749 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002750 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002751 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002752 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002753 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002754 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002755
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002756 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002757 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002758 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002759
2760 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002761}
2762
Bram Moolenaar113e1072019-01-20 15:30:40 +01002763#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002764/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002765 * Return TRUE when channel "channel" is open for writing to.
2766 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002767 */
2768 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002769channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002770{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002771 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002772 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002773}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002774#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002775
2776/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002777 * Return TRUE when channel "channel" is open for reading or writing.
2778 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002779 */
2780 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002781channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002782{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002783 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002784 || channel->CH_IN_FD != INVALID_FD
2785 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002786 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002787}
2788
2789/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002790 * Return TRUE if "channel" has JSON or other typeahead.
2791 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002792 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002793channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002794{
2795 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2796
2797 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2798 {
2799 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2800 jsonq_T *item = head->jq_next;
2801
2802 return item != NULL;
2803 }
2804 return channel_peek(channel, part) != NULL;
2805}
2806
2807/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002808 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002809 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002810 */
2811 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002812channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002813{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002814 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002815 int has_readahead = FALSE;
2816
Bram Moolenaar77073442016-02-13 23:23:53 +01002817 if (channel == NULL)
2818 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002819 if (req_part == PART_OUT)
2820 {
2821 if (channel->CH_OUT_FD != INVALID_FD)
2822 return "open";
2823 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002824 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002825 }
2826 else if (req_part == PART_ERR)
2827 {
2828 if (channel->CH_ERR_FD != INVALID_FD)
2829 return "open";
2830 if (channel_has_readahead(channel, PART_ERR))
2831 has_readahead = TRUE;
2832 }
2833 else
2834 {
2835 if (channel_is_open(channel))
2836 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002837 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002838 if (channel_has_readahead(channel, part))
2839 {
2840 has_readahead = TRUE;
2841 break;
2842 }
2843 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002844
2845 if (has_readahead)
2846 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002847 return "closed";
2848}
2849
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002850 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002851channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002852{
2853 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002854 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002855 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002856 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002857 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002858
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002859 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002860 STRCAT(namebuf, "_");
2861 tail = STRLEN(namebuf);
2862
2863 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002864 if (chanpart->ch_fd != INVALID_FD)
2865 status = "open";
2866 else if (channel_has_readahead(channel, part))
2867 status = "buffered";
2868 else
2869 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002870 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002871
2872 STRCPY(namebuf + tail, "mode");
2873 switch (chanpart->ch_mode)
2874 {
2875 case MODE_NL: s = "NL"; break;
2876 case MODE_RAW: s = "RAW"; break;
2877 case MODE_JSON: s = "JSON"; break;
2878 case MODE_JS: s = "JS"; break;
2879 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002880 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002881
2882 STRCPY(namebuf + tail, "io");
2883 if (part == PART_SOCK)
2884 s = "socket";
2885 else switch (chanpart->ch_io)
2886 {
2887 case JIO_NULL: s = "null"; break;
2888 case JIO_PIPE: s = "pipe"; break;
2889 case JIO_FILE: s = "file"; break;
2890 case JIO_BUFFER: s = "buffer"; break;
2891 case JIO_OUT: s = "out"; break;
2892 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002893 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002894
2895 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002896 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002897}
2898
2899 void
2900channel_info(channel_T *channel, dict_T *dict)
2901{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002902 dict_add_number(dict, "id", channel->ch_id);
2903 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002904
2905 if (channel->ch_hostname != NULL)
2906 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002907 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2908 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002909 channel_part_info(channel, dict, "sock", PART_SOCK);
2910 }
2911 else
2912 {
2913 channel_part_info(channel, dict, "out", PART_OUT);
2914 channel_part_info(channel, dict, "err", PART_ERR);
2915 channel_part_info(channel, dict, "in", PART_IN);
2916 }
2917}
2918
Bram Moolenaar77073442016-02-13 23:23:53 +01002919/*
2920 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002921 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002922 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002923 */
2924 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002925channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002926{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002927 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002928
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002929#ifdef FEAT_GUI
2930 channel_gui_unregister(channel);
2931#endif
2932
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002933 ch_close_part(channel, PART_SOCK);
2934 ch_close_part(channel, PART_IN);
2935 ch_close_part(channel, PART_OUT);
2936 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002937
Bram Moolenaara9f02812017-08-05 17:40:38 +02002938 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002939 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002940 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002941
Bram Moolenaara9f02812017-08-05 17:40:38 +02002942 /* Invoke callbacks and flush buffers before the close callback. */
2943 if (channel->ch_close_cb != NULL)
2944 ch_log(channel,
2945 "Invoking callbacks and flushing buffers before closing");
2946 for (part = PART_SOCK; part < PART_IN; ++part)
2947 {
2948 if (channel->ch_close_cb != NULL
2949 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2950 {
2951 /* Increment the refcount to avoid the channel being freed
2952 * halfway. */
2953 ++channel->ch_refcount;
2954 if (channel->ch_close_cb == NULL)
2955 ch_log(channel, "flushing %s buffers before closing",
2956 part_names[part]);
2957 while (may_invoke_callback(channel, part))
2958 ;
2959 --channel->ch_refcount;
2960 }
2961 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002962
Bram Moolenaara9f02812017-08-05 17:40:38 +02002963 if (channel->ch_close_cb != NULL)
2964 {
2965 typval_T argv[1];
2966 typval_T rettv;
2967 int dummy;
2968
2969 /* Increment the refcount to avoid the channel being freed
2970 * halfway. */
2971 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002972 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002973 (char *)channel->ch_close_cb);
2974 argv[0].v_type = VAR_CHANNEL;
2975 argv[0].vval.v_channel = channel;
2976 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002977 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002978 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002979 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002980 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002981
Bram Moolenaara9f02812017-08-05 17:40:38 +02002982 /* the callback is only called once */
2983 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2984 channel->ch_close_cb = NULL;
2985 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002986
Bram Moolenaara9f02812017-08-05 17:40:38 +02002987 if (channel_need_redraw)
2988 {
2989 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002990 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002991 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002992
Bram Moolenaara9f02812017-08-05 17:40:38 +02002993 if (!channel->ch_drop_never)
2994 /* any remaining messages are useless now */
2995 for (part = PART_SOCK; part < PART_IN; ++part)
2996 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002997
2998 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002999 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003000 }
3001
3002 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003003
3004#ifdef FEAT_TERMINAL
3005 term_channel_closed(channel);
3006#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003007}
3008
Bram Moolenaard04a0202016-01-26 23:30:18 +01003009/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003010 * Close the "in" part channel "channel".
3011 */
3012 void
3013channel_close_in(channel_T *channel)
3014{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003015 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003016}
3017
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003018 static void
3019remove_from_writeque(writeq_T *wq, writeq_T *entry)
3020{
3021 ga_clear(&entry->wq_ga);
3022 wq->wq_next = entry->wq_next;
3023 if (wq->wq_next == NULL)
3024 wq->wq_prev = NULL;
3025 else
3026 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003027 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003028}
3029
Bram Moolenaar0874a832016-09-01 15:11:51 +02003030/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003031 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003032 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003033 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003034channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003035{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003036 chanpart_T *ch_part = &channel->ch_part[part];
3037 jsonq_T *json_head = &ch_part->ch_json_head;
3038 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003039
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003040 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003041 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003042
3043 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003044 {
3045 cbq_T *node = cb_head->cq_next;
3046
3047 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003048 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003049 vim_free(node);
3050 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003051
3052 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003053 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003054 free_tv(json_head->jq_next->jq_value);
3055 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003056 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003057
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003058 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3059 ch_part->ch_callback = NULL;
3060 ch_part->ch_partial = NULL;
3061
3062 while (ch_part->ch_writeque.wq_next != NULL)
3063 remove_from_writeque(&ch_part->ch_writeque,
3064 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003065}
3066
3067/*
3068 * Clear all the read buffers on "channel".
3069 */
3070 void
3071channel_clear(channel_T *channel)
3072{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003073 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003074 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003075 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003076 channel_clear_one(channel, PART_OUT);
3077 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003078 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003079 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003080 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003081 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003082 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003083 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003084 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003085}
3086
Bram Moolenaar77073442016-02-13 23:23:53 +01003087#if defined(EXITFREE) || defined(PROTO)
3088 void
3089channel_free_all(void)
3090{
3091 channel_T *channel;
3092
Bram Moolenaard6051b52016-02-28 15:49:03 +01003093 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003094 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3095 channel_clear(channel);
3096}
3097#endif
3098
3099
Bram Moolenaar715d2852016-04-30 17:06:31 +02003100/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003101#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003102
3103/* Buffer size for reading incoming messages. */
3104#define MAXMSGSIZE 4096
3105
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003106#if defined(HAVE_SELECT)
3107/*
3108 * Add write fds where we are waiting for writing to be possible.
3109 */
3110 static int
3111channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3112{
3113 int maxfd = maxfd_arg;
3114 channel_T *ch;
3115
3116 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3117 {
3118 chanpart_T *in_part = &ch->ch_part[PART_IN];
3119
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003120 if (in_part->ch_fd != INVALID_FD
3121 && (in_part->ch_bufref.br_buf != NULL
3122 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003123 {
3124 FD_SET((int)in_part->ch_fd, wfds);
3125 if ((int)in_part->ch_fd >= maxfd)
3126 maxfd = (int)in_part->ch_fd + 1;
3127 }
3128 }
3129 return maxfd;
3130}
3131#else
3132/*
3133 * Add write fds where we are waiting for writing to be possible.
3134 */
3135 static int
3136channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3137{
3138 int nfd = nfd_in;
3139 channel_T *ch;
3140
3141 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3142 {
3143 chanpart_T *in_part = &ch->ch_part[PART_IN];
3144
Bram Moolenaar683b7962017-08-19 15:51:59 +02003145 if (in_part->ch_fd != INVALID_FD
3146 && (in_part->ch_bufref.br_buf != NULL
3147 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003148 {
3149 in_part->ch_poll_idx = nfd;
3150 fds[nfd].fd = in_part->ch_fd;
3151 fds[nfd].events = POLLOUT;
3152 ++nfd;
3153 }
3154 else
3155 in_part->ch_poll_idx = -1;
3156 }
3157 return nfd;
3158}
3159#endif
3160
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003161typedef enum {
3162 CW_READY,
3163 CW_NOT_READY,
3164 CW_ERROR
3165} channel_wait_result;
3166
Bram Moolenaard04a0202016-01-26 23:30:18 +01003167/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003168 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003169 * Return CW_READY when there is something to read.
3170 * Return CW_NOT_READY when there is nothing to read.
3171 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003172 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003173 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003174channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003175{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003176 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003177 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003178
Bram Moolenaard8070362016-02-15 21:56:54 +01003179# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003180 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003181 {
3182 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003183 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003184 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003185 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003186
3187 /* reading from a pipe, not a socket */
3188 while (TRUE)
3189 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003190 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3191
3192 if (r && nread > 0)
3193 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003194
3195 if (channel->ch_named_pipe)
3196 {
3197 DisconnectNamedPipe((HANDLE)fd);
3198 ConnectNamedPipe((HANDLE)fd, NULL);
3199 }
3200 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003201 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003202
3203 /* perhaps write some buffer lines */
3204 channel_write_any_lines();
3205
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003206 sleep_time = deadline - GetTickCount();
3207 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003208 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003209 /* Wait for a little while. Very short at first, up to 10 msec
3210 * after looping a few times. */
3211 if (sleep_time > delay)
3212 sleep_time = delay;
3213 Sleep(sleep_time);
3214 delay = delay * 2;
3215 if (delay > 10)
3216 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003217 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003218 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003219 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003220#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003221 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003222#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003223 struct timeval tval;
3224 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003225 fd_set wfds;
3226 int ret;
3227 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003228
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003229 tval.tv_sec = timeout / 1000;
3230 tval.tv_usec = (timeout % 1000) * 1000;
3231 for (;;)
3232 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003233 FD_ZERO(&rfds);
3234 FD_SET((int)fd, &rfds);
3235
3236 /* Write lines to a pipe when a pipe can be written to. Need to
3237 * set this every time, some buffers may be done. */
3238 maxfd = (int)fd + 1;
3239 FD_ZERO(&wfds);
3240 maxfd = channel_fill_wfds(maxfd, &wfds);
3241
3242 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003243# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003244 SOCK_ERRNO;
3245 if (ret == -1 && errno == EINTR)
3246 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003247# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003248 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003249 {
3250 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003251 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003252 channel_write_any_lines();
3253 continue;
3254 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003255 break;
3256 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003257#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003258 for (;;)
3259 {
3260 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3261 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003262
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003263 fds[0].fd = fd;
3264 fds[0].events = POLLIN;
3265 nfd = channel_fill_poll_write(nfd, fds);
3266 if (poll(fds, nfd, timeout) > 0)
3267 {
3268 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003269 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003270 channel_write_any_lines();
3271 continue;
3272 }
3273 break;
3274 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003275#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003276 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003277 return CW_NOT_READY;
3278}
3279
3280 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003281ch_close_part_on_error(
3282 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003283{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003284 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003285
3286 if (is_err)
3287 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003288 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003289 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003290 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003291
3292 /* Queue a "DETACH" netbeans message in the command queue in order to
3293 * terminate the netbeans session later. Do not end the session here
3294 * directly as we may be running in the context of a call to
3295 * netbeans_parse_messages():
3296 * netbeans_parse_messages
3297 * -> autocmd triggered while processing the netbeans cmd
3298 * -> ui_breakcheck
3299 * -> gui event loop or select loop
3300 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003301 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003302 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003303 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003304 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003305 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3306
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003307 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003308 * close the channel yet, there may be something to read on another part.
3309 * When stdout and stderr use the same FD we get the error only on one of
3310 * them, also close the other. */
3311 if (part == PART_OUT || part == PART_ERR)
3312 {
3313 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3314
3315 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3316 ch_close_part(channel, other);
3317 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003318 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003319
3320#ifdef FEAT_GUI
3321 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003322 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003323#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003324}
3325
3326 static void
3327channel_close_now(channel_T *channel)
3328{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003329 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003330 if (channel->ch_nb_close_cb != NULL)
3331 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003332 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003333}
3334
3335/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003336 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003337 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003338 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003339 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003340 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003341channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003342{
3343 static char_u *buf = NULL;
3344 int len = 0;
3345 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003346 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003347 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003348
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003349 fd = channel->ch_part[part].ch_fd;
3350 if (fd == INVALID_FD)
3351 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003352 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003353 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003354 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003355 }
3356 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003357
3358 /* Allocate a buffer to read into. */
3359 if (buf == NULL)
3360 {
3361 buf = alloc(MAXMSGSIZE);
3362 if (buf == NULL)
3363 return; /* out of memory! */
3364 }
3365
3366 /* Keep on reading for as long as there is something to read.
3367 * Use select() or poll() to avoid blocking on a message that is exactly
3368 * MAXMSGSIZE long. */
3369 for (;;)
3370 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003371 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003372 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003373 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003374 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003375 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003376 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003377 if (len <= 0)
3378 break; /* error or nothing more to read */
3379
3380 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003381 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003382 readlen += len;
3383 if (len < MAXMSGSIZE)
3384 break; /* did read everything that's available */
3385 }
3386
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003387 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003388 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003389 {
3390 if (!channel->ch_keep_open)
3391 ch_close_part_on_error(channel, part, (len < 0), func);
3392 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003393#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003394 else if (CH_HAS_GUI && gtk_main_level() > 0)
3395 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003396 gtk_main_quit();
3397#endif
3398}
3399
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003400/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003401 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003402 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003403 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003404 * Returns what was read in allocated memory.
3405 * Returns NULL in case of error or timeout.
3406 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003407 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003408channel_read_block(
3409 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003410{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003411 char_u *buf;
3412 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003413 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003414 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003415 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003416 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003417
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003418 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003419 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003420
3421 while (TRUE)
3422 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003423 node = channel_peek(channel, part);
3424 if (node != NULL)
3425 {
3426 if (mode == MODE_RAW || (mode == MODE_NL
3427 && channel_first_nl(node) != NULL))
3428 /* got a complete message */
3429 break;
3430 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3431 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003432 /* If not blocking or nothing more is coming then return what we
3433 * have. */
3434 if (raw || fd == INVALID_FD)
3435 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003436 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003437
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003438 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003439 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003440 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003441 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003442 {
3443 ch_log(channel, "Timed out");
3444 return NULL;
3445 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003446 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003447 }
3448
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003449 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003450 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003451 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003452 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003453 }
3454 else
3455 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003456 char_u *p;
3457
3458 buf = node->rq_buffer;
3459 nl = channel_first_nl(node);
3460
3461 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003462 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003463 if (*p == NUL)
3464 *p = NL;
3465
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003466 if (nl == NULL)
3467 {
3468 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003469 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003470 }
3471 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003472 {
3473 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003474 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003475 *nl = NUL;
3476 }
3477 else
3478 {
3479 /* Copy the message into allocated memory and remove it from the
3480 * buffer. */
3481 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003482 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003483 }
3484 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003485 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003486 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003487 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003488}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003489
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003490/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003491 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003492 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003493 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003494 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003495 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003496 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003497channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003498 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003499 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003500 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003501 int id,
3502 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003503{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003504 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003505 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003506 int timeout;
3507 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003508
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003509 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003510 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003511 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003512 for (;;)
3513 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003514 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003515
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003516 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003517 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003518 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003519 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003520 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003521 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003522
Bram Moolenaard7ece102016-02-02 23:23:02 +01003523 if (!more)
3524 {
3525 /* Handle any other messages in the queue. If done some more
3526 * messages may have arrived. */
3527 if (channel_parse_messages())
3528 continue;
3529
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003530 /* Wait for up to the timeout. If there was an incomplete message
3531 * use the deadline for that. */
3532 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003533 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003534 {
3535#ifdef WIN32
3536 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3537#else
3538 {
3539 struct timeval now_tv;
3540
3541 gettimeofday(&now_tv, NULL);
3542 timeout = (chanpart->ch_deadline.tv_sec
3543 - now_tv.tv_sec) * 1000
3544 + (chanpart->ch_deadline.tv_usec
3545 - now_tv.tv_usec) / 1000
3546 + 1;
3547 }
3548#endif
3549 if (timeout < 0)
3550 {
3551 /* Something went wrong, channel_parse_json() didn't
3552 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003553 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003554 timeout = timeout_arg;
3555 }
3556 else if (timeout > timeout_arg)
3557 timeout = timeout_arg;
3558 }
3559 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003560 if (fd == INVALID_FD
3561 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003562 {
3563 if (timeout == timeout_arg)
3564 {
3565 if (fd != INVALID_FD)
3566 ch_log(channel, "Timed out");
3567 break;
3568 }
3569 }
3570 else
3571 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003572 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003573 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003574 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003575 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003576}
3577
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003578/*
3579 * Common for ch_read() and ch_readraw().
3580 */
3581 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003582common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003583{
3584 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003585 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003586 jobopt_T opt;
3587 int mode;
3588 int timeout;
3589 int id = -1;
3590 typval_T *listtv = NULL;
3591
3592 /* return an empty string by default */
3593 rettv->v_type = VAR_STRING;
3594 rettv->vval.v_string = NULL;
3595
3596 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003597 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003598 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003599 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003600
Bram Moolenaar437905c2016-04-26 19:01:05 +02003601 if (opt.jo_set & JO_PART)
3602 part = opt.jo_part;
3603 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003604 if (channel != NULL)
3605 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003606 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003607 part = channel_part_read(channel);
3608 mode = channel_get_mode(channel, part);
3609 timeout = channel_get_timeout(channel, part);
3610 if (opt.jo_set & JO_TIMEOUT)
3611 timeout = opt.jo_timeout;
3612
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003613 if (blob)
3614 {
3615 int outlen = 0;
3616 char_u *p = channel_read_block(channel, part,
3617 timeout, TRUE, &outlen);
3618 if (p != NULL)
3619 {
3620 blob_T *b = blob_alloc();
3621
3622 if (b != NULL)
3623 {
3624 b->bv_ga.ga_len = outlen;
3625 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3626 blob_free(b);
3627 else
3628 {
3629 memcpy(b->bv_ga.ga_data, p, outlen);
3630 rettv_blob_set(rettv, b);
3631 }
3632 }
3633 vim_free(p);
3634 }
3635 }
3636 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003637 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003638 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003639 else
3640 {
3641 if (opt.jo_set & JO_ID)
3642 id = opt.jo_id;
3643 channel_read_json_block(channel, part, timeout, id, &listtv);
3644 if (listtv != NULL)
3645 {
3646 *rettv = *listtv;
3647 vim_free(listtv);
3648 }
3649 else
3650 {
3651 rettv->v_type = VAR_SPECIAL;
3652 rettv->vval.v_number = VVAL_NONE;
3653 }
3654 }
3655 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003656
3657theend:
3658 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003659}
3660
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003661# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3662 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003663/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003664 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003665 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003666 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003667 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003668channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003669{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003670 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003671 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003672
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003673 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003674 for (channel = first_channel; channel != NULL;
3675 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003676 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003677 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003678 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003679 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003680 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003681 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003682 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003683 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003684 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003685}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003686# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003687
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003688# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003689/*
3690 * Check the channels for anything that is ready to be read.
3691 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003692 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003693 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003694 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003695channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003696{
3697 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003698 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003699 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003700
3701 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3702 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003703 if (only_keep_open && !channel->ch_keep_open)
3704 continue;
3705
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003706 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003707 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003708 {
3709 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003710 if (fd != INVALID_FD)
3711 {
3712 int r = channel_wait(channel, fd, 0);
3713
3714 if (r == CW_READY)
3715 channel_read(channel, part, "channel_handle_events");
3716 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003717 ch_close_part_on_error(channel, part, TRUE,
3718 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003719 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003720 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003721 }
3722}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003723# endif
3724
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003725# if defined(FEAT_GUI) || defined(PROTO)
3726/*
3727 * Return TRUE when there is any channel with a keep_open flag.
3728 */
3729 int
3730channel_any_keep_open()
3731{
3732 channel_T *channel;
3733
3734 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3735 if (channel->ch_keep_open)
3736 return TRUE;
3737 return FALSE;
3738}
3739# endif
3740
Bram Moolenaard04a0202016-01-26 23:30:18 +01003741/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003742 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003743 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003744 */
3745 void
3746channel_set_nonblock(channel_T *channel, ch_part_T part)
3747{
3748 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003749 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003750
3751 if (fd != INVALID_FD)
3752 {
3753#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003754 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003755
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003756 ioctlsocket(fd, FIONBIO, &val);
3757#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003758 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003759#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003760 ch_part->ch_nonblocking = TRUE;
3761 }
3762}
3763
3764/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003765 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003766 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003767 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003768 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003769 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003770channel_send(
3771 channel_T *channel,
3772 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003773 char_u *buf_arg,
3774 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003775 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003776{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003777 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003778 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003779 chanpart_T *ch_part = &channel->ch_part[part];
3780 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003781
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003782 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003783 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003784 {
3785 if (!channel->ch_error && fun != NULL)
3786 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003787 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003788 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003789 }
3790 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003791 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003792 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003793
Bram Moolenaar0b146882018-09-06 16:27:24 +02003794 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3795 channel_set_nonblock(channel, part);
3796
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003797 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003798 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003799 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003800 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003801 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003802 fprintf(log_fd, "'\n");
3803 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003804 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003805 }
3806
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003807 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003808 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003809 writeq_T *wq = &ch_part->ch_writeque;
3810 char_u *buf;
3811 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003812
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003813 if (wq->wq_next != NULL)
3814 {
3815 /* first write what was queued */
3816 buf = wq->wq_next->wq_ga.ga_data;
3817 len = wq->wq_next->wq_ga.ga_len;
3818 did_use_queue = TRUE;
3819 }
3820 else
3821 {
3822 if (len_arg == 0)
3823 /* nothing to write, called from channel_select_check() */
3824 return OK;
3825 buf = buf_arg;
3826 len = len_arg;
3827 }
3828
3829 if (part == PART_SOCK)
3830 res = sock_write(fd, (char *)buf, len);
3831 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003832 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003833 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar31faed62019-01-22 23:01:40 +01003834#ifdef WIN32
3835 if (channel->ch_named_pipe && res < 0)
3836 {
3837 DisconnectNamedPipe((HANDLE)fd);
3838 ConnectNamedPipe((HANDLE)fd, NULL);
3839 }
3840#endif
3841 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003842 if (res < 0 && (errno == EWOULDBLOCK
3843#ifdef EAGAIN
3844 || errno == EAGAIN
3845#endif
3846 ))
3847 res = 0; /* nothing got written */
3848
3849 if (res >= 0 && ch_part->ch_nonblocking)
3850 {
3851 writeq_T *entry = wq->wq_next;
3852
3853 if (did_use_queue)
3854 ch_log(channel, "Sent %d bytes now", res);
3855 if (res == len)
3856 {
3857 /* Wrote all the buf[len] bytes. */
3858 if (entry != NULL)
3859 {
3860 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003861 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003862 continue;
3863 }
3864 if (did_use_queue)
3865 ch_log(channel, "Write queue empty");
3866 }
3867 else
3868 {
3869 /* Wrote only buf[res] bytes, can't write more now. */
3870 if (entry != NULL)
3871 {
3872 if (res > 0)
3873 {
3874 /* Remove the bytes that were written. */
3875 mch_memmove(entry->wq_ga.ga_data,
3876 (char *)entry->wq_ga.ga_data + res,
3877 len - res);
3878 entry->wq_ga.ga_len -= res;
3879 }
3880 buf = buf_arg;
3881 len = len_arg;
3882 }
3883 else
3884 {
3885 buf += res;
3886 len -= res;
3887 }
3888 ch_log(channel, "Adding %d bytes to the write queue", len);
3889
3890 /* Append the not written bytes of the argument to the write
3891 * buffer. Limit entries to 4000 bytes. */
3892 if (wq->wq_prev != NULL
3893 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3894 {
3895 writeq_T *last = wq->wq_prev;
3896
3897 /* append to the last entry */
3898 if (ga_grow(&last->wq_ga, len) == OK)
3899 {
3900 mch_memmove((char *)last->wq_ga.ga_data
3901 + last->wq_ga.ga_len,
3902 buf, len);
3903 last->wq_ga.ga_len += len;
3904 }
3905 }
3906 else
3907 {
3908 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3909
3910 if (last != NULL)
3911 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003912 last->wq_prev = wq->wq_prev;
3913 last->wq_next = NULL;
3914 if (wq->wq_prev == NULL)
3915 wq->wq_next = last;
3916 else
3917 wq->wq_prev->wq_next = last;
3918 wq->wq_prev = last;
3919 ga_init2(&last->wq_ga, 1, 1000);
3920 if (ga_grow(&last->wq_ga, len) == OK)
3921 {
3922 mch_memmove(last->wq_ga.ga_data, buf, len);
3923 last->wq_ga.ga_len = len;
3924 }
3925 }
3926 }
3927 }
3928 }
3929 else if (res != len)
3930 {
3931 if (!channel->ch_error && fun != NULL)
3932 {
3933 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003934 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003935 }
3936 channel->ch_error = TRUE;
3937 return FAIL;
3938 }
3939
3940 channel->ch_error = FALSE;
3941 return OK;
3942 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003943}
3944
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003945/*
3946 * Common for "ch_sendexpr()" and "ch_sendraw()".
3947 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003948 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003949 * Otherwise returns NULL.
3950 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003951 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003952send_common(
3953 typval_T *argvars,
3954 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003955 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003956 int id,
3957 int eval,
3958 jobopt_T *opt,
3959 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003960 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003961{
3962 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003963 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003964
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003965 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003966 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003967 if (channel == NULL)
3968 return NULL;
3969 part_send = channel_part_send(channel);
3970 *part_read = channel_part_read(channel);
3971
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003972 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003973 return NULL;
3974
3975 /* Set the callback. An empty callback means no callback and not reading
3976 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3977 * allowed. */
3978 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3979 {
3980 if (eval)
3981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003982 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003983 return NULL;
3984 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003985 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003986 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003987 }
3988
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003989 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003990 && opt->jo_callback == NULL)
3991 return channel;
3992 return NULL;
3993}
3994
3995/*
3996 * common for "ch_evalexpr()" and "ch_sendexpr()"
3997 */
3998 void
3999ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4000{
4001 char_u *text;
4002 typval_T *listtv;
4003 channel_T *channel;
4004 int id;
4005 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004006 ch_part_T part_send;
4007 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004008 jobopt_T opt;
4009 int timeout;
4010
4011 /* return an empty string by default */
4012 rettv->v_type = VAR_STRING;
4013 rettv->vval.v_string = NULL;
4014
Bram Moolenaar437905c2016-04-26 19:01:05 +02004015 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004016 if (channel == NULL)
4017 return;
4018 part_send = channel_part_send(channel);
4019
4020 ch_mode = channel_get_mode(channel, part_send);
4021 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004023 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004024 return;
4025 }
4026
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004027 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004028 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004029 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004030 if (text == NULL)
4031 return;
4032
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004033 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004034 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4035 vim_free(text);
4036 if (channel != NULL && eval)
4037 {
4038 if (opt.jo_set & JO_TIMEOUT)
4039 timeout = opt.jo_timeout;
4040 else
4041 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004042 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4043 == OK)
4044 {
4045 list_T *list = listtv->vval.v_list;
4046
4047 /* Move the item from the list and then change the type to
4048 * avoid the value being freed. */
4049 *rettv = list->lv_last->li_tv;
4050 list->lv_last->li_tv.v_type = VAR_NUMBER;
4051 free_tv(listtv);
4052 }
4053 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004054 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055}
4056
4057/*
4058 * common for "ch_evalraw()" and "ch_sendraw()"
4059 */
4060 void
4061ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4062{
4063 char_u buf[NUMBUFLEN];
4064 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004065 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004066 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004067 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004068 jobopt_T opt;
4069 int timeout;
4070
4071 /* return an empty string by default */
4072 rettv->v_type = VAR_STRING;
4073 rettv->vval.v_string = NULL;
4074
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004075 if (argvars[1].v_type == VAR_BLOB)
4076 {
4077 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4078 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4079 }
4080 else
4081 {
4082 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004083 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004084 }
4085 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004086 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4087 if (channel != NULL && eval)
4088 {
4089 if (opt.jo_set & JO_TIMEOUT)
4090 timeout = opt.jo_timeout;
4091 else
4092 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004093 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004094 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004095 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004096 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004097}
4098
Bram Moolenaarf3360612017-10-01 16:21:31 +02004099# define KEEP_OPEN_TIME 20 /* msec */
4100
Bram Moolenaard04a0202016-01-26 23:30:18 +01004101# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004102/*
4103 * Add open channels to the poll struct.
4104 * Return the adjusted struct index.
4105 * The type of "fds" is hidden to avoid problems with the function proto.
4106 */
4107 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004108channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004109{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004110 int nfd = nfd_in;
4111 channel_T *channel;
4112 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004113 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004114
Bram Moolenaar77073442016-02-13 23:23:53 +01004115 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004116 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004117 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004118 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004119 chanpart_T *ch_part = &channel->ch_part[part];
4120
4121 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004122 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004123 if (channel->ch_keep_open)
4124 {
4125 /* For unknown reason poll() returns immediately for a
4126 * keep-open channel. Instead of adding it to the fds add
4127 * a short timeout and check, like polling. */
4128 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4129 *towait = KEEP_OPEN_TIME;
4130 }
4131 else
4132 {
4133 ch_part->ch_poll_idx = nfd;
4134 fds[nfd].fd = ch_part->ch_fd;
4135 fds[nfd].events = POLLIN;
4136 nfd++;
4137 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004138 }
4139 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004140 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004141 }
4142 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004143
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004144 nfd = channel_fill_poll_write(nfd, fds);
4145
Bram Moolenaare0874f82016-01-24 20:36:41 +01004146 return nfd;
4147}
4148
4149/*
4150 * The type of "fds" is hidden to avoid problems with the function proto.
4151 */
4152 int
4153channel_poll_check(int ret_in, void *fds_in)
4154{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004155 int ret = ret_in;
4156 channel_T *channel;
4157 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004158 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004159 int idx;
4160 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004161
Bram Moolenaar77073442016-02-13 23:23:53 +01004162 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004163 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004164 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004165 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004166 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004167
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004168 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004169 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004170 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004171 --ret;
4172 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004173 else if (channel->ch_part[part].ch_fd != INVALID_FD
4174 && channel->ch_keep_open)
4175 {
4176 /* polling a keep-open channel */
4177 channel_read(channel, part, "channel_poll_check_keep_open");
4178 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004179 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004180
4181 in_part = &channel->ch_part[PART_IN];
4182 idx = in_part->ch_poll_idx;
4183 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4184 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004185 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004186 --ret;
4187 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004188 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004189
4190 return ret;
4191}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004192# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004193
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004194# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004195
Bram Moolenaare0874f82016-01-24 20:36:41 +01004196/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004197 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004198 */
4199 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004200channel_select_setup(
4201 int maxfd_in,
4202 void *rfds_in,
4203 void *wfds_in,
4204 struct timeval *tv,
4205 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004206{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004207 int maxfd = maxfd_in;
4208 channel_T *channel;
4209 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004210 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004211 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004212
Bram Moolenaar77073442016-02-13 23:23:53 +01004213 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004214 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004215 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004216 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004217 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004218
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004219 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004220 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004221 if (channel->ch_keep_open)
4222 {
4223 /* For unknown reason select() returns immediately for a
4224 * keep-open channel. Instead of adding it to the rfds add
4225 * a short timeout and check, like polling. */
4226 if (*tvp == NULL || tv->tv_sec > 0
4227 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4228 {
4229 *tvp = tv;
4230 tv->tv_sec = 0;
4231 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4232 }
4233 }
4234 else
4235 {
4236 FD_SET((int)fd, rfds);
4237 if (maxfd < (int)fd)
4238 maxfd = (int)fd;
4239 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004240 }
4241 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004242 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004243
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004244 maxfd = channel_fill_wfds(maxfd, wfds);
4245
Bram Moolenaare0874f82016-01-24 20:36:41 +01004246 return maxfd;
4247}
4248
4249/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004250 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004251 */
4252 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004253channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004254{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004255 int ret = ret_in;
4256 channel_T *channel;
4257 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004258 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004259 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004260 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004261
Bram Moolenaar77073442016-02-13 23:23:53 +01004262 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004263 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004264 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004265 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004266 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004267
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004268 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004269 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004270 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004271 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004272 --ret;
4273 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004274 else if (fd != INVALID_FD && channel->ch_keep_open)
4275 {
4276 /* polling a keep-open channel */
4277 channel_read(channel, part, "channel_select_check_keep_open");
4278 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004279 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004280
4281 in_part = &channel->ch_part[PART_IN];
4282 if (ret > 0 && in_part->ch_fd != INVALID_FD
4283 && FD_ISSET(in_part->ch_fd, wfds))
4284 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004285 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004286 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004287 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004288 --ret;
4289 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004290 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004291
4292 return ret;
4293}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004294# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004295
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004296/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004297 * Execute queued up commands.
4298 * Invoked from the main loop when it's safe to execute received commands.
4299 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004300 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004301 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004302channel_parse_messages(void)
4303{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004304 channel_T *channel = first_channel;
4305 int ret = FALSE;
4306 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004307 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004308#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004309 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004310
4311 ELAPSED_INIT(start_tv);
4312#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004313
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004314 ++safe_to_invoke_callback;
4315
Bram Moolenaard0b65022016-03-06 21:50:33 +01004316 /* Only do this message when another message was given, otherwise we get
4317 * lots of them. */
4318 if (did_log_msg)
4319 {
4320 ch_log(NULL, "looking for messages on channels");
4321 did_log_msg = FALSE;
4322 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004323 while (channel != NULL)
4324 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004325 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004326 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004327 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004328 channel_close_now(channel);
4329 /* channel may have been freed, start over */
4330 channel = first_channel;
4331 continue;
4332 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004333 if (channel->ch_to_be_freed)
4334 {
4335 channel_free(channel);
4336 /* channel has been freed, start over */
4337 channel = first_channel;
4338 continue;
4339 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004340 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004341 {
4342 /* channel is no longer useful, free it */
4343 channel_free(channel);
4344 channel = first_channel;
4345 part = PART_SOCK;
4346 continue;
4347 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004348 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004349 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004350 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004351 /* Increase the refcount, in case the handler causes the channel
4352 * to be unreferenced or closed. */
4353 ++channel->ch_refcount;
4354 r = may_invoke_callback(channel, part);
4355 if (r == OK)
4356 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004357 if (channel_unref(channel) || (r == OK
4358#ifdef ELAPSED_FUNC
4359 /* Limit the time we loop here to 100 msec, otherwise
4360 * Vim becomes unresponsive when the callback takes
4361 * more than a bit of time. */
4362 && ELAPSED_FUNC(start_tv) < 100L
4363#endif
4364 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004365 {
4366 /* channel was freed or something was done, start over */
4367 channel = first_channel;
4368 part = PART_SOCK;
4369 continue;
4370 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004371 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004372 if (part < PART_ERR)
4373 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004374 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004375 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004376 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004377 part = PART_SOCK;
4378 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004379 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004380
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004381 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004382 {
4383 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004384 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004385 }
4386
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004387 --safe_to_invoke_callback;
4388
Bram Moolenaard7ece102016-02-02 23:23:02 +01004389 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004390}
4391
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004392/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004393 * Return TRUE if any channel has readahead. That means we should not block on
4394 * waiting for input.
4395 */
4396 int
4397channel_any_readahead(void)
4398{
4399 channel_T *channel = first_channel;
4400 ch_part_T part = PART_SOCK;
4401
4402 while (channel != NULL)
4403 {
4404 if (channel_has_readahead(channel, part))
4405 return TRUE;
4406 if (part < PART_ERR)
4407 ++part;
4408 else
4409 {
4410 channel = channel->ch_next;
4411 part = PART_SOCK;
4412 }
4413 }
4414 return FALSE;
4415}
4416
4417/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004418 * Mark references to lists used in channels.
4419 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004420 int
4421set_ref_in_channel(int copyID)
4422{
Bram Moolenaar77073442016-02-13 23:23:53 +01004423 int abort = FALSE;
4424 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004425 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004426
Bram Moolenaar77073442016-02-13 23:23:53 +01004427 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004428 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004429 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004430 tv.v_type = VAR_CHANNEL;
4431 tv.vval.v_channel = channel;
4432 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004433 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004434 return abort;
4435}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004436
4437/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004438 * Return the "part" to write to for "channel".
4439 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004440 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004441channel_part_send(channel_T *channel)
4442{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004443 if (channel->CH_SOCK_FD == INVALID_FD)
4444 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004445 return PART_SOCK;
4446}
4447
4448/*
4449 * Return the default "part" to read from for "channel".
4450 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004451 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004452channel_part_read(channel_T *channel)
4453{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004454 if (channel->CH_SOCK_FD == INVALID_FD)
4455 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004456 return PART_SOCK;
4457}
4458
4459/*
4460 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004461 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004462 */
4463 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004464channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004465{
Bram Moolenaar77073442016-02-13 23:23:53 +01004466 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004467 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004468 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004469}
4470
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004471/*
4472 * Return the timeout of "channel"/"part"
4473 */
4474 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004475channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004476{
4477 return channel->ch_part[part].ch_timeout;
4478}
4479
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004480 static int
4481handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4482{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004483 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004484
4485 opt->jo_set |= jo;
4486 if (STRCMP(val, "nl") == 0)
4487 *modep = MODE_NL;
4488 else if (STRCMP(val, "raw") == 0)
4489 *modep = MODE_RAW;
4490 else if (STRCMP(val, "js") == 0)
4491 *modep = MODE_JS;
4492 else if (STRCMP(val, "json") == 0)
4493 *modep = MODE_JSON;
4494 else
4495 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004496 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004497 return FAIL;
4498 }
4499 return OK;
4500}
4501
4502 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004503handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004504{
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_OUT_IO << (part - PART_OUT);
4508 if (STRCMP(val, "null") == 0)
4509 opt->jo_io[part] = JIO_NULL;
4510 else if (STRCMP(val, "pipe") == 0)
4511 opt->jo_io[part] = JIO_PIPE;
4512 else if (STRCMP(val, "file") == 0)
4513 opt->jo_io[part] = JIO_FILE;
4514 else if (STRCMP(val, "buffer") == 0)
4515 opt->jo_io[part] = JIO_BUFFER;
4516 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4517 opt->jo_io[part] = JIO_OUT;
4518 else
4519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004520 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004521 return FAIL;
4522 }
4523 return OK;
4524}
4525
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004526/*
4527 * Clear a jobopt_T before using it.
4528 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004529 void
4530clear_job_options(jobopt_T *opt)
4531{
4532 vim_memset(opt, 0, sizeof(jobopt_T));
4533}
4534
4535/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004536 * Free any members of a jobopt_T.
4537 */
4538 void
4539free_job_options(jobopt_T *opt)
4540{
4541 if (opt->jo_partial != NULL)
4542 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004543 else if (opt->jo_callback != NULL)
4544 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004545 if (opt->jo_out_partial != NULL)
4546 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004547 else if (opt->jo_out_cb != NULL)
4548 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004549 if (opt->jo_err_partial != NULL)
4550 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004551 else if (opt->jo_err_cb != NULL)
4552 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004553 if (opt->jo_close_partial != NULL)
4554 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004555 else if (opt->jo_close_cb != NULL)
4556 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004557 if (opt->jo_exit_partial != NULL)
4558 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004559 else if (opt->jo_exit_cb != NULL)
4560 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004561 if (opt->jo_env != NULL)
4562 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004563}
4564
4565/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004566 * Get the PART_ number from the first character of an option name.
4567 */
4568 static int
4569part_from_char(int c)
4570{
4571 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4572}
4573
4574/*
4575 * Get the option entries from the dict in "tv", parse them and put the result
4576 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004577 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004578 * If an option value is invalid return FAIL.
4579 */
4580 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004581get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004582{
4583 typval_T *item;
4584 char_u *val;
4585 dict_T *dict;
4586 int todo;
4587 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004588 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004589
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004590 if (tv->v_type == VAR_UNKNOWN)
4591 return OK;
4592 if (tv->v_type != VAR_DICT)
4593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004594 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004595 return FAIL;
4596 }
4597 dict = tv->vval.v_dict;
4598 if (dict == NULL)
4599 return OK;
4600
4601 todo = (int)dict->dv_hashtab.ht_used;
4602 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4603 if (!HASHITEM_EMPTY(hi))
4604 {
4605 item = &dict_lookup(hi)->di_tv;
4606
4607 if (STRCMP(hi->hi_key, "mode") == 0)
4608 {
4609 if (!(supported & JO_MODE))
4610 break;
4611 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4612 return FAIL;
4613 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004614 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004615 {
4616 if (!(supported & JO_IN_MODE))
4617 break;
4618 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4619 == FAIL)
4620 return FAIL;
4621 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004622 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004623 {
4624 if (!(supported & JO_OUT_MODE))
4625 break;
4626 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4627 == FAIL)
4628 return FAIL;
4629 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004630 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004631 {
4632 if (!(supported & JO_ERR_MODE))
4633 break;
4634 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4635 == FAIL)
4636 return FAIL;
4637 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004638 else if (STRCMP(hi->hi_key, "noblock") == 0)
4639 {
4640 if (!(supported & JO_MODE))
4641 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004642 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004643 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004644 else if (STRCMP(hi->hi_key, "in_io") == 0
4645 || STRCMP(hi->hi_key, "out_io") == 0
4646 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004647 {
4648 if (!(supported & JO_OUT_IO))
4649 break;
4650 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4651 return FAIL;
4652 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004653 else if (STRCMP(hi->hi_key, "in_name") == 0
4654 || STRCMP(hi->hi_key, "out_name") == 0
4655 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004656 {
4657 part = part_from_char(*hi->hi_key);
4658
4659 if (!(supported & JO_OUT_IO))
4660 break;
4661 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4662 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004663 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004664 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004665 else if (STRCMP(hi->hi_key, "pty") == 0)
4666 {
4667 if (!(supported & JO_MODE))
4668 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004669 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004670 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004671 else if (STRCMP(hi->hi_key, "in_buf") == 0
4672 || STRCMP(hi->hi_key, "out_buf") == 0
4673 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004674 {
4675 part = part_from_char(*hi->hi_key);
4676
4677 if (!(supported & JO_OUT_IO))
4678 break;
4679 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004680 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004681 if (opt->jo_io_buf[part] <= 0)
4682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004683 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004684 return FAIL;
4685 }
4686 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004688 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004689 return FAIL;
4690 }
4691 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004692 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4693 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4694 {
4695 part = part_from_char(*hi->hi_key);
4696
4697 if (!(supported & JO_OUT_IO))
4698 break;
4699 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004700 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004701 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004702 else if (STRCMP(hi->hi_key, "out_msg") == 0
4703 || STRCMP(hi->hi_key, "err_msg") == 0)
4704 {
4705 part = part_from_char(*hi->hi_key);
4706
4707 if (!(supported & JO_OUT_IO))
4708 break;
4709 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004710 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004711 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004712 else if (STRCMP(hi->hi_key, "in_top") == 0
4713 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004714 {
4715 linenr_T *lp;
4716
4717 if (!(supported & JO_OUT_IO))
4718 break;
4719 if (hi->hi_key[3] == 't')
4720 {
4721 lp = &opt->jo_in_top;
4722 opt->jo_set |= JO_IN_TOP;
4723 }
4724 else
4725 {
4726 lp = &opt->jo_in_bot;
4727 opt->jo_set |= JO_IN_BOT;
4728 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004729 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004730 if (*lp < 0)
4731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004732 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004733 return FAIL;
4734 }
4735 }
4736 else if (STRCMP(hi->hi_key, "channel") == 0)
4737 {
4738 if (!(supported & JO_OUT_IO))
4739 break;
4740 opt->jo_set |= JO_CHANNEL;
4741 if (item->v_type != VAR_CHANNEL)
4742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004743 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004744 return FAIL;
4745 }
4746 opt->jo_channel = item->vval.v_channel;
4747 }
4748 else if (STRCMP(hi->hi_key, "callback") == 0)
4749 {
4750 if (!(supported & JO_CALLBACK))
4751 break;
4752 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004753 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004754 if (opt->jo_callback == NULL)
4755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004756 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004757 return FAIL;
4758 }
4759 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004760 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004761 {
4762 if (!(supported & JO_OUT_CALLBACK))
4763 break;
4764 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004765 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004766 if (opt->jo_out_cb == NULL)
4767 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004768 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004769 return FAIL;
4770 }
4771 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004772 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004773 {
4774 if (!(supported & JO_ERR_CALLBACK))
4775 break;
4776 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004777 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004778 if (opt->jo_err_cb == NULL)
4779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004780 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004781 return FAIL;
4782 }
4783 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004784 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004785 {
4786 if (!(supported & JO_CLOSE_CALLBACK))
4787 break;
4788 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004789 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004790 if (opt->jo_close_cb == NULL)
4791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004792 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004793 return FAIL;
4794 }
4795 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004796 else if (STRCMP(hi->hi_key, "drop") == 0)
4797 {
4798 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004799 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004800
4801 if (STRCMP(val, "never") == 0)
4802 never = TRUE;
4803 else if (STRCMP(val, "auto") != 0)
4804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004805 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004806 return FAIL;
4807 }
4808 opt->jo_drop_never = never;
4809 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004810 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4811 {
4812 if (!(supported & JO_EXIT_CB))
4813 break;
4814 opt->jo_set |= JO_EXIT_CB;
4815 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4816 if (opt->jo_exit_cb == NULL)
4817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004818 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004819 return FAIL;
4820 }
4821 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004822#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004823 else if (STRCMP(hi->hi_key, "term_name") == 0)
4824 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004825 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004826 break;
4827 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004828 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004829 if (opt->jo_term_name == NULL)
4830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004831 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004832 return FAIL;
4833 }
4834 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004835 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4836 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004837 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004838 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004839 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004840 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004842 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004843 return FAIL;
4844 }
4845 opt->jo_set2 |= JO2_TERM_FINISH;
4846 opt->jo_term_finish = *val;
4847 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004848 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4849 {
4850 char_u *p;
4851
4852 if (!(supported2 & JO2_TERM_OPENCMD))
4853 break;
4854 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004855 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004856 if (p != NULL)
4857 {
4858 /* Must have %d and no other %. */
4859 p = vim_strchr(p, '%');
4860 if (p != NULL && (p[1] != 'd'
4861 || vim_strchr(p + 2, '%') != NULL))
4862 p = NULL;
4863 }
4864 if (p == NULL)
4865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004866 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004867 return FAIL;
4868 }
4869 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004870 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4871 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004872 char_u *p;
4873
4874 if (!(supported2 & JO2_EOF_CHARS))
4875 break;
4876 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004877 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004878 if (p == NULL)
4879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004880 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004881 return FAIL;
4882 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004883 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004884 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4885 {
4886 if (!(supported2 & JO2_TERM_ROWS))
4887 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004888 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004889 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004890 }
4891 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4892 {
4893 if (!(supported2 & JO2_TERM_COLS))
4894 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004895 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004896 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004897 }
4898 else if (STRCMP(hi->hi_key, "vertical") == 0)
4899 {
4900 if (!(supported2 & JO2_VERTICAL))
4901 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004902 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004903 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004904 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004905 else if (STRCMP(hi->hi_key, "curwin") == 0)
4906 {
4907 if (!(supported2 & JO2_CURWIN))
4908 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004909 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004910 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004911 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004912 else if (STRCMP(hi->hi_key, "hidden") == 0)
4913 {
4914 if (!(supported2 & JO2_HIDDEN))
4915 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004916 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004917 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004918 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004919 else if (STRCMP(hi->hi_key, "norestore") == 0)
4920 {
4921 if (!(supported2 & JO2_NORESTORE))
4922 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004923 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004924 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004925 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004926 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4927 {
4928 if (!(supported2 & JO2_TERM_KILL))
4929 break;
4930 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004931 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004932 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004933# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4934 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004936 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004937 listitem_T *li;
4938 long_u rgb[16];
4939
4940 if (!(supported2 & JO2_ANSI_COLORS))
4941 break;
4942
4943 if (item == NULL || item->v_type != VAR_LIST
4944 || item->vval.v_list == NULL)
4945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004946 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004947 return FAIL;
4948 }
4949
4950 li = item->vval.v_list->lv_first;
4951 for (; li != NULL && n < 16; li = li->li_next, n++)
4952 {
4953 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004954 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004956 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004957 if (color_name == NULL)
4958 return FAIL;
4959
4960 guicolor = GUI_GET_COLOR(color_name);
4961 if (guicolor == INVALCOLOR)
4962 return FAIL;
4963
4964 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4965 }
4966
4967 if (n != 16 || li != NULL)
4968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004969 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004970 return FAIL;
4971 }
4972
4973 opt->jo_set2 |= JO2_ANSI_COLORS;
4974 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4975 }
4976# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004977#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004978 else if (STRCMP(hi->hi_key, "env") == 0)
4979 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004980 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004981 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004982 if (item->v_type != VAR_DICT)
4983 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004984 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02004985 return FAIL;
4986 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004987 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004988 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004989 if (opt->jo_env != NULL)
4990 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004991 }
4992 else if (STRCMP(hi->hi_key, "cwd") == 0)
4993 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004994 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004995 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004996 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02004997 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02004998#ifndef WIN32 // Win32 directories don't have the concept of "executable"
4999 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5000#endif
5001 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005003 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005004 return FAIL;
5005 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005006 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005007 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005008 else if (STRCMP(hi->hi_key, "waittime") == 0)
5009 {
5010 if (!(supported & JO_WAITTIME))
5011 break;
5012 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005013 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005014 }
5015 else if (STRCMP(hi->hi_key, "timeout") == 0)
5016 {
5017 if (!(supported & JO_TIMEOUT))
5018 break;
5019 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005020 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005021 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005022 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005023 {
5024 if (!(supported & JO_OUT_TIMEOUT))
5025 break;
5026 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005027 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005028 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005029 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005030 {
5031 if (!(supported & JO_ERR_TIMEOUT))
5032 break;
5033 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005034 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005035 }
5036 else if (STRCMP(hi->hi_key, "part") == 0)
5037 {
5038 if (!(supported & JO_PART))
5039 break;
5040 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005041 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005042 if (STRCMP(val, "err") == 0)
5043 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005044 else if (STRCMP(val, "out") == 0)
5045 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005046 else
5047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005048 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005049 return FAIL;
5050 }
5051 }
5052 else if (STRCMP(hi->hi_key, "id") == 0)
5053 {
5054 if (!(supported & JO_ID))
5055 break;
5056 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005057 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005058 }
5059 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5060 {
5061 if (!(supported & JO_STOPONEXIT))
5062 break;
5063 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005064 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005065 opt->jo_soe_buf);
5066 if (opt->jo_stoponexit == NULL)
5067 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005068 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005069 return FAIL;
5070 }
5071 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005072 else if (STRCMP(hi->hi_key, "block_write") == 0)
5073 {
5074 if (!(supported & JO_BLOCK_WRITE))
5075 break;
5076 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005077 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005078 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005079 else
5080 break;
5081 --todo;
5082 }
5083 if (todo > 0)
5084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005085 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005086 return FAIL;
5087 }
5088
5089 return OK;
5090}
5091
5092/*
5093 * Get the channel from the argument.
5094 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005095 * When "check_open" is TRUE check that the channel can be used.
5096 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005097 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005098 */
5099 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005100get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005101{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005102 channel_T *channel = NULL;
5103 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005104
5105 if (tv->v_type == VAR_JOB)
5106 {
5107 if (tv->vval.v_job != NULL)
5108 channel = tv->vval.v_job->jv_channel;
5109 }
5110 else if (tv->v_type == VAR_CHANNEL)
5111 {
5112 channel = tv->vval.v_channel;
5113 }
5114 else
5115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005116 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005117 return NULL;
5118 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005119 if (channel != NULL && reading)
5120 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005121 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005122
Bram Moolenaar437905c2016-04-26 19:01:05 +02005123 if (check_open && (channel == NULL || (!channel_is_open(channel)
5124 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005126 emsg(_("E906: not an open channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005127 return NULL;
5128 }
5129 return channel;
5130}
5131
5132static job_T *first_job = NULL;
5133
5134 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005135job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005136{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005137 int i;
5138
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005139 ch_log(job->jv_channel, "Freeing job");
5140 if (job->jv_channel != NULL)
5141 {
5142 /* The link from the channel to the job doesn't count as a reference,
5143 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005144 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005145 * NULL the reference. We don't set ch_job_killed, unreferencing the
5146 * job doesn't mean it stops running. */
5147 job->jv_channel->ch_job = NULL;
5148 channel_unref(job->jv_channel);
5149 }
5150 mch_clear_job(job);
5151
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005152 vim_free(job->jv_tty_in);
5153 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005154 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005155#ifdef UNIX
5156 vim_free(job->jv_termsig);
5157#endif
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005158 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005159 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005160 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005161 for (i = 0; job->jv_argv[i] != NULL; i++)
5162 vim_free(job->jv_argv[i]);
5163 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005164 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005165}
5166
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005167/*
5168 * Remove "job" from the list of jobs.
5169 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005170 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005171job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005172{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005173 if (job->jv_next != NULL)
5174 job->jv_next->jv_prev = job->jv_prev;
5175 if (job->jv_prev == NULL)
5176 first_job = job->jv_next;
5177 else
5178 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005179}
5180
5181 static void
5182job_free_job(job_T *job)
5183{
5184 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005185 vim_free(job);
5186}
5187
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005188 static void
5189job_free(job_T *job)
5190{
5191 if (!in_free_unref_items)
5192 {
5193 job_free_contents(job);
5194 job_free_job(job);
5195 }
5196}
5197
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005198job_T *jobs_to_free = NULL;
5199
5200/*
5201 * Put "job" in a list to be freed later, when it's no longer referenced.
5202 */
5203 static void
5204job_free_later(job_T *job)
5205{
5206 job_unlink(job);
5207 job->jv_next = jobs_to_free;
5208 jobs_to_free = job;
5209}
5210
5211 static void
5212free_jobs_to_free_later(void)
5213{
5214 job_T *job;
5215
5216 while (jobs_to_free != NULL)
5217 {
5218 job = jobs_to_free;
5219 jobs_to_free = job->jv_next;
5220 job_free_contents(job);
5221 vim_free(job);
5222 }
5223}
5224
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005225#if defined(EXITFREE) || defined(PROTO)
5226 void
5227job_free_all(void)
5228{
5229 while (first_job != NULL)
5230 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005231 free_jobs_to_free_later();
5232
5233# ifdef FEAT_TERMINAL
5234 free_unused_terminals();
5235# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005236}
5237#endif
5238
5239/*
5240 * Return TRUE if we need to check if the process of "job" has ended.
5241 */
5242 static int
5243job_need_end_check(job_T *job)
5244{
5245 return job->jv_status == JOB_STARTED
5246 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5247}
5248
5249/*
5250 * Return TRUE if the channel of "job" is still useful.
5251 */
5252 static int
5253job_channel_still_useful(job_T *job)
5254{
5255 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5256}
5257
5258/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005259 * Return TRUE if the channel of "job" is closeable.
5260 */
5261 static int
5262job_channel_can_close(job_T *job)
5263{
5264 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5265}
5266
5267/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005268 * Return TRUE if the job should not be freed yet. Do not free the job when
5269 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5270 * or when the associated channel will do something with the job output.
5271 */
5272 static int
5273job_still_useful(job_T *job)
5274{
5275 return job_need_end_check(job) || job_channel_still_useful(job);
5276}
5277
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005278#if defined(GUI_MAY_FORK) || defined(PROTO)
5279/*
5280 * Return TRUE when there is any running job that we care about.
5281 */
5282 int
5283job_any_running()
5284{
5285 job_T *job;
5286
5287 for (job = first_job; job != NULL; job = job->jv_next)
5288 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005289 {
5290 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005291 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005292 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005293 return FALSE;
5294}
5295#endif
5296
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005297#if !defined(USE_ARGV) || defined(PROTO)
5298/*
5299 * Escape one argument for an external command.
5300 * Returns the escaped string in allocated memory. NULL when out of memory.
5301 */
5302 static char_u *
5303win32_escape_arg(char_u *arg)
5304{
5305 int slen, dlen;
5306 int escaping = 0;
5307 int i;
5308 char_u *s, *d;
5309 char_u *escaped_arg;
5310 int has_spaces = FALSE;
5311
5312 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005313 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005314 dlen = slen;
5315 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5316 {
5317 if (*s == '"' || *s == '\\')
5318 ++dlen;
5319 if (*s == ' ' || *s == '\t')
5320 has_spaces = TRUE;
5321 }
5322
5323 if (has_spaces)
5324 dlen += 2;
5325
5326 if (dlen == slen)
5327 return vim_strsave(arg);
5328
5329 /* Allocate memory for the result and fill it. */
5330 escaped_arg = alloc(dlen + 1);
5331 if (escaped_arg == NULL)
5332 return NULL;
5333 memset(escaped_arg, 0, dlen+1);
5334
5335 d = escaped_arg;
5336
5337 if (has_spaces)
5338 *d++ = '"';
5339
5340 for (s = arg; *s != NUL;)
5341 {
5342 switch (*s)
5343 {
5344 case '"':
5345 for (i = 0; i < escaping; i++)
5346 *d++ = '\\';
5347 escaping = 0;
5348 *d++ = '\\';
5349 *d++ = *s++;
5350 break;
5351 case '\\':
5352 escaping++;
5353 *d++ = *s++;
5354 break;
5355 default:
5356 escaping = 0;
5357 MB_COPY_CHAR(s, d);
5358 break;
5359 }
5360 }
5361
5362 /* add terminating quote and finish with a NUL */
5363 if (has_spaces)
5364 {
5365 for (i = 0; i < escaping; i++)
5366 *d++ = '\\';
5367 *d++ = '"';
5368 }
5369 *d = NUL;
5370
5371 return escaped_arg;
5372}
5373
5374/*
5375 * Build a command line from a list, taking care of escaping.
5376 * The result is put in gap->ga_data.
5377 * Returns FAIL when out of memory.
5378 */
5379 int
5380win32_build_cmd(list_T *l, garray_T *gap)
5381{
5382 listitem_T *li;
5383 char_u *s;
5384
5385 for (li = l->lv_first; li != NULL; li = li->li_next)
5386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005387 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005388 if (s == NULL)
5389 return FAIL;
5390 s = win32_escape_arg(s);
5391 if (s == NULL)
5392 return FAIL;
5393 ga_concat(gap, s);
5394 vim_free(s);
5395 if (li->li_next != NULL)
5396 ga_append(gap, ' ');
5397 }
5398 return OK;
5399}
5400#endif
5401
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005402/*
5403 * NOTE: Must call job_cleanup() only once right after the status of "job"
5404 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5405 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005406 * If the job is no longer used it will be removed from the list of jobs, and
5407 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005408 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005409 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005410job_cleanup(job_T *job)
5411{
5412 if (job->jv_status != JOB_ENDED)
5413 return;
5414
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005415 /* Ready to cleanup the job. */
5416 job->jv_status = JOB_FINISHED;
5417
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005418 /* When only channel-in is kept open, close explicitly. */
5419 if (job->jv_channel != NULL)
5420 ch_close_part(job->jv_channel, PART_IN);
5421
Bram Moolenaar97792de2016-10-15 18:36:49 +02005422 if (job->jv_exit_cb != NULL)
5423 {
5424 typval_T argv[3];
5425 typval_T rettv;
5426 int dummy;
5427
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005428 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005429 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005430 ++job->jv_refcount;
5431 argv[0].v_type = VAR_JOB;
5432 argv[0].vval.v_job = job;
5433 argv[1].v_type = VAR_NUMBER;
5434 argv[1].vval.v_number = job->jv_exitval;
5435 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5436 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5437 job->jv_exit_partial, NULL);
5438 clear_tv(&rettv);
5439 --job->jv_refcount;
5440 channel_need_redraw = TRUE;
5441 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005442
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005443 // Do not free the job in case the close callback of the associated channel
5444 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005445 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005446 // The job was already unreferenced and the associated channel was
5447 // detached, now that it ended it can be freed. However, a caller might
5448 // still use it, thus free it a bit later.
5449 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005450}
5451
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005452/*
5453 * Mark references in jobs that are still useful.
5454 */
5455 int
5456set_ref_in_job(int copyID)
5457{
5458 int abort = FALSE;
5459 job_T *job;
5460 typval_T tv;
5461
5462 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005463 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005464 {
5465 tv.v_type = VAR_JOB;
5466 tv.vval.v_job = job;
5467 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5468 }
5469 return abort;
5470}
5471
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005472/*
5473 * Dereference "job". Note that after this "job" may have been freed.
5474 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005475 void
5476job_unref(job_T *job)
5477{
5478 if (job != NULL && --job->jv_refcount <= 0)
5479 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005480 /* Do not free the job if there is a channel where the close callback
5481 * may get the job info. */
5482 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005483 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005484 /* Do not free the job when it has not ended yet and there is a
5485 * "stoponexit" flag or an exit callback. */
5486 if (!job_need_end_check(job))
5487 {
5488 job_free(job);
5489 }
5490 else if (job->jv_channel != NULL)
5491 {
5492 /* Do remove the link to the channel, otherwise it hangs
5493 * around until Vim exits. See job_free() for refcount. */
5494 ch_log(job->jv_channel, "detaching channel from job");
5495 job->jv_channel->ch_job = NULL;
5496 channel_unref(job->jv_channel);
5497 job->jv_channel = NULL;
5498 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005499 }
5500 }
5501}
5502
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005503 int
5504free_unused_jobs_contents(int copyID, int mask)
5505{
5506 int did_free = FALSE;
5507 job_T *job;
5508
5509 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005510 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005511 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005512 {
5513 /* Free the channel and ordinary items it contains, but don't
5514 * recurse into Lists, Dictionaries etc. */
5515 job_free_contents(job);
5516 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005517 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005518 return did_free;
5519}
5520
5521 void
5522free_unused_jobs(int copyID, int mask)
5523{
5524 job_T *job;
5525 job_T *job_next;
5526
5527 for (job = first_job; job != NULL; job = job_next)
5528 {
5529 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005530 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005531 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005532 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005533 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005534 job_free_job(job);
5535 }
5536 }
5537}
5538
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005539/*
5540 * Allocate a job. Sets the refcount to one and sets options default.
5541 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005542 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005543job_alloc(void)
5544{
5545 job_T *job;
5546
5547 job = (job_T *)alloc_clear(sizeof(job_T));
5548 if (job != NULL)
5549 {
5550 job->jv_refcount = 1;
5551 job->jv_stoponexit = vim_strsave((char_u *)"term");
5552
5553 if (first_job != NULL)
5554 {
5555 first_job->jv_prev = job;
5556 job->jv_next = first_job;
5557 }
5558 first_job = job;
5559 }
5560 return job;
5561}
5562
5563 void
5564job_set_options(job_T *job, jobopt_T *opt)
5565{
5566 if (opt->jo_set & JO_STOPONEXIT)
5567 {
5568 vim_free(job->jv_stoponexit);
5569 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5570 job->jv_stoponexit = NULL;
5571 else
5572 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5573 }
5574 if (opt->jo_set & JO_EXIT_CB)
5575 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005576 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005577 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005578 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005579 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005580 job->jv_exit_partial = NULL;
5581 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005582 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005583 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005584 job->jv_exit_partial = opt->jo_exit_partial;
5585 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005586 {
5587 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005588 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005589 }
5590 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005591 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005592 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005593 func_ref(job->jv_exit_cb);
5594 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005595 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005596 }
5597}
5598
5599/*
5600 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5601 */
5602 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005603job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005604{
5605 job_T *job;
5606
5607 for (job = first_job; job != NULL; job = job->jv_next)
5608 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005609 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005610}
5611
5612/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005613 * Return TRUE when there is any job that has an exit callback and might exit,
5614 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005615 */
5616 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005617has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005618{
5619 job_T *job;
5620
5621 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005622 /* Only should check if the channel has been closed, if the channel is
5623 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005624 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5625 || (job->jv_status == JOB_FINISHED
5626 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005627 return TRUE;
5628 return FALSE;
5629}
5630
Bram Moolenaar97792de2016-10-15 18:36:49 +02005631#define MAX_CHECK_ENDED 8
5632
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005633/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005634 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005635 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005636 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005637 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005638job_check_ended(void)
5639{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005640 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005641 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005642
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005643 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005644 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005645 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005646
Bram Moolenaar97792de2016-10-15 18:36:49 +02005647 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005648 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005649 // NOTE: mch_detect_ended_job() must only return a job of which the
5650 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005651 job_T *job = mch_detect_ended_job(first_job);
5652
5653 if (job == NULL)
5654 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005655 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005656 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005657 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005658
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005659 // Actually free jobs that were cleaned up.
5660 free_jobs_to_free_later();
5661
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005662 if (channel_need_redraw)
5663 {
5664 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005665 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005666 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005667 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005668}
5669
5670/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005671 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005672 * "argv_arg" is only for Unix.
5673 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005674 * The returned job has a refcount of one.
5675 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005676 */
5677 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005678job_start(
5679 typval_T *argvars,
5680 char **argv_arg,
5681 jobopt_T *opt_arg,
5682 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005683{
5684 job_T *job;
5685 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005686 char **argv = NULL;
5687 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005688#if defined(UNIX)
5689# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005690 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005691#else
5692 garray_T ga;
5693#endif
5694 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005695 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005696
5697 job = job_alloc();
5698 if (job == NULL)
5699 return NULL;
5700
5701 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005702#ifndef USE_ARGV
5703 ga_init2(&ga, (int)sizeof(char*), 20);
5704#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005705
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005706 if (opt_arg != NULL)
5707 opt = *opt_arg;
5708 else
5709 {
5710 /* Default mode is NL. */
5711 clear_job_options(&opt);
5712 opt.jo_mode = MODE_NL;
5713 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005714 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5715 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5716 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005717 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005718 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005719
5720 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005721 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005722 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5723 && opt.jo_io[part] == JIO_FILE
5724 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5725 || *opt.jo_io_name[part] == NUL))
5726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005727 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005728 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005729 }
5730
5731 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5732 {
5733 buf_T *buf = NULL;
5734
5735 /* check that we can find the buffer before starting the job */
5736 if (opt.jo_set & JO_IN_BUF)
5737 {
5738 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5739 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005740 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005741 }
5742 else if (!(opt.jo_set & JO_IN_NAME))
5743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005744 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005745 }
5746 else
5747 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5748 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005749 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005750 if (buf->b_ml.ml_mfp == NULL)
5751 {
5752 char_u numbuf[NUMBUFLEN];
5753 char_u *s;
5754
5755 if (opt.jo_set & JO_IN_BUF)
5756 {
5757 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5758 s = numbuf;
5759 }
5760 else
5761 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005762 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005763 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005764 }
5765 job->jv_in_buf = buf;
5766 }
5767
5768 job_set_options(job, &opt);
5769
Bram Moolenaar13568252018-03-16 20:46:58 +01005770#ifdef USE_ARGV
5771 if (argv_arg != NULL)
5772 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005773 /* Make a copy of argv_arg for job->jv_argv. */
5774 for (i = 0; argv_arg[i] != NULL; i++)
5775 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005776 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005777 if (argv == NULL)
5778 goto theend;
5779 for (i = 0; i < argc; i++)
5780 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5781 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005782 }
5783 else
5784#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005785 if (argvars[0].v_type == VAR_STRING)
5786 {
5787 /* Command is a string. */
5788 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005789 if (cmd == NULL || *cmd == NUL)
5790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005791 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005792 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005793 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005794
5795 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005796 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005797 }
5798 else if (argvars[0].v_type != VAR_LIST
5799 || argvars[0].vval.v_list == NULL
5800 || argvars[0].vval.v_list->lv_len < 1)
5801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005802 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005803 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005804 }
5805 else
5806 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005807 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005808
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005809 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005810 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005811#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005812 if (win32_build_cmd(l, &ga) == FAIL)
5813 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005814 cmd = ga.ga_data;
5815#endif
5816 }
5817
Bram Moolenaar20608922018-04-21 22:30:08 +02005818 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005819 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005820
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005821#ifdef USE_ARGV
5822 if (ch_log_active())
5823 {
5824 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005825
5826 ga_init2(&ga, (int)sizeof(char), 200);
5827 for (i = 0; i < argc; ++i)
5828 {
5829 if (i > 0)
5830 ga_concat(&ga, (char_u *)" ");
5831 ga_concat(&ga, (char_u *)argv[i]);
5832 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005833 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005834 ga_clear(&ga);
5835 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005836 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005837#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005838 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005839 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005840#endif
5841
5842 /* If the channel is reading from a buffer, write lines now. */
5843 if (job->jv_channel != NULL)
5844 channel_write_in(job->jv_channel);
5845
5846theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005847#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005848 vim_free(ga.ga_data);
5849#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005850 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005851 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005852 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005853 return job;
5854}
5855
5856/*
5857 * Get the status of "job" and invoke the exit callback when needed.
5858 * The returned string is not allocated.
5859 */
5860 char *
5861job_status(job_T *job)
5862{
5863 char *result;
5864
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005865 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005866 /* No need to check, dead is dead. */
5867 result = "dead";
5868 else if (job->jv_status == JOB_FAILED)
5869 result = "fail";
5870 else
5871 {
5872 result = mch_job_status(job);
5873 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005874 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005875 }
5876 return result;
5877}
5878
Bram Moolenaar8950a562016-03-12 15:22:55 +01005879/*
5880 * Implementation of job_info().
5881 */
5882 void
5883job_info(job_T *job, dict_T *dict)
5884{
5885 dictitem_T *item;
5886 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005887 list_T *l;
5888 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005889
Bram Moolenaare0be1672018-07-08 16:50:37 +02005890 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005891
5892 item = dictitem_alloc((char_u *)"channel");
5893 if (item == NULL)
5894 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005895 item->di_tv.v_type = VAR_CHANNEL;
5896 item->di_tv.vval.v_channel = job->jv_channel;
5897 if (job->jv_channel != NULL)
5898 ++job->jv_channel->ch_refcount;
5899 if (dict_add(dict, item) == FAIL)
5900 dictitem_free(item);
5901
5902#ifdef UNIX
5903 nr = job->jv_pid;
5904#else
5905 nr = job->jv_proc_info.dwProcessId;
5906#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005907 dict_add_number(dict, "process", nr);
5908 dict_add_string(dict, "tty_in", job->jv_tty_in);
5909 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005910
Bram Moolenaare0be1672018-07-08 16:50:37 +02005911 dict_add_number(dict, "exitval", job->jv_exitval);
5912 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5913 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005914#ifdef UNIX
5915 dict_add_string(dict, "termsig", job->jv_termsig);
5916#endif
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005917
5918 l = list_alloc();
5919 if (l != NULL)
5920 {
5921 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005922 if (job->jv_argv != NULL)
5923 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005924 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005925 }
5926}
5927
5928/*
5929 * Implementation of job_info() to return info for all jobs.
5930 */
5931 void
5932job_info_all(list_T *l)
5933{
5934 job_T *job;
5935 typval_T tv;
5936
5937 for (job = first_job; job != NULL; job = job->jv_next)
5938 {
5939 tv.v_type = VAR_JOB;
5940 tv.vval.v_job = job;
5941
5942 if (list_append_tv(l, &tv) != OK)
5943 return;
5944 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005945}
5946
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005947/*
5948 * Send a signal to "job". Implements job_stop().
5949 * When "type" is not NULL use this for the type.
5950 * Otherwise use argvars[1] for the type.
5951 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005952 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005953job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005954{
5955 char_u *arg;
5956
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005957 if (type != NULL)
5958 arg = (char_u *)type;
5959 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005960 arg = (char_u *)"";
5961 else
5962 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005963 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005964 if (arg == NULL)
5965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005966 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005967 return 0;
5968 }
5969 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005970 if (job->jv_status == JOB_FAILED)
5971 {
5972 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5973 return 0;
5974 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005975 if (job->jv_status == JOB_ENDED)
5976 {
5977 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5978 return 0;
5979 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005980 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005981 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005982 return 0;
5983
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005984 /* Assume that only "kill" will kill the job. */
5985 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005986 job->jv_channel->ch_job_killed = TRUE;
5987
5988 /* We don't try freeing the job, obviously the caller still has a
5989 * reference to it. */
5990 return 1;
5991}
5992
Bram Moolenaarf2732452018-06-03 14:47:35 +02005993 void
5994invoke_prompt_callback(void)
5995{
5996 typval_T rettv;
5997 int dummy;
5998 typval_T argv[2];
5999 char_u *text;
6000 char_u *prompt;
6001 linenr_T lnum = curbuf->b_ml.ml_line_count;
6002
6003 // Add a new line for the prompt before invoking the callback, so that
6004 // text can always be inserted above the last line.
6005 ml_append(lnum, (char_u *)"", 0, FALSE);
6006 curwin->w_cursor.lnum = lnum + 1;
6007 curwin->w_cursor.col = 0;
6008
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006009 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006010 return;
6011 text = ml_get(lnum);
6012 prompt = prompt_text();
6013 if (STRLEN(text) >= STRLEN(prompt))
6014 text += STRLEN(prompt);
6015 argv[0].v_type = VAR_STRING;
6016 argv[0].vval.v_string = vim_strsave(text);
6017 argv[1].v_type = VAR_UNKNOWN;
6018
6019 call_func(curbuf->b_prompt_callback,
6020 (int)STRLEN(curbuf->b_prompt_callback),
6021 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
6022 curbuf->b_prompt_partial, NULL);
6023 clear_tv(&argv[0]);
6024 clear_tv(&rettv);
6025}
6026
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006027/*
6028 * Return TRUE when the interrupt callback was invoked.
6029 */
6030 int
6031invoke_prompt_interrupt(void)
6032{
6033 typval_T rettv;
6034 int dummy;
6035 typval_T argv[1];
6036
6037 if (curbuf->b_prompt_interrupt == NULL
6038 || *curbuf->b_prompt_interrupt == NUL)
6039 return FALSE;
6040 argv[0].v_type = VAR_UNKNOWN;
6041
6042 got_int = FALSE; // don't skip executing commands
6043 call_func(curbuf->b_prompt_interrupt,
6044 (int)STRLEN(curbuf->b_prompt_interrupt),
6045 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
6046 curbuf->b_prompt_int_partial, NULL);
6047 clear_tv(&rettv);
6048 return TRUE;
6049}
6050
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006051#endif /* FEAT_JOB_CHANNEL */