blob: 9476276ac3e17898232bd608acc13028cc9008cb [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 Moolenaar99ef0622016-03-06 20:22:25 +01001376 * Write any 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 Moolenaar8b877ac2016-03-28 19:16:20 +02001449 * Write any lines waiting to be written to a channel.
1450 */
1451 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001452channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001453{
1454 channel_T *channel;
1455
1456 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1457 {
1458 chanpart_T *in_part = &channel->ch_part[PART_IN];
1459
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001460 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001461 {
1462 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001463 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001464 else
1465 channel_write_in(channel);
1466 }
1467 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001468}
1469
1470/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001471 * Write appended lines above the last one in "buf" to the channel.
1472 */
1473 void
1474channel_write_new_lines(buf_T *buf)
1475{
1476 channel_T *channel;
1477 int found_one = FALSE;
1478
1479 /* There could be more than one channel for the buffer, loop over all of
1480 * them. */
1481 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1482 {
1483 chanpart_T *in_part = &channel->ch_part[PART_IN];
1484 linenr_T lnum;
1485 int written = 0;
1486
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001487 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001488 {
1489 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001490 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001491 found_one = TRUE;
1492 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1493 ++lnum)
1494 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001495 if (!can_write_buf_line(channel))
1496 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001497 write_buf_line(buf, lnum, channel);
1498 ++written;
1499 }
1500
1501 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001502 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001503 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001504 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001505 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001506 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001507 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001508
1509 in_part->ch_buf_bot = lnum;
1510 }
1511 }
1512 if (!found_one)
1513 buf->b_write_to_channel = FALSE;
1514}
1515
1516/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001517 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001518 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001519 */
1520 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001521invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1522 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001523{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001524 typval_T rettv;
1525 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001526
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001527 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001528 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001529
Bram Moolenaar77073442016-02-13 23:23:53 +01001530 argv[0].v_type = VAR_CHANNEL;
1531 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001532
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001533 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1534 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001535 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001536 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001537}
1538
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001539/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001540 * Return the first node from "channel"/"part" without removing it.
1541 * Returns NULL if there is nothing.
1542 */
1543 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001544channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001545{
1546 readq_T *head = &channel->ch_part[part].ch_head;
1547
1548 return head->rq_next;
1549}
1550
1551/*
1552 * Return a pointer to the first NL in "node".
1553 * Skips over NUL characters.
1554 * Returns NULL if there is no NL.
1555 */
1556 char_u *
1557channel_first_nl(readq_T *node)
1558{
1559 char_u *buffer = node->rq_buffer;
1560 long_u i;
1561
1562 for (i = 0; i < node->rq_buflen; ++i)
1563 if (buffer[i] == NL)
1564 return buffer + i;
1565 return NULL;
1566}
1567
1568/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001569 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001570 * The caller must free it.
1571 * Returns NULL if there is nothing.
1572 */
1573 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001574channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001575{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001576 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001577 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001578 char_u *p;
1579
Bram Moolenaar77073442016-02-13 23:23:53 +01001580 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001581 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001582 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001583 p = node->rq_buffer;
1584 head->rq_next = node->rq_next;
1585 if (node->rq_next == NULL)
1586 head->rq_prev = NULL;
1587 else
1588 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001589 vim_free(node);
1590 return p;
1591}
1592
1593/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001595 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001596 */
1597 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001598channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001599{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001600 readq_T *head = &channel->ch_part[part].ch_head;
1601 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001602 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001603 char_u *res;
1604 char_u *p;
1605
1606 /* If there is only one buffer just get that one. */
1607 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1608 return channel_get(channel, part);
1609
1610 /* Concatenate everything into one buffer. */
1611 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001612 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001613 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001614 if (res == NULL)
1615 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001616 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001617 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001618 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001619 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001620 p += node->rq_buflen;
1621 }
1622 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001623
1624 /* Free all buffers */
1625 do
1626 {
1627 p = channel_get(channel, part);
1628 vim_free(p);
1629 } while (p != NULL);
1630
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001631 /* turn all NUL into NL */
1632 while (len > 0)
1633 {
1634 --len;
1635 if (res[len] == NUL)
1636 res[len] = NL;
1637 }
1638
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001639 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001640}
1641
1642/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001643 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001644 * Caller must check these bytes are available.
1645 */
1646 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001647channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001648{
1649 readq_T *head = &channel->ch_part[part].ch_head;
1650 readq_T *node = head->rq_next;
1651 char_u *buf = node->rq_buffer;
1652
1653 mch_memmove(buf, buf + len, node->rq_buflen - len);
1654 node->rq_buflen -= len;
1655}
1656
1657/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001658 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001659 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001660 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001661 */
1662 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001663channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001664{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001665 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001666 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001667 readq_T *last_node;
1668 readq_T *n;
1669 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001671 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672
Bram Moolenaar77073442016-02-13 23:23:53 +01001673 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001674 return FAIL;
1675
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001676 last_node = node->rq_next;
1677 len = node->rq_buflen + last_node->rq_buflen + 1;
1678 if (want_nl)
1679 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001680 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001681 {
1682 last_node = last_node->rq_next;
1683 len += last_node->rq_buflen;
1684 }
1685
1686 p = newbuf = alloc(len);
1687 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001688 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001689 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001690 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001691 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001692 node->rq_buffer = newbuf;
1693 for (n = node; n != last_node; )
1694 {
1695 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001696 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001697 p += n->rq_buflen;
1698 vim_free(n->rq_buffer);
1699 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001700 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001701
1702 /* dispose of the collapsed nodes and their buffers */
1703 for (n = node->rq_next; n != last_node; )
1704 {
1705 n = n->rq_next;
1706 vim_free(n->rq_prev);
1707 }
1708 node->rq_next = last_node->rq_next;
1709 if (last_node->rq_next == NULL)
1710 head->rq_prev = node;
1711 else
1712 last_node->rq_next->rq_prev = node;
1713 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001714 return OK;
1715}
1716
1717/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001718 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001719 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001720 * Returns OK or FAIL.
1721 */
1722 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001723channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001724 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001725{
1726 readq_T *node;
1727 readq_T *head = &channel->ch_part[part].ch_head;
1728 char_u *p;
1729 int i;
1730
1731 node = (readq_T *)alloc(sizeof(readq_T));
1732 if (node == NULL)
1733 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001734 /* A NUL is added at the end, because netbeans code expects that.
1735 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001736 node->rq_buffer = alloc(len + 1);
1737 if (node->rq_buffer == NULL)
1738 {
1739 vim_free(node);
1740 return FAIL; /* out of memory */
1741 }
1742
1743 if (channel->ch_part[part].ch_mode == MODE_NL)
1744 {
1745 /* Drop any CR before a NL. */
1746 p = node->rq_buffer;
1747 for (i = 0; i < len; ++i)
1748 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1749 *p++ = buf[i];
1750 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001751 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001752 }
1753 else
1754 {
1755 mch_memmove(node->rq_buffer, buf, len);
1756 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001757 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001758 }
1759
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001760 if (prepend)
1761 {
1762 /* preend node to the head of the queue */
1763 node->rq_next = head->rq_next;
1764 node->rq_prev = NULL;
1765 if (head->rq_next == NULL)
1766 head->rq_prev = node;
1767 else
1768 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001770 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001771 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001772 {
1773 /* append node to the tail of the queue */
1774 node->rq_next = NULL;
1775 node->rq_prev = head->rq_prev;
1776 if (head->rq_prev == NULL)
1777 head->rq_next = node;
1778 else
1779 head->rq_prev->rq_next = node;
1780 head->rq_prev = node;
1781 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001782
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001783 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001784 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001785 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001786 fprintf(log_fd, "'");
1787 if (fwrite(buf, len, 1, log_fd) != 1)
1788 return FAIL;
1789 fprintf(log_fd, "'\n");
1790 }
1791 return OK;
1792}
1793
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001794/*
1795 * Try to fill the buffer of "reader".
1796 * Returns FALSE when nothing was added.
1797 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001798 static int
1799channel_fill(js_read_T *reader)
1800{
1801 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001802 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001803 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001804 int keeplen;
1805 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001806 char_u *p;
1807
1808 if (next == NULL)
1809 return FALSE;
1810
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001811 keeplen = reader->js_end - reader->js_buf;
1812 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001813 {
1814 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001815 addlen = (int)STRLEN(next);
1816 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001817 if (p == NULL)
1818 {
1819 vim_free(next);
1820 return FALSE;
1821 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001822 mch_memmove(p, reader->js_buf, keeplen);
1823 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001824 vim_free(next);
1825 next = p;
1826 }
1827
1828 vim_free(reader->js_buf);
1829 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001830 return TRUE;
1831}
1832
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001833/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001834 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001835 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001836 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001837 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001838 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001839channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001840{
1841 js_read_T reader;
1842 typval_T listtv;
1843 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001844 chanpart_T *chanpart = &channel->ch_part[part];
1845 jsonq_T *head = &chanpart->ch_json_head;
1846 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001848
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001849 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001850 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001851
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001852 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001853 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001854 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001855 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001856 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001857
1858 /* When a message is incomplete we wait for a short while for more to
1859 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001860 * or list will make us hang.
1861 * Do not generate error messages, they will be written in a channel log. */
1862 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001863 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001864 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001865 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001866 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001867 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001868 /* Only accept the response when it is a list with at least two
1869 * items. */
1870 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001871 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001872 if (listtv.v_type != VAR_LIST)
1873 ch_error(channel, "Did not receive a list, discarding");
1874 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001875 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001876 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001877 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001878 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001879 else
1880 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001881 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1882 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001883 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001884 else
1885 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001886 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001887 item->jq_value = alloc_tv();
1888 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001889 {
1890 vim_free(item);
1891 clear_tv(&listtv);
1892 }
1893 else
1894 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001895 *item->jq_value = listtv;
1896 item->jq_prev = head->jq_prev;
1897 head->jq_prev = item;
1898 item->jq_next = NULL;
1899 if (item->jq_prev == NULL)
1900 head->jq_next = item;
1901 else
1902 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001903 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001904 }
1905 }
1906 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001907
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001908 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001909 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001910 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001911 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001912 size_t buflen = STRLEN(reader.js_buf);
1913
1914 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001915 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001916 /* First time encountering incomplete message or after receiving
1917 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001918 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001919 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001920 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001921 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001922 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001923#ifdef WIN32
1924 chanpart->ch_deadline = GetTickCount() + 100L;
1925#else
1926 gettimeofday(&chanpart->ch_deadline, NULL);
1927 chanpart->ch_deadline.tv_usec += 100 * 1000;
1928 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1929 {
1930 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1931 ++chanpart->ch_deadline.tv_sec;
1932 }
1933#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001934 }
1935 else
1936 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001937 int timeout;
1938#ifdef WIN32
1939 timeout = GetTickCount() > chanpart->ch_deadline;
1940#else
1941 {
1942 struct timeval now_tv;
1943
1944 gettimeofday(&now_tv, NULL);
1945 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1946 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1947 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1948 }
1949#endif
1950 if (timeout)
1951 {
1952 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001953 chanpart->ch_wait_len = 0;
1954 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001955 }
1956 else
1957 {
1958 reader.js_used = 0;
1959 ch_log(channel, "still waiting on incomplete message");
1960 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001961 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001962 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001963
1964 if (status == FAIL)
1965 {
1966 ch_error(channel, "Decoding failed - discarding input");
1967 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001968 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969 }
1970 else if (reader.js_buf[reader.js_used] != NUL)
1971 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001972 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001973 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001974 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1975 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001976 ret = status == MAYBE ? FALSE: TRUE;
1977 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001978 else
1979 ret = FALSE;
1980
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001981 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001982 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001983}
1984
1985/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001986 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001987 */
1988 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001989remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001990{
Bram Moolenaar77073442016-02-13 23:23:53 +01001991 if (node->cq_prev == NULL)
1992 head->cq_next = node->cq_next;
1993 else
1994 node->cq_prev->cq_next = node->cq_next;
1995 if (node->cq_next == NULL)
1996 head->cq_prev = node->cq_prev;
1997 else
1998 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001999}
2000
2001/*
2002 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002003 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002004 */
2005 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002006remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002007{
Bram Moolenaar77073442016-02-13 23:23:53 +01002008 if (node->jq_prev == NULL)
2009 head->jq_next = node->jq_next;
2010 else
2011 node->jq_prev->jq_next = node->jq_next;
2012 if (node->jq_next == NULL)
2013 head->jq_prev = node->jq_prev;
2014 else
2015 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 vim_free(node);
2017}
2018
2019/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002020 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002021 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002022 * When "id" is zero or negative jut get the first message. But not the one
2023 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002024 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002025 * Return OK when found and return the value in "rettv".
2026 * Return FAIL otherwise.
2027 */
2028 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002029channel_get_json(
2030 channel_T *channel,
2031 ch_part_T part,
2032 int id,
2033 int without_callback,
2034 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002035{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002036 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002037 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002038
Bram Moolenaar77073442016-02-13 23:23:53 +01002039 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002040 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002041 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002042 typval_T *tv = &l->lv_first->li_tv;
2043
Bram Moolenaar958dc692016-12-01 15:34:12 +01002044 if ((without_callback || !item->jq_no_callback)
2045 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002046 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002047 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002048 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002049 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002051 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002052 ch_log(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002053 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002054 return OK;
2055 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002056 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057 }
2058 return FAIL;
2059}
2060
Bram Moolenaar958dc692016-12-01 15:34:12 +01002061/*
2062 * Put back "rettv" into the JSON queue, there was no callback for it.
2063 * Takes over the values in "rettv".
2064 */
2065 static void
2066channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2067{
2068 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2069 jsonq_T *item = head->jq_next;
2070 jsonq_T *newitem;
2071
2072 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2073 /* last item was pushed back, append to the end */
2074 item = NULL;
2075 else while (item != NULL && item->jq_no_callback)
2076 /* append after the last item that was pushed back */
2077 item = item->jq_next;
2078
2079 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2080 if (newitem == NULL)
2081 clear_tv(rettv);
2082 else
2083 {
2084 newitem->jq_value = alloc_tv();
2085 if (newitem->jq_value == NULL)
2086 {
2087 vim_free(newitem);
2088 clear_tv(rettv);
2089 }
2090 else
2091 {
2092 newitem->jq_no_callback = FALSE;
2093 *newitem->jq_value = *rettv;
2094 if (item == NULL)
2095 {
2096 /* append to the end */
2097 newitem->jq_prev = head->jq_prev;
2098 head->jq_prev = newitem;
2099 newitem->jq_next = NULL;
2100 if (newitem->jq_prev == NULL)
2101 head->jq_next = newitem;
2102 else
2103 newitem->jq_prev->jq_next = newitem;
2104 }
2105 else
2106 {
2107 /* append after "item" */
2108 newitem->jq_prev = item;
2109 newitem->jq_next = item->jq_next;
2110 item->jq_next = newitem;
2111 if (newitem->jq_next == NULL)
2112 head->jq_prev = newitem;
2113 else
2114 newitem->jq_next->jq_prev = newitem;
2115 }
2116 }
2117 }
2118}
2119
Bram Moolenaarece61b02016-02-20 21:39:05 +01002120#define CH_JSON_MAX_ARGS 4
2121
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002122/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002123 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002124 * "argv[0]" is the command string.
2125 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002126 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002127 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002128channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002129{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002130 char_u *cmd = argv[0].vval.v_string;
2131 char_u *arg;
2132 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002133
Bram Moolenaarece61b02016-02-20 21:39:05 +01002134 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002135 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002136 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002137 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002138 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002139 return;
2140 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002141 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002142 if (arg == NULL)
2143 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002144
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002145 if (STRCMP(cmd, "ex") == 0)
2146 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002147 int save_called_emsg = called_emsg;
2148
2149 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002150 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002151 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002152 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002153 --emsg_silent;
2154 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002155 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002156 (char *)get_vim_var_str(VV_ERRMSG));
2157 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002158 }
2159 else if (STRCMP(cmd, "normal") == 0)
2160 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002161 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002162
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002163 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002164 ea.arg = arg;
2165 ea.addr_count = 0;
2166 ea.forceit = TRUE; /* no mapping */
2167 ex_normal(&ea);
2168 }
2169 else if (STRCMP(cmd, "redraw") == 0)
2170 {
2171 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002172
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002173 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002174 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002175 ex_redraw(&ea);
2176 showruler(FALSE);
2177 setcursor();
2178 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002179#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002180 if (gui.in_use)
2181 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002182 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002183 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002184 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002185#endif
2186 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002187 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002188 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002189 int is_call = cmd[0] == 'c';
2190 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002191
Bram Moolenaarece61b02016-02-20 21:39:05 +01002192 if (argv[id_idx].v_type != VAR_UNKNOWN
2193 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002194 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002195 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002196 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002197 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 }
2199 else if (is_call && argv[2].v_type != VAR_LIST)
2200 {
2201 ch_error(channel, "third argument for call must be a list");
2202 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002203 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002204 }
2205 else
2206 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002207 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002208 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002209 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002210 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002212 /* Don't pollute the display with errors. */
2213 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002214 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002215 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002216 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002217 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002218 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002219 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002220 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002221 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002222 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2223 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002224 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002225
2226 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002227 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002228 int id = argv[id_idx].vval.v_number;
2229
Bram Moolenaar55fab432016-02-07 16:53:13 +01002230 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002231 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002232 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002233 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002234 /* If evaluation failed or the result can't be encoded
2235 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002236 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002237 err_tv.v_type = VAR_STRING;
2238 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002239 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002240 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002241 if (json != NULL)
2242 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 channel_send(channel,
2244 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002245 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002246 vim_free(json);
2247 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002248 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002249 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002250 if (tv == &res_tv)
2251 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002252 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002253 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002254 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002255 }
2256 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002257 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002258 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002259 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002260 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002261}
2262
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002263/*
2264 * Invoke the callback at "cbhead".
2265 * Does not redraw but sets channel_need_redraw.
2266 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002267 static void
2268invoke_one_time_callback(
2269 channel_T *channel,
2270 cbq_T *cbhead,
2271 cbq_T *item,
2272 typval_T *argv)
2273{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002274 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002275 (char *)item->cq_callback);
2276 /* Remove the item from the list first, if the callback
2277 * invokes ch_close() the list will be cleared. */
2278 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002279 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002280 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002281 vim_free(item);
2282}
2283
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002284 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002285append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002286{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002287 bufref_T save_curbuf = {NULL, 0, 0};
2288 win_T *save_curwin = NULL;
2289 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002290 linenr_T lnum = buffer->b_ml.ml_line_count;
2291 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002292 chanpart_T *ch_part = &channel->ch_part[part];
2293 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002294 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002295
2296 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2297 {
2298 if (!ch_part->ch_nomod_error)
2299 {
2300 ch_error(channel, "Buffer is not modifiable, cannot append");
2301 ch_part->ch_nomod_error = TRUE;
2302 }
2303 return;
2304 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002305
2306 /* If the buffer is also used as input insert above the last
2307 * line. Don't write these lines. */
2308 if (save_write_to)
2309 {
2310 --lnum;
2311 buffer->b_write_to_channel = FALSE;
2312 }
2313
2314 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002315 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002316
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002317 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002318
2319 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2320 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2321
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002322 u_sync(TRUE);
2323 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002324 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002325
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002326 if (empty)
2327 {
2328 /* The buffer is empty, replace the first (dummy) line. */
2329 ml_replace(lnum, msg, TRUE);
2330 lnum = 0;
2331 }
2332 else
2333 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002334 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002335
2336 /* Restore curbuf/curwin/curtab */
2337 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2338
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002339 if (ch_part->ch_nomodifiable)
2340 buffer->b_p_ma = FALSE;
2341 else
2342 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002343
2344 if (buffer->b_nwindows > 0)
2345 {
2346 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002347
2348 FOR_ALL_WINDOWS(wp)
2349 {
2350 if (wp->w_buffer == buffer
2351 && (save_write_to
2352 ? wp->w_cursor.lnum == lnum + 1
2353 : (wp->w_cursor.lnum == lnum
2354 && wp->w_cursor.col == 0)))
2355 {
2356 ++wp->w_cursor.lnum;
2357 save_curwin = curwin;
2358 curwin = wp;
2359 curbuf = curwin->w_buffer;
2360 scroll_cursor_bot(0, FALSE);
2361 curwin = save_curwin;
2362 curbuf = curwin->w_buffer;
2363 }
2364 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002365 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002366 channel_need_redraw = TRUE;
2367 }
2368
2369 if (save_write_to)
2370 {
2371 channel_T *ch;
2372
2373 /* Find channels reading from this buffer and adjust their
2374 * next-to-read line number. */
2375 buffer->b_write_to_channel = TRUE;
2376 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2377 {
2378 chanpart_T *in_part = &ch->ch_part[PART_IN];
2379
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002380 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002381 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2382 }
2383 }
2384}
2385
Bram Moolenaar437905c2016-04-26 19:01:05 +02002386 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002387drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002388{
2389 char_u *msg;
2390
2391 while ((msg = channel_get(channel, part)) != NULL)
2392 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002393 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002394 vim_free(msg);
2395 }
2396}
2397
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002398/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002399 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002400 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002401 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002402 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002403 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002404may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002405{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002406 char_u *msg = NULL;
2407 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002408 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002409 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002410 chanpart_T *ch_part = &channel->ch_part[part];
2411 ch_mode_T ch_mode = ch_part->ch_mode;
2412 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002413 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002414 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002415 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002416 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002417 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002418
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002419 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002420 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002421 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002422
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002423 /* Use a message-specific callback, part callback or channel callback */
2424 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2425 if (cbitem->cq_seq_nr == 0)
2426 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002427 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002428 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002429 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002430 partial = cbitem->cq_partial;
2431 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002432 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002433 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002434 callback = ch_part->ch_callback;
2435 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002436 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002437 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002438 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002439 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002440 partial = channel->ch_partial;
2441 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002442
Bram Moolenaarec68a992016-10-03 21:37:41 +02002443 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002444 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2445 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002446 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002447 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002448 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002449 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002450 buffer = NULL;
2451 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002452
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002453 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002454 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002455 listitem_T *item;
2456 int argc = 0;
2457
Bram Moolenaard7ece102016-02-02 23:23:02 +01002458 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002459 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002460 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002461 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002462 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002463 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002464 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002465 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002466
Bram Moolenaarece61b02016-02-20 21:39:05 +01002467 for (item = listtv->vval.v_list->lv_first;
2468 item != NULL && argc < CH_JSON_MAX_ARGS;
2469 item = item->li_next)
2470 argv[argc++] = item->li_tv;
2471 while (argc < CH_JSON_MAX_ARGS)
2472 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002473
Bram Moolenaarece61b02016-02-20 21:39:05 +01002474 if (argv[0].v_type == VAR_STRING)
2475 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002476 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002477 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002478 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002479 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002480 }
2481
Bram Moolenaarece61b02016-02-20 21:39:05 +01002482 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002483 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002484 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002485 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002486 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002487 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002488 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002489 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002490 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002491 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002492 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002493 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002494 return FALSE;
2495 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002496 else
2497 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002498 /* If there is no callback or buffer drop the message. */
2499 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002500 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002501 /* If there is a close callback it may use ch_read() to get the
2502 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002503 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002504 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002505 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002506 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002507
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002508 if (ch_mode == MODE_NL)
2509 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002510 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002511 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002512 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002513
2514 /* See if we have a message ending in NL in the first buffer. If
2515 * not try to concatenate the first and the second buffer. */
2516 while (TRUE)
2517 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002518 node = channel_peek(channel, part);
2519 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002520 if (nl != NULL)
2521 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002522 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002523 {
2524 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2525 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002526 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002527 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002528 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002529 buf = node->rq_buffer;
2530
Bram Moolenaarec68a992016-10-03 21:37:41 +02002531 if (nl == NULL)
2532 {
2533 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002534 char_u *new_buf;
2535
2536 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2537 if (new_buf == NULL)
2538 /* This might fail over and over again, should the message
2539 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002540 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002541 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002542 node->rq_buffer = buf;
2543 nl = buf + node->rq_buflen++;
2544 *nl = NUL;
2545 }
2546
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002547 /* Convert NUL to NL, the internal representation. */
2548 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2549 if (*p == NUL)
2550 *p = NL;
2551
2552 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002553 {
2554 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002555 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002556 *nl = NUL;
2557 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002558 else
2559 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002560 /* Copy the message into allocated memory (excluding the NL)
2561 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002562 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002563 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002564 }
2565 }
2566 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002567 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002568 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002569 * get everything we have.
2570 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002571 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002572 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002573
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002574 if (msg == NULL)
2575 return FALSE; /* out of memory (and avoids Coverity warning) */
2576
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002577 argv[1].v_type = VAR_STRING;
2578 argv[1].vval.v_string = msg;
2579 }
2580
Bram Moolenaara07fec92016-02-05 21:04:08 +01002581 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002582 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002583 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002584
Bram Moolenaar958dc692016-12-01 15:34:12 +01002585 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002586 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002587 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002588 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002589 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002590 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002591 break;
2592 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002593 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002594 {
2595 if (channel->ch_drop_never)
2596 {
2597 /* message must be read with ch_read() */
2598 channel_push_json(channel, part, listtv);
2599 listtv = NULL;
2600 }
2601 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002602 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002603 seq_nr);
2604 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002605 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002606 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002607 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002608 if (buffer != NULL)
2609 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002610 if (msg == NULL)
2611 /* JSON or JS mode: re-encode the message. */
2612 msg = json_encode(listtv, ch_mode);
2613 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002614 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002615#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002616 if (buffer->b_term != NULL)
2617 write_to_term(buffer, msg, channel);
2618 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002619#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002620 append_to_buffer(buffer, msg, channel, part);
2621 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002622 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002623
Bram Moolenaar187db502016-02-27 14:44:26 +01002624 if (callback != NULL)
2625 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002626 if (cbitem != NULL)
2627 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2628 else
2629 {
2630 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002631 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002632 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002633 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002634 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002635 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002636 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002637 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002638 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002639
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002640 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002641 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002642 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002643
2644 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002645}
2646
2647/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002648 * Return TRUE when channel "channel" is open for writing to.
2649 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002650 */
2651 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002652channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002653{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002654 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002655 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002656}
2657
2658/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002659 * Return TRUE when channel "channel" is open for reading or writing.
2660 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002661 */
2662 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002663channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002664{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002665 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002666 || channel->CH_IN_FD != INVALID_FD
2667 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002668 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002669}
2670
2671/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002672 * Return TRUE if "channel" has JSON or other typeahead.
2673 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002674 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002675channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002676{
2677 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2678
2679 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2680 {
2681 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2682 jsonq_T *item = head->jq_next;
2683
2684 return item != NULL;
2685 }
2686 return channel_peek(channel, part) != NULL;
2687}
2688
2689/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002690 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002691 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002692 */
2693 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002694channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002695{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002696 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002697 int has_readahead = FALSE;
2698
Bram Moolenaar77073442016-02-13 23:23:53 +01002699 if (channel == NULL)
2700 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002701 if (req_part == PART_OUT)
2702 {
2703 if (channel->CH_OUT_FD != INVALID_FD)
2704 return "open";
2705 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002706 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002707 }
2708 else if (req_part == PART_ERR)
2709 {
2710 if (channel->CH_ERR_FD != INVALID_FD)
2711 return "open";
2712 if (channel_has_readahead(channel, PART_ERR))
2713 has_readahead = TRUE;
2714 }
2715 else
2716 {
2717 if (channel_is_open(channel))
2718 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002719 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002720 if (channel_has_readahead(channel, part))
2721 {
2722 has_readahead = TRUE;
2723 break;
2724 }
2725 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002726
2727 if (has_readahead)
2728 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002729 return "closed";
2730}
2731
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002732 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002733channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002734{
2735 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002736 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002737 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002738 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002739 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002740
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002741 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002742 STRCAT(namebuf, "_");
2743 tail = STRLEN(namebuf);
2744
2745 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002746 if (chanpart->ch_fd != INVALID_FD)
2747 status = "open";
2748 else if (channel_has_readahead(channel, part))
2749 status = "buffered";
2750 else
2751 status = "closed";
2752 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002753
2754 STRCPY(namebuf + tail, "mode");
2755 switch (chanpart->ch_mode)
2756 {
2757 case MODE_NL: s = "NL"; break;
2758 case MODE_RAW: s = "RAW"; break;
2759 case MODE_JSON: s = "JSON"; break;
2760 case MODE_JS: s = "JS"; break;
2761 }
2762 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2763
2764 STRCPY(namebuf + tail, "io");
2765 if (part == PART_SOCK)
2766 s = "socket";
2767 else switch (chanpart->ch_io)
2768 {
2769 case JIO_NULL: s = "null"; break;
2770 case JIO_PIPE: s = "pipe"; break;
2771 case JIO_FILE: s = "file"; break;
2772 case JIO_BUFFER: s = "buffer"; break;
2773 case JIO_OUT: s = "out"; break;
2774 }
2775 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2776
2777 STRCPY(namebuf + tail, "timeout");
2778 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2779}
2780
2781 void
2782channel_info(channel_T *channel, dict_T *dict)
2783{
2784 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002785 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002786
2787 if (channel->ch_hostname != NULL)
2788 {
2789 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2790 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2791 channel_part_info(channel, dict, "sock", PART_SOCK);
2792 }
2793 else
2794 {
2795 channel_part_info(channel, dict, "out", PART_OUT);
2796 channel_part_info(channel, dict, "err", PART_ERR);
2797 channel_part_info(channel, dict, "in", PART_IN);
2798 }
2799}
2800
Bram Moolenaar77073442016-02-13 23:23:53 +01002801/*
2802 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002803 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002804 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002805 */
2806 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002807channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002808{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002809 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002810
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002811#ifdef FEAT_GUI
2812 channel_gui_unregister(channel);
2813#endif
2814
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002815 ch_close_part(channel, PART_SOCK);
2816 ch_close_part(channel, PART_IN);
2817 ch_close_part(channel, PART_OUT);
2818 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002819
Bram Moolenaara9f02812017-08-05 17:40:38 +02002820 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002821 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002822 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002823
Bram Moolenaara9f02812017-08-05 17:40:38 +02002824 /* Invoke callbacks and flush buffers before the close callback. */
2825 if (channel->ch_close_cb != NULL)
2826 ch_log(channel,
2827 "Invoking callbacks and flushing buffers before closing");
2828 for (part = PART_SOCK; part < PART_IN; ++part)
2829 {
2830 if (channel->ch_close_cb != NULL
2831 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2832 {
2833 /* Increment the refcount to avoid the channel being freed
2834 * halfway. */
2835 ++channel->ch_refcount;
2836 if (channel->ch_close_cb == NULL)
2837 ch_log(channel, "flushing %s buffers before closing",
2838 part_names[part]);
2839 while (may_invoke_callback(channel, part))
2840 ;
2841 --channel->ch_refcount;
2842 }
2843 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002844
Bram Moolenaara9f02812017-08-05 17:40:38 +02002845 if (channel->ch_close_cb != NULL)
2846 {
2847 typval_T argv[1];
2848 typval_T rettv;
2849 int dummy;
2850
2851 /* Increment the refcount to avoid the channel being freed
2852 * halfway. */
2853 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002854 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002855 (char *)channel->ch_close_cb);
2856 argv[0].v_type = VAR_CHANNEL;
2857 argv[0].vval.v_channel = channel;
2858 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002859 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002860 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002861 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002862 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002863
Bram Moolenaara9f02812017-08-05 17:40:38 +02002864 /* the callback is only called once */
2865 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2866 channel->ch_close_cb = NULL;
2867 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002868
Bram Moolenaara9f02812017-08-05 17:40:38 +02002869 --channel->ch_refcount;
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002870
Bram Moolenaara9f02812017-08-05 17:40:38 +02002871 if (channel_need_redraw)
2872 {
2873 channel_need_redraw = FALSE;
2874 redraw_after_callback();
2875 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002876
Bram Moolenaara9f02812017-08-05 17:40:38 +02002877 if (!channel->ch_drop_never)
2878 /* any remaining messages are useless now */
2879 for (part = PART_SOCK; part < PART_IN; ++part)
2880 drop_messages(channel, part);
2881 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002882 }
2883
2884 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002885
2886#ifdef FEAT_TERMINAL
2887 term_channel_closed(channel);
2888#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002889}
2890
Bram Moolenaard04a0202016-01-26 23:30:18 +01002891/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002892 * Close the "in" part channel "channel".
2893 */
2894 void
2895channel_close_in(channel_T *channel)
2896{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002897 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002898}
2899
2900/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002901 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002902 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002903 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002904channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002905{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002906 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2907 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002908
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002909 while (channel_peek(channel, part) != NULL)
2910 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002911
2912 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002913 {
2914 cbq_T *node = cb_head->cq_next;
2915
2916 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002917 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002918 vim_free(node);
2919 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002920
2921 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002922 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002923 free_tv(json_head->jq_next->jq_value);
2924 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002925 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002926
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002927 free_callback(channel->ch_part[part].ch_callback,
2928 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002929 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002930 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002931}
2932
2933/*
2934 * Clear all the read buffers on "channel".
2935 */
2936 void
2937channel_clear(channel_T *channel)
2938{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002939 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002940 vim_free(channel->ch_hostname);
2941 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002942 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002943 channel_clear_one(channel, PART_OUT);
2944 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002945 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002946 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002947 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002948 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002949 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002950 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002951 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002952}
2953
Bram Moolenaar77073442016-02-13 23:23:53 +01002954#if defined(EXITFREE) || defined(PROTO)
2955 void
2956channel_free_all(void)
2957{
2958 channel_T *channel;
2959
Bram Moolenaard6051b52016-02-28 15:49:03 +01002960 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002961 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2962 channel_clear(channel);
2963}
2964#endif
2965
2966
Bram Moolenaar715d2852016-04-30 17:06:31 +02002967/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002968#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002969
2970/* Buffer size for reading incoming messages. */
2971#define MAXMSGSIZE 4096
2972
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002973#if defined(HAVE_SELECT)
2974/*
2975 * Add write fds where we are waiting for writing to be possible.
2976 */
2977 static int
2978channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2979{
2980 int maxfd = maxfd_arg;
2981 channel_T *ch;
2982
2983 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2984 {
2985 chanpart_T *in_part = &ch->ch_part[PART_IN];
2986
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002987 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002988 {
2989 FD_SET((int)in_part->ch_fd, wfds);
2990 if ((int)in_part->ch_fd >= maxfd)
2991 maxfd = (int)in_part->ch_fd + 1;
2992 }
2993 }
2994 return maxfd;
2995}
2996#else
2997/*
2998 * Add write fds where we are waiting for writing to be possible.
2999 */
3000 static int
3001channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3002{
3003 int nfd = nfd_in;
3004 channel_T *ch;
3005
3006 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3007 {
3008 chanpart_T *in_part = &ch->ch_part[PART_IN];
3009
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003010 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003011 {
3012 in_part->ch_poll_idx = nfd;
3013 fds[nfd].fd = in_part->ch_fd;
3014 fds[nfd].events = POLLOUT;
3015 ++nfd;
3016 }
3017 else
3018 in_part->ch_poll_idx = -1;
3019 }
3020 return nfd;
3021}
3022#endif
3023
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003024typedef enum {
3025 CW_READY,
3026 CW_NOT_READY,
3027 CW_ERROR
3028} channel_wait_result;
3029
Bram Moolenaard04a0202016-01-26 23:30:18 +01003030/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003031 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003032 * Return CW_READY when there is something to read.
3033 * Return CW_NOT_READY when there is nothing to read.
3034 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003035 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003036 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003037channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003038{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003039 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003040 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003041
Bram Moolenaard8070362016-02-15 21:56:54 +01003042# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003043 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003044 {
3045 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003046 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003047 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003048 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003049
3050 /* reading from a pipe, not a socket */
3051 while (TRUE)
3052 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003053 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3054
3055 if (r && nread > 0)
3056 return CW_READY;
3057 if (r == 0)
3058 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003059
3060 /* perhaps write some buffer lines */
3061 channel_write_any_lines();
3062
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003063 sleep_time = deadline - GetTickCount();
3064 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003065 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003066 /* Wait for a little while. Very short at first, up to 10 msec
3067 * after looping a few times. */
3068 if (sleep_time > delay)
3069 sleep_time = delay;
3070 Sleep(sleep_time);
3071 delay = delay * 2;
3072 if (delay > 10)
3073 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003074 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003075 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003076 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003077#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003078 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003079#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003080 struct timeval tval;
3081 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003082 fd_set wfds;
3083 int ret;
3084 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003085
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003086 tval.tv_sec = timeout / 1000;
3087 tval.tv_usec = (timeout % 1000) * 1000;
3088 for (;;)
3089 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003090 FD_ZERO(&rfds);
3091 FD_SET((int)fd, &rfds);
3092
3093 /* Write lines to a pipe when a pipe can be written to. Need to
3094 * set this every time, some buffers may be done. */
3095 maxfd = (int)fd + 1;
3096 FD_ZERO(&wfds);
3097 maxfd = channel_fill_wfds(maxfd, &wfds);
3098
3099 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003100# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003101 SOCK_ERRNO;
3102 if (ret == -1 && errno == EINTR)
3103 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003104# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003105 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003106 {
3107 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003108 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003109 channel_write_any_lines();
3110 continue;
3111 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003112 break;
3113 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003114#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003115 for (;;)
3116 {
3117 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3118 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003119
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003120 fds[0].fd = fd;
3121 fds[0].events = POLLIN;
3122 nfd = channel_fill_poll_write(nfd, fds);
3123 if (poll(fds, nfd, timeout) > 0)
3124 {
3125 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003126 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003127 channel_write_any_lines();
3128 continue;
3129 }
3130 break;
3131 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003132#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003133 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003134 return CW_NOT_READY;
3135}
3136
3137 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003138ch_close_part_on_error(
3139 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003140{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003141 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003142
3143 if (is_err)
3144 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003145 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003146 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003147 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003148
3149 /* Queue a "DETACH" netbeans message in the command queue in order to
3150 * terminate the netbeans session later. Do not end the session here
3151 * directly as we may be running in the context of a call to
3152 * netbeans_parse_messages():
3153 * netbeans_parse_messages
3154 * -> autocmd triggered while processing the netbeans cmd
3155 * -> ui_breakcheck
3156 * -> gui event loop or select loop
3157 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003158 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003159 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003160 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003161 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003162 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3163
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003164 /* When reading is not possible close this part of the channel. Don't
3165 * close the channel yet, there may be something to read on another part. */
3166 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003167
3168#ifdef FEAT_GUI
3169 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003170 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003171#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003172}
3173
3174 static void
3175channel_close_now(channel_T *channel)
3176{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003177 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003178 if (channel->ch_nb_close_cb != NULL)
3179 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003180 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003181}
3182
3183/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003184 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003185 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003186 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003187 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003188 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003189channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003190{
3191 static char_u *buf = NULL;
3192 int len = 0;
3193 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003194 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003195 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003196
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003197 fd = channel->ch_part[part].ch_fd;
3198 if (fd == INVALID_FD)
3199 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003200 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003201 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003202 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003203 }
3204 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003205
3206 /* Allocate a buffer to read into. */
3207 if (buf == NULL)
3208 {
3209 buf = alloc(MAXMSGSIZE);
3210 if (buf == NULL)
3211 return; /* out of memory! */
3212 }
3213
3214 /* Keep on reading for as long as there is something to read.
3215 * Use select() or poll() to avoid blocking on a message that is exactly
3216 * MAXMSGSIZE long. */
3217 for (;;)
3218 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003219 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003220 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003221 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003222 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003223 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003224 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003225 if (len <= 0)
3226 break; /* error or nothing more to read */
3227
3228 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003229 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003230 readlen += len;
3231 if (len < MAXMSGSIZE)
3232 break; /* did read everything that's available */
3233 }
3234
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003235 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003236 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003237 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003238
3239#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003240 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003241 if (CH_HAS_GUI && gtk_main_level() > 0)
3242 gtk_main_quit();
3243#endif
3244}
3245
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003246/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003247 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003248 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003249 * Returns what was read in allocated memory.
3250 * Returns NULL in case of error or timeout.
3251 */
3252 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003253channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003254{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003255 char_u *buf;
3256 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003257 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003258 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003259 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003260 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003261
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003262 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003263 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003264
3265 while (TRUE)
3266 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003267 node = channel_peek(channel, part);
3268 if (node != NULL)
3269 {
3270 if (mode == MODE_RAW || (mode == MODE_NL
3271 && channel_first_nl(node) != NULL))
3272 /* got a complete message */
3273 break;
3274 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3275 continue;
3276 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003277
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003278 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003279 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003280 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003281 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003282 {
3283 ch_log(channel, "Timed out");
3284 return NULL;
3285 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003286 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003287 }
3288
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003289 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003290 if (mode == MODE_RAW)
3291 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003292 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003293 }
3294 else
3295 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003296 char_u *p;
3297
3298 buf = node->rq_buffer;
3299 nl = channel_first_nl(node);
3300
3301 /* Convert NUL to NL, the internal representation. */
3302 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3303 if (*p == NUL)
3304 *p = NL;
3305
3306 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003307 {
3308 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003309 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003310 *nl = NUL;
3311 }
3312 else
3313 {
3314 /* Copy the message into allocated memory and remove it from the
3315 * buffer. */
3316 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003317 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003318 }
3319 }
3320 if (log_fd != NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003321 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003322 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003323}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003324
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003325/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003326 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003327 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003328 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003329 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003330 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003331 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003332channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003333 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003334 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003335 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003336 int id,
3337 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003338{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003339 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003340 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003341 int timeout;
3342 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003343
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003344 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003345 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003346 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003347 for (;;)
3348 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003349 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003350
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003351 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003352 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003353 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003354 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003355 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003356 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003357
Bram Moolenaard7ece102016-02-02 23:23:02 +01003358 if (!more)
3359 {
3360 /* Handle any other messages in the queue. If done some more
3361 * messages may have arrived. */
3362 if (channel_parse_messages())
3363 continue;
3364
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003365 /* Wait for up to the timeout. If there was an incomplete message
3366 * use the deadline for that. */
3367 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003368 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003369 {
3370#ifdef WIN32
3371 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3372#else
3373 {
3374 struct timeval now_tv;
3375
3376 gettimeofday(&now_tv, NULL);
3377 timeout = (chanpart->ch_deadline.tv_sec
3378 - now_tv.tv_sec) * 1000
3379 + (chanpart->ch_deadline.tv_usec
3380 - now_tv.tv_usec) / 1000
3381 + 1;
3382 }
3383#endif
3384 if (timeout < 0)
3385 {
3386 /* Something went wrong, channel_parse_json() didn't
3387 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003388 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003389 timeout = timeout_arg;
3390 }
3391 else if (timeout > timeout_arg)
3392 timeout = timeout_arg;
3393 }
3394 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003395 if (fd == INVALID_FD
3396 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003397 {
3398 if (timeout == timeout_arg)
3399 {
3400 if (fd != INVALID_FD)
3401 ch_log(channel, "Timed out");
3402 break;
3403 }
3404 }
3405 else
3406 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003407 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003408 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003409 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003410 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003411}
3412
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003413/*
3414 * Common for ch_read() and ch_readraw().
3415 */
3416 void
3417common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3418{
3419 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003420 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003421 jobopt_T opt;
3422 int mode;
3423 int timeout;
3424 int id = -1;
3425 typval_T *listtv = NULL;
3426
3427 /* return an empty string by default */
3428 rettv->v_type = VAR_STRING;
3429 rettv->vval.v_string = NULL;
3430
3431 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003432 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003433 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003434 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003435
Bram Moolenaar437905c2016-04-26 19:01:05 +02003436 if (opt.jo_set & JO_PART)
3437 part = opt.jo_part;
3438 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003439 if (channel != NULL)
3440 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003441 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003442 part = channel_part_read(channel);
3443 mode = channel_get_mode(channel, part);
3444 timeout = channel_get_timeout(channel, part);
3445 if (opt.jo_set & JO_TIMEOUT)
3446 timeout = opt.jo_timeout;
3447
3448 if (raw || mode == MODE_RAW || mode == MODE_NL)
3449 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3450 else
3451 {
3452 if (opt.jo_set & JO_ID)
3453 id = opt.jo_id;
3454 channel_read_json_block(channel, part, timeout, id, &listtv);
3455 if (listtv != NULL)
3456 {
3457 *rettv = *listtv;
3458 vim_free(listtv);
3459 }
3460 else
3461 {
3462 rettv->v_type = VAR_SPECIAL;
3463 rettv->vval.v_number = VVAL_NONE;
3464 }
3465 }
3466 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003467
3468theend:
3469 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003470}
3471
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003472# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3473 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003474/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003475 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003476 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003477 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003478 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003479channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003480{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003481 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003482 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003483
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003484 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003485 for (channel = first_channel; channel != NULL;
3486 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003487 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003488 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003489 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003490 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003491 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003492 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003493 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003494 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003495 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003496}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003497# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003498
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003499# if defined(WIN32) || defined(PROTO)
3500/*
3501 * Check the channels for anything that is ready to be read.
3502 * The data is put in the read queue.
3503 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003504 void
3505channel_handle_events(void)
3506{
3507 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003508 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003509 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003510
3511 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3512 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003513 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003514 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003515 {
3516 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003517 if (fd != INVALID_FD)
3518 {
3519 int r = channel_wait(channel, fd, 0);
3520
3521 if (r == CW_READY)
3522 channel_read(channel, part, "channel_handle_events");
3523 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003524 ch_close_part_on_error(channel, part, TRUE,
3525 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003526 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003527 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003528 }
3529}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003530# endif
3531
Bram Moolenaard04a0202016-01-26 23:30:18 +01003532/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003533 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003534 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003535 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003536 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003537 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003538channel_send(
3539 channel_T *channel,
3540 ch_part_T part,
3541 char_u *buf,
3542 int len,
3543 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003544{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003545 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003546 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003547
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003548 fd = channel->ch_part[part].ch_fd;
3549 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003550 {
3551 if (!channel->ch_error && fun != NULL)
3552 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003553 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003554 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003555 }
3556 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003557 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003558 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003559
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003560 if (log_fd != NULL)
3561 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003562 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003563 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003564 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003565 fprintf(log_fd, "'\n");
3566 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003567 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003568 }
3569
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003570 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003571 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003572 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003573 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003574 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003575 {
3576 if (!channel->ch_error && fun != NULL)
3577 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003578 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003579 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003580 }
3581 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003582 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003583 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003584
3585 channel->ch_error = FALSE;
3586 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003587}
3588
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003589/*
3590 * Common for "ch_sendexpr()" and "ch_sendraw()".
3591 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003592 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003593 * Otherwise returns NULL.
3594 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003595 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003596send_common(
3597 typval_T *argvars,
3598 char_u *text,
3599 int id,
3600 int eval,
3601 jobopt_T *opt,
3602 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003603 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003604{
3605 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003606 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003607
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003608 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003609 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003610 if (channel == NULL)
3611 return NULL;
3612 part_send = channel_part_send(channel);
3613 *part_read = channel_part_read(channel);
3614
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003615 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003616 return NULL;
3617
3618 /* Set the callback. An empty callback means no callback and not reading
3619 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3620 * allowed. */
3621 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3622 {
3623 if (eval)
3624 {
3625 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3626 return NULL;
3627 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003628 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003629 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003630 }
3631
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003632 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003633 && opt->jo_callback == NULL)
3634 return channel;
3635 return NULL;
3636}
3637
3638/*
3639 * common for "ch_evalexpr()" and "ch_sendexpr()"
3640 */
3641 void
3642ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3643{
3644 char_u *text;
3645 typval_T *listtv;
3646 channel_T *channel;
3647 int id;
3648 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003649 ch_part_T part_send;
3650 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003651 jobopt_T opt;
3652 int timeout;
3653
3654 /* return an empty string by default */
3655 rettv->v_type = VAR_STRING;
3656 rettv->vval.v_string = NULL;
3657
Bram Moolenaar437905c2016-04-26 19:01:05 +02003658 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003659 if (channel == NULL)
3660 return;
3661 part_send = channel_part_send(channel);
3662
3663 ch_mode = channel_get_mode(channel, part_send);
3664 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3665 {
3666 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3667 return;
3668 }
3669
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003670 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003671 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003672 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003673 if (text == NULL)
3674 return;
3675
3676 channel = send_common(argvars, text, id, eval, &opt,
3677 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3678 vim_free(text);
3679 if (channel != NULL && eval)
3680 {
3681 if (opt.jo_set & JO_TIMEOUT)
3682 timeout = opt.jo_timeout;
3683 else
3684 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003685 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3686 == OK)
3687 {
3688 list_T *list = listtv->vval.v_list;
3689
3690 /* Move the item from the list and then change the type to
3691 * avoid the value being freed. */
3692 *rettv = list->lv_last->li_tv;
3693 list->lv_last->li_tv.v_type = VAR_NUMBER;
3694 free_tv(listtv);
3695 }
3696 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003697 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003698}
3699
3700/*
3701 * common for "ch_evalraw()" and "ch_sendraw()"
3702 */
3703 void
3704ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3705{
3706 char_u buf[NUMBUFLEN];
3707 char_u *text;
3708 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003709 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003710 jobopt_T opt;
3711 int timeout;
3712
3713 /* return an empty string by default */
3714 rettv->v_type = VAR_STRING;
3715 rettv->vval.v_string = NULL;
3716
3717 text = get_tv_string_buf(&argvars[1], buf);
3718 channel = send_common(argvars, text, 0, eval, &opt,
3719 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3720 if (channel != NULL && eval)
3721 {
3722 if (opt.jo_set & JO_TIMEOUT)
3723 timeout = opt.jo_timeout;
3724 else
3725 timeout = channel_get_timeout(channel, part_read);
3726 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3727 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003728 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003729}
3730
Bram Moolenaard04a0202016-01-26 23:30:18 +01003731# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003732/*
3733 * Add open channels to the poll struct.
3734 * Return the adjusted struct index.
3735 * The type of "fds" is hidden to avoid problems with the function proto.
3736 */
3737 int
3738channel_poll_setup(int nfd_in, void *fds_in)
3739{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003740 int nfd = nfd_in;
3741 channel_T *channel;
3742 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003743 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003744
Bram Moolenaar77073442016-02-13 23:23:53 +01003745 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003746 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003747 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003748 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003749 chanpart_T *ch_part = &channel->ch_part[part];
3750
3751 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003752 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003753 ch_part->ch_poll_idx = nfd;
3754 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003755 fds[nfd].events = POLLIN;
3756 nfd++;
3757 }
3758 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003759 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003760 }
3761 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003762
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003763 nfd = channel_fill_poll_write(nfd, fds);
3764
Bram Moolenaare0874f82016-01-24 20:36:41 +01003765 return nfd;
3766}
3767
3768/*
3769 * The type of "fds" is hidden to avoid problems with the function proto.
3770 */
3771 int
3772channel_poll_check(int ret_in, void *fds_in)
3773{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003774 int ret = ret_in;
3775 channel_T *channel;
3776 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003777 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003778 int idx;
3779 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003780
Bram Moolenaar77073442016-02-13 23:23:53 +01003781 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003782 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003783 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003784 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003785 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003786
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003787 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003788 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003789 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003790 --ret;
3791 }
3792 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003793
3794 in_part = &channel->ch_part[PART_IN];
3795 idx = in_part->ch_poll_idx;
3796 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3797 {
3798 if (in_part->ch_buf_append)
3799 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003800 if (in_part->ch_bufref.br_buf != NULL)
3801 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003802 }
3803 else
3804 channel_write_in(channel);
3805 --ret;
3806 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003807 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003808
3809 return ret;
3810}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003811# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003812
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003813# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003814/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003815 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003816 */
3817 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003818channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003819{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003820 int maxfd = maxfd_in;
3821 channel_T *channel;
3822 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003823 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003824 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003825
Bram Moolenaar77073442016-02-13 23:23:53 +01003826 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003827 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003828 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003829 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003830 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003831
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003832 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003833 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003834 FD_SET((int)fd, rfds);
3835 if (maxfd < (int)fd)
3836 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003837 }
3838 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003839 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003840
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003841 maxfd = channel_fill_wfds(maxfd, wfds);
3842
Bram Moolenaare0874f82016-01-24 20:36:41 +01003843 return maxfd;
3844}
3845
3846/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003847 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003848 */
3849 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003850channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003851{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003852 int ret = ret_in;
3853 channel_T *channel;
3854 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003855 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003856 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003857 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003858
Bram Moolenaar77073442016-02-13 23:23:53 +01003859 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003860 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003861 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003862 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003863 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003864
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003865 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003866 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003867 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003868 --ret;
3869 }
3870 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003871
3872 in_part = &channel->ch_part[PART_IN];
3873 if (ret > 0 && in_part->ch_fd != INVALID_FD
3874 && FD_ISSET(in_part->ch_fd, wfds))
3875 {
3876 if (in_part->ch_buf_append)
3877 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003878 if (in_part->ch_bufref.br_buf != NULL)
3879 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003880 }
3881 else
3882 channel_write_in(channel);
3883 --ret;
3884 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003885 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003886
3887 return ret;
3888}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003889# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003890
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003891/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003892 * Execute queued up commands.
3893 * Invoked from the main loop when it's safe to execute received commands.
3894 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003895 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003896 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003897channel_parse_messages(void)
3898{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003899 channel_T *channel = first_channel;
3900 int ret = FALSE;
3901 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003902 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003903#ifdef ELAPSED_FUNC
3904 ELAPSED_TYPE start_tv;
3905
3906 ELAPSED_INIT(start_tv);
3907#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003908
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003909 ++safe_to_invoke_callback;
3910
Bram Moolenaard0b65022016-03-06 21:50:33 +01003911 /* Only do this message when another message was given, otherwise we get
3912 * lots of them. */
3913 if (did_log_msg)
3914 {
3915 ch_log(NULL, "looking for messages on channels");
3916 did_log_msg = FALSE;
3917 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003918 while (channel != NULL)
3919 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003920 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003921 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003922 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003923 channel_close_now(channel);
3924 /* channel may have been freed, start over */
3925 channel = first_channel;
3926 continue;
3927 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003928 if (channel->ch_to_be_freed)
3929 {
3930 channel_free(channel);
3931 /* channel has been freed, start over */
3932 channel = first_channel;
3933 continue;
3934 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003935 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003936 {
3937 /* channel is no longer useful, free it */
3938 channel_free(channel);
3939 channel = first_channel;
3940 part = PART_SOCK;
3941 continue;
3942 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003943 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003944 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003945 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003946 /* Increase the refcount, in case the handler causes the channel
3947 * to be unreferenced or closed. */
3948 ++channel->ch_refcount;
3949 r = may_invoke_callback(channel, part);
3950 if (r == OK)
3951 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003952 if (channel_unref(channel) || (r == OK
3953#ifdef ELAPSED_FUNC
3954 /* Limit the time we loop here to 100 msec, otherwise
3955 * Vim becomes unresponsive when the callback takes
3956 * more than a bit of time. */
3957 && ELAPSED_FUNC(start_tv) < 100L
3958#endif
3959 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003960 {
3961 /* channel was freed or something was done, start over */
3962 channel = first_channel;
3963 part = PART_SOCK;
3964 continue;
3965 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003966 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003967 if (part < PART_ERR)
3968 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003969 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003970 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003971 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003972 part = PART_SOCK;
3973 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003974 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003975
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003976 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003977 {
3978 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003979 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003980 }
3981
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003982 --safe_to_invoke_callback;
3983
Bram Moolenaard7ece102016-02-02 23:23:02 +01003984 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003985}
3986
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003987/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01003988 * Return TRUE if any channel has readahead. That means we should not block on
3989 * waiting for input.
3990 */
3991 int
3992channel_any_readahead(void)
3993{
3994 channel_T *channel = first_channel;
3995 ch_part_T part = PART_SOCK;
3996
3997 while (channel != NULL)
3998 {
3999 if (channel_has_readahead(channel, part))
4000 return TRUE;
4001 if (part < PART_ERR)
4002 ++part;
4003 else
4004 {
4005 channel = channel->ch_next;
4006 part = PART_SOCK;
4007 }
4008 }
4009 return FALSE;
4010}
4011
4012/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004013 * Mark references to lists used in channels.
4014 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004015 int
4016set_ref_in_channel(int copyID)
4017{
Bram Moolenaar77073442016-02-13 23:23:53 +01004018 int abort = FALSE;
4019 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004020 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004021
Bram Moolenaar77073442016-02-13 23:23:53 +01004022 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004023 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004024 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004025 tv.v_type = VAR_CHANNEL;
4026 tv.vval.v_channel = channel;
4027 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004028 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004029 return abort;
4030}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004031
4032/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004033 * Return the "part" to write to for "channel".
4034 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004035 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004036channel_part_send(channel_T *channel)
4037{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004038 if (channel->CH_SOCK_FD == INVALID_FD)
4039 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004040 return PART_SOCK;
4041}
4042
4043/*
4044 * Return the default "part" to read from for "channel".
4045 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004046 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004047channel_part_read(channel_T *channel)
4048{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004049 if (channel->CH_SOCK_FD == INVALID_FD)
4050 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004051 return PART_SOCK;
4052}
4053
4054/*
4055 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004056 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004057 */
4058 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004059channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004060{
Bram Moolenaar77073442016-02-13 23:23:53 +01004061 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004062 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004063 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004064}
4065
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004066/*
4067 * Return the timeout of "channel"/"part"
4068 */
4069 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004070channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004071{
4072 return channel->ch_part[part].ch_timeout;
4073}
4074
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004075 static int
4076handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4077{
4078 char_u *val = get_tv_string(item);
4079
4080 opt->jo_set |= jo;
4081 if (STRCMP(val, "nl") == 0)
4082 *modep = MODE_NL;
4083 else if (STRCMP(val, "raw") == 0)
4084 *modep = MODE_RAW;
4085 else if (STRCMP(val, "js") == 0)
4086 *modep = MODE_JS;
4087 else if (STRCMP(val, "json") == 0)
4088 *modep = MODE_JSON;
4089 else
4090 {
4091 EMSG2(_(e_invarg2), val);
4092 return FAIL;
4093 }
4094 return OK;
4095}
4096
4097 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004098handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004099{
4100 char_u *val = get_tv_string(item);
4101
4102 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4103 if (STRCMP(val, "null") == 0)
4104 opt->jo_io[part] = JIO_NULL;
4105 else if (STRCMP(val, "pipe") == 0)
4106 opt->jo_io[part] = JIO_PIPE;
4107 else if (STRCMP(val, "file") == 0)
4108 opt->jo_io[part] = JIO_FILE;
4109 else if (STRCMP(val, "buffer") == 0)
4110 opt->jo_io[part] = JIO_BUFFER;
4111 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4112 opt->jo_io[part] = JIO_OUT;
4113 else
4114 {
4115 EMSG2(_(e_invarg2), val);
4116 return FAIL;
4117 }
4118 return OK;
4119}
4120
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004121/*
4122 * Clear a jobopt_T before using it.
4123 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004124 void
4125clear_job_options(jobopt_T *opt)
4126{
4127 vim_memset(opt, 0, sizeof(jobopt_T));
4128}
4129
4130/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004131 * Free any members of a jobopt_T.
4132 */
4133 void
4134free_job_options(jobopt_T *opt)
4135{
4136 if (opt->jo_partial != NULL)
4137 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 else if (opt->jo_callback != NULL)
4139 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004140 if (opt->jo_out_partial != NULL)
4141 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 else if (opt->jo_out_cb != NULL)
4143 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004144 if (opt->jo_err_partial != NULL)
4145 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004146 else if (opt->jo_err_cb != NULL)
4147 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004148 if (opt->jo_close_partial != NULL)
4149 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150 else if (opt->jo_close_cb != NULL)
4151 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004152 if (opt->jo_exit_partial != NULL)
4153 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154 else if (opt->jo_exit_cb != NULL)
4155 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004156 if (opt->jo_env != NULL)
4157 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004158}
4159
4160/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004161 * Get the PART_ number from the first character of an option name.
4162 */
4163 static int
4164part_from_char(int c)
4165{
4166 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4167}
4168
4169/*
4170 * Get the option entries from the dict in "tv", parse them and put the result
4171 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004172 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004173 * If an option value is invalid return FAIL.
4174 */
4175 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004176get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004177{
4178 typval_T *item;
4179 char_u *val;
4180 dict_T *dict;
4181 int todo;
4182 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004183 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004184
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004185 if (tv->v_type == VAR_UNKNOWN)
4186 return OK;
4187 if (tv->v_type != VAR_DICT)
4188 {
4189 EMSG(_(e_invarg));
4190 return FAIL;
4191 }
4192 dict = tv->vval.v_dict;
4193 if (dict == NULL)
4194 return OK;
4195
4196 todo = (int)dict->dv_hashtab.ht_used;
4197 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4198 if (!HASHITEM_EMPTY(hi))
4199 {
4200 item = &dict_lookup(hi)->di_tv;
4201
4202 if (STRCMP(hi->hi_key, "mode") == 0)
4203 {
4204 if (!(supported & JO_MODE))
4205 break;
4206 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4207 return FAIL;
4208 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004209 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004210 {
4211 if (!(supported & JO_IN_MODE))
4212 break;
4213 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4214 == FAIL)
4215 return FAIL;
4216 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004217 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004218 {
4219 if (!(supported & JO_OUT_MODE))
4220 break;
4221 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4222 == FAIL)
4223 return FAIL;
4224 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004225 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004226 {
4227 if (!(supported & JO_ERR_MODE))
4228 break;
4229 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4230 == FAIL)
4231 return FAIL;
4232 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004233 else if (STRCMP(hi->hi_key, "in_io") == 0
4234 || STRCMP(hi->hi_key, "out_io") == 0
4235 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004236 {
4237 if (!(supported & JO_OUT_IO))
4238 break;
4239 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4240 return FAIL;
4241 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004242 else if (STRCMP(hi->hi_key, "in_name") == 0
4243 || STRCMP(hi->hi_key, "out_name") == 0
4244 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004245 {
4246 part = part_from_char(*hi->hi_key);
4247
4248 if (!(supported & JO_OUT_IO))
4249 break;
4250 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4251 opt->jo_io_name[part] =
4252 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4253 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004254 else if (STRCMP(hi->hi_key, "pty") == 0)
4255 {
4256 if (!(supported & JO_MODE))
4257 break;
4258 opt->jo_pty = get_tv_number(item);
4259 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004260 else if (STRCMP(hi->hi_key, "in_buf") == 0
4261 || STRCMP(hi->hi_key, "out_buf") == 0
4262 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004263 {
4264 part = part_from_char(*hi->hi_key);
4265
4266 if (!(supported & JO_OUT_IO))
4267 break;
4268 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4269 opt->jo_io_buf[part] = get_tv_number(item);
4270 if (opt->jo_io_buf[part] <= 0)
4271 {
4272 EMSG2(_(e_invarg2), get_tv_string(item));
4273 return FAIL;
4274 }
4275 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4276 {
4277 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4278 return FAIL;
4279 }
4280 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004281 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4282 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4283 {
4284 part = part_from_char(*hi->hi_key);
4285
4286 if (!(supported & JO_OUT_IO))
4287 break;
4288 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4289 opt->jo_modifiable[part] = get_tv_number(item);
4290 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004291 else if (STRCMP(hi->hi_key, "out_msg") == 0
4292 || STRCMP(hi->hi_key, "err_msg") == 0)
4293 {
4294 part = part_from_char(*hi->hi_key);
4295
4296 if (!(supported & JO_OUT_IO))
4297 break;
4298 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4299 opt->jo_message[part] = get_tv_number(item);
4300 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004301 else if (STRCMP(hi->hi_key, "in_top") == 0
4302 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004303 {
4304 linenr_T *lp;
4305
4306 if (!(supported & JO_OUT_IO))
4307 break;
4308 if (hi->hi_key[3] == 't')
4309 {
4310 lp = &opt->jo_in_top;
4311 opt->jo_set |= JO_IN_TOP;
4312 }
4313 else
4314 {
4315 lp = &opt->jo_in_bot;
4316 opt->jo_set |= JO_IN_BOT;
4317 }
4318 *lp = get_tv_number(item);
4319 if (*lp < 0)
4320 {
4321 EMSG2(_(e_invarg2), get_tv_string(item));
4322 return FAIL;
4323 }
4324 }
4325 else if (STRCMP(hi->hi_key, "channel") == 0)
4326 {
4327 if (!(supported & JO_OUT_IO))
4328 break;
4329 opt->jo_set |= JO_CHANNEL;
4330 if (item->v_type != VAR_CHANNEL)
4331 {
4332 EMSG2(_(e_invarg2), "channel");
4333 return FAIL;
4334 }
4335 opt->jo_channel = item->vval.v_channel;
4336 }
4337 else if (STRCMP(hi->hi_key, "callback") == 0)
4338 {
4339 if (!(supported & JO_CALLBACK))
4340 break;
4341 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004342 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004343 if (opt->jo_callback == NULL)
4344 {
4345 EMSG2(_(e_invarg2), "callback");
4346 return FAIL;
4347 }
4348 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004349 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004350 {
4351 if (!(supported & JO_OUT_CALLBACK))
4352 break;
4353 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004354 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004355 if (opt->jo_out_cb == NULL)
4356 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004357 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 return FAIL;
4359 }
4360 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004361 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004362 {
4363 if (!(supported & JO_ERR_CALLBACK))
4364 break;
4365 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004366 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004367 if (opt->jo_err_cb == NULL)
4368 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004369 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004370 return FAIL;
4371 }
4372 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004373 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004374 {
4375 if (!(supported & JO_CLOSE_CALLBACK))
4376 break;
4377 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004378 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004379 if (opt->jo_close_cb == NULL)
4380 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004381 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004382 return FAIL;
4383 }
4384 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004385 else if (STRCMP(hi->hi_key, "drop") == 0)
4386 {
4387 int never = FALSE;
4388 val = get_tv_string(item);
4389
4390 if (STRCMP(val, "never") == 0)
4391 never = TRUE;
4392 else if (STRCMP(val, "auto") != 0)
4393 {
4394 EMSG2(_(e_invarg2), "drop");
4395 return FAIL;
4396 }
4397 opt->jo_drop_never = never;
4398 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004399 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4400 {
4401 if (!(supported & JO_EXIT_CB))
4402 break;
4403 opt->jo_set |= JO_EXIT_CB;
4404 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4405 if (opt->jo_exit_cb == NULL)
4406 {
4407 EMSG2(_(e_invarg2), "exit_cb");
4408 return FAIL;
4409 }
4410 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004411#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004412 else if (STRCMP(hi->hi_key, "term_name") == 0)
4413 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004414 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004415 break;
4416 opt->jo_set2 |= JO2_TERM_NAME;
4417 opt->jo_term_name = get_tv_string_chk(item);
4418 if (opt->jo_term_name == NULL)
4419 {
4420 EMSG2(_(e_invarg2), "term_name");
4421 return FAIL;
4422 }
4423 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004424 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4425 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004426 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004427 break;
4428 val = get_tv_string(item);
4429 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4430 {
Bram Moolenaarf1237f12017-08-11 15:45:28 +02004431 EMSG2(_(e_invarg2), val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004432 return FAIL;
4433 }
4434 opt->jo_set2 |= JO2_TERM_FINISH;
4435 opt->jo_term_finish = *val;
4436 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004437 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4438 {
4439 if (!(supported2 & JO2_TERM_ROWS))
4440 break;
4441 opt->jo_set |= JO2_TERM_ROWS;
4442 opt->jo_term_rows = get_tv_number(item);
4443 }
4444 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4445 {
4446 if (!(supported2 & JO2_TERM_COLS))
4447 break;
4448 opt->jo_set |= JO2_TERM_COLS;
4449 opt->jo_term_cols = get_tv_number(item);
4450 }
4451 else if (STRCMP(hi->hi_key, "vertical") == 0)
4452 {
4453 if (!(supported2 & JO2_VERTICAL))
4454 break;
4455 opt->jo_set |= JO2_VERTICAL;
4456 opt->jo_vertical = get_tv_number(item);
4457 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004458 else if (STRCMP(hi->hi_key, "curwin") == 0)
4459 {
4460 if (!(supported2 & JO2_CURWIN))
4461 break;
4462 opt->jo_set |= JO2_CURWIN;
4463 opt->jo_curwin = get_tv_number(item);
4464 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004465#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004466 else if (STRCMP(hi->hi_key, "env") == 0)
4467 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004468 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004469 break;
4470 opt->jo_set |= JO2_ENV;
4471 opt->jo_env = item->vval.v_dict;
4472 ++item->vval.v_dict->dv_refcount;
4473 }
4474 else if (STRCMP(hi->hi_key, "cwd") == 0)
4475 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004476 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004477 break;
4478 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4479 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4480 {
4481 EMSG2(_(e_invarg2), "cwd");
4482 return FAIL;
4483 }
4484 opt->jo_set |= JO2_CWD;
4485 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004486 else if (STRCMP(hi->hi_key, "waittime") == 0)
4487 {
4488 if (!(supported & JO_WAITTIME))
4489 break;
4490 opt->jo_set |= JO_WAITTIME;
4491 opt->jo_waittime = get_tv_number(item);
4492 }
4493 else if (STRCMP(hi->hi_key, "timeout") == 0)
4494 {
4495 if (!(supported & JO_TIMEOUT))
4496 break;
4497 opt->jo_set |= JO_TIMEOUT;
4498 opt->jo_timeout = get_tv_number(item);
4499 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004500 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004501 {
4502 if (!(supported & JO_OUT_TIMEOUT))
4503 break;
4504 opt->jo_set |= JO_OUT_TIMEOUT;
4505 opt->jo_out_timeout = get_tv_number(item);
4506 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004507 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004508 {
4509 if (!(supported & JO_ERR_TIMEOUT))
4510 break;
4511 opt->jo_set |= JO_ERR_TIMEOUT;
4512 opt->jo_err_timeout = get_tv_number(item);
4513 }
4514 else if (STRCMP(hi->hi_key, "part") == 0)
4515 {
4516 if (!(supported & JO_PART))
4517 break;
4518 opt->jo_set |= JO_PART;
4519 val = get_tv_string(item);
4520 if (STRCMP(val, "err") == 0)
4521 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004522 else if (STRCMP(val, "out") == 0)
4523 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004524 else
4525 {
4526 EMSG2(_(e_invarg2), val);
4527 return FAIL;
4528 }
4529 }
4530 else if (STRCMP(hi->hi_key, "id") == 0)
4531 {
4532 if (!(supported & JO_ID))
4533 break;
4534 opt->jo_set |= JO_ID;
4535 opt->jo_id = get_tv_number(item);
4536 }
4537 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4538 {
4539 if (!(supported & JO_STOPONEXIT))
4540 break;
4541 opt->jo_set |= JO_STOPONEXIT;
4542 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4543 opt->jo_soe_buf);
4544 if (opt->jo_stoponexit == NULL)
4545 {
4546 EMSG2(_(e_invarg2), "stoponexit");
4547 return FAIL;
4548 }
4549 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004550 else if (STRCMP(hi->hi_key, "block_write") == 0)
4551 {
4552 if (!(supported & JO_BLOCK_WRITE))
4553 break;
4554 opt->jo_set |= JO_BLOCK_WRITE;
4555 opt->jo_block_write = get_tv_number(item);
4556 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004557 else
4558 break;
4559 --todo;
4560 }
4561 if (todo > 0)
4562 {
4563 EMSG2(_(e_invarg2), hi->hi_key);
4564 return FAIL;
4565 }
4566
4567 return OK;
4568}
4569
4570/*
4571 * Get the channel from the argument.
4572 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004573 * When "check_open" is TRUE check that the channel can be used.
4574 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004575 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004576 */
4577 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004578get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004579{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004580 channel_T *channel = NULL;
4581 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004582
4583 if (tv->v_type == VAR_JOB)
4584 {
4585 if (tv->vval.v_job != NULL)
4586 channel = tv->vval.v_job->jv_channel;
4587 }
4588 else if (tv->v_type == VAR_CHANNEL)
4589 {
4590 channel = tv->vval.v_channel;
4591 }
4592 else
4593 {
4594 EMSG2(_(e_invarg2), get_tv_string(tv));
4595 return NULL;
4596 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004597 if (channel != NULL && reading)
4598 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004599 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004600
Bram Moolenaar437905c2016-04-26 19:01:05 +02004601 if (check_open && (channel == NULL || (!channel_is_open(channel)
4602 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004603 {
4604 EMSG(_("E906: not an open channel"));
4605 return NULL;
4606 }
4607 return channel;
4608}
4609
4610static job_T *first_job = NULL;
4611
4612 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004613job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004614{
4615 ch_log(job->jv_channel, "Freeing job");
4616 if (job->jv_channel != NULL)
4617 {
4618 /* The link from the channel to the job doesn't count as a reference,
4619 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004620 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004621 * NULL the reference. We don't set ch_job_killed, unreferencing the
4622 * job doesn't mean it stops running. */
4623 job->jv_channel->ch_job = NULL;
4624 channel_unref(job->jv_channel);
4625 }
4626 mch_clear_job(job);
4627
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004628 vim_free(job->jv_tty_name);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004629 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004630 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004631}
4632
4633 static void
4634job_free_job(job_T *job)
4635{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004636 if (job->jv_next != NULL)
4637 job->jv_next->jv_prev = job->jv_prev;
4638 if (job->jv_prev == NULL)
4639 first_job = job->jv_next;
4640 else
4641 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004642 vim_free(job);
4643}
4644
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004645 static void
4646job_free(job_T *job)
4647{
4648 if (!in_free_unref_items)
4649 {
4650 job_free_contents(job);
4651 job_free_job(job);
4652 }
4653}
4654
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004655#if defined(EXITFREE) || defined(PROTO)
4656 void
4657job_free_all(void)
4658{
4659 while (first_job != NULL)
4660 job_free(first_job);
4661}
4662#endif
4663
4664/*
4665 * Return TRUE if we need to check if the process of "job" has ended.
4666 */
4667 static int
4668job_need_end_check(job_T *job)
4669{
4670 return job->jv_status == JOB_STARTED
4671 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4672}
4673
4674/*
4675 * Return TRUE if the channel of "job" is still useful.
4676 */
4677 static int
4678job_channel_still_useful(job_T *job)
4679{
4680 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4681}
4682
4683/*
4684 * Return TRUE if the job should not be freed yet. Do not free the job when
4685 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4686 * or when the associated channel will do something with the job output.
4687 */
4688 static int
4689job_still_useful(job_T *job)
4690{
4691 return job_need_end_check(job) || job_channel_still_useful(job);
4692}
4693
4694/*
4695 * NOTE: Must call job_cleanup() only once right after the status of "job"
4696 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4697 * mch_detect_ended_job() returned non-NULL).
4698 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004699 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02004700job_cleanup(job_T *job)
4701{
4702 if (job->jv_status != JOB_ENDED)
4703 return;
4704
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004705 /* Ready to cleanup the job. */
4706 job->jv_status = JOB_FINISHED;
4707
Bram Moolenaar97792de2016-10-15 18:36:49 +02004708 if (job->jv_exit_cb != NULL)
4709 {
4710 typval_T argv[3];
4711 typval_T rettv;
4712 int dummy;
4713
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004714 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02004715 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02004716 ++job->jv_refcount;
4717 argv[0].v_type = VAR_JOB;
4718 argv[0].vval.v_job = job;
4719 argv[1].v_type = VAR_NUMBER;
4720 argv[1].vval.v_number = job->jv_exitval;
4721 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4722 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4723 job->jv_exit_partial, NULL);
4724 clear_tv(&rettv);
4725 --job->jv_refcount;
4726 channel_need_redraw = TRUE;
4727 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004728
4729 /* Do not free the job in case the close callback of the associated channel
4730 * isn't invoked yet and may get information by job_info(). */
4731 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004732 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004733 /* The job was already unreferenced and the associated channel was
4734 * detached, now that it ended it can be freed. Careful: caller must
4735 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004736 job_free(job);
4737 }
4738}
4739
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004740/*
4741 * Mark references in jobs that are still useful.
4742 */
4743 int
4744set_ref_in_job(int copyID)
4745{
4746 int abort = FALSE;
4747 job_T *job;
4748 typval_T tv;
4749
4750 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004751 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004752 {
4753 tv.v_type = VAR_JOB;
4754 tv.vval.v_job = job;
4755 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4756 }
4757 return abort;
4758}
4759
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004760/*
4761 * Dereference "job". Note that after this "job" may have been freed.
4762 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 void
4764job_unref(job_T *job)
4765{
4766 if (job != NULL && --job->jv_refcount <= 0)
4767 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004768 /* Do not free the job if there is a channel where the close callback
4769 * may get the job info. */
4770 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004771 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004772 /* Do not free the job when it has not ended yet and there is a
4773 * "stoponexit" flag or an exit callback. */
4774 if (!job_need_end_check(job))
4775 {
4776 job_free(job);
4777 }
4778 else if (job->jv_channel != NULL)
4779 {
4780 /* Do remove the link to the channel, otherwise it hangs
4781 * around until Vim exits. See job_free() for refcount. */
4782 ch_log(job->jv_channel, "detaching channel from job");
4783 job->jv_channel->ch_job = NULL;
4784 channel_unref(job->jv_channel);
4785 job->jv_channel = NULL;
4786 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004787 }
4788 }
4789}
4790
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791 int
4792free_unused_jobs_contents(int copyID, int mask)
4793{
4794 int did_free = FALSE;
4795 job_T *job;
4796
4797 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004798 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004799 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004800 {
4801 /* Free the channel and ordinary items it contains, but don't
4802 * recurse into Lists, Dictionaries etc. */
4803 job_free_contents(job);
4804 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004805 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004806 return did_free;
4807}
4808
4809 void
4810free_unused_jobs(int copyID, int mask)
4811{
4812 job_T *job;
4813 job_T *job_next;
4814
4815 for (job = first_job; job != NULL; job = job_next)
4816 {
4817 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004818 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004819 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004820 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004821 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004822 job_free_job(job);
4823 }
4824 }
4825}
4826
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004827/*
4828 * Allocate a job. Sets the refcount to one and sets options default.
4829 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004830 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004831job_alloc(void)
4832{
4833 job_T *job;
4834
4835 job = (job_T *)alloc_clear(sizeof(job_T));
4836 if (job != NULL)
4837 {
4838 job->jv_refcount = 1;
4839 job->jv_stoponexit = vim_strsave((char_u *)"term");
4840
4841 if (first_job != NULL)
4842 {
4843 first_job->jv_prev = job;
4844 job->jv_next = first_job;
4845 }
4846 first_job = job;
4847 }
4848 return job;
4849}
4850
4851 void
4852job_set_options(job_T *job, jobopt_T *opt)
4853{
4854 if (opt->jo_set & JO_STOPONEXIT)
4855 {
4856 vim_free(job->jv_stoponexit);
4857 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4858 job->jv_stoponexit = NULL;
4859 else
4860 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4861 }
4862 if (opt->jo_set & JO_EXIT_CB)
4863 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004864 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004865 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004866 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004867 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004868 job->jv_exit_partial = NULL;
4869 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004870 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004871 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004872 job->jv_exit_partial = opt->jo_exit_partial;
4873 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004874 {
4875 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004876 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004877 }
4878 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004879 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004880 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004881 func_ref(job->jv_exit_cb);
4882 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004883 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004884 }
4885}
4886
4887/*
4888 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4889 */
4890 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004891job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004892{
4893 job_T *job;
4894
4895 for (job = first_job; job != NULL; job = job->jv_next)
4896 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02004897 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004898}
4899
4900/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004901 * Return TRUE when there is any job that has an exit callback and might exit,
4902 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004903 */
4904 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004905has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004906{
4907 job_T *job;
4908
4909 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004910 /* Only should check if the channel has been closed, if the channel is
4911 * open the job won't exit. */
4912 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004913 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004914 return TRUE;
4915 return FALSE;
4916}
4917
Bram Moolenaar97792de2016-10-15 18:36:49 +02004918#define MAX_CHECK_ENDED 8
4919
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004920/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004921 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004922 */
4923 void
4924job_check_ended(void)
4925{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004926 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004927
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004928 if (first_job == NULL)
4929 return;
4930
Bram Moolenaar97792de2016-10-15 18:36:49 +02004931 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004932 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004933 /* NOTE: mch_detect_ended_job() must only return a job of which the
4934 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004935 job_T *job = mch_detect_ended_job(first_job);
4936
4937 if (job == NULL)
4938 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004939 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004940 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004941
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004942 if (channel_need_redraw)
4943 {
4944 channel_need_redraw = FALSE;
4945 redraw_after_callback();
4946 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004947}
4948
4949/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02004950 * Create a job and return it. Implements job_start().
4951 * The returned job has a refcount of one.
4952 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004953 */
4954 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004955job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004956{
4957 job_T *job;
4958 char_u *cmd = NULL;
4959#if defined(UNIX)
4960# define USE_ARGV
4961 char **argv = NULL;
4962 int argc = 0;
4963#else
4964 garray_T ga;
4965#endif
4966 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004967 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004968
4969 job = job_alloc();
4970 if (job == NULL)
4971 return NULL;
4972
4973 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004974#ifndef USE_ARGV
4975 ga_init2(&ga, (int)sizeof(char*), 20);
4976#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004977
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004978 if (opt_arg != NULL)
4979 opt = *opt_arg;
4980 else
4981 {
4982 /* Default mode is NL. */
4983 clear_job_options(&opt);
4984 opt.jo_mode = MODE_NL;
4985 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02004986 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4987 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
4988 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02004989 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004990 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004991
4992 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004993 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004994 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4995 && opt.jo_io[part] == JIO_FILE
4996 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4997 || *opt.jo_io_name[part] == NUL))
4998 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004999 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005000 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005001 }
5002
5003 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5004 {
5005 buf_T *buf = NULL;
5006
5007 /* check that we can find the buffer before starting the job */
5008 if (opt.jo_set & JO_IN_BUF)
5009 {
5010 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5011 if (buf == NULL)
5012 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5013 }
5014 else if (!(opt.jo_set & JO_IN_NAME))
5015 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005016 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005017 }
5018 else
5019 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5020 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005021 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005022 if (buf->b_ml.ml_mfp == NULL)
5023 {
5024 char_u numbuf[NUMBUFLEN];
5025 char_u *s;
5026
5027 if (opt.jo_set & JO_IN_BUF)
5028 {
5029 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5030 s = numbuf;
5031 }
5032 else
5033 s = opt.jo_io_name[PART_IN];
5034 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005035 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005036 }
5037 job->jv_in_buf = buf;
5038 }
5039
5040 job_set_options(job, &opt);
5041
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005042 if (argvars[0].v_type == VAR_STRING)
5043 {
5044 /* Command is a string. */
5045 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005046 if (cmd == NULL || *cmd == NUL)
5047 {
5048 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005049 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005050 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005051#ifdef USE_ARGV
5052 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005053 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005054 argv[argc] = NULL;
5055#endif
5056 }
5057 else if (argvars[0].v_type != VAR_LIST
5058 || argvars[0].vval.v_list == NULL
5059 || argvars[0].vval.v_list->lv_len < 1)
5060 {
5061 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005062 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005063 }
5064 else
5065 {
5066 list_T *l = argvars[0].vval.v_list;
5067 listitem_T *li;
5068 char_u *s;
5069
5070#ifdef USE_ARGV
5071 /* Pass argv[] to mch_call_shell(). */
5072 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5073 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005074 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005075#endif
5076 for (li = l->lv_first; li != NULL; li = li->li_next)
5077 {
5078 s = get_tv_string_chk(&li->li_tv);
5079 if (s == NULL)
5080 goto theend;
5081#ifdef USE_ARGV
5082 argv[argc++] = (char *)s;
5083#else
5084 /* Only escape when needed, double quotes are not always allowed. */
5085 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
5086 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005087# ifdef WIN32
5088 int old_ssl = p_ssl;
5089
5090 /* This is using CreateProcess, not cmd.exe. Always use
5091 * double quote and backslashes. */
5092 p_ssl = 0;
5093# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005094 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005095# ifdef WIN32
5096 p_ssl = old_ssl;
5097# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005098 if (s == NULL)
5099 goto theend;
5100 ga_concat(&ga, s);
5101 vim_free(s);
5102 }
5103 else
5104 ga_concat(&ga, s);
5105 if (li->li_next != NULL)
5106 ga_append(&ga, ' ');
5107#endif
5108 }
5109#ifdef USE_ARGV
5110 argv[argc] = NULL;
5111#else
5112 cmd = ga.ga_data;
5113#endif
5114 }
5115
5116#ifdef USE_ARGV
5117 if (ch_log_active())
5118 {
5119 garray_T ga;
5120 int i;
5121
5122 ga_init2(&ga, (int)sizeof(char), 200);
5123 for (i = 0; i < argc; ++i)
5124 {
5125 if (i > 0)
5126 ga_concat(&ga, (char_u *)" ");
5127 ga_concat(&ga, (char_u *)argv[i]);
5128 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005129 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005130 ga_clear(&ga);
5131 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005132 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005133#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005134 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005135 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005136#endif
5137
5138 /* If the channel is reading from a buffer, write lines now. */
5139 if (job->jv_channel != NULL)
5140 channel_write_in(job->jv_channel);
5141
5142theend:
5143#ifdef USE_ARGV
5144 vim_free(argv);
5145#else
5146 vim_free(ga.ga_data);
5147#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005148 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005149 return job;
5150}
5151
5152/*
5153 * Get the status of "job" and invoke the exit callback when needed.
5154 * The returned string is not allocated.
5155 */
5156 char *
5157job_status(job_T *job)
5158{
5159 char *result;
5160
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005161 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005162 /* No need to check, dead is dead. */
5163 result = "dead";
5164 else if (job->jv_status == JOB_FAILED)
5165 result = "fail";
5166 else
5167 {
5168 result = mch_job_status(job);
5169 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005170 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005171 }
5172 return result;
5173}
5174
Bram Moolenaar8950a562016-03-12 15:22:55 +01005175/*
5176 * Implementation of job_info().
5177 */
5178 void
5179job_info(job_T *job, dict_T *dict)
5180{
5181 dictitem_T *item;
5182 varnumber_T nr;
5183
5184 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5185
5186 item = dictitem_alloc((char_u *)"channel");
5187 if (item == NULL)
5188 return;
5189 item->di_tv.v_lock = 0;
5190 item->di_tv.v_type = VAR_CHANNEL;
5191 item->di_tv.vval.v_channel = job->jv_channel;
5192 if (job->jv_channel != NULL)
5193 ++job->jv_channel->ch_refcount;
5194 if (dict_add(dict, item) == FAIL)
5195 dictitem_free(item);
5196
5197#ifdef UNIX
5198 nr = job->jv_pid;
5199#else
5200 nr = job->jv_proc_info.dwProcessId;
5201#endif
5202 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005203 dict_add_nr_str(dict, "tty", 0L,
5204 job->jv_tty_name != NULL ? job->jv_tty_name : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005205
5206 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005207 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005208 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5209}
5210
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005211/*
5212 * Send a signal to "job". Implements job_stop().
5213 * When "type" is not NULL use this for the type.
5214 * Otherwise use argvars[1] for the type.
5215 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005216 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005217job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005218{
5219 char_u *arg;
5220
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005221 if (type != NULL)
5222 arg = (char_u *)type;
5223 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005224 arg = (char_u *)"";
5225 else
5226 {
5227 arg = get_tv_string_chk(&argvars[1]);
5228 if (arg == NULL)
5229 {
5230 EMSG(_(e_invarg));
5231 return 0;
5232 }
5233 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005234 if (job->jv_status == JOB_FAILED)
5235 {
5236 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5237 return 0;
5238 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005239 if (job->jv_status == JOB_ENDED)
5240 {
5241 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5242 return 0;
5243 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005244 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005245 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005246 return 0;
5247
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005248 /* Assume that only "kill" will kill the job. */
5249 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005250 job->jv_channel->ch_job_killed = TRUE;
5251
5252 /* We don't try freeing the job, obviously the caller still has a
5253 * reference to it. */
5254 return 1;
5255}
5256
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005257#endif /* FEAT_JOB_CHANNEL */