blob: 6f5bf20234e608fd50f5f338464292161c2b20ec [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 Moolenaardc0ccae2016-10-09 17:28:01 +02001668channel_get(channel_T *channel, ch_part_T part)
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 Moolenaar19d2f152016-02-01 21:38:19 +01001676 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001677 p = node->rq_buffer;
1678 head->rq_next = node->rq_next;
1679 if (node->rq_next == NULL)
1680 head->rq_prev = NULL;
1681 else
1682 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001683 vim_free(node);
1684 return p;
1685}
1686
1687/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001688 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001689 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001690 */
1691 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001692channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001693{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001694 readq_T *head = &channel->ch_part[part].ch_head;
1695 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001696 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001697 char_u *res;
1698 char_u *p;
1699
1700 /* If there is only one buffer just get that one. */
1701 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1702 return channel_get(channel, part);
1703
1704 /* Concatenate everything into one buffer. */
1705 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001706 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001707 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001708 if (res == NULL)
1709 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001710 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001711 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001712 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001713 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001714 p += node->rq_buflen;
1715 }
1716 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001717
1718 /* Free all buffers */
1719 do
1720 {
1721 p = channel_get(channel, part);
1722 vim_free(p);
1723 } while (p != NULL);
1724
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001725 /* turn all NUL into NL */
1726 while (len > 0)
1727 {
1728 --len;
1729 if (res[len] == NUL)
1730 res[len] = NL;
1731 }
1732
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001733 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001734}
1735
1736/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001737 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001738 * Caller must check these bytes are available.
1739 */
1740 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001741channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001742{
1743 readq_T *head = &channel->ch_part[part].ch_head;
1744 readq_T *node = head->rq_next;
1745 char_u *buf = node->rq_buffer;
1746
1747 mch_memmove(buf, buf + len, node->rq_buflen - len);
1748 node->rq_buflen -= len;
1749}
1750
1751/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001752 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001753 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001754 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001755 */
1756 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001757channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001758{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001759 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001760 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001761 readq_T *last_node;
1762 readq_T *n;
1763 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001764 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001765 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001766
Bram Moolenaar77073442016-02-13 23:23:53 +01001767 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001768 return FAIL;
1769
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001770 last_node = node->rq_next;
1771 len = node->rq_buflen + last_node->rq_buflen + 1;
1772 if (want_nl)
1773 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001774 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001775 {
1776 last_node = last_node->rq_next;
1777 len += last_node->rq_buflen;
1778 }
1779
1780 p = newbuf = alloc(len);
1781 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001782 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001783 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001784 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001785 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001786 node->rq_buffer = newbuf;
1787 for (n = node; n != last_node; )
1788 {
1789 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001790 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001791 p += n->rq_buflen;
1792 vim_free(n->rq_buffer);
1793 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001794 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001795
1796 /* dispose of the collapsed nodes and their buffers */
1797 for (n = node->rq_next; n != last_node; )
1798 {
1799 n = n->rq_next;
1800 vim_free(n->rq_prev);
1801 }
1802 node->rq_next = last_node->rq_next;
1803 if (last_node->rq_next == NULL)
1804 head->rq_prev = node;
1805 else
1806 last_node->rq_next->rq_prev = node;
1807 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001808 return OK;
1809}
1810
1811/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001812 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001813 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001814 * Returns OK or FAIL.
1815 */
1816 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001817channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001818 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001819{
1820 readq_T *node;
1821 readq_T *head = &channel->ch_part[part].ch_head;
1822 char_u *p;
1823 int i;
1824
1825 node = (readq_T *)alloc(sizeof(readq_T));
1826 if (node == NULL)
1827 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001828 /* A NUL is added at the end, because netbeans code expects that.
1829 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001830 node->rq_buffer = alloc(len + 1);
1831 if (node->rq_buffer == NULL)
1832 {
1833 vim_free(node);
1834 return FAIL; /* out of memory */
1835 }
1836
1837 if (channel->ch_part[part].ch_mode == MODE_NL)
1838 {
1839 /* Drop any CR before a NL. */
1840 p = node->rq_buffer;
1841 for (i = 0; i < len; ++i)
1842 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1843 *p++ = buf[i];
1844 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001845 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001846 }
1847 else
1848 {
1849 mch_memmove(node->rq_buffer, buf, len);
1850 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001851 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001852 }
1853
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001854 if (prepend)
1855 {
1856 /* preend node to the head of the queue */
1857 node->rq_next = head->rq_next;
1858 node->rq_prev = NULL;
1859 if (head->rq_next == NULL)
1860 head->rq_prev = node;
1861 else
1862 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001864 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001865 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001866 {
1867 /* append node to the tail of the queue */
1868 node->rq_next = NULL;
1869 node->rq_prev = head->rq_prev;
1870 if (head->rq_prev == NULL)
1871 head->rq_next = node;
1872 else
1873 head->rq_prev->rq_next = node;
1874 head->rq_prev = node;
1875 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001876
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001877 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001878 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001879 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001880 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001881 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001882 fprintf(log_fd, "'\n");
1883 }
1884 return OK;
1885}
1886
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001887/*
1888 * Try to fill the buffer of "reader".
1889 * Returns FALSE when nothing was added.
1890 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001891 static int
1892channel_fill(js_read_T *reader)
1893{
1894 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001895 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001896 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001897 int keeplen;
1898 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001899 char_u *p;
1900
1901 if (next == NULL)
1902 return FALSE;
1903
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001904 keeplen = reader->js_end - reader->js_buf;
1905 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001906 {
1907 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001908 addlen = (int)STRLEN(next);
1909 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 if (p == NULL)
1911 {
1912 vim_free(next);
1913 return FALSE;
1914 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001915 mch_memmove(p, reader->js_buf, keeplen);
1916 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001917 vim_free(next);
1918 next = p;
1919 }
1920
1921 vim_free(reader->js_buf);
1922 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001923 return TRUE;
1924}
1925
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001926/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001927 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001928 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001929 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001930 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001931 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001932channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001933{
1934 js_read_T reader;
1935 typval_T listtv;
1936 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001937 chanpart_T *chanpart = &channel->ch_part[part];
1938 jsonq_T *head = &chanpart->ch_json_head;
1939 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001940 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001941
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001942 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001943 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001944
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001945 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001946 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001947 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001948 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001949 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001950
1951 /* When a message is incomplete we wait for a short while for more to
1952 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001953 * or list will make us hang.
1954 * Do not generate error messages, they will be written in a channel log. */
1955 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001956 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001957 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001958 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001959 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001960 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001961 /* Only accept the response when it is a list with at least two
1962 * items. */
1963 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001964 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001965 if (listtv.v_type != VAR_LIST)
1966 ch_error(channel, "Did not receive a list, discarding");
1967 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001968 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001970 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001971 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001972 else
1973 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001974 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1975 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001976 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001977 else
1978 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001979 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001980 item->jq_value = alloc_tv();
1981 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001982 {
1983 vim_free(item);
1984 clear_tv(&listtv);
1985 }
1986 else
1987 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001988 *item->jq_value = listtv;
1989 item->jq_prev = head->jq_prev;
1990 head->jq_prev = item;
1991 item->jq_next = NULL;
1992 if (item->jq_prev == NULL)
1993 head->jq_next = item;
1994 else
1995 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001996 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001997 }
1998 }
1999 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002000
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002001 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002002 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002003 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002004 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002005 size_t buflen = STRLEN(reader.js_buf);
2006
2007 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002008 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002009 /* First time encountering incomplete message or after receiving
2010 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002011 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002012 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002013 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002014 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002015 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002016#ifdef WIN32
2017 chanpart->ch_deadline = GetTickCount() + 100L;
2018#else
2019 gettimeofday(&chanpart->ch_deadline, NULL);
2020 chanpart->ch_deadline.tv_usec += 100 * 1000;
2021 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2022 {
2023 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2024 ++chanpart->ch_deadline.tv_sec;
2025 }
2026#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002027 }
2028 else
2029 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002030 int timeout;
2031#ifdef WIN32
2032 timeout = GetTickCount() > chanpart->ch_deadline;
2033#else
2034 {
2035 struct timeval now_tv;
2036
2037 gettimeofday(&now_tv, NULL);
2038 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2039 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2040 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2041 }
2042#endif
2043 if (timeout)
2044 {
2045 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002046 chanpart->ch_wait_len = 0;
2047 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002048 }
2049 else
2050 {
2051 reader.js_used = 0;
2052 ch_log(channel, "still waiting on incomplete message");
2053 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002054 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002055 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002056
2057 if (status == FAIL)
2058 {
2059 ch_error(channel, "Decoding failed - discarding input");
2060 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002061 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002062 }
2063 else if (reader.js_buf[reader.js_used] != NUL)
2064 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002065 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002066 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002067 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2068 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002069 ret = status == MAYBE ? FALSE: TRUE;
2070 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002071 else
2072 ret = FALSE;
2073
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002074 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002075 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002076}
2077
2078/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002079 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002080 */
2081 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002082remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002083{
Bram Moolenaar77073442016-02-13 23:23:53 +01002084 if (node->cq_prev == NULL)
2085 head->cq_next = node->cq_next;
2086 else
2087 node->cq_prev->cq_next = node->cq_next;
2088 if (node->cq_next == NULL)
2089 head->cq_prev = node->cq_prev;
2090 else
2091 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002092}
2093
2094/*
2095 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002096 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002097 */
2098 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002099remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002100{
Bram Moolenaar77073442016-02-13 23:23:53 +01002101 if (node->jq_prev == NULL)
2102 head->jq_next = node->jq_next;
2103 else
2104 node->jq_prev->jq_next = node->jq_next;
2105 if (node->jq_next == NULL)
2106 head->jq_prev = node->jq_prev;
2107 else
2108 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002109 vim_free(node);
2110}
2111
2112/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002113 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002114 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002115 * When "id" is zero or negative jut get the first message. But not the one
2116 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002117 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002118 * Return OK when found and return the value in "rettv".
2119 * Return FAIL otherwise.
2120 */
2121 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002122channel_get_json(
2123 channel_T *channel,
2124 ch_part_T part,
2125 int id,
2126 int without_callback,
2127 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002129 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002130 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002131
Bram Moolenaar77073442016-02-13 23:23:53 +01002132 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002133 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002134 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002135 typval_T *tv = &l->lv_first->li_tv;
2136
Bram Moolenaar958dc692016-12-01 15:34:12 +01002137 if ((without_callback || !item->jq_no_callback)
2138 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002139 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002140 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002141 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002142 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002143 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002144 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002145 ch_log(channel, "Getting JSON message %ld",
2146 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002147 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002148 return OK;
2149 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002150 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002151 }
2152 return FAIL;
2153}
2154
Bram Moolenaar958dc692016-12-01 15:34:12 +01002155/*
2156 * Put back "rettv" into the JSON queue, there was no callback for it.
2157 * Takes over the values in "rettv".
2158 */
2159 static void
2160channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2161{
2162 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2163 jsonq_T *item = head->jq_next;
2164 jsonq_T *newitem;
2165
2166 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2167 /* last item was pushed back, append to the end */
2168 item = NULL;
2169 else while (item != NULL && item->jq_no_callback)
2170 /* append after the last item that was pushed back */
2171 item = item->jq_next;
2172
2173 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2174 if (newitem == NULL)
2175 clear_tv(rettv);
2176 else
2177 {
2178 newitem->jq_value = alloc_tv();
2179 if (newitem->jq_value == NULL)
2180 {
2181 vim_free(newitem);
2182 clear_tv(rettv);
2183 }
2184 else
2185 {
2186 newitem->jq_no_callback = FALSE;
2187 *newitem->jq_value = *rettv;
2188 if (item == NULL)
2189 {
2190 /* append to the end */
2191 newitem->jq_prev = head->jq_prev;
2192 head->jq_prev = newitem;
2193 newitem->jq_next = NULL;
2194 if (newitem->jq_prev == NULL)
2195 head->jq_next = newitem;
2196 else
2197 newitem->jq_prev->jq_next = newitem;
2198 }
2199 else
2200 {
2201 /* append after "item" */
2202 newitem->jq_prev = item;
2203 newitem->jq_next = item->jq_next;
2204 item->jq_next = newitem;
2205 if (newitem->jq_next == NULL)
2206 head->jq_prev = newitem;
2207 else
2208 newitem->jq_next->jq_prev = newitem;
2209 }
2210 }
2211 }
2212}
2213
Bram Moolenaarece61b02016-02-20 21:39:05 +01002214#define CH_JSON_MAX_ARGS 4
2215
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002216/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002217 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002218 * "argv[0]" is the command string.
2219 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002220 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002221 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002222channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002223{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002224 char_u *cmd = argv[0].vval.v_string;
2225 char_u *arg;
2226 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002227
Bram Moolenaarece61b02016-02-20 21:39:05 +01002228 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002229 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002230 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002231 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002232 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002233 return;
2234 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002235 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002236 if (arg == NULL)
2237 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002238
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002239 if (STRCMP(cmd, "ex") == 0)
2240 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002241 int save_called_emsg = called_emsg;
2242
2243 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002244 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002245 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002247 --emsg_silent;
2248 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002249 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002250 (char *)get_vim_var_str(VV_ERRMSG));
2251 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002252 }
2253 else if (STRCMP(cmd, "normal") == 0)
2254 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002255 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002256
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002257 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002258 ea.arg = arg;
2259 ea.addr_count = 0;
2260 ea.forceit = TRUE; /* no mapping */
2261 ex_normal(&ea);
2262 }
2263 else if (STRCMP(cmd, "redraw") == 0)
2264 {
2265 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002266
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002267 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002268 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002269 ex_redraw(&ea);
2270 showruler(FALSE);
2271 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002272 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002273 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002274 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002275 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002276 int is_call = cmd[0] == 'c';
2277 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002278
Bram Moolenaarece61b02016-02-20 21:39:05 +01002279 if (argv[id_idx].v_type != VAR_UNKNOWN
2280 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002281 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002282 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002283 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002284 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002285 }
2286 else if (is_call && argv[2].v_type != VAR_LIST)
2287 {
2288 ch_error(channel, "third argument for call must be a list");
2289 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002290 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002291 }
2292 else
2293 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002294 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002295 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002296 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002297 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002298
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002299 /* Don't pollute the display with errors. */
2300 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002301 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002302 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002303 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002304 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002305 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002306 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002307 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002308 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002309 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2310 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002311 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002312
2313 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002314 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002315 int id = argv[id_idx].vval.v_number;
2316
Bram Moolenaar55fab432016-02-07 16:53:13 +01002317 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002318 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002319 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002320 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002321 /* If evaluation failed or the result can't be encoded
2322 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002323 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002324 err_tv.v_type = VAR_STRING;
2325 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002326 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002327 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002328 if (json != NULL)
2329 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002330 channel_send(channel,
2331 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002332 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002333 vim_free(json);
2334 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002335 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002336 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002337 if (tv == &res_tv)
2338 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002339 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002340 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002341 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002342 }
2343 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002344 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002345 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002346 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002347 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002348}
2349
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002350/*
2351 * Invoke the callback at "cbhead".
2352 * Does not redraw but sets channel_need_redraw.
2353 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002354 static void
2355invoke_one_time_callback(
2356 channel_T *channel,
2357 cbq_T *cbhead,
2358 cbq_T *item,
2359 typval_T *argv)
2360{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002361 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002362 (char *)item->cq_callback);
2363 /* Remove the item from the list first, if the callback
2364 * invokes ch_close() the list will be cleared. */
2365 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002366 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002367 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002368 vim_free(item);
2369}
2370
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002371 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002372append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002373{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002374 bufref_T save_curbuf = {NULL, 0, 0};
2375 win_T *save_curwin = NULL;
2376 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002377 linenr_T lnum = buffer->b_ml.ml_line_count;
2378 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002379 chanpart_T *ch_part = &channel->ch_part[part];
2380 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002381 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002382
2383 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2384 {
2385 if (!ch_part->ch_nomod_error)
2386 {
2387 ch_error(channel, "Buffer is not modifiable, cannot append");
2388 ch_part->ch_nomod_error = TRUE;
2389 }
2390 return;
2391 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002392
2393 /* If the buffer is also used as input insert above the last
2394 * line. Don't write these lines. */
2395 if (save_write_to)
2396 {
2397 --lnum;
2398 buffer->b_write_to_channel = FALSE;
2399 }
2400
2401 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002402 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002403
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002404 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002405
2406 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2407 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2408
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002409 u_sync(TRUE);
2410 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002411 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002412
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002413 if (empty)
2414 {
2415 /* The buffer is empty, replace the first (dummy) line. */
2416 ml_replace(lnum, msg, TRUE);
2417 lnum = 0;
2418 }
2419 else
2420 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002421 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002422
2423 /* Restore curbuf/curwin/curtab */
2424 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2425
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002426 if (ch_part->ch_nomodifiable)
2427 buffer->b_p_ma = FALSE;
2428 else
2429 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002430
2431 if (buffer->b_nwindows > 0)
2432 {
2433 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002434
2435 FOR_ALL_WINDOWS(wp)
2436 {
2437 if (wp->w_buffer == buffer
2438 && (save_write_to
2439 ? wp->w_cursor.lnum == lnum + 1
2440 : (wp->w_cursor.lnum == lnum
2441 && wp->w_cursor.col == 0)))
2442 {
2443 ++wp->w_cursor.lnum;
2444 save_curwin = curwin;
2445 curwin = wp;
2446 curbuf = curwin->w_buffer;
2447 scroll_cursor_bot(0, FALSE);
2448 curwin = save_curwin;
2449 curbuf = curwin->w_buffer;
2450 }
2451 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002452 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002453 channel_need_redraw = TRUE;
2454 }
2455
2456 if (save_write_to)
2457 {
2458 channel_T *ch;
2459
2460 /* Find channels reading from this buffer and adjust their
2461 * next-to-read line number. */
2462 buffer->b_write_to_channel = TRUE;
2463 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2464 {
2465 chanpart_T *in_part = &ch->ch_part[PART_IN];
2466
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002467 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002468 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2469 }
2470 }
2471}
2472
Bram Moolenaar437905c2016-04-26 19:01:05 +02002473 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002474drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002475{
2476 char_u *msg;
2477
2478 while ((msg = channel_get(channel, part)) != NULL)
2479 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002480 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002481 vim_free(msg);
2482 }
2483}
2484
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002485/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002486 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002487 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002488 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002489 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002490 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002491may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002492{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002493 char_u *msg = NULL;
2494 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002495 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002496 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002497 chanpart_T *ch_part = &channel->ch_part[part];
2498 ch_mode_T ch_mode = ch_part->ch_mode;
2499 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002500 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002501 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002502 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002503 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002504 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002505
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002506 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002507 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002508 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002509
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002510 /* Use a message-specific callback, part callback or channel callback */
2511 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2512 if (cbitem->cq_seq_nr == 0)
2513 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002514 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002515 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002516 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002517 partial = cbitem->cq_partial;
2518 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002519 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002520 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002521 callback = ch_part->ch_callback;
2522 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002523 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002524 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002525 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002526 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002527 partial = channel->ch_partial;
2528 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002529
Bram Moolenaarec68a992016-10-03 21:37:41 +02002530 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002531 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2532 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002533 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002534 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002535 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002536 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002537 buffer = NULL;
2538 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002539
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002540 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002541 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002542 listitem_T *item;
2543 int argc = 0;
2544
Bram Moolenaard7ece102016-02-02 23:23:02 +01002545 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002546 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002547 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002548 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002549 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002550 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002551 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002552 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002553
Bram Moolenaarece61b02016-02-20 21:39:05 +01002554 for (item = listtv->vval.v_list->lv_first;
2555 item != NULL && argc < CH_JSON_MAX_ARGS;
2556 item = item->li_next)
2557 argv[argc++] = item->li_tv;
2558 while (argc < CH_JSON_MAX_ARGS)
2559 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002560
Bram Moolenaarece61b02016-02-20 21:39:05 +01002561 if (argv[0].v_type == VAR_STRING)
2562 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002563 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002564 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002565 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002566 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002567 }
2568
Bram Moolenaarece61b02016-02-20 21:39:05 +01002569 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002570 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002571 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002572 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002573 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002574 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002575 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002576 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002577 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002578 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002579 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002580 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002581 return FALSE;
2582 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002583 else
2584 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002585 /* If there is no callback or buffer drop the message. */
2586 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002587 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002588 /* If there is a close callback it may use ch_read() to get the
2589 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002590 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002591 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002592 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002593 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002594
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002595 if (ch_mode == MODE_NL)
2596 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002597 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002598 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002599 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002600
2601 /* See if we have a message ending in NL in the first buffer. If
2602 * not try to concatenate the first and the second buffer. */
2603 while (TRUE)
2604 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002605 node = channel_peek(channel, part);
2606 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002607 if (nl != NULL)
2608 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002609 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002610 {
2611 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2612 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002613 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002614 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002615 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002616 buf = node->rq_buffer;
2617
Bram Moolenaarec68a992016-10-03 21:37:41 +02002618 if (nl == NULL)
2619 {
2620 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002621 char_u *new_buf;
2622
2623 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2624 if (new_buf == NULL)
2625 /* This might fail over and over again, should the message
2626 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002627 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002628 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002629 node->rq_buffer = buf;
2630 nl = buf + node->rq_buflen++;
2631 *nl = NUL;
2632 }
2633
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002634 /* Convert NUL to NL, the internal representation. */
2635 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2636 if (*p == NUL)
2637 *p = NL;
2638
2639 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002640 {
2641 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002642 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002643 *nl = NUL;
2644 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002645 else
2646 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002647 /* Copy the message into allocated memory (excluding the NL)
2648 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002649 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002650 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002651 }
2652 }
2653 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002654 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002655 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002656 * get everything we have.
2657 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002658 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002659 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002660
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002661 if (msg == NULL)
2662 return FALSE; /* out of memory (and avoids Coverity warning) */
2663
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002664 argv[1].v_type = VAR_STRING;
2665 argv[1].vval.v_string = msg;
2666 }
2667
Bram Moolenaara07fec92016-02-05 21:04:08 +01002668 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002669 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002670 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002671
Bram Moolenaar958dc692016-12-01 15:34:12 +01002672 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002673 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002674 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002675 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002676 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002677 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002678 break;
2679 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002680 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002681 {
2682 if (channel->ch_drop_never)
2683 {
2684 /* message must be read with ch_read() */
2685 channel_push_json(channel, part, listtv);
2686 listtv = NULL;
2687 }
2688 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002689 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002690 seq_nr);
2691 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002692 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002693 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002694 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002695 if (buffer != NULL)
2696 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002697 if (msg == NULL)
2698 /* JSON or JS mode: re-encode the message. */
2699 msg = json_encode(listtv, ch_mode);
2700 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002701 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002702#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002703 if (buffer->b_term != NULL)
2704 write_to_term(buffer, msg, channel);
2705 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002706#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002707 append_to_buffer(buffer, msg, channel, part);
2708 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002709 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002710
Bram Moolenaar187db502016-02-27 14:44:26 +01002711 if (callback != NULL)
2712 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002713 if (cbitem != NULL)
2714 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2715 else
2716 {
2717 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002718 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002719 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002720 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002721 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002722 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002723 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002724 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002725 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002726
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002727 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002728 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002729 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002730
2731 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002732}
2733
2734/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002735 * Return TRUE when channel "channel" is open for writing to.
2736 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002737 */
2738 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002739channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002740{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002741 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002742 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002743}
2744
2745/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002746 * Return TRUE when channel "channel" is open for reading or writing.
2747 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002748 */
2749 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002750channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002751{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002752 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002753 || channel->CH_IN_FD != INVALID_FD
2754 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002755 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002756}
2757
2758/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002759 * Return TRUE if "channel" has JSON or other typeahead.
2760 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002761 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002762channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002763{
2764 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2765
2766 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2767 {
2768 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2769 jsonq_T *item = head->jq_next;
2770
2771 return item != NULL;
2772 }
2773 return channel_peek(channel, part) != NULL;
2774}
2775
2776/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002777 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002778 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002779 */
2780 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002781channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002782{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002783 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002784 int has_readahead = FALSE;
2785
Bram Moolenaar77073442016-02-13 23:23:53 +01002786 if (channel == NULL)
2787 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002788 if (req_part == PART_OUT)
2789 {
2790 if (channel->CH_OUT_FD != INVALID_FD)
2791 return "open";
2792 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002793 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002794 }
2795 else if (req_part == PART_ERR)
2796 {
2797 if (channel->CH_ERR_FD != INVALID_FD)
2798 return "open";
2799 if (channel_has_readahead(channel, PART_ERR))
2800 has_readahead = TRUE;
2801 }
2802 else
2803 {
2804 if (channel_is_open(channel))
2805 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002806 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002807 if (channel_has_readahead(channel, part))
2808 {
2809 has_readahead = TRUE;
2810 break;
2811 }
2812 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002813
2814 if (has_readahead)
2815 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002816 return "closed";
2817}
2818
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002819 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002820channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002821{
2822 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002823 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002824 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002825 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002826 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002827
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002828 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002829 STRCAT(namebuf, "_");
2830 tail = STRLEN(namebuf);
2831
2832 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002833 if (chanpart->ch_fd != INVALID_FD)
2834 status = "open";
2835 else if (channel_has_readahead(channel, part))
2836 status = "buffered";
2837 else
2838 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002839 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002840
2841 STRCPY(namebuf + tail, "mode");
2842 switch (chanpart->ch_mode)
2843 {
2844 case MODE_NL: s = "NL"; break;
2845 case MODE_RAW: s = "RAW"; break;
2846 case MODE_JSON: s = "JSON"; break;
2847 case MODE_JS: s = "JS"; break;
2848 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002849 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002850
2851 STRCPY(namebuf + tail, "io");
2852 if (part == PART_SOCK)
2853 s = "socket";
2854 else switch (chanpart->ch_io)
2855 {
2856 case JIO_NULL: s = "null"; break;
2857 case JIO_PIPE: s = "pipe"; break;
2858 case JIO_FILE: s = "file"; break;
2859 case JIO_BUFFER: s = "buffer"; break;
2860 case JIO_OUT: s = "out"; break;
2861 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002862 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002863
2864 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002865 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002866}
2867
2868 void
2869channel_info(channel_T *channel, dict_T *dict)
2870{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002871 dict_add_number(dict, "id", channel->ch_id);
2872 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002873
2874 if (channel->ch_hostname != NULL)
2875 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002876 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2877 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002878 channel_part_info(channel, dict, "sock", PART_SOCK);
2879 }
2880 else
2881 {
2882 channel_part_info(channel, dict, "out", PART_OUT);
2883 channel_part_info(channel, dict, "err", PART_ERR);
2884 channel_part_info(channel, dict, "in", PART_IN);
2885 }
2886}
2887
Bram Moolenaar77073442016-02-13 23:23:53 +01002888/*
2889 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002890 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002891 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002892 */
2893 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002894channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002895{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002896 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002897
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002898#ifdef FEAT_GUI
2899 channel_gui_unregister(channel);
2900#endif
2901
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002902 ch_close_part(channel, PART_SOCK);
2903 ch_close_part(channel, PART_IN);
2904 ch_close_part(channel, PART_OUT);
2905 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002906
Bram Moolenaara9f02812017-08-05 17:40:38 +02002907 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002908 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002909 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002910
Bram Moolenaara9f02812017-08-05 17:40:38 +02002911 /* Invoke callbacks and flush buffers before the close callback. */
2912 if (channel->ch_close_cb != NULL)
2913 ch_log(channel,
2914 "Invoking callbacks and flushing buffers before closing");
2915 for (part = PART_SOCK; part < PART_IN; ++part)
2916 {
2917 if (channel->ch_close_cb != NULL
2918 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2919 {
2920 /* Increment the refcount to avoid the channel being freed
2921 * halfway. */
2922 ++channel->ch_refcount;
2923 if (channel->ch_close_cb == NULL)
2924 ch_log(channel, "flushing %s buffers before closing",
2925 part_names[part]);
2926 while (may_invoke_callback(channel, part))
2927 ;
2928 --channel->ch_refcount;
2929 }
2930 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002931
Bram Moolenaara9f02812017-08-05 17:40:38 +02002932 if (channel->ch_close_cb != NULL)
2933 {
2934 typval_T argv[1];
2935 typval_T rettv;
2936 int dummy;
2937
2938 /* Increment the refcount to avoid the channel being freed
2939 * halfway. */
2940 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002941 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002942 (char *)channel->ch_close_cb);
2943 argv[0].v_type = VAR_CHANNEL;
2944 argv[0].vval.v_channel = channel;
2945 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002946 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002947 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002948 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002949 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002950
Bram Moolenaara9f02812017-08-05 17:40:38 +02002951 /* the callback is only called once */
2952 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2953 channel->ch_close_cb = NULL;
2954 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002955
Bram Moolenaara9f02812017-08-05 17:40:38 +02002956 if (channel_need_redraw)
2957 {
2958 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002959 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002960 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002961
Bram Moolenaara9f02812017-08-05 17:40:38 +02002962 if (!channel->ch_drop_never)
2963 /* any remaining messages are useless now */
2964 for (part = PART_SOCK; part < PART_IN; ++part)
2965 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002966
2967 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002968 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002969 }
2970
2971 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002972
2973#ifdef FEAT_TERMINAL
2974 term_channel_closed(channel);
2975#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002976}
2977
Bram Moolenaard04a0202016-01-26 23:30:18 +01002978/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002979 * Close the "in" part channel "channel".
2980 */
2981 void
2982channel_close_in(channel_T *channel)
2983{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002984 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002985}
2986
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002987 static void
2988remove_from_writeque(writeq_T *wq, writeq_T *entry)
2989{
2990 ga_clear(&entry->wq_ga);
2991 wq->wq_next = entry->wq_next;
2992 if (wq->wq_next == NULL)
2993 wq->wq_prev = NULL;
2994 else
2995 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002996 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002997}
2998
Bram Moolenaar0874a832016-09-01 15:11:51 +02002999/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003000 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003001 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003002 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003003channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003004{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003005 chanpart_T *ch_part = &channel->ch_part[part];
3006 jsonq_T *json_head = &ch_part->ch_json_head;
3007 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003008
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003009 while (channel_peek(channel, part) != NULL)
3010 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01003011
3012 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003013 {
3014 cbq_T *node = cb_head->cq_next;
3015
3016 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003017 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003018 vim_free(node);
3019 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003020
3021 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003022 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003023 free_tv(json_head->jq_next->jq_value);
3024 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003025 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003026
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003027 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3028 ch_part->ch_callback = NULL;
3029 ch_part->ch_partial = NULL;
3030
3031 while (ch_part->ch_writeque.wq_next != NULL)
3032 remove_from_writeque(&ch_part->ch_writeque,
3033 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003034}
3035
3036/*
3037 * Clear all the read buffers on "channel".
3038 */
3039 void
3040channel_clear(channel_T *channel)
3041{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003042 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003043 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003044 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003045 channel_clear_one(channel, PART_OUT);
3046 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003047 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003048 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003049 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003050 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003051 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003052 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003053 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003054}
3055
Bram Moolenaar77073442016-02-13 23:23:53 +01003056#if defined(EXITFREE) || defined(PROTO)
3057 void
3058channel_free_all(void)
3059{
3060 channel_T *channel;
3061
Bram Moolenaard6051b52016-02-28 15:49:03 +01003062 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003063 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3064 channel_clear(channel);
3065}
3066#endif
3067
3068
Bram Moolenaar715d2852016-04-30 17:06:31 +02003069/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003070#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003071
3072/* Buffer size for reading incoming messages. */
3073#define MAXMSGSIZE 4096
3074
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003075#if defined(HAVE_SELECT)
3076/*
3077 * Add write fds where we are waiting for writing to be possible.
3078 */
3079 static int
3080channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3081{
3082 int maxfd = maxfd_arg;
3083 channel_T *ch;
3084
3085 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3086 {
3087 chanpart_T *in_part = &ch->ch_part[PART_IN];
3088
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003089 if (in_part->ch_fd != INVALID_FD
3090 && (in_part->ch_bufref.br_buf != NULL
3091 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003092 {
3093 FD_SET((int)in_part->ch_fd, wfds);
3094 if ((int)in_part->ch_fd >= maxfd)
3095 maxfd = (int)in_part->ch_fd + 1;
3096 }
3097 }
3098 return maxfd;
3099}
3100#else
3101/*
3102 * Add write fds where we are waiting for writing to be possible.
3103 */
3104 static int
3105channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3106{
3107 int nfd = nfd_in;
3108 channel_T *ch;
3109
3110 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3111 {
3112 chanpart_T *in_part = &ch->ch_part[PART_IN];
3113
Bram Moolenaar683b7962017-08-19 15:51:59 +02003114 if (in_part->ch_fd != INVALID_FD
3115 && (in_part->ch_bufref.br_buf != NULL
3116 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003117 {
3118 in_part->ch_poll_idx = nfd;
3119 fds[nfd].fd = in_part->ch_fd;
3120 fds[nfd].events = POLLOUT;
3121 ++nfd;
3122 }
3123 else
3124 in_part->ch_poll_idx = -1;
3125 }
3126 return nfd;
3127}
3128#endif
3129
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003130typedef enum {
3131 CW_READY,
3132 CW_NOT_READY,
3133 CW_ERROR
3134} channel_wait_result;
3135
Bram Moolenaard04a0202016-01-26 23:30:18 +01003136/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003137 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003138 * Return CW_READY when there is something to read.
3139 * Return CW_NOT_READY when there is nothing to read.
3140 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003141 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003142 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003143channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003144{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003145 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003146 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003147
Bram Moolenaard8070362016-02-15 21:56:54 +01003148# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003149 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003150 {
3151 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003152 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003153 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003154 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003155
3156 /* reading from a pipe, not a socket */
3157 while (TRUE)
3158 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003159 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3160
3161 if (r && nread > 0)
3162 return CW_READY;
3163 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003164 {
3165 DWORD err = GetLastError();
3166
3167 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3168 return CW_ERROR;
3169
3170 if (channel->ch_named_pipe)
3171 {
3172 DisconnectNamedPipe((HANDLE)fd);
3173 ConnectNamedPipe((HANDLE)fd, NULL);
3174 }
3175 else
3176 return CW_ERROR;
3177 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003178
3179 /* perhaps write some buffer lines */
3180 channel_write_any_lines();
3181
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003182 sleep_time = deadline - GetTickCount();
3183 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003184 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003185 /* Wait for a little while. Very short at first, up to 10 msec
3186 * after looping a few times. */
3187 if (sleep_time > delay)
3188 sleep_time = delay;
3189 Sleep(sleep_time);
3190 delay = delay * 2;
3191 if (delay > 10)
3192 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003193 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003194 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003195 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003196#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003197 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003198#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003199 struct timeval tval;
3200 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003201 fd_set wfds;
3202 int ret;
3203 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003204
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003205 tval.tv_sec = timeout / 1000;
3206 tval.tv_usec = (timeout % 1000) * 1000;
3207 for (;;)
3208 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003209 FD_ZERO(&rfds);
3210 FD_SET((int)fd, &rfds);
3211
3212 /* Write lines to a pipe when a pipe can be written to. Need to
3213 * set this every time, some buffers may be done. */
3214 maxfd = (int)fd + 1;
3215 FD_ZERO(&wfds);
3216 maxfd = channel_fill_wfds(maxfd, &wfds);
3217
3218 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003219# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003220 SOCK_ERRNO;
3221 if (ret == -1 && errno == EINTR)
3222 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003223# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003224 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003225 {
3226 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003227 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003228 channel_write_any_lines();
3229 continue;
3230 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003231 break;
3232 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003233#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003234 for (;;)
3235 {
3236 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3237 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003238
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003239 fds[0].fd = fd;
3240 fds[0].events = POLLIN;
3241 nfd = channel_fill_poll_write(nfd, fds);
3242 if (poll(fds, nfd, timeout) > 0)
3243 {
3244 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003245 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003246 channel_write_any_lines();
3247 continue;
3248 }
3249 break;
3250 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003251#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003252 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003253 return CW_NOT_READY;
3254}
3255
3256 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003257ch_close_part_on_error(
3258 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003259{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003260 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003261
3262 if (is_err)
3263 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003264 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003265 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003266 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003267
3268 /* Queue a "DETACH" netbeans message in the command queue in order to
3269 * terminate the netbeans session later. Do not end the session here
3270 * directly as we may be running in the context of a call to
3271 * netbeans_parse_messages():
3272 * netbeans_parse_messages
3273 * -> autocmd triggered while processing the netbeans cmd
3274 * -> ui_breakcheck
3275 * -> gui event loop or select loop
3276 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003277 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003278 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003279 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003280 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003281 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3282
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003283 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003284 * close the channel yet, there may be something to read on another part.
3285 * When stdout and stderr use the same FD we get the error only on one of
3286 * them, also close the other. */
3287 if (part == PART_OUT || part == PART_ERR)
3288 {
3289 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3290
3291 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3292 ch_close_part(channel, other);
3293 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003294 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003295
3296#ifdef FEAT_GUI
3297 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003298 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003299#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003300}
3301
3302 static void
3303channel_close_now(channel_T *channel)
3304{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003305 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003306 if (channel->ch_nb_close_cb != NULL)
3307 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003308 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003309}
3310
3311/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003312 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003313 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003314 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003315 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003316 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003317channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003318{
3319 static char_u *buf = NULL;
3320 int len = 0;
3321 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003322 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003323 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003324
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003325 fd = channel->ch_part[part].ch_fd;
3326 if (fd == INVALID_FD)
3327 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003328 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003329 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003330 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003331 }
3332 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003333
3334 /* Allocate a buffer to read into. */
3335 if (buf == NULL)
3336 {
3337 buf = alloc(MAXMSGSIZE);
3338 if (buf == NULL)
3339 return; /* out of memory! */
3340 }
3341
3342 /* Keep on reading for as long as there is something to read.
3343 * Use select() or poll() to avoid blocking on a message that is exactly
3344 * MAXMSGSIZE long. */
3345 for (;;)
3346 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003347 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003348 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003349 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003350 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003351 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003352 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003353 if (len <= 0)
3354 break; /* error or nothing more to read */
3355
3356 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003357 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003358 readlen += len;
3359 if (len < MAXMSGSIZE)
3360 break; /* did read everything that's available */
3361 }
3362
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003363 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003364 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003365 {
3366 if (!channel->ch_keep_open)
3367 ch_close_part_on_error(channel, part, (len < 0), func);
3368 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003369#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003370 else if (CH_HAS_GUI && gtk_main_level() > 0)
3371 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003372 gtk_main_quit();
3373#endif
3374}
3375
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003376/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003377 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003378 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003379 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003380 * Returns what was read in allocated memory.
3381 * Returns NULL in case of error or timeout.
3382 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003383 static char_u *
3384channel_read_block(channel_T *channel, ch_part_T part, int timeout, int raw)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003385{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003386 char_u *buf;
3387 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003388 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003389 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003390 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003391 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003392
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003393 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003394 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003395
3396 while (TRUE)
3397 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003398 node = channel_peek(channel, part);
3399 if (node != NULL)
3400 {
3401 if (mode == MODE_RAW || (mode == MODE_NL
3402 && channel_first_nl(node) != NULL))
3403 /* got a complete message */
3404 break;
3405 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3406 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003407 /* If not blocking or nothing more is coming then return what we
3408 * have. */
3409 if (raw || fd == INVALID_FD)
3410 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003411 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003412
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003413 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003414 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003415 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003416 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003417 {
3418 ch_log(channel, "Timed out");
3419 return NULL;
3420 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003421 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003422 }
3423
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003424 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003425 if (mode == MODE_RAW)
3426 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003427 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003428 }
3429 else
3430 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003431 char_u *p;
3432
3433 buf = node->rq_buffer;
3434 nl = channel_first_nl(node);
3435
3436 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003437 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003438 if (*p == NUL)
3439 *p = NL;
3440
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003441 if (nl == NULL)
3442 {
3443 /* must be a closed channel with missing NL */
3444 msg = channel_get(channel, part);
3445 }
3446 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003447 {
3448 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003449 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003450 *nl = NUL;
3451 }
3452 else
3453 {
3454 /* Copy the message into allocated memory and remove it from the
3455 * buffer. */
3456 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003457 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003458 }
3459 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003460 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003461 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003462 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003463}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003464
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003465/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003466 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003467 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003468 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003469 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003470 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003471 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003472channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003473 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003474 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003475 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003476 int id,
3477 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003478{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003479 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003480 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003481 int timeout;
3482 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003483
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003484 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003485 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003486 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003487 for (;;)
3488 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003489 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003490
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003491 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003492 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003493 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003494 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003495 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003496 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003497
Bram Moolenaard7ece102016-02-02 23:23:02 +01003498 if (!more)
3499 {
3500 /* Handle any other messages in the queue. If done some more
3501 * messages may have arrived. */
3502 if (channel_parse_messages())
3503 continue;
3504
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003505 /* Wait for up to the timeout. If there was an incomplete message
3506 * use the deadline for that. */
3507 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003508 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003509 {
3510#ifdef WIN32
3511 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3512#else
3513 {
3514 struct timeval now_tv;
3515
3516 gettimeofday(&now_tv, NULL);
3517 timeout = (chanpart->ch_deadline.tv_sec
3518 - now_tv.tv_sec) * 1000
3519 + (chanpart->ch_deadline.tv_usec
3520 - now_tv.tv_usec) / 1000
3521 + 1;
3522 }
3523#endif
3524 if (timeout < 0)
3525 {
3526 /* Something went wrong, channel_parse_json() didn't
3527 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003528 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003529 timeout = timeout_arg;
3530 }
3531 else if (timeout > timeout_arg)
3532 timeout = timeout_arg;
3533 }
3534 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003535 if (fd == INVALID_FD
3536 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003537 {
3538 if (timeout == timeout_arg)
3539 {
3540 if (fd != INVALID_FD)
3541 ch_log(channel, "Timed out");
3542 break;
3543 }
3544 }
3545 else
3546 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003547 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003548 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003549 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003550 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003551}
3552
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003553/*
3554 * Common for ch_read() and ch_readraw().
3555 */
3556 void
3557common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3558{
3559 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003560 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003561 jobopt_T opt;
3562 int mode;
3563 int timeout;
3564 int id = -1;
3565 typval_T *listtv = NULL;
3566
3567 /* return an empty string by default */
3568 rettv->v_type = VAR_STRING;
3569 rettv->vval.v_string = NULL;
3570
3571 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003572 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003573 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003574 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003575
Bram Moolenaar437905c2016-04-26 19:01:05 +02003576 if (opt.jo_set & JO_PART)
3577 part = opt.jo_part;
3578 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003579 if (channel != NULL)
3580 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003581 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003582 part = channel_part_read(channel);
3583 mode = channel_get_mode(channel, part);
3584 timeout = channel_get_timeout(channel, part);
3585 if (opt.jo_set & JO_TIMEOUT)
3586 timeout = opt.jo_timeout;
3587
3588 if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003589 rettv->vval.v_string = channel_read_block(channel, part,
3590 timeout, raw);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003591 else
3592 {
3593 if (opt.jo_set & JO_ID)
3594 id = opt.jo_id;
3595 channel_read_json_block(channel, part, timeout, id, &listtv);
3596 if (listtv != NULL)
3597 {
3598 *rettv = *listtv;
3599 vim_free(listtv);
3600 }
3601 else
3602 {
3603 rettv->v_type = VAR_SPECIAL;
3604 rettv->vval.v_number = VVAL_NONE;
3605 }
3606 }
3607 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003608
3609theend:
3610 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003611}
3612
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003613# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3614 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003615/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003616 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003617 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003618 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003619 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003620channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003621{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003622 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003623 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003624
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003625 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003626 for (channel = first_channel; channel != NULL;
3627 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003628 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003629 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003630 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003631 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003632 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003633 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003634 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003635 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003636 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003637}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003638# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003639
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003640# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003641/*
3642 * Check the channels for anything that is ready to be read.
3643 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003644 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003645 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003646 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003647channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003648{
3649 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003650 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003651 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003652
3653 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3654 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003655 if (only_keep_open && !channel->ch_keep_open)
3656 continue;
3657
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003658 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003659 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003660 {
3661 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003662 if (fd != INVALID_FD)
3663 {
3664 int r = channel_wait(channel, fd, 0);
3665
3666 if (r == CW_READY)
3667 channel_read(channel, part, "channel_handle_events");
3668 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003669 ch_close_part_on_error(channel, part, TRUE,
3670 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003671 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003672 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003673 }
3674}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003675# endif
3676
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003677# if defined(FEAT_GUI) || defined(PROTO)
3678/*
3679 * Return TRUE when there is any channel with a keep_open flag.
3680 */
3681 int
3682channel_any_keep_open()
3683{
3684 channel_T *channel;
3685
3686 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3687 if (channel->ch_keep_open)
3688 return TRUE;
3689 return FALSE;
3690}
3691# endif
3692
Bram Moolenaard04a0202016-01-26 23:30:18 +01003693/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003694 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003695 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003696 */
3697 void
3698channel_set_nonblock(channel_T *channel, ch_part_T part)
3699{
3700 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003701 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003702
3703 if (fd != INVALID_FD)
3704 {
3705#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003706 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003707
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003708 ioctlsocket(fd, FIONBIO, &val);
3709#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003710 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003711#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003712 ch_part->ch_nonblocking = TRUE;
3713 }
3714}
3715
3716/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003717 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003718 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003719 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003720 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003721 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003722channel_send(
3723 channel_T *channel,
3724 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003725 char_u *buf_arg,
3726 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003727 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003728{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003729 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003730 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003731 chanpart_T *ch_part = &channel->ch_part[part];
3732 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003733
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003734 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003735 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003736 {
3737 if (!channel->ch_error && fun != NULL)
3738 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003739 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003740 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003741 }
3742 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003743 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003744 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003745
Bram Moolenaar0b146882018-09-06 16:27:24 +02003746 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3747 channel_set_nonblock(channel, part);
3748
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003749 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003750 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003751 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003752 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003753 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003754 fprintf(log_fd, "'\n");
3755 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003756 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003757 }
3758
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003759 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003760 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003761 writeq_T *wq = &ch_part->ch_writeque;
3762 char_u *buf;
3763 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003764
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003765 if (wq->wq_next != NULL)
3766 {
3767 /* first write what was queued */
3768 buf = wq->wq_next->wq_ga.ga_data;
3769 len = wq->wq_next->wq_ga.ga_len;
3770 did_use_queue = TRUE;
3771 }
3772 else
3773 {
3774 if (len_arg == 0)
3775 /* nothing to write, called from channel_select_check() */
3776 return OK;
3777 buf = buf_arg;
3778 len = len_arg;
3779 }
3780
3781 if (part == PART_SOCK)
3782 res = sock_write(fd, (char *)buf, len);
3783 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003784 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003785 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003786#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003787 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003788 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003789 DisconnectNamedPipe((HANDLE)fd);
3790 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003791 }
3792#endif
3793
3794 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003795 if (res < 0 && (errno == EWOULDBLOCK
3796#ifdef EAGAIN
3797 || errno == EAGAIN
3798#endif
3799 ))
3800 res = 0; /* nothing got written */
3801
3802 if (res >= 0 && ch_part->ch_nonblocking)
3803 {
3804 writeq_T *entry = wq->wq_next;
3805
3806 if (did_use_queue)
3807 ch_log(channel, "Sent %d bytes now", res);
3808 if (res == len)
3809 {
3810 /* Wrote all the buf[len] bytes. */
3811 if (entry != NULL)
3812 {
3813 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003814 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003815 continue;
3816 }
3817 if (did_use_queue)
3818 ch_log(channel, "Write queue empty");
3819 }
3820 else
3821 {
3822 /* Wrote only buf[res] bytes, can't write more now. */
3823 if (entry != NULL)
3824 {
3825 if (res > 0)
3826 {
3827 /* Remove the bytes that were written. */
3828 mch_memmove(entry->wq_ga.ga_data,
3829 (char *)entry->wq_ga.ga_data + res,
3830 len - res);
3831 entry->wq_ga.ga_len -= res;
3832 }
3833 buf = buf_arg;
3834 len = len_arg;
3835 }
3836 else
3837 {
3838 buf += res;
3839 len -= res;
3840 }
3841 ch_log(channel, "Adding %d bytes to the write queue", len);
3842
3843 /* Append the not written bytes of the argument to the write
3844 * buffer. Limit entries to 4000 bytes. */
3845 if (wq->wq_prev != NULL
3846 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3847 {
3848 writeq_T *last = wq->wq_prev;
3849
3850 /* append to the last entry */
3851 if (ga_grow(&last->wq_ga, len) == OK)
3852 {
3853 mch_memmove((char *)last->wq_ga.ga_data
3854 + last->wq_ga.ga_len,
3855 buf, len);
3856 last->wq_ga.ga_len += len;
3857 }
3858 }
3859 else
3860 {
3861 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3862
3863 if (last != NULL)
3864 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003865 last->wq_prev = wq->wq_prev;
3866 last->wq_next = NULL;
3867 if (wq->wq_prev == NULL)
3868 wq->wq_next = last;
3869 else
3870 wq->wq_prev->wq_next = last;
3871 wq->wq_prev = last;
3872 ga_init2(&last->wq_ga, 1, 1000);
3873 if (ga_grow(&last->wq_ga, len) == OK)
3874 {
3875 mch_memmove(last->wq_ga.ga_data, buf, len);
3876 last->wq_ga.ga_len = len;
3877 }
3878 }
3879 }
3880 }
3881 }
3882 else if (res != len)
3883 {
3884 if (!channel->ch_error && fun != NULL)
3885 {
3886 ch_error(channel, "%s(): write failed", fun);
3887 EMSG2(_("E631: %s(): write failed"), fun);
3888 }
3889 channel->ch_error = TRUE;
3890 return FAIL;
3891 }
3892
3893 channel->ch_error = FALSE;
3894 return OK;
3895 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003896}
3897
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003898/*
3899 * Common for "ch_sendexpr()" and "ch_sendraw()".
3900 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003901 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003902 * Otherwise returns NULL.
3903 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003904 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003905send_common(
3906 typval_T *argvars,
3907 char_u *text,
3908 int id,
3909 int eval,
3910 jobopt_T *opt,
3911 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003912 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003913{
3914 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003915 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003916
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003917 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003918 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003919 if (channel == NULL)
3920 return NULL;
3921 part_send = channel_part_send(channel);
3922 *part_read = channel_part_read(channel);
3923
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003924 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003925 return NULL;
3926
3927 /* Set the callback. An empty callback means no callback and not reading
3928 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3929 * allowed. */
3930 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3931 {
3932 if (eval)
3933 {
3934 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3935 return NULL;
3936 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003937 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003938 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003939 }
3940
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003941 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003942 && opt->jo_callback == NULL)
3943 return channel;
3944 return NULL;
3945}
3946
3947/*
3948 * common for "ch_evalexpr()" and "ch_sendexpr()"
3949 */
3950 void
3951ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3952{
3953 char_u *text;
3954 typval_T *listtv;
3955 channel_T *channel;
3956 int id;
3957 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003958 ch_part_T part_send;
3959 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 jobopt_T opt;
3961 int timeout;
3962
3963 /* return an empty string by default */
3964 rettv->v_type = VAR_STRING;
3965 rettv->vval.v_string = NULL;
3966
Bram Moolenaar437905c2016-04-26 19:01:05 +02003967 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003968 if (channel == NULL)
3969 return;
3970 part_send = channel_part_send(channel);
3971
3972 ch_mode = channel_get_mode(channel, part_send);
3973 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3974 {
3975 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3976 return;
3977 }
3978
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003979 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003980 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003981 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003982 if (text == NULL)
3983 return;
3984
3985 channel = send_common(argvars, text, id, eval, &opt,
3986 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3987 vim_free(text);
3988 if (channel != NULL && eval)
3989 {
3990 if (opt.jo_set & JO_TIMEOUT)
3991 timeout = opt.jo_timeout;
3992 else
3993 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003994 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3995 == OK)
3996 {
3997 list_T *list = listtv->vval.v_list;
3998
3999 /* Move the item from the list and then change the type to
4000 * avoid the value being freed. */
4001 *rettv = list->lv_last->li_tv;
4002 list->lv_last->li_tv.v_type = VAR_NUMBER;
4003 free_tv(listtv);
4004 }
4005 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004006 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004007}
4008
4009/*
4010 * common for "ch_evalraw()" and "ch_sendraw()"
4011 */
4012 void
4013ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4014{
4015 char_u buf[NUMBUFLEN];
4016 char_u *text;
4017 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004018 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004019 jobopt_T opt;
4020 int timeout;
4021
4022 /* return an empty string by default */
4023 rettv->v_type = VAR_STRING;
4024 rettv->vval.v_string = NULL;
4025
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004026 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004027 channel = send_common(argvars, text, 0, eval, &opt,
4028 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4029 if (channel != NULL && eval)
4030 {
4031 if (opt.jo_set & JO_TIMEOUT)
4032 timeout = opt.jo_timeout;
4033 else
4034 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004035 rettv->vval.v_string = channel_read_block(channel, part_read,
4036 timeout, TRUE);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004037 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004038 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004039}
4040
Bram Moolenaarf3360612017-10-01 16:21:31 +02004041# define KEEP_OPEN_TIME 20 /* msec */
4042
Bram Moolenaard04a0202016-01-26 23:30:18 +01004043# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004044/*
4045 * Add open channels to the poll struct.
4046 * Return the adjusted struct index.
4047 * The type of "fds" is hidden to avoid problems with the function proto.
4048 */
4049 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004050channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004051{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004052 int nfd = nfd_in;
4053 channel_T *channel;
4054 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004055 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004056
Bram Moolenaar77073442016-02-13 23:23:53 +01004057 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004058 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004059 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004060 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004061 chanpart_T *ch_part = &channel->ch_part[part];
4062
4063 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004064 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004065 if (channel->ch_keep_open)
4066 {
4067 /* For unknown reason poll() returns immediately for a
4068 * keep-open channel. Instead of adding it to the fds add
4069 * a short timeout and check, like polling. */
4070 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4071 *towait = KEEP_OPEN_TIME;
4072 }
4073 else
4074 {
4075 ch_part->ch_poll_idx = nfd;
4076 fds[nfd].fd = ch_part->ch_fd;
4077 fds[nfd].events = POLLIN;
4078 nfd++;
4079 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004080 }
4081 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004082 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004083 }
4084 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004085
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004086 nfd = channel_fill_poll_write(nfd, fds);
4087
Bram Moolenaare0874f82016-01-24 20:36:41 +01004088 return nfd;
4089}
4090
4091/*
4092 * The type of "fds" is hidden to avoid problems with the function proto.
4093 */
4094 int
4095channel_poll_check(int ret_in, void *fds_in)
4096{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004097 int ret = ret_in;
4098 channel_T *channel;
4099 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004100 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004101 int idx;
4102 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004103
Bram Moolenaar77073442016-02-13 23:23:53 +01004104 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004105 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004106 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004107 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004108 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004109
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004110 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004111 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004112 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004113 --ret;
4114 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004115 else if (channel->ch_part[part].ch_fd != INVALID_FD
4116 && channel->ch_keep_open)
4117 {
4118 /* polling a keep-open channel */
4119 channel_read(channel, part, "channel_poll_check_keep_open");
4120 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004121 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004122
4123 in_part = &channel->ch_part[PART_IN];
4124 idx = in_part->ch_poll_idx;
4125 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4126 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004127 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004128 --ret;
4129 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004130 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004131
4132 return ret;
4133}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004134# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004135
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004136# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004137
Bram Moolenaare0874f82016-01-24 20:36:41 +01004138/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004139 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004140 */
4141 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004142channel_select_setup(
4143 int maxfd_in,
4144 void *rfds_in,
4145 void *wfds_in,
4146 struct timeval *tv,
4147 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004148{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004149 int maxfd = maxfd_in;
4150 channel_T *channel;
4151 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004152 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004153 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004154
Bram Moolenaar77073442016-02-13 23:23:53 +01004155 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004156 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004157 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004158 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004159 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004160
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004161 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004162 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004163 if (channel->ch_keep_open)
4164 {
4165 /* For unknown reason select() returns immediately for a
4166 * keep-open channel. Instead of adding it to the rfds add
4167 * a short timeout and check, like polling. */
4168 if (*tvp == NULL || tv->tv_sec > 0
4169 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4170 {
4171 *tvp = tv;
4172 tv->tv_sec = 0;
4173 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4174 }
4175 }
4176 else
4177 {
4178 FD_SET((int)fd, rfds);
4179 if (maxfd < (int)fd)
4180 maxfd = (int)fd;
4181 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004182 }
4183 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004184 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004185
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004186 maxfd = channel_fill_wfds(maxfd, wfds);
4187
Bram Moolenaare0874f82016-01-24 20:36:41 +01004188 return maxfd;
4189}
4190
4191/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004192 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004193 */
4194 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004195channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004196{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004197 int ret = ret_in;
4198 channel_T *channel;
4199 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004200 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004201 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004202 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004203
Bram Moolenaar77073442016-02-13 23:23:53 +01004204 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004205 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004206 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004207 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004208 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004209
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004210 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004211 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004212 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004213 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004214 --ret;
4215 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004216 else if (fd != INVALID_FD && channel->ch_keep_open)
4217 {
4218 /* polling a keep-open channel */
4219 channel_read(channel, part, "channel_select_check_keep_open");
4220 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004221 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004222
4223 in_part = &channel->ch_part[PART_IN];
4224 if (ret > 0 && in_part->ch_fd != INVALID_FD
4225 && FD_ISSET(in_part->ch_fd, wfds))
4226 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004227 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004228 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004229 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004230 --ret;
4231 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004232 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004233
4234 return ret;
4235}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004236# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004237
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004238/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004239 * Execute queued up commands.
4240 * Invoked from the main loop when it's safe to execute received commands.
4241 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004242 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004243 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004244channel_parse_messages(void)
4245{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004246 channel_T *channel = first_channel;
4247 int ret = FALSE;
4248 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004249 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004250#ifdef ELAPSED_FUNC
4251 ELAPSED_TYPE start_tv;
4252
4253 ELAPSED_INIT(start_tv);
4254#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004255
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004256 ++safe_to_invoke_callback;
4257
Bram Moolenaard0b65022016-03-06 21:50:33 +01004258 /* Only do this message when another message was given, otherwise we get
4259 * lots of them. */
4260 if (did_log_msg)
4261 {
4262 ch_log(NULL, "looking for messages on channels");
4263 did_log_msg = FALSE;
4264 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004265 while (channel != NULL)
4266 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004267 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004268 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004269 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004270 channel_close_now(channel);
4271 /* channel may have been freed, start over */
4272 channel = first_channel;
4273 continue;
4274 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004275 if (channel->ch_to_be_freed)
4276 {
4277 channel_free(channel);
4278 /* channel has been freed, start over */
4279 channel = first_channel;
4280 continue;
4281 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004282 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004283 {
4284 /* channel is no longer useful, free it */
4285 channel_free(channel);
4286 channel = first_channel;
4287 part = PART_SOCK;
4288 continue;
4289 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004290 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004291 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004292 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004293 /* Increase the refcount, in case the handler causes the channel
4294 * to be unreferenced or closed. */
4295 ++channel->ch_refcount;
4296 r = may_invoke_callback(channel, part);
4297 if (r == OK)
4298 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004299 if (channel_unref(channel) || (r == OK
4300#ifdef ELAPSED_FUNC
4301 /* Limit the time we loop here to 100 msec, otherwise
4302 * Vim becomes unresponsive when the callback takes
4303 * more than a bit of time. */
4304 && ELAPSED_FUNC(start_tv) < 100L
4305#endif
4306 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004307 {
4308 /* channel was freed or something was done, start over */
4309 channel = first_channel;
4310 part = PART_SOCK;
4311 continue;
4312 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004313 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004314 if (part < PART_ERR)
4315 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004316 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004317 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004318 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004319 part = PART_SOCK;
4320 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004321 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004322
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004323 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004324 {
4325 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004326 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004327 }
4328
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004329 --safe_to_invoke_callback;
4330
Bram Moolenaard7ece102016-02-02 23:23:02 +01004331 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004332}
4333
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004334/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004335 * Return TRUE if any channel has readahead. That means we should not block on
4336 * waiting for input.
4337 */
4338 int
4339channel_any_readahead(void)
4340{
4341 channel_T *channel = first_channel;
4342 ch_part_T part = PART_SOCK;
4343
4344 while (channel != NULL)
4345 {
4346 if (channel_has_readahead(channel, part))
4347 return TRUE;
4348 if (part < PART_ERR)
4349 ++part;
4350 else
4351 {
4352 channel = channel->ch_next;
4353 part = PART_SOCK;
4354 }
4355 }
4356 return FALSE;
4357}
4358
4359/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004360 * Mark references to lists used in channels.
4361 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004362 int
4363set_ref_in_channel(int copyID)
4364{
Bram Moolenaar77073442016-02-13 23:23:53 +01004365 int abort = FALSE;
4366 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004367 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004368
Bram Moolenaar77073442016-02-13 23:23:53 +01004369 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004370 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004371 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004372 tv.v_type = VAR_CHANNEL;
4373 tv.vval.v_channel = channel;
4374 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004375 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004376 return abort;
4377}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004378
4379/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004380 * Return the "part" to write to for "channel".
4381 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004382 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004383channel_part_send(channel_T *channel)
4384{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004385 if (channel->CH_SOCK_FD == INVALID_FD)
4386 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004387 return PART_SOCK;
4388}
4389
4390/*
4391 * Return the default "part" to read from for "channel".
4392 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004393 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004394channel_part_read(channel_T *channel)
4395{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004396 if (channel->CH_SOCK_FD == INVALID_FD)
4397 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004398 return PART_SOCK;
4399}
4400
4401/*
4402 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004403 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004404 */
4405 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004406channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004407{
Bram Moolenaar77073442016-02-13 23:23:53 +01004408 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004409 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004410 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004411}
4412
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004413/*
4414 * Return the timeout of "channel"/"part"
4415 */
4416 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004417channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004418{
4419 return channel->ch_part[part].ch_timeout;
4420}
4421
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004422 static int
4423handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4424{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004425 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004426
4427 opt->jo_set |= jo;
4428 if (STRCMP(val, "nl") == 0)
4429 *modep = MODE_NL;
4430 else if (STRCMP(val, "raw") == 0)
4431 *modep = MODE_RAW;
4432 else if (STRCMP(val, "js") == 0)
4433 *modep = MODE_JS;
4434 else if (STRCMP(val, "json") == 0)
4435 *modep = MODE_JSON;
4436 else
4437 {
4438 EMSG2(_(e_invarg2), val);
4439 return FAIL;
4440 }
4441 return OK;
4442}
4443
4444 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004445handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004446{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004447 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004448
4449 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4450 if (STRCMP(val, "null") == 0)
4451 opt->jo_io[part] = JIO_NULL;
4452 else if (STRCMP(val, "pipe") == 0)
4453 opt->jo_io[part] = JIO_PIPE;
4454 else if (STRCMP(val, "file") == 0)
4455 opt->jo_io[part] = JIO_FILE;
4456 else if (STRCMP(val, "buffer") == 0)
4457 opt->jo_io[part] = JIO_BUFFER;
4458 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4459 opt->jo_io[part] = JIO_OUT;
4460 else
4461 {
4462 EMSG2(_(e_invarg2), val);
4463 return FAIL;
4464 }
4465 return OK;
4466}
4467
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004468/*
4469 * Clear a jobopt_T before using it.
4470 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004471 void
4472clear_job_options(jobopt_T *opt)
4473{
4474 vim_memset(opt, 0, sizeof(jobopt_T));
4475}
4476
4477/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004478 * Free any members of a jobopt_T.
4479 */
4480 void
4481free_job_options(jobopt_T *opt)
4482{
4483 if (opt->jo_partial != NULL)
4484 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004485 else if (opt->jo_callback != NULL)
4486 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004487 if (opt->jo_out_partial != NULL)
4488 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004489 else if (opt->jo_out_cb != NULL)
4490 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004491 if (opt->jo_err_partial != NULL)
4492 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004493 else if (opt->jo_err_cb != NULL)
4494 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004495 if (opt->jo_close_partial != NULL)
4496 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004497 else if (opt->jo_close_cb != NULL)
4498 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004499 if (opt->jo_exit_partial != NULL)
4500 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004501 else if (opt->jo_exit_cb != NULL)
4502 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004503 if (opt->jo_env != NULL)
4504 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004505}
4506
4507/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004508 * Get the PART_ number from the first character of an option name.
4509 */
4510 static int
4511part_from_char(int c)
4512{
4513 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4514}
4515
4516/*
4517 * Get the option entries from the dict in "tv", parse them and put the result
4518 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004519 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004520 * If an option value is invalid return FAIL.
4521 */
4522 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004523get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004524{
4525 typval_T *item;
4526 char_u *val;
4527 dict_T *dict;
4528 int todo;
4529 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004530 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004531
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004532 if (tv->v_type == VAR_UNKNOWN)
4533 return OK;
4534 if (tv->v_type != VAR_DICT)
4535 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004536 EMSG(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004537 return FAIL;
4538 }
4539 dict = tv->vval.v_dict;
4540 if (dict == NULL)
4541 return OK;
4542
4543 todo = (int)dict->dv_hashtab.ht_used;
4544 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4545 if (!HASHITEM_EMPTY(hi))
4546 {
4547 item = &dict_lookup(hi)->di_tv;
4548
4549 if (STRCMP(hi->hi_key, "mode") == 0)
4550 {
4551 if (!(supported & JO_MODE))
4552 break;
4553 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4554 return FAIL;
4555 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004556 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004557 {
4558 if (!(supported & JO_IN_MODE))
4559 break;
4560 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4561 == FAIL)
4562 return FAIL;
4563 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004564 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004565 {
4566 if (!(supported & JO_OUT_MODE))
4567 break;
4568 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4569 == FAIL)
4570 return FAIL;
4571 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004572 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004573 {
4574 if (!(supported & JO_ERR_MODE))
4575 break;
4576 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4577 == FAIL)
4578 return FAIL;
4579 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004580 else if (STRCMP(hi->hi_key, "noblock") == 0)
4581 {
4582 if (!(supported & JO_MODE))
4583 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004584 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004585 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004586 else if (STRCMP(hi->hi_key, "in_io") == 0
4587 || STRCMP(hi->hi_key, "out_io") == 0
4588 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004589 {
4590 if (!(supported & JO_OUT_IO))
4591 break;
4592 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4593 return FAIL;
4594 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004595 else if (STRCMP(hi->hi_key, "in_name") == 0
4596 || STRCMP(hi->hi_key, "out_name") == 0
4597 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004598 {
4599 part = part_from_char(*hi->hi_key);
4600
4601 if (!(supported & JO_OUT_IO))
4602 break;
4603 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4604 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004605 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004606 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004607 else if (STRCMP(hi->hi_key, "pty") == 0)
4608 {
4609 if (!(supported & JO_MODE))
4610 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004611 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004612 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004613 else if (STRCMP(hi->hi_key, "in_buf") == 0
4614 || STRCMP(hi->hi_key, "out_buf") == 0
4615 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004616 {
4617 part = part_from_char(*hi->hi_key);
4618
4619 if (!(supported & JO_OUT_IO))
4620 break;
4621 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004622 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004623 if (opt->jo_io_buf[part] <= 0)
4624 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004625 EMSG3(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004626 return FAIL;
4627 }
4628 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4629 {
4630 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4631 return FAIL;
4632 }
4633 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004634 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4635 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4636 {
4637 part = part_from_char(*hi->hi_key);
4638
4639 if (!(supported & JO_OUT_IO))
4640 break;
4641 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004642 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004643 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004644 else if (STRCMP(hi->hi_key, "out_msg") == 0
4645 || STRCMP(hi->hi_key, "err_msg") == 0)
4646 {
4647 part = part_from_char(*hi->hi_key);
4648
4649 if (!(supported & JO_OUT_IO))
4650 break;
4651 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004652 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004653 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004654 else if (STRCMP(hi->hi_key, "in_top") == 0
4655 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004656 {
4657 linenr_T *lp;
4658
4659 if (!(supported & JO_OUT_IO))
4660 break;
4661 if (hi->hi_key[3] == 't')
4662 {
4663 lp = &opt->jo_in_top;
4664 opt->jo_set |= JO_IN_TOP;
4665 }
4666 else
4667 {
4668 lp = &opt->jo_in_bot;
4669 opt->jo_set |= JO_IN_BOT;
4670 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004671 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004672 if (*lp < 0)
4673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004674 EMSG3(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004675 return FAIL;
4676 }
4677 }
4678 else if (STRCMP(hi->hi_key, "channel") == 0)
4679 {
4680 if (!(supported & JO_OUT_IO))
4681 break;
4682 opt->jo_set |= JO_CHANNEL;
4683 if (item->v_type != VAR_CHANNEL)
4684 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004685 EMSG2(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004686 return FAIL;
4687 }
4688 opt->jo_channel = item->vval.v_channel;
4689 }
4690 else if (STRCMP(hi->hi_key, "callback") == 0)
4691 {
4692 if (!(supported & JO_CALLBACK))
4693 break;
4694 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004695 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004696 if (opt->jo_callback == NULL)
4697 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004698 EMSG2(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004699 return FAIL;
4700 }
4701 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004702 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004703 {
4704 if (!(supported & JO_OUT_CALLBACK))
4705 break;
4706 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004707 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004708 if (opt->jo_out_cb == NULL)
4709 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004710 EMSG2(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004711 return FAIL;
4712 }
4713 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004714 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004715 {
4716 if (!(supported & JO_ERR_CALLBACK))
4717 break;
4718 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004719 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004720 if (opt->jo_err_cb == NULL)
4721 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004722 EMSG2(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004723 return FAIL;
4724 }
4725 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004726 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727 {
4728 if (!(supported & JO_CLOSE_CALLBACK))
4729 break;
4730 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004731 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004732 if (opt->jo_close_cb == NULL)
4733 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004734 EMSG2(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004735 return FAIL;
4736 }
4737 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004738 else if (STRCMP(hi->hi_key, "drop") == 0)
4739 {
4740 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004741 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004742
4743 if (STRCMP(val, "never") == 0)
4744 never = TRUE;
4745 else if (STRCMP(val, "auto") != 0)
4746 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004747 EMSG3(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004748 return FAIL;
4749 }
4750 opt->jo_drop_never = never;
4751 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004752 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4753 {
4754 if (!(supported & JO_EXIT_CB))
4755 break;
4756 opt->jo_set |= JO_EXIT_CB;
4757 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4758 if (opt->jo_exit_cb == NULL)
4759 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004760 EMSG2(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004761 return FAIL;
4762 }
4763 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004764#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004765 else if (STRCMP(hi->hi_key, "term_name") == 0)
4766 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004767 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004768 break;
4769 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004770 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004771 if (opt->jo_term_name == NULL)
4772 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004773 EMSG2(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004774 return FAIL;
4775 }
4776 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004777 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4778 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004779 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004780 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004781 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004782 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4783 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004784 EMSG3(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004785 return FAIL;
4786 }
4787 opt->jo_set2 |= JO2_TERM_FINISH;
4788 opt->jo_term_finish = *val;
4789 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004790 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4791 {
4792 char_u *p;
4793
4794 if (!(supported2 & JO2_TERM_OPENCMD))
4795 break;
4796 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004797 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004798 if (p != NULL)
4799 {
4800 /* Must have %d and no other %. */
4801 p = vim_strchr(p, '%');
4802 if (p != NULL && (p[1] != 'd'
4803 || vim_strchr(p + 2, '%') != NULL))
4804 p = NULL;
4805 }
4806 if (p == NULL)
4807 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004808 EMSG2(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004809 return FAIL;
4810 }
4811 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004812 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4813 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004814 char_u *p;
4815
4816 if (!(supported2 & JO2_EOF_CHARS))
4817 break;
4818 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004819 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004820 if (p == NULL)
4821 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004822 EMSG2(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004823 return FAIL;
4824 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004825 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004826 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4827 {
4828 if (!(supported2 & JO2_TERM_ROWS))
4829 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004830 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004831 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004832 }
4833 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4834 {
4835 if (!(supported2 & JO2_TERM_COLS))
4836 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004837 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004839 }
4840 else if (STRCMP(hi->hi_key, "vertical") == 0)
4841 {
4842 if (!(supported2 & JO2_VERTICAL))
4843 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004844 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004846 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004847 else if (STRCMP(hi->hi_key, "curwin") == 0)
4848 {
4849 if (!(supported2 & JO2_CURWIN))
4850 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004851 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004852 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004853 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004854 else if (STRCMP(hi->hi_key, "hidden") == 0)
4855 {
4856 if (!(supported2 & JO2_HIDDEN))
4857 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004858 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004859 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004860 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004861 else if (STRCMP(hi->hi_key, "norestore") == 0)
4862 {
4863 if (!(supported2 & JO2_NORESTORE))
4864 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004865 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004866 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004867 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004868 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4869 {
4870 if (!(supported2 & JO2_TERM_KILL))
4871 break;
4872 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004873 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004874 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004875# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4876 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004878 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004879 listitem_T *li;
4880 long_u rgb[16];
4881
4882 if (!(supported2 & JO2_ANSI_COLORS))
4883 break;
4884
4885 if (item == NULL || item->v_type != VAR_LIST
4886 || item->vval.v_list == NULL)
4887 {
4888 EMSG2(_(e_invargval), "ansi_colors");
4889 return FAIL;
4890 }
4891
4892 li = item->vval.v_list->lv_first;
4893 for (; li != NULL && n < 16; li = li->li_next, n++)
4894 {
4895 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004896 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004897
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004898 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004899 if (color_name == NULL)
4900 return FAIL;
4901
4902 guicolor = GUI_GET_COLOR(color_name);
4903 if (guicolor == INVALCOLOR)
4904 return FAIL;
4905
4906 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4907 }
4908
4909 if (n != 16 || li != NULL)
4910 {
4911 EMSG2(_(e_invargval), "ansi_colors");
4912 return FAIL;
4913 }
4914
4915 opt->jo_set2 |= JO2_ANSI_COLORS;
4916 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4917 }
4918# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004919#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004920 else if (STRCMP(hi->hi_key, "env") == 0)
4921 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004922 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004923 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004924 if (item->v_type != VAR_DICT)
4925 {
4926 EMSG2(_(e_invargval), "env");
4927 return FAIL;
4928 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004929 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004930 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004931 if (opt->jo_env != NULL)
4932 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004933 }
4934 else if (STRCMP(hi->hi_key, "cwd") == 0)
4935 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004936 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004937 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004938 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02004939 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02004940#ifndef WIN32 // Win32 directories don't have the concept of "executable"
4941 || mch_access((char *)opt->jo_cwd, X_OK) != 0
4942#endif
4943 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004944 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004945 EMSG2(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004946 return FAIL;
4947 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004948 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004949 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004950 else if (STRCMP(hi->hi_key, "waittime") == 0)
4951 {
4952 if (!(supported & JO_WAITTIME))
4953 break;
4954 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004955 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004956 }
4957 else if (STRCMP(hi->hi_key, "timeout") == 0)
4958 {
4959 if (!(supported & JO_TIMEOUT))
4960 break;
4961 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004963 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004964 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004965 {
4966 if (!(supported & JO_OUT_TIMEOUT))
4967 break;
4968 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004969 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004970 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004971 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004972 {
4973 if (!(supported & JO_ERR_TIMEOUT))
4974 break;
4975 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004976 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004977 }
4978 else if (STRCMP(hi->hi_key, "part") == 0)
4979 {
4980 if (!(supported & JO_PART))
4981 break;
4982 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004983 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004984 if (STRCMP(val, "err") == 0)
4985 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004986 else if (STRCMP(val, "out") == 0)
4987 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004988 else
4989 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004990 EMSG3(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004991 return FAIL;
4992 }
4993 }
4994 else if (STRCMP(hi->hi_key, "id") == 0)
4995 {
4996 if (!(supported & JO_ID))
4997 break;
4998 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004999 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005000 }
5001 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5002 {
5003 if (!(supported & JO_STOPONEXIT))
5004 break;
5005 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005006 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005007 opt->jo_soe_buf);
5008 if (opt->jo_stoponexit == NULL)
5009 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01005010 EMSG2(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005011 return FAIL;
5012 }
5013 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005014 else if (STRCMP(hi->hi_key, "block_write") == 0)
5015 {
5016 if (!(supported & JO_BLOCK_WRITE))
5017 break;
5018 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005019 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005020 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005021 else
5022 break;
5023 --todo;
5024 }
5025 if (todo > 0)
5026 {
5027 EMSG2(_(e_invarg2), hi->hi_key);
5028 return FAIL;
5029 }
5030
5031 return OK;
5032}
5033
5034/*
5035 * Get the channel from the argument.
5036 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005037 * When "check_open" is TRUE check that the channel can be used.
5038 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005039 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005040 */
5041 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005042get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005043{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005044 channel_T *channel = NULL;
5045 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005046
5047 if (tv->v_type == VAR_JOB)
5048 {
5049 if (tv->vval.v_job != NULL)
5050 channel = tv->vval.v_job->jv_channel;
5051 }
5052 else if (tv->v_type == VAR_CHANNEL)
5053 {
5054 channel = tv->vval.v_channel;
5055 }
5056 else
5057 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005058 EMSG2(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005059 return NULL;
5060 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005061 if (channel != NULL && reading)
5062 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005063 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005064
Bram Moolenaar437905c2016-04-26 19:01:05 +02005065 if (check_open && (channel == NULL || (!channel_is_open(channel)
5066 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005067 {
5068 EMSG(_("E906: not an open channel"));
5069 return NULL;
5070 }
5071 return channel;
5072}
5073
5074static job_T *first_job = NULL;
5075
5076 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005077job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005078{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005079 int i;
5080
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005081 ch_log(job->jv_channel, "Freeing job");
5082 if (job->jv_channel != NULL)
5083 {
5084 /* The link from the channel to the job doesn't count as a reference,
5085 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005086 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005087 * NULL the reference. We don't set ch_job_killed, unreferencing the
5088 * job doesn't mean it stops running. */
5089 job->jv_channel->ch_job = NULL;
5090 channel_unref(job->jv_channel);
5091 }
5092 mch_clear_job(job);
5093
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005094 vim_free(job->jv_tty_in);
5095 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005096 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005097 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005098 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005099 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005100 for (i = 0; job->jv_argv[i] != NULL; i++)
5101 vim_free(job->jv_argv[i]);
5102 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005103 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005104}
5105
5106 static void
5107job_free_job(job_T *job)
5108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005109 if (job->jv_next != NULL)
5110 job->jv_next->jv_prev = job->jv_prev;
5111 if (job->jv_prev == NULL)
5112 first_job = job->jv_next;
5113 else
5114 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005115 vim_free(job);
5116}
5117
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005118 static void
5119job_free(job_T *job)
5120{
5121 if (!in_free_unref_items)
5122 {
5123 job_free_contents(job);
5124 job_free_job(job);
5125 }
5126}
5127
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005128#if defined(EXITFREE) || defined(PROTO)
5129 void
5130job_free_all(void)
5131{
5132 while (first_job != NULL)
5133 job_free(first_job);
5134}
5135#endif
5136
5137/*
5138 * Return TRUE if we need to check if the process of "job" has ended.
5139 */
5140 static int
5141job_need_end_check(job_T *job)
5142{
5143 return job->jv_status == JOB_STARTED
5144 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5145}
5146
5147/*
5148 * Return TRUE if the channel of "job" is still useful.
5149 */
5150 static int
5151job_channel_still_useful(job_T *job)
5152{
5153 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5154}
5155
5156/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005157 * Return TRUE if the channel of "job" is closeable.
5158 */
5159 static int
5160job_channel_can_close(job_T *job)
5161{
5162 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5163}
5164
5165/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005166 * Return TRUE if the job should not be freed yet. Do not free the job when
5167 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5168 * or when the associated channel will do something with the job output.
5169 */
5170 static int
5171job_still_useful(job_T *job)
5172{
5173 return job_need_end_check(job) || job_channel_still_useful(job);
5174}
5175
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005176#if defined(GUI_MAY_FORK) || defined(PROTO)
5177/*
5178 * Return TRUE when there is any running job that we care about.
5179 */
5180 int
5181job_any_running()
5182{
5183 job_T *job;
5184
5185 for (job = first_job; job != NULL; job = job->jv_next)
5186 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005187 {
5188 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005189 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005190 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005191 return FALSE;
5192}
5193#endif
5194
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005195#if !defined(USE_ARGV) || defined(PROTO)
5196/*
5197 * Escape one argument for an external command.
5198 * Returns the escaped string in allocated memory. NULL when out of memory.
5199 */
5200 static char_u *
5201win32_escape_arg(char_u *arg)
5202{
5203 int slen, dlen;
5204 int escaping = 0;
5205 int i;
5206 char_u *s, *d;
5207 char_u *escaped_arg;
5208 int has_spaces = FALSE;
5209
5210 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005211 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005212 dlen = slen;
5213 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5214 {
5215 if (*s == '"' || *s == '\\')
5216 ++dlen;
5217 if (*s == ' ' || *s == '\t')
5218 has_spaces = TRUE;
5219 }
5220
5221 if (has_spaces)
5222 dlen += 2;
5223
5224 if (dlen == slen)
5225 return vim_strsave(arg);
5226
5227 /* Allocate memory for the result and fill it. */
5228 escaped_arg = alloc(dlen + 1);
5229 if (escaped_arg == NULL)
5230 return NULL;
5231 memset(escaped_arg, 0, dlen+1);
5232
5233 d = escaped_arg;
5234
5235 if (has_spaces)
5236 *d++ = '"';
5237
5238 for (s = arg; *s != NUL;)
5239 {
5240 switch (*s)
5241 {
5242 case '"':
5243 for (i = 0; i < escaping; i++)
5244 *d++ = '\\';
5245 escaping = 0;
5246 *d++ = '\\';
5247 *d++ = *s++;
5248 break;
5249 case '\\':
5250 escaping++;
5251 *d++ = *s++;
5252 break;
5253 default:
5254 escaping = 0;
5255 MB_COPY_CHAR(s, d);
5256 break;
5257 }
5258 }
5259
5260 /* add terminating quote and finish with a NUL */
5261 if (has_spaces)
5262 {
5263 for (i = 0; i < escaping; i++)
5264 *d++ = '\\';
5265 *d++ = '"';
5266 }
5267 *d = NUL;
5268
5269 return escaped_arg;
5270}
5271
5272/*
5273 * Build a command line from a list, taking care of escaping.
5274 * The result is put in gap->ga_data.
5275 * Returns FAIL when out of memory.
5276 */
5277 int
5278win32_build_cmd(list_T *l, garray_T *gap)
5279{
5280 listitem_T *li;
5281 char_u *s;
5282
5283 for (li = l->lv_first; li != NULL; li = li->li_next)
5284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005285 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005286 if (s == NULL)
5287 return FAIL;
5288 s = win32_escape_arg(s);
5289 if (s == NULL)
5290 return FAIL;
5291 ga_concat(gap, s);
5292 vim_free(s);
5293 if (li->li_next != NULL)
5294 ga_append(gap, ' ');
5295 }
5296 return OK;
5297}
5298#endif
5299
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005300/*
5301 * NOTE: Must call job_cleanup() only once right after the status of "job"
5302 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5303 * mch_detect_ended_job() returned non-NULL).
5304 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005305 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005306job_cleanup(job_T *job)
5307{
5308 if (job->jv_status != JOB_ENDED)
5309 return;
5310
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005311 /* Ready to cleanup the job. */
5312 job->jv_status = JOB_FINISHED;
5313
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005314 /* When only channel-in is kept open, close explicitly. */
5315 if (job->jv_channel != NULL)
5316 ch_close_part(job->jv_channel, PART_IN);
5317
Bram Moolenaar97792de2016-10-15 18:36:49 +02005318 if (job->jv_exit_cb != NULL)
5319 {
5320 typval_T argv[3];
5321 typval_T rettv;
5322 int dummy;
5323
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005324 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005325 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005326 ++job->jv_refcount;
5327 argv[0].v_type = VAR_JOB;
5328 argv[0].vval.v_job = job;
5329 argv[1].v_type = VAR_NUMBER;
5330 argv[1].vval.v_number = job->jv_exitval;
5331 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5332 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5333 job->jv_exit_partial, NULL);
5334 clear_tv(&rettv);
5335 --job->jv_refcount;
5336 channel_need_redraw = TRUE;
5337 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005338
5339 /* Do not free the job in case the close callback of the associated channel
5340 * isn't invoked yet and may get information by job_info(). */
5341 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005342 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005343 /* The job was already unreferenced and the associated channel was
5344 * detached, now that it ended it can be freed. Careful: caller must
5345 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005346 job_free(job);
5347 }
5348}
5349
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005350/*
5351 * Mark references in jobs that are still useful.
5352 */
5353 int
5354set_ref_in_job(int copyID)
5355{
5356 int abort = FALSE;
5357 job_T *job;
5358 typval_T tv;
5359
5360 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005361 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005362 {
5363 tv.v_type = VAR_JOB;
5364 tv.vval.v_job = job;
5365 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5366 }
5367 return abort;
5368}
5369
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005370/*
5371 * Dereference "job". Note that after this "job" may have been freed.
5372 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005373 void
5374job_unref(job_T *job)
5375{
5376 if (job != NULL && --job->jv_refcount <= 0)
5377 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005378 /* Do not free the job if there is a channel where the close callback
5379 * may get the job info. */
5380 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005381 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005382 /* Do not free the job when it has not ended yet and there is a
5383 * "stoponexit" flag or an exit callback. */
5384 if (!job_need_end_check(job))
5385 {
5386 job_free(job);
5387 }
5388 else if (job->jv_channel != NULL)
5389 {
5390 /* Do remove the link to the channel, otherwise it hangs
5391 * around until Vim exits. See job_free() for refcount. */
5392 ch_log(job->jv_channel, "detaching channel from job");
5393 job->jv_channel->ch_job = NULL;
5394 channel_unref(job->jv_channel);
5395 job->jv_channel = NULL;
5396 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005397 }
5398 }
5399}
5400
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005401 int
5402free_unused_jobs_contents(int copyID, int mask)
5403{
5404 int did_free = FALSE;
5405 job_T *job;
5406
5407 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005408 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005409 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005410 {
5411 /* Free the channel and ordinary items it contains, but don't
5412 * recurse into Lists, Dictionaries etc. */
5413 job_free_contents(job);
5414 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005415 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005416 return did_free;
5417}
5418
5419 void
5420free_unused_jobs(int copyID, int mask)
5421{
5422 job_T *job;
5423 job_T *job_next;
5424
5425 for (job = first_job; job != NULL; job = job_next)
5426 {
5427 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005428 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005429 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005430 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005431 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005432 job_free_job(job);
5433 }
5434 }
5435}
5436
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005437/*
5438 * Allocate a job. Sets the refcount to one and sets options default.
5439 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005440 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005441job_alloc(void)
5442{
5443 job_T *job;
5444
5445 job = (job_T *)alloc_clear(sizeof(job_T));
5446 if (job != NULL)
5447 {
5448 job->jv_refcount = 1;
5449 job->jv_stoponexit = vim_strsave((char_u *)"term");
5450
5451 if (first_job != NULL)
5452 {
5453 first_job->jv_prev = job;
5454 job->jv_next = first_job;
5455 }
5456 first_job = job;
5457 }
5458 return job;
5459}
5460
5461 void
5462job_set_options(job_T *job, jobopt_T *opt)
5463{
5464 if (opt->jo_set & JO_STOPONEXIT)
5465 {
5466 vim_free(job->jv_stoponexit);
5467 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5468 job->jv_stoponexit = NULL;
5469 else
5470 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5471 }
5472 if (opt->jo_set & JO_EXIT_CB)
5473 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005474 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005475 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005476 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005477 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005478 job->jv_exit_partial = NULL;
5479 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005480 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005481 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005482 job->jv_exit_partial = opt->jo_exit_partial;
5483 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005484 {
5485 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005486 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005487 }
5488 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005489 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005490 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005491 func_ref(job->jv_exit_cb);
5492 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005493 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005494 }
5495}
5496
5497/*
5498 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5499 */
5500 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005501job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005502{
5503 job_T *job;
5504
5505 for (job = first_job; job != NULL; job = job->jv_next)
5506 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005507 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005508}
5509
5510/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005511 * Return TRUE when there is any job that has an exit callback and might exit,
5512 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005513 */
5514 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005515has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005516{
5517 job_T *job;
5518
5519 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005520 /* Only should check if the channel has been closed, if the channel is
5521 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005522 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5523 || (job->jv_status == JOB_FINISHED
5524 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005525 return TRUE;
5526 return FALSE;
5527}
5528
Bram Moolenaar97792de2016-10-15 18:36:49 +02005529#define MAX_CHECK_ENDED 8
5530
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005531/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005532 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005533 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005534 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005535 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005536job_check_ended(void)
5537{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005538 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005539 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005540
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005541 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005542 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005543 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005544
Bram Moolenaar97792de2016-10-15 18:36:49 +02005545 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005546 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005547 // NOTE: mch_detect_ended_job() must only return a job of which the
5548 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005549 job_T *job = mch_detect_ended_job(first_job);
5550
5551 if (job == NULL)
5552 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005553 did_end = TRUE;
5554 job_cleanup(job); // may free "job"
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005555 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005556
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005557 if (channel_need_redraw)
5558 {
5559 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005560 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005561 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005562 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005563}
5564
5565/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005566 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005567 * "argv_arg" is only for Unix.
5568 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005569 * The returned job has a refcount of one.
5570 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005571 */
5572 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005573job_start(
5574 typval_T *argvars,
5575 char **argv_arg,
5576 jobopt_T *opt_arg,
5577 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005578{
5579 job_T *job;
5580 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005581 char **argv = NULL;
5582 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005583#if defined(UNIX)
5584# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005585 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005586#else
5587 garray_T ga;
5588#endif
5589 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005590 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005591
5592 job = job_alloc();
5593 if (job == NULL)
5594 return NULL;
5595
5596 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005597#ifndef USE_ARGV
5598 ga_init2(&ga, (int)sizeof(char*), 20);
5599#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005600
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005601 if (opt_arg != NULL)
5602 opt = *opt_arg;
5603 else
5604 {
5605 /* Default mode is NL. */
5606 clear_job_options(&opt);
5607 opt.jo_mode = MODE_NL;
5608 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005609 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5610 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5611 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005612 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005613 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005614
5615 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005616 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005617 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5618 && opt.jo_io[part] == JIO_FILE
5619 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5620 || *opt.jo_io_name[part] == NUL))
5621 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005622 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005623 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005624 }
5625
5626 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5627 {
5628 buf_T *buf = NULL;
5629
5630 /* check that we can find the buffer before starting the job */
5631 if (opt.jo_set & JO_IN_BUF)
5632 {
5633 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5634 if (buf == NULL)
5635 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5636 }
5637 else if (!(opt.jo_set & JO_IN_NAME))
5638 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005639 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005640 }
5641 else
5642 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5643 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005644 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005645 if (buf->b_ml.ml_mfp == NULL)
5646 {
5647 char_u numbuf[NUMBUFLEN];
5648 char_u *s;
5649
5650 if (opt.jo_set & JO_IN_BUF)
5651 {
5652 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5653 s = numbuf;
5654 }
5655 else
5656 s = opt.jo_io_name[PART_IN];
5657 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005658 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005659 }
5660 job->jv_in_buf = buf;
5661 }
5662
5663 job_set_options(job, &opt);
5664
Bram Moolenaar13568252018-03-16 20:46:58 +01005665#ifdef USE_ARGV
5666 if (argv_arg != NULL)
5667 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005668 /* Make a copy of argv_arg for job->jv_argv. */
5669 for (i = 0; argv_arg[i] != NULL; i++)
5670 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005671 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005672 if (argv == NULL)
5673 goto theend;
5674 for (i = 0; i < argc; i++)
5675 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5676 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005677 }
5678 else
5679#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005680 if (argvars[0].v_type == VAR_STRING)
5681 {
5682 /* Command is a string. */
5683 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005684 if (cmd == NULL || *cmd == NUL)
5685 {
5686 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005687 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005688 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005689
5690 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005691 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005692 }
5693 else if (argvars[0].v_type != VAR_LIST
5694 || argvars[0].vval.v_list == NULL
5695 || argvars[0].vval.v_list->lv_len < 1)
5696 {
5697 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005698 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005699 }
5700 else
5701 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005702 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005703
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005704 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005705 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005706#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005707 if (win32_build_cmd(l, &ga) == FAIL)
5708 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005709 cmd = ga.ga_data;
5710#endif
5711 }
5712
Bram Moolenaar20608922018-04-21 22:30:08 +02005713 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005714 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005715
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005716#ifdef USE_ARGV
5717 if (ch_log_active())
5718 {
5719 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005720
5721 ga_init2(&ga, (int)sizeof(char), 200);
5722 for (i = 0; i < argc; ++i)
5723 {
5724 if (i > 0)
5725 ga_concat(&ga, (char_u *)" ");
5726 ga_concat(&ga, (char_u *)argv[i]);
5727 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005728 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005729 ga_clear(&ga);
5730 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005731 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005732#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005733 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005734 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005735#endif
5736
5737 /* If the channel is reading from a buffer, write lines now. */
5738 if (job->jv_channel != NULL)
5739 channel_write_in(job->jv_channel);
5740
5741theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005742#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005743 vim_free(ga.ga_data);
5744#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005745 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005746 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005747 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005748 return job;
5749}
5750
5751/*
5752 * Get the status of "job" and invoke the exit callback when needed.
5753 * The returned string is not allocated.
5754 */
5755 char *
5756job_status(job_T *job)
5757{
5758 char *result;
5759
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005760 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005761 /* No need to check, dead is dead. */
5762 result = "dead";
5763 else if (job->jv_status == JOB_FAILED)
5764 result = "fail";
5765 else
5766 {
5767 result = mch_job_status(job);
5768 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005769 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005770 }
5771 return result;
5772}
5773
Bram Moolenaar8950a562016-03-12 15:22:55 +01005774/*
5775 * Implementation of job_info().
5776 */
5777 void
5778job_info(job_T *job, dict_T *dict)
5779{
5780 dictitem_T *item;
5781 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005782 list_T *l;
5783 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005784
Bram Moolenaare0be1672018-07-08 16:50:37 +02005785 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005786
5787 item = dictitem_alloc((char_u *)"channel");
5788 if (item == NULL)
5789 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005790 item->di_tv.v_type = VAR_CHANNEL;
5791 item->di_tv.vval.v_channel = job->jv_channel;
5792 if (job->jv_channel != NULL)
5793 ++job->jv_channel->ch_refcount;
5794 if (dict_add(dict, item) == FAIL)
5795 dictitem_free(item);
5796
5797#ifdef UNIX
5798 nr = job->jv_pid;
5799#else
5800 nr = job->jv_proc_info.dwProcessId;
5801#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005802 dict_add_number(dict, "process", nr);
5803 dict_add_string(dict, "tty_in", job->jv_tty_in);
5804 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005805
Bram Moolenaare0be1672018-07-08 16:50:37 +02005806 dict_add_number(dict, "exitval", job->jv_exitval);
5807 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5808 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005809
5810 l = list_alloc();
5811 if (l != NULL)
5812 {
5813 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005814 if (job->jv_argv != NULL)
5815 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005816 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005817 }
5818}
5819
5820/*
5821 * Implementation of job_info() to return info for all jobs.
5822 */
5823 void
5824job_info_all(list_T *l)
5825{
5826 job_T *job;
5827 typval_T tv;
5828
5829 for (job = first_job; job != NULL; job = job->jv_next)
5830 {
5831 tv.v_type = VAR_JOB;
5832 tv.vval.v_job = job;
5833
5834 if (list_append_tv(l, &tv) != OK)
5835 return;
5836 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005837}
5838
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005839/*
5840 * Send a signal to "job". Implements job_stop().
5841 * When "type" is not NULL use this for the type.
5842 * Otherwise use argvars[1] for the type.
5843 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005844 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005845job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005846{
5847 char_u *arg;
5848
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005849 if (type != NULL)
5850 arg = (char_u *)type;
5851 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005852 arg = (char_u *)"";
5853 else
5854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005855 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005856 if (arg == NULL)
5857 {
5858 EMSG(_(e_invarg));
5859 return 0;
5860 }
5861 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005862 if (job->jv_status == JOB_FAILED)
5863 {
5864 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5865 return 0;
5866 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005867 if (job->jv_status == JOB_ENDED)
5868 {
5869 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5870 return 0;
5871 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005872 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005873 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005874 return 0;
5875
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005876 /* Assume that only "kill" will kill the job. */
5877 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005878 job->jv_channel->ch_job_killed = TRUE;
5879
5880 /* We don't try freeing the job, obviously the caller still has a
5881 * reference to it. */
5882 return 1;
5883}
5884
Bram Moolenaarf2732452018-06-03 14:47:35 +02005885 void
5886invoke_prompt_callback(void)
5887{
5888 typval_T rettv;
5889 int dummy;
5890 typval_T argv[2];
5891 char_u *text;
5892 char_u *prompt;
5893 linenr_T lnum = curbuf->b_ml.ml_line_count;
5894
5895 // Add a new line for the prompt before invoking the callback, so that
5896 // text can always be inserted above the last line.
5897 ml_append(lnum, (char_u *)"", 0, FALSE);
5898 curwin->w_cursor.lnum = lnum + 1;
5899 curwin->w_cursor.col = 0;
5900
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005901 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005902 return;
5903 text = ml_get(lnum);
5904 prompt = prompt_text();
5905 if (STRLEN(text) >= STRLEN(prompt))
5906 text += STRLEN(prompt);
5907 argv[0].v_type = VAR_STRING;
5908 argv[0].vval.v_string = vim_strsave(text);
5909 argv[1].v_type = VAR_UNKNOWN;
5910
5911 call_func(curbuf->b_prompt_callback,
5912 (int)STRLEN(curbuf->b_prompt_callback),
5913 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5914 curbuf->b_prompt_partial, NULL);
5915 clear_tv(&argv[0]);
5916 clear_tv(&rettv);
5917}
5918
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005919/*
5920 * Return TRUE when the interrupt callback was invoked.
5921 */
5922 int
5923invoke_prompt_interrupt(void)
5924{
5925 typval_T rettv;
5926 int dummy;
5927 typval_T argv[1];
5928
5929 if (curbuf->b_prompt_interrupt == NULL
5930 || *curbuf->b_prompt_interrupt == NUL)
5931 return FALSE;
5932 argv[0].v_type = VAR_UNKNOWN;
5933
5934 got_int = FALSE; // don't skip executing commands
5935 call_func(curbuf->b_prompt_interrupt,
5936 (int)STRLEN(curbuf->b_prompt_interrupt),
5937 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5938 curbuf->b_prompt_int_partial, NULL);
5939 clear_tv(&rettv);
5940 return TRUE;
5941}
5942
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005943#endif /* FEAT_JOB_CHANNEL */