blob: e9615279a6412e2170b77265c7ab6d30a425b94b [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
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100941 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100942 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 Moolenaar8b62d872019-01-05 00:02:57 +01001102 * Prepare buffer "buf" for writing channel output to.
1103 */
1104 static void
1105prepare_buffer(buf_T *buf)
1106{
1107 buf_T *save_curbuf = curbuf;
1108
1109 buf_copy_options(buf, BCO_ENTER);
1110 curbuf = buf;
1111#ifdef FEAT_QUICKFIX
1112 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1113 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1114#endif
1115 if (curbuf->b_ml.ml_mfp == NULL)
1116 ml_open(curbuf);
1117 curbuf = save_curbuf;
1118}
1119
1120/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001121 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001122 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001123 */
1124 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001125find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001126{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001127 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001128 buf_T *save_curbuf = curbuf;
1129
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001130 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001131 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001132 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001133 if (buf == NULL)
1134 buf = buflist_findname_exp(name);
1135 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001136 if (buf == NULL)
1137 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001138 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001139 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001140 if (buf == NULL)
1141 return NULL;
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001142 prepare_buffer(buf);
1143
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001144 curbuf = buf;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001145 if (msg)
1146 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001147 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001148 changed_bytes(1, 0);
1149 curbuf = save_curbuf;
1150 }
1151
1152 return buf;
1153}
1154
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001155 static void
1156set_callback(
1157 char_u **cbp,
1158 partial_T **pp,
1159 char_u *callback,
1160 partial_T *partial)
1161{
1162 free_callback(*cbp, *pp);
1163 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001164 {
1165 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001166 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001167 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001168 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001169 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001170 func_ref(*cbp);
1171 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001172 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001173 else
1174 *cbp = NULL;
1175 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001176 if (partial != NULL)
1177 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001178}
1179
Bram Moolenaar187db502016-02-27 14:44:26 +01001180/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001181 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001182 */
1183 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001184channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001185{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001186 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001187
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001188 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001189 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001190 channel->ch_part[part].ch_mode = opt->jo_mode;
1191 if (opt->jo_set & JO_IN_MODE)
1192 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1193 if (opt->jo_set & JO_OUT_MODE)
1194 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1195 if (opt->jo_set & JO_ERR_MODE)
1196 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001197 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001198
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001199 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001200 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001201 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1202 if (opt->jo_set & JO_OUT_TIMEOUT)
1203 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1204 if (opt->jo_set & JO_ERR_TIMEOUT)
1205 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001206 if (opt->jo_set & JO_BLOCK_WRITE)
1207 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001208
1209 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001210 set_callback(&channel->ch_callback, &channel->ch_partial,
1211 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001212 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001213 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1214 &channel->ch_part[PART_OUT].ch_partial,
1215 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001216 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001217 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1218 &channel->ch_part[PART_ERR].ch_partial,
1219 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001220 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001221 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1222 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001223 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001224
1225 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1226 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001227 buf_T *buf;
1228
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001229 /* writing output to a buffer. Default mode is NL. */
1230 if (!(opt->jo_set & JO_OUT_MODE))
1231 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001232 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001233 {
1234 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1235 if (buf == NULL)
1236 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1237 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001238 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001239 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001240 int msg = TRUE;
1241
1242 if (opt->jo_set2 & JO2_OUT_MSG)
1243 msg = opt->jo_message[PART_OUT];
1244 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001245 }
1246 if (buf != NULL)
1247 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001248 if (opt->jo_set & JO_OUT_MODIFIABLE)
1249 channel->ch_part[PART_OUT].ch_nomodifiable =
1250 !opt->jo_modifiable[PART_OUT];
1251
1252 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1253 {
1254 EMSG(_(e_modifiable));
1255 }
1256 else
1257 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001258 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001259 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001260 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001261 // if the buffer was deleted or unloaded resurrect it
1262 if (buf->b_ml.ml_mfp == NULL)
1263 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001264 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001265 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001266 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001267
1268 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1269 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1270 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1271 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001272 buf_T *buf;
1273
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001274 /* writing err to a buffer. Default mode is NL. */
1275 if (!(opt->jo_set & JO_ERR_MODE))
1276 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1277 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001278 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001279 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001280 {
1281 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1282 if (buf == NULL)
1283 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1284 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001285 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001286 {
1287 int msg = TRUE;
1288
1289 if (opt->jo_set2 & JO2_ERR_MSG)
1290 msg = opt->jo_message[PART_ERR];
1291 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1292 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001293 if (buf != NULL)
1294 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001295 if (opt->jo_set & JO_ERR_MODIFIABLE)
1296 channel->ch_part[PART_ERR].ch_nomodifiable =
1297 !opt->jo_modifiable[PART_ERR];
1298 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1299 {
1300 EMSG(_(e_modifiable));
1301 }
1302 else
1303 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001304 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001305 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001306 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001307 // if the buffer was deleted or unloaded resurrect it
1308 if (buf->b_ml.ml_mfp == NULL)
1309 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001310 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001311 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001312 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001313
1314 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1315 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1316 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001317}
1318
1319/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001320 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001321 */
1322 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001323channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001324 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001325 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001326 char_u *callback,
1327 partial_T *partial,
1328 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001329{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001330 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001331 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1332
1333 if (item != NULL)
1334 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001335 item->cq_partial = partial;
1336 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001337 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001338 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001339 item->cq_callback = callback;
1340 }
1341 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001342 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001343 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001344 func_ref(item->cq_callback);
1345 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001346 item->cq_seq_nr = id;
1347 item->cq_prev = head->cq_prev;
1348 head->cq_prev = item;
1349 item->cq_next = NULL;
1350 if (item->cq_prev == NULL)
1351 head->cq_next = item;
1352 else
1353 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001354 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001355}
1356
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001357 static void
1358write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1359{
1360 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001361 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001362 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001363 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001364
Bram Moolenaar655da312016-05-28 22:22:34 +02001365 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001366 if ((p = alloc(len + 2)) == NULL)
1367 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001368 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001369
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001370 if (channel->ch_write_text_mode)
1371 p[len] = CAR;
1372 else
1373 {
1374 for (i = 0; i < len; ++i)
1375 if (p[i] == NL)
1376 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001377
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001378 p[len] = NL;
1379 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001380 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001381 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001382 vim_free(p);
1383}
1384
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001385/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001386 * Return TRUE if "channel" can be written to.
1387 * Returns FALSE if the input is closed or the write would block.
1388 */
1389 static int
1390can_write_buf_line(channel_T *channel)
1391{
1392 chanpart_T *in_part = &channel->ch_part[PART_IN];
1393
1394 if (in_part->ch_fd == INVALID_FD)
1395 return FALSE; /* pipe was closed */
1396
1397 /* for testing: block every other attempt to write */
1398 if (in_part->ch_block_write == 1)
1399 in_part->ch_block_write = -1;
1400 else if (in_part->ch_block_write == -1)
1401 in_part->ch_block_write = 1;
1402
1403 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1404#ifndef WIN32
1405 {
1406# if defined(HAVE_SELECT)
1407 struct timeval tval;
1408 fd_set wfds;
1409 int ret;
1410
1411 FD_ZERO(&wfds);
1412 FD_SET((int)in_part->ch_fd, &wfds);
1413 tval.tv_sec = 0;
1414 tval.tv_usec = 0;
1415 for (;;)
1416 {
1417 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1418# ifdef EINTR
1419 SOCK_ERRNO;
1420 if (ret == -1 && errno == EINTR)
1421 continue;
1422# endif
1423 if (ret <= 0 || in_part->ch_block_write == 1)
1424 {
1425 if (ret > 0)
1426 ch_log(channel, "FAKED Input not ready for writing");
1427 else
1428 ch_log(channel, "Input not ready for writing");
1429 return FALSE;
1430 }
1431 break;
1432 }
1433# else
1434 struct pollfd fds;
1435
1436 fds.fd = in_part->ch_fd;
1437 fds.events = POLLOUT;
1438 if (poll(&fds, 1, 0) <= 0)
1439 {
1440 ch_log(channel, "Input not ready for writing");
1441 return FALSE;
1442 }
1443 if (in_part->ch_block_write == 1)
1444 {
1445 ch_log(channel, "FAKED Input not ready for writing");
1446 return FALSE;
1447 }
1448# endif
1449 }
1450#endif
1451 return TRUE;
1452}
1453
1454/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001455 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001456 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001457 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001458channel_write_in(channel_T *channel)
1459{
1460 chanpart_T *in_part = &channel->ch_part[PART_IN];
1461 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001462 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001463 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001464
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001465 if (buf == NULL || in_part->ch_buf_append)
1466 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001467 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001468 {
1469 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001470 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001471 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001472 return;
1473 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001474
1475 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1476 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1477 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001478 if (!can_write_buf_line(channel))
1479 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001480 write_buf_line(buf, lnum, channel);
1481 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001482 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001483
1484 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001485 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001486 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001487 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001488
Bram Moolenaar014069a2016-03-03 22:51:40 +01001489 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001490 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001491 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001492#if defined(FEAT_TERMINAL)
1493 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001494 if (channel->ch_job != NULL)
1495 term_send_eof(channel);
1496#endif
1497
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001498 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001499 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001500 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001501
1502 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001503 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 }
1505 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001506 ch_log(channel, "Still %ld more lines to write",
1507 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001508}
1509
1510/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001511 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001512 */
1513 void
1514channel_buffer_free(buf_T *buf)
1515{
1516 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001517 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001518
1519 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001520 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001521 {
1522 chanpart_T *ch_part = &channel->ch_part[part];
1523
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001524 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001525 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001526 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001527 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001528 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001529 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001530 }
1531}
1532
1533/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001534 * Write any lines waiting to be written to "channel".
1535 */
1536 static void
1537channel_write_input(channel_T *channel)
1538{
1539 chanpart_T *in_part = &channel->ch_part[PART_IN];
1540
1541 if (in_part->ch_writeque.wq_next != NULL)
1542 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1543 else if (in_part->ch_bufref.br_buf != NULL)
1544 {
1545 if (in_part->ch_buf_append)
1546 channel_write_new_lines(in_part->ch_bufref.br_buf);
1547 else
1548 channel_write_in(channel);
1549 }
1550}
1551
1552/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001553 * Write any lines waiting to be written to a channel.
1554 */
1555 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001556channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001557{
1558 channel_T *channel;
1559
1560 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001561 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001562}
1563
1564/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001565 * Write appended lines above the last one in "buf" to the channel.
1566 */
1567 void
1568channel_write_new_lines(buf_T *buf)
1569{
1570 channel_T *channel;
1571 int found_one = FALSE;
1572
1573 /* There could be more than one channel for the buffer, loop over all of
1574 * them. */
1575 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1576 {
1577 chanpart_T *in_part = &channel->ch_part[PART_IN];
1578 linenr_T lnum;
1579 int written = 0;
1580
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001581 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001582 {
1583 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001584 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001585 found_one = TRUE;
1586 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1587 ++lnum)
1588 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001589 if (!can_write_buf_line(channel))
1590 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001591 write_buf_line(buf, lnum, channel);
1592 ++written;
1593 }
1594
1595 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001596 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001597 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001598 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001599 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001600 ch_log(channel, "Still %ld more lines to write",
1601 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001602
1603 in_part->ch_buf_bot = lnum;
1604 }
1605 }
1606 if (!found_one)
1607 buf->b_write_to_channel = FALSE;
1608}
1609
1610/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001611 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001612 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001613 */
1614 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001615invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1616 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001617{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001618 typval_T rettv;
1619 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001620
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001621 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001622 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001623
Bram Moolenaar77073442016-02-13 23:23:53 +01001624 argv[0].v_type = VAR_CHANNEL;
1625 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001626
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001627 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1628 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001629 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001630 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001631}
1632
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001633/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001634 * Return the first node from "channel"/"part" without removing it.
1635 * Returns NULL if there is nothing.
1636 */
1637 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001638channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001639{
1640 readq_T *head = &channel->ch_part[part].ch_head;
1641
1642 return head->rq_next;
1643}
1644
1645/*
1646 * Return a pointer to the first NL in "node".
1647 * Skips over NUL characters.
1648 * Returns NULL if there is no NL.
1649 */
1650 char_u *
1651channel_first_nl(readq_T *node)
1652{
1653 char_u *buffer = node->rq_buffer;
1654 long_u i;
1655
1656 for (i = 0; i < node->rq_buflen; ++i)
1657 if (buffer[i] == NL)
1658 return buffer + i;
1659 return NULL;
1660}
1661
1662/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001663 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001664 * The caller must free it.
1665 * Returns NULL if there is nothing.
1666 */
1667 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001668channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001669{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001670 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001671 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672 char_u *p;
1673
Bram Moolenaar77073442016-02-13 23:23:53 +01001674 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001676 if (outlen != NULL)
1677 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001678 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001679 p = node->rq_buffer;
1680 head->rq_next = node->rq_next;
1681 if (node->rq_next == NULL)
1682 head->rq_prev = NULL;
1683 else
1684 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001685 vim_free(node);
1686 return p;
1687}
1688
1689/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001690 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001691 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001692 */
1693 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001696 readq_T *head = &channel->ch_part[part].ch_head;
1697 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001698 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001699 char_u *res;
1700 char_u *p;
1701
1702 /* If there is only one buffer just get that one. */
1703 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001704 return channel_get(channel, part, outlen);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001705
1706 /* Concatenate everything into one buffer. */
1707 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001708 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001709 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001710 if (res == NULL)
1711 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001712 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001713 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001714 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001715 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001716 p += node->rq_buflen;
1717 }
1718 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001719
1720 /* Free all buffers */
1721 do
1722 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001724 vim_free(p);
1725 } while (p != NULL);
1726
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001727 if (outlen != NULL)
1728 {
1729 *outlen += len;
1730 return res;
1731 }
1732
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001733 /* turn all NUL into NL */
1734 while (len > 0)
1735 {
1736 --len;
1737 if (res[len] == NUL)
1738 res[len] = NL;
1739 }
1740
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001741 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001742}
1743
1744/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001745 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001746 * Caller must check these bytes are available.
1747 */
1748 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001749channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001750{
1751 readq_T *head = &channel->ch_part[part].ch_head;
1752 readq_T *node = head->rq_next;
1753 char_u *buf = node->rq_buffer;
1754
1755 mch_memmove(buf, buf + len, node->rq_buflen - len);
1756 node->rq_buflen -= len;
1757}
1758
1759/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001760 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001761 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001762 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001763 */
1764 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001765channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001766{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001768 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001769 readq_T *last_node;
1770 readq_T *n;
1771 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001772 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001773 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001774
Bram Moolenaar77073442016-02-13 23:23:53 +01001775 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001776 return FAIL;
1777
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001778 last_node = node->rq_next;
1779 len = node->rq_buflen + last_node->rq_buflen + 1;
1780 if (want_nl)
1781 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001782 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001783 {
1784 last_node = last_node->rq_next;
1785 len += last_node->rq_buflen;
1786 }
1787
1788 p = newbuf = alloc(len);
1789 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001790 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001791 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001792 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001793 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001794 node->rq_buffer = newbuf;
1795 for (n = node; n != last_node; )
1796 {
1797 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001798 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001799 p += n->rq_buflen;
1800 vim_free(n->rq_buffer);
1801 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001802 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001803
1804 /* dispose of the collapsed nodes and their buffers */
1805 for (n = node->rq_next; n != last_node; )
1806 {
1807 n = n->rq_next;
1808 vim_free(n->rq_prev);
1809 }
1810 node->rq_next = last_node->rq_next;
1811 if (last_node->rq_next == NULL)
1812 head->rq_prev = node;
1813 else
1814 last_node->rq_next->rq_prev = node;
1815 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001816 return OK;
1817}
1818
1819/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001820 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001821 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001822 * Returns OK or FAIL.
1823 */
1824 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001825channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001826 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001827{
1828 readq_T *node;
1829 readq_T *head = &channel->ch_part[part].ch_head;
1830 char_u *p;
1831 int i;
1832
1833 node = (readq_T *)alloc(sizeof(readq_T));
1834 if (node == NULL)
1835 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001836 /* A NUL is added at the end, because netbeans code expects that.
1837 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001838 node->rq_buffer = alloc(len + 1);
1839 if (node->rq_buffer == NULL)
1840 {
1841 vim_free(node);
1842 return FAIL; /* out of memory */
1843 }
1844
1845 if (channel->ch_part[part].ch_mode == MODE_NL)
1846 {
1847 /* Drop any CR before a NL. */
1848 p = node->rq_buffer;
1849 for (i = 0; i < len; ++i)
1850 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1851 *p++ = buf[i];
1852 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001853 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001854 }
1855 else
1856 {
1857 mch_memmove(node->rq_buffer, buf, len);
1858 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001859 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001860 }
1861
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 if (prepend)
1863 {
1864 /* preend node to the head of the queue */
1865 node->rq_next = head->rq_next;
1866 node->rq_prev = NULL;
1867 if (head->rq_next == NULL)
1868 head->rq_prev = node;
1869 else
1870 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001871 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001872 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001873 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001874 {
1875 /* append node to the tail of the queue */
1876 node->rq_next = NULL;
1877 node->rq_prev = head->rq_prev;
1878 if (head->rq_prev == NULL)
1879 head->rq_next = node;
1880 else
1881 head->rq_prev->rq_next = node;
1882 head->rq_prev = node;
1883 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001884
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001885 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001886 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001887 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001888 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001889 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890 fprintf(log_fd, "'\n");
1891 }
1892 return OK;
1893}
1894
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001895/*
1896 * Try to fill the buffer of "reader".
1897 * Returns FALSE when nothing was added.
1898 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001899 static int
1900channel_fill(js_read_T *reader)
1901{
1902 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001903 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001904 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001905 int keeplen;
1906 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001907 char_u *p;
1908
1909 if (next == NULL)
1910 return FALSE;
1911
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001912 keeplen = reader->js_end - reader->js_buf;
1913 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001914 {
1915 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001916 addlen = (int)STRLEN(next);
1917 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001918 if (p == NULL)
1919 {
1920 vim_free(next);
1921 return FALSE;
1922 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001923 mch_memmove(p, reader->js_buf, keeplen);
1924 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001925 vim_free(next);
1926 next = p;
1927 }
1928
1929 vim_free(reader->js_buf);
1930 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001931 return TRUE;
1932}
1933
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001934/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001935 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001936 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001937 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001938 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001939 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001940channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001941{
1942 js_read_T reader;
1943 typval_T listtv;
1944 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001945 chanpart_T *chanpart = &channel->ch_part[part];
1946 jsonq_T *head = &chanpart->ch_json_head;
1947 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001948 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001950 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001951 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001952
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001953 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001954 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001955 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001956 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001957 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001958
1959 /* When a message is incomplete we wait for a short while for more to
1960 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001961 * or list will make us hang.
1962 * Do not generate error messages, they will be written in a channel log. */
1963 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001964 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001965 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001966 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001967 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001968 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001969 /* Only accept the response when it is a list with at least two
1970 * items. */
1971 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001972 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001973 if (listtv.v_type != VAR_LIST)
1974 ch_error(channel, "Did not receive a list, discarding");
1975 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001976 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001977 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001978 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001979 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001980 else
1981 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001982 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1983 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001984 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001985 else
1986 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001987 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001988 item->jq_value = alloc_tv();
1989 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001990 {
1991 vim_free(item);
1992 clear_tv(&listtv);
1993 }
1994 else
1995 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001996 *item->jq_value = listtv;
1997 item->jq_prev = head->jq_prev;
1998 head->jq_prev = item;
1999 item->jq_next = NULL;
2000 if (item->jq_prev == NULL)
2001 head->jq_next = item;
2002 else
2003 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002004 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002005 }
2006 }
2007 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002008
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002009 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002010 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002011 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002012 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002013 size_t buflen = STRLEN(reader.js_buf);
2014
2015 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002016 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002017 /* First time encountering incomplete message or after receiving
2018 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002019 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002020 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002021 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002022 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002023 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002024#ifdef WIN32
2025 chanpart->ch_deadline = GetTickCount() + 100L;
2026#else
2027 gettimeofday(&chanpart->ch_deadline, NULL);
2028 chanpart->ch_deadline.tv_usec += 100 * 1000;
2029 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2030 {
2031 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2032 ++chanpart->ch_deadline.tv_sec;
2033 }
2034#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002035 }
2036 else
2037 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002038 int timeout;
2039#ifdef WIN32
2040 timeout = GetTickCount() > chanpart->ch_deadline;
2041#else
2042 {
2043 struct timeval now_tv;
2044
2045 gettimeofday(&now_tv, NULL);
2046 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2047 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2048 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2049 }
2050#endif
2051 if (timeout)
2052 {
2053 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002054 chanpart->ch_wait_len = 0;
2055 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002056 }
2057 else
2058 {
2059 reader.js_used = 0;
2060 ch_log(channel, "still waiting on incomplete message");
2061 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002062 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002063 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002064
2065 if (status == FAIL)
2066 {
2067 ch_error(channel, "Decoding failed - discarding input");
2068 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002069 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002070 }
2071 else if (reader.js_buf[reader.js_used] != NUL)
2072 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002073 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002074 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002075 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2076 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002077 ret = status == MAYBE ? FALSE: TRUE;
2078 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002079 else
2080 ret = FALSE;
2081
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002082 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002083 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002084}
2085
2086/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002087 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002088 */
2089 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002090remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002091{
Bram Moolenaar77073442016-02-13 23:23:53 +01002092 if (node->cq_prev == NULL)
2093 head->cq_next = node->cq_next;
2094 else
2095 node->cq_prev->cq_next = node->cq_next;
2096 if (node->cq_next == NULL)
2097 head->cq_prev = node->cq_prev;
2098 else
2099 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002100}
2101
2102/*
2103 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002104 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002105 */
2106 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002107remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002108{
Bram Moolenaar77073442016-02-13 23:23:53 +01002109 if (node->jq_prev == NULL)
2110 head->jq_next = node->jq_next;
2111 else
2112 node->jq_prev->jq_next = node->jq_next;
2113 if (node->jq_next == NULL)
2114 head->jq_prev = node->jq_prev;
2115 else
2116 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002117 vim_free(node);
2118}
2119
2120/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002121 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002122 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002123 * When "id" is zero or negative jut get the first message. But not the one
2124 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002125 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002126 * Return OK when found and return the value in "rettv".
2127 * Return FAIL otherwise.
2128 */
2129 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002130channel_get_json(
2131 channel_T *channel,
2132 ch_part_T part,
2133 int id,
2134 int without_callback,
2135 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002136{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002137 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002138 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002139
Bram Moolenaar77073442016-02-13 23:23:53 +01002140 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002141 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002142 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002143 typval_T *tv = &l->lv_first->li_tv;
2144
Bram Moolenaar958dc692016-12-01 15:34:12 +01002145 if ((without_callback || !item->jq_no_callback)
2146 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002147 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002148 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002149 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002150 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002151 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002152 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002153 ch_log(channel, "Getting JSON message %ld",
2154 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002155 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002156 return OK;
2157 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002158 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002159 }
2160 return FAIL;
2161}
2162
Bram Moolenaar958dc692016-12-01 15:34:12 +01002163/*
2164 * Put back "rettv" into the JSON queue, there was no callback for it.
2165 * Takes over the values in "rettv".
2166 */
2167 static void
2168channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2169{
2170 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2171 jsonq_T *item = head->jq_next;
2172 jsonq_T *newitem;
2173
2174 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2175 /* last item was pushed back, append to the end */
2176 item = NULL;
2177 else while (item != NULL && item->jq_no_callback)
2178 /* append after the last item that was pushed back */
2179 item = item->jq_next;
2180
2181 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2182 if (newitem == NULL)
2183 clear_tv(rettv);
2184 else
2185 {
2186 newitem->jq_value = alloc_tv();
2187 if (newitem->jq_value == NULL)
2188 {
2189 vim_free(newitem);
2190 clear_tv(rettv);
2191 }
2192 else
2193 {
2194 newitem->jq_no_callback = FALSE;
2195 *newitem->jq_value = *rettv;
2196 if (item == NULL)
2197 {
2198 /* append to the end */
2199 newitem->jq_prev = head->jq_prev;
2200 head->jq_prev = newitem;
2201 newitem->jq_next = NULL;
2202 if (newitem->jq_prev == NULL)
2203 head->jq_next = newitem;
2204 else
2205 newitem->jq_prev->jq_next = newitem;
2206 }
2207 else
2208 {
2209 /* append after "item" */
2210 newitem->jq_prev = item;
2211 newitem->jq_next = item->jq_next;
2212 item->jq_next = newitem;
2213 if (newitem->jq_next == NULL)
2214 head->jq_prev = newitem;
2215 else
2216 newitem->jq_next->jq_prev = newitem;
2217 }
2218 }
2219 }
2220}
2221
Bram Moolenaarece61b02016-02-20 21:39:05 +01002222#define CH_JSON_MAX_ARGS 4
2223
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002224/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002225 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002226 * "argv[0]" is the command string.
2227 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002228 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002229 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002230channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002231{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002232 char_u *cmd = argv[0].vval.v_string;
2233 char_u *arg;
2234 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002235
Bram Moolenaarece61b02016-02-20 21:39:05 +01002236 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002237 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002238 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002239 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002240 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002241 return;
2242 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002244 if (arg == NULL)
2245 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002247 if (STRCMP(cmd, "ex") == 0)
2248 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002249 int save_called_emsg = called_emsg;
2250
2251 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002252 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002253 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002254 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002255 --emsg_silent;
2256 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002257 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002258 (char *)get_vim_var_str(VV_ERRMSG));
2259 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002260 }
2261 else if (STRCMP(cmd, "normal") == 0)
2262 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002263 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002264
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002265 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002266 ea.arg = arg;
2267 ea.addr_count = 0;
2268 ea.forceit = TRUE; /* no mapping */
2269 ex_normal(&ea);
2270 }
2271 else if (STRCMP(cmd, "redraw") == 0)
2272 {
2273 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002274
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002275 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002276 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002277 ex_redraw(&ea);
2278 showruler(FALSE);
2279 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002280 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002281 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002282 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002283 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002284 int is_call = cmd[0] == 'c';
2285 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002286
Bram Moolenaarece61b02016-02-20 21:39:05 +01002287 if (argv[id_idx].v_type != VAR_UNKNOWN
2288 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002289 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002290 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002291 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002292 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002293 }
2294 else if (is_call && argv[2].v_type != VAR_LIST)
2295 {
2296 ch_error(channel, "third argument for call must be a list");
2297 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002298 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002299 }
2300 else
2301 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002302 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002303 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002304 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002305 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002306
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002307 /* Don't pollute the display with errors. */
2308 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002309 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002310 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002311 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002312 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002313 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002314 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002315 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002316 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002317 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2318 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002319 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002320
2321 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002322 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002323 int id = argv[id_idx].vval.v_number;
2324
Bram Moolenaar55fab432016-02-07 16:53:13 +01002325 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002326 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002327 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002328 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002329 /* If evaluation failed or the result can't be encoded
2330 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002331 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002332 err_tv.v_type = VAR_STRING;
2333 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002334 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002335 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002336 if (json != NULL)
2337 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002338 channel_send(channel,
2339 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002340 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002341 vim_free(json);
2342 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002343 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002344 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002345 if (tv == &res_tv)
2346 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002347 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002348 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002349 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002350 }
2351 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002352 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002353 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002354 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002355 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002356}
2357
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002358/*
2359 * Invoke the callback at "cbhead".
2360 * Does not redraw but sets channel_need_redraw.
2361 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002362 static void
2363invoke_one_time_callback(
2364 channel_T *channel,
2365 cbq_T *cbhead,
2366 cbq_T *item,
2367 typval_T *argv)
2368{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002369 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002370 (char *)item->cq_callback);
2371 /* Remove the item from the list first, if the callback
2372 * invokes ch_close() the list will be cleared. */
2373 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002374 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002375 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002376 vim_free(item);
2377}
2378
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002379 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002380append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002381{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002382 bufref_T save_curbuf = {NULL, 0, 0};
2383 win_T *save_curwin = NULL;
2384 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002385 linenr_T lnum = buffer->b_ml.ml_line_count;
2386 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002387 chanpart_T *ch_part = &channel->ch_part[part];
2388 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002389 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002390
2391 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2392 {
2393 if (!ch_part->ch_nomod_error)
2394 {
2395 ch_error(channel, "Buffer is not modifiable, cannot append");
2396 ch_part->ch_nomod_error = TRUE;
2397 }
2398 return;
2399 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002400
2401 /* If the buffer is also used as input insert above the last
2402 * line. Don't write these lines. */
2403 if (save_write_to)
2404 {
2405 --lnum;
2406 buffer->b_write_to_channel = FALSE;
2407 }
2408
2409 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002410 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002411
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002412 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002413
2414 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2415 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2416
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002417 u_sync(TRUE);
2418 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002419 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002420
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002421 if (empty)
2422 {
2423 /* The buffer is empty, replace the first (dummy) line. */
2424 ml_replace(lnum, msg, TRUE);
2425 lnum = 0;
2426 }
2427 else
2428 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002429 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002430
2431 /* Restore curbuf/curwin/curtab */
2432 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2433
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002434 if (ch_part->ch_nomodifiable)
2435 buffer->b_p_ma = FALSE;
2436 else
2437 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002438
2439 if (buffer->b_nwindows > 0)
2440 {
2441 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002442
2443 FOR_ALL_WINDOWS(wp)
2444 {
2445 if (wp->w_buffer == buffer
2446 && (save_write_to
2447 ? wp->w_cursor.lnum == lnum + 1
2448 : (wp->w_cursor.lnum == lnum
2449 && wp->w_cursor.col == 0)))
2450 {
2451 ++wp->w_cursor.lnum;
2452 save_curwin = curwin;
2453 curwin = wp;
2454 curbuf = curwin->w_buffer;
2455 scroll_cursor_bot(0, FALSE);
2456 curwin = save_curwin;
2457 curbuf = curwin->w_buffer;
2458 }
2459 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002460 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002461 channel_need_redraw = TRUE;
2462 }
2463
2464 if (save_write_to)
2465 {
2466 channel_T *ch;
2467
2468 /* Find channels reading from this buffer and adjust their
2469 * next-to-read line number. */
2470 buffer->b_write_to_channel = TRUE;
2471 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2472 {
2473 chanpart_T *in_part = &ch->ch_part[PART_IN];
2474
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002475 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002476 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2477 }
2478 }
2479}
2480
Bram Moolenaar437905c2016-04-26 19:01:05 +02002481 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002482drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002483{
2484 char_u *msg;
2485
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002486 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002487 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002488 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002489 vim_free(msg);
2490 }
2491}
2492
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002493/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002494 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002495 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002496 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002497 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002498 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002499may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002500{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002501 char_u *msg = NULL;
2502 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002503 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002504 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002505 chanpart_T *ch_part = &channel->ch_part[part];
2506 ch_mode_T ch_mode = ch_part->ch_mode;
2507 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002508 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002509 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002510 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002511 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002512 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002513
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002514 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002515 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002516 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002517
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002518 /* Use a message-specific callback, part callback or channel callback */
2519 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2520 if (cbitem->cq_seq_nr == 0)
2521 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002522 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002523 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002524 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002525 partial = cbitem->cq_partial;
2526 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002527 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002528 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002529 callback = ch_part->ch_callback;
2530 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002531 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002532 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002533 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002534 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002535 partial = channel->ch_partial;
2536 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002537
Bram Moolenaarec68a992016-10-03 21:37:41 +02002538 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002539 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2540 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002541 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002542 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002543 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002544 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002545 buffer = NULL;
2546 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002547
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002548 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002549 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002550 listitem_T *item;
2551 int argc = 0;
2552
Bram Moolenaard7ece102016-02-02 23:23:02 +01002553 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002554 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002555 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002556 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002557 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002558 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002559 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002560 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002561
Bram Moolenaarece61b02016-02-20 21:39:05 +01002562 for (item = listtv->vval.v_list->lv_first;
2563 item != NULL && argc < CH_JSON_MAX_ARGS;
2564 item = item->li_next)
2565 argv[argc++] = item->li_tv;
2566 while (argc < CH_JSON_MAX_ARGS)
2567 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002568
Bram Moolenaarece61b02016-02-20 21:39:05 +01002569 if (argv[0].v_type == VAR_STRING)
2570 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002571 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002572 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002573 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002574 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002575 }
2576
Bram Moolenaarece61b02016-02-20 21:39:05 +01002577 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002578 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002579 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002580 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002581 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002582 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002583 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002584 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002585 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002586 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002587 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002588 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002589 return FALSE;
2590 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002591 else
2592 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002593 /* If there is no callback or buffer drop the message. */
2594 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002595 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002596 /* If there is a close callback it may use ch_read() to get the
2597 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002598 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002599 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002600 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002601 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002602
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002603 if (ch_mode == MODE_NL)
2604 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002605 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002606 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002607 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002608
2609 /* See if we have a message ending in NL in the first buffer. If
2610 * not try to concatenate the first and the second buffer. */
2611 while (TRUE)
2612 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002613 node = channel_peek(channel, part);
2614 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002615 if (nl != NULL)
2616 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002617 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002618 {
2619 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2620 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002621 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002622 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002623 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002624 buf = node->rq_buffer;
2625
Bram Moolenaarec68a992016-10-03 21:37:41 +02002626 if (nl == NULL)
2627 {
2628 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002629 char_u *new_buf;
2630
2631 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2632 if (new_buf == NULL)
2633 /* This might fail over and over again, should the message
2634 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002635 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002636 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002637 node->rq_buffer = buf;
2638 nl = buf + node->rq_buflen++;
2639 *nl = NUL;
2640 }
2641
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002642 /* Convert NUL to NL, the internal representation. */
2643 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2644 if (*p == NUL)
2645 *p = NL;
2646
2647 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002648 {
2649 /* get the whole buffer, drop the NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002650 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002651 *nl = NUL;
2652 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002653 else
2654 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002655 /* Copy the message into allocated memory (excluding the NL)
2656 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002657 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002658 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002659 }
2660 }
2661 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002662 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002663 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002664 * get everything we have.
2665 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002666 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002667 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002668
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002669 if (msg == NULL)
2670 return FALSE; /* out of memory (and avoids Coverity warning) */
2671
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002672 argv[1].v_type = VAR_STRING;
2673 argv[1].vval.v_string = msg;
2674 }
2675
Bram Moolenaara07fec92016-02-05 21:04:08 +01002676 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002677 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002678 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002679
Bram Moolenaar958dc692016-12-01 15:34:12 +01002680 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002681 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002682 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002683 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002684 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002685 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002686 break;
2687 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002688 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002689 {
2690 if (channel->ch_drop_never)
2691 {
2692 /* message must be read with ch_read() */
2693 channel_push_json(channel, part, listtv);
2694 listtv = NULL;
2695 }
2696 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002697 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002698 seq_nr);
2699 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002700 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002701 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002702 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002703 if (buffer != NULL)
2704 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002705 if (msg == NULL)
2706 /* JSON or JS mode: re-encode the message. */
2707 msg = json_encode(listtv, ch_mode);
2708 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002709 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002710#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002711 if (buffer->b_term != NULL)
2712 write_to_term(buffer, msg, channel);
2713 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002714#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002715 append_to_buffer(buffer, msg, channel, part);
2716 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002717 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002718
Bram Moolenaar187db502016-02-27 14:44:26 +01002719 if (callback != NULL)
2720 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002721 if (cbitem != NULL)
2722 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2723 else
2724 {
2725 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002726 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002727 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002728 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002729 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002730 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002731 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002732 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002733 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002734
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002735 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002736 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002737 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002738
2739 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002740}
2741
2742/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002743 * Return TRUE when channel "channel" is open for writing to.
2744 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002745 */
2746 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002747channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002748{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002749 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002750 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002751}
2752
2753/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002754 * Return TRUE when channel "channel" is open for reading or writing.
2755 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002756 */
2757 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002758channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002759{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002760 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002761 || channel->CH_IN_FD != INVALID_FD
2762 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002763 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002764}
2765
2766/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002767 * Return TRUE if "channel" has JSON or other typeahead.
2768 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002769 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002770channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002771{
2772 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2773
2774 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2775 {
2776 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2777 jsonq_T *item = head->jq_next;
2778
2779 return item != NULL;
2780 }
2781 return channel_peek(channel, part) != NULL;
2782}
2783
2784/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002785 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002786 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002787 */
2788 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002789channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002790{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002791 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002792 int has_readahead = FALSE;
2793
Bram Moolenaar77073442016-02-13 23:23:53 +01002794 if (channel == NULL)
2795 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002796 if (req_part == PART_OUT)
2797 {
2798 if (channel->CH_OUT_FD != INVALID_FD)
2799 return "open";
2800 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002801 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002802 }
2803 else if (req_part == PART_ERR)
2804 {
2805 if (channel->CH_ERR_FD != INVALID_FD)
2806 return "open";
2807 if (channel_has_readahead(channel, PART_ERR))
2808 has_readahead = TRUE;
2809 }
2810 else
2811 {
2812 if (channel_is_open(channel))
2813 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002814 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002815 if (channel_has_readahead(channel, part))
2816 {
2817 has_readahead = TRUE;
2818 break;
2819 }
2820 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002821
2822 if (has_readahead)
2823 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002824 return "closed";
2825}
2826
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002827 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002828channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002829{
2830 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002831 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002832 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002833 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002834 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002835
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002836 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002837 STRCAT(namebuf, "_");
2838 tail = STRLEN(namebuf);
2839
2840 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002841 if (chanpart->ch_fd != INVALID_FD)
2842 status = "open";
2843 else if (channel_has_readahead(channel, part))
2844 status = "buffered";
2845 else
2846 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002847 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002848
2849 STRCPY(namebuf + tail, "mode");
2850 switch (chanpart->ch_mode)
2851 {
2852 case MODE_NL: s = "NL"; break;
2853 case MODE_RAW: s = "RAW"; break;
2854 case MODE_JSON: s = "JSON"; break;
2855 case MODE_JS: s = "JS"; break;
2856 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002857 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002858
2859 STRCPY(namebuf + tail, "io");
2860 if (part == PART_SOCK)
2861 s = "socket";
2862 else switch (chanpart->ch_io)
2863 {
2864 case JIO_NULL: s = "null"; break;
2865 case JIO_PIPE: s = "pipe"; break;
2866 case JIO_FILE: s = "file"; break;
2867 case JIO_BUFFER: s = "buffer"; break;
2868 case JIO_OUT: s = "out"; break;
2869 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002870 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002871
2872 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002873 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002874}
2875
2876 void
2877channel_info(channel_T *channel, dict_T *dict)
2878{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002879 dict_add_number(dict, "id", channel->ch_id);
2880 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002881
2882 if (channel->ch_hostname != NULL)
2883 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002884 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2885 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002886 channel_part_info(channel, dict, "sock", PART_SOCK);
2887 }
2888 else
2889 {
2890 channel_part_info(channel, dict, "out", PART_OUT);
2891 channel_part_info(channel, dict, "err", PART_ERR);
2892 channel_part_info(channel, dict, "in", PART_IN);
2893 }
2894}
2895
Bram Moolenaar77073442016-02-13 23:23:53 +01002896/*
2897 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002898 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002899 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002900 */
2901 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002902channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002903{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002904 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002905
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002906#ifdef FEAT_GUI
2907 channel_gui_unregister(channel);
2908#endif
2909
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002910 ch_close_part(channel, PART_SOCK);
2911 ch_close_part(channel, PART_IN);
2912 ch_close_part(channel, PART_OUT);
2913 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002914
Bram Moolenaara9f02812017-08-05 17:40:38 +02002915 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002916 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002917 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002918
Bram Moolenaara9f02812017-08-05 17:40:38 +02002919 /* Invoke callbacks and flush buffers before the close callback. */
2920 if (channel->ch_close_cb != NULL)
2921 ch_log(channel,
2922 "Invoking callbacks and flushing buffers before closing");
2923 for (part = PART_SOCK; part < PART_IN; ++part)
2924 {
2925 if (channel->ch_close_cb != NULL
2926 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2927 {
2928 /* Increment the refcount to avoid the channel being freed
2929 * halfway. */
2930 ++channel->ch_refcount;
2931 if (channel->ch_close_cb == NULL)
2932 ch_log(channel, "flushing %s buffers before closing",
2933 part_names[part]);
2934 while (may_invoke_callback(channel, part))
2935 ;
2936 --channel->ch_refcount;
2937 }
2938 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002939
Bram Moolenaara9f02812017-08-05 17:40:38 +02002940 if (channel->ch_close_cb != NULL)
2941 {
2942 typval_T argv[1];
2943 typval_T rettv;
2944 int dummy;
2945
2946 /* Increment the refcount to avoid the channel being freed
2947 * halfway. */
2948 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002949 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002950 (char *)channel->ch_close_cb);
2951 argv[0].v_type = VAR_CHANNEL;
2952 argv[0].vval.v_channel = channel;
2953 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002954 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002955 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002956 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002957 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002958
Bram Moolenaara9f02812017-08-05 17:40:38 +02002959 /* the callback is only called once */
2960 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2961 channel->ch_close_cb = NULL;
2962 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002963
Bram Moolenaara9f02812017-08-05 17:40:38 +02002964 if (channel_need_redraw)
2965 {
2966 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002967 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002968 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002969
Bram Moolenaara9f02812017-08-05 17:40:38 +02002970 if (!channel->ch_drop_never)
2971 /* any remaining messages are useless now */
2972 for (part = PART_SOCK; part < PART_IN; ++part)
2973 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002974
2975 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002976 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002977 }
2978
2979 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002980
2981#ifdef FEAT_TERMINAL
2982 term_channel_closed(channel);
2983#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002984}
2985
Bram Moolenaard04a0202016-01-26 23:30:18 +01002986/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002987 * Close the "in" part channel "channel".
2988 */
2989 void
2990channel_close_in(channel_T *channel)
2991{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002992 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002993}
2994
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002995 static void
2996remove_from_writeque(writeq_T *wq, writeq_T *entry)
2997{
2998 ga_clear(&entry->wq_ga);
2999 wq->wq_next = entry->wq_next;
3000 if (wq->wq_next == NULL)
3001 wq->wq_prev = NULL;
3002 else
3003 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003004 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003005}
3006
Bram Moolenaar0874a832016-09-01 15:11:51 +02003007/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003008 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003009 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003010 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003011channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003012{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003013 chanpart_T *ch_part = &channel->ch_part[part];
3014 jsonq_T *json_head = &ch_part->ch_json_head;
3015 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003016
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003017 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003018 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003019
3020 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003021 {
3022 cbq_T *node = cb_head->cq_next;
3023
3024 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003025 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003026 vim_free(node);
3027 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003028
3029 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003030 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003031 free_tv(json_head->jq_next->jq_value);
3032 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003033 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003034
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003035 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3036 ch_part->ch_callback = NULL;
3037 ch_part->ch_partial = NULL;
3038
3039 while (ch_part->ch_writeque.wq_next != NULL)
3040 remove_from_writeque(&ch_part->ch_writeque,
3041 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003042}
3043
3044/*
3045 * Clear all the read buffers on "channel".
3046 */
3047 void
3048channel_clear(channel_T *channel)
3049{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003050 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003051 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003052 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003053 channel_clear_one(channel, PART_OUT);
3054 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003055 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003056 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003057 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003058 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003059 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003060 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003061 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003062}
3063
Bram Moolenaar77073442016-02-13 23:23:53 +01003064#if defined(EXITFREE) || defined(PROTO)
3065 void
3066channel_free_all(void)
3067{
3068 channel_T *channel;
3069
Bram Moolenaard6051b52016-02-28 15:49:03 +01003070 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003071 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3072 channel_clear(channel);
3073}
3074#endif
3075
3076
Bram Moolenaar715d2852016-04-30 17:06:31 +02003077/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003078#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003079
3080/* Buffer size for reading incoming messages. */
3081#define MAXMSGSIZE 4096
3082
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003083#if defined(HAVE_SELECT)
3084/*
3085 * Add write fds where we are waiting for writing to be possible.
3086 */
3087 static int
3088channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3089{
3090 int maxfd = maxfd_arg;
3091 channel_T *ch;
3092
3093 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3094 {
3095 chanpart_T *in_part = &ch->ch_part[PART_IN];
3096
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003097 if (in_part->ch_fd != INVALID_FD
3098 && (in_part->ch_bufref.br_buf != NULL
3099 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003100 {
3101 FD_SET((int)in_part->ch_fd, wfds);
3102 if ((int)in_part->ch_fd >= maxfd)
3103 maxfd = (int)in_part->ch_fd + 1;
3104 }
3105 }
3106 return maxfd;
3107}
3108#else
3109/*
3110 * Add write fds where we are waiting for writing to be possible.
3111 */
3112 static int
3113channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3114{
3115 int nfd = nfd_in;
3116 channel_T *ch;
3117
3118 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3119 {
3120 chanpart_T *in_part = &ch->ch_part[PART_IN];
3121
Bram Moolenaar683b7962017-08-19 15:51:59 +02003122 if (in_part->ch_fd != INVALID_FD
3123 && (in_part->ch_bufref.br_buf != NULL
3124 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003125 {
3126 in_part->ch_poll_idx = nfd;
3127 fds[nfd].fd = in_part->ch_fd;
3128 fds[nfd].events = POLLOUT;
3129 ++nfd;
3130 }
3131 else
3132 in_part->ch_poll_idx = -1;
3133 }
3134 return nfd;
3135}
3136#endif
3137
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003138typedef enum {
3139 CW_READY,
3140 CW_NOT_READY,
3141 CW_ERROR
3142} channel_wait_result;
3143
Bram Moolenaard04a0202016-01-26 23:30:18 +01003144/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003145 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003146 * Return CW_READY when there is something to read.
3147 * Return CW_NOT_READY when there is nothing to read.
3148 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003149 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003150 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003151channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003152{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003153 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003154 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003155
Bram Moolenaard8070362016-02-15 21:56:54 +01003156# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003157 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003158 {
3159 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003160 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003161 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003162 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003163
3164 /* reading from a pipe, not a socket */
3165 while (TRUE)
3166 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003167 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3168
3169 if (r && nread > 0)
3170 return CW_READY;
3171 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003172 {
3173 DWORD err = GetLastError();
3174
3175 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3176 return CW_ERROR;
3177
3178 if (channel->ch_named_pipe)
3179 {
3180 DisconnectNamedPipe((HANDLE)fd);
3181 ConnectNamedPipe((HANDLE)fd, NULL);
3182 }
3183 else
3184 return CW_ERROR;
3185 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003186
3187 /* perhaps write some buffer lines */
3188 channel_write_any_lines();
3189
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003190 sleep_time = deadline - GetTickCount();
3191 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003192 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003193 /* Wait for a little while. Very short at first, up to 10 msec
3194 * after looping a few times. */
3195 if (sleep_time > delay)
3196 sleep_time = delay;
3197 Sleep(sleep_time);
3198 delay = delay * 2;
3199 if (delay > 10)
3200 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003201 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003202 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003203 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003204#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003205 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003206#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003207 struct timeval tval;
3208 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003209 fd_set wfds;
3210 int ret;
3211 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003212
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003213 tval.tv_sec = timeout / 1000;
3214 tval.tv_usec = (timeout % 1000) * 1000;
3215 for (;;)
3216 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003217 FD_ZERO(&rfds);
3218 FD_SET((int)fd, &rfds);
3219
3220 /* Write lines to a pipe when a pipe can be written to. Need to
3221 * set this every time, some buffers may be done. */
3222 maxfd = (int)fd + 1;
3223 FD_ZERO(&wfds);
3224 maxfd = channel_fill_wfds(maxfd, &wfds);
3225
3226 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003227# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003228 SOCK_ERRNO;
3229 if (ret == -1 && errno == EINTR)
3230 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003231# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003232 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003233 {
3234 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003235 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003236 channel_write_any_lines();
3237 continue;
3238 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003239 break;
3240 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003241#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003242 for (;;)
3243 {
3244 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3245 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003246
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003247 fds[0].fd = fd;
3248 fds[0].events = POLLIN;
3249 nfd = channel_fill_poll_write(nfd, fds);
3250 if (poll(fds, nfd, timeout) > 0)
3251 {
3252 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003253 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003254 channel_write_any_lines();
3255 continue;
3256 }
3257 break;
3258 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003259#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003260 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003261 return CW_NOT_READY;
3262}
3263
3264 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003265ch_close_part_on_error(
3266 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003267{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003268 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003269
3270 if (is_err)
3271 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003272 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003273 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003274 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003275
3276 /* Queue a "DETACH" netbeans message in the command queue in order to
3277 * terminate the netbeans session later. Do not end the session here
3278 * directly as we may be running in the context of a call to
3279 * netbeans_parse_messages():
3280 * netbeans_parse_messages
3281 * -> autocmd triggered while processing the netbeans cmd
3282 * -> ui_breakcheck
3283 * -> gui event loop or select loop
3284 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003285 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003286 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003287 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003288 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003289 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3290
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003291 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003292 * close the channel yet, there may be something to read on another part.
3293 * When stdout and stderr use the same FD we get the error only on one of
3294 * them, also close the other. */
3295 if (part == PART_OUT || part == PART_ERR)
3296 {
3297 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3298
3299 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3300 ch_close_part(channel, other);
3301 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003302 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003303
3304#ifdef FEAT_GUI
3305 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003306 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003307#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003308}
3309
3310 static void
3311channel_close_now(channel_T *channel)
3312{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003313 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003314 if (channel->ch_nb_close_cb != NULL)
3315 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003316 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003317}
3318
3319/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003320 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003321 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003322 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003323 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003324 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003325channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003326{
3327 static char_u *buf = NULL;
3328 int len = 0;
3329 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003330 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003331 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003332
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003333 fd = channel->ch_part[part].ch_fd;
3334 if (fd == INVALID_FD)
3335 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003336 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003337 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003338 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003339 }
3340 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003341
3342 /* Allocate a buffer to read into. */
3343 if (buf == NULL)
3344 {
3345 buf = alloc(MAXMSGSIZE);
3346 if (buf == NULL)
3347 return; /* out of memory! */
3348 }
3349
3350 /* Keep on reading for as long as there is something to read.
3351 * Use select() or poll() to avoid blocking on a message that is exactly
3352 * MAXMSGSIZE long. */
3353 for (;;)
3354 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003355 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003356 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003357 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003358 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003359 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003360 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003361 if (len <= 0)
3362 break; /* error or nothing more to read */
3363
3364 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003365 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003366 readlen += len;
3367 if (len < MAXMSGSIZE)
3368 break; /* did read everything that's available */
3369 }
3370
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003371 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003372 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003373 {
3374 if (!channel->ch_keep_open)
3375 ch_close_part_on_error(channel, part, (len < 0), func);
3376 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003377#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003378 else if (CH_HAS_GUI && gtk_main_level() > 0)
3379 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003380 gtk_main_quit();
3381#endif
3382}
3383
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003384/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003385 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003386 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003387 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003388 * Returns what was read in allocated memory.
3389 * Returns NULL in case of error or timeout.
3390 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003391 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003392channel_read_block(
3393 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003394{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003395 char_u *buf;
3396 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003397 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003398 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003399 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003400 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003401
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003402 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003403 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003404
3405 while (TRUE)
3406 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003407 node = channel_peek(channel, part);
3408 if (node != NULL)
3409 {
3410 if (mode == MODE_RAW || (mode == MODE_NL
3411 && channel_first_nl(node) != NULL))
3412 /* got a complete message */
3413 break;
3414 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3415 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003416 /* If not blocking or nothing more is coming then return what we
3417 * have. */
3418 if (raw || fd == INVALID_FD)
3419 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003420 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003421
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003422 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003423 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003424 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003425 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003426 {
3427 ch_log(channel, "Timed out");
3428 return NULL;
3429 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003430 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003431 }
3432
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003433 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003434 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003435 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003436 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003437 }
3438 else
3439 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003440 char_u *p;
3441
3442 buf = node->rq_buffer;
3443 nl = channel_first_nl(node);
3444
3445 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003446 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003447 if (*p == NUL)
3448 *p = NL;
3449
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003450 if (nl == NULL)
3451 {
3452 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003453 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003454 }
3455 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003456 {
3457 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003458 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003459 *nl = NUL;
3460 }
3461 else
3462 {
3463 /* Copy the message into allocated memory and remove it from the
3464 * buffer. */
3465 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003466 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003467 }
3468 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003469 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003470 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003471 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003472}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003473
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003474/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003475 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003476 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003477 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003478 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003479 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003480 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003481channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003482 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003483 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003484 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003485 int id,
3486 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003487{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003488 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003489 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003490 int timeout;
3491 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003492
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003493 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003494 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003495 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003496 for (;;)
3497 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003498 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003499
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003500 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003501 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003502 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003503 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003504 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003505 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003506
Bram Moolenaard7ece102016-02-02 23:23:02 +01003507 if (!more)
3508 {
3509 /* Handle any other messages in the queue. If done some more
3510 * messages may have arrived. */
3511 if (channel_parse_messages())
3512 continue;
3513
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003514 /* Wait for up to the timeout. If there was an incomplete message
3515 * use the deadline for that. */
3516 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003517 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003518 {
3519#ifdef WIN32
3520 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3521#else
3522 {
3523 struct timeval now_tv;
3524
3525 gettimeofday(&now_tv, NULL);
3526 timeout = (chanpart->ch_deadline.tv_sec
3527 - now_tv.tv_sec) * 1000
3528 + (chanpart->ch_deadline.tv_usec
3529 - now_tv.tv_usec) / 1000
3530 + 1;
3531 }
3532#endif
3533 if (timeout < 0)
3534 {
3535 /* Something went wrong, channel_parse_json() didn't
3536 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003537 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003538 timeout = timeout_arg;
3539 }
3540 else if (timeout > timeout_arg)
3541 timeout = timeout_arg;
3542 }
3543 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003544 if (fd == INVALID_FD
3545 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003546 {
3547 if (timeout == timeout_arg)
3548 {
3549 if (fd != INVALID_FD)
3550 ch_log(channel, "Timed out");
3551 break;
3552 }
3553 }
3554 else
3555 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003556 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003557 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003558 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003559 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003560}
3561
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003562/*
3563 * Common for ch_read() and ch_readraw().
3564 */
3565 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003566common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003567{
3568 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003569 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003570 jobopt_T opt;
3571 int mode;
3572 int timeout;
3573 int id = -1;
3574 typval_T *listtv = NULL;
3575
3576 /* return an empty string by default */
3577 rettv->v_type = VAR_STRING;
3578 rettv->vval.v_string = NULL;
3579
3580 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003581 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003582 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003583 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003584
Bram Moolenaar437905c2016-04-26 19:01:05 +02003585 if (opt.jo_set & JO_PART)
3586 part = opt.jo_part;
3587 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003588 if (channel != NULL)
3589 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003590 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003591 part = channel_part_read(channel);
3592 mode = channel_get_mode(channel, part);
3593 timeout = channel_get_timeout(channel, part);
3594 if (opt.jo_set & JO_TIMEOUT)
3595 timeout = opt.jo_timeout;
3596
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003597 if (blob)
3598 {
3599 int outlen = 0;
3600 char_u *p = channel_read_block(channel, part,
3601 timeout, TRUE, &outlen);
3602 if (p != NULL)
3603 {
3604 blob_T *b = blob_alloc();
3605
3606 if (b != NULL)
3607 {
3608 b->bv_ga.ga_len = outlen;
3609 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3610 blob_free(b);
3611 else
3612 {
3613 memcpy(b->bv_ga.ga_data, p, outlen);
3614 rettv_blob_set(rettv, b);
3615 }
3616 }
3617 vim_free(p);
3618 }
3619 }
3620 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003621 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003622 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003623 else
3624 {
3625 if (opt.jo_set & JO_ID)
3626 id = opt.jo_id;
3627 channel_read_json_block(channel, part, timeout, id, &listtv);
3628 if (listtv != NULL)
3629 {
3630 *rettv = *listtv;
3631 vim_free(listtv);
3632 }
3633 else
3634 {
3635 rettv->v_type = VAR_SPECIAL;
3636 rettv->vval.v_number = VVAL_NONE;
3637 }
3638 }
3639 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003640
3641theend:
3642 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003643}
3644
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003645# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3646 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003647/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003648 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003649 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003650 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003651 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003652channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003653{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003654 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003655 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003656
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003657 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003658 for (channel = first_channel; channel != NULL;
3659 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003660 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003661 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003662 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003663 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003664 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003665 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003666 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003667 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003668 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003669}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003670# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003671
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003672# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003673/*
3674 * Check the channels for anything that is ready to be read.
3675 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003676 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003677 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003678 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003679channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003680{
3681 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003682 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003683 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003684
3685 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3686 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003687 if (only_keep_open && !channel->ch_keep_open)
3688 continue;
3689
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003690 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003691 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003692 {
3693 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003694 if (fd != INVALID_FD)
3695 {
3696 int r = channel_wait(channel, fd, 0);
3697
3698 if (r == CW_READY)
3699 channel_read(channel, part, "channel_handle_events");
3700 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003701 ch_close_part_on_error(channel, part, TRUE,
3702 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003703 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003704 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003705 }
3706}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003707# endif
3708
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003709# if defined(FEAT_GUI) || defined(PROTO)
3710/*
3711 * Return TRUE when there is any channel with a keep_open flag.
3712 */
3713 int
3714channel_any_keep_open()
3715{
3716 channel_T *channel;
3717
3718 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3719 if (channel->ch_keep_open)
3720 return TRUE;
3721 return FALSE;
3722}
3723# endif
3724
Bram Moolenaard04a0202016-01-26 23:30:18 +01003725/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003726 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003727 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003728 */
3729 void
3730channel_set_nonblock(channel_T *channel, ch_part_T part)
3731{
3732 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003733 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003734
3735 if (fd != INVALID_FD)
3736 {
3737#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003738 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003739
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003740 ioctlsocket(fd, FIONBIO, &val);
3741#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003742 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003743#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003744 ch_part->ch_nonblocking = TRUE;
3745 }
3746}
3747
3748/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003749 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003750 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003751 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003752 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003753 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003754channel_send(
3755 channel_T *channel,
3756 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003757 char_u *buf_arg,
3758 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003759 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003760{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003761 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003762 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003763 chanpart_T *ch_part = &channel->ch_part[part];
3764 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003765
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003766 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003767 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003768 {
3769 if (!channel->ch_error && fun != NULL)
3770 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003771 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003772 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003773 }
3774 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003775 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003776 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003777
Bram Moolenaar0b146882018-09-06 16:27:24 +02003778 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3779 channel_set_nonblock(channel, part);
3780
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003781 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003782 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003783 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003784 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003785 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003786 fprintf(log_fd, "'\n");
3787 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003788 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003789 }
3790
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003791 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003792 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003793 writeq_T *wq = &ch_part->ch_writeque;
3794 char_u *buf;
3795 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003796
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003797 if (wq->wq_next != NULL)
3798 {
3799 /* first write what was queued */
3800 buf = wq->wq_next->wq_ga.ga_data;
3801 len = wq->wq_next->wq_ga.ga_len;
3802 did_use_queue = TRUE;
3803 }
3804 else
3805 {
3806 if (len_arg == 0)
3807 /* nothing to write, called from channel_select_check() */
3808 return OK;
3809 buf = buf_arg;
3810 len = len_arg;
3811 }
3812
3813 if (part == PART_SOCK)
3814 res = sock_write(fd, (char *)buf, len);
3815 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003816 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003817 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003818#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003819 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003820 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003821 DisconnectNamedPipe((HANDLE)fd);
3822 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003823 }
3824#endif
3825
3826 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003827 if (res < 0 && (errno == EWOULDBLOCK
3828#ifdef EAGAIN
3829 || errno == EAGAIN
3830#endif
3831 ))
3832 res = 0; /* nothing got written */
3833
3834 if (res >= 0 && ch_part->ch_nonblocking)
3835 {
3836 writeq_T *entry = wq->wq_next;
3837
3838 if (did_use_queue)
3839 ch_log(channel, "Sent %d bytes now", res);
3840 if (res == len)
3841 {
3842 /* Wrote all the buf[len] bytes. */
3843 if (entry != NULL)
3844 {
3845 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003846 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003847 continue;
3848 }
3849 if (did_use_queue)
3850 ch_log(channel, "Write queue empty");
3851 }
3852 else
3853 {
3854 /* Wrote only buf[res] bytes, can't write more now. */
3855 if (entry != NULL)
3856 {
3857 if (res > 0)
3858 {
3859 /* Remove the bytes that were written. */
3860 mch_memmove(entry->wq_ga.ga_data,
3861 (char *)entry->wq_ga.ga_data + res,
3862 len - res);
3863 entry->wq_ga.ga_len -= res;
3864 }
3865 buf = buf_arg;
3866 len = len_arg;
3867 }
3868 else
3869 {
3870 buf += res;
3871 len -= res;
3872 }
3873 ch_log(channel, "Adding %d bytes to the write queue", len);
3874
3875 /* Append the not written bytes of the argument to the write
3876 * buffer. Limit entries to 4000 bytes. */
3877 if (wq->wq_prev != NULL
3878 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3879 {
3880 writeq_T *last = wq->wq_prev;
3881
3882 /* append to the last entry */
3883 if (ga_grow(&last->wq_ga, len) == OK)
3884 {
3885 mch_memmove((char *)last->wq_ga.ga_data
3886 + last->wq_ga.ga_len,
3887 buf, len);
3888 last->wq_ga.ga_len += len;
3889 }
3890 }
3891 else
3892 {
3893 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3894
3895 if (last != NULL)
3896 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003897 last->wq_prev = wq->wq_prev;
3898 last->wq_next = NULL;
3899 if (wq->wq_prev == NULL)
3900 wq->wq_next = last;
3901 else
3902 wq->wq_prev->wq_next = last;
3903 wq->wq_prev = last;
3904 ga_init2(&last->wq_ga, 1, 1000);
3905 if (ga_grow(&last->wq_ga, len) == OK)
3906 {
3907 mch_memmove(last->wq_ga.ga_data, buf, len);
3908 last->wq_ga.ga_len = len;
3909 }
3910 }
3911 }
3912 }
3913 }
3914 else if (res != len)
3915 {
3916 if (!channel->ch_error && fun != NULL)
3917 {
3918 ch_error(channel, "%s(): write failed", fun);
3919 EMSG2(_("E631: %s(): write failed"), fun);
3920 }
3921 channel->ch_error = TRUE;
3922 return FAIL;
3923 }
3924
3925 channel->ch_error = FALSE;
3926 return OK;
3927 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003928}
3929
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003930/*
3931 * Common for "ch_sendexpr()" and "ch_sendraw()".
3932 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003933 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003934 * Otherwise returns NULL.
3935 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003936 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003937send_common(
3938 typval_T *argvars,
3939 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003940 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003941 int id,
3942 int eval,
3943 jobopt_T *opt,
3944 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003945 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003946{
3947 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003948 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003949
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003950 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003951 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003952 if (channel == NULL)
3953 return NULL;
3954 part_send = channel_part_send(channel);
3955 *part_read = channel_part_read(channel);
3956
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003957 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003958 return NULL;
3959
3960 /* Set the callback. An empty callback means no callback and not reading
3961 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3962 * allowed. */
3963 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3964 {
3965 if (eval)
3966 {
3967 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3968 return NULL;
3969 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003970 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003971 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003972 }
3973
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003974 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003975 && opt->jo_callback == NULL)
3976 return channel;
3977 return NULL;
3978}
3979
3980/*
3981 * common for "ch_evalexpr()" and "ch_sendexpr()"
3982 */
3983 void
3984ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3985{
3986 char_u *text;
3987 typval_T *listtv;
3988 channel_T *channel;
3989 int id;
3990 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003991 ch_part_T part_send;
3992 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003993 jobopt_T opt;
3994 int timeout;
3995
3996 /* return an empty string by default */
3997 rettv->v_type = VAR_STRING;
3998 rettv->vval.v_string = NULL;
3999
Bram Moolenaar437905c2016-04-26 19:01:05 +02004000 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004001 if (channel == NULL)
4002 return;
4003 part_send = channel_part_send(channel);
4004
4005 ch_mode = channel_get_mode(channel, part_send);
4006 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4007 {
4008 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
4009 return;
4010 }
4011
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004012 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004013 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004014 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004015 if (text == NULL)
4016 return;
4017
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004018 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004019 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4020 vim_free(text);
4021 if (channel != NULL && eval)
4022 {
4023 if (opt.jo_set & JO_TIMEOUT)
4024 timeout = opt.jo_timeout;
4025 else
4026 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004027 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4028 == OK)
4029 {
4030 list_T *list = listtv->vval.v_list;
4031
4032 /* Move the item from the list and then change the type to
4033 * avoid the value being freed. */
4034 *rettv = list->lv_last->li_tv;
4035 list->lv_last->li_tv.v_type = VAR_NUMBER;
4036 free_tv(listtv);
4037 }
4038 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004039 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004040}
4041
4042/*
4043 * common for "ch_evalraw()" and "ch_sendraw()"
4044 */
4045 void
4046ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4047{
4048 char_u buf[NUMBUFLEN];
4049 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004050 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004051 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004052 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004053 jobopt_T opt;
4054 int timeout;
4055
4056 /* return an empty string by default */
4057 rettv->v_type = VAR_STRING;
4058 rettv->vval.v_string = NULL;
4059
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004060 if (argvars[1].v_type == VAR_BLOB)
4061 {
4062 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4063 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4064 }
4065 else
4066 {
4067 text = tv_get_string_buf(&argvars[1], buf);
4068 len = STRLEN(text);
4069 }
4070 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004071 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4072 if (channel != NULL && eval)
4073 {
4074 if (opt.jo_set & JO_TIMEOUT)
4075 timeout = opt.jo_timeout;
4076 else
4077 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004078 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004079 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004080 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004081 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004082}
4083
Bram Moolenaarf3360612017-10-01 16:21:31 +02004084# define KEEP_OPEN_TIME 20 /* msec */
4085
Bram Moolenaard04a0202016-01-26 23:30:18 +01004086# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004087/*
4088 * Add open channels to the poll struct.
4089 * Return the adjusted struct index.
4090 * The type of "fds" is hidden to avoid problems with the function proto.
4091 */
4092 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004093channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004094{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004095 int nfd = nfd_in;
4096 channel_T *channel;
4097 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004098 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004099
Bram Moolenaar77073442016-02-13 23:23:53 +01004100 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004101 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004102 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004103 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004104 chanpart_T *ch_part = &channel->ch_part[part];
4105
4106 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004107 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004108 if (channel->ch_keep_open)
4109 {
4110 /* For unknown reason poll() returns immediately for a
4111 * keep-open channel. Instead of adding it to the fds add
4112 * a short timeout and check, like polling. */
4113 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4114 *towait = KEEP_OPEN_TIME;
4115 }
4116 else
4117 {
4118 ch_part->ch_poll_idx = nfd;
4119 fds[nfd].fd = ch_part->ch_fd;
4120 fds[nfd].events = POLLIN;
4121 nfd++;
4122 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004123 }
4124 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004125 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004126 }
4127 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004128
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004129 nfd = channel_fill_poll_write(nfd, fds);
4130
Bram Moolenaare0874f82016-01-24 20:36:41 +01004131 return nfd;
4132}
4133
4134/*
4135 * The type of "fds" is hidden to avoid problems with the function proto.
4136 */
4137 int
4138channel_poll_check(int ret_in, void *fds_in)
4139{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004140 int ret = ret_in;
4141 channel_T *channel;
4142 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004143 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004144 int idx;
4145 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004146
Bram Moolenaar77073442016-02-13 23:23:53 +01004147 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004148 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004149 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004150 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004151 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004152
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004153 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004154 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004155 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004156 --ret;
4157 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004158 else if (channel->ch_part[part].ch_fd != INVALID_FD
4159 && channel->ch_keep_open)
4160 {
4161 /* polling a keep-open channel */
4162 channel_read(channel, part, "channel_poll_check_keep_open");
4163 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004164 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004165
4166 in_part = &channel->ch_part[PART_IN];
4167 idx = in_part->ch_poll_idx;
4168 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4169 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004170 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004171 --ret;
4172 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004173 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004174
4175 return ret;
4176}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004177# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004178
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004179# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004180
Bram Moolenaare0874f82016-01-24 20:36:41 +01004181/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004182 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004183 */
4184 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004185channel_select_setup(
4186 int maxfd_in,
4187 void *rfds_in,
4188 void *wfds_in,
4189 struct timeval *tv,
4190 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004191{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004192 int maxfd = maxfd_in;
4193 channel_T *channel;
4194 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004195 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004196 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004197
Bram Moolenaar77073442016-02-13 23:23:53 +01004198 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004199 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004200 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004201 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004202 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004203
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004204 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004205 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004206 if (channel->ch_keep_open)
4207 {
4208 /* For unknown reason select() returns immediately for a
4209 * keep-open channel. Instead of adding it to the rfds add
4210 * a short timeout and check, like polling. */
4211 if (*tvp == NULL || tv->tv_sec > 0
4212 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4213 {
4214 *tvp = tv;
4215 tv->tv_sec = 0;
4216 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4217 }
4218 }
4219 else
4220 {
4221 FD_SET((int)fd, rfds);
4222 if (maxfd < (int)fd)
4223 maxfd = (int)fd;
4224 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004225 }
4226 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004227 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004228
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004229 maxfd = channel_fill_wfds(maxfd, wfds);
4230
Bram Moolenaare0874f82016-01-24 20:36:41 +01004231 return maxfd;
4232}
4233
4234/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004235 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004236 */
4237 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004238channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004239{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004240 int ret = ret_in;
4241 channel_T *channel;
4242 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004243 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004244 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004245 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004246
Bram Moolenaar77073442016-02-13 23:23:53 +01004247 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004248 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004249 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004250 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004251 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004252
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004253 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004254 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004255 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004256 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004257 --ret;
4258 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004259 else if (fd != INVALID_FD && channel->ch_keep_open)
4260 {
4261 /* polling a keep-open channel */
4262 channel_read(channel, part, "channel_select_check_keep_open");
4263 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004264 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004265
4266 in_part = &channel->ch_part[PART_IN];
4267 if (ret > 0 && in_part->ch_fd != INVALID_FD
4268 && FD_ISSET(in_part->ch_fd, wfds))
4269 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004270 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004271 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004272 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004273 --ret;
4274 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004275 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004276
4277 return ret;
4278}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004279# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004280
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004281/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004282 * Execute queued up commands.
4283 * Invoked from the main loop when it's safe to execute received commands.
4284 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004285 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004286 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004287channel_parse_messages(void)
4288{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004289 channel_T *channel = first_channel;
4290 int ret = FALSE;
4291 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004292 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004293#ifdef ELAPSED_FUNC
4294 ELAPSED_TYPE start_tv;
4295
4296 ELAPSED_INIT(start_tv);
4297#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004298
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004299 ++safe_to_invoke_callback;
4300
Bram Moolenaard0b65022016-03-06 21:50:33 +01004301 /* Only do this message when another message was given, otherwise we get
4302 * lots of them. */
4303 if (did_log_msg)
4304 {
4305 ch_log(NULL, "looking for messages on channels");
4306 did_log_msg = FALSE;
4307 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004308 while (channel != NULL)
4309 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004310 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004311 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004312 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004313 channel_close_now(channel);
4314 /* channel may have been freed, start over */
4315 channel = first_channel;
4316 continue;
4317 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004318 if (channel->ch_to_be_freed)
4319 {
4320 channel_free(channel);
4321 /* channel has been freed, start over */
4322 channel = first_channel;
4323 continue;
4324 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004325 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004326 {
4327 /* channel is no longer useful, free it */
4328 channel_free(channel);
4329 channel = first_channel;
4330 part = PART_SOCK;
4331 continue;
4332 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004333 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004334 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004335 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004336 /* Increase the refcount, in case the handler causes the channel
4337 * to be unreferenced or closed. */
4338 ++channel->ch_refcount;
4339 r = may_invoke_callback(channel, part);
4340 if (r == OK)
4341 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004342 if (channel_unref(channel) || (r == OK
4343#ifdef ELAPSED_FUNC
4344 /* Limit the time we loop here to 100 msec, otherwise
4345 * Vim becomes unresponsive when the callback takes
4346 * more than a bit of time. */
4347 && ELAPSED_FUNC(start_tv) < 100L
4348#endif
4349 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004350 {
4351 /* channel was freed or something was done, start over */
4352 channel = first_channel;
4353 part = PART_SOCK;
4354 continue;
4355 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004356 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004357 if (part < PART_ERR)
4358 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004359 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004360 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004361 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004362 part = PART_SOCK;
4363 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004364 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004365
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004366 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004367 {
4368 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004369 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004370 }
4371
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004372 --safe_to_invoke_callback;
4373
Bram Moolenaard7ece102016-02-02 23:23:02 +01004374 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004375}
4376
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004377/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004378 * Return TRUE if any channel has readahead. That means we should not block on
4379 * waiting for input.
4380 */
4381 int
4382channel_any_readahead(void)
4383{
4384 channel_T *channel = first_channel;
4385 ch_part_T part = PART_SOCK;
4386
4387 while (channel != NULL)
4388 {
4389 if (channel_has_readahead(channel, part))
4390 return TRUE;
4391 if (part < PART_ERR)
4392 ++part;
4393 else
4394 {
4395 channel = channel->ch_next;
4396 part = PART_SOCK;
4397 }
4398 }
4399 return FALSE;
4400}
4401
4402/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004403 * Mark references to lists used in channels.
4404 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004405 int
4406set_ref_in_channel(int copyID)
4407{
Bram Moolenaar77073442016-02-13 23:23:53 +01004408 int abort = FALSE;
4409 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004410 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004411
Bram Moolenaar77073442016-02-13 23:23:53 +01004412 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004413 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004414 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004415 tv.v_type = VAR_CHANNEL;
4416 tv.vval.v_channel = channel;
4417 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004418 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004419 return abort;
4420}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004421
4422/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004423 * Return the "part" to write to for "channel".
4424 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004425 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004426channel_part_send(channel_T *channel)
4427{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004428 if (channel->CH_SOCK_FD == INVALID_FD)
4429 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004430 return PART_SOCK;
4431}
4432
4433/*
4434 * Return the default "part" to read from for "channel".
4435 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004436 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004437channel_part_read(channel_T *channel)
4438{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004439 if (channel->CH_SOCK_FD == INVALID_FD)
4440 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004441 return PART_SOCK;
4442}
4443
4444/*
4445 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004446 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004447 */
4448 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004449channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004450{
Bram Moolenaar77073442016-02-13 23:23:53 +01004451 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004452 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004453 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004454}
4455
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004456/*
4457 * Return the timeout of "channel"/"part"
4458 */
4459 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004460channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004461{
4462 return channel->ch_part[part].ch_timeout;
4463}
4464
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004465 static int
4466handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4467{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004468 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004469
4470 opt->jo_set |= jo;
4471 if (STRCMP(val, "nl") == 0)
4472 *modep = MODE_NL;
4473 else if (STRCMP(val, "raw") == 0)
4474 *modep = MODE_RAW;
4475 else if (STRCMP(val, "js") == 0)
4476 *modep = MODE_JS;
4477 else if (STRCMP(val, "json") == 0)
4478 *modep = MODE_JSON;
4479 else
4480 {
4481 EMSG2(_(e_invarg2), val);
4482 return FAIL;
4483 }
4484 return OK;
4485}
4486
4487 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004488handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004489{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004490 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491
4492 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4493 if (STRCMP(val, "null") == 0)
4494 opt->jo_io[part] = JIO_NULL;
4495 else if (STRCMP(val, "pipe") == 0)
4496 opt->jo_io[part] = JIO_PIPE;
4497 else if (STRCMP(val, "file") == 0)
4498 opt->jo_io[part] = JIO_FILE;
4499 else if (STRCMP(val, "buffer") == 0)
4500 opt->jo_io[part] = JIO_BUFFER;
4501 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4502 opt->jo_io[part] = JIO_OUT;
4503 else
4504 {
4505 EMSG2(_(e_invarg2), val);
4506 return FAIL;
4507 }
4508 return OK;
4509}
4510
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004511/*
4512 * Clear a jobopt_T before using it.
4513 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004514 void
4515clear_job_options(jobopt_T *opt)
4516{
4517 vim_memset(opt, 0, sizeof(jobopt_T));
4518}
4519
4520/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004521 * Free any members of a jobopt_T.
4522 */
4523 void
4524free_job_options(jobopt_T *opt)
4525{
4526 if (opt->jo_partial != NULL)
4527 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004528 else if (opt->jo_callback != NULL)
4529 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004530 if (opt->jo_out_partial != NULL)
4531 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004532 else if (opt->jo_out_cb != NULL)
4533 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004534 if (opt->jo_err_partial != NULL)
4535 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004536 else if (opt->jo_err_cb != NULL)
4537 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004538 if (opt->jo_close_partial != NULL)
4539 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004540 else if (opt->jo_close_cb != NULL)
4541 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004542 if (opt->jo_exit_partial != NULL)
4543 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004544 else if (opt->jo_exit_cb != NULL)
4545 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004546 if (opt->jo_env != NULL)
4547 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004548}
4549
4550/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 * Get the PART_ number from the first character of an option name.
4552 */
4553 static int
4554part_from_char(int c)
4555{
4556 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4557}
4558
4559/*
4560 * Get the option entries from the dict in "tv", parse them and put the result
4561 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004562 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004563 * If an option value is invalid return FAIL.
4564 */
4565 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004566get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004567{
4568 typval_T *item;
4569 char_u *val;
4570 dict_T *dict;
4571 int todo;
4572 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004573 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004574
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004575 if (tv->v_type == VAR_UNKNOWN)
4576 return OK;
4577 if (tv->v_type != VAR_DICT)
4578 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004579 EMSG(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004580 return FAIL;
4581 }
4582 dict = tv->vval.v_dict;
4583 if (dict == NULL)
4584 return OK;
4585
4586 todo = (int)dict->dv_hashtab.ht_used;
4587 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4588 if (!HASHITEM_EMPTY(hi))
4589 {
4590 item = &dict_lookup(hi)->di_tv;
4591
4592 if (STRCMP(hi->hi_key, "mode") == 0)
4593 {
4594 if (!(supported & JO_MODE))
4595 break;
4596 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4597 return FAIL;
4598 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004599 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004600 {
4601 if (!(supported & JO_IN_MODE))
4602 break;
4603 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4604 == FAIL)
4605 return FAIL;
4606 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004607 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004608 {
4609 if (!(supported & JO_OUT_MODE))
4610 break;
4611 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4612 == FAIL)
4613 return FAIL;
4614 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004615 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004616 {
4617 if (!(supported & JO_ERR_MODE))
4618 break;
4619 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4620 == FAIL)
4621 return FAIL;
4622 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004623 else if (STRCMP(hi->hi_key, "noblock") == 0)
4624 {
4625 if (!(supported & JO_MODE))
4626 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004627 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004628 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004629 else if (STRCMP(hi->hi_key, "in_io") == 0
4630 || STRCMP(hi->hi_key, "out_io") == 0
4631 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004632 {
4633 if (!(supported & JO_OUT_IO))
4634 break;
4635 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4636 return FAIL;
4637 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004638 else if (STRCMP(hi->hi_key, "in_name") == 0
4639 || STRCMP(hi->hi_key, "out_name") == 0
4640 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004641 {
4642 part = part_from_char(*hi->hi_key);
4643
4644 if (!(supported & JO_OUT_IO))
4645 break;
4646 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4647 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004648 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004649 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004650 else if (STRCMP(hi->hi_key, "pty") == 0)
4651 {
4652 if (!(supported & JO_MODE))
4653 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004654 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004655 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004656 else if (STRCMP(hi->hi_key, "in_buf") == 0
4657 || STRCMP(hi->hi_key, "out_buf") == 0
4658 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004659 {
4660 part = part_from_char(*hi->hi_key);
4661
4662 if (!(supported & JO_OUT_IO))
4663 break;
4664 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004665 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004666 if (opt->jo_io_buf[part] <= 0)
4667 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004668 EMSG3(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004669 return FAIL;
4670 }
4671 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4672 {
4673 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4674 return FAIL;
4675 }
4676 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004677 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4678 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4679 {
4680 part = part_from_char(*hi->hi_key);
4681
4682 if (!(supported & JO_OUT_IO))
4683 break;
4684 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004685 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004686 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004687 else if (STRCMP(hi->hi_key, "out_msg") == 0
4688 || STRCMP(hi->hi_key, "err_msg") == 0)
4689 {
4690 part = part_from_char(*hi->hi_key);
4691
4692 if (!(supported & JO_OUT_IO))
4693 break;
4694 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004695 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004696 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004697 else if (STRCMP(hi->hi_key, "in_top") == 0
4698 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004699 {
4700 linenr_T *lp;
4701
4702 if (!(supported & JO_OUT_IO))
4703 break;
4704 if (hi->hi_key[3] == 't')
4705 {
4706 lp = &opt->jo_in_top;
4707 opt->jo_set |= JO_IN_TOP;
4708 }
4709 else
4710 {
4711 lp = &opt->jo_in_bot;
4712 opt->jo_set |= JO_IN_BOT;
4713 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004714 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004715 if (*lp < 0)
4716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004717 EMSG3(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004718 return FAIL;
4719 }
4720 }
4721 else if (STRCMP(hi->hi_key, "channel") == 0)
4722 {
4723 if (!(supported & JO_OUT_IO))
4724 break;
4725 opt->jo_set |= JO_CHANNEL;
4726 if (item->v_type != VAR_CHANNEL)
4727 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004728 EMSG2(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004729 return FAIL;
4730 }
4731 opt->jo_channel = item->vval.v_channel;
4732 }
4733 else if (STRCMP(hi->hi_key, "callback") == 0)
4734 {
4735 if (!(supported & JO_CALLBACK))
4736 break;
4737 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004738 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004739 if (opt->jo_callback == NULL)
4740 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004741 EMSG2(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004742 return FAIL;
4743 }
4744 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004745 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004746 {
4747 if (!(supported & JO_OUT_CALLBACK))
4748 break;
4749 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004750 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004751 if (opt->jo_out_cb == NULL)
4752 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004753 EMSG2(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004754 return FAIL;
4755 }
4756 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004757 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004758 {
4759 if (!(supported & JO_ERR_CALLBACK))
4760 break;
4761 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004762 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 if (opt->jo_err_cb == NULL)
4764 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004765 EMSG2(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004766 return FAIL;
4767 }
4768 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004769 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004770 {
4771 if (!(supported & JO_CLOSE_CALLBACK))
4772 break;
4773 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004774 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004775 if (opt->jo_close_cb == NULL)
4776 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004777 EMSG2(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004778 return FAIL;
4779 }
4780 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004781 else if (STRCMP(hi->hi_key, "drop") == 0)
4782 {
4783 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004784 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004785
4786 if (STRCMP(val, "never") == 0)
4787 never = TRUE;
4788 else if (STRCMP(val, "auto") != 0)
4789 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004790 EMSG3(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004791 return FAIL;
4792 }
4793 opt->jo_drop_never = never;
4794 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004795 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4796 {
4797 if (!(supported & JO_EXIT_CB))
4798 break;
4799 opt->jo_set |= JO_EXIT_CB;
4800 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4801 if (opt->jo_exit_cb == NULL)
4802 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004803 EMSG2(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004804 return FAIL;
4805 }
4806 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004807#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004808 else if (STRCMP(hi->hi_key, "term_name") == 0)
4809 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004810 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004811 break;
4812 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004813 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004814 if (opt->jo_term_name == NULL)
4815 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004816 EMSG2(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004817 return FAIL;
4818 }
4819 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004820 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4821 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004822 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004823 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004824 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004825 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4826 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004827 EMSG3(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004828 return FAIL;
4829 }
4830 opt->jo_set2 |= JO2_TERM_FINISH;
4831 opt->jo_term_finish = *val;
4832 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004833 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4834 {
4835 char_u *p;
4836
4837 if (!(supported2 & JO2_TERM_OPENCMD))
4838 break;
4839 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004840 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004841 if (p != NULL)
4842 {
4843 /* Must have %d and no other %. */
4844 p = vim_strchr(p, '%');
4845 if (p != NULL && (p[1] != 'd'
4846 || vim_strchr(p + 2, '%') != NULL))
4847 p = NULL;
4848 }
4849 if (p == NULL)
4850 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004851 EMSG2(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004852 return FAIL;
4853 }
4854 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004855 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4856 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004857 char_u *p;
4858
4859 if (!(supported2 & JO2_EOF_CHARS))
4860 break;
4861 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004862 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004863 if (p == NULL)
4864 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004865 EMSG2(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004866 return FAIL;
4867 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004868 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004869 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4870 {
4871 if (!(supported2 & JO2_TERM_ROWS))
4872 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004873 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004874 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004875 }
4876 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4877 {
4878 if (!(supported2 & JO2_TERM_COLS))
4879 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004880 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004881 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004882 }
4883 else if (STRCMP(hi->hi_key, "vertical") == 0)
4884 {
4885 if (!(supported2 & JO2_VERTICAL))
4886 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004887 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004888 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004889 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004890 else if (STRCMP(hi->hi_key, "curwin") == 0)
4891 {
4892 if (!(supported2 & JO2_CURWIN))
4893 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004894 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004895 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004896 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004897 else if (STRCMP(hi->hi_key, "hidden") == 0)
4898 {
4899 if (!(supported2 & JO2_HIDDEN))
4900 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004901 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004902 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004903 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004904 else if (STRCMP(hi->hi_key, "norestore") == 0)
4905 {
4906 if (!(supported2 & JO2_NORESTORE))
4907 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004908 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004909 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004910 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004911 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4912 {
4913 if (!(supported2 & JO2_TERM_KILL))
4914 break;
4915 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004916 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004917 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004918# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4919 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004921 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004922 listitem_T *li;
4923 long_u rgb[16];
4924
4925 if (!(supported2 & JO2_ANSI_COLORS))
4926 break;
4927
4928 if (item == NULL || item->v_type != VAR_LIST
4929 || item->vval.v_list == NULL)
4930 {
4931 EMSG2(_(e_invargval), "ansi_colors");
4932 return FAIL;
4933 }
4934
4935 li = item->vval.v_list->lv_first;
4936 for (; li != NULL && n < 16; li = li->li_next, n++)
4937 {
4938 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004940
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004941 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004942 if (color_name == NULL)
4943 return FAIL;
4944
4945 guicolor = GUI_GET_COLOR(color_name);
4946 if (guicolor == INVALCOLOR)
4947 return FAIL;
4948
4949 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4950 }
4951
4952 if (n != 16 || li != NULL)
4953 {
4954 EMSG2(_(e_invargval), "ansi_colors");
4955 return FAIL;
4956 }
4957
4958 opt->jo_set2 |= JO2_ANSI_COLORS;
4959 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4960 }
4961# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004962#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004963 else if (STRCMP(hi->hi_key, "env") == 0)
4964 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004965 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004966 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004967 if (item->v_type != VAR_DICT)
4968 {
4969 EMSG2(_(e_invargval), "env");
4970 return FAIL;
4971 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004972 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004973 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004974 if (opt->jo_env != NULL)
4975 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004976 }
4977 else if (STRCMP(hi->hi_key, "cwd") == 0)
4978 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004979 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004980 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02004982 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02004983#ifndef WIN32 // Win32 directories don't have the concept of "executable"
4984 || mch_access((char *)opt->jo_cwd, X_OK) != 0
4985#endif
4986 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004987 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004988 EMSG2(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004989 return FAIL;
4990 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004991 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004992 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004993 else if (STRCMP(hi->hi_key, "waittime") == 0)
4994 {
4995 if (!(supported & JO_WAITTIME))
4996 break;
4997 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004998 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004999 }
5000 else if (STRCMP(hi->hi_key, "timeout") == 0)
5001 {
5002 if (!(supported & JO_TIMEOUT))
5003 break;
5004 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005005 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005006 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005007 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005008 {
5009 if (!(supported & JO_OUT_TIMEOUT))
5010 break;
5011 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005012 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005013 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005014 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005015 {
5016 if (!(supported & JO_ERR_TIMEOUT))
5017 break;
5018 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005019 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005020 }
5021 else if (STRCMP(hi->hi_key, "part") == 0)
5022 {
5023 if (!(supported & JO_PART))
5024 break;
5025 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005026 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005027 if (STRCMP(val, "err") == 0)
5028 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005029 else if (STRCMP(val, "out") == 0)
5030 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005031 else
5032 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01005033 EMSG3(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005034 return FAIL;
5035 }
5036 }
5037 else if (STRCMP(hi->hi_key, "id") == 0)
5038 {
5039 if (!(supported & JO_ID))
5040 break;
5041 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005042 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005043 }
5044 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5045 {
5046 if (!(supported & JO_STOPONEXIT))
5047 break;
5048 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005049 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005050 opt->jo_soe_buf);
5051 if (opt->jo_stoponexit == NULL)
5052 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01005053 EMSG2(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005054 return FAIL;
5055 }
5056 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005057 else if (STRCMP(hi->hi_key, "block_write") == 0)
5058 {
5059 if (!(supported & JO_BLOCK_WRITE))
5060 break;
5061 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005062 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005063 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005064 else
5065 break;
5066 --todo;
5067 }
5068 if (todo > 0)
5069 {
5070 EMSG2(_(e_invarg2), hi->hi_key);
5071 return FAIL;
5072 }
5073
5074 return OK;
5075}
5076
5077/*
5078 * Get the channel from the argument.
5079 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005080 * When "check_open" is TRUE check that the channel can be used.
5081 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005082 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005083 */
5084 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005085get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005086{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005087 channel_T *channel = NULL;
5088 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005089
5090 if (tv->v_type == VAR_JOB)
5091 {
5092 if (tv->vval.v_job != NULL)
5093 channel = tv->vval.v_job->jv_channel;
5094 }
5095 else if (tv->v_type == VAR_CHANNEL)
5096 {
5097 channel = tv->vval.v_channel;
5098 }
5099 else
5100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005101 EMSG2(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005102 return NULL;
5103 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005104 if (channel != NULL && reading)
5105 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005106 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005107
Bram Moolenaar437905c2016-04-26 19:01:05 +02005108 if (check_open && (channel == NULL || (!channel_is_open(channel)
5109 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005110 {
5111 EMSG(_("E906: not an open channel"));
5112 return NULL;
5113 }
5114 return channel;
5115}
5116
5117static job_T *first_job = NULL;
5118
5119 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005120job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005121{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005122 int i;
5123
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005124 ch_log(job->jv_channel, "Freeing job");
5125 if (job->jv_channel != NULL)
5126 {
5127 /* The link from the channel to the job doesn't count as a reference,
5128 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005129 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005130 * NULL the reference. We don't set ch_job_killed, unreferencing the
5131 * job doesn't mean it stops running. */
5132 job->jv_channel->ch_job = NULL;
5133 channel_unref(job->jv_channel);
5134 }
5135 mch_clear_job(job);
5136
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005137 vim_free(job->jv_tty_in);
5138 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005139 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005140 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005141 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005142 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005143 for (i = 0; job->jv_argv[i] != NULL; i++)
5144 vim_free(job->jv_argv[i]);
5145 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005146 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005147}
5148
5149 static void
5150job_free_job(job_T *job)
5151{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005152 if (job->jv_next != NULL)
5153 job->jv_next->jv_prev = job->jv_prev;
5154 if (job->jv_prev == NULL)
5155 first_job = job->jv_next;
5156 else
5157 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005158 vim_free(job);
5159}
5160
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005161 static void
5162job_free(job_T *job)
5163{
5164 if (!in_free_unref_items)
5165 {
5166 job_free_contents(job);
5167 job_free_job(job);
5168 }
5169}
5170
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005171#if defined(EXITFREE) || defined(PROTO)
5172 void
5173job_free_all(void)
5174{
5175 while (first_job != NULL)
5176 job_free(first_job);
5177}
5178#endif
5179
5180/*
5181 * Return TRUE if we need to check if the process of "job" has ended.
5182 */
5183 static int
5184job_need_end_check(job_T *job)
5185{
5186 return job->jv_status == JOB_STARTED
5187 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5188}
5189
5190/*
5191 * Return TRUE if the channel of "job" is still useful.
5192 */
5193 static int
5194job_channel_still_useful(job_T *job)
5195{
5196 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5197}
5198
5199/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005200 * Return TRUE if the channel of "job" is closeable.
5201 */
5202 static int
5203job_channel_can_close(job_T *job)
5204{
5205 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5206}
5207
5208/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005209 * Return TRUE if the job should not be freed yet. Do not free the job when
5210 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5211 * or when the associated channel will do something with the job output.
5212 */
5213 static int
5214job_still_useful(job_T *job)
5215{
5216 return job_need_end_check(job) || job_channel_still_useful(job);
5217}
5218
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005219#if defined(GUI_MAY_FORK) || defined(PROTO)
5220/*
5221 * Return TRUE when there is any running job that we care about.
5222 */
5223 int
5224job_any_running()
5225{
5226 job_T *job;
5227
5228 for (job = first_job; job != NULL; job = job->jv_next)
5229 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005230 {
5231 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005232 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005233 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005234 return FALSE;
5235}
5236#endif
5237
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005238#if !defined(USE_ARGV) || defined(PROTO)
5239/*
5240 * Escape one argument for an external command.
5241 * Returns the escaped string in allocated memory. NULL when out of memory.
5242 */
5243 static char_u *
5244win32_escape_arg(char_u *arg)
5245{
5246 int slen, dlen;
5247 int escaping = 0;
5248 int i;
5249 char_u *s, *d;
5250 char_u *escaped_arg;
5251 int has_spaces = FALSE;
5252
5253 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005254 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005255 dlen = slen;
5256 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5257 {
5258 if (*s == '"' || *s == '\\')
5259 ++dlen;
5260 if (*s == ' ' || *s == '\t')
5261 has_spaces = TRUE;
5262 }
5263
5264 if (has_spaces)
5265 dlen += 2;
5266
5267 if (dlen == slen)
5268 return vim_strsave(arg);
5269
5270 /* Allocate memory for the result and fill it. */
5271 escaped_arg = alloc(dlen + 1);
5272 if (escaped_arg == NULL)
5273 return NULL;
5274 memset(escaped_arg, 0, dlen+1);
5275
5276 d = escaped_arg;
5277
5278 if (has_spaces)
5279 *d++ = '"';
5280
5281 for (s = arg; *s != NUL;)
5282 {
5283 switch (*s)
5284 {
5285 case '"':
5286 for (i = 0; i < escaping; i++)
5287 *d++ = '\\';
5288 escaping = 0;
5289 *d++ = '\\';
5290 *d++ = *s++;
5291 break;
5292 case '\\':
5293 escaping++;
5294 *d++ = *s++;
5295 break;
5296 default:
5297 escaping = 0;
5298 MB_COPY_CHAR(s, d);
5299 break;
5300 }
5301 }
5302
5303 /* add terminating quote and finish with a NUL */
5304 if (has_spaces)
5305 {
5306 for (i = 0; i < escaping; i++)
5307 *d++ = '\\';
5308 *d++ = '"';
5309 }
5310 *d = NUL;
5311
5312 return escaped_arg;
5313}
5314
5315/*
5316 * Build a command line from a list, taking care of escaping.
5317 * The result is put in gap->ga_data.
5318 * Returns FAIL when out of memory.
5319 */
5320 int
5321win32_build_cmd(list_T *l, garray_T *gap)
5322{
5323 listitem_T *li;
5324 char_u *s;
5325
5326 for (li = l->lv_first; li != NULL; li = li->li_next)
5327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005328 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005329 if (s == NULL)
5330 return FAIL;
5331 s = win32_escape_arg(s);
5332 if (s == NULL)
5333 return FAIL;
5334 ga_concat(gap, s);
5335 vim_free(s);
5336 if (li->li_next != NULL)
5337 ga_append(gap, ' ');
5338 }
5339 return OK;
5340}
5341#endif
5342
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005343/*
5344 * NOTE: Must call job_cleanup() only once right after the status of "job"
5345 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5346 * mch_detect_ended_job() returned non-NULL).
5347 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005348 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005349job_cleanup(job_T *job)
5350{
5351 if (job->jv_status != JOB_ENDED)
5352 return;
5353
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005354 /* Ready to cleanup the job. */
5355 job->jv_status = JOB_FINISHED;
5356
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005357 /* When only channel-in is kept open, close explicitly. */
5358 if (job->jv_channel != NULL)
5359 ch_close_part(job->jv_channel, PART_IN);
5360
Bram Moolenaar97792de2016-10-15 18:36:49 +02005361 if (job->jv_exit_cb != NULL)
5362 {
5363 typval_T argv[3];
5364 typval_T rettv;
5365 int dummy;
5366
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005367 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005368 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005369 ++job->jv_refcount;
5370 argv[0].v_type = VAR_JOB;
5371 argv[0].vval.v_job = job;
5372 argv[1].v_type = VAR_NUMBER;
5373 argv[1].vval.v_number = job->jv_exitval;
5374 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5375 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5376 job->jv_exit_partial, NULL);
5377 clear_tv(&rettv);
5378 --job->jv_refcount;
5379 channel_need_redraw = TRUE;
5380 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005381
5382 /* Do not free the job in case the close callback of the associated channel
5383 * isn't invoked yet and may get information by job_info(). */
5384 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005385 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005386 /* The job was already unreferenced and the associated channel was
5387 * detached, now that it ended it can be freed. Careful: caller must
5388 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005389 job_free(job);
5390 }
5391}
5392
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005393/*
5394 * Mark references in jobs that are still useful.
5395 */
5396 int
5397set_ref_in_job(int copyID)
5398{
5399 int abort = FALSE;
5400 job_T *job;
5401 typval_T tv;
5402
5403 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005404 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005405 {
5406 tv.v_type = VAR_JOB;
5407 tv.vval.v_job = job;
5408 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5409 }
5410 return abort;
5411}
5412
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005413/*
5414 * Dereference "job". Note that after this "job" may have been freed.
5415 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005416 void
5417job_unref(job_T *job)
5418{
5419 if (job != NULL && --job->jv_refcount <= 0)
5420 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005421 /* Do not free the job if there is a channel where the close callback
5422 * may get the job info. */
5423 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005424 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005425 /* Do not free the job when it has not ended yet and there is a
5426 * "stoponexit" flag or an exit callback. */
5427 if (!job_need_end_check(job))
5428 {
5429 job_free(job);
5430 }
5431 else if (job->jv_channel != NULL)
5432 {
5433 /* Do remove the link to the channel, otherwise it hangs
5434 * around until Vim exits. See job_free() for refcount. */
5435 ch_log(job->jv_channel, "detaching channel from job");
5436 job->jv_channel->ch_job = NULL;
5437 channel_unref(job->jv_channel);
5438 job->jv_channel = NULL;
5439 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005440 }
5441 }
5442}
5443
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005444 int
5445free_unused_jobs_contents(int copyID, int mask)
5446{
5447 int did_free = FALSE;
5448 job_T *job;
5449
5450 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005451 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005452 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005453 {
5454 /* Free the channel and ordinary items it contains, but don't
5455 * recurse into Lists, Dictionaries etc. */
5456 job_free_contents(job);
5457 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005458 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005459 return did_free;
5460}
5461
5462 void
5463free_unused_jobs(int copyID, int mask)
5464{
5465 job_T *job;
5466 job_T *job_next;
5467
5468 for (job = first_job; job != NULL; job = job_next)
5469 {
5470 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005471 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005472 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005473 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005474 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005475 job_free_job(job);
5476 }
5477 }
5478}
5479
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005480/*
5481 * Allocate a job. Sets the refcount to one and sets options default.
5482 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005483 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005484job_alloc(void)
5485{
5486 job_T *job;
5487
5488 job = (job_T *)alloc_clear(sizeof(job_T));
5489 if (job != NULL)
5490 {
5491 job->jv_refcount = 1;
5492 job->jv_stoponexit = vim_strsave((char_u *)"term");
5493
5494 if (first_job != NULL)
5495 {
5496 first_job->jv_prev = job;
5497 job->jv_next = first_job;
5498 }
5499 first_job = job;
5500 }
5501 return job;
5502}
5503
5504 void
5505job_set_options(job_T *job, jobopt_T *opt)
5506{
5507 if (opt->jo_set & JO_STOPONEXIT)
5508 {
5509 vim_free(job->jv_stoponexit);
5510 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5511 job->jv_stoponexit = NULL;
5512 else
5513 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5514 }
5515 if (opt->jo_set & JO_EXIT_CB)
5516 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005517 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005518 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005519 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005520 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005521 job->jv_exit_partial = NULL;
5522 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005523 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005524 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005525 job->jv_exit_partial = opt->jo_exit_partial;
5526 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005527 {
5528 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005529 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005530 }
5531 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005532 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005533 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005534 func_ref(job->jv_exit_cb);
5535 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005536 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005537 }
5538}
5539
5540/*
5541 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5542 */
5543 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005544job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005545{
5546 job_T *job;
5547
5548 for (job = first_job; job != NULL; job = job->jv_next)
5549 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005550 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005551}
5552
5553/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005554 * Return TRUE when there is any job that has an exit callback and might exit,
5555 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005556 */
5557 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005558has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005559{
5560 job_T *job;
5561
5562 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005563 /* Only should check if the channel has been closed, if the channel is
5564 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005565 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5566 || (job->jv_status == JOB_FINISHED
5567 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005568 return TRUE;
5569 return FALSE;
5570}
5571
Bram Moolenaar97792de2016-10-15 18:36:49 +02005572#define MAX_CHECK_ENDED 8
5573
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005574/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005575 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005576 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005577 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005578 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005579job_check_ended(void)
5580{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005581 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005582 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005583
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005584 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005585 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005586 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005587
Bram Moolenaar97792de2016-10-15 18:36:49 +02005588 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005589 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005590 // NOTE: mch_detect_ended_job() must only return a job of which the
5591 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005592 job_T *job = mch_detect_ended_job(first_job);
5593
5594 if (job == NULL)
5595 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005596 did_end = TRUE;
5597 job_cleanup(job); // may free "job"
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005598 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005599
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005600 if (channel_need_redraw)
5601 {
5602 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005603 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005604 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005605 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005606}
5607
5608/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005609 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005610 * "argv_arg" is only for Unix.
5611 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005612 * The returned job has a refcount of one.
5613 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005614 */
5615 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005616job_start(
5617 typval_T *argvars,
5618 char **argv_arg,
5619 jobopt_T *opt_arg,
5620 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005621{
5622 job_T *job;
5623 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005624 char **argv = NULL;
5625 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005626#if defined(UNIX)
5627# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005628 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005629#else
5630 garray_T ga;
5631#endif
5632 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005633 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005634
5635 job = job_alloc();
5636 if (job == NULL)
5637 return NULL;
5638
5639 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005640#ifndef USE_ARGV
5641 ga_init2(&ga, (int)sizeof(char*), 20);
5642#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005643
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005644 if (opt_arg != NULL)
5645 opt = *opt_arg;
5646 else
5647 {
5648 /* Default mode is NL. */
5649 clear_job_options(&opt);
5650 opt.jo_mode = MODE_NL;
5651 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005652 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5653 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5654 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005655 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005656 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005657
5658 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005659 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005660 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5661 && opt.jo_io[part] == JIO_FILE
5662 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5663 || *opt.jo_io_name[part] == NUL))
5664 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005665 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005666 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005667 }
5668
5669 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5670 {
5671 buf_T *buf = NULL;
5672
5673 /* check that we can find the buffer before starting the job */
5674 if (opt.jo_set & JO_IN_BUF)
5675 {
5676 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5677 if (buf == NULL)
5678 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5679 }
5680 else if (!(opt.jo_set & JO_IN_NAME))
5681 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005682 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005683 }
5684 else
5685 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5686 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005687 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005688 if (buf->b_ml.ml_mfp == NULL)
5689 {
5690 char_u numbuf[NUMBUFLEN];
5691 char_u *s;
5692
5693 if (opt.jo_set & JO_IN_BUF)
5694 {
5695 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5696 s = numbuf;
5697 }
5698 else
5699 s = opt.jo_io_name[PART_IN];
5700 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005701 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005702 }
5703 job->jv_in_buf = buf;
5704 }
5705
5706 job_set_options(job, &opt);
5707
Bram Moolenaar13568252018-03-16 20:46:58 +01005708#ifdef USE_ARGV
5709 if (argv_arg != NULL)
5710 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005711 /* Make a copy of argv_arg for job->jv_argv. */
5712 for (i = 0; argv_arg[i] != NULL; i++)
5713 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005714 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005715 if (argv == NULL)
5716 goto theend;
5717 for (i = 0; i < argc; i++)
5718 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5719 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005720 }
5721 else
5722#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005723 if (argvars[0].v_type == VAR_STRING)
5724 {
5725 /* Command is a string. */
5726 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005727 if (cmd == NULL || *cmd == NUL)
5728 {
5729 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005730 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005731 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005732
5733 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005734 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005735 }
5736 else if (argvars[0].v_type != VAR_LIST
5737 || argvars[0].vval.v_list == NULL
5738 || argvars[0].vval.v_list->lv_len < 1)
5739 {
5740 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005741 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005742 }
5743 else
5744 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005745 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005746
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005747 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005748 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005749#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005750 if (win32_build_cmd(l, &ga) == FAIL)
5751 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005752 cmd = ga.ga_data;
5753#endif
5754 }
5755
Bram Moolenaar20608922018-04-21 22:30:08 +02005756 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005757 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005758
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005759#ifdef USE_ARGV
5760 if (ch_log_active())
5761 {
5762 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005763
5764 ga_init2(&ga, (int)sizeof(char), 200);
5765 for (i = 0; i < argc; ++i)
5766 {
5767 if (i > 0)
5768 ga_concat(&ga, (char_u *)" ");
5769 ga_concat(&ga, (char_u *)argv[i]);
5770 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005771 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005772 ga_clear(&ga);
5773 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005774 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005775#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005776 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005777 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005778#endif
5779
5780 /* If the channel is reading from a buffer, write lines now. */
5781 if (job->jv_channel != NULL)
5782 channel_write_in(job->jv_channel);
5783
5784theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005785#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005786 vim_free(ga.ga_data);
5787#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005788 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005789 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005790 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005791 return job;
5792}
5793
5794/*
5795 * Get the status of "job" and invoke the exit callback when needed.
5796 * The returned string is not allocated.
5797 */
5798 char *
5799job_status(job_T *job)
5800{
5801 char *result;
5802
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005803 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005804 /* No need to check, dead is dead. */
5805 result = "dead";
5806 else if (job->jv_status == JOB_FAILED)
5807 result = "fail";
5808 else
5809 {
5810 result = mch_job_status(job);
5811 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005812 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005813 }
5814 return result;
5815}
5816
Bram Moolenaar8950a562016-03-12 15:22:55 +01005817/*
5818 * Implementation of job_info().
5819 */
5820 void
5821job_info(job_T *job, dict_T *dict)
5822{
5823 dictitem_T *item;
5824 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005825 list_T *l;
5826 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005827
Bram Moolenaare0be1672018-07-08 16:50:37 +02005828 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005829
5830 item = dictitem_alloc((char_u *)"channel");
5831 if (item == NULL)
5832 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005833 item->di_tv.v_type = VAR_CHANNEL;
5834 item->di_tv.vval.v_channel = job->jv_channel;
5835 if (job->jv_channel != NULL)
5836 ++job->jv_channel->ch_refcount;
5837 if (dict_add(dict, item) == FAIL)
5838 dictitem_free(item);
5839
5840#ifdef UNIX
5841 nr = job->jv_pid;
5842#else
5843 nr = job->jv_proc_info.dwProcessId;
5844#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005845 dict_add_number(dict, "process", nr);
5846 dict_add_string(dict, "tty_in", job->jv_tty_in);
5847 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005848
Bram Moolenaare0be1672018-07-08 16:50:37 +02005849 dict_add_number(dict, "exitval", job->jv_exitval);
5850 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5851 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005852
5853 l = list_alloc();
5854 if (l != NULL)
5855 {
5856 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005857 if (job->jv_argv != NULL)
5858 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005859 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005860 }
5861}
5862
5863/*
5864 * Implementation of job_info() to return info for all jobs.
5865 */
5866 void
5867job_info_all(list_T *l)
5868{
5869 job_T *job;
5870 typval_T tv;
5871
5872 for (job = first_job; job != NULL; job = job->jv_next)
5873 {
5874 tv.v_type = VAR_JOB;
5875 tv.vval.v_job = job;
5876
5877 if (list_append_tv(l, &tv) != OK)
5878 return;
5879 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005880}
5881
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005882/*
5883 * Send a signal to "job". Implements job_stop().
5884 * When "type" is not NULL use this for the type.
5885 * Otherwise use argvars[1] for the type.
5886 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005887 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005888job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005889{
5890 char_u *arg;
5891
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005892 if (type != NULL)
5893 arg = (char_u *)type;
5894 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005895 arg = (char_u *)"";
5896 else
5897 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005898 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005899 if (arg == NULL)
5900 {
5901 EMSG(_(e_invarg));
5902 return 0;
5903 }
5904 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005905 if (job->jv_status == JOB_FAILED)
5906 {
5907 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5908 return 0;
5909 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005910 if (job->jv_status == JOB_ENDED)
5911 {
5912 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5913 return 0;
5914 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005915 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005916 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005917 return 0;
5918
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005919 /* Assume that only "kill" will kill the job. */
5920 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005921 job->jv_channel->ch_job_killed = TRUE;
5922
5923 /* We don't try freeing the job, obviously the caller still has a
5924 * reference to it. */
5925 return 1;
5926}
5927
Bram Moolenaarf2732452018-06-03 14:47:35 +02005928 void
5929invoke_prompt_callback(void)
5930{
5931 typval_T rettv;
5932 int dummy;
5933 typval_T argv[2];
5934 char_u *text;
5935 char_u *prompt;
5936 linenr_T lnum = curbuf->b_ml.ml_line_count;
5937
5938 // Add a new line for the prompt before invoking the callback, so that
5939 // text can always be inserted above the last line.
5940 ml_append(lnum, (char_u *)"", 0, FALSE);
5941 curwin->w_cursor.lnum = lnum + 1;
5942 curwin->w_cursor.col = 0;
5943
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005944 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005945 return;
5946 text = ml_get(lnum);
5947 prompt = prompt_text();
5948 if (STRLEN(text) >= STRLEN(prompt))
5949 text += STRLEN(prompt);
5950 argv[0].v_type = VAR_STRING;
5951 argv[0].vval.v_string = vim_strsave(text);
5952 argv[1].v_type = VAR_UNKNOWN;
5953
5954 call_func(curbuf->b_prompt_callback,
5955 (int)STRLEN(curbuf->b_prompt_callback),
5956 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5957 curbuf->b_prompt_partial, NULL);
5958 clear_tv(&argv[0]);
5959 clear_tv(&rettv);
5960}
5961
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005962/*
5963 * Return TRUE when the interrupt callback was invoked.
5964 */
5965 int
5966invoke_prompt_interrupt(void)
5967{
5968 typval_T rettv;
5969 int dummy;
5970 typval_T argv[1];
5971
5972 if (curbuf->b_prompt_interrupt == NULL
5973 || *curbuf->b_prompt_interrupt == NUL)
5974 return FALSE;
5975 argv[0].v_type = VAR_UNKNOWN;
5976
5977 got_int = FALSE; // don't skip executing commands
5978 call_func(curbuf->b_prompt_interrupt,
5979 (int)STRLEN(curbuf->b_prompt_interrupt),
5980 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5981 curbuf->b_prompt_int_partial, NULL);
5982 clear_tv(&rettv);
5983 return TRUE;
5984}
5985
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005986#endif /* FEAT_JOB_CHANNEL */