blob: 282340eefc74ed2fe618b2e07b605760954135d2 [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 Moolenaar910b8aa2016-02-16 21:03:07 +01001183
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001184 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001185 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001186 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1187 if (opt->jo_set & JO_OUT_TIMEOUT)
1188 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1189 if (opt->jo_set & JO_ERR_TIMEOUT)
1190 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001191 if (opt->jo_set & JO_BLOCK_WRITE)
1192 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001193
1194 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001195 set_callback(&channel->ch_callback, &channel->ch_partial,
1196 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001197 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001198 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1199 &channel->ch_part[PART_OUT].ch_partial,
1200 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001201 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001202 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1203 &channel->ch_part[PART_ERR].ch_partial,
1204 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001205 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001206 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1207 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001208 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001209
1210 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1211 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001212 buf_T *buf;
1213
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001214 /* writing output to a buffer. Default mode is NL. */
1215 if (!(opt->jo_set & JO_OUT_MODE))
1216 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001217 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001218 {
1219 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1220 if (buf == NULL)
1221 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1222 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001223 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001224 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001225 int msg = TRUE;
1226
1227 if (opt->jo_set2 & JO2_OUT_MSG)
1228 msg = opt->jo_message[PART_OUT];
1229 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 }
1231 if (buf != NULL)
1232 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001233 if (opt->jo_set & JO_OUT_MODIFIABLE)
1234 channel->ch_part[PART_OUT].ch_nomodifiable =
1235 !opt->jo_modifiable[PART_OUT];
1236
1237 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1238 {
1239 EMSG(_(e_modifiable));
1240 }
1241 else
1242 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001243 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001245 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001246 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001247 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001248 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001249
1250 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1251 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1252 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1253 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001254 buf_T *buf;
1255
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001256 /* writing err to a buffer. Default mode is NL. */
1257 if (!(opt->jo_set & JO_ERR_MODE))
1258 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1259 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001260 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001261 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001262 {
1263 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1264 if (buf == NULL)
1265 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1266 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001267 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001268 {
1269 int msg = TRUE;
1270
1271 if (opt->jo_set2 & JO2_ERR_MSG)
1272 msg = opt->jo_message[PART_ERR];
1273 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1274 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001275 if (buf != NULL)
1276 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001277 if (opt->jo_set & JO_ERR_MODIFIABLE)
1278 channel->ch_part[PART_ERR].ch_nomodifiable =
1279 !opt->jo_modifiable[PART_ERR];
1280 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1281 {
1282 EMSG(_(e_modifiable));
1283 }
1284 else
1285 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001286 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001287 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001288 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001289 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001290 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001291 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001292
1293 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1294 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1295 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001296}
1297
1298/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001299 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001300 */
1301 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001302channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001303 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001304 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001305 char_u *callback,
1306 partial_T *partial,
1307 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001308{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001309 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001310 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1311
1312 if (item != NULL)
1313 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001314 item->cq_partial = partial;
1315 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001316 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001317 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001318 item->cq_callback = callback;
1319 }
1320 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001321 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001322 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001323 func_ref(item->cq_callback);
1324 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001325 item->cq_seq_nr = id;
1326 item->cq_prev = head->cq_prev;
1327 head->cq_prev = item;
1328 item->cq_next = NULL;
1329 if (item->cq_prev == NULL)
1330 head->cq_next = item;
1331 else
1332 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001333 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001334}
1335
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001336 static void
1337write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1338{
1339 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001340 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001341 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001342 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001343
Bram Moolenaar655da312016-05-28 22:22:34 +02001344 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001345 if ((p = alloc(len + 2)) == NULL)
1346 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001347 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001348
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001349 if (channel->ch_write_text_mode)
1350 p[len] = CAR;
1351 else
1352 {
1353 for (i = 0; i < len; ++i)
1354 if (p[i] == NL)
1355 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001356
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001357 p[len] = NL;
1358 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001359 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001360 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001361 vim_free(p);
1362}
1363
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001364/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001365 * Return TRUE if "channel" can be written to.
1366 * Returns FALSE if the input is closed or the write would block.
1367 */
1368 static int
1369can_write_buf_line(channel_T *channel)
1370{
1371 chanpart_T *in_part = &channel->ch_part[PART_IN];
1372
1373 if (in_part->ch_fd == INVALID_FD)
1374 return FALSE; /* pipe was closed */
1375
1376 /* for testing: block every other attempt to write */
1377 if (in_part->ch_block_write == 1)
1378 in_part->ch_block_write = -1;
1379 else if (in_part->ch_block_write == -1)
1380 in_part->ch_block_write = 1;
1381
1382 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1383#ifndef WIN32
1384 {
1385# if defined(HAVE_SELECT)
1386 struct timeval tval;
1387 fd_set wfds;
1388 int ret;
1389
1390 FD_ZERO(&wfds);
1391 FD_SET((int)in_part->ch_fd, &wfds);
1392 tval.tv_sec = 0;
1393 tval.tv_usec = 0;
1394 for (;;)
1395 {
1396 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1397# ifdef EINTR
1398 SOCK_ERRNO;
1399 if (ret == -1 && errno == EINTR)
1400 continue;
1401# endif
1402 if (ret <= 0 || in_part->ch_block_write == 1)
1403 {
1404 if (ret > 0)
1405 ch_log(channel, "FAKED Input not ready for writing");
1406 else
1407 ch_log(channel, "Input not ready for writing");
1408 return FALSE;
1409 }
1410 break;
1411 }
1412# else
1413 struct pollfd fds;
1414
1415 fds.fd = in_part->ch_fd;
1416 fds.events = POLLOUT;
1417 if (poll(&fds, 1, 0) <= 0)
1418 {
1419 ch_log(channel, "Input not ready for writing");
1420 return FALSE;
1421 }
1422 if (in_part->ch_block_write == 1)
1423 {
1424 ch_log(channel, "FAKED Input not ready for writing");
1425 return FALSE;
1426 }
1427# endif
1428 }
1429#endif
1430 return TRUE;
1431}
1432
1433/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001434 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001435 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001436 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001437channel_write_in(channel_T *channel)
1438{
1439 chanpart_T *in_part = &channel->ch_part[PART_IN];
1440 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001441 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001442 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001443
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001444 if (buf == NULL || in_part->ch_buf_append)
1445 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001446 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001447 {
1448 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001449 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001450 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001451 return;
1452 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001453
1454 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1455 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1456 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001457 if (!can_write_buf_line(channel))
1458 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001459 write_buf_line(buf, lnum, channel);
1460 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001461 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001462
1463 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001464 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001465 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001466 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001467
Bram Moolenaar014069a2016-03-03 22:51:40 +01001468 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001469 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001470 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001471#if defined(FEAT_TERMINAL)
1472 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001473 if (channel->ch_job != NULL)
1474 term_send_eof(channel);
1475#endif
1476
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001477 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001478 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001479 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001480
1481 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001482 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001483 }
1484 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001485 ch_log(channel, "Still %ld more lines to write",
1486 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001487}
1488
1489/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001490 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001491 */
1492 void
1493channel_buffer_free(buf_T *buf)
1494{
1495 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001496 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001497
1498 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001499 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001500 {
1501 chanpart_T *ch_part = &channel->ch_part[part];
1502
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001503 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001504 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001505 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001506 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001507 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001508 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001509 }
1510}
1511
1512/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001513 * Write any lines waiting to be written to "channel".
1514 */
1515 static void
1516channel_write_input(channel_T *channel)
1517{
1518 chanpart_T *in_part = &channel->ch_part[PART_IN];
1519
1520 if (in_part->ch_writeque.wq_next != NULL)
1521 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1522 else if (in_part->ch_bufref.br_buf != NULL)
1523 {
1524 if (in_part->ch_buf_append)
1525 channel_write_new_lines(in_part->ch_bufref.br_buf);
1526 else
1527 channel_write_in(channel);
1528 }
1529}
1530
1531/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001532 * Write any lines waiting to be written to a channel.
1533 */
1534 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001535channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001536{
1537 channel_T *channel;
1538
1539 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001540 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001541}
1542
1543/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001544 * Write appended lines above the last one in "buf" to the channel.
1545 */
1546 void
1547channel_write_new_lines(buf_T *buf)
1548{
1549 channel_T *channel;
1550 int found_one = FALSE;
1551
1552 /* There could be more than one channel for the buffer, loop over all of
1553 * them. */
1554 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1555 {
1556 chanpart_T *in_part = &channel->ch_part[PART_IN];
1557 linenr_T lnum;
1558 int written = 0;
1559
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001560 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001561 {
1562 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001563 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001564 found_one = TRUE;
1565 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1566 ++lnum)
1567 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001568 if (!can_write_buf_line(channel))
1569 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001570 write_buf_line(buf, lnum, channel);
1571 ++written;
1572 }
1573
1574 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001575 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001576 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001577 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001578 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001579 ch_log(channel, "Still %ld more lines to write",
1580 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001581
1582 in_part->ch_buf_bot = lnum;
1583 }
1584 }
1585 if (!found_one)
1586 buf->b_write_to_channel = FALSE;
1587}
1588
1589/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001590 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001591 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001592 */
1593 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001594invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1595 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001596{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001597 typval_T rettv;
1598 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001599
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001600 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001601 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001602
Bram Moolenaar77073442016-02-13 23:23:53 +01001603 argv[0].v_type = VAR_CHANNEL;
1604 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001605
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001606 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1607 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001608 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001609 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001610}
1611
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001612/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001613 * Return the first node from "channel"/"part" without removing it.
1614 * Returns NULL if there is nothing.
1615 */
1616 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001617channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001618{
1619 readq_T *head = &channel->ch_part[part].ch_head;
1620
1621 return head->rq_next;
1622}
1623
1624/*
1625 * Return a pointer to the first NL in "node".
1626 * Skips over NUL characters.
1627 * Returns NULL if there is no NL.
1628 */
1629 char_u *
1630channel_first_nl(readq_T *node)
1631{
1632 char_u *buffer = node->rq_buffer;
1633 long_u i;
1634
1635 for (i = 0; i < node->rq_buflen; ++i)
1636 if (buffer[i] == NL)
1637 return buffer + i;
1638 return NULL;
1639}
1640
1641/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001642 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001643 * The caller must free it.
1644 * Returns NULL if there is nothing.
1645 */
1646 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001647channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001648{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001649 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001650 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001651 char_u *p;
1652
Bram Moolenaar77073442016-02-13 23:23:53 +01001653 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001654 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001655 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001656 p = node->rq_buffer;
1657 head->rq_next = node->rq_next;
1658 if (node->rq_next == NULL)
1659 head->rq_prev = NULL;
1660 else
1661 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001662 vim_free(node);
1663 return p;
1664}
1665
1666/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001667 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001668 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001669 */
1670 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001671channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001673 readq_T *head = &channel->ch_part[part].ch_head;
1674 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001675 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001676 char_u *res;
1677 char_u *p;
1678
1679 /* If there is only one buffer just get that one. */
1680 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1681 return channel_get(channel, part);
1682
1683 /* Concatenate everything into one buffer. */
1684 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001685 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001686 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001687 if (res == NULL)
1688 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001689 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001690 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001691 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001692 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001693 p += node->rq_buflen;
1694 }
1695 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001696
1697 /* Free all buffers */
1698 do
1699 {
1700 p = channel_get(channel, part);
1701 vim_free(p);
1702 } while (p != NULL);
1703
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001704 /* turn all NUL into NL */
1705 while (len > 0)
1706 {
1707 --len;
1708 if (res[len] == NUL)
1709 res[len] = NL;
1710 }
1711
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001712 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713}
1714
1715/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001716 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001717 * Caller must check these bytes are available.
1718 */
1719 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001720channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001721{
1722 readq_T *head = &channel->ch_part[part].ch_head;
1723 readq_T *node = head->rq_next;
1724 char_u *buf = node->rq_buffer;
1725
1726 mch_memmove(buf, buf + len, node->rq_buflen - len);
1727 node->rq_buflen -= len;
1728}
1729
1730/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001731 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001732 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001733 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001734 */
1735 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001736channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001737{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001738 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001739 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001740 readq_T *last_node;
1741 readq_T *n;
1742 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001743 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001744 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001745
Bram Moolenaar77073442016-02-13 23:23:53 +01001746 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001747 return FAIL;
1748
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001749 last_node = node->rq_next;
1750 len = node->rq_buflen + last_node->rq_buflen + 1;
1751 if (want_nl)
1752 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001753 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001754 {
1755 last_node = last_node->rq_next;
1756 len += last_node->rq_buflen;
1757 }
1758
1759 p = newbuf = alloc(len);
1760 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001761 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001762 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001763 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001764 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001765 node->rq_buffer = newbuf;
1766 for (n = node; n != last_node; )
1767 {
1768 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001769 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001770 p += n->rq_buflen;
1771 vim_free(n->rq_buffer);
1772 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001773 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001774
1775 /* dispose of the collapsed nodes and their buffers */
1776 for (n = node->rq_next; n != last_node; )
1777 {
1778 n = n->rq_next;
1779 vim_free(n->rq_prev);
1780 }
1781 node->rq_next = last_node->rq_next;
1782 if (last_node->rq_next == NULL)
1783 head->rq_prev = node;
1784 else
1785 last_node->rq_next->rq_prev = node;
1786 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001787 return OK;
1788}
1789
1790/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001791 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001792 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793 * Returns OK or FAIL.
1794 */
1795 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001796channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001797 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001798{
1799 readq_T *node;
1800 readq_T *head = &channel->ch_part[part].ch_head;
1801 char_u *p;
1802 int i;
1803
1804 node = (readq_T *)alloc(sizeof(readq_T));
1805 if (node == NULL)
1806 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001807 /* A NUL is added at the end, because netbeans code expects that.
1808 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001809 node->rq_buffer = alloc(len + 1);
1810 if (node->rq_buffer == NULL)
1811 {
1812 vim_free(node);
1813 return FAIL; /* out of memory */
1814 }
1815
1816 if (channel->ch_part[part].ch_mode == MODE_NL)
1817 {
1818 /* Drop any CR before a NL. */
1819 p = node->rq_buffer;
1820 for (i = 0; i < len; ++i)
1821 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1822 *p++ = buf[i];
1823 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001825 }
1826 else
1827 {
1828 mch_memmove(node->rq_buffer, buf, len);
1829 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001830 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001831 }
1832
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001833 if (prepend)
1834 {
1835 /* preend node to the head of the queue */
1836 node->rq_next = head->rq_next;
1837 node->rq_prev = NULL;
1838 if (head->rq_next == NULL)
1839 head->rq_prev = node;
1840 else
1841 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001842 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001843 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001844 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001845 {
1846 /* append node to the tail of the queue */
1847 node->rq_next = NULL;
1848 node->rq_prev = head->rq_prev;
1849 if (head->rq_prev == NULL)
1850 head->rq_next = node;
1851 else
1852 head->rq_prev->rq_next = node;
1853 head->rq_prev = node;
1854 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001855
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001856 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001857 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001858 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001859 fprintf(log_fd, "'");
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001860 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001861 fprintf(log_fd, "'\n");
1862 }
1863 return OK;
1864}
1865
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001866/*
1867 * Try to fill the buffer of "reader".
1868 * Returns FALSE when nothing was added.
1869 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001870 static int
1871channel_fill(js_read_T *reader)
1872{
1873 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001874 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001875 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001876 int keeplen;
1877 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001878 char_u *p;
1879
1880 if (next == NULL)
1881 return FALSE;
1882
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001883 keeplen = reader->js_end - reader->js_buf;
1884 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001885 {
1886 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001887 addlen = (int)STRLEN(next);
1888 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001889 if (p == NULL)
1890 {
1891 vim_free(next);
1892 return FALSE;
1893 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001894 mch_memmove(p, reader->js_buf, keeplen);
1895 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001896 vim_free(next);
1897 next = p;
1898 }
1899
1900 vim_free(reader->js_buf);
1901 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001902 return TRUE;
1903}
1904
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001905/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001906 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001907 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001908 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001909 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001910 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001911channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001912{
1913 js_read_T reader;
1914 typval_T listtv;
1915 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001916 chanpart_T *chanpart = &channel->ch_part[part];
1917 jsonq_T *head = &chanpart->ch_json_head;
1918 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001919 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001920
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001921 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001922 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001923
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001924 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001925 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001926 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001927 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001928 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001929
1930 /* When a message is incomplete we wait for a short while for more to
1931 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001932 * or list will make us hang.
1933 * Do not generate error messages, they will be written in a channel log. */
1934 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001935 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001936 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001937 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001938 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001939 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001940 /* Only accept the response when it is a list with at least two
1941 * items. */
1942 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001943 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001944 if (listtv.v_type != VAR_LIST)
1945 ch_error(channel, "Did not receive a list, discarding");
1946 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001947 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001948 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001950 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001951 else
1952 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001953 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1954 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001955 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001956 else
1957 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001958 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001959 item->jq_value = alloc_tv();
1960 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001961 {
1962 vim_free(item);
1963 clear_tv(&listtv);
1964 }
1965 else
1966 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001967 *item->jq_value = listtv;
1968 item->jq_prev = head->jq_prev;
1969 head->jq_prev = item;
1970 item->jq_next = NULL;
1971 if (item->jq_prev == NULL)
1972 head->jq_next = item;
1973 else
1974 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001975 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001976 }
1977 }
1978 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001979
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001980 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001981 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001982 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001983 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001984 size_t buflen = STRLEN(reader.js_buf);
1985
1986 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001987 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001988 /* First time encountering incomplete message or after receiving
1989 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001990 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001991 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001992 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001993 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001994 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001995#ifdef WIN32
1996 chanpart->ch_deadline = GetTickCount() + 100L;
1997#else
1998 gettimeofday(&chanpart->ch_deadline, NULL);
1999 chanpart->ch_deadline.tv_usec += 100 * 1000;
2000 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2001 {
2002 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2003 ++chanpart->ch_deadline.tv_sec;
2004 }
2005#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002006 }
2007 else
2008 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002009 int timeout;
2010#ifdef WIN32
2011 timeout = GetTickCount() > chanpart->ch_deadline;
2012#else
2013 {
2014 struct timeval now_tv;
2015
2016 gettimeofday(&now_tv, NULL);
2017 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2018 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2019 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2020 }
2021#endif
2022 if (timeout)
2023 {
2024 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002025 chanpart->ch_wait_len = 0;
2026 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002027 }
2028 else
2029 {
2030 reader.js_used = 0;
2031 ch_log(channel, "still waiting on incomplete message");
2032 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002033 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002034 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002035
2036 if (status == FAIL)
2037 {
2038 ch_error(channel, "Decoding failed - discarding input");
2039 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002040 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002041 }
2042 else if (reader.js_buf[reader.js_used] != NUL)
2043 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002044 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002045 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002046 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2047 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002048 ret = status == MAYBE ? FALSE: TRUE;
2049 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002050 else
2051 ret = FALSE;
2052
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002053 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002054 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002055}
2056
2057/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002058 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002059 */
2060 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002061remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002062{
Bram Moolenaar77073442016-02-13 23:23:53 +01002063 if (node->cq_prev == NULL)
2064 head->cq_next = node->cq_next;
2065 else
2066 node->cq_prev->cq_next = node->cq_next;
2067 if (node->cq_next == NULL)
2068 head->cq_prev = node->cq_prev;
2069 else
2070 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002071}
2072
2073/*
2074 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002075 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002076 */
2077 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002078remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002079{
Bram Moolenaar77073442016-02-13 23:23:53 +01002080 if (node->jq_prev == NULL)
2081 head->jq_next = node->jq_next;
2082 else
2083 node->jq_prev->jq_next = node->jq_next;
2084 if (node->jq_next == NULL)
2085 head->jq_prev = node->jq_prev;
2086 else
2087 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002088 vim_free(node);
2089}
2090
2091/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002092 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002093 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002094 * When "id" is zero or negative jut get the first message. But not the one
2095 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002096 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002097 * Return OK when found and return the value in "rettv".
2098 * Return FAIL otherwise.
2099 */
2100 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002101channel_get_json(
2102 channel_T *channel,
2103 ch_part_T part,
2104 int id,
2105 int without_callback,
2106 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002107{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002108 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002109 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002110
Bram Moolenaar77073442016-02-13 23:23:53 +01002111 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002112 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002113 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002114 typval_T *tv = &l->lv_first->li_tv;
2115
Bram Moolenaar958dc692016-12-01 15:34:12 +01002116 if ((without_callback || !item->jq_no_callback)
2117 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002118 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002119 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002120 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002121 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002122 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002123 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002124 ch_log(channel, "Getting JSON message %ld",
2125 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002126 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002127 return OK;
2128 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002129 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002130 }
2131 return FAIL;
2132}
2133
Bram Moolenaar958dc692016-12-01 15:34:12 +01002134/*
2135 * Put back "rettv" into the JSON queue, there was no callback for it.
2136 * Takes over the values in "rettv".
2137 */
2138 static void
2139channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2140{
2141 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2142 jsonq_T *item = head->jq_next;
2143 jsonq_T *newitem;
2144
2145 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2146 /* last item was pushed back, append to the end */
2147 item = NULL;
2148 else while (item != NULL && item->jq_no_callback)
2149 /* append after the last item that was pushed back */
2150 item = item->jq_next;
2151
2152 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2153 if (newitem == NULL)
2154 clear_tv(rettv);
2155 else
2156 {
2157 newitem->jq_value = alloc_tv();
2158 if (newitem->jq_value == NULL)
2159 {
2160 vim_free(newitem);
2161 clear_tv(rettv);
2162 }
2163 else
2164 {
2165 newitem->jq_no_callback = FALSE;
2166 *newitem->jq_value = *rettv;
2167 if (item == NULL)
2168 {
2169 /* append to the end */
2170 newitem->jq_prev = head->jq_prev;
2171 head->jq_prev = newitem;
2172 newitem->jq_next = NULL;
2173 if (newitem->jq_prev == NULL)
2174 head->jq_next = newitem;
2175 else
2176 newitem->jq_prev->jq_next = newitem;
2177 }
2178 else
2179 {
2180 /* append after "item" */
2181 newitem->jq_prev = item;
2182 newitem->jq_next = item->jq_next;
2183 item->jq_next = newitem;
2184 if (newitem->jq_next == NULL)
2185 head->jq_prev = newitem;
2186 else
2187 newitem->jq_next->jq_prev = newitem;
2188 }
2189 }
2190 }
2191}
2192
Bram Moolenaarece61b02016-02-20 21:39:05 +01002193#define CH_JSON_MAX_ARGS 4
2194
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002195/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002196 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002197 * "argv[0]" is the command string.
2198 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002199 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002200 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002201channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002202{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002203 char_u *cmd = argv[0].vval.v_string;
2204 char_u *arg;
2205 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002206
Bram Moolenaarece61b02016-02-20 21:39:05 +01002207 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002208 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002209 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002210 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002211 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002212 return;
2213 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002214 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002215 if (arg == NULL)
2216 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002217
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002218 if (STRCMP(cmd, "ex") == 0)
2219 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002220 int save_called_emsg = called_emsg;
2221
2222 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002223 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002224 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002225 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002226 --emsg_silent;
2227 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002228 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002229 (char *)get_vim_var_str(VV_ERRMSG));
2230 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002231 }
2232 else if (STRCMP(cmd, "normal") == 0)
2233 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002234 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002235
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002236 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002237 ea.arg = arg;
2238 ea.addr_count = 0;
2239 ea.forceit = TRUE; /* no mapping */
2240 ex_normal(&ea);
2241 }
2242 else if (STRCMP(cmd, "redraw") == 0)
2243 {
2244 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002245
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002246 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002247 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002248 ex_redraw(&ea);
2249 showruler(FALSE);
2250 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002251 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002252 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002253 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002254 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 int is_call = cmd[0] == 'c';
2256 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002257
Bram Moolenaarece61b02016-02-20 21:39:05 +01002258 if (argv[id_idx].v_type != VAR_UNKNOWN
2259 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002260 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002261 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002262 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002263 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 }
2265 else if (is_call && argv[2].v_type != VAR_LIST)
2266 {
2267 ch_error(channel, "third argument for call must be a list");
2268 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002269 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002270 }
2271 else
2272 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002273 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002274 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002275 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002276 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002277
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002278 /* Don't pollute the display with errors. */
2279 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002280 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002281 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002282 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002283 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002284 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002285 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002286 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002287 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002288 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2289 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002290 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002291
2292 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002293 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002294 int id = argv[id_idx].vval.v_number;
2295
Bram Moolenaar55fab432016-02-07 16:53:13 +01002296 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002297 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002298 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002299 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002300 /* If evaluation failed or the result can't be encoded
2301 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002302 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002303 err_tv.v_type = VAR_STRING;
2304 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002305 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002306 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002307 if (json != NULL)
2308 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002309 channel_send(channel,
2310 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002311 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002312 vim_free(json);
2313 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002314 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002315 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002316 if (tv == &res_tv)
2317 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002318 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002319 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002320 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002321 }
2322 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002323 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002324 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002325 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002326 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002327}
2328
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002329/*
2330 * Invoke the callback at "cbhead".
2331 * Does not redraw but sets channel_need_redraw.
2332 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002333 static void
2334invoke_one_time_callback(
2335 channel_T *channel,
2336 cbq_T *cbhead,
2337 cbq_T *item,
2338 typval_T *argv)
2339{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002340 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002341 (char *)item->cq_callback);
2342 /* Remove the item from the list first, if the callback
2343 * invokes ch_close() the list will be cleared. */
2344 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002345 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002346 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002347 vim_free(item);
2348}
2349
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002350 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002351append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002352{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002353 bufref_T save_curbuf = {NULL, 0, 0};
2354 win_T *save_curwin = NULL;
2355 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002356 linenr_T lnum = buffer->b_ml.ml_line_count;
2357 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002358 chanpart_T *ch_part = &channel->ch_part[part];
2359 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002360 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002361
2362 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2363 {
2364 if (!ch_part->ch_nomod_error)
2365 {
2366 ch_error(channel, "Buffer is not modifiable, cannot append");
2367 ch_part->ch_nomod_error = TRUE;
2368 }
2369 return;
2370 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002371
2372 /* If the buffer is also used as input insert above the last
2373 * line. Don't write these lines. */
2374 if (save_write_to)
2375 {
2376 --lnum;
2377 buffer->b_write_to_channel = FALSE;
2378 }
2379
2380 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002381 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002382
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002383 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002384
2385 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2386 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2387
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002388 u_sync(TRUE);
2389 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002390 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002391
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002392 if (empty)
2393 {
2394 /* The buffer is empty, replace the first (dummy) line. */
2395 ml_replace(lnum, msg, TRUE);
2396 lnum = 0;
2397 }
2398 else
2399 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002400 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002401
2402 /* Restore curbuf/curwin/curtab */
2403 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2404
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002405 if (ch_part->ch_nomodifiable)
2406 buffer->b_p_ma = FALSE;
2407 else
2408 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002409
2410 if (buffer->b_nwindows > 0)
2411 {
2412 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002413
2414 FOR_ALL_WINDOWS(wp)
2415 {
2416 if (wp->w_buffer == buffer
2417 && (save_write_to
2418 ? wp->w_cursor.lnum == lnum + 1
2419 : (wp->w_cursor.lnum == lnum
2420 && wp->w_cursor.col == 0)))
2421 {
2422 ++wp->w_cursor.lnum;
2423 save_curwin = curwin;
2424 curwin = wp;
2425 curbuf = curwin->w_buffer;
2426 scroll_cursor_bot(0, FALSE);
2427 curwin = save_curwin;
2428 curbuf = curwin->w_buffer;
2429 }
2430 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002431 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002432 channel_need_redraw = TRUE;
2433 }
2434
2435 if (save_write_to)
2436 {
2437 channel_T *ch;
2438
2439 /* Find channels reading from this buffer and adjust their
2440 * next-to-read line number. */
2441 buffer->b_write_to_channel = TRUE;
2442 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2443 {
2444 chanpart_T *in_part = &ch->ch_part[PART_IN];
2445
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002446 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002447 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2448 }
2449 }
2450}
2451
Bram Moolenaar437905c2016-04-26 19:01:05 +02002452 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002453drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002454{
2455 char_u *msg;
2456
2457 while ((msg = channel_get(channel, part)) != NULL)
2458 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002459 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002460 vim_free(msg);
2461 }
2462}
2463
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002464/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002465 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002466 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002467 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002468 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002469 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002470may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002471{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002472 char_u *msg = NULL;
2473 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002474 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002475 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002476 chanpart_T *ch_part = &channel->ch_part[part];
2477 ch_mode_T ch_mode = ch_part->ch_mode;
2478 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002479 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002480 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002481 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002482 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002483 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002484
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002485 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002486 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002487 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002488
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002489 /* Use a message-specific callback, part callback or channel callback */
2490 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2491 if (cbitem->cq_seq_nr == 0)
2492 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002493 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002494 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002495 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002496 partial = cbitem->cq_partial;
2497 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002498 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002499 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002500 callback = ch_part->ch_callback;
2501 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002502 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002503 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002504 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002505 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002506 partial = channel->ch_partial;
2507 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002508
Bram Moolenaarec68a992016-10-03 21:37:41 +02002509 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002510 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2511 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002512 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002513 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002514 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002515 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002516 buffer = NULL;
2517 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002518
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002519 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002520 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002521 listitem_T *item;
2522 int argc = 0;
2523
Bram Moolenaard7ece102016-02-02 23:23:02 +01002524 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002525 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002526 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002527 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002528 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002529 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002530 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002531 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002532
Bram Moolenaarece61b02016-02-20 21:39:05 +01002533 for (item = listtv->vval.v_list->lv_first;
2534 item != NULL && argc < CH_JSON_MAX_ARGS;
2535 item = item->li_next)
2536 argv[argc++] = item->li_tv;
2537 while (argc < CH_JSON_MAX_ARGS)
2538 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002539
Bram Moolenaarece61b02016-02-20 21:39:05 +01002540 if (argv[0].v_type == VAR_STRING)
2541 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002542 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002543 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002544 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002545 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002546 }
2547
Bram Moolenaarece61b02016-02-20 21:39:05 +01002548 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002549 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002550 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002551 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002552 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002553 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002554 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002555 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002556 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002557 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002558 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002559 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002560 return FALSE;
2561 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002562 else
2563 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002564 /* If there is no callback or buffer drop the message. */
2565 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002566 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002567 /* If there is a close callback it may use ch_read() to get the
2568 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002569 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002570 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002571 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002572 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002573
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002574 if (ch_mode == MODE_NL)
2575 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002576 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002577 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002578 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002579
2580 /* See if we have a message ending in NL in the first buffer. If
2581 * not try to concatenate the first and the second buffer. */
2582 while (TRUE)
2583 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002584 node = channel_peek(channel, part);
2585 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002586 if (nl != NULL)
2587 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002588 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002589 {
2590 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2591 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002592 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002593 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002594 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002595 buf = node->rq_buffer;
2596
Bram Moolenaarec68a992016-10-03 21:37:41 +02002597 if (nl == NULL)
2598 {
2599 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002600 char_u *new_buf;
2601
2602 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2603 if (new_buf == NULL)
2604 /* This might fail over and over again, should the message
2605 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002606 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002607 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002608 node->rq_buffer = buf;
2609 nl = buf + node->rq_buflen++;
2610 *nl = NUL;
2611 }
2612
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002613 /* Convert NUL to NL, the internal representation. */
2614 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2615 if (*p == NUL)
2616 *p = NL;
2617
2618 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002619 {
2620 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002621 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002622 *nl = NUL;
2623 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002624 else
2625 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002626 /* Copy the message into allocated memory (excluding the NL)
2627 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002628 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002629 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002630 }
2631 }
2632 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002633 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002634 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002635 * get everything we have.
2636 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002637 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002638 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002639
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002640 if (msg == NULL)
2641 return FALSE; /* out of memory (and avoids Coverity warning) */
2642
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002643 argv[1].v_type = VAR_STRING;
2644 argv[1].vval.v_string = msg;
2645 }
2646
Bram Moolenaara07fec92016-02-05 21:04:08 +01002647 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002648 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002649 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002650
Bram Moolenaar958dc692016-12-01 15:34:12 +01002651 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002652 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002653 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002654 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002655 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002656 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002657 break;
2658 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002659 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002660 {
2661 if (channel->ch_drop_never)
2662 {
2663 /* message must be read with ch_read() */
2664 channel_push_json(channel, part, listtv);
2665 listtv = NULL;
2666 }
2667 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002668 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002669 seq_nr);
2670 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002671 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002672 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002673 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002674 if (buffer != NULL)
2675 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002676 if (msg == NULL)
2677 /* JSON or JS mode: re-encode the message. */
2678 msg = json_encode(listtv, ch_mode);
2679 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002680 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002681#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002682 if (buffer->b_term != NULL)
2683 write_to_term(buffer, msg, channel);
2684 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002685#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002686 append_to_buffer(buffer, msg, channel, part);
2687 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002688 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002689
Bram Moolenaar187db502016-02-27 14:44:26 +01002690 if (callback != NULL)
2691 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002692 if (cbitem != NULL)
2693 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2694 else
2695 {
2696 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002697 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002698 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002699 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002700 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002701 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002702 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002703 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002704 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002705
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002706 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002707 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002708 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002709
2710 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002711}
2712
2713/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002714 * Return TRUE when channel "channel" is open for writing to.
2715 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002716 */
2717 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002718channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002719{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002720 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002721 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002722}
2723
2724/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002725 * Return TRUE when channel "channel" is open for reading or writing.
2726 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002727 */
2728 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002729channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002730{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002731 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002732 || channel->CH_IN_FD != INVALID_FD
2733 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002734 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002735}
2736
2737/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002738 * Return TRUE if "channel" has JSON or other typeahead.
2739 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002740 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002741channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002742{
2743 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2744
2745 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2746 {
2747 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2748 jsonq_T *item = head->jq_next;
2749
2750 return item != NULL;
2751 }
2752 return channel_peek(channel, part) != NULL;
2753}
2754
2755/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002756 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002757 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002758 */
2759 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002760channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002761{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002762 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002763 int has_readahead = FALSE;
2764
Bram Moolenaar77073442016-02-13 23:23:53 +01002765 if (channel == NULL)
2766 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002767 if (req_part == PART_OUT)
2768 {
2769 if (channel->CH_OUT_FD != INVALID_FD)
2770 return "open";
2771 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002772 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002773 }
2774 else if (req_part == PART_ERR)
2775 {
2776 if (channel->CH_ERR_FD != INVALID_FD)
2777 return "open";
2778 if (channel_has_readahead(channel, PART_ERR))
2779 has_readahead = TRUE;
2780 }
2781 else
2782 {
2783 if (channel_is_open(channel))
2784 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002785 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002786 if (channel_has_readahead(channel, part))
2787 {
2788 has_readahead = TRUE;
2789 break;
2790 }
2791 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002792
2793 if (has_readahead)
2794 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002795 return "closed";
2796}
2797
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002798 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002799channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002800{
2801 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002802 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002803 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002804 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002805 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002806
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002807 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002808 STRCAT(namebuf, "_");
2809 tail = STRLEN(namebuf);
2810
2811 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002812 if (chanpart->ch_fd != INVALID_FD)
2813 status = "open";
2814 else if (channel_has_readahead(channel, part))
2815 status = "buffered";
2816 else
2817 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002818 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002819
2820 STRCPY(namebuf + tail, "mode");
2821 switch (chanpart->ch_mode)
2822 {
2823 case MODE_NL: s = "NL"; break;
2824 case MODE_RAW: s = "RAW"; break;
2825 case MODE_JSON: s = "JSON"; break;
2826 case MODE_JS: s = "JS"; break;
2827 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002828 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002829
2830 STRCPY(namebuf + tail, "io");
2831 if (part == PART_SOCK)
2832 s = "socket";
2833 else switch (chanpart->ch_io)
2834 {
2835 case JIO_NULL: s = "null"; break;
2836 case JIO_PIPE: s = "pipe"; break;
2837 case JIO_FILE: s = "file"; break;
2838 case JIO_BUFFER: s = "buffer"; break;
2839 case JIO_OUT: s = "out"; break;
2840 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002841 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002842
2843 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002844 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002845}
2846
2847 void
2848channel_info(channel_T *channel, dict_T *dict)
2849{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002850 dict_add_number(dict, "id", channel->ch_id);
2851 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002852
2853 if (channel->ch_hostname != NULL)
2854 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002855 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2856 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002857 channel_part_info(channel, dict, "sock", PART_SOCK);
2858 }
2859 else
2860 {
2861 channel_part_info(channel, dict, "out", PART_OUT);
2862 channel_part_info(channel, dict, "err", PART_ERR);
2863 channel_part_info(channel, dict, "in", PART_IN);
2864 }
2865}
2866
Bram Moolenaar77073442016-02-13 23:23:53 +01002867/*
2868 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002869 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002870 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002871 */
2872 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002873channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002874{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002875 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002876
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002877#ifdef FEAT_GUI
2878 channel_gui_unregister(channel);
2879#endif
2880
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002881 ch_close_part(channel, PART_SOCK);
2882 ch_close_part(channel, PART_IN);
2883 ch_close_part(channel, PART_OUT);
2884 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002885
Bram Moolenaara9f02812017-08-05 17:40:38 +02002886 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002887 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002888 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002889
Bram Moolenaara9f02812017-08-05 17:40:38 +02002890 /* Invoke callbacks and flush buffers before the close callback. */
2891 if (channel->ch_close_cb != NULL)
2892 ch_log(channel,
2893 "Invoking callbacks and flushing buffers before closing");
2894 for (part = PART_SOCK; part < PART_IN; ++part)
2895 {
2896 if (channel->ch_close_cb != NULL
2897 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2898 {
2899 /* Increment the refcount to avoid the channel being freed
2900 * halfway. */
2901 ++channel->ch_refcount;
2902 if (channel->ch_close_cb == NULL)
2903 ch_log(channel, "flushing %s buffers before closing",
2904 part_names[part]);
2905 while (may_invoke_callback(channel, part))
2906 ;
2907 --channel->ch_refcount;
2908 }
2909 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002910
Bram Moolenaara9f02812017-08-05 17:40:38 +02002911 if (channel->ch_close_cb != NULL)
2912 {
2913 typval_T argv[1];
2914 typval_T rettv;
2915 int dummy;
2916
2917 /* Increment the refcount to avoid the channel being freed
2918 * halfway. */
2919 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002920 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002921 (char *)channel->ch_close_cb);
2922 argv[0].v_type = VAR_CHANNEL;
2923 argv[0].vval.v_channel = channel;
2924 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002925 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002926 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002927 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002928 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002929
Bram Moolenaara9f02812017-08-05 17:40:38 +02002930 /* the callback is only called once */
2931 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2932 channel->ch_close_cb = NULL;
2933 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002934
Bram Moolenaara9f02812017-08-05 17:40:38 +02002935 if (channel_need_redraw)
2936 {
2937 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002938 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002939 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002940
Bram Moolenaara9f02812017-08-05 17:40:38 +02002941 if (!channel->ch_drop_never)
2942 /* any remaining messages are useless now */
2943 for (part = PART_SOCK; part < PART_IN; ++part)
2944 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002945
2946 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002947 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002948 }
2949
2950 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002951
2952#ifdef FEAT_TERMINAL
2953 term_channel_closed(channel);
2954#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002955}
2956
Bram Moolenaard04a0202016-01-26 23:30:18 +01002957/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002958 * Close the "in" part channel "channel".
2959 */
2960 void
2961channel_close_in(channel_T *channel)
2962{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002963 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002964}
2965
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002966 static void
2967remove_from_writeque(writeq_T *wq, writeq_T *entry)
2968{
2969 ga_clear(&entry->wq_ga);
2970 wq->wq_next = entry->wq_next;
2971 if (wq->wq_next == NULL)
2972 wq->wq_prev = NULL;
2973 else
2974 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002975 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002976}
2977
Bram Moolenaar0874a832016-09-01 15:11:51 +02002978/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002979 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002980 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002981 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002982channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002983{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002984 chanpart_T *ch_part = &channel->ch_part[part];
2985 jsonq_T *json_head = &ch_part->ch_json_head;
2986 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002987
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002988 while (channel_peek(channel, part) != NULL)
2989 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002990
2991 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002992 {
2993 cbq_T *node = cb_head->cq_next;
2994
2995 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002996 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002997 vim_free(node);
2998 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002999
3000 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003001 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003002 free_tv(json_head->jq_next->jq_value);
3003 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003004 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003005
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003006 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3007 ch_part->ch_callback = NULL;
3008 ch_part->ch_partial = NULL;
3009
3010 while (ch_part->ch_writeque.wq_next != NULL)
3011 remove_from_writeque(&ch_part->ch_writeque,
3012 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003013}
3014
3015/*
3016 * Clear all the read buffers on "channel".
3017 */
3018 void
3019channel_clear(channel_T *channel)
3020{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003021 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003022 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003023 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003024 channel_clear_one(channel, PART_OUT);
3025 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003026 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003027 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003028 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003029 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003030 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003031 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003032 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003033}
3034
Bram Moolenaar77073442016-02-13 23:23:53 +01003035#if defined(EXITFREE) || defined(PROTO)
3036 void
3037channel_free_all(void)
3038{
3039 channel_T *channel;
3040
Bram Moolenaard6051b52016-02-28 15:49:03 +01003041 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003042 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3043 channel_clear(channel);
3044}
3045#endif
3046
3047
Bram Moolenaar715d2852016-04-30 17:06:31 +02003048/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003049#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003050
3051/* Buffer size for reading incoming messages. */
3052#define MAXMSGSIZE 4096
3053
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003054#if defined(HAVE_SELECT)
3055/*
3056 * Add write fds where we are waiting for writing to be possible.
3057 */
3058 static int
3059channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3060{
3061 int maxfd = maxfd_arg;
3062 channel_T *ch;
3063
3064 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3065 {
3066 chanpart_T *in_part = &ch->ch_part[PART_IN];
3067
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003068 if (in_part->ch_fd != INVALID_FD
3069 && (in_part->ch_bufref.br_buf != NULL
3070 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003071 {
3072 FD_SET((int)in_part->ch_fd, wfds);
3073 if ((int)in_part->ch_fd >= maxfd)
3074 maxfd = (int)in_part->ch_fd + 1;
3075 }
3076 }
3077 return maxfd;
3078}
3079#else
3080/*
3081 * Add write fds where we are waiting for writing to be possible.
3082 */
3083 static int
3084channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3085{
3086 int nfd = nfd_in;
3087 channel_T *ch;
3088
3089 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3090 {
3091 chanpart_T *in_part = &ch->ch_part[PART_IN];
3092
Bram Moolenaar683b7962017-08-19 15:51:59 +02003093 if (in_part->ch_fd != INVALID_FD
3094 && (in_part->ch_bufref.br_buf != NULL
3095 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003096 {
3097 in_part->ch_poll_idx = nfd;
3098 fds[nfd].fd = in_part->ch_fd;
3099 fds[nfd].events = POLLOUT;
3100 ++nfd;
3101 }
3102 else
3103 in_part->ch_poll_idx = -1;
3104 }
3105 return nfd;
3106}
3107#endif
3108
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003109typedef enum {
3110 CW_READY,
3111 CW_NOT_READY,
3112 CW_ERROR
3113} channel_wait_result;
3114
Bram Moolenaard04a0202016-01-26 23:30:18 +01003115/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003116 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003117 * Return CW_READY when there is something to read.
3118 * Return CW_NOT_READY when there is nothing to read.
3119 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003120 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003121 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003122channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003123{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003124 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003125 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003126
Bram Moolenaard8070362016-02-15 21:56:54 +01003127# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003128 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003129 {
3130 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003131 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003132 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003133 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003134
3135 /* reading from a pipe, not a socket */
3136 while (TRUE)
3137 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003138 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3139
3140 if (r && nread > 0)
3141 return CW_READY;
3142 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003143 {
3144 DWORD err = GetLastError();
3145
3146 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3147 return CW_ERROR;
3148
3149 if (channel->ch_named_pipe)
3150 {
3151 DisconnectNamedPipe((HANDLE)fd);
3152 ConnectNamedPipe((HANDLE)fd, NULL);
3153 }
3154 else
3155 return CW_ERROR;
3156 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003157
3158 /* perhaps write some buffer lines */
3159 channel_write_any_lines();
3160
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003161 sleep_time = deadline - GetTickCount();
3162 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003163 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003164 /* Wait for a little while. Very short at first, up to 10 msec
3165 * after looping a few times. */
3166 if (sleep_time > delay)
3167 sleep_time = delay;
3168 Sleep(sleep_time);
3169 delay = delay * 2;
3170 if (delay > 10)
3171 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003172 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003173 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003174 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003175#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003176 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003177#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003178 struct timeval tval;
3179 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003180 fd_set wfds;
3181 int ret;
3182 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003183
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003184 tval.tv_sec = timeout / 1000;
3185 tval.tv_usec = (timeout % 1000) * 1000;
3186 for (;;)
3187 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003188 FD_ZERO(&rfds);
3189 FD_SET((int)fd, &rfds);
3190
3191 /* Write lines to a pipe when a pipe can be written to. Need to
3192 * set this every time, some buffers may be done. */
3193 maxfd = (int)fd + 1;
3194 FD_ZERO(&wfds);
3195 maxfd = channel_fill_wfds(maxfd, &wfds);
3196
3197 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003198# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003199 SOCK_ERRNO;
3200 if (ret == -1 && errno == EINTR)
3201 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003202# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003203 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003204 {
3205 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003206 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003207 channel_write_any_lines();
3208 continue;
3209 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003210 break;
3211 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003212#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003213 for (;;)
3214 {
3215 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3216 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003217
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003218 fds[0].fd = fd;
3219 fds[0].events = POLLIN;
3220 nfd = channel_fill_poll_write(nfd, fds);
3221 if (poll(fds, nfd, timeout) > 0)
3222 {
3223 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003224 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003225 channel_write_any_lines();
3226 continue;
3227 }
3228 break;
3229 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003230#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003231 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003232 return CW_NOT_READY;
3233}
3234
3235 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003236ch_close_part_on_error(
3237 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003238{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003239 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003240
3241 if (is_err)
3242 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003243 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003244 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003245 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003246
3247 /* Queue a "DETACH" netbeans message in the command queue in order to
3248 * terminate the netbeans session later. Do not end the session here
3249 * directly as we may be running in the context of a call to
3250 * netbeans_parse_messages():
3251 * netbeans_parse_messages
3252 * -> autocmd triggered while processing the netbeans cmd
3253 * -> ui_breakcheck
3254 * -> gui event loop or select loop
3255 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003256 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003257 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003258 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003259 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003260 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3261
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003262 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003263 * close the channel yet, there may be something to read on another part.
3264 * When stdout and stderr use the same FD we get the error only on one of
3265 * them, also close the other. */
3266 if (part == PART_OUT || part == PART_ERR)
3267 {
3268 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3269
3270 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3271 ch_close_part(channel, other);
3272 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003273 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003274
3275#ifdef FEAT_GUI
3276 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003277 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003278#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003279}
3280
3281 static void
3282channel_close_now(channel_T *channel)
3283{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003284 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003285 if (channel->ch_nb_close_cb != NULL)
3286 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003287 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003288}
3289
3290/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003291 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003292 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003293 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003294 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003295 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003296channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003297{
3298 static char_u *buf = NULL;
3299 int len = 0;
3300 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003301 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003302 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003303
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003304 fd = channel->ch_part[part].ch_fd;
3305 if (fd == INVALID_FD)
3306 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003307 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003308 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003309 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003310 }
3311 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003312
3313 /* Allocate a buffer to read into. */
3314 if (buf == NULL)
3315 {
3316 buf = alloc(MAXMSGSIZE);
3317 if (buf == NULL)
3318 return; /* out of memory! */
3319 }
3320
3321 /* Keep on reading for as long as there is something to read.
3322 * Use select() or poll() to avoid blocking on a message that is exactly
3323 * MAXMSGSIZE long. */
3324 for (;;)
3325 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003326 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003327 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003328 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003329 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003330 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003331 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003332 if (len <= 0)
3333 break; /* error or nothing more to read */
3334
3335 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003336 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003337 readlen += len;
3338 if (len < MAXMSGSIZE)
3339 break; /* did read everything that's available */
3340 }
3341
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003342 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003343 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003344 {
3345 if (!channel->ch_keep_open)
3346 ch_close_part_on_error(channel, part, (len < 0), func);
3347 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003348#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003349 else if (CH_HAS_GUI && gtk_main_level() > 0)
3350 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003351 gtk_main_quit();
3352#endif
3353}
3354
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003355/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003356 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003357 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003358 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003359 * Returns what was read in allocated memory.
3360 * Returns NULL in case of error or timeout.
3361 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003362 static char_u *
3363channel_read_block(channel_T *channel, ch_part_T part, int timeout, int raw)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003364{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003365 char_u *buf;
3366 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003367 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003368 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003369 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003370 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003371
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003372 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003373 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003374
3375 while (TRUE)
3376 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003377 node = channel_peek(channel, part);
3378 if (node != NULL)
3379 {
3380 if (mode == MODE_RAW || (mode == MODE_NL
3381 && channel_first_nl(node) != NULL))
3382 /* got a complete message */
3383 break;
3384 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3385 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003386 /* If not blocking or nothing more is coming then return what we
3387 * have. */
3388 if (raw || fd == INVALID_FD)
3389 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003390 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003391
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003392 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003393 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003394 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003395 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003396 {
3397 ch_log(channel, "Timed out");
3398 return NULL;
3399 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003400 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003401 }
3402
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003403 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003404 if (mode == MODE_RAW)
3405 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003406 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003407 }
3408 else
3409 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003410 char_u *p;
3411
3412 buf = node->rq_buffer;
3413 nl = channel_first_nl(node);
3414
3415 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003416 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003417 if (*p == NUL)
3418 *p = NL;
3419
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003420 if (nl == NULL)
3421 {
3422 /* must be a closed channel with missing NL */
3423 msg = channel_get(channel, part);
3424 }
3425 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003426 {
3427 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003428 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003429 *nl = NUL;
3430 }
3431 else
3432 {
3433 /* Copy the message into allocated memory and remove it from the
3434 * buffer. */
3435 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003436 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003437 }
3438 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003439 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003440 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003441 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003442}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003443
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003444/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003445 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003446 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003447 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003448 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003449 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003450 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003451channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003452 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003453 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003454 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003455 int id,
3456 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003457{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003458 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003459 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003460 int timeout;
3461 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003462
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003463 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003464 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003465 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003466 for (;;)
3467 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003468 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003469
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003470 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003471 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003472 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003473 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003474 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003475 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003476
Bram Moolenaard7ece102016-02-02 23:23:02 +01003477 if (!more)
3478 {
3479 /* Handle any other messages in the queue. If done some more
3480 * messages may have arrived. */
3481 if (channel_parse_messages())
3482 continue;
3483
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003484 /* Wait for up to the timeout. If there was an incomplete message
3485 * use the deadline for that. */
3486 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003487 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003488 {
3489#ifdef WIN32
3490 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3491#else
3492 {
3493 struct timeval now_tv;
3494
3495 gettimeofday(&now_tv, NULL);
3496 timeout = (chanpart->ch_deadline.tv_sec
3497 - now_tv.tv_sec) * 1000
3498 + (chanpart->ch_deadline.tv_usec
3499 - now_tv.tv_usec) / 1000
3500 + 1;
3501 }
3502#endif
3503 if (timeout < 0)
3504 {
3505 /* Something went wrong, channel_parse_json() didn't
3506 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003507 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003508 timeout = timeout_arg;
3509 }
3510 else if (timeout > timeout_arg)
3511 timeout = timeout_arg;
3512 }
3513 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003514 if (fd == INVALID_FD
3515 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003516 {
3517 if (timeout == timeout_arg)
3518 {
3519 if (fd != INVALID_FD)
3520 ch_log(channel, "Timed out");
3521 break;
3522 }
3523 }
3524 else
3525 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003526 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003527 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003528 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003529 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003530}
3531
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003532/*
3533 * Common for ch_read() and ch_readraw().
3534 */
3535 void
3536common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3537{
3538 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003539 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003540 jobopt_T opt;
3541 int mode;
3542 int timeout;
3543 int id = -1;
3544 typval_T *listtv = NULL;
3545
3546 /* return an empty string by default */
3547 rettv->v_type = VAR_STRING;
3548 rettv->vval.v_string = NULL;
3549
3550 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003551 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003552 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003553 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003554
Bram Moolenaar437905c2016-04-26 19:01:05 +02003555 if (opt.jo_set & JO_PART)
3556 part = opt.jo_part;
3557 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003558 if (channel != NULL)
3559 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003560 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003561 part = channel_part_read(channel);
3562 mode = channel_get_mode(channel, part);
3563 timeout = channel_get_timeout(channel, part);
3564 if (opt.jo_set & JO_TIMEOUT)
3565 timeout = opt.jo_timeout;
3566
3567 if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003568 rettv->vval.v_string = channel_read_block(channel, part,
3569 timeout, raw);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003570 else
3571 {
3572 if (opt.jo_set & JO_ID)
3573 id = opt.jo_id;
3574 channel_read_json_block(channel, part, timeout, id, &listtv);
3575 if (listtv != NULL)
3576 {
3577 *rettv = *listtv;
3578 vim_free(listtv);
3579 }
3580 else
3581 {
3582 rettv->v_type = VAR_SPECIAL;
3583 rettv->vval.v_number = VVAL_NONE;
3584 }
3585 }
3586 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003587
3588theend:
3589 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003590}
3591
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003592# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3593 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003594/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003595 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003596 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003597 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003598 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003599channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003600{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003601 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003602 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003603
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003604 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003605 for (channel = first_channel; channel != NULL;
3606 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003607 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003608 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003609 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003610 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003611 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003612 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003613 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003614 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003615 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003616}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003617# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003618
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003619# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003620/*
3621 * Check the channels for anything that is ready to be read.
3622 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003623 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003624 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003625 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003626channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003627{
3628 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003629 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003630 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003631
3632 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3633 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003634 if (only_keep_open && !channel->ch_keep_open)
3635 continue;
3636
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003637 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003638 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003639 {
3640 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003641 if (fd != INVALID_FD)
3642 {
3643 int r = channel_wait(channel, fd, 0);
3644
3645 if (r == CW_READY)
3646 channel_read(channel, part, "channel_handle_events");
3647 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003648 ch_close_part_on_error(channel, part, TRUE,
3649 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003650 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003651 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003652 }
3653}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003654# endif
3655
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003656# if defined(FEAT_GUI) || defined(PROTO)
3657/*
3658 * Return TRUE when there is any channel with a keep_open flag.
3659 */
3660 int
3661channel_any_keep_open()
3662{
3663 channel_T *channel;
3664
3665 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3666 if (channel->ch_keep_open)
3667 return TRUE;
3668 return FALSE;
3669}
3670# endif
3671
Bram Moolenaard04a0202016-01-26 23:30:18 +01003672/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003673 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003674 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003675 */
3676 void
3677channel_set_nonblock(channel_T *channel, ch_part_T part)
3678{
3679 chanpart_T *ch_part = &channel->ch_part[part];
3680 int fd = ch_part->ch_fd;
3681
3682 if (fd != INVALID_FD)
3683 {
3684#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003685 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003686
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003687 ioctlsocket(fd, FIONBIO, &val);
3688#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003689 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003690#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003691 ch_part->ch_nonblocking = TRUE;
3692 }
3693}
3694
3695/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003696 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003697 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003698 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003699 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003700 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003701channel_send(
3702 channel_T *channel,
3703 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003704 char_u *buf_arg,
3705 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003706 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003707{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003708 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003709 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003710 chanpart_T *ch_part = &channel->ch_part[part];
3711 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003712
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003713 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003714 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003715 {
3716 if (!channel->ch_error && fun != NULL)
3717 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003718 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003719 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003720 }
3721 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003722 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003723 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003724
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003725 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003726 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003727 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003728 fprintf(log_fd, "'");
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003729 ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003730 fprintf(log_fd, "'\n");
3731 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003732 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003733 }
3734
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003735 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003736 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003737 writeq_T *wq = &ch_part->ch_writeque;
3738 char_u *buf;
3739 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003740
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003741 if (wq->wq_next != NULL)
3742 {
3743 /* first write what was queued */
3744 buf = wq->wq_next->wq_ga.ga_data;
3745 len = wq->wq_next->wq_ga.ga_len;
3746 did_use_queue = TRUE;
3747 }
3748 else
3749 {
3750 if (len_arg == 0)
3751 /* nothing to write, called from channel_select_check() */
3752 return OK;
3753 buf = buf_arg;
3754 len = len_arg;
3755 }
3756
3757 if (part == PART_SOCK)
3758 res = sock_write(fd, (char *)buf, len);
3759 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003760 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003761 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003762#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003763 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003764 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003765 DisconnectNamedPipe((HANDLE)fd);
3766 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003767 }
3768#endif
3769
3770 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003771 if (res < 0 && (errno == EWOULDBLOCK
3772#ifdef EAGAIN
3773 || errno == EAGAIN
3774#endif
3775 ))
3776 res = 0; /* nothing got written */
3777
3778 if (res >= 0 && ch_part->ch_nonblocking)
3779 {
3780 writeq_T *entry = wq->wq_next;
3781
3782 if (did_use_queue)
3783 ch_log(channel, "Sent %d bytes now", res);
3784 if (res == len)
3785 {
3786 /* Wrote all the buf[len] bytes. */
3787 if (entry != NULL)
3788 {
3789 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003790 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003791 continue;
3792 }
3793 if (did_use_queue)
3794 ch_log(channel, "Write queue empty");
3795 }
3796 else
3797 {
3798 /* Wrote only buf[res] bytes, can't write more now. */
3799 if (entry != NULL)
3800 {
3801 if (res > 0)
3802 {
3803 /* Remove the bytes that were written. */
3804 mch_memmove(entry->wq_ga.ga_data,
3805 (char *)entry->wq_ga.ga_data + res,
3806 len - res);
3807 entry->wq_ga.ga_len -= res;
3808 }
3809 buf = buf_arg;
3810 len = len_arg;
3811 }
3812 else
3813 {
3814 buf += res;
3815 len -= res;
3816 }
3817 ch_log(channel, "Adding %d bytes to the write queue", len);
3818
3819 /* Append the not written bytes of the argument to the write
3820 * buffer. Limit entries to 4000 bytes. */
3821 if (wq->wq_prev != NULL
3822 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3823 {
3824 writeq_T *last = wq->wq_prev;
3825
3826 /* append to the last entry */
3827 if (ga_grow(&last->wq_ga, len) == OK)
3828 {
3829 mch_memmove((char *)last->wq_ga.ga_data
3830 + last->wq_ga.ga_len,
3831 buf, len);
3832 last->wq_ga.ga_len += len;
3833 }
3834 }
3835 else
3836 {
3837 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3838
3839 if (last != NULL)
3840 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003841 last->wq_prev = wq->wq_prev;
3842 last->wq_next = NULL;
3843 if (wq->wq_prev == NULL)
3844 wq->wq_next = last;
3845 else
3846 wq->wq_prev->wq_next = last;
3847 wq->wq_prev = last;
3848 ga_init2(&last->wq_ga, 1, 1000);
3849 if (ga_grow(&last->wq_ga, len) == OK)
3850 {
3851 mch_memmove(last->wq_ga.ga_data, buf, len);
3852 last->wq_ga.ga_len = len;
3853 }
3854 }
3855 }
3856 }
3857 }
3858 else if (res != len)
3859 {
3860 if (!channel->ch_error && fun != NULL)
3861 {
3862 ch_error(channel, "%s(): write failed", fun);
3863 EMSG2(_("E631: %s(): write failed"), fun);
3864 }
3865 channel->ch_error = TRUE;
3866 return FAIL;
3867 }
3868
3869 channel->ch_error = FALSE;
3870 return OK;
3871 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003872}
3873
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003874/*
3875 * Common for "ch_sendexpr()" and "ch_sendraw()".
3876 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003877 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003878 * Otherwise returns NULL.
3879 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003880 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003881send_common(
3882 typval_T *argvars,
3883 char_u *text,
3884 int id,
3885 int eval,
3886 jobopt_T *opt,
3887 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003888 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003889{
3890 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003891 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003892
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003893 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003894 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003895 if (channel == NULL)
3896 return NULL;
3897 part_send = channel_part_send(channel);
3898 *part_read = channel_part_read(channel);
3899
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003900 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003901 return NULL;
3902
3903 /* Set the callback. An empty callback means no callback and not reading
3904 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3905 * allowed. */
3906 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3907 {
3908 if (eval)
3909 {
3910 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3911 return NULL;
3912 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003913 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003914 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003915 }
3916
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003917 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003918 && opt->jo_callback == NULL)
3919 return channel;
3920 return NULL;
3921}
3922
3923/*
3924 * common for "ch_evalexpr()" and "ch_sendexpr()"
3925 */
3926 void
3927ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3928{
3929 char_u *text;
3930 typval_T *listtv;
3931 channel_T *channel;
3932 int id;
3933 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003934 ch_part_T part_send;
3935 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003936 jobopt_T opt;
3937 int timeout;
3938
3939 /* return an empty string by default */
3940 rettv->v_type = VAR_STRING;
3941 rettv->vval.v_string = NULL;
3942
Bram Moolenaar437905c2016-04-26 19:01:05 +02003943 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003944 if (channel == NULL)
3945 return;
3946 part_send = channel_part_send(channel);
3947
3948 ch_mode = channel_get_mode(channel, part_send);
3949 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3950 {
3951 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3952 return;
3953 }
3954
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003955 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003956 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003957 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003958 if (text == NULL)
3959 return;
3960
3961 channel = send_common(argvars, text, id, eval, &opt,
3962 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3963 vim_free(text);
3964 if (channel != NULL && eval)
3965 {
3966 if (opt.jo_set & JO_TIMEOUT)
3967 timeout = opt.jo_timeout;
3968 else
3969 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003970 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3971 == OK)
3972 {
3973 list_T *list = listtv->vval.v_list;
3974
3975 /* Move the item from the list and then change the type to
3976 * avoid the value being freed. */
3977 *rettv = list->lv_last->li_tv;
3978 list->lv_last->li_tv.v_type = VAR_NUMBER;
3979 free_tv(listtv);
3980 }
3981 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003982 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003983}
3984
3985/*
3986 * common for "ch_evalraw()" and "ch_sendraw()"
3987 */
3988 void
3989ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3990{
3991 char_u buf[NUMBUFLEN];
3992 char_u *text;
3993 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003994 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003995 jobopt_T opt;
3996 int timeout;
3997
3998 /* return an empty string by default */
3999 rettv->v_type = VAR_STRING;
4000 rettv->vval.v_string = NULL;
4001
4002 text = get_tv_string_buf(&argvars[1], buf);
4003 channel = send_common(argvars, text, 0, eval, &opt,
4004 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4005 if (channel != NULL && eval)
4006 {
4007 if (opt.jo_set & JO_TIMEOUT)
4008 timeout = opt.jo_timeout;
4009 else
4010 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004011 rettv->vval.v_string = channel_read_block(channel, part_read,
4012 timeout, TRUE);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004013 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004014 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004015}
4016
Bram Moolenaarf3360612017-10-01 16:21:31 +02004017# define KEEP_OPEN_TIME 20 /* msec */
4018
Bram Moolenaard04a0202016-01-26 23:30:18 +01004019# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004020/*
4021 * Add open channels to the poll struct.
4022 * Return the adjusted struct index.
4023 * The type of "fds" is hidden to avoid problems with the function proto.
4024 */
4025 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004026channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004027{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004028 int nfd = nfd_in;
4029 channel_T *channel;
4030 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004031 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004032
Bram Moolenaar77073442016-02-13 23:23:53 +01004033 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004034 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004035 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004036 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004037 chanpart_T *ch_part = &channel->ch_part[part];
4038
4039 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004040 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004041 if (channel->ch_keep_open)
4042 {
4043 /* For unknown reason poll() returns immediately for a
4044 * keep-open channel. Instead of adding it to the fds add
4045 * a short timeout and check, like polling. */
4046 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4047 *towait = KEEP_OPEN_TIME;
4048 }
4049 else
4050 {
4051 ch_part->ch_poll_idx = nfd;
4052 fds[nfd].fd = ch_part->ch_fd;
4053 fds[nfd].events = POLLIN;
4054 nfd++;
4055 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004056 }
4057 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004058 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004059 }
4060 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004061
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004062 nfd = channel_fill_poll_write(nfd, fds);
4063
Bram Moolenaare0874f82016-01-24 20:36:41 +01004064 return nfd;
4065}
4066
4067/*
4068 * The type of "fds" is hidden to avoid problems with the function proto.
4069 */
4070 int
4071channel_poll_check(int ret_in, void *fds_in)
4072{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004073 int ret = ret_in;
4074 channel_T *channel;
4075 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004076 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004077 int idx;
4078 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004079
Bram Moolenaar77073442016-02-13 23:23:53 +01004080 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004081 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004082 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004083 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004084 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004085
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004086 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004087 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004088 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004089 --ret;
4090 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004091 else if (channel->ch_part[part].ch_fd != INVALID_FD
4092 && channel->ch_keep_open)
4093 {
4094 /* polling a keep-open channel */
4095 channel_read(channel, part, "channel_poll_check_keep_open");
4096 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004097 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004098
4099 in_part = &channel->ch_part[PART_IN];
4100 idx = in_part->ch_poll_idx;
4101 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4102 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004103 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004104 --ret;
4105 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004106 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004107
4108 return ret;
4109}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004110# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004111
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004112# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004113
Bram Moolenaare0874f82016-01-24 20:36:41 +01004114/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004115 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004116 */
4117 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004118channel_select_setup(
4119 int maxfd_in,
4120 void *rfds_in,
4121 void *wfds_in,
4122 struct timeval *tv,
4123 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004124{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004125 int maxfd = maxfd_in;
4126 channel_T *channel;
4127 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004128 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004129 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004130
Bram Moolenaar77073442016-02-13 23:23:53 +01004131 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004132 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004133 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004134 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004135 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004136
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004137 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004138 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004139 if (channel->ch_keep_open)
4140 {
4141 /* For unknown reason select() returns immediately for a
4142 * keep-open channel. Instead of adding it to the rfds add
4143 * a short timeout and check, like polling. */
4144 if (*tvp == NULL || tv->tv_sec > 0
4145 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4146 {
4147 *tvp = tv;
4148 tv->tv_sec = 0;
4149 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4150 }
4151 }
4152 else
4153 {
4154 FD_SET((int)fd, rfds);
4155 if (maxfd < (int)fd)
4156 maxfd = (int)fd;
4157 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004158 }
4159 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004160 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004161
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004162 maxfd = channel_fill_wfds(maxfd, wfds);
4163
Bram Moolenaare0874f82016-01-24 20:36:41 +01004164 return maxfd;
4165}
4166
4167/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004168 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004169 */
4170 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004171channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004172{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004173 int ret = ret_in;
4174 channel_T *channel;
4175 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004176 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004177 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004178 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004179
Bram Moolenaar77073442016-02-13 23:23:53 +01004180 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004181 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004182 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004183 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004184 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004185
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004186 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004187 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004188 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004189 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004190 --ret;
4191 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004192 else if (fd != INVALID_FD && channel->ch_keep_open)
4193 {
4194 /* polling a keep-open channel */
4195 channel_read(channel, part, "channel_select_check_keep_open");
4196 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004197 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004198
4199 in_part = &channel->ch_part[PART_IN];
4200 if (ret > 0 && in_part->ch_fd != INVALID_FD
4201 && FD_ISSET(in_part->ch_fd, wfds))
4202 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004203 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004204 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004205 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004206 --ret;
4207 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004208 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004209
4210 return ret;
4211}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004212# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004213
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004214/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004215 * Execute queued up commands.
4216 * Invoked from the main loop when it's safe to execute received commands.
4217 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004218 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004219 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004220channel_parse_messages(void)
4221{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004222 channel_T *channel = first_channel;
4223 int ret = FALSE;
4224 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004225 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004226#ifdef ELAPSED_FUNC
4227 ELAPSED_TYPE start_tv;
4228
4229 ELAPSED_INIT(start_tv);
4230#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004231
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004232 ++safe_to_invoke_callback;
4233
Bram Moolenaard0b65022016-03-06 21:50:33 +01004234 /* Only do this message when another message was given, otherwise we get
4235 * lots of them. */
4236 if (did_log_msg)
4237 {
4238 ch_log(NULL, "looking for messages on channels");
4239 did_log_msg = FALSE;
4240 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004241 while (channel != NULL)
4242 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004243 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004244 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004245 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004246 channel_close_now(channel);
4247 /* channel may have been freed, start over */
4248 channel = first_channel;
4249 continue;
4250 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004251 if (channel->ch_to_be_freed)
4252 {
4253 channel_free(channel);
4254 /* channel has been freed, start over */
4255 channel = first_channel;
4256 continue;
4257 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004258 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004259 {
4260 /* channel is no longer useful, free it */
4261 channel_free(channel);
4262 channel = first_channel;
4263 part = PART_SOCK;
4264 continue;
4265 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004266 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004267 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004268 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004269 /* Increase the refcount, in case the handler causes the channel
4270 * to be unreferenced or closed. */
4271 ++channel->ch_refcount;
4272 r = may_invoke_callback(channel, part);
4273 if (r == OK)
4274 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004275 if (channel_unref(channel) || (r == OK
4276#ifdef ELAPSED_FUNC
4277 /* Limit the time we loop here to 100 msec, otherwise
4278 * Vim becomes unresponsive when the callback takes
4279 * more than a bit of time. */
4280 && ELAPSED_FUNC(start_tv) < 100L
4281#endif
4282 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004283 {
4284 /* channel was freed or something was done, start over */
4285 channel = first_channel;
4286 part = PART_SOCK;
4287 continue;
4288 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004289 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004290 if (part < PART_ERR)
4291 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004292 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004293 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004294 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004295 part = PART_SOCK;
4296 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004297 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004298
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004299 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004300 {
4301 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004302 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004303 }
4304
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004305 --safe_to_invoke_callback;
4306
Bram Moolenaard7ece102016-02-02 23:23:02 +01004307 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004308}
4309
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004310/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004311 * Return TRUE if any channel has readahead. That means we should not block on
4312 * waiting for input.
4313 */
4314 int
4315channel_any_readahead(void)
4316{
4317 channel_T *channel = first_channel;
4318 ch_part_T part = PART_SOCK;
4319
4320 while (channel != NULL)
4321 {
4322 if (channel_has_readahead(channel, part))
4323 return TRUE;
4324 if (part < PART_ERR)
4325 ++part;
4326 else
4327 {
4328 channel = channel->ch_next;
4329 part = PART_SOCK;
4330 }
4331 }
4332 return FALSE;
4333}
4334
4335/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004336 * Mark references to lists used in channels.
4337 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004338 int
4339set_ref_in_channel(int copyID)
4340{
Bram Moolenaar77073442016-02-13 23:23:53 +01004341 int abort = FALSE;
4342 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004343 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004344
Bram Moolenaar77073442016-02-13 23:23:53 +01004345 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004346 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004347 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004348 tv.v_type = VAR_CHANNEL;
4349 tv.vval.v_channel = channel;
4350 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004351 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004352 return abort;
4353}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004354
4355/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004356 * Return the "part" to write to for "channel".
4357 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004358 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004359channel_part_send(channel_T *channel)
4360{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004361 if (channel->CH_SOCK_FD == INVALID_FD)
4362 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004363 return PART_SOCK;
4364}
4365
4366/*
4367 * Return the default "part" to read from for "channel".
4368 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004369 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004370channel_part_read(channel_T *channel)
4371{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004372 if (channel->CH_SOCK_FD == INVALID_FD)
4373 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004374 return PART_SOCK;
4375}
4376
4377/*
4378 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004379 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004380 */
4381 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004382channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004383{
Bram Moolenaar77073442016-02-13 23:23:53 +01004384 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004385 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004386 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004387}
4388
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004389/*
4390 * Return the timeout of "channel"/"part"
4391 */
4392 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004393channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004394{
4395 return channel->ch_part[part].ch_timeout;
4396}
4397
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004398 static int
4399handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4400{
4401 char_u *val = get_tv_string(item);
4402
4403 opt->jo_set |= jo;
4404 if (STRCMP(val, "nl") == 0)
4405 *modep = MODE_NL;
4406 else if (STRCMP(val, "raw") == 0)
4407 *modep = MODE_RAW;
4408 else if (STRCMP(val, "js") == 0)
4409 *modep = MODE_JS;
4410 else if (STRCMP(val, "json") == 0)
4411 *modep = MODE_JSON;
4412 else
4413 {
4414 EMSG2(_(e_invarg2), val);
4415 return FAIL;
4416 }
4417 return OK;
4418}
4419
4420 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004421handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004422{
4423 char_u *val = get_tv_string(item);
4424
4425 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4426 if (STRCMP(val, "null") == 0)
4427 opt->jo_io[part] = JIO_NULL;
4428 else if (STRCMP(val, "pipe") == 0)
4429 opt->jo_io[part] = JIO_PIPE;
4430 else if (STRCMP(val, "file") == 0)
4431 opt->jo_io[part] = JIO_FILE;
4432 else if (STRCMP(val, "buffer") == 0)
4433 opt->jo_io[part] = JIO_BUFFER;
4434 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4435 opt->jo_io[part] = JIO_OUT;
4436 else
4437 {
4438 EMSG2(_(e_invarg2), val);
4439 return FAIL;
4440 }
4441 return OK;
4442}
4443
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004444/*
4445 * Clear a jobopt_T before using it.
4446 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004447 void
4448clear_job_options(jobopt_T *opt)
4449{
4450 vim_memset(opt, 0, sizeof(jobopt_T));
4451}
4452
4453/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004454 * Free any members of a jobopt_T.
4455 */
4456 void
4457free_job_options(jobopt_T *opt)
4458{
4459 if (opt->jo_partial != NULL)
4460 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004461 else if (opt->jo_callback != NULL)
4462 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004463 if (opt->jo_out_partial != NULL)
4464 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004465 else if (opt->jo_out_cb != NULL)
4466 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004467 if (opt->jo_err_partial != NULL)
4468 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004469 else if (opt->jo_err_cb != NULL)
4470 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004471 if (opt->jo_close_partial != NULL)
4472 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004473 else if (opt->jo_close_cb != NULL)
4474 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004475 if (opt->jo_exit_partial != NULL)
4476 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004477 else if (opt->jo_exit_cb != NULL)
4478 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004479 if (opt->jo_env != NULL)
4480 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004481}
4482
4483/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004484 * Get the PART_ number from the first character of an option name.
4485 */
4486 static int
4487part_from_char(int c)
4488{
4489 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4490}
4491
4492/*
4493 * Get the option entries from the dict in "tv", parse them and put the result
4494 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004495 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004496 * If an option value is invalid return FAIL.
4497 */
4498 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004499get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004500{
4501 typval_T *item;
4502 char_u *val;
4503 dict_T *dict;
4504 int todo;
4505 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004506 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004507
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004508 if (tv->v_type == VAR_UNKNOWN)
4509 return OK;
4510 if (tv->v_type != VAR_DICT)
4511 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004512 EMSG(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004513 return FAIL;
4514 }
4515 dict = tv->vval.v_dict;
4516 if (dict == NULL)
4517 return OK;
4518
4519 todo = (int)dict->dv_hashtab.ht_used;
4520 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4521 if (!HASHITEM_EMPTY(hi))
4522 {
4523 item = &dict_lookup(hi)->di_tv;
4524
4525 if (STRCMP(hi->hi_key, "mode") == 0)
4526 {
4527 if (!(supported & JO_MODE))
4528 break;
4529 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4530 return FAIL;
4531 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004532 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004533 {
4534 if (!(supported & JO_IN_MODE))
4535 break;
4536 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4537 == FAIL)
4538 return FAIL;
4539 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004540 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004541 {
4542 if (!(supported & JO_OUT_MODE))
4543 break;
4544 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4545 == FAIL)
4546 return FAIL;
4547 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004548 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004549 {
4550 if (!(supported & JO_ERR_MODE))
4551 break;
4552 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4553 == FAIL)
4554 return FAIL;
4555 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004556 else if (STRCMP(hi->hi_key, "in_io") == 0
4557 || STRCMP(hi->hi_key, "out_io") == 0
4558 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004559 {
4560 if (!(supported & JO_OUT_IO))
4561 break;
4562 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4563 return FAIL;
4564 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004565 else if (STRCMP(hi->hi_key, "in_name") == 0
4566 || STRCMP(hi->hi_key, "out_name") == 0
4567 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568 {
4569 part = part_from_char(*hi->hi_key);
4570
4571 if (!(supported & JO_OUT_IO))
4572 break;
4573 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4574 opt->jo_io_name[part] =
4575 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4576 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004577 else if (STRCMP(hi->hi_key, "pty") == 0)
4578 {
4579 if (!(supported & JO_MODE))
4580 break;
4581 opt->jo_pty = get_tv_number(item);
4582 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004583 else if (STRCMP(hi->hi_key, "in_buf") == 0
4584 || STRCMP(hi->hi_key, "out_buf") == 0
4585 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004586 {
4587 part = part_from_char(*hi->hi_key);
4588
4589 if (!(supported & JO_OUT_IO))
4590 break;
4591 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4592 opt->jo_io_buf[part] = get_tv_number(item);
4593 if (opt->jo_io_buf[part] <= 0)
4594 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004595 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004596 return FAIL;
4597 }
4598 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4599 {
4600 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4601 return FAIL;
4602 }
4603 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004604 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4605 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4606 {
4607 part = part_from_char(*hi->hi_key);
4608
4609 if (!(supported & JO_OUT_IO))
4610 break;
4611 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4612 opt->jo_modifiable[part] = get_tv_number(item);
4613 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004614 else if (STRCMP(hi->hi_key, "out_msg") == 0
4615 || STRCMP(hi->hi_key, "err_msg") == 0)
4616 {
4617 part = part_from_char(*hi->hi_key);
4618
4619 if (!(supported & JO_OUT_IO))
4620 break;
4621 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4622 opt->jo_message[part] = get_tv_number(item);
4623 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004624 else if (STRCMP(hi->hi_key, "in_top") == 0
4625 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004626 {
4627 linenr_T *lp;
4628
4629 if (!(supported & JO_OUT_IO))
4630 break;
4631 if (hi->hi_key[3] == 't')
4632 {
4633 lp = &opt->jo_in_top;
4634 opt->jo_set |= JO_IN_TOP;
4635 }
4636 else
4637 {
4638 lp = &opt->jo_in_bot;
4639 opt->jo_set |= JO_IN_BOT;
4640 }
4641 *lp = get_tv_number(item);
4642 if (*lp < 0)
4643 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004644 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004645 return FAIL;
4646 }
4647 }
4648 else if (STRCMP(hi->hi_key, "channel") == 0)
4649 {
4650 if (!(supported & JO_OUT_IO))
4651 break;
4652 opt->jo_set |= JO_CHANNEL;
4653 if (item->v_type != VAR_CHANNEL)
4654 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004655 EMSG2(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004656 return FAIL;
4657 }
4658 opt->jo_channel = item->vval.v_channel;
4659 }
4660 else if (STRCMP(hi->hi_key, "callback") == 0)
4661 {
4662 if (!(supported & JO_CALLBACK))
4663 break;
4664 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004665 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004666 if (opt->jo_callback == NULL)
4667 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004668 EMSG2(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004669 return FAIL;
4670 }
4671 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004672 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004673 {
4674 if (!(supported & JO_OUT_CALLBACK))
4675 break;
4676 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004677 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004678 if (opt->jo_out_cb == NULL)
4679 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004680 EMSG2(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004681 return FAIL;
4682 }
4683 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004684 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004685 {
4686 if (!(supported & JO_ERR_CALLBACK))
4687 break;
4688 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004689 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004690 if (opt->jo_err_cb == NULL)
4691 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004692 EMSG2(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004693 return FAIL;
4694 }
4695 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004696 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004697 {
4698 if (!(supported & JO_CLOSE_CALLBACK))
4699 break;
4700 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004701 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004702 if (opt->jo_close_cb == NULL)
4703 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004704 EMSG2(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004705 return FAIL;
4706 }
4707 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004708 else if (STRCMP(hi->hi_key, "drop") == 0)
4709 {
4710 int never = FALSE;
4711 val = get_tv_string(item);
4712
4713 if (STRCMP(val, "never") == 0)
4714 never = TRUE;
4715 else if (STRCMP(val, "auto") != 0)
4716 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004717 EMSG3(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004718 return FAIL;
4719 }
4720 opt->jo_drop_never = never;
4721 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004722 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4723 {
4724 if (!(supported & JO_EXIT_CB))
4725 break;
4726 opt->jo_set |= JO_EXIT_CB;
4727 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4728 if (opt->jo_exit_cb == NULL)
4729 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004730 EMSG2(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004731 return FAIL;
4732 }
4733 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004734#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004735 else if (STRCMP(hi->hi_key, "term_name") == 0)
4736 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004737 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004738 break;
4739 opt->jo_set2 |= JO2_TERM_NAME;
4740 opt->jo_term_name = get_tv_string_chk(item);
4741 if (opt->jo_term_name == NULL)
4742 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004743 EMSG2(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004744 return FAIL;
4745 }
4746 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004747 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4748 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004749 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004750 break;
4751 val = get_tv_string(item);
4752 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4753 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004754 EMSG3(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004755 return FAIL;
4756 }
4757 opt->jo_set2 |= JO2_TERM_FINISH;
4758 opt->jo_term_finish = *val;
4759 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004760 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4761 {
4762 char_u *p;
4763
4764 if (!(supported2 & JO2_TERM_OPENCMD))
4765 break;
4766 opt->jo_set2 |= JO2_TERM_OPENCMD;
4767 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4768 if (p != NULL)
4769 {
4770 /* Must have %d and no other %. */
4771 p = vim_strchr(p, '%');
4772 if (p != NULL && (p[1] != 'd'
4773 || vim_strchr(p + 2, '%') != NULL))
4774 p = NULL;
4775 }
4776 if (p == NULL)
4777 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004778 EMSG2(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004779 return FAIL;
4780 }
4781 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004782 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4783 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004784 char_u *p;
4785
4786 if (!(supported2 & JO2_EOF_CHARS))
4787 break;
4788 opt->jo_set2 |= JO2_EOF_CHARS;
4789 p = opt->jo_eof_chars = get_tv_string_chk(item);
4790 if (p == NULL)
4791 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004792 EMSG2(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004793 return FAIL;
4794 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004795 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004796 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4797 {
4798 if (!(supported2 & JO2_TERM_ROWS))
4799 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004800 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004801 opt->jo_term_rows = get_tv_number(item);
4802 }
4803 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4804 {
4805 if (!(supported2 & JO2_TERM_COLS))
4806 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004807 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004808 opt->jo_term_cols = get_tv_number(item);
4809 }
4810 else if (STRCMP(hi->hi_key, "vertical") == 0)
4811 {
4812 if (!(supported2 & JO2_VERTICAL))
4813 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004814 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004815 opt->jo_vertical = get_tv_number(item);
4816 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004817 else if (STRCMP(hi->hi_key, "curwin") == 0)
4818 {
4819 if (!(supported2 & JO2_CURWIN))
4820 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004821 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaarda43b612017-08-11 22:27:50 +02004822 opt->jo_curwin = get_tv_number(item);
4823 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004824 else if (STRCMP(hi->hi_key, "hidden") == 0)
4825 {
4826 if (!(supported2 & JO2_HIDDEN))
4827 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004828 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004829 opt->jo_hidden = get_tv_number(item);
4830 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004831 else if (STRCMP(hi->hi_key, "norestore") == 0)
4832 {
4833 if (!(supported2 & JO2_NORESTORE))
4834 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004835 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004836 opt->jo_term_norestore = get_tv_number(item);
4837 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004838 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4839 {
4840 if (!(supported2 & JO2_TERM_KILL))
4841 break;
4842 opt->jo_set2 |= JO2_TERM_KILL;
4843 opt->jo_term_kill = get_tv_string_chk(item);
4844 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004845# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4846 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4847 {
4848 int n = 0;
4849 listitem_T *li;
4850 long_u rgb[16];
4851
4852 if (!(supported2 & JO2_ANSI_COLORS))
4853 break;
4854
4855 if (item == NULL || item->v_type != VAR_LIST
4856 || item->vval.v_list == NULL)
4857 {
4858 EMSG2(_(e_invargval), "ansi_colors");
4859 return FAIL;
4860 }
4861
4862 li = item->vval.v_list->lv_first;
4863 for (; li != NULL && n < 16; li = li->li_next, n++)
4864 {
4865 char_u *color_name;
4866 guicolor_T guicolor;
4867
4868 color_name = get_tv_string_chk(&li->li_tv);
4869 if (color_name == NULL)
4870 return FAIL;
4871
4872 guicolor = GUI_GET_COLOR(color_name);
4873 if (guicolor == INVALCOLOR)
4874 return FAIL;
4875
4876 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4877 }
4878
4879 if (n != 16 || li != NULL)
4880 {
4881 EMSG2(_(e_invargval), "ansi_colors");
4882 return FAIL;
4883 }
4884
4885 opt->jo_set2 |= JO2_ANSI_COLORS;
4886 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4887 }
4888# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004889#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004890 else if (STRCMP(hi->hi_key, "env") == 0)
4891 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004892 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004893 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004894 if (item->v_type != VAR_DICT)
4895 {
4896 EMSG2(_(e_invargval), "env");
4897 return FAIL;
4898 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004899 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004900 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004901 if (opt->jo_env != NULL)
4902 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004903 }
4904 else if (STRCMP(hi->hi_key, "cwd") == 0)
4905 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004906 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004907 break;
4908 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4909 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4910 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004911 EMSG2(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004912 return FAIL;
4913 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004914 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004915 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004916 else if (STRCMP(hi->hi_key, "waittime") == 0)
4917 {
4918 if (!(supported & JO_WAITTIME))
4919 break;
4920 opt->jo_set |= JO_WAITTIME;
4921 opt->jo_waittime = get_tv_number(item);
4922 }
4923 else if (STRCMP(hi->hi_key, "timeout") == 0)
4924 {
4925 if (!(supported & JO_TIMEOUT))
4926 break;
4927 opt->jo_set |= JO_TIMEOUT;
4928 opt->jo_timeout = get_tv_number(item);
4929 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004930 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004931 {
4932 if (!(supported & JO_OUT_TIMEOUT))
4933 break;
4934 opt->jo_set |= JO_OUT_TIMEOUT;
4935 opt->jo_out_timeout = get_tv_number(item);
4936 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004937 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004938 {
4939 if (!(supported & JO_ERR_TIMEOUT))
4940 break;
4941 opt->jo_set |= JO_ERR_TIMEOUT;
4942 opt->jo_err_timeout = get_tv_number(item);
4943 }
4944 else if (STRCMP(hi->hi_key, "part") == 0)
4945 {
4946 if (!(supported & JO_PART))
4947 break;
4948 opt->jo_set |= JO_PART;
4949 val = get_tv_string(item);
4950 if (STRCMP(val, "err") == 0)
4951 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004952 else if (STRCMP(val, "out") == 0)
4953 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004954 else
4955 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004956 EMSG3(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004957 return FAIL;
4958 }
4959 }
4960 else if (STRCMP(hi->hi_key, "id") == 0)
4961 {
4962 if (!(supported & JO_ID))
4963 break;
4964 opt->jo_set |= JO_ID;
4965 opt->jo_id = get_tv_number(item);
4966 }
4967 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4968 {
4969 if (!(supported & JO_STOPONEXIT))
4970 break;
4971 opt->jo_set |= JO_STOPONEXIT;
4972 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4973 opt->jo_soe_buf);
4974 if (opt->jo_stoponexit == NULL)
4975 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004976 EMSG2(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004977 return FAIL;
4978 }
4979 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004980 else if (STRCMP(hi->hi_key, "block_write") == 0)
4981 {
4982 if (!(supported & JO_BLOCK_WRITE))
4983 break;
4984 opt->jo_set |= JO_BLOCK_WRITE;
4985 opt->jo_block_write = get_tv_number(item);
4986 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004987 else
4988 break;
4989 --todo;
4990 }
4991 if (todo > 0)
4992 {
4993 EMSG2(_(e_invarg2), hi->hi_key);
4994 return FAIL;
4995 }
4996
4997 return OK;
4998}
4999
5000/*
5001 * Get the channel from the argument.
5002 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005003 * When "check_open" is TRUE check that the channel can be used.
5004 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005005 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005006 */
5007 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005008get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005009{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005010 channel_T *channel = NULL;
5011 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005012
5013 if (tv->v_type == VAR_JOB)
5014 {
5015 if (tv->vval.v_job != NULL)
5016 channel = tv->vval.v_job->jv_channel;
5017 }
5018 else if (tv->v_type == VAR_CHANNEL)
5019 {
5020 channel = tv->vval.v_channel;
5021 }
5022 else
5023 {
5024 EMSG2(_(e_invarg2), get_tv_string(tv));
5025 return NULL;
5026 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005027 if (channel != NULL && reading)
5028 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005029 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005030
Bram Moolenaar437905c2016-04-26 19:01:05 +02005031 if (check_open && (channel == NULL || (!channel_is_open(channel)
5032 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005033 {
5034 EMSG(_("E906: not an open channel"));
5035 return NULL;
5036 }
5037 return channel;
5038}
5039
5040static job_T *first_job = NULL;
5041
5042 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005043job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005044{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005045 int i;
5046
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005047 ch_log(job->jv_channel, "Freeing job");
5048 if (job->jv_channel != NULL)
5049 {
5050 /* The link from the channel to the job doesn't count as a reference,
5051 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005052 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005053 * NULL the reference. We don't set ch_job_killed, unreferencing the
5054 * job doesn't mean it stops running. */
5055 job->jv_channel->ch_job = NULL;
5056 channel_unref(job->jv_channel);
5057 }
5058 mch_clear_job(job);
5059
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005060 vim_free(job->jv_tty_in);
5061 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005062 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005063 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005064 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005065 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005066 for (i = 0; job->jv_argv[i] != NULL; i++)
5067 vim_free(job->jv_argv[i]);
5068 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005069 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005070}
5071
5072 static void
5073job_free_job(job_T *job)
5074{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005075 if (job->jv_next != NULL)
5076 job->jv_next->jv_prev = job->jv_prev;
5077 if (job->jv_prev == NULL)
5078 first_job = job->jv_next;
5079 else
5080 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005081 vim_free(job);
5082}
5083
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005084 static void
5085job_free(job_T *job)
5086{
5087 if (!in_free_unref_items)
5088 {
5089 job_free_contents(job);
5090 job_free_job(job);
5091 }
5092}
5093
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005094#if defined(EXITFREE) || defined(PROTO)
5095 void
5096job_free_all(void)
5097{
5098 while (first_job != NULL)
5099 job_free(first_job);
5100}
5101#endif
5102
5103/*
5104 * Return TRUE if we need to check if the process of "job" has ended.
5105 */
5106 static int
5107job_need_end_check(job_T *job)
5108{
5109 return job->jv_status == JOB_STARTED
5110 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5111}
5112
5113/*
5114 * Return TRUE if the channel of "job" is still useful.
5115 */
5116 static int
5117job_channel_still_useful(job_T *job)
5118{
5119 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5120}
5121
5122/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005123 * Return TRUE if the channel of "job" is closeable.
5124 */
5125 static int
5126job_channel_can_close(job_T *job)
5127{
5128 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5129}
5130
5131/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005132 * Return TRUE if the job should not be freed yet. Do not free the job when
5133 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5134 * or when the associated channel will do something with the job output.
5135 */
5136 static int
5137job_still_useful(job_T *job)
5138{
5139 return job_need_end_check(job) || job_channel_still_useful(job);
5140}
5141
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005142#if defined(GUI_MAY_FORK) || defined(PROTO)
5143/*
5144 * Return TRUE when there is any running job that we care about.
5145 */
5146 int
5147job_any_running()
5148{
5149 job_T *job;
5150
5151 for (job = first_job; job != NULL; job = job->jv_next)
5152 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005153 {
5154 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005155 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005156 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005157 return FALSE;
5158}
5159#endif
5160
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005161#if !defined(USE_ARGV) || defined(PROTO)
5162/*
5163 * Escape one argument for an external command.
5164 * Returns the escaped string in allocated memory. NULL when out of memory.
5165 */
5166 static char_u *
5167win32_escape_arg(char_u *arg)
5168{
5169 int slen, dlen;
5170 int escaping = 0;
5171 int i;
5172 char_u *s, *d;
5173 char_u *escaped_arg;
5174 int has_spaces = FALSE;
5175
5176 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005177 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005178 dlen = slen;
5179 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5180 {
5181 if (*s == '"' || *s == '\\')
5182 ++dlen;
5183 if (*s == ' ' || *s == '\t')
5184 has_spaces = TRUE;
5185 }
5186
5187 if (has_spaces)
5188 dlen += 2;
5189
5190 if (dlen == slen)
5191 return vim_strsave(arg);
5192
5193 /* Allocate memory for the result and fill it. */
5194 escaped_arg = alloc(dlen + 1);
5195 if (escaped_arg == NULL)
5196 return NULL;
5197 memset(escaped_arg, 0, dlen+1);
5198
5199 d = escaped_arg;
5200
5201 if (has_spaces)
5202 *d++ = '"';
5203
5204 for (s = arg; *s != NUL;)
5205 {
5206 switch (*s)
5207 {
5208 case '"':
5209 for (i = 0; i < escaping; i++)
5210 *d++ = '\\';
5211 escaping = 0;
5212 *d++ = '\\';
5213 *d++ = *s++;
5214 break;
5215 case '\\':
5216 escaping++;
5217 *d++ = *s++;
5218 break;
5219 default:
5220 escaping = 0;
5221 MB_COPY_CHAR(s, d);
5222 break;
5223 }
5224 }
5225
5226 /* add terminating quote and finish with a NUL */
5227 if (has_spaces)
5228 {
5229 for (i = 0; i < escaping; i++)
5230 *d++ = '\\';
5231 *d++ = '"';
5232 }
5233 *d = NUL;
5234
5235 return escaped_arg;
5236}
5237
5238/*
5239 * Build a command line from a list, taking care of escaping.
5240 * The result is put in gap->ga_data.
5241 * Returns FAIL when out of memory.
5242 */
5243 int
5244win32_build_cmd(list_T *l, garray_T *gap)
5245{
5246 listitem_T *li;
5247 char_u *s;
5248
5249 for (li = l->lv_first; li != NULL; li = li->li_next)
5250 {
5251 s = get_tv_string_chk(&li->li_tv);
5252 if (s == NULL)
5253 return FAIL;
5254 s = win32_escape_arg(s);
5255 if (s == NULL)
5256 return FAIL;
5257 ga_concat(gap, s);
5258 vim_free(s);
5259 if (li->li_next != NULL)
5260 ga_append(gap, ' ');
5261 }
5262 return OK;
5263}
5264#endif
5265
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005266/*
5267 * NOTE: Must call job_cleanup() only once right after the status of "job"
5268 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5269 * mch_detect_ended_job() returned non-NULL).
5270 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005271 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005272job_cleanup(job_T *job)
5273{
5274 if (job->jv_status != JOB_ENDED)
5275 return;
5276
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005277 /* Ready to cleanup the job. */
5278 job->jv_status = JOB_FINISHED;
5279
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005280 /* When only channel-in is kept open, close explicitly. */
5281 if (job->jv_channel != NULL)
5282 ch_close_part(job->jv_channel, PART_IN);
5283
Bram Moolenaar97792de2016-10-15 18:36:49 +02005284 if (job->jv_exit_cb != NULL)
5285 {
5286 typval_T argv[3];
5287 typval_T rettv;
5288 int dummy;
5289
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005290 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005291 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005292 ++job->jv_refcount;
5293 argv[0].v_type = VAR_JOB;
5294 argv[0].vval.v_job = job;
5295 argv[1].v_type = VAR_NUMBER;
5296 argv[1].vval.v_number = job->jv_exitval;
5297 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5298 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5299 job->jv_exit_partial, NULL);
5300 clear_tv(&rettv);
5301 --job->jv_refcount;
5302 channel_need_redraw = TRUE;
5303 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005304
5305 /* Do not free the job in case the close callback of the associated channel
5306 * isn't invoked yet and may get information by job_info(). */
5307 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005308 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005309 /* The job was already unreferenced and the associated channel was
5310 * detached, now that it ended it can be freed. Careful: caller must
5311 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005312 job_free(job);
5313 }
5314}
5315
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005316/*
5317 * Mark references in jobs that are still useful.
5318 */
5319 int
5320set_ref_in_job(int copyID)
5321{
5322 int abort = FALSE;
5323 job_T *job;
5324 typval_T tv;
5325
5326 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005327 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005328 {
5329 tv.v_type = VAR_JOB;
5330 tv.vval.v_job = job;
5331 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5332 }
5333 return abort;
5334}
5335
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005336/*
5337 * Dereference "job". Note that after this "job" may have been freed.
5338 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005339 void
5340job_unref(job_T *job)
5341{
5342 if (job != NULL && --job->jv_refcount <= 0)
5343 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005344 /* Do not free the job if there is a channel where the close callback
5345 * may get the job info. */
5346 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005347 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005348 /* Do not free the job when it has not ended yet and there is a
5349 * "stoponexit" flag or an exit callback. */
5350 if (!job_need_end_check(job))
5351 {
5352 job_free(job);
5353 }
5354 else if (job->jv_channel != NULL)
5355 {
5356 /* Do remove the link to the channel, otherwise it hangs
5357 * around until Vim exits. See job_free() for refcount. */
5358 ch_log(job->jv_channel, "detaching channel from job");
5359 job->jv_channel->ch_job = NULL;
5360 channel_unref(job->jv_channel);
5361 job->jv_channel = NULL;
5362 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005363 }
5364 }
5365}
5366
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005367 int
5368free_unused_jobs_contents(int copyID, int mask)
5369{
5370 int did_free = FALSE;
5371 job_T *job;
5372
5373 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005374 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005375 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005376 {
5377 /* Free the channel and ordinary items it contains, but don't
5378 * recurse into Lists, Dictionaries etc. */
5379 job_free_contents(job);
5380 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005381 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005382 return did_free;
5383}
5384
5385 void
5386free_unused_jobs(int copyID, int mask)
5387{
5388 job_T *job;
5389 job_T *job_next;
5390
5391 for (job = first_job; job != NULL; job = job_next)
5392 {
5393 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005394 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005395 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005396 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005397 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005398 job_free_job(job);
5399 }
5400 }
5401}
5402
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005403/*
5404 * Allocate a job. Sets the refcount to one and sets options default.
5405 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005406 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005407job_alloc(void)
5408{
5409 job_T *job;
5410
5411 job = (job_T *)alloc_clear(sizeof(job_T));
5412 if (job != NULL)
5413 {
5414 job->jv_refcount = 1;
5415 job->jv_stoponexit = vim_strsave((char_u *)"term");
5416
5417 if (first_job != NULL)
5418 {
5419 first_job->jv_prev = job;
5420 job->jv_next = first_job;
5421 }
5422 first_job = job;
5423 }
5424 return job;
5425}
5426
5427 void
5428job_set_options(job_T *job, jobopt_T *opt)
5429{
5430 if (opt->jo_set & JO_STOPONEXIT)
5431 {
5432 vim_free(job->jv_stoponexit);
5433 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5434 job->jv_stoponexit = NULL;
5435 else
5436 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5437 }
5438 if (opt->jo_set & JO_EXIT_CB)
5439 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005440 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005441 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005442 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005443 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005444 job->jv_exit_partial = NULL;
5445 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005446 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005447 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005448 job->jv_exit_partial = opt->jo_exit_partial;
5449 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005450 {
5451 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005452 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005453 }
5454 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005455 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005456 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005457 func_ref(job->jv_exit_cb);
5458 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005459 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005460 }
5461}
5462
5463/*
5464 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5465 */
5466 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005467job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005468{
5469 job_T *job;
5470
5471 for (job = first_job; job != NULL; job = job->jv_next)
5472 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005473 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005474}
5475
5476/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005477 * Return TRUE when there is any job that has an exit callback and might exit,
5478 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005479 */
5480 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005481has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005482{
5483 job_T *job;
5484
5485 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005486 /* Only should check if the channel has been closed, if the channel is
5487 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005488 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5489 || (job->jv_status == JOB_FINISHED
5490 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005491 return TRUE;
5492 return FALSE;
5493}
5494
Bram Moolenaar97792de2016-10-15 18:36:49 +02005495#define MAX_CHECK_ENDED 8
5496
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005497/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005498 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005499 */
5500 void
5501job_check_ended(void)
5502{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005503 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005504
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005505 if (first_job == NULL)
5506 return;
5507
Bram Moolenaar97792de2016-10-15 18:36:49 +02005508 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005509 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005510 /* NOTE: mch_detect_ended_job() must only return a job of which the
5511 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005512 job_T *job = mch_detect_ended_job(first_job);
5513
5514 if (job == NULL)
5515 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005516 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005517 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005518
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005519 if (channel_need_redraw)
5520 {
5521 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005522 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005523 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005524}
5525
5526/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005527 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005528 * "argv_arg" is only for Unix.
5529 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005530 * The returned job has a refcount of one.
5531 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005532 */
5533 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005534job_start(
5535 typval_T *argvars,
5536 char **argv_arg,
5537 jobopt_T *opt_arg,
5538 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005539{
5540 job_T *job;
5541 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005542 char **argv = NULL;
5543 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005544#if defined(UNIX)
5545# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005546 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005547#else
5548 garray_T ga;
5549#endif
5550 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005551 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005552
5553 job = job_alloc();
5554 if (job == NULL)
5555 return NULL;
5556
5557 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005558#ifndef USE_ARGV
5559 ga_init2(&ga, (int)sizeof(char*), 20);
5560#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005561
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005562 if (opt_arg != NULL)
5563 opt = *opt_arg;
5564 else
5565 {
5566 /* Default mode is NL. */
5567 clear_job_options(&opt);
5568 opt.jo_mode = MODE_NL;
5569 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005570 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5571 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5572 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005573 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005574 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005575
5576 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005577 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005578 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5579 && opt.jo_io[part] == JIO_FILE
5580 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5581 || *opt.jo_io_name[part] == NUL))
5582 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005583 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005584 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005585 }
5586
5587 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5588 {
5589 buf_T *buf = NULL;
5590
5591 /* check that we can find the buffer before starting the job */
5592 if (opt.jo_set & JO_IN_BUF)
5593 {
5594 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5595 if (buf == NULL)
5596 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5597 }
5598 else if (!(opt.jo_set & JO_IN_NAME))
5599 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005600 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005601 }
5602 else
5603 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5604 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005605 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005606 if (buf->b_ml.ml_mfp == NULL)
5607 {
5608 char_u numbuf[NUMBUFLEN];
5609 char_u *s;
5610
5611 if (opt.jo_set & JO_IN_BUF)
5612 {
5613 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5614 s = numbuf;
5615 }
5616 else
5617 s = opt.jo_io_name[PART_IN];
5618 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005619 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005620 }
5621 job->jv_in_buf = buf;
5622 }
5623
5624 job_set_options(job, &opt);
5625
Bram Moolenaar13568252018-03-16 20:46:58 +01005626#ifdef USE_ARGV
5627 if (argv_arg != NULL)
5628 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005629 /* Make a copy of argv_arg for job->jv_argv. */
5630 for (i = 0; argv_arg[i] != NULL; i++)
5631 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005632 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005633 if (argv == NULL)
5634 goto theend;
5635 for (i = 0; i < argc; i++)
5636 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5637 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005638 }
5639 else
5640#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005641 if (argvars[0].v_type == VAR_STRING)
5642 {
5643 /* Command is a string. */
5644 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005645 if (cmd == NULL || *cmd == NUL)
5646 {
5647 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005648 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005649 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005650
5651 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005652 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005653 }
5654 else if (argvars[0].v_type != VAR_LIST
5655 || argvars[0].vval.v_list == NULL
5656 || argvars[0].vval.v_list->lv_len < 1)
5657 {
5658 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005659 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005660 }
5661 else
5662 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005663 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005664
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005665 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005666 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005667#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005668 if (win32_build_cmd(l, &ga) == FAIL)
5669 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005670 cmd = ga.ga_data;
5671#endif
5672 }
5673
Bram Moolenaar20608922018-04-21 22:30:08 +02005674 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005675 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005676
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005677#ifdef USE_ARGV
5678 if (ch_log_active())
5679 {
5680 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005681
5682 ga_init2(&ga, (int)sizeof(char), 200);
5683 for (i = 0; i < argc; ++i)
5684 {
5685 if (i > 0)
5686 ga_concat(&ga, (char_u *)" ");
5687 ga_concat(&ga, (char_u *)argv[i]);
5688 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005689 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005690 ga_clear(&ga);
5691 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005692 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005693#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005694 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005695 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005696#endif
5697
5698 /* If the channel is reading from a buffer, write lines now. */
5699 if (job->jv_channel != NULL)
5700 channel_write_in(job->jv_channel);
5701
5702theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005703#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005704 vim_free(ga.ga_data);
5705#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005706 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005707 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005708 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005709 return job;
5710}
5711
5712/*
5713 * Get the status of "job" and invoke the exit callback when needed.
5714 * The returned string is not allocated.
5715 */
5716 char *
5717job_status(job_T *job)
5718{
5719 char *result;
5720
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005721 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005722 /* No need to check, dead is dead. */
5723 result = "dead";
5724 else if (job->jv_status == JOB_FAILED)
5725 result = "fail";
5726 else
5727 {
5728 result = mch_job_status(job);
5729 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005730 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005731 }
5732 return result;
5733}
5734
Bram Moolenaar8950a562016-03-12 15:22:55 +01005735/*
5736 * Implementation of job_info().
5737 */
5738 void
5739job_info(job_T *job, dict_T *dict)
5740{
5741 dictitem_T *item;
5742 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005743 list_T *l;
5744 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005745
Bram Moolenaare0be1672018-07-08 16:50:37 +02005746 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005747
5748 item = dictitem_alloc((char_u *)"channel");
5749 if (item == NULL)
5750 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005751 item->di_tv.v_type = VAR_CHANNEL;
5752 item->di_tv.vval.v_channel = job->jv_channel;
5753 if (job->jv_channel != NULL)
5754 ++job->jv_channel->ch_refcount;
5755 if (dict_add(dict, item) == FAIL)
5756 dictitem_free(item);
5757
5758#ifdef UNIX
5759 nr = job->jv_pid;
5760#else
5761 nr = job->jv_proc_info.dwProcessId;
5762#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005763 dict_add_number(dict, "process", nr);
5764 dict_add_string(dict, "tty_in", job->jv_tty_in);
5765 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005766
Bram Moolenaare0be1672018-07-08 16:50:37 +02005767 dict_add_number(dict, "exitval", job->jv_exitval);
5768 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5769 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005770
5771 l = list_alloc();
5772 if (l != NULL)
5773 {
5774 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005775 if (job->jv_argv != NULL)
5776 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005777 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005778 }
5779}
5780
5781/*
5782 * Implementation of job_info() to return info for all jobs.
5783 */
5784 void
5785job_info_all(list_T *l)
5786{
5787 job_T *job;
5788 typval_T tv;
5789
5790 for (job = first_job; job != NULL; job = job->jv_next)
5791 {
5792 tv.v_type = VAR_JOB;
5793 tv.vval.v_job = job;
5794
5795 if (list_append_tv(l, &tv) != OK)
5796 return;
5797 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005798}
5799
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005800/*
5801 * Send a signal to "job". Implements job_stop().
5802 * When "type" is not NULL use this for the type.
5803 * Otherwise use argvars[1] for the type.
5804 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005805 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005806job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005807{
5808 char_u *arg;
5809
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005810 if (type != NULL)
5811 arg = (char_u *)type;
5812 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005813 arg = (char_u *)"";
5814 else
5815 {
5816 arg = get_tv_string_chk(&argvars[1]);
5817 if (arg == NULL)
5818 {
5819 EMSG(_(e_invarg));
5820 return 0;
5821 }
5822 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005823 if (job->jv_status == JOB_FAILED)
5824 {
5825 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5826 return 0;
5827 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005828 if (job->jv_status == JOB_ENDED)
5829 {
5830 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5831 return 0;
5832 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005833 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005834 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005835 return 0;
5836
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005837 /* Assume that only "kill" will kill the job. */
5838 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005839 job->jv_channel->ch_job_killed = TRUE;
5840
5841 /* We don't try freeing the job, obviously the caller still has a
5842 * reference to it. */
5843 return 1;
5844}
5845
Bram Moolenaarf2732452018-06-03 14:47:35 +02005846 void
5847invoke_prompt_callback(void)
5848{
5849 typval_T rettv;
5850 int dummy;
5851 typval_T argv[2];
5852 char_u *text;
5853 char_u *prompt;
5854 linenr_T lnum = curbuf->b_ml.ml_line_count;
5855
5856 // Add a new line for the prompt before invoking the callback, so that
5857 // text can always be inserted above the last line.
5858 ml_append(lnum, (char_u *)"", 0, FALSE);
5859 curwin->w_cursor.lnum = lnum + 1;
5860 curwin->w_cursor.col = 0;
5861
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005862 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005863 return;
5864 text = ml_get(lnum);
5865 prompt = prompt_text();
5866 if (STRLEN(text) >= STRLEN(prompt))
5867 text += STRLEN(prompt);
5868 argv[0].v_type = VAR_STRING;
5869 argv[0].vval.v_string = vim_strsave(text);
5870 argv[1].v_type = VAR_UNKNOWN;
5871
5872 call_func(curbuf->b_prompt_callback,
5873 (int)STRLEN(curbuf->b_prompt_callback),
5874 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5875 curbuf->b_prompt_partial, NULL);
5876 clear_tv(&argv[0]);
5877 clear_tv(&rettv);
5878}
5879
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005880/*
5881 * Return TRUE when the interrupt callback was invoked.
5882 */
5883 int
5884invoke_prompt_interrupt(void)
5885{
5886 typval_T rettv;
5887 int dummy;
5888 typval_T argv[1];
5889
5890 if (curbuf->b_prompt_interrupt == NULL
5891 || *curbuf->b_prompt_interrupt == NUL)
5892 return FALSE;
5893 argv[0].v_type = VAR_UNKNOWN;
5894
5895 got_int = FALSE; // don't skip executing commands
5896 call_func(curbuf->b_prompt_interrupt,
5897 (int)STRLEN(curbuf->b_prompt_interrupt),
5898 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5899 curbuf->b_prompt_int_partial, NULL);
5900 clear_tv(&rettv);
5901 return TRUE;
5902}
5903
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005904#endif /* FEAT_JOB_CHANNEL */