blob: 8f0577f9b62ec6d36155a3ee5a1b81936ff5d959 [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{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100119 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200135ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200141ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200153 {
154 if (part < PART_COUNT)
155 fprintf(log_fd, "%son %d(%s): ",
156 what, ch->ch_id, part_names[part]);
157 else
158 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
159 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100160 else
161 fprintf(log_fd, "%s: ", what);
162 }
163}
164
Bram Moolenaard0b65022016-03-06 21:50:33 +0100165static int did_log_msg = TRUE;
166
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200167#ifndef PROTO /* prototype is in vim.h */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200169ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100170{
171 if (log_fd != NULL)
172 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200173 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100174
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200175 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200176 va_start(ap, fmt);
177 vfprintf(log_fd, fmt, ap);
178 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100179 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100181 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 }
183}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200184#endif
185
186 static void
187ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200188#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
189 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200190#endif
191 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192
193 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200194ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195{
196 if (log_fd != NULL)
197 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200198 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100199
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200200 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200201 va_start(ap, fmt);
202 vfprintf(log_fd, fmt, ap);
203 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100204 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100205 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100206 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100207 }
208}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100209
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100210#ifdef _WIN32
211# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100212# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100213
214 static char *
215strerror_win32(int eno)
216{
217 static LPVOID msgbuf = NULL;
218 char_u *ptr;
219
220 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200221 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100222 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200223 msgbuf = NULL;
224 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100225 FormatMessage(
226 FORMAT_MESSAGE_ALLOCATE_BUFFER |
227 FORMAT_MESSAGE_FROM_SYSTEM |
228 FORMAT_MESSAGE_IGNORE_INSERTS,
229 NULL,
230 eno,
231 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
232 (LPTSTR) &msgbuf,
233 0,
234 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200235 if (msgbuf != NULL)
236 /* chomp \r or \n */
237 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
238 switch (*ptr)
239 {
240 case '\r':
241 STRMOVE(ptr, ptr + 1);
242 ptr--;
243 break;
244 case '\n':
245 if (*(ptr + 1) == '\0')
246 *ptr = '\0';
247 else
248 *ptr = ' ';
249 break;
250 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100251 return msgbuf;
252}
253#endif
254
Bram Moolenaar77073442016-02-13 23:23:53 +0100255/*
256 * The list of all allocated channels.
257 */
258static channel_T *first_channel = NULL;
259static int next_ch_id = 0;
260
261/*
262 * Allocate a new channel. The refcount is set to 1.
263 * The channel isn't actually used until it is opened.
264 * Returns NULL if out of memory.
265 */
266 channel_T *
267add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100268{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200269 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100270 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100271
Bram Moolenaar77073442016-02-13 23:23:53 +0100272 if (channel == NULL)
273 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100274
Bram Moolenaar77073442016-02-13 23:23:53 +0100275 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100276 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100277
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200278 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100279 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100280 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100281#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100282 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100283#endif
284#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100285 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100286#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100287 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100288 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100289
Bram Moolenaar77073442016-02-13 23:23:53 +0100290 if (first_channel != NULL)
291 {
292 first_channel->ch_prev = channel;
293 channel->ch_next = first_channel;
294 }
295 first_channel = channel;
296
297 channel->ch_refcount = 1;
298 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100299}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100300
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200301 int
302has_any_channel(void)
303{
304 return first_channel != NULL;
305}
306
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100307/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100308 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100309 * Return TRUE if "channel" has a callback and the associated job wasn't
310 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100311 */
312 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100313channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100314{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100315 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100316 int has_out_msg;
317 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100318
319 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100320 if (channel->ch_job_killed && channel->ch_job == NULL)
321 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100322
323 /* If there is a close callback it may still need to be invoked. */
324 if (channel->ch_close_cb != NULL)
325 return TRUE;
326
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200327 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200328 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200329 return TRUE;
330
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100331 /* If there is no callback then nobody can get readahead. If the fd is
332 * closed and there is no readahead then the callback won't be called. */
333 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100334 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
335 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100336 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
337 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
338 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
339 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
340 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
341 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100342 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100343 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200344 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200345 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
346 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200347 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200348 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
349 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100350}
351
352/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200353 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
354 */
355 static int
356channel_can_close(channel_T *channel)
357{
358 return channel->ch_to_be_closed == 0;
359}
360
361/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200362 * Close a channel and free all its resources.
363 */
364 static void
365channel_free_contents(channel_T *channel)
366{
367 channel_close(channel, TRUE);
368 channel_clear(channel);
369 ch_log(channel, "Freeing channel");
370}
371
372 static void
373channel_free_channel(channel_T *channel)
374{
375 if (channel->ch_next != NULL)
376 channel->ch_next->ch_prev = channel->ch_prev;
377 if (channel->ch_prev == NULL)
378 first_channel = channel->ch_next;
379 else
380 channel->ch_prev->ch_next = channel->ch_next;
381 vim_free(channel);
382}
383
384 static void
385channel_free(channel_T *channel)
386{
387 if (!in_free_unref_items)
388 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200389 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200390 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200391 else
392 {
393 channel_free_contents(channel);
394 channel_free_channel(channel);
395 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200396 }
397}
398
399/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100400 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100401 * possible, there is no callback to be invoked or the associated job was
402 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100403 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100404 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100405 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100406channel_may_free(channel_T *channel)
407{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100408 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100409 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100410 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100411 return TRUE;
412 }
413 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100414}
415
416/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100417 * Decrement the reference count on "channel" and maybe free it when it goes
418 * down to zero. Don't free it if there is a pending action.
419 * Returns TRUE when the channel is no longer referenced.
420 */
421 int
422channel_unref(channel_T *channel)
423{
424 if (channel != NULL && --channel->ch_refcount <= 0)
425 return channel_may_free(channel);
426 return FALSE;
427}
428
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200429 int
430free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100431{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200432 int did_free = FALSE;
433 channel_T *ch;
434
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200435 /* This is invoked from the garbage collector, which only runs at a safe
436 * point. */
437 ++safe_to_invoke_callback;
438
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200440 if (!channel_still_useful(ch)
441 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200442 {
443 /* Free the channel and ordinary items it contains, but don't
444 * recurse into Lists, Dictionaries etc. */
445 channel_free_contents(ch);
446 did_free = TRUE;
447 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200448
449 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200450 return did_free;
451}
452
453 void
454free_unused_channels(int copyID, int mask)
455{
456 channel_T *ch;
457 channel_T *ch_next;
458
459 for (ch = first_channel; ch != NULL; ch = ch_next)
460 {
461 ch_next = 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 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200465 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 channel_free_channel(ch);
467 }
468 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100469}
470
Bram Moolenaard04a0202016-01-26 23:30:18 +0100471#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100472
473#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
474 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100475channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100476{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100477 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200478 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100479
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100480 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100481 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200482 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100483 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200484 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100485}
486#endif
487
Bram Moolenaare0874f82016-01-24 20:36:41 +0100488/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100489 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100490 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100491#ifdef FEAT_GUI_X11
492 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200493messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200494 int *unused1 UNUSED,
495 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100497 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100500
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100502# if GTK_CHECK_VERSION(3,0,0)
503 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200504messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200505 GIOCondition unused2 UNUSED,
506 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100507{
508 channel_read_fd(GPOINTER_TO_INT(clientData));
509 return TRUE; /* Return FALSE instead in case the event source is to
510 * be removed after this function returns. */
511}
512# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200514messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200515 gint unused1 UNUSED,
516 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100518 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519}
Bram Moolenaar98921892016-02-23 17:14:37 +0100520# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100521#endif
522
523 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200524channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100525{
Bram Moolenaarde279892016-03-11 22:19:44 +0100526 if (!CH_HAS_GUI)
527 return;
528
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200529 /* gets stuck in handling events for a not connected channel */
530 if (channel->ch_keep_open)
531 return;
532
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100533# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200534 /* Tell notifier we are interested in being called when there is input on
535 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100536 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200537 {
538 ch_log(channel, "Registering part %s with fd %d",
539 part_names[part], channel->ch_part[part].ch_fd);
540
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100541 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100542 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100543 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100544 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200545 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100546 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200547 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100548# else
549# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200550 /* Tell gdk we are interested in being called when there is input on the
551 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100552 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100553 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200554 ch_log(channel, "Registering part %s with fd %d",
555 part_names[part], channel->ch_part[part].ch_fd);
556# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100557 GIOChannel *chnnl = g_io_channel_unix_new(
558 (gint)channel->ch_part[part].ch_fd);
559
560 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
561 chnnl,
562 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200563 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100564 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
565
566 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100567# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 channel->ch_part[part].ch_inputHandler = gdk_input_add(
569 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100570 (GdkInputCondition)
571 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200572 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100573 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100574# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200575 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100576# endif
577# endif
578}
579
Bram Moolenaarde279892016-03-11 22:19:44 +0100580 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100581channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100582{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100583 if (channel->CH_SOCK_FD != INVALID_FD)
584 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200585 if (channel->CH_OUT_FD != INVALID_FD
586 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100587 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200588 if (channel->CH_ERR_FD != INVALID_FD
589 && channel->CH_ERR_FD != channel->CH_SOCK_FD
590 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100591 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100592}
593
594/*
595 * Register any of our file descriptors with the GUI event handling system.
596 * Called when the GUI has started.
597 */
598 void
599channel_gui_register_all(void)
600{
Bram Moolenaar77073442016-02-13 23:23:53 +0100601 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100602
Bram Moolenaar77073442016-02-13 23:23:53 +0100603 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100604 channel_gui_register(channel);
605}
606
607 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200608channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100609{
610# ifdef FEAT_GUI_X11
611 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
612 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200613 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100614 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
615 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
616 }
617# else
618# ifdef FEAT_GUI_GTK
619 if (channel->ch_part[part].ch_inputHandler != 0)
620 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200621 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100622# if GTK_CHECK_VERSION(3,0,0)
623 g_source_remove(channel->ch_part[part].ch_inputHandler);
624# else
625 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
626# endif
627 channel->ch_part[part].ch_inputHandler = 0;
628 }
629# endif
630# endif
631}
632
633 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100634channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100635{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200636 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100637
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100638 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100639 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100640}
641
642#endif
643
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100644static char *e_cannot_connect = N_("E902: Cannot connect to port");
645
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100647 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100648 * "waittime" is the time in msec to wait for the connection.
649 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100650 * Returns the channel for success.
651 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100652 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100653 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100654channel_open(
655 char *hostname,
656 int port_in,
657 int waittime,
658 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100659{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100660 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100661 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100662 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100663#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100665 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100666#else
667 int port = port_in;
668#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100669 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100670 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100672#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673 channel_init_winsock();
674#endif
675
Bram Moolenaar77073442016-02-13 23:23:53 +0100676 channel = add_channel();
677 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100679 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100680 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681 }
682
683 /* Get the server internet address and put into addr structure */
684 /* fill in the socket address structure and connect to server */
685 vim_memset((char *)&server, 0, sizeof(server));
686 server.sin_family = AF_INET;
687 server.sin_port = htons(port);
688 if ((host = gethostbyname(hostname)) == NULL)
689 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100690 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200691 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100692 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100693 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100694 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100695 {
696 char *p;
697
Bram Moolenaar2e324952018-04-14 14:37:07 +0200698 /* When using host->h_addr_list[0] directly ubsan warns for it to not
699 * be aligned. First copy the pointer to avoid that. */
700 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100701 memcpy((char *)&server.sin_addr, p, host->h_length);
702 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100703
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100704 /* On Mac and Solaris a zero timeout almost never works. At least wait
705 * one millisecond. Let's do it for all systems, because we don't know why
706 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100707 if (waittime == 0)
708 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100709
710 /*
711 * For Unix we need to call connect() again after connect() failed.
712 * On Win32 one time is sufficient.
713 */
714 while (TRUE)
715 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100716 long elapsed_msec = 0;
717 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100718
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100719 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100720 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100721 sd = socket(AF_INET, SOCK_STREAM, 0);
722 if (sd == -1)
723 {
724 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200725 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100726 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100727 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100728 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100729
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100730 if (waittime >= 0)
731 {
732 /* Make connect() non-blocking. */
733 if (
734#ifdef _WIN32
735 ioctlsocket(sd, FIONBIO, &val) < 0
736#else
737 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
738#endif
739 )
740 {
741 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200742 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100743 "channel_open: Connect failed with errno %d", errno);
744 sock_close(sd);
745 channel_free(channel);
746 return NULL;
747 }
748 }
749
750 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200751 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100752 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
753
Bram Moolenaar045a2842016-03-08 22:33:07 +0100754 if (ret == 0)
755 /* The connection could be established. */
756 break;
757
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100758 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100759 if (waittime < 0 || (errno != EWOULDBLOCK
760 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100761#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100762 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100763#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100764 ))
765 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200766 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100767 "channel_open: Connect failed with errno %d", errno);
768 PERROR(_(e_cannot_connect));
769 sock_close(sd);
770 channel_free(channel);
771 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100772 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100773
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100774 /* Limit the waittime to 50 msec. If it doesn't work within this
775 * time we close the socket and try creating it again. */
776 waitnow = waittime > 50 ? 50 : waittime;
777
Bram Moolenaar045a2842016-03-08 22:33:07 +0100778 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100779 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100780#ifndef WIN32
781 if (errno != ECONNREFUSED)
782#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100783 {
784 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100785 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100786 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100787#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100788 int so_error = 0;
789 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100790 struct timeval start_tv;
791 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100792#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100793 FD_ZERO(&rfds);
794 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100795 FD_ZERO(&wfds);
796 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100797
Bram Moolenaar562ca712016-03-09 21:50:05 +0100798 tv.tv_sec = waitnow / 1000;
799 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100800#ifndef WIN32
801 gettimeofday(&start_tv, NULL);
802#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200803 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100804 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100805 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100806
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100807 if (ret < 0)
808 {
809 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200810 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100811 "channel_open: Connect failed with errno %d", errno);
812 PERROR(_(e_cannot_connect));
813 sock_close(sd);
814 channel_free(channel);
815 return NULL;
816 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100817
Bram Moolenaare081e212016-02-28 22:33:46 +0100818#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100819 /* On Win32: select() is expected to work and wait for up to
820 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100821 if (FD_ISSET(sd, &wfds))
822 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100823 elapsed_msec = waitnow;
824 if (waittime > 1 && elapsed_msec < waittime)
825 {
826 waittime -= elapsed_msec;
827 continue;
828 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100829#else
830 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100831 * After putting the socket in non-blocking mode, connect() will
832 * return EINPROGRESS, select() will not wait (as if writing is
833 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100834 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100835 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100836 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100837 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100838 {
839 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100840 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100841 if (ret < 0 || (so_error != 0
842 && so_error != EWOULDBLOCK
843 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100844# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100845 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100846# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100847 ))
848 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200849 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100850 "channel_open: Connect failed with errno %d",
851 so_error);
852 PERROR(_(e_cannot_connect));
853 sock_close(sd);
854 channel_free(channel);
855 return NULL;
856 }
857 }
858
Bram Moolenaar045a2842016-03-08 22:33:07 +0100859 if (FD_ISSET(sd, &wfds) && so_error == 0)
860 /* Did not detect an error, connection is established. */
861 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100862
Bram Moolenaar045a2842016-03-08 22:33:07 +0100863 gettimeofday(&end_tv, NULL);
864 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
865 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100866#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100867 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100868
869#ifndef WIN32
870 if (waittime > 1 && elapsed_msec < waittime)
871 {
872 /* The port isn't ready but we also didn't get an error.
873 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100874 * yet. Select() may return early, wait until the remaining
875 * "waitnow" and try again. */
876 waitnow -= elapsed_msec;
877 waittime -= elapsed_msec;
878 if (waitnow > 0)
879 {
880 mch_delay((long)waitnow, TRUE);
881 ui_breakcheck();
882 waittime -= waitnow;
883 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100884 if (!got_int)
885 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100886 if (waittime <= 0)
887 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100888 waittime = 1;
889 continue;
890 }
891 /* we were interrupted, behave as if timed out */
892 }
893#endif
894
895 /* We timed out. */
896 ch_error(channel, "Connection timed out");
897 sock_close(sd);
898 channel_free(channel);
899 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100900 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100901
Bram Moolenaar045a2842016-03-08 22:33:07 +0100902 ch_log(channel, "Connection made");
903
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100904 if (waittime >= 0)
905 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100906#ifdef _WIN32
907 val = 0;
908 ioctlsocket(sd, FIONBIO, &val);
909#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100910 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100911#endif
912 }
913
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100914 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100915 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100916 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
917 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200918 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100919
920#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100921 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100922#endif
923
Bram Moolenaar77073442016-02-13 23:23:53 +0100924 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100925}
926
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100927/*
928 * Implements ch_open().
929 */
930 channel_T *
931channel_open_func(typval_T *argvars)
932{
933 char_u *address;
934 char_u *p;
935 char *rest;
936 int port;
937 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200938 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100940 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100941 if (argvars[1].v_type != VAR_UNKNOWN
942 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100944 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945 return NULL;
946 }
947
948 /* parse address */
949 p = vim_strchr(address, ':');
950 if (p == NULL)
951 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100952 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100953 return NULL;
954 }
955 *p++ = NUL;
956 port = strtol((char *)p, &rest, 10);
957 if (*address == NUL || port <= 0 || *rest != NUL)
958 {
959 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100960 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100961 return NULL;
962 }
963
964 /* parse options */
965 clear_job_options(&opt);
966 opt.jo_mode = MODE_JSON;
967 opt.jo_timeout = 2000;
968 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200969 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200970 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100971 if (opt.jo_timeout < 0)
972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100973 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200974 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100975 }
976
977 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
978 if (channel != NULL)
979 {
980 opt.jo_set = JO_ALL;
981 channel_set_options(channel, &opt);
982 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200983theend:
984 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100985 return channel;
986}
987
Bram Moolenaarde279892016-03-11 22:19:44 +0100988 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200989ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100990{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200991 sock_T *fd = &channel->ch_part[part].ch_fd;
992
Bram Moolenaarde279892016-03-11 22:19:44 +0100993 if (*fd != INVALID_FD)
994 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200995 if (part == PART_SOCK)
996 sock_close(*fd);
997 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200998 {
999 /* When using a pty the same FD is set on multiple parts, only
1000 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001001 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1002 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1003 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001004 {
1005#ifdef WIN32
1006 if (channel->ch_named_pipe)
1007 DisconnectNamedPipe((HANDLE)fd);
1008#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001009 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001010 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001011 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001012 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001013
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001014 /* channel is closed, may want to end the job if it was the last */
1015 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001016 }
1017}
1018
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001019 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001020channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001021{
Bram Moolenaarde279892016-03-11 22:19:44 +01001022 if (in != INVALID_FD)
1023 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001024 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001025 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001026# if defined(UNIX)
1027 /* Do not end the job when all output channels are closed, wait until
1028 * the job ended. */
1029 if (isatty(in))
1030 channel->ch_to_be_closed |= (1U << PART_IN);
1031# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001032 }
1033 if (out != INVALID_FD)
1034 {
1035# if defined(FEAT_GUI)
1036 channel_gui_unregister_one(channel, PART_OUT);
1037# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001038 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001039 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001040 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001041# if defined(FEAT_GUI)
1042 channel_gui_register_one(channel, PART_OUT);
1043# endif
1044 }
1045 if (err != INVALID_FD)
1046 {
1047# if defined(FEAT_GUI)
1048 channel_gui_unregister_one(channel, PART_ERR);
1049# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001050 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001051 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001052 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001053# if defined(FEAT_GUI)
1054 channel_gui_register_one(channel, PART_ERR);
1055# endif
1056 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001057}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001058
Bram Moolenaard6051b52016-02-28 15:49:03 +01001059/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001060 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001061 * This does not keep a refcount, when the job is freed ch_job is cleared.
1062 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001063 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001064channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001065{
Bram Moolenaar77073442016-02-13 23:23:53 +01001066 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001067
1068 channel_set_options(channel, options);
1069
1070 if (job->jv_in_buf != NULL)
1071 {
1072 chanpart_T *in_part = &channel->ch_part[PART_IN];
1073
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001074 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001075 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001076 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001077 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001078 {
1079 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1080 {
1081 /* Special mode: send last-but-one line when appending a line
1082 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001083 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001084 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001085 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001086 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001087 }
1088 else
1089 in_part->ch_buf_top = options->jo_in_top;
1090 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001091 else
1092 in_part->ch_buf_top = 1;
1093 if (options->jo_set & JO_IN_BOT)
1094 in_part->ch_buf_bot = options->jo_in_bot;
1095 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001096 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001097 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001098}
1099
1100/*
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001101 * Prepare buffer "buf" for writing channel output to.
1102 */
1103 static void
1104prepare_buffer(buf_T *buf)
1105{
1106 buf_T *save_curbuf = curbuf;
1107
1108 buf_copy_options(buf, BCO_ENTER);
1109 curbuf = buf;
1110#ifdef FEAT_QUICKFIX
1111 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1112 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1113#endif
1114 if (curbuf->b_ml.ml_mfp == NULL)
1115 ml_open(curbuf);
1116 curbuf = save_curbuf;
1117}
1118
1119/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001120 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001121 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001122 */
1123 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001124find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001125{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001126 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001127 buf_T *save_curbuf = curbuf;
1128
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001129 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001130 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001131 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001132 if (buf == NULL)
1133 buf = buflist_findname_exp(name);
1134 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001135 if (buf == NULL)
1136 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001137 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001138 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001139 if (buf == NULL)
1140 return NULL;
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001141 prepare_buffer(buf);
1142
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001143 curbuf = buf;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001144 if (msg)
1145 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001146 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001147 changed_bytes(1, 0);
1148 curbuf = save_curbuf;
1149 }
1150
1151 return buf;
1152}
1153
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001154 static void
1155set_callback(
1156 char_u **cbp,
1157 partial_T **pp,
1158 char_u *callback,
1159 partial_T *partial)
1160{
1161 free_callback(*cbp, *pp);
1162 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001163 {
1164 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001165 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001166 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001167 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001168 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001169 func_ref(*cbp);
1170 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001171 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001172 else
1173 *cbp = NULL;
1174 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001175 if (partial != NULL)
1176 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001177}
1178
Bram Moolenaar187db502016-02-27 14:44:26 +01001179/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001180 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001181 */
1182 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001183channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001184{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001185 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001186
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001187 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001188 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001189 channel->ch_part[part].ch_mode = opt->jo_mode;
1190 if (opt->jo_set & JO_IN_MODE)
1191 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1192 if (opt->jo_set & JO_OUT_MODE)
1193 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1194 if (opt->jo_set & JO_ERR_MODE)
1195 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001196 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001197
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001198 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001199 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001200 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1201 if (opt->jo_set & JO_OUT_TIMEOUT)
1202 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1203 if (opt->jo_set & JO_ERR_TIMEOUT)
1204 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001205 if (opt->jo_set & JO_BLOCK_WRITE)
1206 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001207
1208 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001209 set_callback(&channel->ch_callback, &channel->ch_partial,
1210 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001211 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001212 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1213 &channel->ch_part[PART_OUT].ch_partial,
1214 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001215 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001216 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1217 &channel->ch_part[PART_ERR].ch_partial,
1218 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001219 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001220 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1221 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001222 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001223
1224 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1225 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001226 buf_T *buf;
1227
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001228 /* writing output to a buffer. Default mode is NL. */
1229 if (!(opt->jo_set & JO_OUT_MODE))
1230 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001231 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001232 {
1233 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1234 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001236 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001237 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001238 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001239 int msg = TRUE;
1240
1241 if (opt->jo_set2 & JO2_OUT_MSG)
1242 msg = opt->jo_message[PART_OUT];
1243 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 }
1245 if (buf != NULL)
1246 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001247 if (opt->jo_set & JO_OUT_MODIFIABLE)
1248 channel->ch_part[PART_OUT].ch_nomodifiable =
1249 !opt->jo_modifiable[PART_OUT];
1250
1251 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001253 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001254 }
1255 else
1256 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001257 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001258 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001259 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001260 // if the buffer was deleted or unloaded resurrect it
1261 if (buf->b_ml.ml_mfp == NULL)
1262 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001263 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001264 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001265 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001266
1267 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1268 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1269 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1270 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001271 buf_T *buf;
1272
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001273 /* writing err to a buffer. Default mode is NL. */
1274 if (!(opt->jo_set & JO_ERR_MODE))
1275 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1276 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001277 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001278 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001279 {
1280 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1281 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001282 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001283 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001284 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001285 {
1286 int msg = TRUE;
1287
1288 if (opt->jo_set2 & JO2_ERR_MSG)
1289 msg = opt->jo_message[PART_ERR];
1290 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1291 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001292 if (buf != NULL)
1293 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001294 if (opt->jo_set & JO_ERR_MODIFIABLE)
1295 channel->ch_part[PART_ERR].ch_nomodifiable =
1296 !opt->jo_modifiable[PART_ERR];
1297 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001300 }
1301 else
1302 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001303 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001304 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001305 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001306 // if the buffer was deleted or unloaded resurrect it
1307 if (buf->b_ml.ml_mfp == NULL)
1308 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001309 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001310 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001311 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001312
1313 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1314 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1315 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001316}
1317
1318/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001319 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001320 */
1321 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001322channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001323 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001324 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001325 char_u *callback,
1326 partial_T *partial,
1327 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001328{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001329 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001330 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1331
1332 if (item != NULL)
1333 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001334 item->cq_partial = partial;
1335 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001336 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001337 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001338 item->cq_callback = callback;
1339 }
1340 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001341 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001342 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001343 func_ref(item->cq_callback);
1344 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001345 item->cq_seq_nr = id;
1346 item->cq_prev = head->cq_prev;
1347 head->cq_prev = item;
1348 item->cq_next = NULL;
1349 if (item->cq_prev == NULL)
1350 head->cq_next = item;
1351 else
1352 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001353 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001354}
1355
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001356 static void
1357write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1358{
1359 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001360 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001361 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001362 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001363
Bram Moolenaar655da312016-05-28 22:22:34 +02001364 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001365 if ((p = alloc(len + 2)) == NULL)
1366 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001367 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001368
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001369 if (channel->ch_write_text_mode)
1370 p[len] = CAR;
1371 else
1372 {
1373 for (i = 0; i < len; ++i)
1374 if (p[i] == NL)
1375 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001376
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001377 p[len] = NL;
1378 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001379 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001380 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001381 vim_free(p);
1382}
1383
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001384/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001385 * Return TRUE if "channel" can be written to.
1386 * Returns FALSE if the input is closed or the write would block.
1387 */
1388 static int
1389can_write_buf_line(channel_T *channel)
1390{
1391 chanpart_T *in_part = &channel->ch_part[PART_IN];
1392
1393 if (in_part->ch_fd == INVALID_FD)
1394 return FALSE; /* pipe was closed */
1395
1396 /* for testing: block every other attempt to write */
1397 if (in_part->ch_block_write == 1)
1398 in_part->ch_block_write = -1;
1399 else if (in_part->ch_block_write == -1)
1400 in_part->ch_block_write = 1;
1401
1402 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1403#ifndef WIN32
1404 {
1405# if defined(HAVE_SELECT)
1406 struct timeval tval;
1407 fd_set wfds;
1408 int ret;
1409
1410 FD_ZERO(&wfds);
1411 FD_SET((int)in_part->ch_fd, &wfds);
1412 tval.tv_sec = 0;
1413 tval.tv_usec = 0;
1414 for (;;)
1415 {
1416 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1417# ifdef EINTR
1418 SOCK_ERRNO;
1419 if (ret == -1 && errno == EINTR)
1420 continue;
1421# endif
1422 if (ret <= 0 || in_part->ch_block_write == 1)
1423 {
1424 if (ret > 0)
1425 ch_log(channel, "FAKED Input not ready for writing");
1426 else
1427 ch_log(channel, "Input not ready for writing");
1428 return FALSE;
1429 }
1430 break;
1431 }
1432# else
1433 struct pollfd fds;
1434
1435 fds.fd = in_part->ch_fd;
1436 fds.events = POLLOUT;
1437 if (poll(&fds, 1, 0) <= 0)
1438 {
1439 ch_log(channel, "Input not ready for writing");
1440 return FALSE;
1441 }
1442 if (in_part->ch_block_write == 1)
1443 {
1444 ch_log(channel, "FAKED Input not ready for writing");
1445 return FALSE;
1446 }
1447# endif
1448 }
1449#endif
1450 return TRUE;
1451}
1452
1453/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001454 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001455 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001456 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001457channel_write_in(channel_T *channel)
1458{
1459 chanpart_T *in_part = &channel->ch_part[PART_IN];
1460 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001461 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001462 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001463
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001464 if (buf == NULL || in_part->ch_buf_append)
1465 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001466 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001467 {
1468 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001469 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001470 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001471 return;
1472 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001473
1474 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1475 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1476 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001477 if (!can_write_buf_line(channel))
1478 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001479 write_buf_line(buf, lnum, channel);
1480 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001481 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001482
1483 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001484 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001485 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001486 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001487
Bram Moolenaar014069a2016-03-03 22:51:40 +01001488 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001489 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001490 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001491#if defined(FEAT_TERMINAL)
1492 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001493 if (channel->ch_job != NULL)
1494 term_send_eof(channel);
1495#endif
1496
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001497 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001498 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001499 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001500
1501 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001502 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001503 }
1504 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001505 ch_log(channel, "Still %ld more lines to write",
1506 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001507}
1508
1509/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001510 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001511 */
1512 void
1513channel_buffer_free(buf_T *buf)
1514{
1515 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001516 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001517
1518 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001519 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001520 {
1521 chanpart_T *ch_part = &channel->ch_part[part];
1522
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001523 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001524 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001525 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001526 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001527 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001528 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001529 }
1530}
1531
1532/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001533 * Write any lines waiting to be written to "channel".
1534 */
1535 static void
1536channel_write_input(channel_T *channel)
1537{
1538 chanpart_T *in_part = &channel->ch_part[PART_IN];
1539
1540 if (in_part->ch_writeque.wq_next != NULL)
1541 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1542 else if (in_part->ch_bufref.br_buf != NULL)
1543 {
1544 if (in_part->ch_buf_append)
1545 channel_write_new_lines(in_part->ch_bufref.br_buf);
1546 else
1547 channel_write_in(channel);
1548 }
1549}
1550
1551/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001552 * Write any lines waiting to be written to a channel.
1553 */
1554 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001555channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001556{
1557 channel_T *channel;
1558
1559 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001560 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001561}
1562
1563/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001564 * Write appended lines above the last one in "buf" to the channel.
1565 */
1566 void
1567channel_write_new_lines(buf_T *buf)
1568{
1569 channel_T *channel;
1570 int found_one = FALSE;
1571
1572 /* There could be more than one channel for the buffer, loop over all of
1573 * them. */
1574 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1575 {
1576 chanpart_T *in_part = &channel->ch_part[PART_IN];
1577 linenr_T lnum;
1578 int written = 0;
1579
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001580 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001581 {
1582 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001583 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001584 found_one = TRUE;
1585 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1586 ++lnum)
1587 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001588 if (!can_write_buf_line(channel))
1589 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001590 write_buf_line(buf, lnum, channel);
1591 ++written;
1592 }
1593
1594 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001595 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001596 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001597 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001598 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001599 ch_log(channel, "Still %ld more lines to write",
1600 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001601
1602 in_part->ch_buf_bot = lnum;
1603 }
1604 }
1605 if (!found_one)
1606 buf->b_write_to_channel = FALSE;
1607}
1608
1609/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001611 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001612 */
1613 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001614invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1615 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001616{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001617 typval_T rettv;
1618 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001619
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001620 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001621 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001622
Bram Moolenaar77073442016-02-13 23:23:53 +01001623 argv[0].v_type = VAR_CHANNEL;
1624 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001625
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001626 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1627 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001628 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001629 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001630}
1631
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001632/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001633 * Return the first node from "channel"/"part" without removing it.
1634 * Returns NULL if there is nothing.
1635 */
1636 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001637channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001638{
1639 readq_T *head = &channel->ch_part[part].ch_head;
1640
1641 return head->rq_next;
1642}
1643
1644/*
1645 * Return a pointer to the first NL in "node".
1646 * Skips over NUL characters.
1647 * Returns NULL if there is no NL.
1648 */
1649 char_u *
1650channel_first_nl(readq_T *node)
1651{
1652 char_u *buffer = node->rq_buffer;
1653 long_u i;
1654
1655 for (i = 0; i < node->rq_buflen; ++i)
1656 if (buffer[i] == NL)
1657 return buffer + i;
1658 return NULL;
1659}
1660
1661/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001662 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001663 * The caller must free it.
1664 * Returns NULL if there is nothing.
1665 */
1666 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001668{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001669 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001670 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001671 char_u *p;
1672
Bram Moolenaar77073442016-02-13 23:23:53 +01001673 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001674 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001675 if (outlen != NULL)
1676 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001678 p = node->rq_buffer;
1679 head->rq_next = node->rq_next;
1680 if (node->rq_next == NULL)
1681 head->rq_prev = NULL;
1682 else
1683 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001684 vim_free(node);
1685 return p;
1686}
1687
1688/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001689 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001690 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001691 */
1692 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001693channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001694{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001695 readq_T *head = &channel->ch_part[part].ch_head;
1696 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001697 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001698 char_u *res;
1699 char_u *p;
1700
1701 /* If there is only one buffer just get that one. */
1702 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 return channel_get(channel, part, outlen);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001704
1705 /* Concatenate everything into one buffer. */
1706 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001707 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001708 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001709 if (res == NULL)
1710 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001711 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001712 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001713 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001714 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001715 p += node->rq_buflen;
1716 }
1717 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001718
1719 /* Free all buffers */
1720 do
1721 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001723 vim_free(p);
1724 } while (p != NULL);
1725
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 if (outlen != NULL)
1727 {
1728 *outlen += len;
1729 return res;
1730 }
1731
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001732 /* turn all NUL into NL */
1733 while (len > 0)
1734 {
1735 --len;
1736 if (res[len] == NUL)
1737 res[len] = NL;
1738 }
1739
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001740 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001741}
1742
1743/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001744 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001745 * Caller must check these bytes are available.
1746 */
1747 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001748channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001749{
1750 readq_T *head = &channel->ch_part[part].ch_head;
1751 readq_T *node = head->rq_next;
1752 char_u *buf = node->rq_buffer;
1753
1754 mch_memmove(buf, buf + len, node->rq_buflen - len);
1755 node->rq_buflen -= len;
1756}
1757
1758/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001759 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001760 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001761 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001762 */
1763 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001764channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001765{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001766 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001767 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001768 readq_T *last_node;
1769 readq_T *n;
1770 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001771 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001772 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001773
Bram Moolenaar77073442016-02-13 23:23:53 +01001774 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001775 return FAIL;
1776
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001777 last_node = node->rq_next;
1778 len = node->rq_buflen + last_node->rq_buflen + 1;
1779 if (want_nl)
1780 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001781 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001782 {
1783 last_node = last_node->rq_next;
1784 len += last_node->rq_buflen;
1785 }
1786
1787 p = newbuf = alloc(len);
1788 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001789 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001790 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001791 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001792 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001793 node->rq_buffer = newbuf;
1794 for (n = node; n != last_node; )
1795 {
1796 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001797 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001798 p += n->rq_buflen;
1799 vim_free(n->rq_buffer);
1800 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001801 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001802
1803 /* dispose of the collapsed nodes and their buffers */
1804 for (n = node->rq_next; n != last_node; )
1805 {
1806 n = n->rq_next;
1807 vim_free(n->rq_prev);
1808 }
1809 node->rq_next = last_node->rq_next;
1810 if (last_node->rq_next == NULL)
1811 head->rq_prev = node;
1812 else
1813 last_node->rq_next->rq_prev = node;
1814 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001815 return OK;
1816}
1817
1818/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001819 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001820 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001821 * Returns OK or FAIL.
1822 */
1823 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001824channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001825 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001826{
1827 readq_T *node;
1828 readq_T *head = &channel->ch_part[part].ch_head;
1829 char_u *p;
1830 int i;
1831
1832 node = (readq_T *)alloc(sizeof(readq_T));
1833 if (node == NULL)
1834 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001835 /* A NUL is added at the end, because netbeans code expects that.
1836 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001837 node->rq_buffer = alloc(len + 1);
1838 if (node->rq_buffer == NULL)
1839 {
1840 vim_free(node);
1841 return FAIL; /* out of memory */
1842 }
1843
1844 if (channel->ch_part[part].ch_mode == MODE_NL)
1845 {
1846 /* Drop any CR before a NL. */
1847 p = node->rq_buffer;
1848 for (i = 0; i < len; ++i)
1849 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1850 *p++ = buf[i];
1851 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001852 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001853 }
1854 else
1855 {
1856 mch_memmove(node->rq_buffer, buf, len);
1857 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001858 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001859 }
1860
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001861 if (prepend)
1862 {
1863 /* preend node to the head of the queue */
1864 node->rq_next = head->rq_next;
1865 node->rq_prev = NULL;
1866 if (head->rq_next == NULL)
1867 head->rq_prev = node;
1868 else
1869 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001871 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001872 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001873 {
1874 /* append node to the tail of the queue */
1875 node->rq_next = NULL;
1876 node->rq_prev = head->rq_prev;
1877 if (head->rq_prev == NULL)
1878 head->rq_next = node;
1879 else
1880 head->rq_prev->rq_next = node;
1881 head->rq_prev = node;
1882 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001883
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001884 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001885 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001886 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001887 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001888 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001889 fprintf(log_fd, "'\n");
1890 }
1891 return OK;
1892}
1893
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001894/*
1895 * Try to fill the buffer of "reader".
1896 * Returns FALSE when nothing was added.
1897 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001898 static int
1899channel_fill(js_read_T *reader)
1900{
1901 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001902 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001903 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001904 int keeplen;
1905 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001906 char_u *p;
1907
1908 if (next == NULL)
1909 return FALSE;
1910
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001911 keeplen = reader->js_end - reader->js_buf;
1912 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001913 {
1914 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001915 addlen = (int)STRLEN(next);
1916 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001917 if (p == NULL)
1918 {
1919 vim_free(next);
1920 return FALSE;
1921 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001922 mch_memmove(p, reader->js_buf, keeplen);
1923 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001924 vim_free(next);
1925 next = p;
1926 }
1927
1928 vim_free(reader->js_buf);
1929 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001930 return TRUE;
1931}
1932
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001933/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001934 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001935 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001936 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001937 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001938 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001939channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001940{
1941 js_read_T reader;
1942 typval_T listtv;
1943 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001944 chanpart_T *chanpart = &channel->ch_part[part];
1945 jsonq_T *head = &chanpart->ch_json_head;
1946 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001947 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001948
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001949 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001950 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001951
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001952 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001953 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001954 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001955 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001956 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001957
1958 /* When a message is incomplete we wait for a short while for more to
1959 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001960 * or list will make us hang.
1961 * Do not generate error messages, they will be written in a channel log. */
1962 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001963 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001964 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001965 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001966 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001967 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001968 /* Only accept the response when it is a list with at least two
1969 * items. */
1970 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001971 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001972 if (listtv.v_type != VAR_LIST)
1973 ch_error(channel, "Did not receive a list, discarding");
1974 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001975 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001976 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001977 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001978 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001979 else
1980 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001981 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1982 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001983 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001984 else
1985 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001986 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001987 item->jq_value = alloc_tv();
1988 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001989 {
1990 vim_free(item);
1991 clear_tv(&listtv);
1992 }
1993 else
1994 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001995 *item->jq_value = listtv;
1996 item->jq_prev = head->jq_prev;
1997 head->jq_prev = item;
1998 item->jq_next = NULL;
1999 if (item->jq_prev == NULL)
2000 head->jq_next = item;
2001 else
2002 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002003 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002004 }
2005 }
2006 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002007
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002008 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002009 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002010 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002011 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002012 size_t buflen = STRLEN(reader.js_buf);
2013
2014 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002015 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002016 /* First time encountering incomplete message or after receiving
2017 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002018 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002019 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002020 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002021 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002022 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002023#ifdef WIN32
2024 chanpart->ch_deadline = GetTickCount() + 100L;
2025#else
2026 gettimeofday(&chanpart->ch_deadline, NULL);
2027 chanpart->ch_deadline.tv_usec += 100 * 1000;
2028 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2029 {
2030 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2031 ++chanpart->ch_deadline.tv_sec;
2032 }
2033#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002034 }
2035 else
2036 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002037 int timeout;
2038#ifdef WIN32
2039 timeout = GetTickCount() > chanpart->ch_deadline;
2040#else
2041 {
2042 struct timeval now_tv;
2043
2044 gettimeofday(&now_tv, NULL);
2045 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2046 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2047 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2048 }
2049#endif
2050 if (timeout)
2051 {
2052 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002053 chanpart->ch_wait_len = 0;
2054 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002055 }
2056 else
2057 {
2058 reader.js_used = 0;
2059 ch_log(channel, "still waiting on incomplete message");
2060 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002061 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002062 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002063
2064 if (status == FAIL)
2065 {
2066 ch_error(channel, "Decoding failed - discarding input");
2067 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002068 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002069 }
2070 else if (reader.js_buf[reader.js_used] != NUL)
2071 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002072 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002073 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002074 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2075 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002076 ret = status == MAYBE ? FALSE: TRUE;
2077 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002078 else
2079 ret = FALSE;
2080
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002081 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002082 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002083}
2084
2085/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002086 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002087 */
2088 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002089remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002090{
Bram Moolenaar77073442016-02-13 23:23:53 +01002091 if (node->cq_prev == NULL)
2092 head->cq_next = node->cq_next;
2093 else
2094 node->cq_prev->cq_next = node->cq_next;
2095 if (node->cq_next == NULL)
2096 head->cq_prev = node->cq_prev;
2097 else
2098 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002099}
2100
2101/*
2102 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002103 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002104 */
2105 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002106remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002107{
Bram Moolenaar77073442016-02-13 23:23:53 +01002108 if (node->jq_prev == NULL)
2109 head->jq_next = node->jq_next;
2110 else
2111 node->jq_prev->jq_next = node->jq_next;
2112 if (node->jq_next == NULL)
2113 head->jq_prev = node->jq_prev;
2114 else
2115 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002116 vim_free(node);
2117}
2118
2119/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002120 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002121 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002122 * When "id" is zero or negative jut get the first message. But not the one
2123 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002124 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002125 * Return OK when found and return the value in "rettv".
2126 * Return FAIL otherwise.
2127 */
2128 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002129channel_get_json(
2130 channel_T *channel,
2131 ch_part_T part,
2132 int id,
2133 int without_callback,
2134 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002135{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002136 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002137 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002138
Bram Moolenaar77073442016-02-13 23:23:53 +01002139 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002140 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002141 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002142 typval_T *tv = &l->lv_first->li_tv;
2143
Bram Moolenaar958dc692016-12-01 15:34:12 +01002144 if ((without_callback || !item->jq_no_callback)
2145 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002146 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002147 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002148 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002149 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002150 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002151 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002152 ch_log(channel, "Getting JSON message %ld",
2153 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002154 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002155 return OK;
2156 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002157 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002158 }
2159 return FAIL;
2160}
2161
Bram Moolenaar958dc692016-12-01 15:34:12 +01002162/*
2163 * Put back "rettv" into the JSON queue, there was no callback for it.
2164 * Takes over the values in "rettv".
2165 */
2166 static void
2167channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2168{
2169 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2170 jsonq_T *item = head->jq_next;
2171 jsonq_T *newitem;
2172
2173 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2174 /* last item was pushed back, append to the end */
2175 item = NULL;
2176 else while (item != NULL && item->jq_no_callback)
2177 /* append after the last item that was pushed back */
2178 item = item->jq_next;
2179
2180 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2181 if (newitem == NULL)
2182 clear_tv(rettv);
2183 else
2184 {
2185 newitem->jq_value = alloc_tv();
2186 if (newitem->jq_value == NULL)
2187 {
2188 vim_free(newitem);
2189 clear_tv(rettv);
2190 }
2191 else
2192 {
2193 newitem->jq_no_callback = FALSE;
2194 *newitem->jq_value = *rettv;
2195 if (item == NULL)
2196 {
2197 /* append to the end */
2198 newitem->jq_prev = head->jq_prev;
2199 head->jq_prev = newitem;
2200 newitem->jq_next = NULL;
2201 if (newitem->jq_prev == NULL)
2202 head->jq_next = newitem;
2203 else
2204 newitem->jq_prev->jq_next = newitem;
2205 }
2206 else
2207 {
2208 /* append after "item" */
2209 newitem->jq_prev = item;
2210 newitem->jq_next = item->jq_next;
2211 item->jq_next = newitem;
2212 if (newitem->jq_next == NULL)
2213 head->jq_prev = newitem;
2214 else
2215 newitem->jq_next->jq_prev = newitem;
2216 }
2217 }
2218 }
2219}
2220
Bram Moolenaarece61b02016-02-20 21:39:05 +01002221#define CH_JSON_MAX_ARGS 4
2222
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002223/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002224 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002225 * "argv[0]" is the command string.
2226 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002227 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002228 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002229channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002230{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002231 char_u *cmd = argv[0].vval.v_string;
2232 char_u *arg;
2233 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002234
Bram Moolenaarece61b02016-02-20 21:39:05 +01002235 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002236 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002237 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002238 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002239 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002240 return;
2241 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002242 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002243 if (arg == NULL)
2244 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002245
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002246 if (STRCMP(cmd, "ex") == 0)
2247 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002248 int save_called_emsg = called_emsg;
2249
2250 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002251 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002252 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002253 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002254 --emsg_silent;
2255 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002256 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002257 (char *)get_vim_var_str(VV_ERRMSG));
2258 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002259 }
2260 else if (STRCMP(cmd, "normal") == 0)
2261 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002262 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002263
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002264 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002265 ea.arg = arg;
2266 ea.addr_count = 0;
2267 ea.forceit = TRUE; /* no mapping */
2268 ex_normal(&ea);
2269 }
2270 else if (STRCMP(cmd, "redraw") == 0)
2271 {
2272 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002273
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002274 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002275 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002276 ex_redraw(&ea);
2277 showruler(FALSE);
2278 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002279 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002280 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002281 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002282 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002283 int is_call = cmd[0] == 'c';
2284 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002285
Bram Moolenaarece61b02016-02-20 21:39:05 +01002286 if (argv[id_idx].v_type != VAR_UNKNOWN
2287 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002288 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002289 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002290 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002291 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002292 }
2293 else if (is_call && argv[2].v_type != VAR_LIST)
2294 {
2295 ch_error(channel, "third argument for call must be a list");
2296 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002297 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002298 }
2299 else
2300 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002301 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002302 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002303 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002304 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002305
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002306 /* Don't pollute the display with errors. */
2307 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002308 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002309 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002310 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002311 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002312 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002313 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002314 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002315 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002316 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2317 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002318 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002319
2320 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002321 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002322 int id = argv[id_idx].vval.v_number;
2323
Bram Moolenaar55fab432016-02-07 16:53:13 +01002324 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002325 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002326 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002327 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002328 /* If evaluation failed or the result can't be encoded
2329 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002330 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002331 err_tv.v_type = VAR_STRING;
2332 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002333 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002334 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002335 if (json != NULL)
2336 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002337 channel_send(channel,
2338 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002339 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002340 vim_free(json);
2341 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002342 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002343 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002344 if (tv == &res_tv)
2345 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002346 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002347 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002348 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002349 }
2350 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002351 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002352 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002354 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002355}
2356
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002357/*
2358 * Invoke the callback at "cbhead".
2359 * Does not redraw but sets channel_need_redraw.
2360 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002361 static void
2362invoke_one_time_callback(
2363 channel_T *channel,
2364 cbq_T *cbhead,
2365 cbq_T *item,
2366 typval_T *argv)
2367{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002368 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002369 (char *)item->cq_callback);
2370 /* Remove the item from the list first, if the callback
2371 * invokes ch_close() the list will be cleared. */
2372 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002373 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002374 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002375 vim_free(item);
2376}
2377
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002378 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002379append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002380{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002381 bufref_T save_curbuf = {NULL, 0, 0};
2382 win_T *save_curwin = NULL;
2383 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002384 linenr_T lnum = buffer->b_ml.ml_line_count;
2385 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002386 chanpart_T *ch_part = &channel->ch_part[part];
2387 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002388 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002389
2390 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2391 {
2392 if (!ch_part->ch_nomod_error)
2393 {
2394 ch_error(channel, "Buffer is not modifiable, cannot append");
2395 ch_part->ch_nomod_error = TRUE;
2396 }
2397 return;
2398 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002399
2400 /* If the buffer is also used as input insert above the last
2401 * line. Don't write these lines. */
2402 if (save_write_to)
2403 {
2404 --lnum;
2405 buffer->b_write_to_channel = FALSE;
2406 }
2407
2408 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002409 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002410
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002411 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002412
2413 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2414 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2415
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002416 u_sync(TRUE);
2417 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002418 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002419
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002420 if (empty)
2421 {
2422 /* The buffer is empty, replace the first (dummy) line. */
2423 ml_replace(lnum, msg, TRUE);
2424 lnum = 0;
2425 }
2426 else
2427 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002428 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002429
2430 /* Restore curbuf/curwin/curtab */
2431 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2432
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002433 if (ch_part->ch_nomodifiable)
2434 buffer->b_p_ma = FALSE;
2435 else
2436 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002437
2438 if (buffer->b_nwindows > 0)
2439 {
2440 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002441
2442 FOR_ALL_WINDOWS(wp)
2443 {
2444 if (wp->w_buffer == buffer
2445 && (save_write_to
2446 ? wp->w_cursor.lnum == lnum + 1
2447 : (wp->w_cursor.lnum == lnum
2448 && wp->w_cursor.col == 0)))
2449 {
2450 ++wp->w_cursor.lnum;
2451 save_curwin = curwin;
2452 curwin = wp;
2453 curbuf = curwin->w_buffer;
2454 scroll_cursor_bot(0, FALSE);
2455 curwin = save_curwin;
2456 curbuf = curwin->w_buffer;
2457 }
2458 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002459 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002460 channel_need_redraw = TRUE;
2461 }
2462
2463 if (save_write_to)
2464 {
2465 channel_T *ch;
2466
2467 /* Find channels reading from this buffer and adjust their
2468 * next-to-read line number. */
2469 buffer->b_write_to_channel = TRUE;
2470 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2471 {
2472 chanpart_T *in_part = &ch->ch_part[PART_IN];
2473
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002474 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002475 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2476 }
2477 }
2478}
2479
Bram Moolenaar437905c2016-04-26 19:01:05 +02002480 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002481drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002482{
2483 char_u *msg;
2484
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002485 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002486 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002487 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002488 vim_free(msg);
2489 }
2490}
2491
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002492/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002493 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002494 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002495 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002496 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002497 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002498may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002499{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002500 char_u *msg = NULL;
2501 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002502 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002503 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002504 chanpart_T *ch_part = &channel->ch_part[part];
2505 ch_mode_T ch_mode = ch_part->ch_mode;
2506 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002507 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002508 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002509 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002510 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002511 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002512
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002513 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002514 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002515 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002516
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002517 /* Use a message-specific callback, part callback or channel callback */
2518 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2519 if (cbitem->cq_seq_nr == 0)
2520 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002521 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002522 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002523 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002524 partial = cbitem->cq_partial;
2525 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002526 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002527 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002528 callback = ch_part->ch_callback;
2529 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002530 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002532 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002533 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002534 partial = channel->ch_partial;
2535 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002536
Bram Moolenaarec68a992016-10-03 21:37:41 +02002537 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002538 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2539 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002540 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002541 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002542 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002543 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002544 buffer = NULL;
2545 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002546
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002547 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002548 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002549 listitem_T *item;
2550 int argc = 0;
2551
Bram Moolenaard7ece102016-02-02 23:23:02 +01002552 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002553 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002554 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002555 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002556 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002557 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002558 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002559 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002560
Bram Moolenaarece61b02016-02-20 21:39:05 +01002561 for (item = listtv->vval.v_list->lv_first;
2562 item != NULL && argc < CH_JSON_MAX_ARGS;
2563 item = item->li_next)
2564 argv[argc++] = item->li_tv;
2565 while (argc < CH_JSON_MAX_ARGS)
2566 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002567
Bram Moolenaarece61b02016-02-20 21:39:05 +01002568 if (argv[0].v_type == VAR_STRING)
2569 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002570 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002571 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002572 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002573 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002574 }
2575
Bram Moolenaarece61b02016-02-20 21:39:05 +01002576 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002577 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002578 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002579 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002580 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002581 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002582 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002583 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002584 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002585 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002586 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002587 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002588 return FALSE;
2589 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002590 else
2591 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002592 /* If there is no callback or buffer drop the message. */
2593 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002594 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002595 /* If there is a close callback it may use ch_read() to get the
2596 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002597 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002598 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002599 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002600 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002601
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002602 if (ch_mode == MODE_NL)
2603 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002604 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002605 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002606 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002607
2608 /* See if we have a message ending in NL in the first buffer. If
2609 * not try to concatenate the first and the second buffer. */
2610 while (TRUE)
2611 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002612 node = channel_peek(channel, part);
2613 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002614 if (nl != NULL)
2615 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002616 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002617 {
2618 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2619 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002620 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002621 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002622 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002623 buf = node->rq_buffer;
2624
Bram Moolenaarec68a992016-10-03 21:37:41 +02002625 if (nl == NULL)
2626 {
2627 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002628 char_u *new_buf;
2629
2630 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2631 if (new_buf == NULL)
2632 /* This might fail over and over again, should the message
2633 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002634 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002635 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002636 node->rq_buffer = buf;
2637 nl = buf + node->rq_buflen++;
2638 *nl = NUL;
2639 }
2640
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002641 /* Convert NUL to NL, the internal representation. */
2642 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2643 if (*p == NUL)
2644 *p = NL;
2645
2646 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002647 {
2648 /* get the whole buffer, drop the NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002649 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002650 *nl = NUL;
2651 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002652 else
2653 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002654 /* Copy the message into allocated memory (excluding the NL)
2655 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002656 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002657 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002658 }
2659 }
2660 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002661 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002662 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002663 * get everything we have.
2664 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002665 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002666 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002667
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002668 if (msg == NULL)
2669 return FALSE; /* out of memory (and avoids Coverity warning) */
2670
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002671 argv[1].v_type = VAR_STRING;
2672 argv[1].vval.v_string = msg;
2673 }
2674
Bram Moolenaara07fec92016-02-05 21:04:08 +01002675 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002676 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002677 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002678
Bram Moolenaar958dc692016-12-01 15:34:12 +01002679 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002680 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002681 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002682 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002683 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002684 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002685 break;
2686 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002687 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002688 {
2689 if (channel->ch_drop_never)
2690 {
2691 /* message must be read with ch_read() */
2692 channel_push_json(channel, part, listtv);
2693 listtv = NULL;
2694 }
2695 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002696 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002697 seq_nr);
2698 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002699 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002700 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002701 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002702 if (buffer != NULL)
2703 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002704 if (msg == NULL)
2705 /* JSON or JS mode: re-encode the message. */
2706 msg = json_encode(listtv, ch_mode);
2707 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002708 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002709#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002710 if (buffer->b_term != NULL)
2711 write_to_term(buffer, msg, channel);
2712 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002713#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002714 append_to_buffer(buffer, msg, channel, part);
2715 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002716 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002717
Bram Moolenaar187db502016-02-27 14:44:26 +01002718 if (callback != NULL)
2719 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002720 if (cbitem != NULL)
2721 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2722 else
2723 {
2724 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002725 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002726 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002727 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002728 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002729 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002730 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002731 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002732 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002733
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002734 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002735 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002736 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002737
2738 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002739}
2740
2741/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002742 * Return TRUE when channel "channel" is open for writing to.
2743 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002744 */
2745 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002746channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002747{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002748 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002749 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002750}
2751
2752/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002753 * Return TRUE when channel "channel" is open for reading or writing.
2754 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002755 */
2756 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002757channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002758{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002759 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002760 || channel->CH_IN_FD != INVALID_FD
2761 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002762 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002763}
2764
2765/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002766 * Return TRUE if "channel" has JSON or other typeahead.
2767 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002768 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002769channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002770{
2771 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2772
2773 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2774 {
2775 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2776 jsonq_T *item = head->jq_next;
2777
2778 return item != NULL;
2779 }
2780 return channel_peek(channel, part) != NULL;
2781}
2782
2783/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002784 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002785 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002786 */
2787 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002788channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002789{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002790 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002791 int has_readahead = FALSE;
2792
Bram Moolenaar77073442016-02-13 23:23:53 +01002793 if (channel == NULL)
2794 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002795 if (req_part == PART_OUT)
2796 {
2797 if (channel->CH_OUT_FD != INVALID_FD)
2798 return "open";
2799 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002800 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002801 }
2802 else if (req_part == PART_ERR)
2803 {
2804 if (channel->CH_ERR_FD != INVALID_FD)
2805 return "open";
2806 if (channel_has_readahead(channel, PART_ERR))
2807 has_readahead = TRUE;
2808 }
2809 else
2810 {
2811 if (channel_is_open(channel))
2812 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002813 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002814 if (channel_has_readahead(channel, part))
2815 {
2816 has_readahead = TRUE;
2817 break;
2818 }
2819 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002820
2821 if (has_readahead)
2822 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002823 return "closed";
2824}
2825
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002826 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002827channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002828{
2829 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002830 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002831 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002832 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002833 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002834
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002835 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002836 STRCAT(namebuf, "_");
2837 tail = STRLEN(namebuf);
2838
2839 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002840 if (chanpart->ch_fd != INVALID_FD)
2841 status = "open";
2842 else if (channel_has_readahead(channel, part))
2843 status = "buffered";
2844 else
2845 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002846 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002847
2848 STRCPY(namebuf + tail, "mode");
2849 switch (chanpart->ch_mode)
2850 {
2851 case MODE_NL: s = "NL"; break;
2852 case MODE_RAW: s = "RAW"; break;
2853 case MODE_JSON: s = "JSON"; break;
2854 case MODE_JS: s = "JS"; break;
2855 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002856 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002857
2858 STRCPY(namebuf + tail, "io");
2859 if (part == PART_SOCK)
2860 s = "socket";
2861 else switch (chanpart->ch_io)
2862 {
2863 case JIO_NULL: s = "null"; break;
2864 case JIO_PIPE: s = "pipe"; break;
2865 case JIO_FILE: s = "file"; break;
2866 case JIO_BUFFER: s = "buffer"; break;
2867 case JIO_OUT: s = "out"; break;
2868 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002869 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002870
2871 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002872 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002873}
2874
2875 void
2876channel_info(channel_T *channel, dict_T *dict)
2877{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002878 dict_add_number(dict, "id", channel->ch_id);
2879 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002880
2881 if (channel->ch_hostname != NULL)
2882 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002883 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2884 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002885 channel_part_info(channel, dict, "sock", PART_SOCK);
2886 }
2887 else
2888 {
2889 channel_part_info(channel, dict, "out", PART_OUT);
2890 channel_part_info(channel, dict, "err", PART_ERR);
2891 channel_part_info(channel, dict, "in", PART_IN);
2892 }
2893}
2894
Bram Moolenaar77073442016-02-13 23:23:53 +01002895/*
2896 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002897 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002898 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002899 */
2900 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002901channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002902{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002903 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002904
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002905#ifdef FEAT_GUI
2906 channel_gui_unregister(channel);
2907#endif
2908
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002909 ch_close_part(channel, PART_SOCK);
2910 ch_close_part(channel, PART_IN);
2911 ch_close_part(channel, PART_OUT);
2912 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002913
Bram Moolenaara9f02812017-08-05 17:40:38 +02002914 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002915 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002916 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002917
Bram Moolenaara9f02812017-08-05 17:40:38 +02002918 /* Invoke callbacks and flush buffers before the close callback. */
2919 if (channel->ch_close_cb != NULL)
2920 ch_log(channel,
2921 "Invoking callbacks and flushing buffers before closing");
2922 for (part = PART_SOCK; part < PART_IN; ++part)
2923 {
2924 if (channel->ch_close_cb != NULL
2925 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2926 {
2927 /* Increment the refcount to avoid the channel being freed
2928 * halfway. */
2929 ++channel->ch_refcount;
2930 if (channel->ch_close_cb == NULL)
2931 ch_log(channel, "flushing %s buffers before closing",
2932 part_names[part]);
2933 while (may_invoke_callback(channel, part))
2934 ;
2935 --channel->ch_refcount;
2936 }
2937 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002938
Bram Moolenaara9f02812017-08-05 17:40:38 +02002939 if (channel->ch_close_cb != NULL)
2940 {
2941 typval_T argv[1];
2942 typval_T rettv;
2943 int dummy;
2944
2945 /* Increment the refcount to avoid the channel being freed
2946 * halfway. */
2947 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002948 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002949 (char *)channel->ch_close_cb);
2950 argv[0].v_type = VAR_CHANNEL;
2951 argv[0].vval.v_channel = channel;
2952 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002953 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002954 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002955 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002956 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002957
Bram Moolenaara9f02812017-08-05 17:40:38 +02002958 /* the callback is only called once */
2959 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2960 channel->ch_close_cb = NULL;
2961 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002962
Bram Moolenaara9f02812017-08-05 17:40:38 +02002963 if (channel_need_redraw)
2964 {
2965 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002966 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002967 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002968
Bram Moolenaara9f02812017-08-05 17:40:38 +02002969 if (!channel->ch_drop_never)
2970 /* any remaining messages are useless now */
2971 for (part = PART_SOCK; part < PART_IN; ++part)
2972 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002973
2974 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002975 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002976 }
2977
2978 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002979
2980#ifdef FEAT_TERMINAL
2981 term_channel_closed(channel);
2982#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002983}
2984
Bram Moolenaard04a0202016-01-26 23:30:18 +01002985/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002986 * Close the "in" part channel "channel".
2987 */
2988 void
2989channel_close_in(channel_T *channel)
2990{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002991 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002992}
2993
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002994 static void
2995remove_from_writeque(writeq_T *wq, writeq_T *entry)
2996{
2997 ga_clear(&entry->wq_ga);
2998 wq->wq_next = entry->wq_next;
2999 if (wq->wq_next == NULL)
3000 wq->wq_prev = NULL;
3001 else
3002 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003003 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003004}
3005
Bram Moolenaar0874a832016-09-01 15:11:51 +02003006/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003007 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003008 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003009 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003010channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003011{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003012 chanpart_T *ch_part = &channel->ch_part[part];
3013 jsonq_T *json_head = &ch_part->ch_json_head;
3014 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003015
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003016 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003017 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003018
3019 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003020 {
3021 cbq_T *node = cb_head->cq_next;
3022
3023 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003024 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003025 vim_free(node);
3026 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003027
3028 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003029 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003030 free_tv(json_head->jq_next->jq_value);
3031 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003032 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003033
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003034 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3035 ch_part->ch_callback = NULL;
3036 ch_part->ch_partial = NULL;
3037
3038 while (ch_part->ch_writeque.wq_next != NULL)
3039 remove_from_writeque(&ch_part->ch_writeque,
3040 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003041}
3042
3043/*
3044 * Clear all the read buffers on "channel".
3045 */
3046 void
3047channel_clear(channel_T *channel)
3048{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003049 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003050 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003051 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003052 channel_clear_one(channel, PART_OUT);
3053 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003054 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003055 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003056 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003057 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003058 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003059 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003060 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003061}
3062
Bram Moolenaar77073442016-02-13 23:23:53 +01003063#if defined(EXITFREE) || defined(PROTO)
3064 void
3065channel_free_all(void)
3066{
3067 channel_T *channel;
3068
Bram Moolenaard6051b52016-02-28 15:49:03 +01003069 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003070 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3071 channel_clear(channel);
3072}
3073#endif
3074
3075
Bram Moolenaar715d2852016-04-30 17:06:31 +02003076/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003077#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003078
3079/* Buffer size for reading incoming messages. */
3080#define MAXMSGSIZE 4096
3081
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003082#if defined(HAVE_SELECT)
3083/*
3084 * Add write fds where we are waiting for writing to be possible.
3085 */
3086 static int
3087channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3088{
3089 int maxfd = maxfd_arg;
3090 channel_T *ch;
3091
3092 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3093 {
3094 chanpart_T *in_part = &ch->ch_part[PART_IN];
3095
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003096 if (in_part->ch_fd != INVALID_FD
3097 && (in_part->ch_bufref.br_buf != NULL
3098 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003099 {
3100 FD_SET((int)in_part->ch_fd, wfds);
3101 if ((int)in_part->ch_fd >= maxfd)
3102 maxfd = (int)in_part->ch_fd + 1;
3103 }
3104 }
3105 return maxfd;
3106}
3107#else
3108/*
3109 * Add write fds where we are waiting for writing to be possible.
3110 */
3111 static int
3112channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3113{
3114 int nfd = nfd_in;
3115 channel_T *ch;
3116
3117 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3118 {
3119 chanpart_T *in_part = &ch->ch_part[PART_IN];
3120
Bram Moolenaar683b7962017-08-19 15:51:59 +02003121 if (in_part->ch_fd != INVALID_FD
3122 && (in_part->ch_bufref.br_buf != NULL
3123 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003124 {
3125 in_part->ch_poll_idx = nfd;
3126 fds[nfd].fd = in_part->ch_fd;
3127 fds[nfd].events = POLLOUT;
3128 ++nfd;
3129 }
3130 else
3131 in_part->ch_poll_idx = -1;
3132 }
3133 return nfd;
3134}
3135#endif
3136
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003137typedef enum {
3138 CW_READY,
3139 CW_NOT_READY,
3140 CW_ERROR
3141} channel_wait_result;
3142
Bram Moolenaard04a0202016-01-26 23:30:18 +01003143/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003144 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003145 * Return CW_READY when there is something to read.
3146 * Return CW_NOT_READY when there is nothing to read.
3147 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003148 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003149 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003150channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003151{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003152 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003153 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003154
Bram Moolenaard8070362016-02-15 21:56:54 +01003155# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003156 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003157 {
3158 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003159 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003160 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003161 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003162
3163 /* reading from a pipe, not a socket */
3164 while (TRUE)
3165 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003166 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3167
3168 if (r && nread > 0)
3169 return CW_READY;
3170 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003171 {
3172 DWORD err = GetLastError();
3173
3174 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3175 return CW_ERROR;
3176
3177 if (channel->ch_named_pipe)
3178 {
3179 DisconnectNamedPipe((HANDLE)fd);
3180 ConnectNamedPipe((HANDLE)fd, NULL);
3181 }
3182 else
3183 return CW_ERROR;
3184 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003185
3186 /* perhaps write some buffer lines */
3187 channel_write_any_lines();
3188
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003189 sleep_time = deadline - GetTickCount();
3190 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003191 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003192 /* Wait for a little while. Very short at first, up to 10 msec
3193 * after looping a few times. */
3194 if (sleep_time > delay)
3195 sleep_time = delay;
3196 Sleep(sleep_time);
3197 delay = delay * 2;
3198 if (delay > 10)
3199 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003200 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003201 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003202 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003203#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003204 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003205#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003206 struct timeval tval;
3207 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003208 fd_set wfds;
3209 int ret;
3210 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003211
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003212 tval.tv_sec = timeout / 1000;
3213 tval.tv_usec = (timeout % 1000) * 1000;
3214 for (;;)
3215 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003216 FD_ZERO(&rfds);
3217 FD_SET((int)fd, &rfds);
3218
3219 /* Write lines to a pipe when a pipe can be written to. Need to
3220 * set this every time, some buffers may be done. */
3221 maxfd = (int)fd + 1;
3222 FD_ZERO(&wfds);
3223 maxfd = channel_fill_wfds(maxfd, &wfds);
3224
3225 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003226# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003227 SOCK_ERRNO;
3228 if (ret == -1 && errno == EINTR)
3229 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003230# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003231 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003232 {
3233 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003234 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003235 channel_write_any_lines();
3236 continue;
3237 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003238 break;
3239 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003240#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003241 for (;;)
3242 {
3243 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3244 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003245
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003246 fds[0].fd = fd;
3247 fds[0].events = POLLIN;
3248 nfd = channel_fill_poll_write(nfd, fds);
3249 if (poll(fds, nfd, timeout) > 0)
3250 {
3251 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003252 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003253 channel_write_any_lines();
3254 continue;
3255 }
3256 break;
3257 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003258#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003259 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003260 return CW_NOT_READY;
3261}
3262
3263 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003264ch_close_part_on_error(
3265 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003266{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003267 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003268
3269 if (is_err)
3270 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003271 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003272 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003273 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003274
3275 /* Queue a "DETACH" netbeans message in the command queue in order to
3276 * terminate the netbeans session later. Do not end the session here
3277 * directly as we may be running in the context of a call to
3278 * netbeans_parse_messages():
3279 * netbeans_parse_messages
3280 * -> autocmd triggered while processing the netbeans cmd
3281 * -> ui_breakcheck
3282 * -> gui event loop or select loop
3283 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003284 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003285 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003286 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003287 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003288 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3289
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003290 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003291 * close the channel yet, there may be something to read on another part.
3292 * When stdout and stderr use the same FD we get the error only on one of
3293 * them, also close the other. */
3294 if (part == PART_OUT || part == PART_ERR)
3295 {
3296 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3297
3298 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3299 ch_close_part(channel, other);
3300 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003301 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003302
3303#ifdef FEAT_GUI
3304 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003305 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003306#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003307}
3308
3309 static void
3310channel_close_now(channel_T *channel)
3311{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003312 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003313 if (channel->ch_nb_close_cb != NULL)
3314 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003315 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003316}
3317
3318/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003319 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003320 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003321 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003322 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003323 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003324channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003325{
3326 static char_u *buf = NULL;
3327 int len = 0;
3328 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003329 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003330 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003331
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003332 fd = channel->ch_part[part].ch_fd;
3333 if (fd == INVALID_FD)
3334 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003335 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003336 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003337 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003338 }
3339 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003340
3341 /* Allocate a buffer to read into. */
3342 if (buf == NULL)
3343 {
3344 buf = alloc(MAXMSGSIZE);
3345 if (buf == NULL)
3346 return; /* out of memory! */
3347 }
3348
3349 /* Keep on reading for as long as there is something to read.
3350 * Use select() or poll() to avoid blocking on a message that is exactly
3351 * MAXMSGSIZE long. */
3352 for (;;)
3353 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003354 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003355 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003356 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003357 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003358 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003359 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003360 if (len <= 0)
3361 break; /* error or nothing more to read */
3362
3363 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003364 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003365 readlen += len;
3366 if (len < MAXMSGSIZE)
3367 break; /* did read everything that's available */
3368 }
3369
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003370 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003371 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003372 {
3373 if (!channel->ch_keep_open)
3374 ch_close_part_on_error(channel, part, (len < 0), func);
3375 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003376#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003377 else if (CH_HAS_GUI && gtk_main_level() > 0)
3378 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003379 gtk_main_quit();
3380#endif
3381}
3382
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003383/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003384 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003385 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003386 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003387 * Returns what was read in allocated memory.
3388 * Returns NULL in case of error or timeout.
3389 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003390 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003391channel_read_block(
3392 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003393{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003394 char_u *buf;
3395 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003396 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003397 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003398 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003399 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003400
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003401 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003402 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003403
3404 while (TRUE)
3405 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003406 node = channel_peek(channel, part);
3407 if (node != NULL)
3408 {
3409 if (mode == MODE_RAW || (mode == MODE_NL
3410 && channel_first_nl(node) != NULL))
3411 /* got a complete message */
3412 break;
3413 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3414 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003415 /* If not blocking or nothing more is coming then return what we
3416 * have. */
3417 if (raw || fd == INVALID_FD)
3418 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003419 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003420
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003421 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003422 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003423 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003424 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003425 {
3426 ch_log(channel, "Timed out");
3427 return NULL;
3428 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003429 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003430 }
3431
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003432 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003433 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003434 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003435 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003436 }
3437 else
3438 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003439 char_u *p;
3440
3441 buf = node->rq_buffer;
3442 nl = channel_first_nl(node);
3443
3444 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003445 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003446 if (*p == NUL)
3447 *p = NL;
3448
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003449 if (nl == NULL)
3450 {
3451 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003452 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003453 }
3454 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003455 {
3456 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003457 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003458 *nl = NUL;
3459 }
3460 else
3461 {
3462 /* Copy the message into allocated memory and remove it from the
3463 * buffer. */
3464 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003465 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003466 }
3467 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003468 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003469 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003470 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003471}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003472
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003473/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003474 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003475 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003476 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003477 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003478 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003479 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003480channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003481 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003482 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003483 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003484 int id,
3485 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003486{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003487 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003488 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003489 int timeout;
3490 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003491
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003492 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003493 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003494 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003495 for (;;)
3496 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003497 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003498
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003499 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003500 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003501 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003502 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003503 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003504 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003505
Bram Moolenaard7ece102016-02-02 23:23:02 +01003506 if (!more)
3507 {
3508 /* Handle any other messages in the queue. If done some more
3509 * messages may have arrived. */
3510 if (channel_parse_messages())
3511 continue;
3512
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003513 /* Wait for up to the timeout. If there was an incomplete message
3514 * use the deadline for that. */
3515 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003516 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003517 {
3518#ifdef WIN32
3519 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3520#else
3521 {
3522 struct timeval now_tv;
3523
3524 gettimeofday(&now_tv, NULL);
3525 timeout = (chanpart->ch_deadline.tv_sec
3526 - now_tv.tv_sec) * 1000
3527 + (chanpart->ch_deadline.tv_usec
3528 - now_tv.tv_usec) / 1000
3529 + 1;
3530 }
3531#endif
3532 if (timeout < 0)
3533 {
3534 /* Something went wrong, channel_parse_json() didn't
3535 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003536 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003537 timeout = timeout_arg;
3538 }
3539 else if (timeout > timeout_arg)
3540 timeout = timeout_arg;
3541 }
3542 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003543 if (fd == INVALID_FD
3544 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003545 {
3546 if (timeout == timeout_arg)
3547 {
3548 if (fd != INVALID_FD)
3549 ch_log(channel, "Timed out");
3550 break;
3551 }
3552 }
3553 else
3554 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003555 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003556 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003557 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003558 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003559}
3560
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003561/*
3562 * Common for ch_read() and ch_readraw().
3563 */
3564 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003565common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003566{
3567 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003568 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003569 jobopt_T opt;
3570 int mode;
3571 int timeout;
3572 int id = -1;
3573 typval_T *listtv = NULL;
3574
3575 /* return an empty string by default */
3576 rettv->v_type = VAR_STRING;
3577 rettv->vval.v_string = NULL;
3578
3579 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003580 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003581 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003582 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003583
Bram Moolenaar437905c2016-04-26 19:01:05 +02003584 if (opt.jo_set & JO_PART)
3585 part = opt.jo_part;
3586 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003587 if (channel != NULL)
3588 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003589 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003590 part = channel_part_read(channel);
3591 mode = channel_get_mode(channel, part);
3592 timeout = channel_get_timeout(channel, part);
3593 if (opt.jo_set & JO_TIMEOUT)
3594 timeout = opt.jo_timeout;
3595
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003596 if (blob)
3597 {
3598 int outlen = 0;
3599 char_u *p = channel_read_block(channel, part,
3600 timeout, TRUE, &outlen);
3601 if (p != NULL)
3602 {
3603 blob_T *b = blob_alloc();
3604
3605 if (b != NULL)
3606 {
3607 b->bv_ga.ga_len = outlen;
3608 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3609 blob_free(b);
3610 else
3611 {
3612 memcpy(b->bv_ga.ga_data, p, outlen);
3613 rettv_blob_set(rettv, b);
3614 }
3615 }
3616 vim_free(p);
3617 }
3618 }
3619 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003620 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003621 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003622 else
3623 {
3624 if (opt.jo_set & JO_ID)
3625 id = opt.jo_id;
3626 channel_read_json_block(channel, part, timeout, id, &listtv);
3627 if (listtv != NULL)
3628 {
3629 *rettv = *listtv;
3630 vim_free(listtv);
3631 }
3632 else
3633 {
3634 rettv->v_type = VAR_SPECIAL;
3635 rettv->vval.v_number = VVAL_NONE;
3636 }
3637 }
3638 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003639
3640theend:
3641 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003642}
3643
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003644# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3645 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003646/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003647 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003648 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003649 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003650 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003651channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003652{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003653 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003654 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003655
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003656 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003657 for (channel = first_channel; channel != NULL;
3658 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003659 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003660 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003661 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003662 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003663 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003664 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003665 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003666 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003667 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003668}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003669# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003670
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003671# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003672/*
3673 * Check the channels for anything that is ready to be read.
3674 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003675 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003676 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003677 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003678channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003679{
3680 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003681 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003682 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003683
3684 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3685 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003686 if (only_keep_open && !channel->ch_keep_open)
3687 continue;
3688
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003689 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003690 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003691 {
3692 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003693 if (fd != INVALID_FD)
3694 {
3695 int r = channel_wait(channel, fd, 0);
3696
3697 if (r == CW_READY)
3698 channel_read(channel, part, "channel_handle_events");
3699 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003700 ch_close_part_on_error(channel, part, TRUE,
3701 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003702 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003703 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003704 }
3705}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003706# endif
3707
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003708# if defined(FEAT_GUI) || defined(PROTO)
3709/*
3710 * Return TRUE when there is any channel with a keep_open flag.
3711 */
3712 int
3713channel_any_keep_open()
3714{
3715 channel_T *channel;
3716
3717 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3718 if (channel->ch_keep_open)
3719 return TRUE;
3720 return FALSE;
3721}
3722# endif
3723
Bram Moolenaard04a0202016-01-26 23:30:18 +01003724/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003725 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003726 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003727 */
3728 void
3729channel_set_nonblock(channel_T *channel, ch_part_T part)
3730{
3731 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003732 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003733
3734 if (fd != INVALID_FD)
3735 {
3736#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003737 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003738
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003739 ioctlsocket(fd, FIONBIO, &val);
3740#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003741 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003742#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003743 ch_part->ch_nonblocking = TRUE;
3744 }
3745}
3746
3747/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003748 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003749 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003750 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003751 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003752 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003753channel_send(
3754 channel_T *channel,
3755 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003756 char_u *buf_arg,
3757 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003758 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003759{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003760 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003761 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003762 chanpart_T *ch_part = &channel->ch_part[part];
3763 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003764
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003765 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003766 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003767 {
3768 if (!channel->ch_error && fun != NULL)
3769 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003770 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003771 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003772 }
3773 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003774 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003775 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003776
Bram Moolenaar0b146882018-09-06 16:27:24 +02003777 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3778 channel_set_nonblock(channel, part);
3779
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003780 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003781 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003782 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003783 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003784 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003785 fprintf(log_fd, "'\n");
3786 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003787 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003788 }
3789
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003790 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003791 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003792 writeq_T *wq = &ch_part->ch_writeque;
3793 char_u *buf;
3794 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003795
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003796 if (wq->wq_next != NULL)
3797 {
3798 /* first write what was queued */
3799 buf = wq->wq_next->wq_ga.ga_data;
3800 len = wq->wq_next->wq_ga.ga_len;
3801 did_use_queue = TRUE;
3802 }
3803 else
3804 {
3805 if (len_arg == 0)
3806 /* nothing to write, called from channel_select_check() */
3807 return OK;
3808 buf = buf_arg;
3809 len = len_arg;
3810 }
3811
3812 if (part == PART_SOCK)
3813 res = sock_write(fd, (char *)buf, len);
3814 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003815 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003816 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003817#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003818 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003819 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003820 DisconnectNamedPipe((HANDLE)fd);
3821 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003822 }
3823#endif
3824
3825 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003826 if (res < 0 && (errno == EWOULDBLOCK
3827#ifdef EAGAIN
3828 || errno == EAGAIN
3829#endif
3830 ))
3831 res = 0; /* nothing got written */
3832
3833 if (res >= 0 && ch_part->ch_nonblocking)
3834 {
3835 writeq_T *entry = wq->wq_next;
3836
3837 if (did_use_queue)
3838 ch_log(channel, "Sent %d bytes now", res);
3839 if (res == len)
3840 {
3841 /* Wrote all the buf[len] bytes. */
3842 if (entry != NULL)
3843 {
3844 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003845 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003846 continue;
3847 }
3848 if (did_use_queue)
3849 ch_log(channel, "Write queue empty");
3850 }
3851 else
3852 {
3853 /* Wrote only buf[res] bytes, can't write more now. */
3854 if (entry != NULL)
3855 {
3856 if (res > 0)
3857 {
3858 /* Remove the bytes that were written. */
3859 mch_memmove(entry->wq_ga.ga_data,
3860 (char *)entry->wq_ga.ga_data + res,
3861 len - res);
3862 entry->wq_ga.ga_len -= res;
3863 }
3864 buf = buf_arg;
3865 len = len_arg;
3866 }
3867 else
3868 {
3869 buf += res;
3870 len -= res;
3871 }
3872 ch_log(channel, "Adding %d bytes to the write queue", len);
3873
3874 /* Append the not written bytes of the argument to the write
3875 * buffer. Limit entries to 4000 bytes. */
3876 if (wq->wq_prev != NULL
3877 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3878 {
3879 writeq_T *last = wq->wq_prev;
3880
3881 /* append to the last entry */
3882 if (ga_grow(&last->wq_ga, len) == OK)
3883 {
3884 mch_memmove((char *)last->wq_ga.ga_data
3885 + last->wq_ga.ga_len,
3886 buf, len);
3887 last->wq_ga.ga_len += len;
3888 }
3889 }
3890 else
3891 {
3892 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3893
3894 if (last != NULL)
3895 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003896 last->wq_prev = wq->wq_prev;
3897 last->wq_next = NULL;
3898 if (wq->wq_prev == NULL)
3899 wq->wq_next = last;
3900 else
3901 wq->wq_prev->wq_next = last;
3902 wq->wq_prev = last;
3903 ga_init2(&last->wq_ga, 1, 1000);
3904 if (ga_grow(&last->wq_ga, len) == OK)
3905 {
3906 mch_memmove(last->wq_ga.ga_data, buf, len);
3907 last->wq_ga.ga_len = len;
3908 }
3909 }
3910 }
3911 }
3912 }
3913 else if (res != len)
3914 {
3915 if (!channel->ch_error && fun != NULL)
3916 {
3917 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003918 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003919 }
3920 channel->ch_error = TRUE;
3921 return FAIL;
3922 }
3923
3924 channel->ch_error = FALSE;
3925 return OK;
3926 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003927}
3928
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003929/*
3930 * Common for "ch_sendexpr()" and "ch_sendraw()".
3931 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003932 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003933 * Otherwise returns NULL.
3934 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003935 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003936send_common(
3937 typval_T *argvars,
3938 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003939 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003940 int id,
3941 int eval,
3942 jobopt_T *opt,
3943 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003944 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003945{
3946 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003947 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003948
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003949 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003950 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003951 if (channel == NULL)
3952 return NULL;
3953 part_send = channel_part_send(channel);
3954 *part_read = channel_part_read(channel);
3955
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003956 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003957 return NULL;
3958
3959 /* Set the callback. An empty callback means no callback and not reading
3960 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3961 * allowed. */
3962 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3963 {
3964 if (eval)
3965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003966 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003967 return NULL;
3968 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003969 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003970 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003971 }
3972
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003973 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003974 && opt->jo_callback == NULL)
3975 return channel;
3976 return NULL;
3977}
3978
3979/*
3980 * common for "ch_evalexpr()" and "ch_sendexpr()"
3981 */
3982 void
3983ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3984{
3985 char_u *text;
3986 typval_T *listtv;
3987 channel_T *channel;
3988 int id;
3989 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003990 ch_part_T part_send;
3991 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003992 jobopt_T opt;
3993 int timeout;
3994
3995 /* return an empty string by default */
3996 rettv->v_type = VAR_STRING;
3997 rettv->vval.v_string = NULL;
3998
Bram Moolenaar437905c2016-04-26 19:01:05 +02003999 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004000 if (channel == NULL)
4001 return;
4002 part_send = channel_part_send(channel);
4003
4004 ch_mode = channel_get_mode(channel, part_send);
4005 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004007 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004008 return;
4009 }
4010
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004011 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004012 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004013 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004014 if (text == NULL)
4015 return;
4016
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004017 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004018 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4019 vim_free(text);
4020 if (channel != NULL && eval)
4021 {
4022 if (opt.jo_set & JO_TIMEOUT)
4023 timeout = opt.jo_timeout;
4024 else
4025 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004026 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4027 == OK)
4028 {
4029 list_T *list = listtv->vval.v_list;
4030
4031 /* Move the item from the list and then change the type to
4032 * avoid the value being freed. */
4033 *rettv = list->lv_last->li_tv;
4034 list->lv_last->li_tv.v_type = VAR_NUMBER;
4035 free_tv(listtv);
4036 }
4037 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004038 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004039}
4040
4041/*
4042 * common for "ch_evalraw()" and "ch_sendraw()"
4043 */
4044 void
4045ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4046{
4047 char_u buf[NUMBUFLEN];
4048 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004049 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004050 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004051 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004052 jobopt_T opt;
4053 int timeout;
4054
4055 /* return an empty string by default */
4056 rettv->v_type = VAR_STRING;
4057 rettv->vval.v_string = NULL;
4058
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004059 if (argvars[1].v_type == VAR_BLOB)
4060 {
4061 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4062 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4063 }
4064 else
4065 {
4066 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004067 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004068 }
4069 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4071 if (channel != NULL && eval)
4072 {
4073 if (opt.jo_set & JO_TIMEOUT)
4074 timeout = opt.jo_timeout;
4075 else
4076 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004077 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004078 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004079 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004080 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004081}
4082
Bram Moolenaarf3360612017-10-01 16:21:31 +02004083# define KEEP_OPEN_TIME 20 /* msec */
4084
Bram Moolenaard04a0202016-01-26 23:30:18 +01004085# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004086/*
4087 * Add open channels to the poll struct.
4088 * Return the adjusted struct index.
4089 * The type of "fds" is hidden to avoid problems with the function proto.
4090 */
4091 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004092channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004093{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004094 int nfd = nfd_in;
4095 channel_T *channel;
4096 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004097 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004098
Bram Moolenaar77073442016-02-13 23:23:53 +01004099 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004100 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004101 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004102 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004103 chanpart_T *ch_part = &channel->ch_part[part];
4104
4105 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004106 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004107 if (channel->ch_keep_open)
4108 {
4109 /* For unknown reason poll() returns immediately for a
4110 * keep-open channel. Instead of adding it to the fds add
4111 * a short timeout and check, like polling. */
4112 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4113 *towait = KEEP_OPEN_TIME;
4114 }
4115 else
4116 {
4117 ch_part->ch_poll_idx = nfd;
4118 fds[nfd].fd = ch_part->ch_fd;
4119 fds[nfd].events = POLLIN;
4120 nfd++;
4121 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004122 }
4123 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004124 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004125 }
4126 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004127
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004128 nfd = channel_fill_poll_write(nfd, fds);
4129
Bram Moolenaare0874f82016-01-24 20:36:41 +01004130 return nfd;
4131}
4132
4133/*
4134 * The type of "fds" is hidden to avoid problems with the function proto.
4135 */
4136 int
4137channel_poll_check(int ret_in, void *fds_in)
4138{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004139 int ret = ret_in;
4140 channel_T *channel;
4141 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004142 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004143 int idx;
4144 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004145
Bram Moolenaar77073442016-02-13 23:23:53 +01004146 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004147 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004148 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004149 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004150 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004151
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004152 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004153 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004154 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004155 --ret;
4156 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004157 else if (channel->ch_part[part].ch_fd != INVALID_FD
4158 && channel->ch_keep_open)
4159 {
4160 /* polling a keep-open channel */
4161 channel_read(channel, part, "channel_poll_check_keep_open");
4162 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004163 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004164
4165 in_part = &channel->ch_part[PART_IN];
4166 idx = in_part->ch_poll_idx;
4167 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4168 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004169 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004170 --ret;
4171 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004172 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004173
4174 return ret;
4175}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004176# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004177
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004178# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004179
Bram Moolenaare0874f82016-01-24 20:36:41 +01004180/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004181 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004182 */
4183 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004184channel_select_setup(
4185 int maxfd_in,
4186 void *rfds_in,
4187 void *wfds_in,
4188 struct timeval *tv,
4189 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004190{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004191 int maxfd = maxfd_in;
4192 channel_T *channel;
4193 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004194 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004195 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004196
Bram Moolenaar77073442016-02-13 23:23:53 +01004197 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004198 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004199 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004200 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004201 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004202
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004203 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004204 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004205 if (channel->ch_keep_open)
4206 {
4207 /* For unknown reason select() returns immediately for a
4208 * keep-open channel. Instead of adding it to the rfds add
4209 * a short timeout and check, like polling. */
4210 if (*tvp == NULL || tv->tv_sec > 0
4211 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4212 {
4213 *tvp = tv;
4214 tv->tv_sec = 0;
4215 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4216 }
4217 }
4218 else
4219 {
4220 FD_SET((int)fd, rfds);
4221 if (maxfd < (int)fd)
4222 maxfd = (int)fd;
4223 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004224 }
4225 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004226 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004227
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004228 maxfd = channel_fill_wfds(maxfd, wfds);
4229
Bram Moolenaare0874f82016-01-24 20:36:41 +01004230 return maxfd;
4231}
4232
4233/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004234 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004235 */
4236 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004237channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004238{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004239 int ret = ret_in;
4240 channel_T *channel;
4241 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004242 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004243 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004244 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004245
Bram Moolenaar77073442016-02-13 23:23:53 +01004246 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004247 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004248 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004249 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004250 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004251
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004252 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004253 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004254 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004255 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004256 --ret;
4257 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004258 else if (fd != INVALID_FD && channel->ch_keep_open)
4259 {
4260 /* polling a keep-open channel */
4261 channel_read(channel, part, "channel_select_check_keep_open");
4262 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004263 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004264
4265 in_part = &channel->ch_part[PART_IN];
4266 if (ret > 0 && in_part->ch_fd != INVALID_FD
4267 && FD_ISSET(in_part->ch_fd, wfds))
4268 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004269 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004270 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004271 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004272 --ret;
4273 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004274 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004275
4276 return ret;
4277}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004278# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004279
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004280/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004281 * Execute queued up commands.
4282 * Invoked from the main loop when it's safe to execute received commands.
4283 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004284 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004285 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004286channel_parse_messages(void)
4287{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004288 channel_T *channel = first_channel;
4289 int ret = FALSE;
4290 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004291 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004292#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004293 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004294
4295 ELAPSED_INIT(start_tv);
4296#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004297
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004298 ++safe_to_invoke_callback;
4299
Bram Moolenaard0b65022016-03-06 21:50:33 +01004300 /* Only do this message when another message was given, otherwise we get
4301 * lots of them. */
4302 if (did_log_msg)
4303 {
4304 ch_log(NULL, "looking for messages on channels");
4305 did_log_msg = FALSE;
4306 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004307 while (channel != NULL)
4308 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004309 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004310 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004311 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004312 channel_close_now(channel);
4313 /* channel may have been freed, start over */
4314 channel = first_channel;
4315 continue;
4316 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004317 if (channel->ch_to_be_freed)
4318 {
4319 channel_free(channel);
4320 /* channel has been freed, start over */
4321 channel = first_channel;
4322 continue;
4323 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004324 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004325 {
4326 /* channel is no longer useful, free it */
4327 channel_free(channel);
4328 channel = first_channel;
4329 part = PART_SOCK;
4330 continue;
4331 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004332 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004333 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004334 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004335 /* Increase the refcount, in case the handler causes the channel
4336 * to be unreferenced or closed. */
4337 ++channel->ch_refcount;
4338 r = may_invoke_callback(channel, part);
4339 if (r == OK)
4340 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004341 if (channel_unref(channel) || (r == OK
4342#ifdef ELAPSED_FUNC
4343 /* Limit the time we loop here to 100 msec, otherwise
4344 * Vim becomes unresponsive when the callback takes
4345 * more than a bit of time. */
4346 && ELAPSED_FUNC(start_tv) < 100L
4347#endif
4348 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004349 {
4350 /* channel was freed or something was done, start over */
4351 channel = first_channel;
4352 part = PART_SOCK;
4353 continue;
4354 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004355 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004356 if (part < PART_ERR)
4357 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004358 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004359 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004360 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004361 part = PART_SOCK;
4362 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004363 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004364
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004365 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004366 {
4367 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004368 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004369 }
4370
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004371 --safe_to_invoke_callback;
4372
Bram Moolenaard7ece102016-02-02 23:23:02 +01004373 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004374}
4375
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004376/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004377 * Return TRUE if any channel has readahead. That means we should not block on
4378 * waiting for input.
4379 */
4380 int
4381channel_any_readahead(void)
4382{
4383 channel_T *channel = first_channel;
4384 ch_part_T part = PART_SOCK;
4385
4386 while (channel != NULL)
4387 {
4388 if (channel_has_readahead(channel, part))
4389 return TRUE;
4390 if (part < PART_ERR)
4391 ++part;
4392 else
4393 {
4394 channel = channel->ch_next;
4395 part = PART_SOCK;
4396 }
4397 }
4398 return FALSE;
4399}
4400
4401/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004402 * Mark references to lists used in channels.
4403 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004404 int
4405set_ref_in_channel(int copyID)
4406{
Bram Moolenaar77073442016-02-13 23:23:53 +01004407 int abort = FALSE;
4408 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004409 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004410
Bram Moolenaar77073442016-02-13 23:23:53 +01004411 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004412 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004413 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004414 tv.v_type = VAR_CHANNEL;
4415 tv.vval.v_channel = channel;
4416 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004417 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004418 return abort;
4419}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004420
4421/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004422 * Return the "part" to write to for "channel".
4423 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004424 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004425channel_part_send(channel_T *channel)
4426{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004427 if (channel->CH_SOCK_FD == INVALID_FD)
4428 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004429 return PART_SOCK;
4430}
4431
4432/*
4433 * Return the default "part" to read from for "channel".
4434 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004435 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004436channel_part_read(channel_T *channel)
4437{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004438 if (channel->CH_SOCK_FD == INVALID_FD)
4439 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004440 return PART_SOCK;
4441}
4442
4443/*
4444 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004445 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004446 */
4447 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004448channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004449{
Bram Moolenaar77073442016-02-13 23:23:53 +01004450 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004451 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004452 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004453}
4454
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004455/*
4456 * Return the timeout of "channel"/"part"
4457 */
4458 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004459channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004460{
4461 return channel->ch_part[part].ch_timeout;
4462}
4463
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004464 static int
4465handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4466{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004467 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004468
4469 opt->jo_set |= jo;
4470 if (STRCMP(val, "nl") == 0)
4471 *modep = MODE_NL;
4472 else if (STRCMP(val, "raw") == 0)
4473 *modep = MODE_RAW;
4474 else if (STRCMP(val, "js") == 0)
4475 *modep = MODE_JS;
4476 else if (STRCMP(val, "json") == 0)
4477 *modep = MODE_JSON;
4478 else
4479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004480 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004481 return FAIL;
4482 }
4483 return OK;
4484}
4485
4486 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004487handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004488{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004489 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004490
4491 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4492 if (STRCMP(val, "null") == 0)
4493 opt->jo_io[part] = JIO_NULL;
4494 else if (STRCMP(val, "pipe") == 0)
4495 opt->jo_io[part] = JIO_PIPE;
4496 else if (STRCMP(val, "file") == 0)
4497 opt->jo_io[part] = JIO_FILE;
4498 else if (STRCMP(val, "buffer") == 0)
4499 opt->jo_io[part] = JIO_BUFFER;
4500 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4501 opt->jo_io[part] = JIO_OUT;
4502 else
4503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004504 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004505 return FAIL;
4506 }
4507 return OK;
4508}
4509
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004510/*
4511 * Clear a jobopt_T before using it.
4512 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004513 void
4514clear_job_options(jobopt_T *opt)
4515{
4516 vim_memset(opt, 0, sizeof(jobopt_T));
4517}
4518
4519/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004520 * Free any members of a jobopt_T.
4521 */
4522 void
4523free_job_options(jobopt_T *opt)
4524{
4525 if (opt->jo_partial != NULL)
4526 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004527 else if (opt->jo_callback != NULL)
4528 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004529 if (opt->jo_out_partial != NULL)
4530 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004531 else if (opt->jo_out_cb != NULL)
4532 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004533 if (opt->jo_err_partial != NULL)
4534 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004535 else if (opt->jo_err_cb != NULL)
4536 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004537 if (opt->jo_close_partial != NULL)
4538 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004539 else if (opt->jo_close_cb != NULL)
4540 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004541 if (opt->jo_exit_partial != NULL)
4542 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004543 else if (opt->jo_exit_cb != NULL)
4544 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004545 if (opt->jo_env != NULL)
4546 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004547}
4548
4549/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004550 * Get the PART_ number from the first character of an option name.
4551 */
4552 static int
4553part_from_char(int c)
4554{
4555 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4556}
4557
4558/*
4559 * Get the option entries from the dict in "tv", parse them and put the result
4560 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004561 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004562 * If an option value is invalid return FAIL.
4563 */
4564 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004565get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004566{
4567 typval_T *item;
4568 char_u *val;
4569 dict_T *dict;
4570 int todo;
4571 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004572 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004573
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004574 if (tv->v_type == VAR_UNKNOWN)
4575 return OK;
4576 if (tv->v_type != VAR_DICT)
4577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004578 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004579 return FAIL;
4580 }
4581 dict = tv->vval.v_dict;
4582 if (dict == NULL)
4583 return OK;
4584
4585 todo = (int)dict->dv_hashtab.ht_used;
4586 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4587 if (!HASHITEM_EMPTY(hi))
4588 {
4589 item = &dict_lookup(hi)->di_tv;
4590
4591 if (STRCMP(hi->hi_key, "mode") == 0)
4592 {
4593 if (!(supported & JO_MODE))
4594 break;
4595 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4596 return FAIL;
4597 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004598 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004599 {
4600 if (!(supported & JO_IN_MODE))
4601 break;
4602 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4603 == FAIL)
4604 return FAIL;
4605 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004606 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004607 {
4608 if (!(supported & JO_OUT_MODE))
4609 break;
4610 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4611 == FAIL)
4612 return FAIL;
4613 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004614 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004615 {
4616 if (!(supported & JO_ERR_MODE))
4617 break;
4618 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4619 == FAIL)
4620 return FAIL;
4621 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004622 else if (STRCMP(hi->hi_key, "noblock") == 0)
4623 {
4624 if (!(supported & JO_MODE))
4625 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004626 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004627 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004628 else if (STRCMP(hi->hi_key, "in_io") == 0
4629 || STRCMP(hi->hi_key, "out_io") == 0
4630 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004631 {
4632 if (!(supported & JO_OUT_IO))
4633 break;
4634 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4635 return FAIL;
4636 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004637 else if (STRCMP(hi->hi_key, "in_name") == 0
4638 || STRCMP(hi->hi_key, "out_name") == 0
4639 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004640 {
4641 part = part_from_char(*hi->hi_key);
4642
4643 if (!(supported & JO_OUT_IO))
4644 break;
4645 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4646 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004647 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004648 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004649 else if (STRCMP(hi->hi_key, "pty") == 0)
4650 {
4651 if (!(supported & JO_MODE))
4652 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004653 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004654 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004655 else if (STRCMP(hi->hi_key, "in_buf") == 0
4656 || STRCMP(hi->hi_key, "out_buf") == 0
4657 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004658 {
4659 part = part_from_char(*hi->hi_key);
4660
4661 if (!(supported & JO_OUT_IO))
4662 break;
4663 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004664 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004665 if (opt->jo_io_buf[part] <= 0)
4666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004667 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004668 return FAIL;
4669 }
4670 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004672 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004673 return FAIL;
4674 }
4675 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004676 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4677 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4678 {
4679 part = part_from_char(*hi->hi_key);
4680
4681 if (!(supported & JO_OUT_IO))
4682 break;
4683 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004684 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004685 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004686 else if (STRCMP(hi->hi_key, "out_msg") == 0
4687 || STRCMP(hi->hi_key, "err_msg") == 0)
4688 {
4689 part = part_from_char(*hi->hi_key);
4690
4691 if (!(supported & JO_OUT_IO))
4692 break;
4693 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004694 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004695 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004696 else if (STRCMP(hi->hi_key, "in_top") == 0
4697 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004698 {
4699 linenr_T *lp;
4700
4701 if (!(supported & JO_OUT_IO))
4702 break;
4703 if (hi->hi_key[3] == 't')
4704 {
4705 lp = &opt->jo_in_top;
4706 opt->jo_set |= JO_IN_TOP;
4707 }
4708 else
4709 {
4710 lp = &opt->jo_in_bot;
4711 opt->jo_set |= JO_IN_BOT;
4712 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004713 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004714 if (*lp < 0)
4715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004717 return FAIL;
4718 }
4719 }
4720 else if (STRCMP(hi->hi_key, "channel") == 0)
4721 {
4722 if (!(supported & JO_OUT_IO))
4723 break;
4724 opt->jo_set |= JO_CHANNEL;
4725 if (item->v_type != VAR_CHANNEL)
4726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004727 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004728 return FAIL;
4729 }
4730 opt->jo_channel = item->vval.v_channel;
4731 }
4732 else if (STRCMP(hi->hi_key, "callback") == 0)
4733 {
4734 if (!(supported & JO_CALLBACK))
4735 break;
4736 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004737 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004738 if (opt->jo_callback == NULL)
4739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004740 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004741 return FAIL;
4742 }
4743 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004744 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004745 {
4746 if (!(supported & JO_OUT_CALLBACK))
4747 break;
4748 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004749 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004750 if (opt->jo_out_cb == NULL)
4751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004752 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004753 return FAIL;
4754 }
4755 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004756 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004757 {
4758 if (!(supported & JO_ERR_CALLBACK))
4759 break;
4760 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004761 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004762 if (opt->jo_err_cb == NULL)
4763 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004764 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004765 return FAIL;
4766 }
4767 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004768 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004769 {
4770 if (!(supported & JO_CLOSE_CALLBACK))
4771 break;
4772 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004773 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004774 if (opt->jo_close_cb == NULL)
4775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004776 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004777 return FAIL;
4778 }
4779 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004780 else if (STRCMP(hi->hi_key, "drop") == 0)
4781 {
4782 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004784
4785 if (STRCMP(val, "never") == 0)
4786 never = TRUE;
4787 else if (STRCMP(val, "auto") != 0)
4788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004789 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004790 return FAIL;
4791 }
4792 opt->jo_drop_never = never;
4793 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004794 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4795 {
4796 if (!(supported & JO_EXIT_CB))
4797 break;
4798 opt->jo_set |= JO_EXIT_CB;
4799 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4800 if (opt->jo_exit_cb == NULL)
4801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004802 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004803 return FAIL;
4804 }
4805 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004806#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004807 else if (STRCMP(hi->hi_key, "term_name") == 0)
4808 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004809 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004810 break;
4811 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004812 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004813 if (opt->jo_term_name == NULL)
4814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004815 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004816 return FAIL;
4817 }
4818 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004819 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4820 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004821 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004822 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004823 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004824 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004826 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004827 return FAIL;
4828 }
4829 opt->jo_set2 |= JO2_TERM_FINISH;
4830 opt->jo_term_finish = *val;
4831 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004832 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4833 {
4834 char_u *p;
4835
4836 if (!(supported2 & JO2_TERM_OPENCMD))
4837 break;
4838 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004839 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004840 if (p != NULL)
4841 {
4842 /* Must have %d and no other %. */
4843 p = vim_strchr(p, '%');
4844 if (p != NULL && (p[1] != 'd'
4845 || vim_strchr(p + 2, '%') != NULL))
4846 p = NULL;
4847 }
4848 if (p == NULL)
4849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004850 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004851 return FAIL;
4852 }
4853 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004854 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4855 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004856 char_u *p;
4857
4858 if (!(supported2 & JO2_EOF_CHARS))
4859 break;
4860 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004861 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004862 if (p == NULL)
4863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004864 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004865 return FAIL;
4866 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004867 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004868 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4869 {
4870 if (!(supported2 & JO2_TERM_ROWS))
4871 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004872 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004873 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004874 }
4875 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4876 {
4877 if (!(supported2 & JO2_TERM_COLS))
4878 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004879 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004880 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004881 }
4882 else if (STRCMP(hi->hi_key, "vertical") == 0)
4883 {
4884 if (!(supported2 & JO2_VERTICAL))
4885 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004886 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004887 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004888 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004889 else if (STRCMP(hi->hi_key, "curwin") == 0)
4890 {
4891 if (!(supported2 & JO2_CURWIN))
4892 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004893 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004894 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004895 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004896 else if (STRCMP(hi->hi_key, "hidden") == 0)
4897 {
4898 if (!(supported2 & JO2_HIDDEN))
4899 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004900 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004901 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004902 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004903 else if (STRCMP(hi->hi_key, "norestore") == 0)
4904 {
4905 if (!(supported2 & JO2_NORESTORE))
4906 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004907 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004908 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004909 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004910 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4911 {
4912 if (!(supported2 & JO2_TERM_KILL))
4913 break;
4914 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004915 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004916 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004917# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4918 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4919 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004920 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004921 listitem_T *li;
4922 long_u rgb[16];
4923
4924 if (!(supported2 & JO2_ANSI_COLORS))
4925 break;
4926
4927 if (item == NULL || item->v_type != VAR_LIST
4928 || item->vval.v_list == NULL)
4929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004930 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004931 return FAIL;
4932 }
4933
4934 li = item->vval.v_list->lv_first;
4935 for (; li != NULL && n < 16; li = li->li_next, n++)
4936 {
4937 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004938 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004940 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004941 if (color_name == NULL)
4942 return FAIL;
4943
4944 guicolor = GUI_GET_COLOR(color_name);
4945 if (guicolor == INVALCOLOR)
4946 return FAIL;
4947
4948 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4949 }
4950
4951 if (n != 16 || li != NULL)
4952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004953 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004954 return FAIL;
4955 }
4956
4957 opt->jo_set2 |= JO2_ANSI_COLORS;
4958 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4959 }
4960# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004961#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004962 else if (STRCMP(hi->hi_key, "env") == 0)
4963 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004964 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004965 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004966 if (item->v_type != VAR_DICT)
4967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004968 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02004969 return FAIL;
4970 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004971 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004972 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004973 if (opt->jo_env != NULL)
4974 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004975 }
4976 else if (STRCMP(hi->hi_key, "cwd") == 0)
4977 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004978 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004979 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004980 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02004981 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02004982#ifndef WIN32 // Win32 directories don't have the concept of "executable"
4983 || mch_access((char *)opt->jo_cwd, X_OK) != 0
4984#endif
4985 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004987 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004988 return FAIL;
4989 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004990 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004991 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004992 else if (STRCMP(hi->hi_key, "waittime") == 0)
4993 {
4994 if (!(supported & JO_WAITTIME))
4995 break;
4996 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004997 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004998 }
4999 else if (STRCMP(hi->hi_key, "timeout") == 0)
5000 {
5001 if (!(supported & JO_TIMEOUT))
5002 break;
5003 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005004 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005005 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005006 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005007 {
5008 if (!(supported & JO_OUT_TIMEOUT))
5009 break;
5010 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005011 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005012 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005013 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005014 {
5015 if (!(supported & JO_ERR_TIMEOUT))
5016 break;
5017 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005018 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005019 }
5020 else if (STRCMP(hi->hi_key, "part") == 0)
5021 {
5022 if (!(supported & JO_PART))
5023 break;
5024 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005025 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005026 if (STRCMP(val, "err") == 0)
5027 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005028 else if (STRCMP(val, "out") == 0)
5029 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005030 else
5031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005032 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005033 return FAIL;
5034 }
5035 }
5036 else if (STRCMP(hi->hi_key, "id") == 0)
5037 {
5038 if (!(supported & JO_ID))
5039 break;
5040 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005041 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005042 }
5043 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5044 {
5045 if (!(supported & JO_STOPONEXIT))
5046 break;
5047 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005048 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005049 opt->jo_soe_buf);
5050 if (opt->jo_stoponexit == NULL)
5051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005052 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005053 return FAIL;
5054 }
5055 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005056 else if (STRCMP(hi->hi_key, "block_write") == 0)
5057 {
5058 if (!(supported & JO_BLOCK_WRITE))
5059 break;
5060 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005061 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005062 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005063 else
5064 break;
5065 --todo;
5066 }
5067 if (todo > 0)
5068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005069 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005070 return FAIL;
5071 }
5072
5073 return OK;
5074}
5075
5076/*
5077 * Get the channel from the argument.
5078 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005079 * When "check_open" is TRUE check that the channel can be used.
5080 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005081 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005082 */
5083 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005084get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005085{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005086 channel_T *channel = NULL;
5087 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005088
5089 if (tv->v_type == VAR_JOB)
5090 {
5091 if (tv->vval.v_job != NULL)
5092 channel = tv->vval.v_job->jv_channel;
5093 }
5094 else if (tv->v_type == VAR_CHANNEL)
5095 {
5096 channel = tv->vval.v_channel;
5097 }
5098 else
5099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005100 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005101 return NULL;
5102 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005103 if (channel != NULL && reading)
5104 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005105 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005106
Bram Moolenaar437905c2016-04-26 19:01:05 +02005107 if (check_open && (channel == NULL || (!channel_is_open(channel)
5108 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005110 emsg(_("E906: not an open channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005111 return NULL;
5112 }
5113 return channel;
5114}
5115
5116static job_T *first_job = NULL;
5117
5118 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005119job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005120{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005121 int i;
5122
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005123 ch_log(job->jv_channel, "Freeing job");
5124 if (job->jv_channel != NULL)
5125 {
5126 /* The link from the channel to the job doesn't count as a reference,
5127 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005128 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005129 * NULL the reference. We don't set ch_job_killed, unreferencing the
5130 * job doesn't mean it stops running. */
5131 job->jv_channel->ch_job = NULL;
5132 channel_unref(job->jv_channel);
5133 }
5134 mch_clear_job(job);
5135
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005136 vim_free(job->jv_tty_in);
5137 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005138 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005139 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005140 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005141 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005142 for (i = 0; job->jv_argv[i] != NULL; i++)
5143 vim_free(job->jv_argv[i]);
5144 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005145 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005146}
5147
5148 static void
5149job_free_job(job_T *job)
5150{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005151 if (job->jv_next != NULL)
5152 job->jv_next->jv_prev = job->jv_prev;
5153 if (job->jv_prev == NULL)
5154 first_job = job->jv_next;
5155 else
5156 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005157 vim_free(job);
5158}
5159
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005160 static void
5161job_free(job_T *job)
5162{
5163 if (!in_free_unref_items)
5164 {
5165 job_free_contents(job);
5166 job_free_job(job);
5167 }
5168}
5169
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005170#if defined(EXITFREE) || defined(PROTO)
5171 void
5172job_free_all(void)
5173{
5174 while (first_job != NULL)
5175 job_free(first_job);
5176}
5177#endif
5178
5179/*
5180 * Return TRUE if we need to check if the process of "job" has ended.
5181 */
5182 static int
5183job_need_end_check(job_T *job)
5184{
5185 return job->jv_status == JOB_STARTED
5186 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5187}
5188
5189/*
5190 * Return TRUE if the channel of "job" is still useful.
5191 */
5192 static int
5193job_channel_still_useful(job_T *job)
5194{
5195 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5196}
5197
5198/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005199 * Return TRUE if the channel of "job" is closeable.
5200 */
5201 static int
5202job_channel_can_close(job_T *job)
5203{
5204 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5205}
5206
5207/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005208 * Return TRUE if the job should not be freed yet. Do not free the job when
5209 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5210 * or when the associated channel will do something with the job output.
5211 */
5212 static int
5213job_still_useful(job_T *job)
5214{
5215 return job_need_end_check(job) || job_channel_still_useful(job);
5216}
5217
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005218#if defined(GUI_MAY_FORK) || defined(PROTO)
5219/*
5220 * Return TRUE when there is any running job that we care about.
5221 */
5222 int
5223job_any_running()
5224{
5225 job_T *job;
5226
5227 for (job = first_job; job != NULL; job = job->jv_next)
5228 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005229 {
5230 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005231 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005232 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005233 return FALSE;
5234}
5235#endif
5236
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005237#if !defined(USE_ARGV) || defined(PROTO)
5238/*
5239 * Escape one argument for an external command.
5240 * Returns the escaped string in allocated memory. NULL when out of memory.
5241 */
5242 static char_u *
5243win32_escape_arg(char_u *arg)
5244{
5245 int slen, dlen;
5246 int escaping = 0;
5247 int i;
5248 char_u *s, *d;
5249 char_u *escaped_arg;
5250 int has_spaces = FALSE;
5251
5252 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005253 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005254 dlen = slen;
5255 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5256 {
5257 if (*s == '"' || *s == '\\')
5258 ++dlen;
5259 if (*s == ' ' || *s == '\t')
5260 has_spaces = TRUE;
5261 }
5262
5263 if (has_spaces)
5264 dlen += 2;
5265
5266 if (dlen == slen)
5267 return vim_strsave(arg);
5268
5269 /* Allocate memory for the result and fill it. */
5270 escaped_arg = alloc(dlen + 1);
5271 if (escaped_arg == NULL)
5272 return NULL;
5273 memset(escaped_arg, 0, dlen+1);
5274
5275 d = escaped_arg;
5276
5277 if (has_spaces)
5278 *d++ = '"';
5279
5280 for (s = arg; *s != NUL;)
5281 {
5282 switch (*s)
5283 {
5284 case '"':
5285 for (i = 0; i < escaping; i++)
5286 *d++ = '\\';
5287 escaping = 0;
5288 *d++ = '\\';
5289 *d++ = *s++;
5290 break;
5291 case '\\':
5292 escaping++;
5293 *d++ = *s++;
5294 break;
5295 default:
5296 escaping = 0;
5297 MB_COPY_CHAR(s, d);
5298 break;
5299 }
5300 }
5301
5302 /* add terminating quote and finish with a NUL */
5303 if (has_spaces)
5304 {
5305 for (i = 0; i < escaping; i++)
5306 *d++ = '\\';
5307 *d++ = '"';
5308 }
5309 *d = NUL;
5310
5311 return escaped_arg;
5312}
5313
5314/*
5315 * Build a command line from a list, taking care of escaping.
5316 * The result is put in gap->ga_data.
5317 * Returns FAIL when out of memory.
5318 */
5319 int
5320win32_build_cmd(list_T *l, garray_T *gap)
5321{
5322 listitem_T *li;
5323 char_u *s;
5324
5325 for (li = l->lv_first; li != NULL; li = li->li_next)
5326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005327 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005328 if (s == NULL)
5329 return FAIL;
5330 s = win32_escape_arg(s);
5331 if (s == NULL)
5332 return FAIL;
5333 ga_concat(gap, s);
5334 vim_free(s);
5335 if (li->li_next != NULL)
5336 ga_append(gap, ' ');
5337 }
5338 return OK;
5339}
5340#endif
5341
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005342/*
5343 * NOTE: Must call job_cleanup() only once right after the status of "job"
5344 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5345 * mch_detect_ended_job() returned non-NULL).
5346 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005347 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005348job_cleanup(job_T *job)
5349{
5350 if (job->jv_status != JOB_ENDED)
5351 return;
5352
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005353 /* Ready to cleanup the job. */
5354 job->jv_status = JOB_FINISHED;
5355
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005356 /* When only channel-in is kept open, close explicitly. */
5357 if (job->jv_channel != NULL)
5358 ch_close_part(job->jv_channel, PART_IN);
5359
Bram Moolenaar97792de2016-10-15 18:36:49 +02005360 if (job->jv_exit_cb != NULL)
5361 {
5362 typval_T argv[3];
5363 typval_T rettv;
5364 int dummy;
5365
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005366 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005367 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005368 ++job->jv_refcount;
5369 argv[0].v_type = VAR_JOB;
5370 argv[0].vval.v_job = job;
5371 argv[1].v_type = VAR_NUMBER;
5372 argv[1].vval.v_number = job->jv_exitval;
5373 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5374 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5375 job->jv_exit_partial, NULL);
5376 clear_tv(&rettv);
5377 --job->jv_refcount;
5378 channel_need_redraw = TRUE;
5379 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005380
5381 /* Do not free the job in case the close callback of the associated channel
5382 * isn't invoked yet and may get information by job_info(). */
5383 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005384 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005385 /* The job was already unreferenced and the associated channel was
5386 * detached, now that it ended it can be freed. Careful: caller must
5387 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005388 job_free(job);
5389 }
5390}
5391
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005392/*
5393 * Mark references in jobs that are still useful.
5394 */
5395 int
5396set_ref_in_job(int copyID)
5397{
5398 int abort = FALSE;
5399 job_T *job;
5400 typval_T tv;
5401
5402 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005403 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005404 {
5405 tv.v_type = VAR_JOB;
5406 tv.vval.v_job = job;
5407 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5408 }
5409 return abort;
5410}
5411
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005412/*
5413 * Dereference "job". Note that after this "job" may have been freed.
5414 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005415 void
5416job_unref(job_T *job)
5417{
5418 if (job != NULL && --job->jv_refcount <= 0)
5419 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005420 /* Do not free the job if there is a channel where the close callback
5421 * may get the job info. */
5422 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005423 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005424 /* Do not free the job when it has not ended yet and there is a
5425 * "stoponexit" flag or an exit callback. */
5426 if (!job_need_end_check(job))
5427 {
5428 job_free(job);
5429 }
5430 else if (job->jv_channel != NULL)
5431 {
5432 /* Do remove the link to the channel, otherwise it hangs
5433 * around until Vim exits. See job_free() for refcount. */
5434 ch_log(job->jv_channel, "detaching channel from job");
5435 job->jv_channel->ch_job = NULL;
5436 channel_unref(job->jv_channel);
5437 job->jv_channel = NULL;
5438 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005439 }
5440 }
5441}
5442
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005443 int
5444free_unused_jobs_contents(int copyID, int mask)
5445{
5446 int did_free = FALSE;
5447 job_T *job;
5448
5449 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005450 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005451 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005452 {
5453 /* Free the channel and ordinary items it contains, but don't
5454 * recurse into Lists, Dictionaries etc. */
5455 job_free_contents(job);
5456 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005457 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005458 return did_free;
5459}
5460
5461 void
5462free_unused_jobs(int copyID, int mask)
5463{
5464 job_T *job;
5465 job_T *job_next;
5466
5467 for (job = first_job; job != NULL; job = job_next)
5468 {
5469 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005470 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005471 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005472 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005473 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005474 job_free_job(job);
5475 }
5476 }
5477}
5478
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005479/*
5480 * Allocate a job. Sets the refcount to one and sets options default.
5481 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005482 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005483job_alloc(void)
5484{
5485 job_T *job;
5486
5487 job = (job_T *)alloc_clear(sizeof(job_T));
5488 if (job != NULL)
5489 {
5490 job->jv_refcount = 1;
5491 job->jv_stoponexit = vim_strsave((char_u *)"term");
5492
5493 if (first_job != NULL)
5494 {
5495 first_job->jv_prev = job;
5496 job->jv_next = first_job;
5497 }
5498 first_job = job;
5499 }
5500 return job;
5501}
5502
5503 void
5504job_set_options(job_T *job, jobopt_T *opt)
5505{
5506 if (opt->jo_set & JO_STOPONEXIT)
5507 {
5508 vim_free(job->jv_stoponexit);
5509 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5510 job->jv_stoponexit = NULL;
5511 else
5512 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5513 }
5514 if (opt->jo_set & JO_EXIT_CB)
5515 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005516 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005517 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005518 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005519 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005520 job->jv_exit_partial = NULL;
5521 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005522 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005523 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005524 job->jv_exit_partial = opt->jo_exit_partial;
5525 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005526 {
5527 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005528 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005529 }
5530 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005531 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005532 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005533 func_ref(job->jv_exit_cb);
5534 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005535 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005536 }
5537}
5538
5539/*
5540 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5541 */
5542 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005543job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005544{
5545 job_T *job;
5546
5547 for (job = first_job; job != NULL; job = job->jv_next)
5548 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005549 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005550}
5551
5552/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005553 * Return TRUE when there is any job that has an exit callback and might exit,
5554 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005555 */
5556 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005557has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005558{
5559 job_T *job;
5560
5561 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005562 /* Only should check if the channel has been closed, if the channel is
5563 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005564 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5565 || (job->jv_status == JOB_FINISHED
5566 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005567 return TRUE;
5568 return FALSE;
5569}
5570
Bram Moolenaar97792de2016-10-15 18:36:49 +02005571#define MAX_CHECK_ENDED 8
5572
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005573/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005574 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005575 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005576 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005577 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005578job_check_ended(void)
5579{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005580 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005581 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005582
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005583 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005584 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005585 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005586
Bram Moolenaar97792de2016-10-15 18:36:49 +02005587 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005588 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005589 // NOTE: mch_detect_ended_job() must only return a job of which the
5590 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005591 job_T *job = mch_detect_ended_job(first_job);
5592
5593 if (job == NULL)
5594 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005595 did_end = TRUE;
5596 job_cleanup(job); // may free "job"
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005597 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005598
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005599 if (channel_need_redraw)
5600 {
5601 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005602 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005603 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005604 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005605}
5606
5607/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005608 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005609 * "argv_arg" is only for Unix.
5610 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005611 * The returned job has a refcount of one.
5612 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005613 */
5614 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005615job_start(
5616 typval_T *argvars,
5617 char **argv_arg,
5618 jobopt_T *opt_arg,
5619 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005620{
5621 job_T *job;
5622 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005623 char **argv = NULL;
5624 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005625#if defined(UNIX)
5626# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005627 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005628#else
5629 garray_T ga;
5630#endif
5631 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005632 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005633
5634 job = job_alloc();
5635 if (job == NULL)
5636 return NULL;
5637
5638 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005639#ifndef USE_ARGV
5640 ga_init2(&ga, (int)sizeof(char*), 20);
5641#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005642
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005643 if (opt_arg != NULL)
5644 opt = *opt_arg;
5645 else
5646 {
5647 /* Default mode is NL. */
5648 clear_job_options(&opt);
5649 opt.jo_mode = MODE_NL;
5650 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005651 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5652 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5653 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005654 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005655 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005656
5657 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005658 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005659 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5660 && opt.jo_io[part] == JIO_FILE
5661 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5662 || *opt.jo_io_name[part] == NUL))
5663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005664 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005665 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005666 }
5667
5668 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5669 {
5670 buf_T *buf = NULL;
5671
5672 /* check that we can find the buffer before starting the job */
5673 if (opt.jo_set & JO_IN_BUF)
5674 {
5675 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5676 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005677 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005678 }
5679 else if (!(opt.jo_set & JO_IN_NAME))
5680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005681 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005682 }
5683 else
5684 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5685 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005686 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005687 if (buf->b_ml.ml_mfp == NULL)
5688 {
5689 char_u numbuf[NUMBUFLEN];
5690 char_u *s;
5691
5692 if (opt.jo_set & JO_IN_BUF)
5693 {
5694 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5695 s = numbuf;
5696 }
5697 else
5698 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005699 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005700 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005701 }
5702 job->jv_in_buf = buf;
5703 }
5704
5705 job_set_options(job, &opt);
5706
Bram Moolenaar13568252018-03-16 20:46:58 +01005707#ifdef USE_ARGV
5708 if (argv_arg != NULL)
5709 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005710 /* Make a copy of argv_arg for job->jv_argv. */
5711 for (i = 0; argv_arg[i] != NULL; i++)
5712 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005713 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005714 if (argv == NULL)
5715 goto theend;
5716 for (i = 0; i < argc; i++)
5717 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5718 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005719 }
5720 else
5721#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005722 if (argvars[0].v_type == VAR_STRING)
5723 {
5724 /* Command is a string. */
5725 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005726 if (cmd == NULL || *cmd == NUL)
5727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005728 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005729 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005730 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005731
5732 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005733 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005734 }
5735 else if (argvars[0].v_type != VAR_LIST
5736 || argvars[0].vval.v_list == NULL
5737 || argvars[0].vval.v_list->lv_len < 1)
5738 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005739 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005740 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005741 }
5742 else
5743 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005744 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005745
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005746 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005747 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005748#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005749 if (win32_build_cmd(l, &ga) == FAIL)
5750 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005751 cmd = ga.ga_data;
5752#endif
5753 }
5754
Bram Moolenaar20608922018-04-21 22:30:08 +02005755 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005756 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005757
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005758#ifdef USE_ARGV
5759 if (ch_log_active())
5760 {
5761 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005762
5763 ga_init2(&ga, (int)sizeof(char), 200);
5764 for (i = 0; i < argc; ++i)
5765 {
5766 if (i > 0)
5767 ga_concat(&ga, (char_u *)" ");
5768 ga_concat(&ga, (char_u *)argv[i]);
5769 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005770 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005771 ga_clear(&ga);
5772 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005773 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005774#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005775 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005776 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005777#endif
5778
5779 /* If the channel is reading from a buffer, write lines now. */
5780 if (job->jv_channel != NULL)
5781 channel_write_in(job->jv_channel);
5782
5783theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005784#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005785 vim_free(ga.ga_data);
5786#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005787 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005788 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005789 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005790 return job;
5791}
5792
5793/*
5794 * Get the status of "job" and invoke the exit callback when needed.
5795 * The returned string is not allocated.
5796 */
5797 char *
5798job_status(job_T *job)
5799{
5800 char *result;
5801
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005802 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005803 /* No need to check, dead is dead. */
5804 result = "dead";
5805 else if (job->jv_status == JOB_FAILED)
5806 result = "fail";
5807 else
5808 {
5809 result = mch_job_status(job);
5810 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005811 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005812 }
5813 return result;
5814}
5815
Bram Moolenaar8950a562016-03-12 15:22:55 +01005816/*
5817 * Implementation of job_info().
5818 */
5819 void
5820job_info(job_T *job, dict_T *dict)
5821{
5822 dictitem_T *item;
5823 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005824 list_T *l;
5825 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005826
Bram Moolenaare0be1672018-07-08 16:50:37 +02005827 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005828
5829 item = dictitem_alloc((char_u *)"channel");
5830 if (item == NULL)
5831 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005832 item->di_tv.v_type = VAR_CHANNEL;
5833 item->di_tv.vval.v_channel = job->jv_channel;
5834 if (job->jv_channel != NULL)
5835 ++job->jv_channel->ch_refcount;
5836 if (dict_add(dict, item) == FAIL)
5837 dictitem_free(item);
5838
5839#ifdef UNIX
5840 nr = job->jv_pid;
5841#else
5842 nr = job->jv_proc_info.dwProcessId;
5843#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005844 dict_add_number(dict, "process", nr);
5845 dict_add_string(dict, "tty_in", job->jv_tty_in);
5846 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005847
Bram Moolenaare0be1672018-07-08 16:50:37 +02005848 dict_add_number(dict, "exitval", job->jv_exitval);
5849 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5850 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005851
5852 l = list_alloc();
5853 if (l != NULL)
5854 {
5855 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005856 if (job->jv_argv != NULL)
5857 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005858 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005859 }
5860}
5861
5862/*
5863 * Implementation of job_info() to return info for all jobs.
5864 */
5865 void
5866job_info_all(list_T *l)
5867{
5868 job_T *job;
5869 typval_T tv;
5870
5871 for (job = first_job; job != NULL; job = job->jv_next)
5872 {
5873 tv.v_type = VAR_JOB;
5874 tv.vval.v_job = job;
5875
5876 if (list_append_tv(l, &tv) != OK)
5877 return;
5878 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005879}
5880
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005881/*
5882 * Send a signal to "job". Implements job_stop().
5883 * When "type" is not NULL use this for the type.
5884 * Otherwise use argvars[1] for the type.
5885 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005886 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005887job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005888{
5889 char_u *arg;
5890
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005891 if (type != NULL)
5892 arg = (char_u *)type;
5893 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005894 arg = (char_u *)"";
5895 else
5896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005897 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005898 if (arg == NULL)
5899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005900 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005901 return 0;
5902 }
5903 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005904 if (job->jv_status == JOB_FAILED)
5905 {
5906 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5907 return 0;
5908 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005909 if (job->jv_status == JOB_ENDED)
5910 {
5911 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5912 return 0;
5913 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005914 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005915 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005916 return 0;
5917
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005918 /* Assume that only "kill" will kill the job. */
5919 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005920 job->jv_channel->ch_job_killed = TRUE;
5921
5922 /* We don't try freeing the job, obviously the caller still has a
5923 * reference to it. */
5924 return 1;
5925}
5926
Bram Moolenaarf2732452018-06-03 14:47:35 +02005927 void
5928invoke_prompt_callback(void)
5929{
5930 typval_T rettv;
5931 int dummy;
5932 typval_T argv[2];
5933 char_u *text;
5934 char_u *prompt;
5935 linenr_T lnum = curbuf->b_ml.ml_line_count;
5936
5937 // Add a new line for the prompt before invoking the callback, so that
5938 // text can always be inserted above the last line.
5939 ml_append(lnum, (char_u *)"", 0, FALSE);
5940 curwin->w_cursor.lnum = lnum + 1;
5941 curwin->w_cursor.col = 0;
5942
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005943 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005944 return;
5945 text = ml_get(lnum);
5946 prompt = prompt_text();
5947 if (STRLEN(text) >= STRLEN(prompt))
5948 text += STRLEN(prompt);
5949 argv[0].v_type = VAR_STRING;
5950 argv[0].vval.v_string = vim_strsave(text);
5951 argv[1].v_type = VAR_UNKNOWN;
5952
5953 call_func(curbuf->b_prompt_callback,
5954 (int)STRLEN(curbuf->b_prompt_callback),
5955 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5956 curbuf->b_prompt_partial, NULL);
5957 clear_tv(&argv[0]);
5958 clear_tv(&rettv);
5959}
5960
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005961/*
5962 * Return TRUE when the interrupt callback was invoked.
5963 */
5964 int
5965invoke_prompt_interrupt(void)
5966{
5967 typval_T rettv;
5968 int dummy;
5969 typval_T argv[1];
5970
5971 if (curbuf->b_prompt_interrupt == NULL
5972 || *curbuf->b_prompt_interrupt == NUL)
5973 return FALSE;
5974 argv[0].v_type = VAR_UNKNOWN;
5975
5976 got_int = FALSE; // don't skip executing commands
5977 call_func(curbuf->b_prompt_interrupt,
5978 (int)STRLEN(curbuf->b_prompt_interrupt),
5979 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5980 curbuf->b_prompt_int_partial, NULL);
5981 clear_tv(&rettv);
5982 return TRUE;
5983}
5984
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005985#endif /* FEAT_JOB_CHANNEL */