blob: 1680d0dd1e17efb2c11dc24085edf2b2c17390cf [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 Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
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)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200162ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200166 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167
Bram Moolenaar77073442016-02-13 23:23:53 +0100168 ch_log_lead("", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200169 va_start(ap, fmt);
170 vfprintf(log_fd, fmt, ap);
171 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100174 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 }
176}
177
178 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200179ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180{
181 if (log_fd != NULL)
182 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200183 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184
Bram Moolenaar77073442016-02-13 23:23:53 +0100185 ch_log_lead("ERR ", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200186 va_start(ap, fmt);
187 vfprintf(log_fd, fmt, ap);
188 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100194
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100195#ifdef _WIN32
196# undef PERROR
197# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
198 (char_u *)msg, (char_u *)strerror_win32(errno))
199
200 static char *
201strerror_win32(int eno)
202{
203 static LPVOID msgbuf = NULL;
204 char_u *ptr;
205
206 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200207 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100208 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200209 msgbuf = NULL;
210 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100211 FormatMessage(
212 FORMAT_MESSAGE_ALLOCATE_BUFFER |
213 FORMAT_MESSAGE_FROM_SYSTEM |
214 FORMAT_MESSAGE_IGNORE_INSERTS,
215 NULL,
216 eno,
217 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
218 (LPTSTR) &msgbuf,
219 0,
220 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200221 if (msgbuf != NULL)
222 /* chomp \r or \n */
223 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
224 switch (*ptr)
225 {
226 case '\r':
227 STRMOVE(ptr, ptr + 1);
228 ptr--;
229 break;
230 case '\n':
231 if (*(ptr + 1) == '\0')
232 *ptr = '\0';
233 else
234 *ptr = ' ';
235 break;
236 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100237 return msgbuf;
238}
239#endif
240
Bram Moolenaar77073442016-02-13 23:23:53 +0100241/*
242 * The list of all allocated channels.
243 */
244static channel_T *first_channel = NULL;
245static int next_ch_id = 0;
246
247/*
248 * Allocate a new channel. The refcount is set to 1.
249 * The channel isn't actually used until it is opened.
250 * Returns NULL if out of memory.
251 */
252 channel_T *
253add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100254{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200255 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100256 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100257
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if (channel == NULL)
259 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100260
Bram Moolenaar77073442016-02-13 23:23:53 +0100261 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100262 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100263
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200264 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100265 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100266 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100267#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100268 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100269#endif
270#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100271 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100272#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100273 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100274 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100275
Bram Moolenaar77073442016-02-13 23:23:53 +0100276 if (first_channel != NULL)
277 {
278 first_channel->ch_prev = channel;
279 channel->ch_next = first_channel;
280 }
281 first_channel = channel;
282
283 channel->ch_refcount = 1;
284 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100285}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100286
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200287 int
288has_any_channel(void)
289{
290 return first_channel != NULL;
291}
292
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100293/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100294 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100295 * Return TRUE if "channel" has a callback and the associated job wasn't
296 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100297 */
298 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100299channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100300{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100301 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100302 int has_out_msg;
303 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100304
305 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100306 if (channel->ch_job_killed && channel->ch_job == NULL)
307 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100308
309 /* If there is a close callback it may still need to be invoked. */
310 if (channel->ch_close_cb != NULL)
311 return TRUE;
312
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200313 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200314 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200315 return TRUE;
316
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100317 /* If there is no callback then nobody can get readahead. If the fd is
318 * closed and there is no readahead then the callback won't be called. */
319 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
320 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
321 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100322 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
323 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
324 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
325 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
326 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
327 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100328 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100329 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200330 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
332 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200333 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200334 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
335 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100336}
337
338/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200339 * Close a channel and free all its resources.
340 */
341 static void
342channel_free_contents(channel_T *channel)
343{
344 channel_close(channel, TRUE);
345 channel_clear(channel);
346 ch_log(channel, "Freeing channel");
347}
348
349 static void
350channel_free_channel(channel_T *channel)
351{
352 if (channel->ch_next != NULL)
353 channel->ch_next->ch_prev = channel->ch_prev;
354 if (channel->ch_prev == NULL)
355 first_channel = channel->ch_next;
356 else
357 channel->ch_prev->ch_next = channel->ch_next;
358 vim_free(channel);
359}
360
361 static void
362channel_free(channel_T *channel)
363{
364 if (!in_free_unref_items)
365 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200366 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200367 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200368 else
369 {
370 channel_free_contents(channel);
371 channel_free_channel(channel);
372 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 }
374}
375
376/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100378 * possible, there is no callback to be invoked or the associated job was
379 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100380 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100382 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100383channel_may_free(channel_T *channel)
384{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100385 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100386 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100387 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100388 return TRUE;
389 }
390 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100391}
392
393/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100394 * Decrement the reference count on "channel" and maybe free it when it goes
395 * down to zero. Don't free it if there is a pending action.
396 * Returns TRUE when the channel is no longer referenced.
397 */
398 int
399channel_unref(channel_T *channel)
400{
401 if (channel != NULL && --channel->ch_refcount <= 0)
402 return channel_may_free(channel);
403 return FALSE;
404}
405
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200406 int
407free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100408{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200409 int did_free = FALSE;
410 channel_T *ch;
411
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200412 /* This is invoked from the garbage collector, which only runs at a safe
413 * point. */
414 ++safe_to_invoke_callback;
415
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200416 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200417 if (!channel_still_useful(ch)
418 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200419 {
420 /* Free the channel and ordinary items it contains, but don't
421 * recurse into Lists, Dictionaries etc. */
422 channel_free_contents(ch);
423 did_free = TRUE;
424 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425
426 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200427 return did_free;
428}
429
430 void
431free_unused_channels(int copyID, int mask)
432{
433 channel_T *ch;
434 channel_T *ch_next;
435
436 for (ch = first_channel; ch != NULL; ch = ch_next)
437 {
438 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200439 if (!channel_still_useful(ch)
440 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200441 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200442 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200443 channel_free_channel(ch);
444 }
445 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100446}
447
Bram Moolenaard04a0202016-01-26 23:30:18 +0100448#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100449
450#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
451 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100452channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100453{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100454 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200455 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100456
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100457 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100458 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200459 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100460 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200461 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100462}
463#endif
464
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100466 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100468#ifdef FEAT_GUI_X11
469 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200470messageFromServer(XtPointer clientData,
471 int *unused1 UNUSED,
472 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100473{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100474 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100475}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100476#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100477
Bram Moolenaard04a0202016-01-26 23:30:18 +0100478#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100479# if GTK_CHECK_VERSION(3,0,0)
480 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200481messageFromServer(GIOChannel *unused1 UNUSED,
482 GIOCondition unused2 UNUSED,
483 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100484{
485 channel_read_fd(GPOINTER_TO_INT(clientData));
486 return TRUE; /* Return FALSE instead in case the event source is to
487 * be removed after this function returns. */
488}
489# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100490 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200491messageFromServer(gpointer clientData,
492 gint unused1 UNUSED,
493 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100495 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
Bram Moolenaar98921892016-02-23 17:14:37 +0100497# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498#endif
499
500 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200501channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100502{
Bram Moolenaarde279892016-03-11 22:19:44 +0100503 if (!CH_HAS_GUI)
504 return;
505
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100506# ifdef FEAT_GUI_X11
507 /* Tell notifier we are interested in being called
508 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100509 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
510 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100511 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100512 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100513 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200514 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100515 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100516# else
517# ifdef FEAT_GUI_GTK
518 /* Tell gdk we are interested in being called when there
519 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100520 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100521# if GTK_CHECK_VERSION(3,0,0)
522 {
523 GIOChannel *chnnl = g_io_channel_unix_new(
524 (gint)channel->ch_part[part].ch_fd);
525
526 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
527 chnnl,
528 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200529 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100530 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
531
532 g_io_channel_unref(chnnl);
533 }
534# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100535 channel->ch_part[part].ch_inputHandler = gdk_input_add(
536 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100537 (GdkInputCondition)
538 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200539 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100540 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100541# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100542# endif
543# endif
544}
545
Bram Moolenaarde279892016-03-11 22:19:44 +0100546 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100547channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100548{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100549 if (channel->CH_SOCK_FD != INVALID_FD)
550 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100551 if (channel->CH_OUT_FD != INVALID_FD)
552 channel_gui_register_one(channel, PART_OUT);
553 if (channel->CH_ERR_FD != INVALID_FD)
554 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100555}
556
557/*
558 * Register any of our file descriptors with the GUI event handling system.
559 * Called when the GUI has started.
560 */
561 void
562channel_gui_register_all(void)
563{
Bram Moolenaar77073442016-02-13 23:23:53 +0100564 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100565
Bram Moolenaar77073442016-02-13 23:23:53 +0100566 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100567 channel_gui_register(channel);
568}
569
570 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200571channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100572{
573# ifdef FEAT_GUI_X11
574 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
575 {
576 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
577 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
578 }
579# else
580# ifdef FEAT_GUI_GTK
581 if (channel->ch_part[part].ch_inputHandler != 0)
582 {
583# if GTK_CHECK_VERSION(3,0,0)
584 g_source_remove(channel->ch_part[part].ch_inputHandler);
585# else
586 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
587# endif
588 channel->ch_part[part].ch_inputHandler = 0;
589 }
590# endif
591# endif
592}
593
594 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100595channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100596{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200597 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100598
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100599 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100600 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100601}
602
603#endif
604
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100605static char *e_cannot_connect = N_("E902: Cannot connect to port");
606
Bram Moolenaard04a0202016-01-26 23:30:18 +0100607/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100608 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100609 * "waittime" is the time in msec to wait for the connection.
610 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100611 * Returns the channel for success.
612 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100613 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100614 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100615channel_open(
616 char *hostname,
617 int port_in,
618 int waittime,
619 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100620{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100621 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100623 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100624#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100625 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100626 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100627#else
628 int port = port_in;
629#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100631 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100632
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100633#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100634 channel_init_winsock();
635#endif
636
Bram Moolenaar77073442016-02-13 23:23:53 +0100637 channel = add_channel();
638 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100639 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100640 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100641 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100642 }
643
644 /* Get the server internet address and put into addr structure */
645 /* fill in the socket address structure and connect to server */
646 vim_memset((char *)&server, 0, sizeof(server));
647 server.sin_family = AF_INET;
648 server.sin_port = htons(port);
649 if ((host = gethostbyname(hostname)) == NULL)
650 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100651 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200652 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100653 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100654 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100656 {
657 char *p;
658
659 /* When using host->h_addr directly ubsan warns for it to not be
660 * aligned. First copy the pointer to aviod that. */
661 memcpy(&p, &host->h_addr, sizeof(p));
662 memcpy((char *)&server.sin_addr, p, host->h_length);
663 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100665 /* On Mac and Solaris a zero timeout almost never works. At least wait
666 * one millisecond. Let's do it for all systems, because we don't know why
667 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100668 if (waittime == 0)
669 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670
671 /*
672 * For Unix we need to call connect() again after connect() failed.
673 * On Win32 one time is sufficient.
674 */
675 while (TRUE)
676 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100677 long elapsed_msec = 0;
678 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100679
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 sd = socket(AF_INET, SOCK_STREAM, 0);
683 if (sd == -1)
684 {
685 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200686 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100687 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100690
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100691 if (waittime >= 0)
692 {
693 /* Make connect() non-blocking. */
694 if (
695#ifdef _WIN32
696 ioctlsocket(sd, FIONBIO, &val) < 0
697#else
698 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
699#endif
700 )
701 {
702 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200703 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100704 "channel_open: Connect failed with errno %d", errno);
705 sock_close(sd);
706 channel_free(channel);
707 return NULL;
708 }
709 }
710
711 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200712 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100713 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
714
Bram Moolenaar045a2842016-03-08 22:33:07 +0100715 if (ret == 0)
716 /* The connection could be established. */
717 break;
718
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100719 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100720 if (waittime < 0 || (errno != EWOULDBLOCK
721 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100722#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100723 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100724#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100725 ))
726 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200727 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100728 "channel_open: Connect failed with errno %d", errno);
729 PERROR(_(e_cannot_connect));
730 sock_close(sd);
731 channel_free(channel);
732 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100733 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100734
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100735 /* Limit the waittime to 50 msec. If it doesn't work within this
736 * time we close the socket and try creating it again. */
737 waitnow = waittime > 50 ? 50 : waittime;
738
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100740 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100741#ifndef WIN32
742 if (errno != ECONNREFUSED)
743#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100744 {
745 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100746 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100747 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100748#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100749 int so_error = 0;
750 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100751 struct timeval start_tv;
752 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100753#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100754 FD_ZERO(&rfds);
755 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100756 FD_ZERO(&wfds);
757 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100758
Bram Moolenaar562ca712016-03-09 21:50:05 +0100759 tv.tv_sec = waitnow / 1000;
760 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100761#ifndef WIN32
762 gettimeofday(&start_tv, NULL);
763#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200764 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100765 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100766 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100767
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768 if (ret < 0)
769 {
770 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200771 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100772 "channel_open: Connect failed with errno %d", errno);
773 PERROR(_(e_cannot_connect));
774 sock_close(sd);
775 channel_free(channel);
776 return NULL;
777 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100778
Bram Moolenaare081e212016-02-28 22:33:46 +0100779#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100780 /* On Win32: select() is expected to work and wait for up to
781 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100782 if (FD_ISSET(sd, &wfds))
783 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100784 elapsed_msec = waitnow;
785 if (waittime > 1 && elapsed_msec < waittime)
786 {
787 waittime -= elapsed_msec;
788 continue;
789 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100790#else
791 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100792 * After putting the socket in non-blocking mode, connect() will
793 * return EINPROGRESS, select() will not wait (as if writing is
794 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100795 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100796 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100797 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100798 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100799 {
800 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100801 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100802 if (ret < 0 || (so_error != 0
803 && so_error != EWOULDBLOCK
804 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100805# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100807# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100808 ))
809 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200810 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100811 "channel_open: Connect failed with errno %d",
812 so_error);
813 PERROR(_(e_cannot_connect));
814 sock_close(sd);
815 channel_free(channel);
816 return NULL;
817 }
818 }
819
Bram Moolenaar045a2842016-03-08 22:33:07 +0100820 if (FD_ISSET(sd, &wfds) && so_error == 0)
821 /* Did not detect an error, connection is established. */
822 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100823
Bram Moolenaar045a2842016-03-08 22:33:07 +0100824 gettimeofday(&end_tv, NULL);
825 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
826 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100827#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100828 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100829
830#ifndef WIN32
831 if (waittime > 1 && elapsed_msec < waittime)
832 {
833 /* The port isn't ready but we also didn't get an error.
834 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100835 * yet. Select() may return early, wait until the remaining
836 * "waitnow" and try again. */
837 waitnow -= elapsed_msec;
838 waittime -= elapsed_msec;
839 if (waitnow > 0)
840 {
841 mch_delay((long)waitnow, TRUE);
842 ui_breakcheck();
843 waittime -= waitnow;
844 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100845 if (!got_int)
846 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100847 if (waittime <= 0)
848 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100849 waittime = 1;
850 continue;
851 }
852 /* we were interrupted, behave as if timed out */
853 }
854#endif
855
856 /* We timed out. */
857 ch_error(channel, "Connection timed out");
858 sock_close(sd);
859 channel_free(channel);
860 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100861 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100862
Bram Moolenaar045a2842016-03-08 22:33:07 +0100863 ch_log(channel, "Connection made");
864
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100865 if (waittime >= 0)
866 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100867#ifdef _WIN32
868 val = 0;
869 ioctlsocket(sd, FIONBIO, &val);
870#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100871 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100872#endif
873 }
874
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100875 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100876 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100877 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
878 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200879 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100880
881#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100882 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100883#endif
884
Bram Moolenaar77073442016-02-13 23:23:53 +0100885 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100886}
887
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100888/*
889 * Implements ch_open().
890 */
891 channel_T *
892channel_open_func(typval_T *argvars)
893{
894 char_u *address;
895 char_u *p;
896 char *rest;
897 int port;
898 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200899 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100900
901 address = get_tv_string(&argvars[0]);
902 if (argvars[1].v_type != VAR_UNKNOWN
903 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
904 {
905 EMSG(_(e_invarg));
906 return NULL;
907 }
908
909 /* parse address */
910 p = vim_strchr(address, ':');
911 if (p == NULL)
912 {
913 EMSG2(_(e_invarg2), address);
914 return NULL;
915 }
916 *p++ = NUL;
917 port = strtol((char *)p, &rest, 10);
918 if (*address == NUL || port <= 0 || *rest != NUL)
919 {
920 p[-1] = ':';
921 EMSG2(_(e_invarg2), address);
922 return NULL;
923 }
924
925 /* parse options */
926 clear_job_options(&opt);
927 opt.jo_mode = MODE_JSON;
928 opt.jo_timeout = 2000;
929 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200930 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200931 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100932 if (opt.jo_timeout < 0)
933 {
934 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200935 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100936 }
937
938 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
939 if (channel != NULL)
940 {
941 opt.jo_set = JO_ALL;
942 channel_set_options(channel, &opt);
943 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200944theend:
945 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100946 return channel;
947}
948
Bram Moolenaarde279892016-03-11 22:19:44 +0100949 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200950ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100951{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200952 sock_T *fd = &channel->ch_part[part].ch_fd;
953
Bram Moolenaarde279892016-03-11 22:19:44 +0100954 if (*fd != INVALID_FD)
955 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200956 if (part == PART_SOCK)
957 sock_close(*fd);
958 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200959 {
960 /* When using a pty the same FD is set on multiple parts, only
961 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200962 if ((part == PART_IN || channel->CH_IN_FD != *fd)
963 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
964 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200965 fd_close(*fd);
966 }
Bram Moolenaarde279892016-03-11 22:19:44 +0100967 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200968
969 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +0100970 }
971}
972
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100973 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100974channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100975{
Bram Moolenaarde279892016-03-11 22:19:44 +0100976 if (in != INVALID_FD)
977 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200978 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +0100979 channel->CH_IN_FD = in;
980 }
981 if (out != INVALID_FD)
982 {
983# if defined(FEAT_GUI)
984 channel_gui_unregister_one(channel, PART_OUT);
985# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200986 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +0100987 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200988 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +0100989# if defined(FEAT_GUI)
990 channel_gui_register_one(channel, PART_OUT);
991# endif
992 }
993 if (err != INVALID_FD)
994 {
995# if defined(FEAT_GUI)
996 channel_gui_unregister_one(channel, PART_ERR);
997# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200998 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +0100999 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001000 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001001# if defined(FEAT_GUI)
1002 channel_gui_register_one(channel, PART_ERR);
1003# endif
1004 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001005}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001006
Bram Moolenaard6051b52016-02-28 15:49:03 +01001007/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001008 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001009 * This does not keep a refcount, when the job is freed ch_job is cleared.
1010 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001011 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001012channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001013{
Bram Moolenaar77073442016-02-13 23:23:53 +01001014 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001015
1016 channel_set_options(channel, options);
1017
1018 if (job->jv_in_buf != NULL)
1019 {
1020 chanpart_T *in_part = &channel->ch_part[PART_IN];
1021
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001022 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001023 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001024 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001025 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001026 {
1027 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1028 {
1029 /* Special mode: send last-but-one line when appending a line
1030 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001031 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001032 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001033 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001034 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001035 }
1036 else
1037 in_part->ch_buf_top = options->jo_in_top;
1038 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001039 else
1040 in_part->ch_buf_top = 1;
1041 if (options->jo_set & JO_IN_BOT)
1042 in_part->ch_buf_bot = options->jo_in_bot;
1043 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001044 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001045 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001046}
1047
1048/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001049 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001050 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001051 */
1052 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001053find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001054{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001055 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001056 buf_T *save_curbuf = curbuf;
1057
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001058 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001059 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001060 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001061 if (buf == NULL)
1062 buf = buflist_findname_exp(name);
1063 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001064 if (buf == NULL)
1065 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001066 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001067 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001068 if (buf == NULL)
1069 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001070 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001071 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001072#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001073 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1074 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001075#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001076 if (curbuf->b_ml.ml_mfp == NULL)
1077 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001078 if (msg)
1079 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001080 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001081 changed_bytes(1, 0);
1082 curbuf = save_curbuf;
1083 }
1084
1085 return buf;
1086}
1087
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001088 static void
1089set_callback(
1090 char_u **cbp,
1091 partial_T **pp,
1092 char_u *callback,
1093 partial_T *partial)
1094{
1095 free_callback(*cbp, *pp);
1096 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001097 {
1098 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001099 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001100 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001101 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001102 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001103 func_ref(*cbp);
1104 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001105 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001106 else
1107 *cbp = NULL;
1108 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001109 if (partial != NULL)
1110 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001111}
1112
Bram Moolenaar187db502016-02-27 14:44:26 +01001113/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001115 */
1116 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001117channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001118{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001119 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001120
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001121 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001122 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001123 channel->ch_part[part].ch_mode = opt->jo_mode;
1124 if (opt->jo_set & JO_IN_MODE)
1125 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1126 if (opt->jo_set & JO_OUT_MODE)
1127 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1128 if (opt->jo_set & JO_ERR_MODE)
1129 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001130
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001131 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001132 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001133 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1134 if (opt->jo_set & JO_OUT_TIMEOUT)
1135 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1136 if (opt->jo_set & JO_ERR_TIMEOUT)
1137 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001138 if (opt->jo_set & JO_BLOCK_WRITE)
1139 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140
1141 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001142 set_callback(&channel->ch_callback, &channel->ch_partial,
1143 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001144 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001145 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1146 &channel->ch_part[PART_OUT].ch_partial,
1147 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001148 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001149 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1150 &channel->ch_part[PART_ERR].ch_partial,
1151 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001152 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001153 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1154 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001155 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001156
1157 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1158 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001159 buf_T *buf;
1160
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001161 /* writing output to a buffer. Default mode is NL. */
1162 if (!(opt->jo_set & JO_OUT_MODE))
1163 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001164 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001165 {
1166 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1167 if (buf == NULL)
1168 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1169 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001170 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001171 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001172 int msg = TRUE;
1173
1174 if (opt->jo_set2 & JO2_OUT_MSG)
1175 msg = opt->jo_message[PART_OUT];
1176 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001177 }
1178 if (buf != NULL)
1179 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001180 if (opt->jo_set & JO_OUT_MODIFIABLE)
1181 channel->ch_part[PART_OUT].ch_nomodifiable =
1182 !opt->jo_modifiable[PART_OUT];
1183
1184 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1185 {
1186 EMSG(_(e_modifiable));
1187 }
1188 else
1189 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001190 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001191 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001192 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001193 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001194 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001195 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001196
1197 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1198 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1199 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1200 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001201 buf_T *buf;
1202
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001203 /* writing err to a buffer. Default mode is NL. */
1204 if (!(opt->jo_set & JO_ERR_MODE))
1205 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1206 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001207 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001208 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001209 {
1210 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1211 if (buf == NULL)
1212 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1213 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001214 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001215 {
1216 int msg = TRUE;
1217
1218 if (opt->jo_set2 & JO2_ERR_MSG)
1219 msg = opt->jo_message[PART_ERR];
1220 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1221 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 if (buf != NULL)
1223 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001224 if (opt->jo_set & JO_ERR_MODIFIABLE)
1225 channel->ch_part[PART_ERR].ch_nomodifiable =
1226 !opt->jo_modifiable[PART_ERR];
1227 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1228 {
1229 EMSG(_(e_modifiable));
1230 }
1231 else
1232 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001233 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001234 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001235 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001236 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001237 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001238 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001239
1240 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1241 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1242 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001243}
1244
1245/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001246 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001247 */
1248 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001249channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001250 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001251 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001252 char_u *callback,
1253 partial_T *partial,
1254 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001255{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001256 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001257 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1258
1259 if (item != NULL)
1260 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001261 item->cq_partial = partial;
1262 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001263 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001264 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001265 item->cq_callback = callback;
1266 }
1267 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001268 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001269 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001270 func_ref(item->cq_callback);
1271 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001272 item->cq_seq_nr = id;
1273 item->cq_prev = head->cq_prev;
1274 head->cq_prev = item;
1275 item->cq_next = NULL;
1276 if (item->cq_prev == NULL)
1277 head->cq_next = item;
1278 else
1279 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001280 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001281}
1282
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001283 static void
1284write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1285{
1286 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001287 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001288 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001289 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001290
Bram Moolenaar655da312016-05-28 22:22:34 +02001291 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001292 if ((p = alloc(len + 2)) == NULL)
1293 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001294 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001295
1296 for (i = 0; i < len; ++i)
1297 if (p[i] == NL)
1298 p[i] = NUL;
1299
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001300 p[len] = NL;
1301 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001302 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001303 vim_free(p);
1304}
1305
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001306/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001307 * Return TRUE if "channel" can be written to.
1308 * Returns FALSE if the input is closed or the write would block.
1309 */
1310 static int
1311can_write_buf_line(channel_T *channel)
1312{
1313 chanpart_T *in_part = &channel->ch_part[PART_IN];
1314
1315 if (in_part->ch_fd == INVALID_FD)
1316 return FALSE; /* pipe was closed */
1317
1318 /* for testing: block every other attempt to write */
1319 if (in_part->ch_block_write == 1)
1320 in_part->ch_block_write = -1;
1321 else if (in_part->ch_block_write == -1)
1322 in_part->ch_block_write = 1;
1323
1324 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1325#ifndef WIN32
1326 {
1327# if defined(HAVE_SELECT)
1328 struct timeval tval;
1329 fd_set wfds;
1330 int ret;
1331
1332 FD_ZERO(&wfds);
1333 FD_SET((int)in_part->ch_fd, &wfds);
1334 tval.tv_sec = 0;
1335 tval.tv_usec = 0;
1336 for (;;)
1337 {
1338 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1339# ifdef EINTR
1340 SOCK_ERRNO;
1341 if (ret == -1 && errno == EINTR)
1342 continue;
1343# endif
1344 if (ret <= 0 || in_part->ch_block_write == 1)
1345 {
1346 if (ret > 0)
1347 ch_log(channel, "FAKED Input not ready for writing");
1348 else
1349 ch_log(channel, "Input not ready for writing");
1350 return FALSE;
1351 }
1352 break;
1353 }
1354# else
1355 struct pollfd fds;
1356
1357 fds.fd = in_part->ch_fd;
1358 fds.events = POLLOUT;
1359 if (poll(&fds, 1, 0) <= 0)
1360 {
1361 ch_log(channel, "Input not ready for writing");
1362 return FALSE;
1363 }
1364 if (in_part->ch_block_write == 1)
1365 {
1366 ch_log(channel, "FAKED Input not ready for writing");
1367 return FALSE;
1368 }
1369# endif
1370 }
1371#endif
1372 return TRUE;
1373}
1374
1375/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001376 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001377 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001378 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001379channel_write_in(channel_T *channel)
1380{
1381 chanpart_T *in_part = &channel->ch_part[PART_IN];
1382 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001383 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001384 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001385
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001386 if (buf == NULL || in_part->ch_buf_append)
1387 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001388 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001389 {
1390 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001391 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001392 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001393 return;
1394 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001395
1396 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1397 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1398 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001399 if (!can_write_buf_line(channel))
1400 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001401 write_buf_line(buf, lnum, channel);
1402 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001403 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001404
1405 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001406 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001407 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001408 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001409
Bram Moolenaar014069a2016-03-03 22:51:40 +01001410 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001411 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001412 {
1413 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001414 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001416
1417 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001418 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001419 }
1420 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001421 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001422 buf->b_ml.ml_line_count - lnum + 1);
1423}
1424
1425/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001426 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001427 */
1428 void
1429channel_buffer_free(buf_T *buf)
1430{
1431 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001432 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001433
1434 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001435 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001436 {
1437 chanpart_T *ch_part = &channel->ch_part[part];
1438
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001439 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001440 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001441 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001442 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001443 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001444 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001445 }
1446}
1447
1448/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001449 * Write any lines waiting to be written to "channel".
1450 */
1451 static void
1452channel_write_input(channel_T *channel)
1453{
1454 chanpart_T *in_part = &channel->ch_part[PART_IN];
1455
1456 if (in_part->ch_writeque.wq_next != NULL)
1457 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1458 else if (in_part->ch_bufref.br_buf != NULL)
1459 {
1460 if (in_part->ch_buf_append)
1461 channel_write_new_lines(in_part->ch_bufref.br_buf);
1462 else
1463 channel_write_in(channel);
1464 }
1465}
1466
1467/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 * Write any lines waiting to be written to a channel.
1469 */
1470 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001471channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001472{
1473 channel_T *channel;
1474
1475 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001476 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001477}
1478
1479/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001480 * Write appended lines above the last one in "buf" to the channel.
1481 */
1482 void
1483channel_write_new_lines(buf_T *buf)
1484{
1485 channel_T *channel;
1486 int found_one = FALSE;
1487
1488 /* There could be more than one channel for the buffer, loop over all of
1489 * them. */
1490 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1491 {
1492 chanpart_T *in_part = &channel->ch_part[PART_IN];
1493 linenr_T lnum;
1494 int written = 0;
1495
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001496 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001497 {
1498 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001499 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001500 found_one = TRUE;
1501 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1502 ++lnum)
1503 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 if (!can_write_buf_line(channel))
1505 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506 write_buf_line(buf, lnum, channel);
1507 ++written;
1508 }
1509
1510 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001511 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001512 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001513 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001514 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001515 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001516 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001517
1518 in_part->ch_buf_bot = lnum;
1519 }
1520 }
1521 if (!found_one)
1522 buf->b_write_to_channel = FALSE;
1523}
1524
1525/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001526 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001527 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001528 */
1529 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001530invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1531 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001532{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001533 typval_T rettv;
1534 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001535
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001536 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001537 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001538
Bram Moolenaar77073442016-02-13 23:23:53 +01001539 argv[0].v_type = VAR_CHANNEL;
1540 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001541
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001542 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1543 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001544 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001545 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001546}
1547
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001548/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001549 * Return the first node from "channel"/"part" without removing it.
1550 * Returns NULL if there is nothing.
1551 */
1552 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001553channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001554{
1555 readq_T *head = &channel->ch_part[part].ch_head;
1556
1557 return head->rq_next;
1558}
1559
1560/*
1561 * Return a pointer to the first NL in "node".
1562 * Skips over NUL characters.
1563 * Returns NULL if there is no NL.
1564 */
1565 char_u *
1566channel_first_nl(readq_T *node)
1567{
1568 char_u *buffer = node->rq_buffer;
1569 long_u i;
1570
1571 for (i = 0; i < node->rq_buflen; ++i)
1572 if (buffer[i] == NL)
1573 return buffer + i;
1574 return NULL;
1575}
1576
1577/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001578 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001579 * The caller must free it.
1580 * Returns NULL if there is nothing.
1581 */
1582 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001583channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001584{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001585 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001586 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001587 char_u *p;
1588
Bram Moolenaar77073442016-02-13 23:23:53 +01001589 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001590 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001591 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001592 p = node->rq_buffer;
1593 head->rq_next = node->rq_next;
1594 if (node->rq_next == NULL)
1595 head->rq_prev = NULL;
1596 else
1597 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001598 vim_free(node);
1599 return p;
1600}
1601
1602/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001603 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001604 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001605 */
1606 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001607channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001608{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001609 readq_T *head = &channel->ch_part[part].ch_head;
1610 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001611 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001612 char_u *res;
1613 char_u *p;
1614
1615 /* If there is only one buffer just get that one. */
1616 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1617 return channel_get(channel, part);
1618
1619 /* Concatenate everything into one buffer. */
1620 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001621 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001622 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001623 if (res == NULL)
1624 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001625 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001626 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001627 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001628 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001629 p += node->rq_buflen;
1630 }
1631 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001632
1633 /* Free all buffers */
1634 do
1635 {
1636 p = channel_get(channel, part);
1637 vim_free(p);
1638 } while (p != NULL);
1639
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001640 /* turn all NUL into NL */
1641 while (len > 0)
1642 {
1643 --len;
1644 if (res[len] == NUL)
1645 res[len] = NL;
1646 }
1647
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001648 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001649}
1650
1651/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001652 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001653 * Caller must check these bytes are available.
1654 */
1655 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001656channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001657{
1658 readq_T *head = &channel->ch_part[part].ch_head;
1659 readq_T *node = head->rq_next;
1660 char_u *buf = node->rq_buffer;
1661
1662 mch_memmove(buf, buf + len, node->rq_buflen - len);
1663 node->rq_buflen -= len;
1664}
1665
1666/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001667 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001668 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001669 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670 */
1671 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001672channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001674 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001675 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001676 readq_T *last_node;
1677 readq_T *n;
1678 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001679 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001680 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681
Bram Moolenaar77073442016-02-13 23:23:53 +01001682 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001683 return FAIL;
1684
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001685 last_node = node->rq_next;
1686 len = node->rq_buflen + last_node->rq_buflen + 1;
1687 if (want_nl)
1688 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001689 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001690 {
1691 last_node = last_node->rq_next;
1692 len += last_node->rq_buflen;
1693 }
1694
1695 p = newbuf = alloc(len);
1696 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001698 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001699 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001700 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001701 node->rq_buffer = newbuf;
1702 for (n = node; n != last_node; )
1703 {
1704 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001705 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001706 p += n->rq_buflen;
1707 vim_free(n->rq_buffer);
1708 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001709 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001710
1711 /* dispose of the collapsed nodes and their buffers */
1712 for (n = node->rq_next; n != last_node; )
1713 {
1714 n = n->rq_next;
1715 vim_free(n->rq_prev);
1716 }
1717 node->rq_next = last_node->rq_next;
1718 if (last_node->rq_next == NULL)
1719 head->rq_prev = node;
1720 else
1721 last_node->rq_next->rq_prev = node;
1722 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001723 return OK;
1724}
1725
1726/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001728 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001729 * Returns OK or FAIL.
1730 */
1731 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001732channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001733 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001734{
1735 readq_T *node;
1736 readq_T *head = &channel->ch_part[part].ch_head;
1737 char_u *p;
1738 int i;
1739
1740 node = (readq_T *)alloc(sizeof(readq_T));
1741 if (node == NULL)
1742 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001743 /* A NUL is added at the end, because netbeans code expects that.
1744 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001745 node->rq_buffer = alloc(len + 1);
1746 if (node->rq_buffer == NULL)
1747 {
1748 vim_free(node);
1749 return FAIL; /* out of memory */
1750 }
1751
1752 if (channel->ch_part[part].ch_mode == MODE_NL)
1753 {
1754 /* Drop any CR before a NL. */
1755 p = node->rq_buffer;
1756 for (i = 0; i < len; ++i)
1757 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1758 *p++ = buf[i];
1759 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001760 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001761 }
1762 else
1763 {
1764 mch_memmove(node->rq_buffer, buf, len);
1765 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001766 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767 }
1768
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001769 if (prepend)
1770 {
1771 /* preend node to the head of the queue */
1772 node->rq_next = head->rq_next;
1773 node->rq_prev = NULL;
1774 if (head->rq_next == NULL)
1775 head->rq_prev = node;
1776 else
1777 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001779 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001780 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001781 {
1782 /* append node to the tail of the queue */
1783 node->rq_next = NULL;
1784 node->rq_prev = head->rq_prev;
1785 if (head->rq_prev == NULL)
1786 head->rq_next = node;
1787 else
1788 head->rq_prev->rq_next = node;
1789 head->rq_prev = node;
1790 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001791
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001792 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001794 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001795 fprintf(log_fd, "'");
1796 if (fwrite(buf, len, 1, log_fd) != 1)
1797 return FAIL;
1798 fprintf(log_fd, "'\n");
1799 }
1800 return OK;
1801}
1802
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001803/*
1804 * Try to fill the buffer of "reader".
1805 * Returns FALSE when nothing was added.
1806 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001807 static int
1808channel_fill(js_read_T *reader)
1809{
1810 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001811 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001812 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001813 int keeplen;
1814 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001815 char_u *p;
1816
1817 if (next == NULL)
1818 return FALSE;
1819
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001820 keeplen = reader->js_end - reader->js_buf;
1821 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001822 {
1823 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001824 addlen = (int)STRLEN(next);
1825 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001826 if (p == NULL)
1827 {
1828 vim_free(next);
1829 return FALSE;
1830 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001831 mch_memmove(p, reader->js_buf, keeplen);
1832 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001833 vim_free(next);
1834 next = p;
1835 }
1836
1837 vim_free(reader->js_buf);
1838 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001839 return TRUE;
1840}
1841
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001842/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001843 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001845 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001846 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001848channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849{
1850 js_read_T reader;
1851 typval_T listtv;
1852 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001853 chanpart_T *chanpart = &channel->ch_part[part];
1854 jsonq_T *head = &chanpart->ch_json_head;
1855 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001856 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001859 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001860
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001861 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001863 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001864 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001865 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001866
1867 /* When a message is incomplete we wait for a short while for more to
1868 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001869 * or list will make us hang.
1870 * Do not generate error messages, they will be written in a channel log. */
1871 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001872 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001873 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001874 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001875 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001877 /* Only accept the response when it is a list with at least two
1878 * items. */
1879 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001880 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001881 if (listtv.v_type != VAR_LIST)
1882 ch_error(channel, "Did not receive a list, discarding");
1883 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001884 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001885 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001886 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001887 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001888 else
1889 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001890 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1891 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001893 else
1894 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001895 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001896 item->jq_value = alloc_tv();
1897 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001898 {
1899 vim_free(item);
1900 clear_tv(&listtv);
1901 }
1902 else
1903 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001904 *item->jq_value = listtv;
1905 item->jq_prev = head->jq_prev;
1906 head->jq_prev = item;
1907 item->jq_next = NULL;
1908 if (item->jq_prev == NULL)
1909 head->jq_next = item;
1910 else
1911 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001912 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001913 }
1914 }
1915 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001916
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001917 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001918 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001919 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001920 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001921 size_t buflen = STRLEN(reader.js_buf);
1922
1923 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001924 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001925 /* First time encountering incomplete message or after receiving
1926 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001927 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001928 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001929 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001930 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001931 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001932#ifdef WIN32
1933 chanpart->ch_deadline = GetTickCount() + 100L;
1934#else
1935 gettimeofday(&chanpart->ch_deadline, NULL);
1936 chanpart->ch_deadline.tv_usec += 100 * 1000;
1937 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1938 {
1939 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1940 ++chanpart->ch_deadline.tv_sec;
1941 }
1942#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001943 }
1944 else
1945 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001946 int timeout;
1947#ifdef WIN32
1948 timeout = GetTickCount() > chanpart->ch_deadline;
1949#else
1950 {
1951 struct timeval now_tv;
1952
1953 gettimeofday(&now_tv, NULL);
1954 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1955 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1956 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1957 }
1958#endif
1959 if (timeout)
1960 {
1961 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001962 chanpart->ch_wait_len = 0;
1963 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001964 }
1965 else
1966 {
1967 reader.js_used = 0;
1968 ch_log(channel, "still waiting on incomplete message");
1969 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001970 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001971 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001972
1973 if (status == FAIL)
1974 {
1975 ch_error(channel, "Decoding failed - discarding input");
1976 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001977 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 }
1979 else if (reader.js_buf[reader.js_used] != NUL)
1980 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001981 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001982 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001983 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1984 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001985 ret = status == MAYBE ? FALSE: TRUE;
1986 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001987 else
1988 ret = FALSE;
1989
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001990 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001991 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001992}
1993
1994/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001995 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001996 */
1997 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001998remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001999{
Bram Moolenaar77073442016-02-13 23:23:53 +01002000 if (node->cq_prev == NULL)
2001 head->cq_next = node->cq_next;
2002 else
2003 node->cq_prev->cq_next = node->cq_next;
2004 if (node->cq_next == NULL)
2005 head->cq_prev = node->cq_prev;
2006 else
2007 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002008}
2009
2010/*
2011 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002012 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002013 */
2014 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002015remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016{
Bram Moolenaar77073442016-02-13 23:23:53 +01002017 if (node->jq_prev == NULL)
2018 head->jq_next = node->jq_next;
2019 else
2020 node->jq_prev->jq_next = node->jq_next;
2021 if (node->jq_next == NULL)
2022 head->jq_prev = node->jq_prev;
2023 else
2024 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002025 vim_free(node);
2026}
2027
2028/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002029 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002030 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002031 * When "id" is zero or negative jut get the first message. But not the one
2032 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002033 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002034 * Return OK when found and return the value in "rettv".
2035 * Return FAIL otherwise.
2036 */
2037 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002038channel_get_json(
2039 channel_T *channel,
2040 ch_part_T part,
2041 int id,
2042 int without_callback,
2043 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002044{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002045 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002046 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047
Bram Moolenaar77073442016-02-13 23:23:53 +01002048 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002049 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002051 typval_T *tv = &l->lv_first->li_tv;
2052
Bram Moolenaar958dc692016-12-01 15:34:12 +01002053 if ((without_callback || !item->jq_no_callback)
2054 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002055 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002056 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002057 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002058 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002059 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002060 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002061 ch_log(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002062 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002063 return OK;
2064 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002065 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066 }
2067 return FAIL;
2068}
2069
Bram Moolenaar958dc692016-12-01 15:34:12 +01002070/*
2071 * Put back "rettv" into the JSON queue, there was no callback for it.
2072 * Takes over the values in "rettv".
2073 */
2074 static void
2075channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2076{
2077 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2078 jsonq_T *item = head->jq_next;
2079 jsonq_T *newitem;
2080
2081 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2082 /* last item was pushed back, append to the end */
2083 item = NULL;
2084 else while (item != NULL && item->jq_no_callback)
2085 /* append after the last item that was pushed back */
2086 item = item->jq_next;
2087
2088 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2089 if (newitem == NULL)
2090 clear_tv(rettv);
2091 else
2092 {
2093 newitem->jq_value = alloc_tv();
2094 if (newitem->jq_value == NULL)
2095 {
2096 vim_free(newitem);
2097 clear_tv(rettv);
2098 }
2099 else
2100 {
2101 newitem->jq_no_callback = FALSE;
2102 *newitem->jq_value = *rettv;
2103 if (item == NULL)
2104 {
2105 /* append to the end */
2106 newitem->jq_prev = head->jq_prev;
2107 head->jq_prev = newitem;
2108 newitem->jq_next = NULL;
2109 if (newitem->jq_prev == NULL)
2110 head->jq_next = newitem;
2111 else
2112 newitem->jq_prev->jq_next = newitem;
2113 }
2114 else
2115 {
2116 /* append after "item" */
2117 newitem->jq_prev = item;
2118 newitem->jq_next = item->jq_next;
2119 item->jq_next = newitem;
2120 if (newitem->jq_next == NULL)
2121 head->jq_prev = newitem;
2122 else
2123 newitem->jq_next->jq_prev = newitem;
2124 }
2125 }
2126 }
2127}
2128
Bram Moolenaarece61b02016-02-20 21:39:05 +01002129#define CH_JSON_MAX_ARGS 4
2130
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002131/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002132 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002133 * "argv[0]" is the command string.
2134 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002135 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002136 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002137channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002138{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002139 char_u *cmd = argv[0].vval.v_string;
2140 char_u *arg;
2141 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002142
Bram Moolenaarece61b02016-02-20 21:39:05 +01002143 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002144 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002145 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002146 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002147 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002148 return;
2149 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002150 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002151 if (arg == NULL)
2152 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002153
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002154 if (STRCMP(cmd, "ex") == 0)
2155 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002156 int save_called_emsg = called_emsg;
2157
2158 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002159 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002160 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002161 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002162 --emsg_silent;
2163 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002164 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002165 (char *)get_vim_var_str(VV_ERRMSG));
2166 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002167 }
2168 else if (STRCMP(cmd, "normal") == 0)
2169 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002170 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002171
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002172 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002173 ea.arg = arg;
2174 ea.addr_count = 0;
2175 ea.forceit = TRUE; /* no mapping */
2176 ex_normal(&ea);
2177 }
2178 else if (STRCMP(cmd, "redraw") == 0)
2179 {
2180 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002181
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002182 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002183 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002184 ex_redraw(&ea);
2185 showruler(FALSE);
2186 setcursor();
2187 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002188#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002189 if (gui.in_use)
2190 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002191 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002193 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002194#endif
2195 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002196 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002197 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 int is_call = cmd[0] == 'c';
2199 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002200
Bram Moolenaarece61b02016-02-20 21:39:05 +01002201 if (argv[id_idx].v_type != VAR_UNKNOWN
2202 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002204 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002205 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002206 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002207 }
2208 else if (is_call && argv[2].v_type != VAR_LIST)
2209 {
2210 ch_error(channel, "third argument for call must be a list");
2211 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002212 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002213 }
2214 else
2215 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002216 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002217 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002218 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002219 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002220
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002221 /* Don't pollute the display with errors. */
2222 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002223 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002224 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002225 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002226 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002227 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002228 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002229 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002230 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002231 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2232 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002233 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002234
2235 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002236 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002237 int id = argv[id_idx].vval.v_number;
2238
Bram Moolenaar55fab432016-02-07 16:53:13 +01002239 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002240 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002241 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002242 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002243 /* If evaluation failed or the result can't be encoded
2244 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002245 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246 err_tv.v_type = VAR_STRING;
2247 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002248 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002249 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002250 if (json != NULL)
2251 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002252 channel_send(channel,
2253 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002254 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002255 vim_free(json);
2256 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002257 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002258 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002259 if (tv == &res_tv)
2260 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002261 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002262 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002263 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002264 }
2265 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002266 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002267 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002268 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002269 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002270}
2271
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002272/*
2273 * Invoke the callback at "cbhead".
2274 * Does not redraw but sets channel_need_redraw.
2275 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002276 static void
2277invoke_one_time_callback(
2278 channel_T *channel,
2279 cbq_T *cbhead,
2280 cbq_T *item,
2281 typval_T *argv)
2282{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002283 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002284 (char *)item->cq_callback);
2285 /* Remove the item from the list first, if the callback
2286 * invokes ch_close() the list will be cleared. */
2287 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002288 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002289 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002290 vim_free(item);
2291}
2292
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002293 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002294append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002295{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002296 bufref_T save_curbuf = {NULL, 0, 0};
2297 win_T *save_curwin = NULL;
2298 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002299 linenr_T lnum = buffer->b_ml.ml_line_count;
2300 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002301 chanpart_T *ch_part = &channel->ch_part[part];
2302 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002303 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002304
2305 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2306 {
2307 if (!ch_part->ch_nomod_error)
2308 {
2309 ch_error(channel, "Buffer is not modifiable, cannot append");
2310 ch_part->ch_nomod_error = TRUE;
2311 }
2312 return;
2313 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002314
2315 /* If the buffer is also used as input insert above the last
2316 * line. Don't write these lines. */
2317 if (save_write_to)
2318 {
2319 --lnum;
2320 buffer->b_write_to_channel = FALSE;
2321 }
2322
2323 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002324 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002325
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002326 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002327
2328 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2329 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2330
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002331 u_sync(TRUE);
2332 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002333 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002334
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002335 if (empty)
2336 {
2337 /* The buffer is empty, replace the first (dummy) line. */
2338 ml_replace(lnum, msg, TRUE);
2339 lnum = 0;
2340 }
2341 else
2342 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002343 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002344
2345 /* Restore curbuf/curwin/curtab */
2346 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2347
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002348 if (ch_part->ch_nomodifiable)
2349 buffer->b_p_ma = FALSE;
2350 else
2351 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002352
2353 if (buffer->b_nwindows > 0)
2354 {
2355 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002356
2357 FOR_ALL_WINDOWS(wp)
2358 {
2359 if (wp->w_buffer == buffer
2360 && (save_write_to
2361 ? wp->w_cursor.lnum == lnum + 1
2362 : (wp->w_cursor.lnum == lnum
2363 && wp->w_cursor.col == 0)))
2364 {
2365 ++wp->w_cursor.lnum;
2366 save_curwin = curwin;
2367 curwin = wp;
2368 curbuf = curwin->w_buffer;
2369 scroll_cursor_bot(0, FALSE);
2370 curwin = save_curwin;
2371 curbuf = curwin->w_buffer;
2372 }
2373 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002374 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002375 channel_need_redraw = TRUE;
2376 }
2377
2378 if (save_write_to)
2379 {
2380 channel_T *ch;
2381
2382 /* Find channels reading from this buffer and adjust their
2383 * next-to-read line number. */
2384 buffer->b_write_to_channel = TRUE;
2385 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2386 {
2387 chanpart_T *in_part = &ch->ch_part[PART_IN];
2388
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002389 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002390 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2391 }
2392 }
2393}
2394
Bram Moolenaar437905c2016-04-26 19:01:05 +02002395 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002396drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002397{
2398 char_u *msg;
2399
2400 while ((msg = channel_get(channel, part)) != NULL)
2401 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002402 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002403 vim_free(msg);
2404 }
2405}
2406
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002407/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002408 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002409 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002410 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002411 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002412 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002413may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002414{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002415 char_u *msg = NULL;
2416 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002417 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002418 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002419 chanpart_T *ch_part = &channel->ch_part[part];
2420 ch_mode_T ch_mode = ch_part->ch_mode;
2421 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002422 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002423 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002424 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002425 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002426 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002427
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002428 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002429 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002430 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002431
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002432 /* Use a message-specific callback, part callback or channel callback */
2433 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2434 if (cbitem->cq_seq_nr == 0)
2435 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002436 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002437 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002438 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002439 partial = cbitem->cq_partial;
2440 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002441 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002442 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002443 callback = ch_part->ch_callback;
2444 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002445 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002446 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002447 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002448 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002449 partial = channel->ch_partial;
2450 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002451
Bram Moolenaarec68a992016-10-03 21:37:41 +02002452 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002453 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2454 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002455 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002456 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002457 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002458 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002459 buffer = NULL;
2460 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002462 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002463 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002464 listitem_T *item;
2465 int argc = 0;
2466
Bram Moolenaard7ece102016-02-02 23:23:02 +01002467 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002468 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002469 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002470 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002471 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002472 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002473 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002474 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002475
Bram Moolenaarece61b02016-02-20 21:39:05 +01002476 for (item = listtv->vval.v_list->lv_first;
2477 item != NULL && argc < CH_JSON_MAX_ARGS;
2478 item = item->li_next)
2479 argv[argc++] = item->li_tv;
2480 while (argc < CH_JSON_MAX_ARGS)
2481 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002482
Bram Moolenaarece61b02016-02-20 21:39:05 +01002483 if (argv[0].v_type == VAR_STRING)
2484 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002485 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002486 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002487 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002488 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002489 }
2490
Bram Moolenaarece61b02016-02-20 21:39:05 +01002491 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002492 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002493 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002494 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002495 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002496 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002497 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002498 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002499 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002500 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002501 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002502 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002503 return FALSE;
2504 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002505 else
2506 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002507 /* If there is no callback or buffer drop the message. */
2508 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002509 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002510 /* If there is a close callback it may use ch_read() to get the
2511 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002512 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002513 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002514 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002515 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002516
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002517 if (ch_mode == MODE_NL)
2518 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002519 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002520 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002521 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002522
2523 /* See if we have a message ending in NL in the first buffer. If
2524 * not try to concatenate the first and the second buffer. */
2525 while (TRUE)
2526 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002527 node = channel_peek(channel, part);
2528 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002529 if (nl != NULL)
2530 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002531 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002532 {
2533 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2534 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002535 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002536 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002537 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002538 buf = node->rq_buffer;
2539
Bram Moolenaarec68a992016-10-03 21:37:41 +02002540 if (nl == NULL)
2541 {
2542 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002543 char_u *new_buf;
2544
2545 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2546 if (new_buf == NULL)
2547 /* This might fail over and over again, should the message
2548 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002549 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002550 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002551 node->rq_buffer = buf;
2552 nl = buf + node->rq_buflen++;
2553 *nl = NUL;
2554 }
2555
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002556 /* Convert NUL to NL, the internal representation. */
2557 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2558 if (*p == NUL)
2559 *p = NL;
2560
2561 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002562 {
2563 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002564 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002565 *nl = NUL;
2566 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002567 else
2568 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002569 /* Copy the message into allocated memory (excluding the NL)
2570 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002571 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002572 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002573 }
2574 }
2575 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002576 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002577 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002578 * get everything we have.
2579 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002580 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002581 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002582
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002583 if (msg == NULL)
2584 return FALSE; /* out of memory (and avoids Coverity warning) */
2585
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002586 argv[1].v_type = VAR_STRING;
2587 argv[1].vval.v_string = msg;
2588 }
2589
Bram Moolenaara07fec92016-02-05 21:04:08 +01002590 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002591 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002592 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002593
Bram Moolenaar958dc692016-12-01 15:34:12 +01002594 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002595 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002596 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002597 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002598 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002599 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002600 break;
2601 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002602 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002603 {
2604 if (channel->ch_drop_never)
2605 {
2606 /* message must be read with ch_read() */
2607 channel_push_json(channel, part, listtv);
2608 listtv = NULL;
2609 }
2610 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002611 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002612 seq_nr);
2613 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002614 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002615 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002616 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002617 if (buffer != NULL)
2618 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002619 if (msg == NULL)
2620 /* JSON or JS mode: re-encode the message. */
2621 msg = json_encode(listtv, ch_mode);
2622 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002623 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002624#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002625 if (buffer->b_term != NULL)
2626 write_to_term(buffer, msg, channel);
2627 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002628#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002629 append_to_buffer(buffer, msg, channel, part);
2630 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002631 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002632
Bram Moolenaar187db502016-02-27 14:44:26 +01002633 if (callback != NULL)
2634 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002635 if (cbitem != NULL)
2636 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2637 else
2638 {
2639 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002640 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002641 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002642 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002643 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002644 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002645 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002646 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002647 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002648
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002649 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002650 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002651 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002652
2653 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002654}
2655
2656/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002657 * Return TRUE when channel "channel" is open for writing to.
2658 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002659 */
2660 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002661channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002662{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002663 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002664 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002665}
2666
2667/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002668 * Return TRUE when channel "channel" is open for reading or writing.
2669 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002670 */
2671 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002672channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002673{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002675 || channel->CH_IN_FD != INVALID_FD
2676 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002677 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002678}
2679
2680/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002681 * Return TRUE if "channel" has JSON or other typeahead.
2682 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002683 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002684channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002685{
2686 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2687
2688 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2689 {
2690 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2691 jsonq_T *item = head->jq_next;
2692
2693 return item != NULL;
2694 }
2695 return channel_peek(channel, part) != NULL;
2696}
2697
2698/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002699 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002700 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002701 */
2702 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002703channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002704{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002705 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002706 int has_readahead = FALSE;
2707
Bram Moolenaar77073442016-02-13 23:23:53 +01002708 if (channel == NULL)
2709 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002710 if (req_part == PART_OUT)
2711 {
2712 if (channel->CH_OUT_FD != INVALID_FD)
2713 return "open";
2714 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002715 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002716 }
2717 else if (req_part == PART_ERR)
2718 {
2719 if (channel->CH_ERR_FD != INVALID_FD)
2720 return "open";
2721 if (channel_has_readahead(channel, PART_ERR))
2722 has_readahead = TRUE;
2723 }
2724 else
2725 {
2726 if (channel_is_open(channel))
2727 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002728 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002729 if (channel_has_readahead(channel, part))
2730 {
2731 has_readahead = TRUE;
2732 break;
2733 }
2734 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002735
2736 if (has_readahead)
2737 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002738 return "closed";
2739}
2740
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002741 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002742channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002743{
2744 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002745 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002746 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002747 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002748 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002749
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002750 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002751 STRCAT(namebuf, "_");
2752 tail = STRLEN(namebuf);
2753
2754 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002755 if (chanpart->ch_fd != INVALID_FD)
2756 status = "open";
2757 else if (channel_has_readahead(channel, part))
2758 status = "buffered";
2759 else
2760 status = "closed";
2761 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002762
2763 STRCPY(namebuf + tail, "mode");
2764 switch (chanpart->ch_mode)
2765 {
2766 case MODE_NL: s = "NL"; break;
2767 case MODE_RAW: s = "RAW"; break;
2768 case MODE_JSON: s = "JSON"; break;
2769 case MODE_JS: s = "JS"; break;
2770 }
2771 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2772
2773 STRCPY(namebuf + tail, "io");
2774 if (part == PART_SOCK)
2775 s = "socket";
2776 else switch (chanpart->ch_io)
2777 {
2778 case JIO_NULL: s = "null"; break;
2779 case JIO_PIPE: s = "pipe"; break;
2780 case JIO_FILE: s = "file"; break;
2781 case JIO_BUFFER: s = "buffer"; break;
2782 case JIO_OUT: s = "out"; break;
2783 }
2784 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2785
2786 STRCPY(namebuf + tail, "timeout");
2787 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2788}
2789
2790 void
2791channel_info(channel_T *channel, dict_T *dict)
2792{
2793 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002794 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002795
2796 if (channel->ch_hostname != NULL)
2797 {
2798 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2799 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2800 channel_part_info(channel, dict, "sock", PART_SOCK);
2801 }
2802 else
2803 {
2804 channel_part_info(channel, dict, "out", PART_OUT);
2805 channel_part_info(channel, dict, "err", PART_ERR);
2806 channel_part_info(channel, dict, "in", PART_IN);
2807 }
2808}
2809
Bram Moolenaar77073442016-02-13 23:23:53 +01002810/*
2811 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002812 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002813 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002814 */
2815 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002816channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002817{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002818 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002819
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002820#ifdef FEAT_GUI
2821 channel_gui_unregister(channel);
2822#endif
2823
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002824 ch_close_part(channel, PART_SOCK);
2825 ch_close_part(channel, PART_IN);
2826 ch_close_part(channel, PART_OUT);
2827 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002828
Bram Moolenaara9f02812017-08-05 17:40:38 +02002829 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002830 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002831 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002832
Bram Moolenaara9f02812017-08-05 17:40:38 +02002833 /* Invoke callbacks and flush buffers before the close callback. */
2834 if (channel->ch_close_cb != NULL)
2835 ch_log(channel,
2836 "Invoking callbacks and flushing buffers before closing");
2837 for (part = PART_SOCK; part < PART_IN; ++part)
2838 {
2839 if (channel->ch_close_cb != NULL
2840 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2841 {
2842 /* Increment the refcount to avoid the channel being freed
2843 * halfway. */
2844 ++channel->ch_refcount;
2845 if (channel->ch_close_cb == NULL)
2846 ch_log(channel, "flushing %s buffers before closing",
2847 part_names[part]);
2848 while (may_invoke_callback(channel, part))
2849 ;
2850 --channel->ch_refcount;
2851 }
2852 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002853
Bram Moolenaara9f02812017-08-05 17:40:38 +02002854 if (channel->ch_close_cb != NULL)
2855 {
2856 typval_T argv[1];
2857 typval_T rettv;
2858 int dummy;
2859
2860 /* Increment the refcount to avoid the channel being freed
2861 * halfway. */
2862 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002863 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002864 (char *)channel->ch_close_cb);
2865 argv[0].v_type = VAR_CHANNEL;
2866 argv[0].vval.v_channel = channel;
2867 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002868 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002869 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002870 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002871 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002872
Bram Moolenaara9f02812017-08-05 17:40:38 +02002873 /* the callback is only called once */
2874 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2875 channel->ch_close_cb = NULL;
2876 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002877
Bram Moolenaara9f02812017-08-05 17:40:38 +02002878 --channel->ch_refcount;
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002879
Bram Moolenaara9f02812017-08-05 17:40:38 +02002880 if (channel_need_redraw)
2881 {
2882 channel_need_redraw = FALSE;
2883 redraw_after_callback();
2884 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002885
Bram Moolenaara9f02812017-08-05 17:40:38 +02002886 if (!channel->ch_drop_never)
2887 /* any remaining messages are useless now */
2888 for (part = PART_SOCK; part < PART_IN; ++part)
2889 drop_messages(channel, part);
2890 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002891 }
2892
2893 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002894
2895#ifdef FEAT_TERMINAL
2896 term_channel_closed(channel);
2897#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002898}
2899
Bram Moolenaard04a0202016-01-26 23:30:18 +01002900/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002901 * Close the "in" part channel "channel".
2902 */
2903 void
2904channel_close_in(channel_T *channel)
2905{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002906 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002907}
2908
2909/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002910 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002911 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002912 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002913channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002914{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002915 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2916 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002917
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002918 while (channel_peek(channel, part) != NULL)
2919 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002920
2921 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002922 {
2923 cbq_T *node = cb_head->cq_next;
2924
2925 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002926 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002927 vim_free(node);
2928 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002929
2930 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002931 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002932 free_tv(json_head->jq_next->jq_value);
2933 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002934 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002935
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002936 free_callback(channel->ch_part[part].ch_callback,
2937 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002938 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002939 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002940}
2941
2942/*
2943 * Clear all the read buffers on "channel".
2944 */
2945 void
2946channel_clear(channel_T *channel)
2947{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002948 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002949 vim_free(channel->ch_hostname);
2950 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002951 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002952 channel_clear_one(channel, PART_OUT);
2953 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002954 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002955 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002956 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002957 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002958 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002959 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002960 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002961}
2962
Bram Moolenaar77073442016-02-13 23:23:53 +01002963#if defined(EXITFREE) || defined(PROTO)
2964 void
2965channel_free_all(void)
2966{
2967 channel_T *channel;
2968
Bram Moolenaard6051b52016-02-28 15:49:03 +01002969 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002970 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2971 channel_clear(channel);
2972}
2973#endif
2974
2975
Bram Moolenaar715d2852016-04-30 17:06:31 +02002976/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002977#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002978
2979/* Buffer size for reading incoming messages. */
2980#define MAXMSGSIZE 4096
2981
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002982#if defined(HAVE_SELECT)
2983/*
2984 * Add write fds where we are waiting for writing to be possible.
2985 */
2986 static int
2987channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2988{
2989 int maxfd = maxfd_arg;
2990 channel_T *ch;
2991
2992 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2993 {
2994 chanpart_T *in_part = &ch->ch_part[PART_IN];
2995
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02002996 if (in_part->ch_fd != INVALID_FD
2997 && (in_part->ch_bufref.br_buf != NULL
2998 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002999 {
3000 FD_SET((int)in_part->ch_fd, wfds);
3001 if ((int)in_part->ch_fd >= maxfd)
3002 maxfd = (int)in_part->ch_fd + 1;
3003 }
3004 }
3005 return maxfd;
3006}
3007#else
3008/*
3009 * Add write fds where we are waiting for writing to be possible.
3010 */
3011 static int
3012channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3013{
3014 int nfd = nfd_in;
3015 channel_T *ch;
3016
3017 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3018 {
3019 chanpart_T *in_part = &ch->ch_part[PART_IN];
3020
Bram Moolenaar683b7962017-08-19 15:51:59 +02003021 if (in_part->ch_fd != INVALID_FD
3022 && (in_part->ch_bufref.br_buf != NULL
3023 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003024 {
3025 in_part->ch_poll_idx = nfd;
3026 fds[nfd].fd = in_part->ch_fd;
3027 fds[nfd].events = POLLOUT;
3028 ++nfd;
3029 }
3030 else
3031 in_part->ch_poll_idx = -1;
3032 }
3033 return nfd;
3034}
3035#endif
3036
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003037typedef enum {
3038 CW_READY,
3039 CW_NOT_READY,
3040 CW_ERROR
3041} channel_wait_result;
3042
Bram Moolenaard04a0202016-01-26 23:30:18 +01003043/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003044 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003045 * Return CW_READY when there is something to read.
3046 * Return CW_NOT_READY when there is nothing to read.
3047 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003048 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003049 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003050channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003051{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003052 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003053 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003054
Bram Moolenaard8070362016-02-15 21:56:54 +01003055# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003056 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003057 {
3058 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003059 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003060 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003061 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003062
3063 /* reading from a pipe, not a socket */
3064 while (TRUE)
3065 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003066 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3067
3068 if (r && nread > 0)
3069 return CW_READY;
3070 if (r == 0)
3071 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003072
3073 /* perhaps write some buffer lines */
3074 channel_write_any_lines();
3075
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003076 sleep_time = deadline - GetTickCount();
3077 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003078 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003079 /* Wait for a little while. Very short at first, up to 10 msec
3080 * after looping a few times. */
3081 if (sleep_time > delay)
3082 sleep_time = delay;
3083 Sleep(sleep_time);
3084 delay = delay * 2;
3085 if (delay > 10)
3086 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003087 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003088 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003089 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003090#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003091 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003092#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003093 struct timeval tval;
3094 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003095 fd_set wfds;
3096 int ret;
3097 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003098
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003099 tval.tv_sec = timeout / 1000;
3100 tval.tv_usec = (timeout % 1000) * 1000;
3101 for (;;)
3102 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003103 FD_ZERO(&rfds);
3104 FD_SET((int)fd, &rfds);
3105
3106 /* Write lines to a pipe when a pipe can be written to. Need to
3107 * set this every time, some buffers may be done. */
3108 maxfd = (int)fd + 1;
3109 FD_ZERO(&wfds);
3110 maxfd = channel_fill_wfds(maxfd, &wfds);
3111
3112 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003113# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003114 SOCK_ERRNO;
3115 if (ret == -1 && errno == EINTR)
3116 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003117# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003118 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003119 {
3120 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003121 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003122 channel_write_any_lines();
3123 continue;
3124 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003125 break;
3126 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003127#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003128 for (;;)
3129 {
3130 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3131 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003132
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003133 fds[0].fd = fd;
3134 fds[0].events = POLLIN;
3135 nfd = channel_fill_poll_write(nfd, fds);
3136 if (poll(fds, nfd, timeout) > 0)
3137 {
3138 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003139 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003140 channel_write_any_lines();
3141 continue;
3142 }
3143 break;
3144 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003145#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003146 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003147 return CW_NOT_READY;
3148}
3149
3150 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003151ch_close_part_on_error(
3152 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003153{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003154 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003155
3156 if (is_err)
3157 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003158 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003159 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003160 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003161
3162 /* Queue a "DETACH" netbeans message in the command queue in order to
3163 * terminate the netbeans session later. Do not end the session here
3164 * directly as we may be running in the context of a call to
3165 * netbeans_parse_messages():
3166 * netbeans_parse_messages
3167 * -> autocmd triggered while processing the netbeans cmd
3168 * -> ui_breakcheck
3169 * -> gui event loop or select loop
3170 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003171 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003172 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003173 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003174 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003175 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3176
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003177 /* When reading is not possible close this part of the channel. Don't
3178 * close the channel yet, there may be something to read on another part. */
3179 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003180
3181#ifdef FEAT_GUI
3182 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003183 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003184#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003185}
3186
3187 static void
3188channel_close_now(channel_T *channel)
3189{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003190 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003191 if (channel->ch_nb_close_cb != NULL)
3192 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003193 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003194}
3195
3196/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003197 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003198 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003199 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003200 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003201 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003202channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003203{
3204 static char_u *buf = NULL;
3205 int len = 0;
3206 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003207 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003208 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003209
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003210 fd = channel->ch_part[part].ch_fd;
3211 if (fd == INVALID_FD)
3212 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003213 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003214 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003215 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003216 }
3217 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003218
3219 /* Allocate a buffer to read into. */
3220 if (buf == NULL)
3221 {
3222 buf = alloc(MAXMSGSIZE);
3223 if (buf == NULL)
3224 return; /* out of memory! */
3225 }
3226
3227 /* Keep on reading for as long as there is something to read.
3228 * Use select() or poll() to avoid blocking on a message that is exactly
3229 * MAXMSGSIZE long. */
3230 for (;;)
3231 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003232 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003233 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003234 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003235 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003236 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003237 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003238 if (len <= 0)
3239 break; /* error or nothing more to read */
3240
3241 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003242 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003243 readlen += len;
3244 if (len < MAXMSGSIZE)
3245 break; /* did read everything that's available */
3246 }
3247
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003248 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003249 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003250 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003251
3252#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003253 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003254 if (CH_HAS_GUI && gtk_main_level() > 0)
3255 gtk_main_quit();
3256#endif
3257}
3258
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003259/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003260 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003261 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003262 * Returns what was read in allocated memory.
3263 * Returns NULL in case of error or timeout.
3264 */
3265 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003266channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003267{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003268 char_u *buf;
3269 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003270 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003271 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003272 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003273 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003274
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003275 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003276 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003277
3278 while (TRUE)
3279 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003280 node = channel_peek(channel, part);
3281 if (node != NULL)
3282 {
3283 if (mode == MODE_RAW || (mode == MODE_NL
3284 && channel_first_nl(node) != NULL))
3285 /* got a complete message */
3286 break;
3287 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3288 continue;
3289 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003290
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003291 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003292 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003293 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003294 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003295 {
3296 ch_log(channel, "Timed out");
3297 return NULL;
3298 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003299 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003300 }
3301
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003302 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003303 if (mode == MODE_RAW)
3304 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003305 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003306 }
3307 else
3308 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003309 char_u *p;
3310
3311 buf = node->rq_buffer;
3312 nl = channel_first_nl(node);
3313
3314 /* Convert NUL to NL, the internal representation. */
3315 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3316 if (*p == NUL)
3317 *p = NL;
3318
3319 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003320 {
3321 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003322 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003323 *nl = NUL;
3324 }
3325 else
3326 {
3327 /* Copy the message into allocated memory and remove it from the
3328 * buffer. */
3329 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003330 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003331 }
3332 }
3333 if (log_fd != NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003334 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003335 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003336}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003337
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003338/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003339 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003340 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003341 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003342 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003343 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003344 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003345channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003346 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003347 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003348 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003349 int id,
3350 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003351{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003352 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003353 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003354 int timeout;
3355 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003356
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003357 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003358 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003359 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003360 for (;;)
3361 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003362 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003363
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003364 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003365 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003366 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003367 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003368 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003369 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003370
Bram Moolenaard7ece102016-02-02 23:23:02 +01003371 if (!more)
3372 {
3373 /* Handle any other messages in the queue. If done some more
3374 * messages may have arrived. */
3375 if (channel_parse_messages())
3376 continue;
3377
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003378 /* Wait for up to the timeout. If there was an incomplete message
3379 * use the deadline for that. */
3380 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003381 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003382 {
3383#ifdef WIN32
3384 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3385#else
3386 {
3387 struct timeval now_tv;
3388
3389 gettimeofday(&now_tv, NULL);
3390 timeout = (chanpart->ch_deadline.tv_sec
3391 - now_tv.tv_sec) * 1000
3392 + (chanpart->ch_deadline.tv_usec
3393 - now_tv.tv_usec) / 1000
3394 + 1;
3395 }
3396#endif
3397 if (timeout < 0)
3398 {
3399 /* Something went wrong, channel_parse_json() didn't
3400 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003401 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003402 timeout = timeout_arg;
3403 }
3404 else if (timeout > timeout_arg)
3405 timeout = timeout_arg;
3406 }
3407 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003408 if (fd == INVALID_FD
3409 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003410 {
3411 if (timeout == timeout_arg)
3412 {
3413 if (fd != INVALID_FD)
3414 ch_log(channel, "Timed out");
3415 break;
3416 }
3417 }
3418 else
3419 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003420 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003421 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003422 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003423 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003424}
3425
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003426/*
3427 * Common for ch_read() and ch_readraw().
3428 */
3429 void
3430common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3431{
3432 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003433 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003434 jobopt_T opt;
3435 int mode;
3436 int timeout;
3437 int id = -1;
3438 typval_T *listtv = NULL;
3439
3440 /* return an empty string by default */
3441 rettv->v_type = VAR_STRING;
3442 rettv->vval.v_string = NULL;
3443
3444 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003445 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003446 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003447 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003448
Bram Moolenaar437905c2016-04-26 19:01:05 +02003449 if (opt.jo_set & JO_PART)
3450 part = opt.jo_part;
3451 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003452 if (channel != NULL)
3453 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003454 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003455 part = channel_part_read(channel);
3456 mode = channel_get_mode(channel, part);
3457 timeout = channel_get_timeout(channel, part);
3458 if (opt.jo_set & JO_TIMEOUT)
3459 timeout = opt.jo_timeout;
3460
3461 if (raw || mode == MODE_RAW || mode == MODE_NL)
3462 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3463 else
3464 {
3465 if (opt.jo_set & JO_ID)
3466 id = opt.jo_id;
3467 channel_read_json_block(channel, part, timeout, id, &listtv);
3468 if (listtv != NULL)
3469 {
3470 *rettv = *listtv;
3471 vim_free(listtv);
3472 }
3473 else
3474 {
3475 rettv->v_type = VAR_SPECIAL;
3476 rettv->vval.v_number = VVAL_NONE;
3477 }
3478 }
3479 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003480
3481theend:
3482 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003483}
3484
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003485# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3486 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003487/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003488 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003489 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003490 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003491 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003492channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003493{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003494 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003495 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003496
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003497 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003498 for (channel = first_channel; channel != NULL;
3499 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003500 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003501 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003502 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003503 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003504 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003505 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003506 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003507 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003508 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003509}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003510# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003511
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003512# if defined(WIN32) || defined(PROTO)
3513/*
3514 * Check the channels for anything that is ready to be read.
3515 * The data is put in the read queue.
3516 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003517 void
3518channel_handle_events(void)
3519{
3520 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003521 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003522 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003523
3524 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3525 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003526 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003527 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003528 {
3529 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003530 if (fd != INVALID_FD)
3531 {
3532 int r = channel_wait(channel, fd, 0);
3533
3534 if (r == CW_READY)
3535 channel_read(channel, part, "channel_handle_events");
3536 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003537 ch_close_part_on_error(channel, part, TRUE,
3538 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003539 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003540 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003541 }
3542}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003543# endif
3544
Bram Moolenaard04a0202016-01-26 23:30:18 +01003545/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003546 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003547 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003548 */
3549 void
3550channel_set_nonblock(channel_T *channel, ch_part_T part)
3551{
3552 chanpart_T *ch_part = &channel->ch_part[part];
3553 int fd = ch_part->ch_fd;
3554
3555 if (fd != INVALID_FD)
3556 {
3557#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003558 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003559
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003560 ioctlsocket(fd, FIONBIO, &val);
3561#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003562 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003563#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003564 ch_part->ch_nonblocking = TRUE;
3565 }
3566}
3567
3568/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003569 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003570 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003571 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003572 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003573 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003574channel_send(
3575 channel_T *channel,
3576 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003577 char_u *buf_arg,
3578 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003579 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003580{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003581 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003582 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003583 chanpart_T *ch_part = &channel->ch_part[part];
3584 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003585
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003586 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003587 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003588 {
3589 if (!channel->ch_error && fun != NULL)
3590 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003591 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003592 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003593 }
3594 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003595 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003596 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003597
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003598 if (log_fd != NULL)
3599 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003600 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003601 fprintf(log_fd, "'");
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003602 ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003603 fprintf(log_fd, "'\n");
3604 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003605 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003606 }
3607
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003608 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003609 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003610 writeq_T *wq = &ch_part->ch_writeque;
3611 char_u *buf;
3612 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003613
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003614 if (wq->wq_next != NULL)
3615 {
3616 /* first write what was queued */
3617 buf = wq->wq_next->wq_ga.ga_data;
3618 len = wq->wq_next->wq_ga.ga_len;
3619 did_use_queue = TRUE;
3620 }
3621 else
3622 {
3623 if (len_arg == 0)
3624 /* nothing to write, called from channel_select_check() */
3625 return OK;
3626 buf = buf_arg;
3627 len = len_arg;
3628 }
3629
3630 if (part == PART_SOCK)
3631 res = sock_write(fd, (char *)buf, len);
3632 else
3633 res = fd_write(fd, (char *)buf, len);
3634 if (res < 0 && (errno == EWOULDBLOCK
3635#ifdef EAGAIN
3636 || errno == EAGAIN
3637#endif
3638 ))
3639 res = 0; /* nothing got written */
3640
3641 if (res >= 0 && ch_part->ch_nonblocking)
3642 {
3643 writeq_T *entry = wq->wq_next;
3644
3645 if (did_use_queue)
3646 ch_log(channel, "Sent %d bytes now", res);
3647 if (res == len)
3648 {
3649 /* Wrote all the buf[len] bytes. */
3650 if (entry != NULL)
3651 {
3652 /* Remove the entry from the write queue. */
3653 ga_clear(&entry->wq_ga);
3654 wq->wq_next = entry->wq_next;
3655 if (wq->wq_next == NULL)
3656 wq->wq_prev = NULL;
3657 else
3658 wq->wq_next->wq_prev = NULL;
3659 continue;
3660 }
3661 if (did_use_queue)
3662 ch_log(channel, "Write queue empty");
3663 }
3664 else
3665 {
3666 /* Wrote only buf[res] bytes, can't write more now. */
3667 if (entry != NULL)
3668 {
3669 if (res > 0)
3670 {
3671 /* Remove the bytes that were written. */
3672 mch_memmove(entry->wq_ga.ga_data,
3673 (char *)entry->wq_ga.ga_data + res,
3674 len - res);
3675 entry->wq_ga.ga_len -= res;
3676 }
3677 buf = buf_arg;
3678 len = len_arg;
3679 }
3680 else
3681 {
3682 buf += res;
3683 len -= res;
3684 }
3685 ch_log(channel, "Adding %d bytes to the write queue", len);
3686
3687 /* Append the not written bytes of the argument to the write
3688 * buffer. Limit entries to 4000 bytes. */
3689 if (wq->wq_prev != NULL
3690 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3691 {
3692 writeq_T *last = wq->wq_prev;
3693
3694 /* append to the last entry */
3695 if (ga_grow(&last->wq_ga, len) == OK)
3696 {
3697 mch_memmove((char *)last->wq_ga.ga_data
3698 + last->wq_ga.ga_len,
3699 buf, len);
3700 last->wq_ga.ga_len += len;
3701 }
3702 }
3703 else
3704 {
3705 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3706
3707 if (last != NULL)
3708 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003709 last->wq_prev = wq->wq_prev;
3710 last->wq_next = NULL;
3711 if (wq->wq_prev == NULL)
3712 wq->wq_next = last;
3713 else
3714 wq->wq_prev->wq_next = last;
3715 wq->wq_prev = last;
3716 ga_init2(&last->wq_ga, 1, 1000);
3717 if (ga_grow(&last->wq_ga, len) == OK)
3718 {
3719 mch_memmove(last->wq_ga.ga_data, buf, len);
3720 last->wq_ga.ga_len = len;
3721 }
3722 }
3723 }
3724 }
3725 }
3726 else if (res != len)
3727 {
3728 if (!channel->ch_error && fun != NULL)
3729 {
3730 ch_error(channel, "%s(): write failed", fun);
3731 EMSG2(_("E631: %s(): write failed"), fun);
3732 }
3733 channel->ch_error = TRUE;
3734 return FAIL;
3735 }
3736
3737 channel->ch_error = FALSE;
3738 return OK;
3739 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003740}
3741
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003742/*
3743 * Common for "ch_sendexpr()" and "ch_sendraw()".
3744 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003745 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003746 * Otherwise returns NULL.
3747 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003748 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003749send_common(
3750 typval_T *argvars,
3751 char_u *text,
3752 int id,
3753 int eval,
3754 jobopt_T *opt,
3755 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003756 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003757{
3758 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003759 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003760
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003761 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003762 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003763 if (channel == NULL)
3764 return NULL;
3765 part_send = channel_part_send(channel);
3766 *part_read = channel_part_read(channel);
3767
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003768 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003769 return NULL;
3770
3771 /* Set the callback. An empty callback means no callback and not reading
3772 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3773 * allowed. */
3774 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3775 {
3776 if (eval)
3777 {
3778 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3779 return NULL;
3780 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003781 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003782 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003783 }
3784
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003785 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003786 && opt->jo_callback == NULL)
3787 return channel;
3788 return NULL;
3789}
3790
3791/*
3792 * common for "ch_evalexpr()" and "ch_sendexpr()"
3793 */
3794 void
3795ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3796{
3797 char_u *text;
3798 typval_T *listtv;
3799 channel_T *channel;
3800 int id;
3801 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003802 ch_part_T part_send;
3803 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003804 jobopt_T opt;
3805 int timeout;
3806
3807 /* return an empty string by default */
3808 rettv->v_type = VAR_STRING;
3809 rettv->vval.v_string = NULL;
3810
Bram Moolenaar437905c2016-04-26 19:01:05 +02003811 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003812 if (channel == NULL)
3813 return;
3814 part_send = channel_part_send(channel);
3815
3816 ch_mode = channel_get_mode(channel, part_send);
3817 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3818 {
3819 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3820 return;
3821 }
3822
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003823 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003824 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003825 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003826 if (text == NULL)
3827 return;
3828
3829 channel = send_common(argvars, text, id, eval, &opt,
3830 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3831 vim_free(text);
3832 if (channel != NULL && eval)
3833 {
3834 if (opt.jo_set & JO_TIMEOUT)
3835 timeout = opt.jo_timeout;
3836 else
3837 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003838 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3839 == OK)
3840 {
3841 list_T *list = listtv->vval.v_list;
3842
3843 /* Move the item from the list and then change the type to
3844 * avoid the value being freed. */
3845 *rettv = list->lv_last->li_tv;
3846 list->lv_last->li_tv.v_type = VAR_NUMBER;
3847 free_tv(listtv);
3848 }
3849 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003850 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003851}
3852
3853/*
3854 * common for "ch_evalraw()" and "ch_sendraw()"
3855 */
3856 void
3857ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3858{
3859 char_u buf[NUMBUFLEN];
3860 char_u *text;
3861 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003862 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003863 jobopt_T opt;
3864 int timeout;
3865
3866 /* return an empty string by default */
3867 rettv->v_type = VAR_STRING;
3868 rettv->vval.v_string = NULL;
3869
3870 text = get_tv_string_buf(&argvars[1], buf);
3871 channel = send_common(argvars, text, 0, eval, &opt,
3872 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3873 if (channel != NULL && eval)
3874 {
3875 if (opt.jo_set & JO_TIMEOUT)
3876 timeout = opt.jo_timeout;
3877 else
3878 timeout = channel_get_timeout(channel, part_read);
3879 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3880 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003881 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003882}
3883
Bram Moolenaard04a0202016-01-26 23:30:18 +01003884# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003885/*
3886 * Add open channels to the poll struct.
3887 * Return the adjusted struct index.
3888 * The type of "fds" is hidden to avoid problems with the function proto.
3889 */
3890 int
3891channel_poll_setup(int nfd_in, void *fds_in)
3892{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003893 int nfd = nfd_in;
3894 channel_T *channel;
3895 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003896 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003897
Bram Moolenaar77073442016-02-13 23:23:53 +01003898 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003899 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003900 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003901 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003902 chanpart_T *ch_part = &channel->ch_part[part];
3903
3904 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003905 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003906 ch_part->ch_poll_idx = nfd;
3907 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003908 fds[nfd].events = POLLIN;
3909 nfd++;
3910 }
3911 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003912 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003913 }
3914 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003915
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003916 nfd = channel_fill_poll_write(nfd, fds);
3917
Bram Moolenaare0874f82016-01-24 20:36:41 +01003918 return nfd;
3919}
3920
3921/*
3922 * The type of "fds" is hidden to avoid problems with the function proto.
3923 */
3924 int
3925channel_poll_check(int ret_in, void *fds_in)
3926{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003927 int ret = ret_in;
3928 channel_T *channel;
3929 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003930 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003931 int idx;
3932 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003933
Bram Moolenaar77073442016-02-13 23:23:53 +01003934 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003935 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003936 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003937 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003938 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003939
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003940 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003941 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003942 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003943 --ret;
3944 }
3945 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003946
3947 in_part = &channel->ch_part[PART_IN];
3948 idx = in_part->ch_poll_idx;
3949 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3950 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02003951 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003952 --ret;
3953 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003954 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003955
3956 return ret;
3957}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003958# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003959
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003960# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003961/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003962 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003963 */
3964 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003965channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003966{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003967 int maxfd = maxfd_in;
3968 channel_T *channel;
3969 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003970 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003971 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003972
Bram Moolenaar77073442016-02-13 23:23:53 +01003973 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003974 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003975 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003976 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003977 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003978
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003979 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003980 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003981 FD_SET((int)fd, rfds);
3982 if (maxfd < (int)fd)
3983 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003984 }
3985 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003986 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003987
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003988 maxfd = channel_fill_wfds(maxfd, wfds);
3989
Bram Moolenaare0874f82016-01-24 20:36:41 +01003990 return maxfd;
3991}
3992
3993/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003994 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003995 */
3996 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003997channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003998{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003999 int ret = ret_in;
4000 channel_T *channel;
4001 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004002 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004003 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004004 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004005
Bram Moolenaar77073442016-02-13 23:23:53 +01004006 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004007 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004008 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004009 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004010 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004011
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004012 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004013 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004014 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004015 --ret;
4016 }
4017 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004018
4019 in_part = &channel->ch_part[PART_IN];
4020 if (ret > 0 && in_part->ch_fd != INVALID_FD
4021 && FD_ISSET(in_part->ch_fd, wfds))
4022 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004023 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004024 --ret;
4025 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004026 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004027
4028 return ret;
4029}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004030# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004031
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004032/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004033 * Execute queued up commands.
4034 * Invoked from the main loop when it's safe to execute received commands.
4035 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004036 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004037 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004038channel_parse_messages(void)
4039{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004040 channel_T *channel = first_channel;
4041 int ret = FALSE;
4042 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004043 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004044#ifdef ELAPSED_FUNC
4045 ELAPSED_TYPE start_tv;
4046
4047 ELAPSED_INIT(start_tv);
4048#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004049
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004050 ++safe_to_invoke_callback;
4051
Bram Moolenaard0b65022016-03-06 21:50:33 +01004052 /* Only do this message when another message was given, otherwise we get
4053 * lots of them. */
4054 if (did_log_msg)
4055 {
4056 ch_log(NULL, "looking for messages on channels");
4057 did_log_msg = FALSE;
4058 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004059 while (channel != NULL)
4060 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004061 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004062 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004063 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004064 channel_close_now(channel);
4065 /* channel may have been freed, start over */
4066 channel = first_channel;
4067 continue;
4068 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004069 if (channel->ch_to_be_freed)
4070 {
4071 channel_free(channel);
4072 /* channel has been freed, start over */
4073 channel = first_channel;
4074 continue;
4075 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004076 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004077 {
4078 /* channel is no longer useful, free it */
4079 channel_free(channel);
4080 channel = first_channel;
4081 part = PART_SOCK;
4082 continue;
4083 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004084 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004085 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004086 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004087 /* Increase the refcount, in case the handler causes the channel
4088 * to be unreferenced or closed. */
4089 ++channel->ch_refcount;
4090 r = may_invoke_callback(channel, part);
4091 if (r == OK)
4092 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004093 if (channel_unref(channel) || (r == OK
4094#ifdef ELAPSED_FUNC
4095 /* Limit the time we loop here to 100 msec, otherwise
4096 * Vim becomes unresponsive when the callback takes
4097 * more than a bit of time. */
4098 && ELAPSED_FUNC(start_tv) < 100L
4099#endif
4100 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004101 {
4102 /* channel was freed or something was done, start over */
4103 channel = first_channel;
4104 part = PART_SOCK;
4105 continue;
4106 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004107 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004108 if (part < PART_ERR)
4109 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004110 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004111 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004112 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004113 part = PART_SOCK;
4114 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004115 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004116
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004117 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004118 {
4119 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004120 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01004121 }
4122
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004123 --safe_to_invoke_callback;
4124
Bram Moolenaard7ece102016-02-02 23:23:02 +01004125 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004126}
4127
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004128/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004129 * Return TRUE if any channel has readahead. That means we should not block on
4130 * waiting for input.
4131 */
4132 int
4133channel_any_readahead(void)
4134{
4135 channel_T *channel = first_channel;
4136 ch_part_T part = PART_SOCK;
4137
4138 while (channel != NULL)
4139 {
4140 if (channel_has_readahead(channel, part))
4141 return TRUE;
4142 if (part < PART_ERR)
4143 ++part;
4144 else
4145 {
4146 channel = channel->ch_next;
4147 part = PART_SOCK;
4148 }
4149 }
4150 return FALSE;
4151}
4152
4153/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004154 * Mark references to lists used in channels.
4155 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004156 int
4157set_ref_in_channel(int copyID)
4158{
Bram Moolenaar77073442016-02-13 23:23:53 +01004159 int abort = FALSE;
4160 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004161 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004162
Bram Moolenaar77073442016-02-13 23:23:53 +01004163 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004164 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004165 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004166 tv.v_type = VAR_CHANNEL;
4167 tv.vval.v_channel = channel;
4168 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004169 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004170 return abort;
4171}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004172
4173/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004174 * Return the "part" to write to for "channel".
4175 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004176 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004177channel_part_send(channel_T *channel)
4178{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004179 if (channel->CH_SOCK_FD == INVALID_FD)
4180 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004181 return PART_SOCK;
4182}
4183
4184/*
4185 * Return the default "part" to read from for "channel".
4186 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004187 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004188channel_part_read(channel_T *channel)
4189{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004190 if (channel->CH_SOCK_FD == INVALID_FD)
4191 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004192 return PART_SOCK;
4193}
4194
4195/*
4196 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004197 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004198 */
4199 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004200channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004201{
Bram Moolenaar77073442016-02-13 23:23:53 +01004202 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004203 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004204 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004205}
4206
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004207/*
4208 * Return the timeout of "channel"/"part"
4209 */
4210 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004211channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004212{
4213 return channel->ch_part[part].ch_timeout;
4214}
4215
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004216 static int
4217handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4218{
4219 char_u *val = get_tv_string(item);
4220
4221 opt->jo_set |= jo;
4222 if (STRCMP(val, "nl") == 0)
4223 *modep = MODE_NL;
4224 else if (STRCMP(val, "raw") == 0)
4225 *modep = MODE_RAW;
4226 else if (STRCMP(val, "js") == 0)
4227 *modep = MODE_JS;
4228 else if (STRCMP(val, "json") == 0)
4229 *modep = MODE_JSON;
4230 else
4231 {
4232 EMSG2(_(e_invarg2), val);
4233 return FAIL;
4234 }
4235 return OK;
4236}
4237
4238 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004239handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004240{
4241 char_u *val = get_tv_string(item);
4242
4243 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4244 if (STRCMP(val, "null") == 0)
4245 opt->jo_io[part] = JIO_NULL;
4246 else if (STRCMP(val, "pipe") == 0)
4247 opt->jo_io[part] = JIO_PIPE;
4248 else if (STRCMP(val, "file") == 0)
4249 opt->jo_io[part] = JIO_FILE;
4250 else if (STRCMP(val, "buffer") == 0)
4251 opt->jo_io[part] = JIO_BUFFER;
4252 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4253 opt->jo_io[part] = JIO_OUT;
4254 else
4255 {
4256 EMSG2(_(e_invarg2), val);
4257 return FAIL;
4258 }
4259 return OK;
4260}
4261
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004262/*
4263 * Clear a jobopt_T before using it.
4264 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004265 void
4266clear_job_options(jobopt_T *opt)
4267{
4268 vim_memset(opt, 0, sizeof(jobopt_T));
4269}
4270
4271/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004272 * Free any members of a jobopt_T.
4273 */
4274 void
4275free_job_options(jobopt_T *opt)
4276{
4277 if (opt->jo_partial != NULL)
4278 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 else if (opt->jo_callback != NULL)
4280 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004281 if (opt->jo_out_partial != NULL)
4282 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004283 else if (opt->jo_out_cb != NULL)
4284 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004285 if (opt->jo_err_partial != NULL)
4286 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004287 else if (opt->jo_err_cb != NULL)
4288 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004289 if (opt->jo_close_partial != NULL)
4290 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004291 else if (opt->jo_close_cb != NULL)
4292 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004293 if (opt->jo_exit_partial != NULL)
4294 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004295 else if (opt->jo_exit_cb != NULL)
4296 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004297 if (opt->jo_env != NULL)
4298 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004299}
4300
4301/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004302 * Get the PART_ number from the first character of an option name.
4303 */
4304 static int
4305part_from_char(int c)
4306{
4307 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4308}
4309
4310/*
4311 * Get the option entries from the dict in "tv", parse them and put the result
4312 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004313 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004314 * If an option value is invalid return FAIL.
4315 */
4316 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004317get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004318{
4319 typval_T *item;
4320 char_u *val;
4321 dict_T *dict;
4322 int todo;
4323 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004324 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004325
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004326 if (tv->v_type == VAR_UNKNOWN)
4327 return OK;
4328 if (tv->v_type != VAR_DICT)
4329 {
4330 EMSG(_(e_invarg));
4331 return FAIL;
4332 }
4333 dict = tv->vval.v_dict;
4334 if (dict == NULL)
4335 return OK;
4336
4337 todo = (int)dict->dv_hashtab.ht_used;
4338 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4339 if (!HASHITEM_EMPTY(hi))
4340 {
4341 item = &dict_lookup(hi)->di_tv;
4342
4343 if (STRCMP(hi->hi_key, "mode") == 0)
4344 {
4345 if (!(supported & JO_MODE))
4346 break;
4347 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4348 return FAIL;
4349 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004350 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004351 {
4352 if (!(supported & JO_IN_MODE))
4353 break;
4354 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4355 == FAIL)
4356 return FAIL;
4357 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004358 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004359 {
4360 if (!(supported & JO_OUT_MODE))
4361 break;
4362 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4363 == FAIL)
4364 return FAIL;
4365 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004366 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004367 {
4368 if (!(supported & JO_ERR_MODE))
4369 break;
4370 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4371 == FAIL)
4372 return FAIL;
4373 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004374 else if (STRCMP(hi->hi_key, "in_io") == 0
4375 || STRCMP(hi->hi_key, "out_io") == 0
4376 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004377 {
4378 if (!(supported & JO_OUT_IO))
4379 break;
4380 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4381 return FAIL;
4382 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004383 else if (STRCMP(hi->hi_key, "in_name") == 0
4384 || STRCMP(hi->hi_key, "out_name") == 0
4385 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004386 {
4387 part = part_from_char(*hi->hi_key);
4388
4389 if (!(supported & JO_OUT_IO))
4390 break;
4391 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4392 opt->jo_io_name[part] =
4393 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4394 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004395 else if (STRCMP(hi->hi_key, "pty") == 0)
4396 {
4397 if (!(supported & JO_MODE))
4398 break;
4399 opt->jo_pty = get_tv_number(item);
4400 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004401 else if (STRCMP(hi->hi_key, "in_buf") == 0
4402 || STRCMP(hi->hi_key, "out_buf") == 0
4403 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004404 {
4405 part = part_from_char(*hi->hi_key);
4406
4407 if (!(supported & JO_OUT_IO))
4408 break;
4409 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4410 opt->jo_io_buf[part] = get_tv_number(item);
4411 if (opt->jo_io_buf[part] <= 0)
4412 {
4413 EMSG2(_(e_invarg2), get_tv_string(item));
4414 return FAIL;
4415 }
4416 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4417 {
4418 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4419 return FAIL;
4420 }
4421 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004422 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4423 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4424 {
4425 part = part_from_char(*hi->hi_key);
4426
4427 if (!(supported & JO_OUT_IO))
4428 break;
4429 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4430 opt->jo_modifiable[part] = get_tv_number(item);
4431 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004432 else if (STRCMP(hi->hi_key, "out_msg") == 0
4433 || STRCMP(hi->hi_key, "err_msg") == 0)
4434 {
4435 part = part_from_char(*hi->hi_key);
4436
4437 if (!(supported & JO_OUT_IO))
4438 break;
4439 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4440 opt->jo_message[part] = get_tv_number(item);
4441 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004442 else if (STRCMP(hi->hi_key, "in_top") == 0
4443 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004444 {
4445 linenr_T *lp;
4446
4447 if (!(supported & JO_OUT_IO))
4448 break;
4449 if (hi->hi_key[3] == 't')
4450 {
4451 lp = &opt->jo_in_top;
4452 opt->jo_set |= JO_IN_TOP;
4453 }
4454 else
4455 {
4456 lp = &opt->jo_in_bot;
4457 opt->jo_set |= JO_IN_BOT;
4458 }
4459 *lp = get_tv_number(item);
4460 if (*lp < 0)
4461 {
4462 EMSG2(_(e_invarg2), get_tv_string(item));
4463 return FAIL;
4464 }
4465 }
4466 else if (STRCMP(hi->hi_key, "channel") == 0)
4467 {
4468 if (!(supported & JO_OUT_IO))
4469 break;
4470 opt->jo_set |= JO_CHANNEL;
4471 if (item->v_type != VAR_CHANNEL)
4472 {
4473 EMSG2(_(e_invarg2), "channel");
4474 return FAIL;
4475 }
4476 opt->jo_channel = item->vval.v_channel;
4477 }
4478 else if (STRCMP(hi->hi_key, "callback") == 0)
4479 {
4480 if (!(supported & JO_CALLBACK))
4481 break;
4482 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004483 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004484 if (opt->jo_callback == NULL)
4485 {
4486 EMSG2(_(e_invarg2), "callback");
4487 return FAIL;
4488 }
4489 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004490 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491 {
4492 if (!(supported & JO_OUT_CALLBACK))
4493 break;
4494 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004495 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004496 if (opt->jo_out_cb == NULL)
4497 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004498 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004499 return FAIL;
4500 }
4501 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004502 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004503 {
4504 if (!(supported & JO_ERR_CALLBACK))
4505 break;
4506 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004507 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004508 if (opt->jo_err_cb == NULL)
4509 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004510 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004511 return FAIL;
4512 }
4513 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004514 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004515 {
4516 if (!(supported & JO_CLOSE_CALLBACK))
4517 break;
4518 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004519 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004520 if (opt->jo_close_cb == NULL)
4521 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004522 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004523 return FAIL;
4524 }
4525 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004526 else if (STRCMP(hi->hi_key, "drop") == 0)
4527 {
4528 int never = FALSE;
4529 val = get_tv_string(item);
4530
4531 if (STRCMP(val, "never") == 0)
4532 never = TRUE;
4533 else if (STRCMP(val, "auto") != 0)
4534 {
4535 EMSG2(_(e_invarg2), "drop");
4536 return FAIL;
4537 }
4538 opt->jo_drop_never = never;
4539 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004540 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4541 {
4542 if (!(supported & JO_EXIT_CB))
4543 break;
4544 opt->jo_set |= JO_EXIT_CB;
4545 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4546 if (opt->jo_exit_cb == NULL)
4547 {
4548 EMSG2(_(e_invarg2), "exit_cb");
4549 return FAIL;
4550 }
4551 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004552#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004553 else if (STRCMP(hi->hi_key, "term_name") == 0)
4554 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004555 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004556 break;
4557 opt->jo_set2 |= JO2_TERM_NAME;
4558 opt->jo_term_name = get_tv_string_chk(item);
4559 if (opt->jo_term_name == NULL)
4560 {
4561 EMSG2(_(e_invarg2), "term_name");
4562 return FAIL;
4563 }
4564 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004565 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4566 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004567 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004568 break;
4569 val = get_tv_string(item);
4570 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4571 {
Bram Moolenaarf1237f12017-08-11 15:45:28 +02004572 EMSG2(_(e_invarg2), val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004573 return FAIL;
4574 }
4575 opt->jo_set2 |= JO2_TERM_FINISH;
4576 opt->jo_term_finish = *val;
4577 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004578 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4579 {
4580 char_u *p;
4581
4582 if (!(supported2 & JO2_TERM_OPENCMD))
4583 break;
4584 opt->jo_set2 |= JO2_TERM_OPENCMD;
4585 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4586 if (p != NULL)
4587 {
4588 /* Must have %d and no other %. */
4589 p = vim_strchr(p, '%');
4590 if (p != NULL && (p[1] != 'd'
4591 || vim_strchr(p + 2, '%') != NULL))
4592 p = NULL;
4593 }
4594 if (p == NULL)
4595 {
4596 EMSG2(_(e_invarg2), "term_opencmd");
4597 return FAIL;
4598 }
4599 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004600 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4601 {
4602 if (!(supported2 & JO2_TERM_ROWS))
4603 break;
4604 opt->jo_set |= JO2_TERM_ROWS;
4605 opt->jo_term_rows = get_tv_number(item);
4606 }
4607 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4608 {
4609 if (!(supported2 & JO2_TERM_COLS))
4610 break;
4611 opt->jo_set |= JO2_TERM_COLS;
4612 opt->jo_term_cols = get_tv_number(item);
4613 }
4614 else if (STRCMP(hi->hi_key, "vertical") == 0)
4615 {
4616 if (!(supported2 & JO2_VERTICAL))
4617 break;
4618 opt->jo_set |= JO2_VERTICAL;
4619 opt->jo_vertical = get_tv_number(item);
4620 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004621 else if (STRCMP(hi->hi_key, "curwin") == 0)
4622 {
4623 if (!(supported2 & JO2_CURWIN))
4624 break;
4625 opt->jo_set |= JO2_CURWIN;
4626 opt->jo_curwin = get_tv_number(item);
4627 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004628 else if (STRCMP(hi->hi_key, "hidden") == 0)
4629 {
4630 if (!(supported2 & JO2_HIDDEN))
4631 break;
4632 opt->jo_set |= JO2_HIDDEN;
4633 opt->jo_hidden = get_tv_number(item);
4634 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004635#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004636 else if (STRCMP(hi->hi_key, "env") == 0)
4637 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004638 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004639 break;
4640 opt->jo_set |= JO2_ENV;
4641 opt->jo_env = item->vval.v_dict;
4642 ++item->vval.v_dict->dv_refcount;
4643 }
4644 else if (STRCMP(hi->hi_key, "cwd") == 0)
4645 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004646 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004647 break;
4648 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4649 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4650 {
4651 EMSG2(_(e_invarg2), "cwd");
4652 return FAIL;
4653 }
4654 opt->jo_set |= JO2_CWD;
4655 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004656 else if (STRCMP(hi->hi_key, "waittime") == 0)
4657 {
4658 if (!(supported & JO_WAITTIME))
4659 break;
4660 opt->jo_set |= JO_WAITTIME;
4661 opt->jo_waittime = get_tv_number(item);
4662 }
4663 else if (STRCMP(hi->hi_key, "timeout") == 0)
4664 {
4665 if (!(supported & JO_TIMEOUT))
4666 break;
4667 opt->jo_set |= JO_TIMEOUT;
4668 opt->jo_timeout = get_tv_number(item);
4669 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004670 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004671 {
4672 if (!(supported & JO_OUT_TIMEOUT))
4673 break;
4674 opt->jo_set |= JO_OUT_TIMEOUT;
4675 opt->jo_out_timeout = get_tv_number(item);
4676 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004677 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004678 {
4679 if (!(supported & JO_ERR_TIMEOUT))
4680 break;
4681 opt->jo_set |= JO_ERR_TIMEOUT;
4682 opt->jo_err_timeout = get_tv_number(item);
4683 }
4684 else if (STRCMP(hi->hi_key, "part") == 0)
4685 {
4686 if (!(supported & JO_PART))
4687 break;
4688 opt->jo_set |= JO_PART;
4689 val = get_tv_string(item);
4690 if (STRCMP(val, "err") == 0)
4691 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004692 else if (STRCMP(val, "out") == 0)
4693 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004694 else
4695 {
4696 EMSG2(_(e_invarg2), val);
4697 return FAIL;
4698 }
4699 }
4700 else if (STRCMP(hi->hi_key, "id") == 0)
4701 {
4702 if (!(supported & JO_ID))
4703 break;
4704 opt->jo_set |= JO_ID;
4705 opt->jo_id = get_tv_number(item);
4706 }
4707 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4708 {
4709 if (!(supported & JO_STOPONEXIT))
4710 break;
4711 opt->jo_set |= JO_STOPONEXIT;
4712 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4713 opt->jo_soe_buf);
4714 if (opt->jo_stoponexit == NULL)
4715 {
4716 EMSG2(_(e_invarg2), "stoponexit");
4717 return FAIL;
4718 }
4719 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004720 else if (STRCMP(hi->hi_key, "block_write") == 0)
4721 {
4722 if (!(supported & JO_BLOCK_WRITE))
4723 break;
4724 opt->jo_set |= JO_BLOCK_WRITE;
4725 opt->jo_block_write = get_tv_number(item);
4726 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727 else
4728 break;
4729 --todo;
4730 }
4731 if (todo > 0)
4732 {
4733 EMSG2(_(e_invarg2), hi->hi_key);
4734 return FAIL;
4735 }
4736
4737 return OK;
4738}
4739
4740/*
4741 * Get the channel from the argument.
4742 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004743 * When "check_open" is TRUE check that the channel can be used.
4744 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004745 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004746 */
4747 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004748get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004749{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004750 channel_T *channel = NULL;
4751 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004752
4753 if (tv->v_type == VAR_JOB)
4754 {
4755 if (tv->vval.v_job != NULL)
4756 channel = tv->vval.v_job->jv_channel;
4757 }
4758 else if (tv->v_type == VAR_CHANNEL)
4759 {
4760 channel = tv->vval.v_channel;
4761 }
4762 else
4763 {
4764 EMSG2(_(e_invarg2), get_tv_string(tv));
4765 return NULL;
4766 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004767 if (channel != NULL && reading)
4768 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004769 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004770
Bram Moolenaar437905c2016-04-26 19:01:05 +02004771 if (check_open && (channel == NULL || (!channel_is_open(channel)
4772 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004773 {
4774 EMSG(_("E906: not an open channel"));
4775 return NULL;
4776 }
4777 return channel;
4778}
4779
4780static job_T *first_job = NULL;
4781
4782 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004783job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004784{
4785 ch_log(job->jv_channel, "Freeing job");
4786 if (job->jv_channel != NULL)
4787 {
4788 /* The link from the channel to the job doesn't count as a reference,
4789 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004790 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004791 * NULL the reference. We don't set ch_job_killed, unreferencing the
4792 * job doesn't mean it stops running. */
4793 job->jv_channel->ch_job = NULL;
4794 channel_unref(job->jv_channel);
4795 }
4796 mch_clear_job(job);
4797
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004798 vim_free(job->jv_tty_name);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004799 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004800 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004801}
4802
4803 static void
4804job_free_job(job_T *job)
4805{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004806 if (job->jv_next != NULL)
4807 job->jv_next->jv_prev = job->jv_prev;
4808 if (job->jv_prev == NULL)
4809 first_job = job->jv_next;
4810 else
4811 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004812 vim_free(job);
4813}
4814
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004815 static void
4816job_free(job_T *job)
4817{
4818 if (!in_free_unref_items)
4819 {
4820 job_free_contents(job);
4821 job_free_job(job);
4822 }
4823}
4824
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004825#if defined(EXITFREE) || defined(PROTO)
4826 void
4827job_free_all(void)
4828{
4829 while (first_job != NULL)
4830 job_free(first_job);
4831}
4832#endif
4833
4834/*
4835 * Return TRUE if we need to check if the process of "job" has ended.
4836 */
4837 static int
4838job_need_end_check(job_T *job)
4839{
4840 return job->jv_status == JOB_STARTED
4841 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4842}
4843
4844/*
4845 * Return TRUE if the channel of "job" is still useful.
4846 */
4847 static int
4848job_channel_still_useful(job_T *job)
4849{
4850 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4851}
4852
4853/*
4854 * Return TRUE if the job should not be freed yet. Do not free the job when
4855 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4856 * or when the associated channel will do something with the job output.
4857 */
4858 static int
4859job_still_useful(job_T *job)
4860{
4861 return job_need_end_check(job) || job_channel_still_useful(job);
4862}
4863
Bram Moolenaardcaa6132017-08-13 17:13:09 +02004864#if !defined(USE_ARGV) || defined(PROTO)
4865/*
4866 * Escape one argument for an external command.
4867 * Returns the escaped string in allocated memory. NULL when out of memory.
4868 */
4869 static char_u *
4870win32_escape_arg(char_u *arg)
4871{
4872 int slen, dlen;
4873 int escaping = 0;
4874 int i;
4875 char_u *s, *d;
4876 char_u *escaped_arg;
4877 int has_spaces = FALSE;
4878
4879 /* First count the number of extra bytes required. */
4880 slen = STRLEN(arg);
4881 dlen = slen;
4882 for (s = arg; *s != NUL; MB_PTR_ADV(s))
4883 {
4884 if (*s == '"' || *s == '\\')
4885 ++dlen;
4886 if (*s == ' ' || *s == '\t')
4887 has_spaces = TRUE;
4888 }
4889
4890 if (has_spaces)
4891 dlen += 2;
4892
4893 if (dlen == slen)
4894 return vim_strsave(arg);
4895
4896 /* Allocate memory for the result and fill it. */
4897 escaped_arg = alloc(dlen + 1);
4898 if (escaped_arg == NULL)
4899 return NULL;
4900 memset(escaped_arg, 0, dlen+1);
4901
4902 d = escaped_arg;
4903
4904 if (has_spaces)
4905 *d++ = '"';
4906
4907 for (s = arg; *s != NUL;)
4908 {
4909 switch (*s)
4910 {
4911 case '"':
4912 for (i = 0; i < escaping; i++)
4913 *d++ = '\\';
4914 escaping = 0;
4915 *d++ = '\\';
4916 *d++ = *s++;
4917 break;
4918 case '\\':
4919 escaping++;
4920 *d++ = *s++;
4921 break;
4922 default:
4923 escaping = 0;
4924 MB_COPY_CHAR(s, d);
4925 break;
4926 }
4927 }
4928
4929 /* add terminating quote and finish with a NUL */
4930 if (has_spaces)
4931 {
4932 for (i = 0; i < escaping; i++)
4933 *d++ = '\\';
4934 *d++ = '"';
4935 }
4936 *d = NUL;
4937
4938 return escaped_arg;
4939}
4940
4941/*
4942 * Build a command line from a list, taking care of escaping.
4943 * The result is put in gap->ga_data.
4944 * Returns FAIL when out of memory.
4945 */
4946 int
4947win32_build_cmd(list_T *l, garray_T *gap)
4948{
4949 listitem_T *li;
4950 char_u *s;
4951
4952 for (li = l->lv_first; li != NULL; li = li->li_next)
4953 {
4954 s = get_tv_string_chk(&li->li_tv);
4955 if (s == NULL)
4956 return FAIL;
4957 s = win32_escape_arg(s);
4958 if (s == NULL)
4959 return FAIL;
4960 ga_concat(gap, s);
4961 vim_free(s);
4962 if (li->li_next != NULL)
4963 ga_append(gap, ' ');
4964 }
4965 return OK;
4966}
4967#endif
4968
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004969/*
4970 * NOTE: Must call job_cleanup() only once right after the status of "job"
4971 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4972 * mch_detect_ended_job() returned non-NULL).
4973 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004974 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02004975job_cleanup(job_T *job)
4976{
4977 if (job->jv_status != JOB_ENDED)
4978 return;
4979
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004980 /* Ready to cleanup the job. */
4981 job->jv_status = JOB_FINISHED;
4982
Bram Moolenaar97792de2016-10-15 18:36:49 +02004983 if (job->jv_exit_cb != NULL)
4984 {
4985 typval_T argv[3];
4986 typval_T rettv;
4987 int dummy;
4988
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004989 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02004990 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02004991 ++job->jv_refcount;
4992 argv[0].v_type = VAR_JOB;
4993 argv[0].vval.v_job = job;
4994 argv[1].v_type = VAR_NUMBER;
4995 argv[1].vval.v_number = job->jv_exitval;
4996 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4997 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4998 job->jv_exit_partial, NULL);
4999 clear_tv(&rettv);
5000 --job->jv_refcount;
5001 channel_need_redraw = TRUE;
5002 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005003
5004 /* Do not free the job in case the close callback of the associated channel
5005 * isn't invoked yet and may get information by job_info(). */
5006 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005007 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005008 /* The job was already unreferenced and the associated channel was
5009 * detached, now that it ended it can be freed. Careful: caller must
5010 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005011 job_free(job);
5012 }
5013}
5014
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005015/*
5016 * Mark references in jobs that are still useful.
5017 */
5018 int
5019set_ref_in_job(int copyID)
5020{
5021 int abort = FALSE;
5022 job_T *job;
5023 typval_T tv;
5024
5025 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005026 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005027 {
5028 tv.v_type = VAR_JOB;
5029 tv.vval.v_job = job;
5030 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5031 }
5032 return abort;
5033}
5034
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005035/*
5036 * Dereference "job". Note that after this "job" may have been freed.
5037 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005038 void
5039job_unref(job_T *job)
5040{
5041 if (job != NULL && --job->jv_refcount <= 0)
5042 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005043 /* Do not free the job if there is a channel where the close callback
5044 * may get the job info. */
5045 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005046 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005047 /* Do not free the job when it has not ended yet and there is a
5048 * "stoponexit" flag or an exit callback. */
5049 if (!job_need_end_check(job))
5050 {
5051 job_free(job);
5052 }
5053 else if (job->jv_channel != NULL)
5054 {
5055 /* Do remove the link to the channel, otherwise it hangs
5056 * around until Vim exits. See job_free() for refcount. */
5057 ch_log(job->jv_channel, "detaching channel from job");
5058 job->jv_channel->ch_job = NULL;
5059 channel_unref(job->jv_channel);
5060 job->jv_channel = NULL;
5061 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005062 }
5063 }
5064}
5065
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005066 int
5067free_unused_jobs_contents(int copyID, int mask)
5068{
5069 int did_free = FALSE;
5070 job_T *job;
5071
5072 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005073 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005074 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005075 {
5076 /* Free the channel and ordinary items it contains, but don't
5077 * recurse into Lists, Dictionaries etc. */
5078 job_free_contents(job);
5079 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005080 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005081 return did_free;
5082}
5083
5084 void
5085free_unused_jobs(int copyID, int mask)
5086{
5087 job_T *job;
5088 job_T *job_next;
5089
5090 for (job = first_job; job != NULL; job = job_next)
5091 {
5092 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005093 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005094 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005095 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005096 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005097 job_free_job(job);
5098 }
5099 }
5100}
5101
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005102/*
5103 * Allocate a job. Sets the refcount to one and sets options default.
5104 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005105 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005106job_alloc(void)
5107{
5108 job_T *job;
5109
5110 job = (job_T *)alloc_clear(sizeof(job_T));
5111 if (job != NULL)
5112 {
5113 job->jv_refcount = 1;
5114 job->jv_stoponexit = vim_strsave((char_u *)"term");
5115
5116 if (first_job != NULL)
5117 {
5118 first_job->jv_prev = job;
5119 job->jv_next = first_job;
5120 }
5121 first_job = job;
5122 }
5123 return job;
5124}
5125
5126 void
5127job_set_options(job_T *job, jobopt_T *opt)
5128{
5129 if (opt->jo_set & JO_STOPONEXIT)
5130 {
5131 vim_free(job->jv_stoponexit);
5132 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5133 job->jv_stoponexit = NULL;
5134 else
5135 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5136 }
5137 if (opt->jo_set & JO_EXIT_CB)
5138 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005139 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005140 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005141 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005142 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005143 job->jv_exit_partial = NULL;
5144 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005145 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005146 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005147 job->jv_exit_partial = opt->jo_exit_partial;
5148 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005149 {
5150 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005151 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005152 }
5153 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005154 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005155 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005156 func_ref(job->jv_exit_cb);
5157 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005158 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005159 }
5160}
5161
5162/*
5163 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5164 */
5165 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005166job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005167{
5168 job_T *job;
5169
5170 for (job = first_job; job != NULL; job = job->jv_next)
5171 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005172 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005173}
5174
5175/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005176 * Return TRUE when there is any job that has an exit callback and might exit,
5177 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005178 */
5179 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005180has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005181{
5182 job_T *job;
5183
5184 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005185 /* Only should check if the channel has been closed, if the channel is
5186 * open the job won't exit. */
5187 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005188 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005189 return TRUE;
5190 return FALSE;
5191}
5192
Bram Moolenaar97792de2016-10-15 18:36:49 +02005193#define MAX_CHECK_ENDED 8
5194
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005195/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005196 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005197 */
5198 void
5199job_check_ended(void)
5200{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005201 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005202
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005203 if (first_job == NULL)
5204 return;
5205
Bram Moolenaar97792de2016-10-15 18:36:49 +02005206 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005207 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005208 /* NOTE: mch_detect_ended_job() must only return a job of which the
5209 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005210 job_T *job = mch_detect_ended_job(first_job);
5211
5212 if (job == NULL)
5213 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005214 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005215 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005216
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005217 if (channel_need_redraw)
5218 {
5219 channel_need_redraw = FALSE;
5220 redraw_after_callback();
5221 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005222}
5223
5224/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005225 * Create a job and return it. Implements job_start().
5226 * The returned job has a refcount of one.
5227 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005228 */
5229 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005230job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005231{
5232 job_T *job;
5233 char_u *cmd = NULL;
5234#if defined(UNIX)
5235# define USE_ARGV
5236 char **argv = NULL;
5237 int argc = 0;
5238#else
5239 garray_T ga;
5240#endif
5241 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005242 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005243
5244 job = job_alloc();
5245 if (job == NULL)
5246 return NULL;
5247
5248 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005249#ifndef USE_ARGV
5250 ga_init2(&ga, (int)sizeof(char*), 20);
5251#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005252
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005253 if (opt_arg != NULL)
5254 opt = *opt_arg;
5255 else
5256 {
5257 /* Default mode is NL. */
5258 clear_job_options(&opt);
5259 opt.jo_mode = MODE_NL;
5260 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005261 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5262 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5263 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005264 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005265 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005266
5267 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005268 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005269 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5270 && opt.jo_io[part] == JIO_FILE
5271 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5272 || *opt.jo_io_name[part] == NUL))
5273 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005274 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005275 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005276 }
5277
5278 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5279 {
5280 buf_T *buf = NULL;
5281
5282 /* check that we can find the buffer before starting the job */
5283 if (opt.jo_set & JO_IN_BUF)
5284 {
5285 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5286 if (buf == NULL)
5287 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5288 }
5289 else if (!(opt.jo_set & JO_IN_NAME))
5290 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005291 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005292 }
5293 else
5294 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5295 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005296 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005297 if (buf->b_ml.ml_mfp == NULL)
5298 {
5299 char_u numbuf[NUMBUFLEN];
5300 char_u *s;
5301
5302 if (opt.jo_set & JO_IN_BUF)
5303 {
5304 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5305 s = numbuf;
5306 }
5307 else
5308 s = opt.jo_io_name[PART_IN];
5309 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005310 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005311 }
5312 job->jv_in_buf = buf;
5313 }
5314
5315 job_set_options(job, &opt);
5316
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005317 if (argvars[0].v_type == VAR_STRING)
5318 {
5319 /* Command is a string. */
5320 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005321 if (cmd == NULL || *cmd == NUL)
5322 {
5323 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005324 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005325 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005326#ifdef USE_ARGV
5327 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005328 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005329 argv[argc] = NULL;
5330#endif
5331 }
5332 else if (argvars[0].v_type != VAR_LIST
5333 || argvars[0].vval.v_list == NULL
5334 || argvars[0].vval.v_list->lv_len < 1)
5335 {
5336 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005337 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005338 }
5339 else
5340 {
5341 list_T *l = argvars[0].vval.v_list;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005342#ifdef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005343 listitem_T *li;
5344 char_u *s;
5345
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005346 /* Pass argv[] to mch_call_shell(). */
5347 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5348 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005349 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005350 for (li = l->lv_first; li != NULL; li = li->li_next)
5351 {
5352 s = get_tv_string_chk(&li->li_tv);
5353 if (s == NULL)
5354 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005355 argv[argc++] = (char *)s;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005356 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005357 argv[argc] = NULL;
5358#else
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005359 if (win32_build_cmd(l, &ga) == FAIL)
5360 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005361 cmd = ga.ga_data;
5362#endif
5363 }
5364
5365#ifdef USE_ARGV
5366 if (ch_log_active())
5367 {
5368 garray_T ga;
5369 int i;
5370
5371 ga_init2(&ga, (int)sizeof(char), 200);
5372 for (i = 0; i < argc; ++i)
5373 {
5374 if (i > 0)
5375 ga_concat(&ga, (char_u *)" ");
5376 ga_concat(&ga, (char_u *)argv[i]);
5377 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005378 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005379 ga_clear(&ga);
5380 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005381 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005382#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005383 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005384 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005385#endif
5386
5387 /* If the channel is reading from a buffer, write lines now. */
5388 if (job->jv_channel != NULL)
5389 channel_write_in(job->jv_channel);
5390
5391theend:
5392#ifdef USE_ARGV
5393 vim_free(argv);
5394#else
5395 vim_free(ga.ga_data);
5396#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005397 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005398 return job;
5399}
5400
5401/*
5402 * Get the status of "job" and invoke the exit callback when needed.
5403 * The returned string is not allocated.
5404 */
5405 char *
5406job_status(job_T *job)
5407{
5408 char *result;
5409
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005410 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005411 /* No need to check, dead is dead. */
5412 result = "dead";
5413 else if (job->jv_status == JOB_FAILED)
5414 result = "fail";
5415 else
5416 {
5417 result = mch_job_status(job);
5418 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005419 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005420 }
5421 return result;
5422}
5423
Bram Moolenaar8950a562016-03-12 15:22:55 +01005424/*
5425 * Implementation of job_info().
5426 */
5427 void
5428job_info(job_T *job, dict_T *dict)
5429{
5430 dictitem_T *item;
5431 varnumber_T nr;
5432
5433 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5434
5435 item = dictitem_alloc((char_u *)"channel");
5436 if (item == NULL)
5437 return;
5438 item->di_tv.v_lock = 0;
5439 item->di_tv.v_type = VAR_CHANNEL;
5440 item->di_tv.vval.v_channel = job->jv_channel;
5441 if (job->jv_channel != NULL)
5442 ++job->jv_channel->ch_refcount;
5443 if (dict_add(dict, item) == FAIL)
5444 dictitem_free(item);
5445
5446#ifdef UNIX
5447 nr = job->jv_pid;
5448#else
5449 nr = job->jv_proc_info.dwProcessId;
5450#endif
5451 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005452 dict_add_nr_str(dict, "tty", 0L,
5453 job->jv_tty_name != NULL ? job->jv_tty_name : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005454
5455 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005456 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005457 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5458}
5459
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005460/*
5461 * Send a signal to "job". Implements job_stop().
5462 * When "type" is not NULL use this for the type.
5463 * Otherwise use argvars[1] for the type.
5464 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005465 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005466job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005467{
5468 char_u *arg;
5469
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005470 if (type != NULL)
5471 arg = (char_u *)type;
5472 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005473 arg = (char_u *)"";
5474 else
5475 {
5476 arg = get_tv_string_chk(&argvars[1]);
5477 if (arg == NULL)
5478 {
5479 EMSG(_(e_invarg));
5480 return 0;
5481 }
5482 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005483 if (job->jv_status == JOB_FAILED)
5484 {
5485 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5486 return 0;
5487 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005488 if (job->jv_status == JOB_ENDED)
5489 {
5490 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5491 return 0;
5492 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005493 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005494 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005495 return 0;
5496
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005497 /* Assume that only "kill" will kill the job. */
5498 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005499 job->jv_channel->ch_job_killed = TRUE;
5500
5501 /* We don't try freeing the job, obviously the caller still has a
5502 * reference to it. */
5503 return 1;
5504}
5505
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005506#endif /* FEAT_JOB_CHANNEL */