blob: aa0a0d38cae97bd753e203843613be9fdd5c9628 [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 {
119 EMSG2(_(e_notopen), fname);
120 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
212# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
213 (char_u *)msg, (char_u *)strerror_win32(errno))
214
215 static char *
216strerror_win32(int eno)
217{
218 static LPVOID msgbuf = NULL;
219 char_u *ptr;
220
221 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200222 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100223 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200224 msgbuf = NULL;
225 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100226 FormatMessage(
227 FORMAT_MESSAGE_ALLOCATE_BUFFER |
228 FORMAT_MESSAGE_FROM_SYSTEM |
229 FORMAT_MESSAGE_IGNORE_INSERTS,
230 NULL,
231 eno,
232 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
233 (LPTSTR) &msgbuf,
234 0,
235 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200236 if (msgbuf != NULL)
237 /* chomp \r or \n */
238 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
239 switch (*ptr)
240 {
241 case '\r':
242 STRMOVE(ptr, ptr + 1);
243 ptr--;
244 break;
245 case '\n':
246 if (*(ptr + 1) == '\0')
247 *ptr = '\0';
248 else
249 *ptr = ' ';
250 break;
251 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252 return msgbuf;
253}
254#endif
255
Bram Moolenaar77073442016-02-13 23:23:53 +0100256/*
257 * The list of all allocated channels.
258 */
259static channel_T *first_channel = NULL;
260static int next_ch_id = 0;
261
262/*
263 * Allocate a new channel. The refcount is set to 1.
264 * The channel isn't actually used until it is opened.
265 * Returns NULL if out of memory.
266 */
267 channel_T *
268add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100269{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200270 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100271 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100272
Bram Moolenaar77073442016-02-13 23:23:53 +0100273 if (channel == NULL)
274 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100275
Bram Moolenaar77073442016-02-13 23:23:53 +0100276 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100277 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100278
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200279 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100280 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100281 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100282#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100283 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100284#endif
285#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100286 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100287#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100288 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100289 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100290
Bram Moolenaar77073442016-02-13 23:23:53 +0100291 if (first_channel != NULL)
292 {
293 first_channel->ch_prev = channel;
294 channel->ch_next = first_channel;
295 }
296 first_channel = channel;
297
298 channel->ch_refcount = 1;
299 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100300}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100301
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200302 int
303has_any_channel(void)
304{
305 return first_channel != NULL;
306}
307
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100308/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100309 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100310 * Return TRUE if "channel" has a callback and the associated job wasn't
311 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100312 */
313 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100314channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100315{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100316 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100317 int has_out_msg;
318 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100319
320 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100321 if (channel->ch_job_killed && channel->ch_job == NULL)
322 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100323
324 /* If there is a close callback it may still need to be invoked. */
325 if (channel->ch_close_cb != NULL)
326 return TRUE;
327
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200328 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200329 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200330 return TRUE;
331
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100332 /* If there is no callback then nobody can get readahead. If the fd is
333 * closed and there is no readahead then the callback won't be called. */
334 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100335 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
336 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100337 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
338 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
339 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
340 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
341 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
342 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100344 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200345 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200346 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
347 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200348 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200349 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
350 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100351}
352
353/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200354 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
355 */
356 static int
357channel_can_close(channel_T *channel)
358{
359 return channel->ch_to_be_closed == 0;
360}
361
362/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200363 * Close a channel and free all its resources.
364 */
365 static void
366channel_free_contents(channel_T *channel)
367{
368 channel_close(channel, TRUE);
369 channel_clear(channel);
370 ch_log(channel, "Freeing channel");
371}
372
373 static void
374channel_free_channel(channel_T *channel)
375{
376 if (channel->ch_next != NULL)
377 channel->ch_next->ch_prev = channel->ch_prev;
378 if (channel->ch_prev == NULL)
379 first_channel = channel->ch_next;
380 else
381 channel->ch_prev->ch_next = channel->ch_next;
382 vim_free(channel);
383}
384
385 static void
386channel_free(channel_T *channel)
387{
388 if (!in_free_unref_items)
389 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200390 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200391 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200392 else
393 {
394 channel_free_contents(channel);
395 channel_free_channel(channel);
396 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200397 }
398}
399
400/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100401 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100402 * possible, there is no callback to be invoked or the associated job was
403 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100404 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100405 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100406 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100407channel_may_free(channel_T *channel)
408{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100409 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100410 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100411 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100412 return TRUE;
413 }
414 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100415}
416
417/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100418 * Decrement the reference count on "channel" and maybe free it when it goes
419 * down to zero. Don't free it if there is a pending action.
420 * Returns TRUE when the channel is no longer referenced.
421 */
422 int
423channel_unref(channel_T *channel)
424{
425 if (channel != NULL && --channel->ch_refcount <= 0)
426 return channel_may_free(channel);
427 return FALSE;
428}
429
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200430 int
431free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100432{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200433 int did_free = FALSE;
434 channel_T *ch;
435
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200436 /* This is invoked from the garbage collector, which only runs at a safe
437 * point. */
438 ++safe_to_invoke_callback;
439
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200440 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200441 if (!channel_still_useful(ch)
442 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200443 {
444 /* Free the channel and ordinary items it contains, but don't
445 * recurse into Lists, Dictionaries etc. */
446 channel_free_contents(ch);
447 did_free = TRUE;
448 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200449
450 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200451 return did_free;
452}
453
454 void
455free_unused_channels(int copyID, int mask)
456{
457 channel_T *ch;
458 channel_T *ch_next;
459
460 for (ch = first_channel; ch != NULL; ch = ch_next)
461 {
462 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200463 if (!channel_still_useful(ch)
464 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200465 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200466 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467 channel_free_channel(ch);
468 }
469 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100470}
471
Bram Moolenaard04a0202016-01-26 23:30:18 +0100472#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100473
474#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
475 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100476channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100477{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100478 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200479 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100480
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100481 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100482 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200483 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100484 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200485 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100486}
487#endif
488
Bram Moolenaare0874f82016-01-24 20:36:41 +0100489/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100490 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100491 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100492#ifdef FEAT_GUI_X11
493 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200494messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200495 int *unused1 UNUSED,
496 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100497{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100498 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100499}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100500#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100501
Bram Moolenaard04a0202016-01-26 23:30:18 +0100502#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100503# if GTK_CHECK_VERSION(3,0,0)
504 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200505messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200506 GIOCondition unused2 UNUSED,
507 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100508{
509 channel_read_fd(GPOINTER_TO_INT(clientData));
510 return TRUE; /* Return FALSE instead in case the event source is to
511 * be removed after this function returns. */
512}
513# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100514 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200515messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200516 gint unused1 UNUSED,
517 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100519 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520}
Bram Moolenaar98921892016-02-23 17:14:37 +0100521# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522#endif
523
524 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200525channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100526{
Bram Moolenaarde279892016-03-11 22:19:44 +0100527 if (!CH_HAS_GUI)
528 return;
529
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200530 /* gets stuck in handling events for a not connected channel */
531 if (channel->ch_keep_open)
532 return;
533
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100534# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200535 /* Tell notifier we are interested in being called when there is input on
536 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100537 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200538 {
539 ch_log(channel, "Registering part %s with fd %d",
540 part_names[part], channel->ch_part[part].ch_fd);
541
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100542 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100543 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100544 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100545 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200546 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100547 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200548 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100549# else
550# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200551 /* Tell gdk we are interested in being called when there is input on the
552 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100553 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100554 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200555 ch_log(channel, "Registering part %s with fd %d",
556 part_names[part], channel->ch_part[part].ch_fd);
557# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100558 GIOChannel *chnnl = g_io_channel_unix_new(
559 (gint)channel->ch_part[part].ch_fd);
560
561 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
562 chnnl,
563 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200564 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100565 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
566
567 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100568# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100569 channel->ch_part[part].ch_inputHandler = gdk_input_add(
570 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100571 (GdkInputCondition)
572 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200573 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100574 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100575# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200576 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100577# endif
578# endif
579}
580
Bram Moolenaarde279892016-03-11 22:19:44 +0100581 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100582channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100583{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100584 if (channel->CH_SOCK_FD != INVALID_FD)
585 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200586 if (channel->CH_OUT_FD != INVALID_FD
587 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100588 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200589 if (channel->CH_ERR_FD != INVALID_FD
590 && channel->CH_ERR_FD != channel->CH_SOCK_FD
591 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100592 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100593}
594
595/*
596 * Register any of our file descriptors with the GUI event handling system.
597 * Called when the GUI has started.
598 */
599 void
600channel_gui_register_all(void)
601{
Bram Moolenaar77073442016-02-13 23:23:53 +0100602 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100603
Bram Moolenaar77073442016-02-13 23:23:53 +0100604 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100605 channel_gui_register(channel);
606}
607
608 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200609channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100610{
611# ifdef FEAT_GUI_X11
612 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
613 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200614 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100615 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
616 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
617 }
618# else
619# ifdef FEAT_GUI_GTK
620 if (channel->ch_part[part].ch_inputHandler != 0)
621 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200622 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100623# if GTK_CHECK_VERSION(3,0,0)
624 g_source_remove(channel->ch_part[part].ch_inputHandler);
625# else
626 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
627# endif
628 channel->ch_part[part].ch_inputHandler = 0;
629 }
630# endif
631# endif
632}
633
634 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100635channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100636{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200637 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100638
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100639 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100640 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641}
642
643#endif
644
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100645static char *e_cannot_connect = N_("E902: Cannot connect to port");
646
Bram Moolenaard04a0202016-01-26 23:30:18 +0100647/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100648 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100649 * "waittime" is the time in msec to wait for the connection.
650 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100651 * Returns the channel for success.
652 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100653 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100654 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100655channel_open(
656 char *hostname,
657 int port_in,
658 int waittime,
659 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100661 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100663 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100664#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100665 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100666 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667#else
668 int port = port_in;
669#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100670 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100671 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100672
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100673#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674 channel_init_winsock();
675#endif
676
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 channel = add_channel();
678 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100681 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100682 }
683
684 /* Get the server internet address and put into addr structure */
685 /* fill in the socket address structure and connect to server */
686 vim_memset((char *)&server, 0, sizeof(server));
687 server.sin_family = AF_INET;
688 server.sin_port = htons(port);
689 if ((host = gethostbyname(hostname)) == NULL)
690 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100691 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200692 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100693 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100694 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100695 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100696 {
697 char *p;
698
Bram Moolenaar2e324952018-04-14 14:37:07 +0200699 /* When using host->h_addr_list[0] directly ubsan warns for it to not
700 * be aligned. First copy the pointer to avoid that. */
701 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100702 memcpy((char *)&server.sin_addr, p, host->h_length);
703 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100704
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100705 /* On Mac and Solaris a zero timeout almost never works. At least wait
706 * one millisecond. Let's do it for all systems, because we don't know why
707 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 if (waittime == 0)
709 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100710
711 /*
712 * For Unix we need to call connect() again after connect() failed.
713 * On Win32 one time is sufficient.
714 */
715 while (TRUE)
716 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100717 long elapsed_msec = 0;
718 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100719
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100722 sd = socket(AF_INET, SOCK_STREAM, 0);
723 if (sd == -1)
724 {
725 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200726 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100727 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100728 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100729 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 if (waittime >= 0)
732 {
733 /* Make connect() non-blocking. */
734 if (
735#ifdef _WIN32
736 ioctlsocket(sd, FIONBIO, &val) < 0
737#else
738 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
739#endif
740 )
741 {
742 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200743 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100744 "channel_open: Connect failed with errno %d", errno);
745 sock_close(sd);
746 channel_free(channel);
747 return NULL;
748 }
749 }
750
751 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200752 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100753 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 if (ret == 0)
756 /* The connection could be established. */
757 break;
758
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100759 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100760 if (waittime < 0 || (errno != EWOULDBLOCK
761 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100762#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100764#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 ))
766 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200767 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100768 "channel_open: Connect failed with errno %d", errno);
769 PERROR(_(e_cannot_connect));
770 sock_close(sd);
771 channel_free(channel);
772 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100773 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100774
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100775 /* Limit the waittime to 50 msec. If it doesn't work within this
776 * time we close the socket and try creating it again. */
777 waitnow = waittime > 50 ? 50 : waittime;
778
Bram Moolenaar045a2842016-03-08 22:33:07 +0100779 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100780 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781#ifndef WIN32
782 if (errno != ECONNREFUSED)
783#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 {
785 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100786 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100787 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100788#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100789 int so_error = 0;
790 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 struct timeval start_tv;
792 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100793#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100794 FD_ZERO(&rfds);
795 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100796 FD_ZERO(&wfds);
797 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100798
Bram Moolenaar562ca712016-03-09 21:50:05 +0100799 tv.tv_sec = waitnow / 1000;
800 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801#ifndef WIN32
802 gettimeofday(&start_tv, NULL);
803#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200804 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100805 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100807
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 if (ret < 0)
809 {
810 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200811 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100812 "channel_open: Connect failed with errno %d", errno);
813 PERROR(_(e_cannot_connect));
814 sock_close(sd);
815 channel_free(channel);
816 return NULL;
817 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818
Bram Moolenaare081e212016-02-28 22:33:46 +0100819#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100820 /* On Win32: select() is expected to work and wait for up to
821 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100822 if (FD_ISSET(sd, &wfds))
823 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100824 elapsed_msec = waitnow;
825 if (waittime > 1 && elapsed_msec < waittime)
826 {
827 waittime -= elapsed_msec;
828 continue;
829 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100830#else
831 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100832 * After putting the socket in non-blocking mode, connect() will
833 * return EINPROGRESS, select() will not wait (as if writing is
834 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100835 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100837 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100838 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100839 {
840 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100841 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100842 if (ret < 0 || (so_error != 0
843 && so_error != EWOULDBLOCK
844 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100845# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100846 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100847# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 ))
849 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200850 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100851 "channel_open: Connect failed with errno %d",
852 so_error);
853 PERROR(_(e_cannot_connect));
854 sock_close(sd);
855 channel_free(channel);
856 return NULL;
857 }
858 }
859
Bram Moolenaar045a2842016-03-08 22:33:07 +0100860 if (FD_ISSET(sd, &wfds) && so_error == 0)
861 /* Did not detect an error, connection is established. */
862 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100863
Bram Moolenaar045a2842016-03-08 22:33:07 +0100864 gettimeofday(&end_tv, NULL);
865 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
866 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100867#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100868 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100869
870#ifndef WIN32
871 if (waittime > 1 && elapsed_msec < waittime)
872 {
873 /* The port isn't ready but we also didn't get an error.
874 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100875 * yet. Select() may return early, wait until the remaining
876 * "waitnow" and try again. */
877 waitnow -= elapsed_msec;
878 waittime -= elapsed_msec;
879 if (waitnow > 0)
880 {
881 mch_delay((long)waitnow, TRUE);
882 ui_breakcheck();
883 waittime -= waitnow;
884 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100885 if (!got_int)
886 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100887 if (waittime <= 0)
888 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100889 waittime = 1;
890 continue;
891 }
892 /* we were interrupted, behave as if timed out */
893 }
894#endif
895
896 /* We timed out. */
897 ch_error(channel, "Connection timed out");
898 sock_close(sd);
899 channel_free(channel);
900 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100901 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100902
Bram Moolenaar045a2842016-03-08 22:33:07 +0100903 ch_log(channel, "Connection made");
904
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100905 if (waittime >= 0)
906 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100907#ifdef _WIN32
908 val = 0;
909 ioctlsocket(sd, FIONBIO, &val);
910#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100911 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100912#endif
913 }
914
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100915 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100916 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100917 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
918 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200919 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100920
921#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100922 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100923#endif
924
Bram Moolenaar77073442016-02-13 23:23:53 +0100925 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100926}
927
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100928/*
929 * Implements ch_open().
930 */
931 channel_T *
932channel_open_func(typval_T *argvars)
933{
934 char_u *address;
935 char_u *p;
936 char *rest;
937 int port;
938 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200939 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100940
941 address = get_tv_string(&argvars[0]);
942 if (argvars[1].v_type != VAR_UNKNOWN
943 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
944 {
945 EMSG(_(e_invarg));
946 return NULL;
947 }
948
949 /* parse address */
950 p = vim_strchr(address, ':');
951 if (p == NULL)
952 {
953 EMSG2(_(e_invarg2), address);
954 return NULL;
955 }
956 *p++ = NUL;
957 port = strtol((char *)p, &rest, 10);
958 if (*address == NUL || port <= 0 || *rest != NUL)
959 {
960 p[-1] = ':';
961 EMSG2(_(e_invarg2), address);
962 return NULL;
963 }
964
965 /* parse options */
966 clear_job_options(&opt);
967 opt.jo_mode = MODE_JSON;
968 opt.jo_timeout = 2000;
969 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200970 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200971 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100972 if (opt.jo_timeout < 0)
973 {
974 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200975 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100976 }
977
978 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
979 if (channel != NULL)
980 {
981 opt.jo_set = JO_ALL;
982 channel_set_options(channel, &opt);
983 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200984theend:
985 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100986 return channel;
987}
988
Bram Moolenaarde279892016-03-11 22:19:44 +0100989 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200990ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100991{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200992 sock_T *fd = &channel->ch_part[part].ch_fd;
993
Bram Moolenaarde279892016-03-11 22:19:44 +0100994 if (*fd != INVALID_FD)
995 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200996 if (part == PART_SOCK)
997 sock_close(*fd);
998 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200999 {
1000 /* When using a pty the same FD is set on multiple parts, only
1001 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001002 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1003 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1004 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001005 {
1006#ifdef WIN32
1007 if (channel->ch_named_pipe)
1008 DisconnectNamedPipe((HANDLE)fd);
1009#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001010 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001011 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001012 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001013 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001014
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001015 /* channel is closed, may want to end the job if it was the last */
1016 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001017 }
1018}
1019
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001020 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001021channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001022{
Bram Moolenaarde279892016-03-11 22:19:44 +01001023 if (in != INVALID_FD)
1024 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001025 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001026 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001027# if defined(UNIX)
1028 /* Do not end the job when all output channels are closed, wait until
1029 * the job ended. */
1030 if (isatty(in))
1031 channel->ch_to_be_closed |= (1U << PART_IN);
1032# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001033 }
1034 if (out != INVALID_FD)
1035 {
1036# if defined(FEAT_GUI)
1037 channel_gui_unregister_one(channel, PART_OUT);
1038# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001039 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001040 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001041 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001042# if defined(FEAT_GUI)
1043 channel_gui_register_one(channel, PART_OUT);
1044# endif
1045 }
1046 if (err != INVALID_FD)
1047 {
1048# if defined(FEAT_GUI)
1049 channel_gui_unregister_one(channel, PART_ERR);
1050# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001051 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001052 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001053 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001054# if defined(FEAT_GUI)
1055 channel_gui_register_one(channel, PART_ERR);
1056# endif
1057 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001058}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001059
Bram Moolenaard6051b52016-02-28 15:49:03 +01001060/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001061 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001062 * This does not keep a refcount, when the job is freed ch_job is cleared.
1063 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001064 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001065channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001066{
Bram Moolenaar77073442016-02-13 23:23:53 +01001067 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001068
1069 channel_set_options(channel, options);
1070
1071 if (job->jv_in_buf != NULL)
1072 {
1073 chanpart_T *in_part = &channel->ch_part[PART_IN];
1074
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001075 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001076 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001077 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001078 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001079 {
1080 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1081 {
1082 /* Special mode: send last-but-one line when appending a line
1083 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001084 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001085 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001086 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001087 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001088 }
1089 else
1090 in_part->ch_buf_top = options->jo_in_top;
1091 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001092 else
1093 in_part->ch_buf_top = 1;
1094 if (options->jo_set & JO_IN_BOT)
1095 in_part->ch_buf_bot = options->jo_in_bot;
1096 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001097 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001098 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001099}
1100
1101/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001102 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001103 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001104 */
1105 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001106find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001107{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001108 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001109 buf_T *save_curbuf = curbuf;
1110
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001111 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001112 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001113 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001114 if (buf == NULL)
1115 buf = buflist_findname_exp(name);
1116 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001117 if (buf == NULL)
1118 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001119 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001120 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001121 if (buf == NULL)
1122 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001123 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001124 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001125#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001126 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1127 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001128#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001129 if (curbuf->b_ml.ml_mfp == NULL)
1130 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001131 if (msg)
1132 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001133 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001134 changed_bytes(1, 0);
1135 curbuf = save_curbuf;
1136 }
1137
1138 return buf;
1139}
1140
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001141 static void
1142set_callback(
1143 char_u **cbp,
1144 partial_T **pp,
1145 char_u *callback,
1146 partial_T *partial)
1147{
1148 free_callback(*cbp, *pp);
1149 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001150 {
1151 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001152 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001153 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001154 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001155 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001156 func_ref(*cbp);
1157 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001158 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001159 else
1160 *cbp = NULL;
1161 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001162 if (partial != NULL)
1163 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001164}
1165
Bram Moolenaar187db502016-02-27 14:44:26 +01001166/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001167 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001168 */
1169 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001170channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001171{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001172 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001173
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001174 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001175 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001176 channel->ch_part[part].ch_mode = opt->jo_mode;
1177 if (opt->jo_set & JO_IN_MODE)
1178 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1179 if (opt->jo_set & JO_OUT_MODE)
1180 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1181 if (opt->jo_set & JO_ERR_MODE)
1182 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001183 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001184
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001185 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001186 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001187 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1188 if (opt->jo_set & JO_OUT_TIMEOUT)
1189 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1190 if (opt->jo_set & JO_ERR_TIMEOUT)
1191 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001192 if (opt->jo_set & JO_BLOCK_WRITE)
1193 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001194
1195 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001196 set_callback(&channel->ch_callback, &channel->ch_partial,
1197 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001198 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001199 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1200 &channel->ch_part[PART_OUT].ch_partial,
1201 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001202 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001203 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1204 &channel->ch_part[PART_ERR].ch_partial,
1205 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001206 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001207 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1208 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001209 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001210
1211 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1212 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001213 buf_T *buf;
1214
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001215 /* writing output to a buffer. Default mode is NL. */
1216 if (!(opt->jo_set & JO_OUT_MODE))
1217 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001218 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001219 {
1220 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1221 if (buf == NULL)
1222 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1223 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001224 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001225 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001226 int msg = TRUE;
1227
1228 if (opt->jo_set2 & JO2_OUT_MSG)
1229 msg = opt->jo_message[PART_OUT];
1230 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001231 }
1232 if (buf != NULL)
1233 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001234 if (opt->jo_set & JO_OUT_MODIFIABLE)
1235 channel->ch_part[PART_OUT].ch_nomodifiable =
1236 !opt->jo_modifiable[PART_OUT];
1237
1238 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1239 {
1240 EMSG(_(e_modifiable));
1241 }
1242 else
1243 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001244 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001245 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001246 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001247 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001249 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001250
1251 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1252 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1253 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1254 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001255 buf_T *buf;
1256
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001257 /* writing err to a buffer. Default mode is NL. */
1258 if (!(opt->jo_set & JO_ERR_MODE))
1259 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1260 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001261 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001262 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001263 {
1264 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1265 if (buf == NULL)
1266 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1267 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001268 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001269 {
1270 int msg = TRUE;
1271
1272 if (opt->jo_set2 & JO2_ERR_MSG)
1273 msg = opt->jo_message[PART_ERR];
1274 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1275 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001276 if (buf != NULL)
1277 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001278 if (opt->jo_set & JO_ERR_MODIFIABLE)
1279 channel->ch_part[PART_ERR].ch_nomodifiable =
1280 !opt->jo_modifiable[PART_ERR];
1281 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1282 {
1283 EMSG(_(e_modifiable));
1284 }
1285 else
1286 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001287 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001288 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001289 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001290 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001291 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001292 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001293
1294 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1295 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1296 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001297}
1298
1299/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001300 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001301 */
1302 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001303channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001304 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001305 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001306 char_u *callback,
1307 partial_T *partial,
1308 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001309{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001310 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001311 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1312
1313 if (item != NULL)
1314 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001315 item->cq_partial = partial;
1316 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001317 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001318 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001319 item->cq_callback = callback;
1320 }
1321 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001322 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001323 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001324 func_ref(item->cq_callback);
1325 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001326 item->cq_seq_nr = id;
1327 item->cq_prev = head->cq_prev;
1328 head->cq_prev = item;
1329 item->cq_next = NULL;
1330 if (item->cq_prev == NULL)
1331 head->cq_next = item;
1332 else
1333 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001334 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001335}
1336
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001337 static void
1338write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1339{
1340 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001341 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001342 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001343 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001344
Bram Moolenaar655da312016-05-28 22:22:34 +02001345 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001346 if ((p = alloc(len + 2)) == NULL)
1347 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001348 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001349
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001350 if (channel->ch_write_text_mode)
1351 p[len] = CAR;
1352 else
1353 {
1354 for (i = 0; i < len; ++i)
1355 if (p[i] == NL)
1356 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001357
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001358 p[len] = NL;
1359 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001360 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001361 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001362 vim_free(p);
1363}
1364
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001365/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001366 * Return TRUE if "channel" can be written to.
1367 * Returns FALSE if the input is closed or the write would block.
1368 */
1369 static int
1370can_write_buf_line(channel_T *channel)
1371{
1372 chanpart_T *in_part = &channel->ch_part[PART_IN];
1373
1374 if (in_part->ch_fd == INVALID_FD)
1375 return FALSE; /* pipe was closed */
1376
1377 /* for testing: block every other attempt to write */
1378 if (in_part->ch_block_write == 1)
1379 in_part->ch_block_write = -1;
1380 else if (in_part->ch_block_write == -1)
1381 in_part->ch_block_write = 1;
1382
1383 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1384#ifndef WIN32
1385 {
1386# if defined(HAVE_SELECT)
1387 struct timeval tval;
1388 fd_set wfds;
1389 int ret;
1390
1391 FD_ZERO(&wfds);
1392 FD_SET((int)in_part->ch_fd, &wfds);
1393 tval.tv_sec = 0;
1394 tval.tv_usec = 0;
1395 for (;;)
1396 {
1397 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1398# ifdef EINTR
1399 SOCK_ERRNO;
1400 if (ret == -1 && errno == EINTR)
1401 continue;
1402# endif
1403 if (ret <= 0 || in_part->ch_block_write == 1)
1404 {
1405 if (ret > 0)
1406 ch_log(channel, "FAKED Input not ready for writing");
1407 else
1408 ch_log(channel, "Input not ready for writing");
1409 return FALSE;
1410 }
1411 break;
1412 }
1413# else
1414 struct pollfd fds;
1415
1416 fds.fd = in_part->ch_fd;
1417 fds.events = POLLOUT;
1418 if (poll(&fds, 1, 0) <= 0)
1419 {
1420 ch_log(channel, "Input not ready for writing");
1421 return FALSE;
1422 }
1423 if (in_part->ch_block_write == 1)
1424 {
1425 ch_log(channel, "FAKED Input not ready for writing");
1426 return FALSE;
1427 }
1428# endif
1429 }
1430#endif
1431 return TRUE;
1432}
1433
1434/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001435 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001436 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001437 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001438channel_write_in(channel_T *channel)
1439{
1440 chanpart_T *in_part = &channel->ch_part[PART_IN];
1441 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001442 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001443 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001444
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001445 if (buf == NULL || in_part->ch_buf_append)
1446 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001447 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001448 {
1449 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001450 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001451 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001452 return;
1453 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001454
1455 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1456 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1457 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001458 if (!can_write_buf_line(channel))
1459 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001460 write_buf_line(buf, lnum, channel);
1461 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001462 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001463
1464 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001465 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001466 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001467 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001468
Bram Moolenaar014069a2016-03-03 22:51:40 +01001469 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001470 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001471 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001472#if defined(FEAT_TERMINAL)
1473 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001474 if (channel->ch_job != NULL)
1475 term_send_eof(channel);
1476#endif
1477
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001478 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001479 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001480 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001481
1482 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001483 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001484 }
1485 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001486 ch_log(channel, "Still %ld more lines to write",
1487 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001488}
1489
1490/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001491 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001492 */
1493 void
1494channel_buffer_free(buf_T *buf)
1495{
1496 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001497 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001498
1499 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001500 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001501 {
1502 chanpart_T *ch_part = &channel->ch_part[part];
1503
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001504 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001505 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001506 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001507 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001508 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001509 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001510 }
1511}
1512
1513/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001514 * Write any lines waiting to be written to "channel".
1515 */
1516 static void
1517channel_write_input(channel_T *channel)
1518{
1519 chanpart_T *in_part = &channel->ch_part[PART_IN];
1520
1521 if (in_part->ch_writeque.wq_next != NULL)
1522 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1523 else if (in_part->ch_bufref.br_buf != NULL)
1524 {
1525 if (in_part->ch_buf_append)
1526 channel_write_new_lines(in_part->ch_bufref.br_buf);
1527 else
1528 channel_write_in(channel);
1529 }
1530}
1531
1532/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001533 * Write any lines waiting to be written to a channel.
1534 */
1535 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001536channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001537{
1538 channel_T *channel;
1539
1540 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001541 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001542}
1543
1544/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001545 * Write appended lines above the last one in "buf" to the channel.
1546 */
1547 void
1548channel_write_new_lines(buf_T *buf)
1549{
1550 channel_T *channel;
1551 int found_one = FALSE;
1552
1553 /* There could be more than one channel for the buffer, loop over all of
1554 * them. */
1555 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1556 {
1557 chanpart_T *in_part = &channel->ch_part[PART_IN];
1558 linenr_T lnum;
1559 int written = 0;
1560
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001561 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001562 {
1563 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001564 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001565 found_one = TRUE;
1566 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1567 ++lnum)
1568 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001569 if (!can_write_buf_line(channel))
1570 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001571 write_buf_line(buf, lnum, channel);
1572 ++written;
1573 }
1574
1575 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001576 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001577 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001578 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001579 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001580 ch_log(channel, "Still %ld more lines to write",
1581 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001582
1583 in_part->ch_buf_bot = lnum;
1584 }
1585 }
1586 if (!found_one)
1587 buf->b_write_to_channel = FALSE;
1588}
1589
1590/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001591 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001592 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001593 */
1594 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001595invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1596 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001597{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001598 typval_T rettv;
1599 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001600
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001601 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001602 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001603
Bram Moolenaar77073442016-02-13 23:23:53 +01001604 argv[0].v_type = VAR_CHANNEL;
1605 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001606
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001607 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1608 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001609 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001610 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001611}
1612
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001613/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001614 * Return the first node from "channel"/"part" without removing it.
1615 * Returns NULL if there is nothing.
1616 */
1617 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001618channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001619{
1620 readq_T *head = &channel->ch_part[part].ch_head;
1621
1622 return head->rq_next;
1623}
1624
1625/*
1626 * Return a pointer to the first NL in "node".
1627 * Skips over NUL characters.
1628 * Returns NULL if there is no NL.
1629 */
1630 char_u *
1631channel_first_nl(readq_T *node)
1632{
1633 char_u *buffer = node->rq_buffer;
1634 long_u i;
1635
1636 for (i = 0; i < node->rq_buflen; ++i)
1637 if (buffer[i] == NL)
1638 return buffer + i;
1639 return NULL;
1640}
1641
1642/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001643 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001644 * The caller must free it.
1645 * Returns NULL if there is nothing.
1646 */
1647 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001648channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001649{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001650 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001651 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001652 char_u *p;
1653
Bram Moolenaar77073442016-02-13 23:23:53 +01001654 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001655 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001656 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001657 p = node->rq_buffer;
1658 head->rq_next = node->rq_next;
1659 if (node->rq_next == NULL)
1660 head->rq_prev = NULL;
1661 else
1662 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001663 vim_free(node);
1664 return p;
1665}
1666
1667/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001668 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001669 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670 */
1671 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001672channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001674 readq_T *head = &channel->ch_part[part].ch_head;
1675 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001676 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001677 char_u *res;
1678 char_u *p;
1679
1680 /* If there is only one buffer just get that one. */
1681 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1682 return channel_get(channel, part);
1683
1684 /* Concatenate everything into one buffer. */
1685 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001686 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001687 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001688 if (res == NULL)
1689 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001690 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001691 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001692 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001693 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001694 p += node->rq_buflen;
1695 }
1696 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001697
1698 /* Free all buffers */
1699 do
1700 {
1701 p = channel_get(channel, part);
1702 vim_free(p);
1703 } while (p != NULL);
1704
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001705 /* turn all NUL into NL */
1706 while (len > 0)
1707 {
1708 --len;
1709 if (res[len] == NUL)
1710 res[len] = NL;
1711 }
1712
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001713 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001714}
1715
1716/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001717 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001718 * Caller must check these bytes are available.
1719 */
1720 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001721channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001722{
1723 readq_T *head = &channel->ch_part[part].ch_head;
1724 readq_T *node = head->rq_next;
1725 char_u *buf = node->rq_buffer;
1726
1727 mch_memmove(buf, buf + len, node->rq_buflen - len);
1728 node->rq_buflen -= len;
1729}
1730
1731/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001732 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001733 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001735 */
1736 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001737channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001738{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001739 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001740 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001741 readq_T *last_node;
1742 readq_T *n;
1743 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001744 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001745 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001746
Bram Moolenaar77073442016-02-13 23:23:53 +01001747 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001748 return FAIL;
1749
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001750 last_node = node->rq_next;
1751 len = node->rq_buflen + last_node->rq_buflen + 1;
1752 if (want_nl)
1753 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001754 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001755 {
1756 last_node = last_node->rq_next;
1757 len += last_node->rq_buflen;
1758 }
1759
1760 p = newbuf = alloc(len);
1761 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001762 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001763 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001764 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001765 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001766 node->rq_buffer = newbuf;
1767 for (n = node; n != last_node; )
1768 {
1769 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001770 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001771 p += n->rq_buflen;
1772 vim_free(n->rq_buffer);
1773 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001774 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001775
1776 /* dispose of the collapsed nodes and their buffers */
1777 for (n = node->rq_next; n != last_node; )
1778 {
1779 n = n->rq_next;
1780 vim_free(n->rq_prev);
1781 }
1782 node->rq_next = last_node->rq_next;
1783 if (last_node->rq_next == NULL)
1784 head->rq_prev = node;
1785 else
1786 last_node->rq_next->rq_prev = node;
1787 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001788 return OK;
1789}
1790
1791/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001792 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001793 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001794 * Returns OK or FAIL.
1795 */
1796 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001797channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001798 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001799{
1800 readq_T *node;
1801 readq_T *head = &channel->ch_part[part].ch_head;
1802 char_u *p;
1803 int i;
1804
1805 node = (readq_T *)alloc(sizeof(readq_T));
1806 if (node == NULL)
1807 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001808 /* A NUL is added at the end, because netbeans code expects that.
1809 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001810 node->rq_buffer = alloc(len + 1);
1811 if (node->rq_buffer == NULL)
1812 {
1813 vim_free(node);
1814 return FAIL; /* out of memory */
1815 }
1816
1817 if (channel->ch_part[part].ch_mode == MODE_NL)
1818 {
1819 /* Drop any CR before a NL. */
1820 p = node->rq_buffer;
1821 for (i = 0; i < len; ++i)
1822 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1823 *p++ = buf[i];
1824 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001825 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001826 }
1827 else
1828 {
1829 mch_memmove(node->rq_buffer, buf, len);
1830 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001831 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001832 }
1833
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001834 if (prepend)
1835 {
1836 /* preend node to the head of the queue */
1837 node->rq_next = head->rq_next;
1838 node->rq_prev = NULL;
1839 if (head->rq_next == NULL)
1840 head->rq_prev = node;
1841 else
1842 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001844 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001845 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001846 {
1847 /* append node to the tail of the queue */
1848 node->rq_next = NULL;
1849 node->rq_prev = head->rq_prev;
1850 if (head->rq_prev == NULL)
1851 head->rq_next = node;
1852 else
1853 head->rq_prev->rq_next = node;
1854 head->rq_prev = node;
1855 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001856
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001857 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001859 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001860 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001861 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001862 fprintf(log_fd, "'\n");
1863 }
1864 return OK;
1865}
1866
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001867/*
1868 * Try to fill the buffer of "reader".
1869 * Returns FALSE when nothing was added.
1870 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001871 static int
1872channel_fill(js_read_T *reader)
1873{
1874 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001875 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001876 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001877 int keeplen;
1878 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001879 char_u *p;
1880
1881 if (next == NULL)
1882 return FALSE;
1883
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001884 keeplen = reader->js_end - reader->js_buf;
1885 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001886 {
1887 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001888 addlen = (int)STRLEN(next);
1889 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001890 if (p == NULL)
1891 {
1892 vim_free(next);
1893 return FALSE;
1894 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001895 mch_memmove(p, reader->js_buf, keeplen);
1896 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001897 vim_free(next);
1898 next = p;
1899 }
1900
1901 vim_free(reader->js_buf);
1902 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001903 return TRUE;
1904}
1905
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001906/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001907 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001908 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001909 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001910 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001911 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001912channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001913{
1914 js_read_T reader;
1915 typval_T listtv;
1916 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001917 chanpart_T *chanpart = &channel->ch_part[part];
1918 jsonq_T *head = &chanpart->ch_json_head;
1919 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001920 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001921
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001922 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001923 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001924
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001925 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001926 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001927 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001928 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001929 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001930
1931 /* When a message is incomplete we wait for a short while for more to
1932 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001933 * or list will make us hang.
1934 * Do not generate error messages, they will be written in a channel log. */
1935 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001936 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001937 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001938 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001939 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001940 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001941 /* Only accept the response when it is a list with at least two
1942 * items. */
1943 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001944 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001945 if (listtv.v_type != VAR_LIST)
1946 ch_error(channel, "Did not receive a list, discarding");
1947 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001948 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001949 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001950 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001951 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001952 else
1953 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001954 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1955 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001956 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001957 else
1958 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001959 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001960 item->jq_value = alloc_tv();
1961 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001962 {
1963 vim_free(item);
1964 clear_tv(&listtv);
1965 }
1966 else
1967 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001968 *item->jq_value = listtv;
1969 item->jq_prev = head->jq_prev;
1970 head->jq_prev = item;
1971 item->jq_next = NULL;
1972 if (item->jq_prev == NULL)
1973 head->jq_next = item;
1974 else
1975 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001976 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001977 }
1978 }
1979 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001980
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001981 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001982 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001983 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001984 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001985 size_t buflen = STRLEN(reader.js_buf);
1986
1987 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001988 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001989 /* First time encountering incomplete message or after receiving
1990 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001991 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001992 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001993 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001995 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001996#ifdef WIN32
1997 chanpart->ch_deadline = GetTickCount() + 100L;
1998#else
1999 gettimeofday(&chanpart->ch_deadline, NULL);
2000 chanpart->ch_deadline.tv_usec += 100 * 1000;
2001 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2002 {
2003 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2004 ++chanpart->ch_deadline.tv_sec;
2005 }
2006#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002007 }
2008 else
2009 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002010 int timeout;
2011#ifdef WIN32
2012 timeout = GetTickCount() > chanpart->ch_deadline;
2013#else
2014 {
2015 struct timeval now_tv;
2016
2017 gettimeofday(&now_tv, NULL);
2018 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2019 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2020 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2021 }
2022#endif
2023 if (timeout)
2024 {
2025 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002026 chanpart->ch_wait_len = 0;
2027 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002028 }
2029 else
2030 {
2031 reader.js_used = 0;
2032 ch_log(channel, "still waiting on incomplete message");
2033 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002034 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002035 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002036
2037 if (status == FAIL)
2038 {
2039 ch_error(channel, "Decoding failed - discarding input");
2040 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002041 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002042 }
2043 else if (reader.js_buf[reader.js_used] != NUL)
2044 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002045 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002046 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002047 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2048 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002049 ret = status == MAYBE ? FALSE: TRUE;
2050 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002051 else
2052 ret = FALSE;
2053
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002054 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002055 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002056}
2057
2058/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002059 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002060 */
2061 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002062remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002063{
Bram Moolenaar77073442016-02-13 23:23:53 +01002064 if (node->cq_prev == NULL)
2065 head->cq_next = node->cq_next;
2066 else
2067 node->cq_prev->cq_next = node->cq_next;
2068 if (node->cq_next == NULL)
2069 head->cq_prev = node->cq_prev;
2070 else
2071 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002072}
2073
2074/*
2075 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002076 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002077 */
2078 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002079remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002080{
Bram Moolenaar77073442016-02-13 23:23:53 +01002081 if (node->jq_prev == NULL)
2082 head->jq_next = node->jq_next;
2083 else
2084 node->jq_prev->jq_next = node->jq_next;
2085 if (node->jq_next == NULL)
2086 head->jq_prev = node->jq_prev;
2087 else
2088 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002089 vim_free(node);
2090}
2091
2092/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002093 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002094 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002095 * When "id" is zero or negative jut get the first message. But not the one
2096 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002097 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002098 * Return OK when found and return the value in "rettv".
2099 * Return FAIL otherwise.
2100 */
2101 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002102channel_get_json(
2103 channel_T *channel,
2104 ch_part_T part,
2105 int id,
2106 int without_callback,
2107 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002108{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002109 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002110 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002111
Bram Moolenaar77073442016-02-13 23:23:53 +01002112 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002113 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002114 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002115 typval_T *tv = &l->lv_first->li_tv;
2116
Bram Moolenaar958dc692016-12-01 15:34:12 +01002117 if ((without_callback || !item->jq_no_callback)
2118 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002119 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002120 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002121 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002122 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002123 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002124 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002125 ch_log(channel, "Getting JSON message %ld",
2126 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002127 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128 return OK;
2129 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002130 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002131 }
2132 return FAIL;
2133}
2134
Bram Moolenaar958dc692016-12-01 15:34:12 +01002135/*
2136 * Put back "rettv" into the JSON queue, there was no callback for it.
2137 * Takes over the values in "rettv".
2138 */
2139 static void
2140channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2141{
2142 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2143 jsonq_T *item = head->jq_next;
2144 jsonq_T *newitem;
2145
2146 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2147 /* last item was pushed back, append to the end */
2148 item = NULL;
2149 else while (item != NULL && item->jq_no_callback)
2150 /* append after the last item that was pushed back */
2151 item = item->jq_next;
2152
2153 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2154 if (newitem == NULL)
2155 clear_tv(rettv);
2156 else
2157 {
2158 newitem->jq_value = alloc_tv();
2159 if (newitem->jq_value == NULL)
2160 {
2161 vim_free(newitem);
2162 clear_tv(rettv);
2163 }
2164 else
2165 {
2166 newitem->jq_no_callback = FALSE;
2167 *newitem->jq_value = *rettv;
2168 if (item == NULL)
2169 {
2170 /* append to the end */
2171 newitem->jq_prev = head->jq_prev;
2172 head->jq_prev = newitem;
2173 newitem->jq_next = NULL;
2174 if (newitem->jq_prev == NULL)
2175 head->jq_next = newitem;
2176 else
2177 newitem->jq_prev->jq_next = newitem;
2178 }
2179 else
2180 {
2181 /* append after "item" */
2182 newitem->jq_prev = item;
2183 newitem->jq_next = item->jq_next;
2184 item->jq_next = newitem;
2185 if (newitem->jq_next == NULL)
2186 head->jq_prev = newitem;
2187 else
2188 newitem->jq_next->jq_prev = newitem;
2189 }
2190 }
2191 }
2192}
2193
Bram Moolenaarece61b02016-02-20 21:39:05 +01002194#define CH_JSON_MAX_ARGS 4
2195
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002196/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002197 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 * "argv[0]" is the command string.
2199 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002200 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002201 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002202channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002203{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002204 char_u *cmd = argv[0].vval.v_string;
2205 char_u *arg;
2206 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002207
Bram Moolenaarece61b02016-02-20 21:39:05 +01002208 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002209 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002210 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002212 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002213 return;
2214 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002215 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002216 if (arg == NULL)
2217 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002218
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002219 if (STRCMP(cmd, "ex") == 0)
2220 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002221 int save_called_emsg = called_emsg;
2222
2223 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002224 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002225 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002226 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002227 --emsg_silent;
2228 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002229 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002230 (char *)get_vim_var_str(VV_ERRMSG));
2231 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002232 }
2233 else if (STRCMP(cmd, "normal") == 0)
2234 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002235 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002236
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002237 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002238 ea.arg = arg;
2239 ea.addr_count = 0;
2240 ea.forceit = TRUE; /* no mapping */
2241 ex_normal(&ea);
2242 }
2243 else if (STRCMP(cmd, "redraw") == 0)
2244 {
2245 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002246
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002247 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002248 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002249 ex_redraw(&ea);
2250 showruler(FALSE);
2251 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002252 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002253 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002254 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002255 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002256 int is_call = cmd[0] == 'c';
2257 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002258
Bram Moolenaarece61b02016-02-20 21:39:05 +01002259 if (argv[id_idx].v_type != VAR_UNKNOWN
2260 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002261 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002262 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002263 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002264 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002265 }
2266 else if (is_call && argv[2].v_type != VAR_LIST)
2267 {
2268 ch_error(channel, "third argument for call must be a list");
2269 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002270 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002271 }
2272 else
2273 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002274 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002275 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002276 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002277 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002278
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002279 /* Don't pollute the display with errors. */
2280 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002281 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002282 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002283 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002284 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002285 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002286 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002287 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002288 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002289 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2290 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002291 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002292
2293 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002294 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002295 int id = argv[id_idx].vval.v_number;
2296
Bram Moolenaar55fab432016-02-07 16:53:13 +01002297 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002298 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002299 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002300 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002301 /* If evaluation failed or the result can't be encoded
2302 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002303 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002304 err_tv.v_type = VAR_STRING;
2305 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002306 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002307 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002308 if (json != NULL)
2309 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002310 channel_send(channel,
2311 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002312 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002313 vim_free(json);
2314 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002315 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002316 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002317 if (tv == &res_tv)
2318 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002319 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002320 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002321 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002322 }
2323 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002324 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002325 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002326 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002327 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002328}
2329
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002330/*
2331 * Invoke the callback at "cbhead".
2332 * Does not redraw but sets channel_need_redraw.
2333 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002334 static void
2335invoke_one_time_callback(
2336 channel_T *channel,
2337 cbq_T *cbhead,
2338 cbq_T *item,
2339 typval_T *argv)
2340{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002341 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002342 (char *)item->cq_callback);
2343 /* Remove the item from the list first, if the callback
2344 * invokes ch_close() the list will be cleared. */
2345 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002346 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002347 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002348 vim_free(item);
2349}
2350
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002351 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002352append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002353{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002354 bufref_T save_curbuf = {NULL, 0, 0};
2355 win_T *save_curwin = NULL;
2356 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002357 linenr_T lnum = buffer->b_ml.ml_line_count;
2358 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002359 chanpart_T *ch_part = &channel->ch_part[part];
2360 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002361 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002362
2363 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2364 {
2365 if (!ch_part->ch_nomod_error)
2366 {
2367 ch_error(channel, "Buffer is not modifiable, cannot append");
2368 ch_part->ch_nomod_error = TRUE;
2369 }
2370 return;
2371 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002372
2373 /* If the buffer is also used as input insert above the last
2374 * line. Don't write these lines. */
2375 if (save_write_to)
2376 {
2377 --lnum;
2378 buffer->b_write_to_channel = FALSE;
2379 }
2380
2381 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002382 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002383
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002384 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002385
2386 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2387 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2388
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002389 u_sync(TRUE);
2390 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002391 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002392
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002393 if (empty)
2394 {
2395 /* The buffer is empty, replace the first (dummy) line. */
2396 ml_replace(lnum, msg, TRUE);
2397 lnum = 0;
2398 }
2399 else
2400 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002401 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002402
2403 /* Restore curbuf/curwin/curtab */
2404 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2405
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002406 if (ch_part->ch_nomodifiable)
2407 buffer->b_p_ma = FALSE;
2408 else
2409 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002410
2411 if (buffer->b_nwindows > 0)
2412 {
2413 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002414
2415 FOR_ALL_WINDOWS(wp)
2416 {
2417 if (wp->w_buffer == buffer
2418 && (save_write_to
2419 ? wp->w_cursor.lnum == lnum + 1
2420 : (wp->w_cursor.lnum == lnum
2421 && wp->w_cursor.col == 0)))
2422 {
2423 ++wp->w_cursor.lnum;
2424 save_curwin = curwin;
2425 curwin = wp;
2426 curbuf = curwin->w_buffer;
2427 scroll_cursor_bot(0, FALSE);
2428 curwin = save_curwin;
2429 curbuf = curwin->w_buffer;
2430 }
2431 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002432 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002433 channel_need_redraw = TRUE;
2434 }
2435
2436 if (save_write_to)
2437 {
2438 channel_T *ch;
2439
2440 /* Find channels reading from this buffer and adjust their
2441 * next-to-read line number. */
2442 buffer->b_write_to_channel = TRUE;
2443 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2444 {
2445 chanpart_T *in_part = &ch->ch_part[PART_IN];
2446
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002447 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002448 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2449 }
2450 }
2451}
2452
Bram Moolenaar437905c2016-04-26 19:01:05 +02002453 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002454drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002455{
2456 char_u *msg;
2457
2458 while ((msg = channel_get(channel, part)) != NULL)
2459 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002460 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002461 vim_free(msg);
2462 }
2463}
2464
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002465/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002466 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002467 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002468 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002469 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002470 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002471may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002472{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002473 char_u *msg = NULL;
2474 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002475 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002476 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002477 chanpart_T *ch_part = &channel->ch_part[part];
2478 ch_mode_T ch_mode = ch_part->ch_mode;
2479 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002480 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002481 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002482 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002483 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002484 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002485
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002486 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002487 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002488 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002489
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002490 /* Use a message-specific callback, part callback or channel callback */
2491 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2492 if (cbitem->cq_seq_nr == 0)
2493 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002494 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002495 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002496 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002497 partial = cbitem->cq_partial;
2498 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002499 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002500 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002501 callback = ch_part->ch_callback;
2502 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002503 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002504 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002505 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002506 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002507 partial = channel->ch_partial;
2508 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002509
Bram Moolenaarec68a992016-10-03 21:37:41 +02002510 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002511 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2512 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002513 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002514 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002515 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002516 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002517 buffer = NULL;
2518 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002519
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002520 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002521 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002522 listitem_T *item;
2523 int argc = 0;
2524
Bram Moolenaard7ece102016-02-02 23:23:02 +01002525 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002526 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002527 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002528 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002529 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002530 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002531 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002532 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002533
Bram Moolenaarece61b02016-02-20 21:39:05 +01002534 for (item = listtv->vval.v_list->lv_first;
2535 item != NULL && argc < CH_JSON_MAX_ARGS;
2536 item = item->li_next)
2537 argv[argc++] = item->li_tv;
2538 while (argc < CH_JSON_MAX_ARGS)
2539 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002540
Bram Moolenaarece61b02016-02-20 21:39:05 +01002541 if (argv[0].v_type == VAR_STRING)
2542 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002543 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002544 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002545 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002546 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002547 }
2548
Bram Moolenaarece61b02016-02-20 21:39:05 +01002549 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002550 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002551 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002552 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002553 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002554 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002555 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002556 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002557 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002558 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002559 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002560 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002561 return FALSE;
2562 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002563 else
2564 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002565 /* If there is no callback or buffer drop the message. */
2566 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002567 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002568 /* If there is a close callback it may use ch_read() to get the
2569 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002570 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002571 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002572 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002573 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002574
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002575 if (ch_mode == MODE_NL)
2576 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002577 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002578 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002579 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002580
2581 /* See if we have a message ending in NL in the first buffer. If
2582 * not try to concatenate the first and the second buffer. */
2583 while (TRUE)
2584 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002585 node = channel_peek(channel, part);
2586 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002587 if (nl != NULL)
2588 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002589 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002590 {
2591 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2592 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002593 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002594 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002595 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002596 buf = node->rq_buffer;
2597
Bram Moolenaarec68a992016-10-03 21:37:41 +02002598 if (nl == NULL)
2599 {
2600 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002601 char_u *new_buf;
2602
2603 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2604 if (new_buf == NULL)
2605 /* This might fail over and over again, should the message
2606 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002607 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002608 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002609 node->rq_buffer = buf;
2610 nl = buf + node->rq_buflen++;
2611 *nl = NUL;
2612 }
2613
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002614 /* Convert NUL to NL, the internal representation. */
2615 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2616 if (*p == NUL)
2617 *p = NL;
2618
2619 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002620 {
2621 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002622 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002623 *nl = NUL;
2624 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002625 else
2626 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002627 /* Copy the message into allocated memory (excluding the NL)
2628 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002629 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002630 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002631 }
2632 }
2633 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002634 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002635 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002636 * get everything we have.
2637 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002638 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002639 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002640
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002641 if (msg == NULL)
2642 return FALSE; /* out of memory (and avoids Coverity warning) */
2643
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002644 argv[1].v_type = VAR_STRING;
2645 argv[1].vval.v_string = msg;
2646 }
2647
Bram Moolenaara07fec92016-02-05 21:04:08 +01002648 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002649 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002650 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002651
Bram Moolenaar958dc692016-12-01 15:34:12 +01002652 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002653 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002654 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002655 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002656 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002657 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002658 break;
2659 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002660 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002661 {
2662 if (channel->ch_drop_never)
2663 {
2664 /* message must be read with ch_read() */
2665 channel_push_json(channel, part, listtv);
2666 listtv = NULL;
2667 }
2668 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002669 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002670 seq_nr);
2671 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002672 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002673 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002674 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002675 if (buffer != NULL)
2676 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002677 if (msg == NULL)
2678 /* JSON or JS mode: re-encode the message. */
2679 msg = json_encode(listtv, ch_mode);
2680 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002681 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002682#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002683 if (buffer->b_term != NULL)
2684 write_to_term(buffer, msg, channel);
2685 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002686#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002687 append_to_buffer(buffer, msg, channel, part);
2688 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002689 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002690
Bram Moolenaar187db502016-02-27 14:44:26 +01002691 if (callback != NULL)
2692 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002693 if (cbitem != NULL)
2694 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2695 else
2696 {
2697 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002698 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002699 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002700 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002701 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002702 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002703 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002704 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002705 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002706
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002707 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002708 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002709 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002710
2711 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002712}
2713
2714/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002715 * Return TRUE when channel "channel" is open for writing to.
2716 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002717 */
2718 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002719channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002720{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002721 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002722 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002723}
2724
2725/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002726 * Return TRUE when channel "channel" is open for reading or writing.
2727 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002728 */
2729 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002730channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002731{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002732 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002733 || channel->CH_IN_FD != INVALID_FD
2734 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002735 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002736}
2737
2738/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002739 * Return TRUE if "channel" has JSON or other typeahead.
2740 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002741 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002742channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002743{
2744 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2745
2746 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2747 {
2748 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2749 jsonq_T *item = head->jq_next;
2750
2751 return item != NULL;
2752 }
2753 return channel_peek(channel, part) != NULL;
2754}
2755
2756/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002757 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002758 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002759 */
2760 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002761channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002762{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002763 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002764 int has_readahead = FALSE;
2765
Bram Moolenaar77073442016-02-13 23:23:53 +01002766 if (channel == NULL)
2767 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002768 if (req_part == PART_OUT)
2769 {
2770 if (channel->CH_OUT_FD != INVALID_FD)
2771 return "open";
2772 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002773 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002774 }
2775 else if (req_part == PART_ERR)
2776 {
2777 if (channel->CH_ERR_FD != INVALID_FD)
2778 return "open";
2779 if (channel_has_readahead(channel, PART_ERR))
2780 has_readahead = TRUE;
2781 }
2782 else
2783 {
2784 if (channel_is_open(channel))
2785 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002786 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002787 if (channel_has_readahead(channel, part))
2788 {
2789 has_readahead = TRUE;
2790 break;
2791 }
2792 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002793
2794 if (has_readahead)
2795 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002796 return "closed";
2797}
2798
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002799 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002800channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002801{
2802 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002803 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002804 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002805 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002806 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002807
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002808 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002809 STRCAT(namebuf, "_");
2810 tail = STRLEN(namebuf);
2811
2812 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002813 if (chanpart->ch_fd != INVALID_FD)
2814 status = "open";
2815 else if (channel_has_readahead(channel, part))
2816 status = "buffered";
2817 else
2818 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002819 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002820
2821 STRCPY(namebuf + tail, "mode");
2822 switch (chanpart->ch_mode)
2823 {
2824 case MODE_NL: s = "NL"; break;
2825 case MODE_RAW: s = "RAW"; break;
2826 case MODE_JSON: s = "JSON"; break;
2827 case MODE_JS: s = "JS"; break;
2828 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002829 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002830
2831 STRCPY(namebuf + tail, "io");
2832 if (part == PART_SOCK)
2833 s = "socket";
2834 else switch (chanpart->ch_io)
2835 {
2836 case JIO_NULL: s = "null"; break;
2837 case JIO_PIPE: s = "pipe"; break;
2838 case JIO_FILE: s = "file"; break;
2839 case JIO_BUFFER: s = "buffer"; break;
2840 case JIO_OUT: s = "out"; break;
2841 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002842 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002843
2844 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002845 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002846}
2847
2848 void
2849channel_info(channel_T *channel, dict_T *dict)
2850{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002851 dict_add_number(dict, "id", channel->ch_id);
2852 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002853
2854 if (channel->ch_hostname != NULL)
2855 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002856 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2857 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002858 channel_part_info(channel, dict, "sock", PART_SOCK);
2859 }
2860 else
2861 {
2862 channel_part_info(channel, dict, "out", PART_OUT);
2863 channel_part_info(channel, dict, "err", PART_ERR);
2864 channel_part_info(channel, dict, "in", PART_IN);
2865 }
2866}
2867
Bram Moolenaar77073442016-02-13 23:23:53 +01002868/*
2869 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002870 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002871 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002872 */
2873 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002874channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002875{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002876 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002877
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002878#ifdef FEAT_GUI
2879 channel_gui_unregister(channel);
2880#endif
2881
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002882 ch_close_part(channel, PART_SOCK);
2883 ch_close_part(channel, PART_IN);
2884 ch_close_part(channel, PART_OUT);
2885 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002886
Bram Moolenaara9f02812017-08-05 17:40:38 +02002887 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002888 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002889 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002890
Bram Moolenaara9f02812017-08-05 17:40:38 +02002891 /* Invoke callbacks and flush buffers before the close callback. */
2892 if (channel->ch_close_cb != NULL)
2893 ch_log(channel,
2894 "Invoking callbacks and flushing buffers before closing");
2895 for (part = PART_SOCK; part < PART_IN; ++part)
2896 {
2897 if (channel->ch_close_cb != NULL
2898 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2899 {
2900 /* Increment the refcount to avoid the channel being freed
2901 * halfway. */
2902 ++channel->ch_refcount;
2903 if (channel->ch_close_cb == NULL)
2904 ch_log(channel, "flushing %s buffers before closing",
2905 part_names[part]);
2906 while (may_invoke_callback(channel, part))
2907 ;
2908 --channel->ch_refcount;
2909 }
2910 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002911
Bram Moolenaara9f02812017-08-05 17:40:38 +02002912 if (channel->ch_close_cb != NULL)
2913 {
2914 typval_T argv[1];
2915 typval_T rettv;
2916 int dummy;
2917
2918 /* Increment the refcount to avoid the channel being freed
2919 * halfway. */
2920 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002921 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002922 (char *)channel->ch_close_cb);
2923 argv[0].v_type = VAR_CHANNEL;
2924 argv[0].vval.v_channel = channel;
2925 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002926 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002927 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002928 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002929 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002930
Bram Moolenaara9f02812017-08-05 17:40:38 +02002931 /* the callback is only called once */
2932 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2933 channel->ch_close_cb = NULL;
2934 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002935
Bram Moolenaara9f02812017-08-05 17:40:38 +02002936 if (channel_need_redraw)
2937 {
2938 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002939 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002940 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002941
Bram Moolenaara9f02812017-08-05 17:40:38 +02002942 if (!channel->ch_drop_never)
2943 /* any remaining messages are useless now */
2944 for (part = PART_SOCK; part < PART_IN; ++part)
2945 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002946
2947 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002948 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002949 }
2950
2951 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002952
2953#ifdef FEAT_TERMINAL
2954 term_channel_closed(channel);
2955#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002956}
2957
Bram Moolenaard04a0202016-01-26 23:30:18 +01002958/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002959 * Close the "in" part channel "channel".
2960 */
2961 void
2962channel_close_in(channel_T *channel)
2963{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002964 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002965}
2966
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002967 static void
2968remove_from_writeque(writeq_T *wq, writeq_T *entry)
2969{
2970 ga_clear(&entry->wq_ga);
2971 wq->wq_next = entry->wq_next;
2972 if (wq->wq_next == NULL)
2973 wq->wq_prev = NULL;
2974 else
2975 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002976 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002977}
2978
Bram Moolenaar0874a832016-09-01 15:11:51 +02002979/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002980 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002981 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002982 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002983channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002984{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002985 chanpart_T *ch_part = &channel->ch_part[part];
2986 jsonq_T *json_head = &ch_part->ch_json_head;
2987 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002988
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002989 while (channel_peek(channel, part) != NULL)
2990 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002991
2992 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002993 {
2994 cbq_T *node = cb_head->cq_next;
2995
2996 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002997 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002998 vim_free(node);
2999 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003000
3001 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003002 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003003 free_tv(json_head->jq_next->jq_value);
3004 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003005 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003006
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003007 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3008 ch_part->ch_callback = NULL;
3009 ch_part->ch_partial = NULL;
3010
3011 while (ch_part->ch_writeque.wq_next != NULL)
3012 remove_from_writeque(&ch_part->ch_writeque,
3013 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003014}
3015
3016/*
3017 * Clear all the read buffers on "channel".
3018 */
3019 void
3020channel_clear(channel_T *channel)
3021{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003022 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003023 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003024 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003025 channel_clear_one(channel, PART_OUT);
3026 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003027 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003028 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003029 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003030 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003031 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003032 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003033 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003034}
3035
Bram Moolenaar77073442016-02-13 23:23:53 +01003036#if defined(EXITFREE) || defined(PROTO)
3037 void
3038channel_free_all(void)
3039{
3040 channel_T *channel;
3041
Bram Moolenaard6051b52016-02-28 15:49:03 +01003042 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003043 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3044 channel_clear(channel);
3045}
3046#endif
3047
3048
Bram Moolenaar715d2852016-04-30 17:06:31 +02003049/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003050#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003051
3052/* Buffer size for reading incoming messages. */
3053#define MAXMSGSIZE 4096
3054
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003055#if defined(HAVE_SELECT)
3056/*
3057 * Add write fds where we are waiting for writing to be possible.
3058 */
3059 static int
3060channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3061{
3062 int maxfd = maxfd_arg;
3063 channel_T *ch;
3064
3065 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3066 {
3067 chanpart_T *in_part = &ch->ch_part[PART_IN];
3068
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003069 if (in_part->ch_fd != INVALID_FD
3070 && (in_part->ch_bufref.br_buf != NULL
3071 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003072 {
3073 FD_SET((int)in_part->ch_fd, wfds);
3074 if ((int)in_part->ch_fd >= maxfd)
3075 maxfd = (int)in_part->ch_fd + 1;
3076 }
3077 }
3078 return maxfd;
3079}
3080#else
3081/*
3082 * Add write fds where we are waiting for writing to be possible.
3083 */
3084 static int
3085channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3086{
3087 int nfd = nfd_in;
3088 channel_T *ch;
3089
3090 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3091 {
3092 chanpart_T *in_part = &ch->ch_part[PART_IN];
3093
Bram Moolenaar683b7962017-08-19 15:51:59 +02003094 if (in_part->ch_fd != INVALID_FD
3095 && (in_part->ch_bufref.br_buf != NULL
3096 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003097 {
3098 in_part->ch_poll_idx = nfd;
3099 fds[nfd].fd = in_part->ch_fd;
3100 fds[nfd].events = POLLOUT;
3101 ++nfd;
3102 }
3103 else
3104 in_part->ch_poll_idx = -1;
3105 }
3106 return nfd;
3107}
3108#endif
3109
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003110typedef enum {
3111 CW_READY,
3112 CW_NOT_READY,
3113 CW_ERROR
3114} channel_wait_result;
3115
Bram Moolenaard04a0202016-01-26 23:30:18 +01003116/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003117 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003118 * Return CW_READY when there is something to read.
3119 * Return CW_NOT_READY when there is nothing to read.
3120 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003121 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003122 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003123channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003124{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003125 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003126 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003127
Bram Moolenaard8070362016-02-15 21:56:54 +01003128# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003129 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003130 {
3131 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003132 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003133 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003134 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003135
3136 /* reading from a pipe, not a socket */
3137 while (TRUE)
3138 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003139 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3140
3141 if (r && nread > 0)
3142 return CW_READY;
3143 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003144 {
3145 DWORD err = GetLastError();
3146
3147 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3148 return CW_ERROR;
3149
3150 if (channel->ch_named_pipe)
3151 {
3152 DisconnectNamedPipe((HANDLE)fd);
3153 ConnectNamedPipe((HANDLE)fd, NULL);
3154 }
3155 else
3156 return CW_ERROR;
3157 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003158
3159 /* perhaps write some buffer lines */
3160 channel_write_any_lines();
3161
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003162 sleep_time = deadline - GetTickCount();
3163 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003164 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003165 /* Wait for a little while. Very short at first, up to 10 msec
3166 * after looping a few times. */
3167 if (sleep_time > delay)
3168 sleep_time = delay;
3169 Sleep(sleep_time);
3170 delay = delay * 2;
3171 if (delay > 10)
3172 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003173 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003174 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003175 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003176#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003177 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003178#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003179 struct timeval tval;
3180 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003181 fd_set wfds;
3182 int ret;
3183 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003184
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003185 tval.tv_sec = timeout / 1000;
3186 tval.tv_usec = (timeout % 1000) * 1000;
3187 for (;;)
3188 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003189 FD_ZERO(&rfds);
3190 FD_SET((int)fd, &rfds);
3191
3192 /* Write lines to a pipe when a pipe can be written to. Need to
3193 * set this every time, some buffers may be done. */
3194 maxfd = (int)fd + 1;
3195 FD_ZERO(&wfds);
3196 maxfd = channel_fill_wfds(maxfd, &wfds);
3197
3198 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003199# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003200 SOCK_ERRNO;
3201 if (ret == -1 && errno == EINTR)
3202 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003203# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003204 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003205 {
3206 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003207 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003208 channel_write_any_lines();
3209 continue;
3210 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003211 break;
3212 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003213#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003214 for (;;)
3215 {
3216 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3217 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003218
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003219 fds[0].fd = fd;
3220 fds[0].events = POLLIN;
3221 nfd = channel_fill_poll_write(nfd, fds);
3222 if (poll(fds, nfd, timeout) > 0)
3223 {
3224 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003225 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003226 channel_write_any_lines();
3227 continue;
3228 }
3229 break;
3230 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003231#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003232 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003233 return CW_NOT_READY;
3234}
3235
3236 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003237ch_close_part_on_error(
3238 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003239{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003240 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003241
3242 if (is_err)
3243 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003244 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003245 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003246 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003247
3248 /* Queue a "DETACH" netbeans message in the command queue in order to
3249 * terminate the netbeans session later. Do not end the session here
3250 * directly as we may be running in the context of a call to
3251 * netbeans_parse_messages():
3252 * netbeans_parse_messages
3253 * -> autocmd triggered while processing the netbeans cmd
3254 * -> ui_breakcheck
3255 * -> gui event loop or select loop
3256 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003257 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003258 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003259 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003260 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003261 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3262
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003263 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003264 * close the channel yet, there may be something to read on another part.
3265 * When stdout and stderr use the same FD we get the error only on one of
3266 * them, also close the other. */
3267 if (part == PART_OUT || part == PART_ERR)
3268 {
3269 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3270
3271 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3272 ch_close_part(channel, other);
3273 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003274 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003275
3276#ifdef FEAT_GUI
3277 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003278 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003279#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003280}
3281
3282 static void
3283channel_close_now(channel_T *channel)
3284{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003285 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003286 if (channel->ch_nb_close_cb != NULL)
3287 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003288 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003289}
3290
3291/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003292 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003293 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003294 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003295 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003296 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003297channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003298{
3299 static char_u *buf = NULL;
3300 int len = 0;
3301 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003302 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003303 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003304
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003305 fd = channel->ch_part[part].ch_fd;
3306 if (fd == INVALID_FD)
3307 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003308 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003309 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003310 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003311 }
3312 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003313
3314 /* Allocate a buffer to read into. */
3315 if (buf == NULL)
3316 {
3317 buf = alloc(MAXMSGSIZE);
3318 if (buf == NULL)
3319 return; /* out of memory! */
3320 }
3321
3322 /* Keep on reading for as long as there is something to read.
3323 * Use select() or poll() to avoid blocking on a message that is exactly
3324 * MAXMSGSIZE long. */
3325 for (;;)
3326 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003327 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003328 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003329 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003330 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003331 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003332 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003333 if (len <= 0)
3334 break; /* error or nothing more to read */
3335
3336 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003337 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003338 readlen += len;
3339 if (len < MAXMSGSIZE)
3340 break; /* did read everything that's available */
3341 }
3342
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003343 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003344 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003345 {
3346 if (!channel->ch_keep_open)
3347 ch_close_part_on_error(channel, part, (len < 0), func);
3348 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003349#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003350 else if (CH_HAS_GUI && gtk_main_level() > 0)
3351 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003352 gtk_main_quit();
3353#endif
3354}
3355
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003356/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003357 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003358 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003359 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003360 * Returns what was read in allocated memory.
3361 * Returns NULL in case of error or timeout.
3362 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003363 static char_u *
3364channel_read_block(channel_T *channel, ch_part_T part, int timeout, int raw)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003365{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003366 char_u *buf;
3367 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003368 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003369 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003370 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003371 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003372
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003373 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003374 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003375
3376 while (TRUE)
3377 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003378 node = channel_peek(channel, part);
3379 if (node != NULL)
3380 {
3381 if (mode == MODE_RAW || (mode == MODE_NL
3382 && channel_first_nl(node) != NULL))
3383 /* got a complete message */
3384 break;
3385 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3386 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003387 /* If not blocking or nothing more is coming then return what we
3388 * have. */
3389 if (raw || fd == INVALID_FD)
3390 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003391 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003392
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003393 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003394 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003395 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003396 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003397 {
3398 ch_log(channel, "Timed out");
3399 return NULL;
3400 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003401 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003402 }
3403
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003404 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003405 if (mode == MODE_RAW)
3406 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003407 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003408 }
3409 else
3410 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003411 char_u *p;
3412
3413 buf = node->rq_buffer;
3414 nl = channel_first_nl(node);
3415
3416 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003417 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003418 if (*p == NUL)
3419 *p = NL;
3420
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003421 if (nl == NULL)
3422 {
3423 /* must be a closed channel with missing NL */
3424 msg = channel_get(channel, part);
3425 }
3426 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003427 {
3428 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003429 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003430 *nl = NUL;
3431 }
3432 else
3433 {
3434 /* Copy the message into allocated memory and remove it from the
3435 * buffer. */
3436 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003437 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003438 }
3439 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003440 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003441 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003442 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003443}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003444
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003445/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003446 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003447 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003448 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003449 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003450 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003451 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003452channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003453 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003454 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003455 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003456 int id,
3457 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003458{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003459 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003460 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003461 int timeout;
3462 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003463
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003464 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003465 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003466 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003467 for (;;)
3468 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003469 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003470
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003471 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003472 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003473 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003474 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003475 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003476 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003477
Bram Moolenaard7ece102016-02-02 23:23:02 +01003478 if (!more)
3479 {
3480 /* Handle any other messages in the queue. If done some more
3481 * messages may have arrived. */
3482 if (channel_parse_messages())
3483 continue;
3484
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003485 /* Wait for up to the timeout. If there was an incomplete message
3486 * use the deadline for that. */
3487 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003488 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003489 {
3490#ifdef WIN32
3491 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3492#else
3493 {
3494 struct timeval now_tv;
3495
3496 gettimeofday(&now_tv, NULL);
3497 timeout = (chanpart->ch_deadline.tv_sec
3498 - now_tv.tv_sec) * 1000
3499 + (chanpart->ch_deadline.tv_usec
3500 - now_tv.tv_usec) / 1000
3501 + 1;
3502 }
3503#endif
3504 if (timeout < 0)
3505 {
3506 /* Something went wrong, channel_parse_json() didn't
3507 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003508 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003509 timeout = timeout_arg;
3510 }
3511 else if (timeout > timeout_arg)
3512 timeout = timeout_arg;
3513 }
3514 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003515 if (fd == INVALID_FD
3516 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003517 {
3518 if (timeout == timeout_arg)
3519 {
3520 if (fd != INVALID_FD)
3521 ch_log(channel, "Timed out");
3522 break;
3523 }
3524 }
3525 else
3526 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003527 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003528 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003529 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003530 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003531}
3532
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003533/*
3534 * Common for ch_read() and ch_readraw().
3535 */
3536 void
3537common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3538{
3539 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003540 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003541 jobopt_T opt;
3542 int mode;
3543 int timeout;
3544 int id = -1;
3545 typval_T *listtv = NULL;
3546
3547 /* return an empty string by default */
3548 rettv->v_type = VAR_STRING;
3549 rettv->vval.v_string = NULL;
3550
3551 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003552 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003553 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003554 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003555
Bram Moolenaar437905c2016-04-26 19:01:05 +02003556 if (opt.jo_set & JO_PART)
3557 part = opt.jo_part;
3558 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003559 if (channel != NULL)
3560 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003561 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003562 part = channel_part_read(channel);
3563 mode = channel_get_mode(channel, part);
3564 timeout = channel_get_timeout(channel, part);
3565 if (opt.jo_set & JO_TIMEOUT)
3566 timeout = opt.jo_timeout;
3567
3568 if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003569 rettv->vval.v_string = channel_read_block(channel, part,
3570 timeout, raw);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003571 else
3572 {
3573 if (opt.jo_set & JO_ID)
3574 id = opt.jo_id;
3575 channel_read_json_block(channel, part, timeout, id, &listtv);
3576 if (listtv != NULL)
3577 {
3578 *rettv = *listtv;
3579 vim_free(listtv);
3580 }
3581 else
3582 {
3583 rettv->v_type = VAR_SPECIAL;
3584 rettv->vval.v_number = VVAL_NONE;
3585 }
3586 }
3587 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003588
3589theend:
3590 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003591}
3592
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003593# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3594 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003595/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003596 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003597 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003598 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003599 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003600channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003601{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003602 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003603 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003604
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003605 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003606 for (channel = first_channel; channel != NULL;
3607 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003608 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003609 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003610 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003611 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003612 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003613 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003614 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003615 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003616 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003617}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003618# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003619
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003620# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003621/*
3622 * Check the channels for anything that is ready to be read.
3623 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003624 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003625 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003626 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003627channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003628{
3629 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003630 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003631 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003632
3633 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3634 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003635 if (only_keep_open && !channel->ch_keep_open)
3636 continue;
3637
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003638 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003639 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003640 {
3641 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003642 if (fd != INVALID_FD)
3643 {
3644 int r = channel_wait(channel, fd, 0);
3645
3646 if (r == CW_READY)
3647 channel_read(channel, part, "channel_handle_events");
3648 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003649 ch_close_part_on_error(channel, part, TRUE,
3650 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003651 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003652 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003653 }
3654}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003655# endif
3656
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003657# if defined(FEAT_GUI) || defined(PROTO)
3658/*
3659 * Return TRUE when there is any channel with a keep_open flag.
3660 */
3661 int
3662channel_any_keep_open()
3663{
3664 channel_T *channel;
3665
3666 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3667 if (channel->ch_keep_open)
3668 return TRUE;
3669 return FALSE;
3670}
3671# endif
3672
Bram Moolenaard04a0202016-01-26 23:30:18 +01003673/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003674 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003675 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003676 */
3677 void
3678channel_set_nonblock(channel_T *channel, ch_part_T part)
3679{
3680 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003681 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003682
3683 if (fd != INVALID_FD)
3684 {
3685#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003686 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003687
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003688 ioctlsocket(fd, FIONBIO, &val);
3689#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003690 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003691#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003692 ch_part->ch_nonblocking = TRUE;
3693 }
3694}
3695
3696/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003697 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003698 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003699 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003700 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003701 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003702channel_send(
3703 channel_T *channel,
3704 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003705 char_u *buf_arg,
3706 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003707 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003708{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003709 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003710 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003711 chanpart_T *ch_part = &channel->ch_part[part];
3712 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003713
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003714 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003715 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003716 {
3717 if (!channel->ch_error && fun != NULL)
3718 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003719 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003720 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003721 }
3722 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003723 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003724 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003725
Bram Moolenaar0b146882018-09-06 16:27:24 +02003726 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3727 channel_set_nonblock(channel, part);
3728
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003729 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003730 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003731 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003732 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003733 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003734 fprintf(log_fd, "'\n");
3735 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003736 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003737 }
3738
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003739 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003740 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003741 writeq_T *wq = &ch_part->ch_writeque;
3742 char_u *buf;
3743 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003744
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003745 if (wq->wq_next != NULL)
3746 {
3747 /* first write what was queued */
3748 buf = wq->wq_next->wq_ga.ga_data;
3749 len = wq->wq_next->wq_ga.ga_len;
3750 did_use_queue = TRUE;
3751 }
3752 else
3753 {
3754 if (len_arg == 0)
3755 /* nothing to write, called from channel_select_check() */
3756 return OK;
3757 buf = buf_arg;
3758 len = len_arg;
3759 }
3760
3761 if (part == PART_SOCK)
3762 res = sock_write(fd, (char *)buf, len);
3763 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003764 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003765 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003766#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003767 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003768 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003769 DisconnectNamedPipe((HANDLE)fd);
3770 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003771 }
3772#endif
3773
3774 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003775 if (res < 0 && (errno == EWOULDBLOCK
3776#ifdef EAGAIN
3777 || errno == EAGAIN
3778#endif
3779 ))
3780 res = 0; /* nothing got written */
3781
3782 if (res >= 0 && ch_part->ch_nonblocking)
3783 {
3784 writeq_T *entry = wq->wq_next;
3785
3786 if (did_use_queue)
3787 ch_log(channel, "Sent %d bytes now", res);
3788 if (res == len)
3789 {
3790 /* Wrote all the buf[len] bytes. */
3791 if (entry != NULL)
3792 {
3793 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003794 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003795 continue;
3796 }
3797 if (did_use_queue)
3798 ch_log(channel, "Write queue empty");
3799 }
3800 else
3801 {
3802 /* Wrote only buf[res] bytes, can't write more now. */
3803 if (entry != NULL)
3804 {
3805 if (res > 0)
3806 {
3807 /* Remove the bytes that were written. */
3808 mch_memmove(entry->wq_ga.ga_data,
3809 (char *)entry->wq_ga.ga_data + res,
3810 len - res);
3811 entry->wq_ga.ga_len -= res;
3812 }
3813 buf = buf_arg;
3814 len = len_arg;
3815 }
3816 else
3817 {
3818 buf += res;
3819 len -= res;
3820 }
3821 ch_log(channel, "Adding %d bytes to the write queue", len);
3822
3823 /* Append the not written bytes of the argument to the write
3824 * buffer. Limit entries to 4000 bytes. */
3825 if (wq->wq_prev != NULL
3826 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3827 {
3828 writeq_T *last = wq->wq_prev;
3829
3830 /* append to the last entry */
3831 if (ga_grow(&last->wq_ga, len) == OK)
3832 {
3833 mch_memmove((char *)last->wq_ga.ga_data
3834 + last->wq_ga.ga_len,
3835 buf, len);
3836 last->wq_ga.ga_len += len;
3837 }
3838 }
3839 else
3840 {
3841 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3842
3843 if (last != NULL)
3844 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003845 last->wq_prev = wq->wq_prev;
3846 last->wq_next = NULL;
3847 if (wq->wq_prev == NULL)
3848 wq->wq_next = last;
3849 else
3850 wq->wq_prev->wq_next = last;
3851 wq->wq_prev = last;
3852 ga_init2(&last->wq_ga, 1, 1000);
3853 if (ga_grow(&last->wq_ga, len) == OK)
3854 {
3855 mch_memmove(last->wq_ga.ga_data, buf, len);
3856 last->wq_ga.ga_len = len;
3857 }
3858 }
3859 }
3860 }
3861 }
3862 else if (res != len)
3863 {
3864 if (!channel->ch_error && fun != NULL)
3865 {
3866 ch_error(channel, "%s(): write failed", fun);
3867 EMSG2(_("E631: %s(): write failed"), fun);
3868 }
3869 channel->ch_error = TRUE;
3870 return FAIL;
3871 }
3872
3873 channel->ch_error = FALSE;
3874 return OK;
3875 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003876}
3877
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003878/*
3879 * Common for "ch_sendexpr()" and "ch_sendraw()".
3880 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003881 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003882 * Otherwise returns NULL.
3883 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003884 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003885send_common(
3886 typval_T *argvars,
3887 char_u *text,
3888 int id,
3889 int eval,
3890 jobopt_T *opt,
3891 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003892 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003893{
3894 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003895 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003896
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003897 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003898 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003899 if (channel == NULL)
3900 return NULL;
3901 part_send = channel_part_send(channel);
3902 *part_read = channel_part_read(channel);
3903
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003904 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003905 return NULL;
3906
3907 /* Set the callback. An empty callback means no callback and not reading
3908 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3909 * allowed. */
3910 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3911 {
3912 if (eval)
3913 {
3914 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3915 return NULL;
3916 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003917 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003918 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003919 }
3920
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003921 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003922 && opt->jo_callback == NULL)
3923 return channel;
3924 return NULL;
3925}
3926
3927/*
3928 * common for "ch_evalexpr()" and "ch_sendexpr()"
3929 */
3930 void
3931ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3932{
3933 char_u *text;
3934 typval_T *listtv;
3935 channel_T *channel;
3936 int id;
3937 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003938 ch_part_T part_send;
3939 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003940 jobopt_T opt;
3941 int timeout;
3942
3943 /* return an empty string by default */
3944 rettv->v_type = VAR_STRING;
3945 rettv->vval.v_string = NULL;
3946
Bram Moolenaar437905c2016-04-26 19:01:05 +02003947 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003948 if (channel == NULL)
3949 return;
3950 part_send = channel_part_send(channel);
3951
3952 ch_mode = channel_get_mode(channel, part_send);
3953 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3954 {
3955 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3956 return;
3957 }
3958
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003959 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003961 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003962 if (text == NULL)
3963 return;
3964
3965 channel = send_common(argvars, text, id, eval, &opt,
3966 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3967 vim_free(text);
3968 if (channel != NULL && eval)
3969 {
3970 if (opt.jo_set & JO_TIMEOUT)
3971 timeout = opt.jo_timeout;
3972 else
3973 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003974 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3975 == OK)
3976 {
3977 list_T *list = listtv->vval.v_list;
3978
3979 /* Move the item from the list and then change the type to
3980 * avoid the value being freed. */
3981 *rettv = list->lv_last->li_tv;
3982 list->lv_last->li_tv.v_type = VAR_NUMBER;
3983 free_tv(listtv);
3984 }
3985 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003986 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003987}
3988
3989/*
3990 * common for "ch_evalraw()" and "ch_sendraw()"
3991 */
3992 void
3993ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3994{
3995 char_u buf[NUMBUFLEN];
3996 char_u *text;
3997 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003998 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003999 jobopt_T opt;
4000 int timeout;
4001
4002 /* return an empty string by default */
4003 rettv->v_type = VAR_STRING;
4004 rettv->vval.v_string = NULL;
4005
4006 text = get_tv_string_buf(&argvars[1], buf);
4007 channel = send_common(argvars, text, 0, eval, &opt,
4008 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4009 if (channel != NULL && eval)
4010 {
4011 if (opt.jo_set & JO_TIMEOUT)
4012 timeout = opt.jo_timeout;
4013 else
4014 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004015 rettv->vval.v_string = channel_read_block(channel, part_read,
4016 timeout, TRUE);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004017 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004018 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004019}
4020
Bram Moolenaarf3360612017-10-01 16:21:31 +02004021# define KEEP_OPEN_TIME 20 /* msec */
4022
Bram Moolenaard04a0202016-01-26 23:30:18 +01004023# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004024/*
4025 * Add open channels to the poll struct.
4026 * Return the adjusted struct index.
4027 * The type of "fds" is hidden to avoid problems with the function proto.
4028 */
4029 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004030channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004031{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004032 int nfd = nfd_in;
4033 channel_T *channel;
4034 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004035 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004036
Bram Moolenaar77073442016-02-13 23:23:53 +01004037 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004038 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004039 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004040 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004041 chanpart_T *ch_part = &channel->ch_part[part];
4042
4043 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004044 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004045 if (channel->ch_keep_open)
4046 {
4047 /* For unknown reason poll() returns immediately for a
4048 * keep-open channel. Instead of adding it to the fds add
4049 * a short timeout and check, like polling. */
4050 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4051 *towait = KEEP_OPEN_TIME;
4052 }
4053 else
4054 {
4055 ch_part->ch_poll_idx = nfd;
4056 fds[nfd].fd = ch_part->ch_fd;
4057 fds[nfd].events = POLLIN;
4058 nfd++;
4059 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004060 }
4061 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004062 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004063 }
4064 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004065
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004066 nfd = channel_fill_poll_write(nfd, fds);
4067
Bram Moolenaare0874f82016-01-24 20:36:41 +01004068 return nfd;
4069}
4070
4071/*
4072 * The type of "fds" is hidden to avoid problems with the function proto.
4073 */
4074 int
4075channel_poll_check(int ret_in, void *fds_in)
4076{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004077 int ret = ret_in;
4078 channel_T *channel;
4079 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004080 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004081 int idx;
4082 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004083
Bram Moolenaar77073442016-02-13 23:23:53 +01004084 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004085 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004086 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004087 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004088 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004089
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004090 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004091 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004092 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004093 --ret;
4094 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004095 else if (channel->ch_part[part].ch_fd != INVALID_FD
4096 && channel->ch_keep_open)
4097 {
4098 /* polling a keep-open channel */
4099 channel_read(channel, part, "channel_poll_check_keep_open");
4100 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004101 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004102
4103 in_part = &channel->ch_part[PART_IN];
4104 idx = in_part->ch_poll_idx;
4105 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4106 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004107 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004108 --ret;
4109 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004110 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004111
4112 return ret;
4113}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004114# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004115
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004116# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004117
Bram Moolenaare0874f82016-01-24 20:36:41 +01004118/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004119 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004120 */
4121 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004122channel_select_setup(
4123 int maxfd_in,
4124 void *rfds_in,
4125 void *wfds_in,
4126 struct timeval *tv,
4127 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004128{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004129 int maxfd = maxfd_in;
4130 channel_T *channel;
4131 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004132 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004133 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004134
Bram Moolenaar77073442016-02-13 23:23:53 +01004135 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004136 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004137 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004138 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004139 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004140
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004141 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004142 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004143 if (channel->ch_keep_open)
4144 {
4145 /* For unknown reason select() returns immediately for a
4146 * keep-open channel. Instead of adding it to the rfds add
4147 * a short timeout and check, like polling. */
4148 if (*tvp == NULL || tv->tv_sec > 0
4149 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4150 {
4151 *tvp = tv;
4152 tv->tv_sec = 0;
4153 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4154 }
4155 }
4156 else
4157 {
4158 FD_SET((int)fd, rfds);
4159 if (maxfd < (int)fd)
4160 maxfd = (int)fd;
4161 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004162 }
4163 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004164 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004165
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004166 maxfd = channel_fill_wfds(maxfd, wfds);
4167
Bram Moolenaare0874f82016-01-24 20:36:41 +01004168 return maxfd;
4169}
4170
4171/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004172 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004173 */
4174 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004175channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004176{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004177 int ret = ret_in;
4178 channel_T *channel;
4179 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004180 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004181 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004182 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004183
Bram Moolenaar77073442016-02-13 23:23:53 +01004184 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004185 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004186 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004187 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004188 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004189
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004190 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004191 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004192 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004193 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004194 --ret;
4195 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004196 else if (fd != INVALID_FD && channel->ch_keep_open)
4197 {
4198 /* polling a keep-open channel */
4199 channel_read(channel, part, "channel_select_check_keep_open");
4200 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004201 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004202
4203 in_part = &channel->ch_part[PART_IN];
4204 if (ret > 0 && in_part->ch_fd != INVALID_FD
4205 && FD_ISSET(in_part->ch_fd, wfds))
4206 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004207 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004208 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004209 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004210 --ret;
4211 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004212 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004213
4214 return ret;
4215}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004216# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004217
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004218/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004219 * Execute queued up commands.
4220 * Invoked from the main loop when it's safe to execute received commands.
4221 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004222 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004223 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004224channel_parse_messages(void)
4225{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004226 channel_T *channel = first_channel;
4227 int ret = FALSE;
4228 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004229 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004230#ifdef ELAPSED_FUNC
4231 ELAPSED_TYPE start_tv;
4232
4233 ELAPSED_INIT(start_tv);
4234#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004235
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004236 ++safe_to_invoke_callback;
4237
Bram Moolenaard0b65022016-03-06 21:50:33 +01004238 /* Only do this message when another message was given, otherwise we get
4239 * lots of them. */
4240 if (did_log_msg)
4241 {
4242 ch_log(NULL, "looking for messages on channels");
4243 did_log_msg = FALSE;
4244 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004245 while (channel != NULL)
4246 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004247 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004248 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004249 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004250 channel_close_now(channel);
4251 /* channel may have been freed, start over */
4252 channel = first_channel;
4253 continue;
4254 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004255 if (channel->ch_to_be_freed)
4256 {
4257 channel_free(channel);
4258 /* channel has been freed, start over */
4259 channel = first_channel;
4260 continue;
4261 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004262 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004263 {
4264 /* channel is no longer useful, free it */
4265 channel_free(channel);
4266 channel = first_channel;
4267 part = PART_SOCK;
4268 continue;
4269 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004270 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004271 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004272 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004273 /* Increase the refcount, in case the handler causes the channel
4274 * to be unreferenced or closed. */
4275 ++channel->ch_refcount;
4276 r = may_invoke_callback(channel, part);
4277 if (r == OK)
4278 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004279 if (channel_unref(channel) || (r == OK
4280#ifdef ELAPSED_FUNC
4281 /* Limit the time we loop here to 100 msec, otherwise
4282 * Vim becomes unresponsive when the callback takes
4283 * more than a bit of time. */
4284 && ELAPSED_FUNC(start_tv) < 100L
4285#endif
4286 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004287 {
4288 /* channel was freed or something was done, start over */
4289 channel = first_channel;
4290 part = PART_SOCK;
4291 continue;
4292 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004293 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004294 if (part < PART_ERR)
4295 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004296 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004297 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004298 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004299 part = PART_SOCK;
4300 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004301 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004302
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004303 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004304 {
4305 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004306 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004307 }
4308
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004309 --safe_to_invoke_callback;
4310
Bram Moolenaard7ece102016-02-02 23:23:02 +01004311 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004312}
4313
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004314/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004315 * Return TRUE if any channel has readahead. That means we should not block on
4316 * waiting for input.
4317 */
4318 int
4319channel_any_readahead(void)
4320{
4321 channel_T *channel = first_channel;
4322 ch_part_T part = PART_SOCK;
4323
4324 while (channel != NULL)
4325 {
4326 if (channel_has_readahead(channel, part))
4327 return TRUE;
4328 if (part < PART_ERR)
4329 ++part;
4330 else
4331 {
4332 channel = channel->ch_next;
4333 part = PART_SOCK;
4334 }
4335 }
4336 return FALSE;
4337}
4338
4339/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004340 * Mark references to lists used in channels.
4341 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004342 int
4343set_ref_in_channel(int copyID)
4344{
Bram Moolenaar77073442016-02-13 23:23:53 +01004345 int abort = FALSE;
4346 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004347 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004348
Bram Moolenaar77073442016-02-13 23:23:53 +01004349 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004350 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004351 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004352 tv.v_type = VAR_CHANNEL;
4353 tv.vval.v_channel = channel;
4354 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004355 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004356 return abort;
4357}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004358
4359/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004360 * Return the "part" to write to for "channel".
4361 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004362 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004363channel_part_send(channel_T *channel)
4364{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004365 if (channel->CH_SOCK_FD == INVALID_FD)
4366 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004367 return PART_SOCK;
4368}
4369
4370/*
4371 * Return the default "part" to read from for "channel".
4372 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004373 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004374channel_part_read(channel_T *channel)
4375{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004376 if (channel->CH_SOCK_FD == INVALID_FD)
4377 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004378 return PART_SOCK;
4379}
4380
4381/*
4382 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004383 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004384 */
4385 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004386channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004387{
Bram Moolenaar77073442016-02-13 23:23:53 +01004388 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004389 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004390 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004391}
4392
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004393/*
4394 * Return the timeout of "channel"/"part"
4395 */
4396 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004397channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004398{
4399 return channel->ch_part[part].ch_timeout;
4400}
4401
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004402 static int
4403handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4404{
4405 char_u *val = get_tv_string(item);
4406
4407 opt->jo_set |= jo;
4408 if (STRCMP(val, "nl") == 0)
4409 *modep = MODE_NL;
4410 else if (STRCMP(val, "raw") == 0)
4411 *modep = MODE_RAW;
4412 else if (STRCMP(val, "js") == 0)
4413 *modep = MODE_JS;
4414 else if (STRCMP(val, "json") == 0)
4415 *modep = MODE_JSON;
4416 else
4417 {
4418 EMSG2(_(e_invarg2), val);
4419 return FAIL;
4420 }
4421 return OK;
4422}
4423
4424 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004425handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004426{
4427 char_u *val = get_tv_string(item);
4428
4429 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4430 if (STRCMP(val, "null") == 0)
4431 opt->jo_io[part] = JIO_NULL;
4432 else if (STRCMP(val, "pipe") == 0)
4433 opt->jo_io[part] = JIO_PIPE;
4434 else if (STRCMP(val, "file") == 0)
4435 opt->jo_io[part] = JIO_FILE;
4436 else if (STRCMP(val, "buffer") == 0)
4437 opt->jo_io[part] = JIO_BUFFER;
4438 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4439 opt->jo_io[part] = JIO_OUT;
4440 else
4441 {
4442 EMSG2(_(e_invarg2), val);
4443 return FAIL;
4444 }
4445 return OK;
4446}
4447
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004448/*
4449 * Clear a jobopt_T before using it.
4450 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004451 void
4452clear_job_options(jobopt_T *opt)
4453{
4454 vim_memset(opt, 0, sizeof(jobopt_T));
4455}
4456
4457/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004458 * Free any members of a jobopt_T.
4459 */
4460 void
4461free_job_options(jobopt_T *opt)
4462{
4463 if (opt->jo_partial != NULL)
4464 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004465 else if (opt->jo_callback != NULL)
4466 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004467 if (opt->jo_out_partial != NULL)
4468 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004469 else if (opt->jo_out_cb != NULL)
4470 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004471 if (opt->jo_err_partial != NULL)
4472 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004473 else if (opt->jo_err_cb != NULL)
4474 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004475 if (opt->jo_close_partial != NULL)
4476 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004477 else if (opt->jo_close_cb != NULL)
4478 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004479 if (opt->jo_exit_partial != NULL)
4480 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004481 else if (opt->jo_exit_cb != NULL)
4482 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004483 if (opt->jo_env != NULL)
4484 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004485}
4486
4487/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004488 * Get the PART_ number from the first character of an option name.
4489 */
4490 static int
4491part_from_char(int c)
4492{
4493 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4494}
4495
4496/*
4497 * Get the option entries from the dict in "tv", parse them and put the result
4498 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004499 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004500 * If an option value is invalid return FAIL.
4501 */
4502 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004503get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004504{
4505 typval_T *item;
4506 char_u *val;
4507 dict_T *dict;
4508 int todo;
4509 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004510 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004511
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004512 if (tv->v_type == VAR_UNKNOWN)
4513 return OK;
4514 if (tv->v_type != VAR_DICT)
4515 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004516 EMSG(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004517 return FAIL;
4518 }
4519 dict = tv->vval.v_dict;
4520 if (dict == NULL)
4521 return OK;
4522
4523 todo = (int)dict->dv_hashtab.ht_used;
4524 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4525 if (!HASHITEM_EMPTY(hi))
4526 {
4527 item = &dict_lookup(hi)->di_tv;
4528
4529 if (STRCMP(hi->hi_key, "mode") == 0)
4530 {
4531 if (!(supported & JO_MODE))
4532 break;
4533 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4534 return FAIL;
4535 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004536 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004537 {
4538 if (!(supported & JO_IN_MODE))
4539 break;
4540 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4541 == FAIL)
4542 return FAIL;
4543 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004544 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004545 {
4546 if (!(supported & JO_OUT_MODE))
4547 break;
4548 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4549 == FAIL)
4550 return FAIL;
4551 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004552 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004553 {
4554 if (!(supported & JO_ERR_MODE))
4555 break;
4556 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4557 == FAIL)
4558 return FAIL;
4559 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004560 else if (STRCMP(hi->hi_key, "noblock") == 0)
4561 {
4562 if (!(supported & JO_MODE))
4563 break;
4564 opt->jo_noblock = get_tv_number(item);
4565 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004566 else if (STRCMP(hi->hi_key, "in_io") == 0
4567 || STRCMP(hi->hi_key, "out_io") == 0
4568 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004569 {
4570 if (!(supported & JO_OUT_IO))
4571 break;
4572 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4573 return FAIL;
4574 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004575 else if (STRCMP(hi->hi_key, "in_name") == 0
4576 || STRCMP(hi->hi_key, "out_name") == 0
4577 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004578 {
4579 part = part_from_char(*hi->hi_key);
4580
4581 if (!(supported & JO_OUT_IO))
4582 break;
4583 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4584 opt->jo_io_name[part] =
4585 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4586 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004587 else if (STRCMP(hi->hi_key, "pty") == 0)
4588 {
4589 if (!(supported & JO_MODE))
4590 break;
4591 opt->jo_pty = get_tv_number(item);
4592 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004593 else if (STRCMP(hi->hi_key, "in_buf") == 0
4594 || STRCMP(hi->hi_key, "out_buf") == 0
4595 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004596 {
4597 part = part_from_char(*hi->hi_key);
4598
4599 if (!(supported & JO_OUT_IO))
4600 break;
4601 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4602 opt->jo_io_buf[part] = get_tv_number(item);
4603 if (opt->jo_io_buf[part] <= 0)
4604 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004605 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004606 return FAIL;
4607 }
4608 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4609 {
4610 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4611 return FAIL;
4612 }
4613 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004614 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4615 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4616 {
4617 part = part_from_char(*hi->hi_key);
4618
4619 if (!(supported & JO_OUT_IO))
4620 break;
4621 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4622 opt->jo_modifiable[part] = get_tv_number(item);
4623 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004624 else if (STRCMP(hi->hi_key, "out_msg") == 0
4625 || STRCMP(hi->hi_key, "err_msg") == 0)
4626 {
4627 part = part_from_char(*hi->hi_key);
4628
4629 if (!(supported & JO_OUT_IO))
4630 break;
4631 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4632 opt->jo_message[part] = get_tv_number(item);
4633 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004634 else if (STRCMP(hi->hi_key, "in_top") == 0
4635 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004636 {
4637 linenr_T *lp;
4638
4639 if (!(supported & JO_OUT_IO))
4640 break;
4641 if (hi->hi_key[3] == 't')
4642 {
4643 lp = &opt->jo_in_top;
4644 opt->jo_set |= JO_IN_TOP;
4645 }
4646 else
4647 {
4648 lp = &opt->jo_in_bot;
4649 opt->jo_set |= JO_IN_BOT;
4650 }
4651 *lp = get_tv_number(item);
4652 if (*lp < 0)
4653 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004654 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004655 return FAIL;
4656 }
4657 }
4658 else if (STRCMP(hi->hi_key, "channel") == 0)
4659 {
4660 if (!(supported & JO_OUT_IO))
4661 break;
4662 opt->jo_set |= JO_CHANNEL;
4663 if (item->v_type != VAR_CHANNEL)
4664 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004665 EMSG2(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004666 return FAIL;
4667 }
4668 opt->jo_channel = item->vval.v_channel;
4669 }
4670 else if (STRCMP(hi->hi_key, "callback") == 0)
4671 {
4672 if (!(supported & JO_CALLBACK))
4673 break;
4674 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004675 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004676 if (opt->jo_callback == NULL)
4677 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004678 EMSG2(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004679 return FAIL;
4680 }
4681 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004682 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004683 {
4684 if (!(supported & JO_OUT_CALLBACK))
4685 break;
4686 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004687 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004688 if (opt->jo_out_cb == NULL)
4689 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004690 EMSG2(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004691 return FAIL;
4692 }
4693 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004694 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004695 {
4696 if (!(supported & JO_ERR_CALLBACK))
4697 break;
4698 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004699 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004700 if (opt->jo_err_cb == NULL)
4701 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004702 EMSG2(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004703 return FAIL;
4704 }
4705 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004706 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004707 {
4708 if (!(supported & JO_CLOSE_CALLBACK))
4709 break;
4710 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004711 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004712 if (opt->jo_close_cb == NULL)
4713 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004714 EMSG2(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004715 return FAIL;
4716 }
4717 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004718 else if (STRCMP(hi->hi_key, "drop") == 0)
4719 {
4720 int never = FALSE;
4721 val = get_tv_string(item);
4722
4723 if (STRCMP(val, "never") == 0)
4724 never = TRUE;
4725 else if (STRCMP(val, "auto") != 0)
4726 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004727 EMSG3(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004728 return FAIL;
4729 }
4730 opt->jo_drop_never = never;
4731 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004732 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4733 {
4734 if (!(supported & JO_EXIT_CB))
4735 break;
4736 opt->jo_set |= JO_EXIT_CB;
4737 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4738 if (opt->jo_exit_cb == NULL)
4739 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004740 EMSG2(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004741 return FAIL;
4742 }
4743 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004744#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004745 else if (STRCMP(hi->hi_key, "term_name") == 0)
4746 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004747 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004748 break;
4749 opt->jo_set2 |= JO2_TERM_NAME;
4750 opt->jo_term_name = get_tv_string_chk(item);
4751 if (opt->jo_term_name == NULL)
4752 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004753 EMSG2(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004754 return FAIL;
4755 }
4756 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004757 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4758 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004759 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004760 break;
4761 val = get_tv_string(item);
4762 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4763 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004764 EMSG3(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004765 return FAIL;
4766 }
4767 opt->jo_set2 |= JO2_TERM_FINISH;
4768 opt->jo_term_finish = *val;
4769 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004770 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4771 {
4772 char_u *p;
4773
4774 if (!(supported2 & JO2_TERM_OPENCMD))
4775 break;
4776 opt->jo_set2 |= JO2_TERM_OPENCMD;
4777 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4778 if (p != NULL)
4779 {
4780 /* Must have %d and no other %. */
4781 p = vim_strchr(p, '%');
4782 if (p != NULL && (p[1] != 'd'
4783 || vim_strchr(p + 2, '%') != NULL))
4784 p = NULL;
4785 }
4786 if (p == NULL)
4787 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004788 EMSG2(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004789 return FAIL;
4790 }
4791 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004792 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4793 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004794 char_u *p;
4795
4796 if (!(supported2 & JO2_EOF_CHARS))
4797 break;
4798 opt->jo_set2 |= JO2_EOF_CHARS;
4799 p = opt->jo_eof_chars = get_tv_string_chk(item);
4800 if (p == NULL)
4801 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004802 EMSG2(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004803 return FAIL;
4804 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004805 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004806 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4807 {
4808 if (!(supported2 & JO2_TERM_ROWS))
4809 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004810 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004811 opt->jo_term_rows = get_tv_number(item);
4812 }
4813 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4814 {
4815 if (!(supported2 & JO2_TERM_COLS))
4816 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004817 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004818 opt->jo_term_cols = get_tv_number(item);
4819 }
4820 else if (STRCMP(hi->hi_key, "vertical") == 0)
4821 {
4822 if (!(supported2 & JO2_VERTICAL))
4823 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004824 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004825 opt->jo_vertical = get_tv_number(item);
4826 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004827 else if (STRCMP(hi->hi_key, "curwin") == 0)
4828 {
4829 if (!(supported2 & JO2_CURWIN))
4830 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004831 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaarda43b612017-08-11 22:27:50 +02004832 opt->jo_curwin = get_tv_number(item);
4833 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004834 else if (STRCMP(hi->hi_key, "hidden") == 0)
4835 {
4836 if (!(supported2 & JO2_HIDDEN))
4837 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004838 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004839 opt->jo_hidden = get_tv_number(item);
4840 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004841 else if (STRCMP(hi->hi_key, "norestore") == 0)
4842 {
4843 if (!(supported2 & JO2_NORESTORE))
4844 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004845 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004846 opt->jo_term_norestore = get_tv_number(item);
4847 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004848 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4849 {
4850 if (!(supported2 & JO2_TERM_KILL))
4851 break;
4852 opt->jo_set2 |= JO2_TERM_KILL;
4853 opt->jo_term_kill = get_tv_string_chk(item);
4854 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004855# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4856 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4857 {
4858 int n = 0;
4859 listitem_T *li;
4860 long_u rgb[16];
4861
4862 if (!(supported2 & JO2_ANSI_COLORS))
4863 break;
4864
4865 if (item == NULL || item->v_type != VAR_LIST
4866 || item->vval.v_list == NULL)
4867 {
4868 EMSG2(_(e_invargval), "ansi_colors");
4869 return FAIL;
4870 }
4871
4872 li = item->vval.v_list->lv_first;
4873 for (; li != NULL && n < 16; li = li->li_next, n++)
4874 {
4875 char_u *color_name;
4876 guicolor_T guicolor;
4877
4878 color_name = get_tv_string_chk(&li->li_tv);
4879 if (color_name == NULL)
4880 return FAIL;
4881
4882 guicolor = GUI_GET_COLOR(color_name);
4883 if (guicolor == INVALCOLOR)
4884 return FAIL;
4885
4886 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4887 }
4888
4889 if (n != 16 || li != NULL)
4890 {
4891 EMSG2(_(e_invargval), "ansi_colors");
4892 return FAIL;
4893 }
4894
4895 opt->jo_set2 |= JO2_ANSI_COLORS;
4896 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4897 }
4898# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004899#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004900 else if (STRCMP(hi->hi_key, "env") == 0)
4901 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004902 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004903 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004904 if (item->v_type != VAR_DICT)
4905 {
4906 EMSG2(_(e_invargval), "env");
4907 return FAIL;
4908 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004909 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004910 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004911 if (opt->jo_env != NULL)
4912 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004913 }
4914 else if (STRCMP(hi->hi_key, "cwd") == 0)
4915 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004916 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004917 break;
4918 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4919 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4920 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004921 EMSG2(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004922 return FAIL;
4923 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004924 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004925 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004926 else if (STRCMP(hi->hi_key, "waittime") == 0)
4927 {
4928 if (!(supported & JO_WAITTIME))
4929 break;
4930 opt->jo_set |= JO_WAITTIME;
4931 opt->jo_waittime = get_tv_number(item);
4932 }
4933 else if (STRCMP(hi->hi_key, "timeout") == 0)
4934 {
4935 if (!(supported & JO_TIMEOUT))
4936 break;
4937 opt->jo_set |= JO_TIMEOUT;
4938 opt->jo_timeout = get_tv_number(item);
4939 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004940 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004941 {
4942 if (!(supported & JO_OUT_TIMEOUT))
4943 break;
4944 opt->jo_set |= JO_OUT_TIMEOUT;
4945 opt->jo_out_timeout = get_tv_number(item);
4946 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004947 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004948 {
4949 if (!(supported & JO_ERR_TIMEOUT))
4950 break;
4951 opt->jo_set |= JO_ERR_TIMEOUT;
4952 opt->jo_err_timeout = get_tv_number(item);
4953 }
4954 else if (STRCMP(hi->hi_key, "part") == 0)
4955 {
4956 if (!(supported & JO_PART))
4957 break;
4958 opt->jo_set |= JO_PART;
4959 val = get_tv_string(item);
4960 if (STRCMP(val, "err") == 0)
4961 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004962 else if (STRCMP(val, "out") == 0)
4963 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004964 else
4965 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004966 EMSG3(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004967 return FAIL;
4968 }
4969 }
4970 else if (STRCMP(hi->hi_key, "id") == 0)
4971 {
4972 if (!(supported & JO_ID))
4973 break;
4974 opt->jo_set |= JO_ID;
4975 opt->jo_id = get_tv_number(item);
4976 }
4977 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4978 {
4979 if (!(supported & JO_STOPONEXIT))
4980 break;
4981 opt->jo_set |= JO_STOPONEXIT;
4982 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4983 opt->jo_soe_buf);
4984 if (opt->jo_stoponexit == NULL)
4985 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004986 EMSG2(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004987 return FAIL;
4988 }
4989 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004990 else if (STRCMP(hi->hi_key, "block_write") == 0)
4991 {
4992 if (!(supported & JO_BLOCK_WRITE))
4993 break;
4994 opt->jo_set |= JO_BLOCK_WRITE;
4995 opt->jo_block_write = get_tv_number(item);
4996 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004997 else
4998 break;
4999 --todo;
5000 }
5001 if (todo > 0)
5002 {
5003 EMSG2(_(e_invarg2), hi->hi_key);
5004 return FAIL;
5005 }
5006
5007 return OK;
5008}
5009
5010/*
5011 * Get the channel from the argument.
5012 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005013 * When "check_open" is TRUE check that the channel can be used.
5014 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005015 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005016 */
5017 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005018get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005019{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005020 channel_T *channel = NULL;
5021 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005022
5023 if (tv->v_type == VAR_JOB)
5024 {
5025 if (tv->vval.v_job != NULL)
5026 channel = tv->vval.v_job->jv_channel;
5027 }
5028 else if (tv->v_type == VAR_CHANNEL)
5029 {
5030 channel = tv->vval.v_channel;
5031 }
5032 else
5033 {
5034 EMSG2(_(e_invarg2), get_tv_string(tv));
5035 return NULL;
5036 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005037 if (channel != NULL && reading)
5038 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005039 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005040
Bram Moolenaar437905c2016-04-26 19:01:05 +02005041 if (check_open && (channel == NULL || (!channel_is_open(channel)
5042 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005043 {
5044 EMSG(_("E906: not an open channel"));
5045 return NULL;
5046 }
5047 return channel;
5048}
5049
5050static job_T *first_job = NULL;
5051
5052 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005053job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005054{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005055 int i;
5056
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005057 ch_log(job->jv_channel, "Freeing job");
5058 if (job->jv_channel != NULL)
5059 {
5060 /* The link from the channel to the job doesn't count as a reference,
5061 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005062 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005063 * NULL the reference. We don't set ch_job_killed, unreferencing the
5064 * job doesn't mean it stops running. */
5065 job->jv_channel->ch_job = NULL;
5066 channel_unref(job->jv_channel);
5067 }
5068 mch_clear_job(job);
5069
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005070 vim_free(job->jv_tty_in);
5071 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005072 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005073 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005074 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005075 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005076 for (i = 0; job->jv_argv[i] != NULL; i++)
5077 vim_free(job->jv_argv[i]);
5078 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005079 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005080}
5081
5082 static void
5083job_free_job(job_T *job)
5084{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005085 if (job->jv_next != NULL)
5086 job->jv_next->jv_prev = job->jv_prev;
5087 if (job->jv_prev == NULL)
5088 first_job = job->jv_next;
5089 else
5090 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005091 vim_free(job);
5092}
5093
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005094 static void
5095job_free(job_T *job)
5096{
5097 if (!in_free_unref_items)
5098 {
5099 job_free_contents(job);
5100 job_free_job(job);
5101 }
5102}
5103
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005104#if defined(EXITFREE) || defined(PROTO)
5105 void
5106job_free_all(void)
5107{
5108 while (first_job != NULL)
5109 job_free(first_job);
5110}
5111#endif
5112
5113/*
5114 * Return TRUE if we need to check if the process of "job" has ended.
5115 */
5116 static int
5117job_need_end_check(job_T *job)
5118{
5119 return job->jv_status == JOB_STARTED
5120 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5121}
5122
5123/*
5124 * Return TRUE if the channel of "job" is still useful.
5125 */
5126 static int
5127job_channel_still_useful(job_T *job)
5128{
5129 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5130}
5131
5132/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005133 * Return TRUE if the channel of "job" is closeable.
5134 */
5135 static int
5136job_channel_can_close(job_T *job)
5137{
5138 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5139}
5140
5141/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005142 * Return TRUE if the job should not be freed yet. Do not free the job when
5143 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5144 * or when the associated channel will do something with the job output.
5145 */
5146 static int
5147job_still_useful(job_T *job)
5148{
5149 return job_need_end_check(job) || job_channel_still_useful(job);
5150}
5151
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005152#if defined(GUI_MAY_FORK) || defined(PROTO)
5153/*
5154 * Return TRUE when there is any running job that we care about.
5155 */
5156 int
5157job_any_running()
5158{
5159 job_T *job;
5160
5161 for (job = first_job; job != NULL; job = job->jv_next)
5162 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005163 {
5164 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005165 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005166 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005167 return FALSE;
5168}
5169#endif
5170
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005171#if !defined(USE_ARGV) || defined(PROTO)
5172/*
5173 * Escape one argument for an external command.
5174 * Returns the escaped string in allocated memory. NULL when out of memory.
5175 */
5176 static char_u *
5177win32_escape_arg(char_u *arg)
5178{
5179 int slen, dlen;
5180 int escaping = 0;
5181 int i;
5182 char_u *s, *d;
5183 char_u *escaped_arg;
5184 int has_spaces = FALSE;
5185
5186 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005187 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005188 dlen = slen;
5189 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5190 {
5191 if (*s == '"' || *s == '\\')
5192 ++dlen;
5193 if (*s == ' ' || *s == '\t')
5194 has_spaces = TRUE;
5195 }
5196
5197 if (has_spaces)
5198 dlen += 2;
5199
5200 if (dlen == slen)
5201 return vim_strsave(arg);
5202
5203 /* Allocate memory for the result and fill it. */
5204 escaped_arg = alloc(dlen + 1);
5205 if (escaped_arg == NULL)
5206 return NULL;
5207 memset(escaped_arg, 0, dlen+1);
5208
5209 d = escaped_arg;
5210
5211 if (has_spaces)
5212 *d++ = '"';
5213
5214 for (s = arg; *s != NUL;)
5215 {
5216 switch (*s)
5217 {
5218 case '"':
5219 for (i = 0; i < escaping; i++)
5220 *d++ = '\\';
5221 escaping = 0;
5222 *d++ = '\\';
5223 *d++ = *s++;
5224 break;
5225 case '\\':
5226 escaping++;
5227 *d++ = *s++;
5228 break;
5229 default:
5230 escaping = 0;
5231 MB_COPY_CHAR(s, d);
5232 break;
5233 }
5234 }
5235
5236 /* add terminating quote and finish with a NUL */
5237 if (has_spaces)
5238 {
5239 for (i = 0; i < escaping; i++)
5240 *d++ = '\\';
5241 *d++ = '"';
5242 }
5243 *d = NUL;
5244
5245 return escaped_arg;
5246}
5247
5248/*
5249 * Build a command line from a list, taking care of escaping.
5250 * The result is put in gap->ga_data.
5251 * Returns FAIL when out of memory.
5252 */
5253 int
5254win32_build_cmd(list_T *l, garray_T *gap)
5255{
5256 listitem_T *li;
5257 char_u *s;
5258
5259 for (li = l->lv_first; li != NULL; li = li->li_next)
5260 {
5261 s = get_tv_string_chk(&li->li_tv);
5262 if (s == NULL)
5263 return FAIL;
5264 s = win32_escape_arg(s);
5265 if (s == NULL)
5266 return FAIL;
5267 ga_concat(gap, s);
5268 vim_free(s);
5269 if (li->li_next != NULL)
5270 ga_append(gap, ' ');
5271 }
5272 return OK;
5273}
5274#endif
5275
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005276/*
5277 * NOTE: Must call job_cleanup() only once right after the status of "job"
5278 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5279 * mch_detect_ended_job() returned non-NULL).
5280 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005281 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005282job_cleanup(job_T *job)
5283{
5284 if (job->jv_status != JOB_ENDED)
5285 return;
5286
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005287 /* Ready to cleanup the job. */
5288 job->jv_status = JOB_FINISHED;
5289
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005290 /* When only channel-in is kept open, close explicitly. */
5291 if (job->jv_channel != NULL)
5292 ch_close_part(job->jv_channel, PART_IN);
5293
Bram Moolenaar97792de2016-10-15 18:36:49 +02005294 if (job->jv_exit_cb != NULL)
5295 {
5296 typval_T argv[3];
5297 typval_T rettv;
5298 int dummy;
5299
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005300 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005301 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005302 ++job->jv_refcount;
5303 argv[0].v_type = VAR_JOB;
5304 argv[0].vval.v_job = job;
5305 argv[1].v_type = VAR_NUMBER;
5306 argv[1].vval.v_number = job->jv_exitval;
5307 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5308 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5309 job->jv_exit_partial, NULL);
5310 clear_tv(&rettv);
5311 --job->jv_refcount;
5312 channel_need_redraw = TRUE;
5313 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005314
5315 /* Do not free the job in case the close callback of the associated channel
5316 * isn't invoked yet and may get information by job_info(). */
5317 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005318 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005319 /* The job was already unreferenced and the associated channel was
5320 * detached, now that it ended it can be freed. Careful: caller must
5321 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005322 job_free(job);
5323 }
5324}
5325
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005326/*
5327 * Mark references in jobs that are still useful.
5328 */
5329 int
5330set_ref_in_job(int copyID)
5331{
5332 int abort = FALSE;
5333 job_T *job;
5334 typval_T tv;
5335
5336 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005337 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005338 {
5339 tv.v_type = VAR_JOB;
5340 tv.vval.v_job = job;
5341 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5342 }
5343 return abort;
5344}
5345
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005346/*
5347 * Dereference "job". Note that after this "job" may have been freed.
5348 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005349 void
5350job_unref(job_T *job)
5351{
5352 if (job != NULL && --job->jv_refcount <= 0)
5353 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005354 /* Do not free the job if there is a channel where the close callback
5355 * may get the job info. */
5356 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005357 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005358 /* Do not free the job when it has not ended yet and there is a
5359 * "stoponexit" flag or an exit callback. */
5360 if (!job_need_end_check(job))
5361 {
5362 job_free(job);
5363 }
5364 else if (job->jv_channel != NULL)
5365 {
5366 /* Do remove the link to the channel, otherwise it hangs
5367 * around until Vim exits. See job_free() for refcount. */
5368 ch_log(job->jv_channel, "detaching channel from job");
5369 job->jv_channel->ch_job = NULL;
5370 channel_unref(job->jv_channel);
5371 job->jv_channel = NULL;
5372 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005373 }
5374 }
5375}
5376
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005377 int
5378free_unused_jobs_contents(int copyID, int mask)
5379{
5380 int did_free = FALSE;
5381 job_T *job;
5382
5383 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005384 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005385 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005386 {
5387 /* Free the channel and ordinary items it contains, but don't
5388 * recurse into Lists, Dictionaries etc. */
5389 job_free_contents(job);
5390 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005391 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005392 return did_free;
5393}
5394
5395 void
5396free_unused_jobs(int copyID, int mask)
5397{
5398 job_T *job;
5399 job_T *job_next;
5400
5401 for (job = first_job; job != NULL; job = job_next)
5402 {
5403 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005404 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005405 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005406 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005407 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005408 job_free_job(job);
5409 }
5410 }
5411}
5412
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005413/*
5414 * Allocate a job. Sets the refcount to one and sets options default.
5415 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005416 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005417job_alloc(void)
5418{
5419 job_T *job;
5420
5421 job = (job_T *)alloc_clear(sizeof(job_T));
5422 if (job != NULL)
5423 {
5424 job->jv_refcount = 1;
5425 job->jv_stoponexit = vim_strsave((char_u *)"term");
5426
5427 if (first_job != NULL)
5428 {
5429 first_job->jv_prev = job;
5430 job->jv_next = first_job;
5431 }
5432 first_job = job;
5433 }
5434 return job;
5435}
5436
5437 void
5438job_set_options(job_T *job, jobopt_T *opt)
5439{
5440 if (opt->jo_set & JO_STOPONEXIT)
5441 {
5442 vim_free(job->jv_stoponexit);
5443 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5444 job->jv_stoponexit = NULL;
5445 else
5446 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5447 }
5448 if (opt->jo_set & JO_EXIT_CB)
5449 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005450 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005451 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005452 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005453 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005454 job->jv_exit_partial = NULL;
5455 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005456 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005457 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005458 job->jv_exit_partial = opt->jo_exit_partial;
5459 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005460 {
5461 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005462 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005463 }
5464 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005465 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005466 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005467 func_ref(job->jv_exit_cb);
5468 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005469 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005470 }
5471}
5472
5473/*
5474 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5475 */
5476 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005477job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005478{
5479 job_T *job;
5480
5481 for (job = first_job; job != NULL; job = job->jv_next)
5482 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005483 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005484}
5485
5486/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005487 * Return TRUE when there is any job that has an exit callback and might exit,
5488 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005489 */
5490 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005491has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005492{
5493 job_T *job;
5494
5495 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005496 /* Only should check if the channel has been closed, if the channel is
5497 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005498 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5499 || (job->jv_status == JOB_FINISHED
5500 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005501 return TRUE;
5502 return FALSE;
5503}
5504
Bram Moolenaar97792de2016-10-15 18:36:49 +02005505#define MAX_CHECK_ENDED 8
5506
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005507/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005508 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005509 */
5510 void
5511job_check_ended(void)
5512{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005513 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005514
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005515 if (first_job == NULL)
5516 return;
5517
Bram Moolenaar97792de2016-10-15 18:36:49 +02005518 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005519 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005520 /* NOTE: mch_detect_ended_job() must only return a job of which the
5521 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005522 job_T *job = mch_detect_ended_job(first_job);
5523
5524 if (job == NULL)
5525 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005526 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005527 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005528
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005529 if (channel_need_redraw)
5530 {
5531 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005532 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005533 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005534}
5535
5536/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005537 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005538 * "argv_arg" is only for Unix.
5539 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005540 * The returned job has a refcount of one.
5541 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005542 */
5543 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005544job_start(
5545 typval_T *argvars,
5546 char **argv_arg,
5547 jobopt_T *opt_arg,
5548 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005549{
5550 job_T *job;
5551 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005552 char **argv = NULL;
5553 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005554#if defined(UNIX)
5555# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005556 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005557#else
5558 garray_T ga;
5559#endif
5560 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005561 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005562
5563 job = job_alloc();
5564 if (job == NULL)
5565 return NULL;
5566
5567 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005568#ifndef USE_ARGV
5569 ga_init2(&ga, (int)sizeof(char*), 20);
5570#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005571
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005572 if (opt_arg != NULL)
5573 opt = *opt_arg;
5574 else
5575 {
5576 /* Default mode is NL. */
5577 clear_job_options(&opt);
5578 opt.jo_mode = MODE_NL;
5579 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005580 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5581 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5582 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005583 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005584 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005585
5586 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005587 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005588 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5589 && opt.jo_io[part] == JIO_FILE
5590 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5591 || *opt.jo_io_name[part] == NUL))
5592 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005593 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005594 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005595 }
5596
5597 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5598 {
5599 buf_T *buf = NULL;
5600
5601 /* check that we can find the buffer before starting the job */
5602 if (opt.jo_set & JO_IN_BUF)
5603 {
5604 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5605 if (buf == NULL)
5606 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5607 }
5608 else if (!(opt.jo_set & JO_IN_NAME))
5609 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005610 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005611 }
5612 else
5613 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5614 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005615 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005616 if (buf->b_ml.ml_mfp == NULL)
5617 {
5618 char_u numbuf[NUMBUFLEN];
5619 char_u *s;
5620
5621 if (opt.jo_set & JO_IN_BUF)
5622 {
5623 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5624 s = numbuf;
5625 }
5626 else
5627 s = opt.jo_io_name[PART_IN];
5628 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005629 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005630 }
5631 job->jv_in_buf = buf;
5632 }
5633
5634 job_set_options(job, &opt);
5635
Bram Moolenaar13568252018-03-16 20:46:58 +01005636#ifdef USE_ARGV
5637 if (argv_arg != NULL)
5638 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005639 /* Make a copy of argv_arg for job->jv_argv. */
5640 for (i = 0; argv_arg[i] != NULL; i++)
5641 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005642 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005643 if (argv == NULL)
5644 goto theend;
5645 for (i = 0; i < argc; i++)
5646 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5647 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005648 }
5649 else
5650#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005651 if (argvars[0].v_type == VAR_STRING)
5652 {
5653 /* Command is a string. */
5654 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005655 if (cmd == NULL || *cmd == NUL)
5656 {
5657 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005658 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005659 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005660
5661 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005662 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005663 }
5664 else if (argvars[0].v_type != VAR_LIST
5665 || argvars[0].vval.v_list == NULL
5666 || argvars[0].vval.v_list->lv_len < 1)
5667 {
5668 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005669 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005670 }
5671 else
5672 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005673 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005674
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005675 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005676 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005677#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005678 if (win32_build_cmd(l, &ga) == FAIL)
5679 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005680 cmd = ga.ga_data;
5681#endif
5682 }
5683
Bram Moolenaar20608922018-04-21 22:30:08 +02005684 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005685 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005686
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005687#ifdef USE_ARGV
5688 if (ch_log_active())
5689 {
5690 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005691
5692 ga_init2(&ga, (int)sizeof(char), 200);
5693 for (i = 0; i < argc; ++i)
5694 {
5695 if (i > 0)
5696 ga_concat(&ga, (char_u *)" ");
5697 ga_concat(&ga, (char_u *)argv[i]);
5698 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005699 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005700 ga_clear(&ga);
5701 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005702 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005703#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005704 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005705 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005706#endif
5707
5708 /* If the channel is reading from a buffer, write lines now. */
5709 if (job->jv_channel != NULL)
5710 channel_write_in(job->jv_channel);
5711
5712theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005713#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005714 vim_free(ga.ga_data);
5715#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005716 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005717 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005718 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005719 return job;
5720}
5721
5722/*
5723 * Get the status of "job" and invoke the exit callback when needed.
5724 * The returned string is not allocated.
5725 */
5726 char *
5727job_status(job_T *job)
5728{
5729 char *result;
5730
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005731 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005732 /* No need to check, dead is dead. */
5733 result = "dead";
5734 else if (job->jv_status == JOB_FAILED)
5735 result = "fail";
5736 else
5737 {
5738 result = mch_job_status(job);
5739 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005740 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005741 }
5742 return result;
5743}
5744
Bram Moolenaar8950a562016-03-12 15:22:55 +01005745/*
5746 * Implementation of job_info().
5747 */
5748 void
5749job_info(job_T *job, dict_T *dict)
5750{
5751 dictitem_T *item;
5752 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005753 list_T *l;
5754 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005755
Bram Moolenaare0be1672018-07-08 16:50:37 +02005756 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005757
5758 item = dictitem_alloc((char_u *)"channel");
5759 if (item == NULL)
5760 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005761 item->di_tv.v_type = VAR_CHANNEL;
5762 item->di_tv.vval.v_channel = job->jv_channel;
5763 if (job->jv_channel != NULL)
5764 ++job->jv_channel->ch_refcount;
5765 if (dict_add(dict, item) == FAIL)
5766 dictitem_free(item);
5767
5768#ifdef UNIX
5769 nr = job->jv_pid;
5770#else
5771 nr = job->jv_proc_info.dwProcessId;
5772#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005773 dict_add_number(dict, "process", nr);
5774 dict_add_string(dict, "tty_in", job->jv_tty_in);
5775 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005776
Bram Moolenaare0be1672018-07-08 16:50:37 +02005777 dict_add_number(dict, "exitval", job->jv_exitval);
5778 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5779 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005780
5781 l = list_alloc();
5782 if (l != NULL)
5783 {
5784 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005785 if (job->jv_argv != NULL)
5786 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005787 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005788 }
5789}
5790
5791/*
5792 * Implementation of job_info() to return info for all jobs.
5793 */
5794 void
5795job_info_all(list_T *l)
5796{
5797 job_T *job;
5798 typval_T tv;
5799
5800 for (job = first_job; job != NULL; job = job->jv_next)
5801 {
5802 tv.v_type = VAR_JOB;
5803 tv.vval.v_job = job;
5804
5805 if (list_append_tv(l, &tv) != OK)
5806 return;
5807 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005808}
5809
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005810/*
5811 * Send a signal to "job". Implements job_stop().
5812 * When "type" is not NULL use this for the type.
5813 * Otherwise use argvars[1] for the type.
5814 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005815 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005816job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005817{
5818 char_u *arg;
5819
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005820 if (type != NULL)
5821 arg = (char_u *)type;
5822 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005823 arg = (char_u *)"";
5824 else
5825 {
5826 arg = get_tv_string_chk(&argvars[1]);
5827 if (arg == NULL)
5828 {
5829 EMSG(_(e_invarg));
5830 return 0;
5831 }
5832 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005833 if (job->jv_status == JOB_FAILED)
5834 {
5835 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5836 return 0;
5837 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005838 if (job->jv_status == JOB_ENDED)
5839 {
5840 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5841 return 0;
5842 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005843 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005844 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005845 return 0;
5846
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005847 /* Assume that only "kill" will kill the job. */
5848 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005849 job->jv_channel->ch_job_killed = TRUE;
5850
5851 /* We don't try freeing the job, obviously the caller still has a
5852 * reference to it. */
5853 return 1;
5854}
5855
Bram Moolenaarf2732452018-06-03 14:47:35 +02005856 void
5857invoke_prompt_callback(void)
5858{
5859 typval_T rettv;
5860 int dummy;
5861 typval_T argv[2];
5862 char_u *text;
5863 char_u *prompt;
5864 linenr_T lnum = curbuf->b_ml.ml_line_count;
5865
5866 // Add a new line for the prompt before invoking the callback, so that
5867 // text can always be inserted above the last line.
5868 ml_append(lnum, (char_u *)"", 0, FALSE);
5869 curwin->w_cursor.lnum = lnum + 1;
5870 curwin->w_cursor.col = 0;
5871
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005872 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005873 return;
5874 text = ml_get(lnum);
5875 prompt = prompt_text();
5876 if (STRLEN(text) >= STRLEN(prompt))
5877 text += STRLEN(prompt);
5878 argv[0].v_type = VAR_STRING;
5879 argv[0].vval.v_string = vim_strsave(text);
5880 argv[1].v_type = VAR_UNKNOWN;
5881
5882 call_func(curbuf->b_prompt_callback,
5883 (int)STRLEN(curbuf->b_prompt_callback),
5884 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5885 curbuf->b_prompt_partial, NULL);
5886 clear_tv(&argv[0]);
5887 clear_tv(&rettv);
5888}
5889
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005890/*
5891 * Return TRUE when the interrupt callback was invoked.
5892 */
5893 int
5894invoke_prompt_interrupt(void)
5895{
5896 typval_T rettv;
5897 int dummy;
5898 typval_T argv[1];
5899
5900 if (curbuf->b_prompt_interrupt == NULL
5901 || *curbuf->b_prompt_interrupt == NUL)
5902 return FALSE;
5903 argv[0].v_type = VAR_UNKNOWN;
5904
5905 got_int = FALSE; // don't skip executing commands
5906 call_func(curbuf->b_prompt_interrupt,
5907 (int)STRLEN(curbuf->b_prompt_interrupt),
5908 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5909 curbuf->b_prompt_int_partial, NULL);
5910 clear_tv(&rettv);
5911 return TRUE;
5912}
5913
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005914#endif /* FEAT_JOB_CHANNEL */