blob: 801e370cc963a914069bbec126a75e1f11138470 [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,
930 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == 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 Moolenaar8b374212016-02-24 20:43:06 +01002820 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002821 {
2822 typval_T argv[1];
2823 typval_T rettv;
2824 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002825 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002826
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002827 /* Invoke callbacks before the close callback, since it's weird to
2828 * first invoke the close callback. Increment the refcount to avoid
2829 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002830 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002831 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002832 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002833 while (may_invoke_callback(channel, part))
2834 ;
2835
2836 /* Invoke the close callback, if still set. */
2837 if (channel->ch_close_cb != NULL)
2838 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002839 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002840 (char *)channel->ch_close_cb);
2841 argv[0].v_type = VAR_CHANNEL;
2842 argv[0].vval.v_channel = channel;
2843 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002844 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002845 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002846 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002847 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002848 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002849
2850 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002851 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002852 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002853 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002854
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002855 --channel->ch_refcount;
2856
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002857 if (channel_need_redraw)
2858 {
2859 channel_need_redraw = FALSE;
2860 redraw_after_callback();
2861 }
2862
Bram Moolenaar958dc692016-12-01 15:34:12 +01002863 if (!channel->ch_drop_never)
2864 /* any remaining messages are useless now */
2865 for (part = PART_SOCK; part < PART_IN; ++part)
2866 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002867 }
2868
2869 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002870
2871#ifdef FEAT_TERMINAL
2872 term_channel_closed(channel);
2873#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002874}
2875
Bram Moolenaard04a0202016-01-26 23:30:18 +01002876/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002877 * Close the "in" part channel "channel".
2878 */
2879 void
2880channel_close_in(channel_T *channel)
2881{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002882 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002883}
2884
2885/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002886 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002887 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002888 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002889channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002890{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002891 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2892 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002893
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002894 while (channel_peek(channel, part) != NULL)
2895 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002896
2897 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002898 {
2899 cbq_T *node = cb_head->cq_next;
2900
2901 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002902 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002903 vim_free(node);
2904 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002905
2906 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002907 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002908 free_tv(json_head->jq_next->jq_value);
2909 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002910 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002911
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002912 free_callback(channel->ch_part[part].ch_callback,
2913 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002914 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002915 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002916}
2917
2918/*
2919 * Clear all the read buffers on "channel".
2920 */
2921 void
2922channel_clear(channel_T *channel)
2923{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002924 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002925 vim_free(channel->ch_hostname);
2926 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002927 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002928 channel_clear_one(channel, PART_OUT);
2929 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002930 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002931 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002932 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002933 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002934 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002935 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002936 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002937}
2938
Bram Moolenaar77073442016-02-13 23:23:53 +01002939#if defined(EXITFREE) || defined(PROTO)
2940 void
2941channel_free_all(void)
2942{
2943 channel_T *channel;
2944
Bram Moolenaard6051b52016-02-28 15:49:03 +01002945 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002946 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2947 channel_clear(channel);
2948}
2949#endif
2950
2951
Bram Moolenaar715d2852016-04-30 17:06:31 +02002952/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002953#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002954
2955/* Buffer size for reading incoming messages. */
2956#define MAXMSGSIZE 4096
2957
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002958#if defined(HAVE_SELECT)
2959/*
2960 * Add write fds where we are waiting for writing to be possible.
2961 */
2962 static int
2963channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2964{
2965 int maxfd = maxfd_arg;
2966 channel_T *ch;
2967
2968 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2969 {
2970 chanpart_T *in_part = &ch->ch_part[PART_IN];
2971
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002972 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002973 {
2974 FD_SET((int)in_part->ch_fd, wfds);
2975 if ((int)in_part->ch_fd >= maxfd)
2976 maxfd = (int)in_part->ch_fd + 1;
2977 }
2978 }
2979 return maxfd;
2980}
2981#else
2982/*
2983 * Add write fds where we are waiting for writing to be possible.
2984 */
2985 static int
2986channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2987{
2988 int nfd = nfd_in;
2989 channel_T *ch;
2990
2991 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2992 {
2993 chanpart_T *in_part = &ch->ch_part[PART_IN];
2994
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002995 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002996 {
2997 in_part->ch_poll_idx = nfd;
2998 fds[nfd].fd = in_part->ch_fd;
2999 fds[nfd].events = POLLOUT;
3000 ++nfd;
3001 }
3002 else
3003 in_part->ch_poll_idx = -1;
3004 }
3005 return nfd;
3006}
3007#endif
3008
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003009typedef enum {
3010 CW_READY,
3011 CW_NOT_READY,
3012 CW_ERROR
3013} channel_wait_result;
3014
Bram Moolenaard04a0202016-01-26 23:30:18 +01003015/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003016 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003017 * Return CW_READY when there is something to read.
3018 * Return CW_NOT_READY when there is nothing to read.
3019 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003020 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003021 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003022channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003023{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003024 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003025 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003026
Bram Moolenaard8070362016-02-15 21:56:54 +01003027# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003028 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003029 {
3030 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003031 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003032 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003033 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003034
3035 /* reading from a pipe, not a socket */
3036 while (TRUE)
3037 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003038 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3039
3040 if (r && nread > 0)
3041 return CW_READY;
3042 if (r == 0)
3043 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003044
3045 /* perhaps write some buffer lines */
3046 channel_write_any_lines();
3047
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003048 sleep_time = deadline - GetTickCount();
3049 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003050 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003051 /* Wait for a little while. Very short at first, up to 10 msec
3052 * after looping a few times. */
3053 if (sleep_time > delay)
3054 sleep_time = delay;
3055 Sleep(sleep_time);
3056 delay = delay * 2;
3057 if (delay > 10)
3058 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003059 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003060 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003061 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003062#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003063 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003064#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003065 struct timeval tval;
3066 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003067 fd_set wfds;
3068 int ret;
3069 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003070
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003071 tval.tv_sec = timeout / 1000;
3072 tval.tv_usec = (timeout % 1000) * 1000;
3073 for (;;)
3074 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003075 FD_ZERO(&rfds);
3076 FD_SET((int)fd, &rfds);
3077
3078 /* Write lines to a pipe when a pipe can be written to. Need to
3079 * set this every time, some buffers may be done. */
3080 maxfd = (int)fd + 1;
3081 FD_ZERO(&wfds);
3082 maxfd = channel_fill_wfds(maxfd, &wfds);
3083
3084 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003085# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003086 SOCK_ERRNO;
3087 if (ret == -1 && errno == EINTR)
3088 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003089# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003090 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003091 {
3092 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003093 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003094 channel_write_any_lines();
3095 continue;
3096 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003097 break;
3098 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003099#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003100 for (;;)
3101 {
3102 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3103 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003104
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003105 fds[0].fd = fd;
3106 fds[0].events = POLLIN;
3107 nfd = channel_fill_poll_write(nfd, fds);
3108 if (poll(fds, nfd, timeout) > 0)
3109 {
3110 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003111 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003112 channel_write_any_lines();
3113 continue;
3114 }
3115 break;
3116 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003117#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003118 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003119 return CW_NOT_READY;
3120}
3121
3122 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003123ch_close_part_on_error(
3124 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003125{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003126 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003127
3128 if (is_err)
3129 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003130 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003131 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003132 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003133
3134 /* Queue a "DETACH" netbeans message in the command queue in order to
3135 * terminate the netbeans session later. Do not end the session here
3136 * directly as we may be running in the context of a call to
3137 * netbeans_parse_messages():
3138 * netbeans_parse_messages
3139 * -> autocmd triggered while processing the netbeans cmd
3140 * -> ui_breakcheck
3141 * -> gui event loop or select loop
3142 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003143 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003144 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003145 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003146 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003147 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3148
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003149 /* When reading is not possible close this part of the channel. Don't
3150 * close the channel yet, there may be something to read on another part. */
3151 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003152
3153#ifdef FEAT_GUI
3154 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003155 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003156#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003157}
3158
3159 static void
3160channel_close_now(channel_T *channel)
3161{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003162 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003163 if (channel->ch_nb_close_cb != NULL)
3164 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003165 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003166}
3167
3168/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003169 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003170 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003171 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003172 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003173 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003174channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003175{
3176 static char_u *buf = NULL;
3177 int len = 0;
3178 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003179 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003180 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003181
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003182 fd = channel->ch_part[part].ch_fd;
3183 if (fd == INVALID_FD)
3184 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003185 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003186 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003187 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003188 }
3189 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003190
3191 /* Allocate a buffer to read into. */
3192 if (buf == NULL)
3193 {
3194 buf = alloc(MAXMSGSIZE);
3195 if (buf == NULL)
3196 return; /* out of memory! */
3197 }
3198
3199 /* Keep on reading for as long as there is something to read.
3200 * Use select() or poll() to avoid blocking on a message that is exactly
3201 * MAXMSGSIZE long. */
3202 for (;;)
3203 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003204 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003205 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003206 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003207 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003208 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003209 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003210 if (len <= 0)
3211 break; /* error or nothing more to read */
3212
3213 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003214 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003215 readlen += len;
3216 if (len < MAXMSGSIZE)
3217 break; /* did read everything that's available */
3218 }
3219
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003220 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003221 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003222 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003223
3224#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003225 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003226 if (CH_HAS_GUI && gtk_main_level() > 0)
3227 gtk_main_quit();
3228#endif
3229}
3230
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003231/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003232 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003233 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003234 * Returns what was read in allocated memory.
3235 * Returns NULL in case of error or timeout.
3236 */
3237 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003238channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003239{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003240 char_u *buf;
3241 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003242 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003243 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003244 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003245 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003246
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003247 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003248 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003249
3250 while (TRUE)
3251 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003252 node = channel_peek(channel, part);
3253 if (node != NULL)
3254 {
3255 if (mode == MODE_RAW || (mode == MODE_NL
3256 && channel_first_nl(node) != NULL))
3257 /* got a complete message */
3258 break;
3259 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3260 continue;
3261 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003262
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003263 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003264 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003265 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003266 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003267 {
3268 ch_log(channel, "Timed out");
3269 return NULL;
3270 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003271 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003272 }
3273
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003274 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003275 if (mode == MODE_RAW)
3276 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003277 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003278 }
3279 else
3280 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003281 char_u *p;
3282
3283 buf = node->rq_buffer;
3284 nl = channel_first_nl(node);
3285
3286 /* Convert NUL to NL, the internal representation. */
3287 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3288 if (*p == NUL)
3289 *p = NL;
3290
3291 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003292 {
3293 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003294 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003295 *nl = NUL;
3296 }
3297 else
3298 {
3299 /* Copy the message into allocated memory and remove it from the
3300 * buffer. */
3301 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003302 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003303 }
3304 }
3305 if (log_fd != NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003306 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003307 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003308}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003309
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003310/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003311 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003312 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003313 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003314 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003315 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003316 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003317channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003318 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003319 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003320 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003321 int id,
3322 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003323{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003324 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003325 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003326 int timeout;
3327 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003328
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003329 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003330 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003331 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003332 for (;;)
3333 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003334 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003335
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003336 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003337 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003338 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003339 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003340 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003341 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003342
Bram Moolenaard7ece102016-02-02 23:23:02 +01003343 if (!more)
3344 {
3345 /* Handle any other messages in the queue. If done some more
3346 * messages may have arrived. */
3347 if (channel_parse_messages())
3348 continue;
3349
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003350 /* Wait for up to the timeout. If there was an incomplete message
3351 * use the deadline for that. */
3352 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003353 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003354 {
3355#ifdef WIN32
3356 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3357#else
3358 {
3359 struct timeval now_tv;
3360
3361 gettimeofday(&now_tv, NULL);
3362 timeout = (chanpart->ch_deadline.tv_sec
3363 - now_tv.tv_sec) * 1000
3364 + (chanpart->ch_deadline.tv_usec
3365 - now_tv.tv_usec) / 1000
3366 + 1;
3367 }
3368#endif
3369 if (timeout < 0)
3370 {
3371 /* Something went wrong, channel_parse_json() didn't
3372 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003373 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003374 timeout = timeout_arg;
3375 }
3376 else if (timeout > timeout_arg)
3377 timeout = timeout_arg;
3378 }
3379 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003380 if (fd == INVALID_FD
3381 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003382 {
3383 if (timeout == timeout_arg)
3384 {
3385 if (fd != INVALID_FD)
3386 ch_log(channel, "Timed out");
3387 break;
3388 }
3389 }
3390 else
3391 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003392 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003393 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003394 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003395 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003396}
3397
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003398/*
3399 * Common for ch_read() and ch_readraw().
3400 */
3401 void
3402common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3403{
3404 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003405 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003406 jobopt_T opt;
3407 int mode;
3408 int timeout;
3409 int id = -1;
3410 typval_T *listtv = NULL;
3411
3412 /* return an empty string by default */
3413 rettv->v_type = VAR_STRING;
3414 rettv->vval.v_string = NULL;
3415
3416 clear_job_options(&opt);
3417 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3418 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003419 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003420
Bram Moolenaar437905c2016-04-26 19:01:05 +02003421 if (opt.jo_set & JO_PART)
3422 part = opt.jo_part;
3423 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003424 if (channel != NULL)
3425 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003426 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003427 part = channel_part_read(channel);
3428 mode = channel_get_mode(channel, part);
3429 timeout = channel_get_timeout(channel, part);
3430 if (opt.jo_set & JO_TIMEOUT)
3431 timeout = opt.jo_timeout;
3432
3433 if (raw || mode == MODE_RAW || mode == MODE_NL)
3434 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3435 else
3436 {
3437 if (opt.jo_set & JO_ID)
3438 id = opt.jo_id;
3439 channel_read_json_block(channel, part, timeout, id, &listtv);
3440 if (listtv != NULL)
3441 {
3442 *rettv = *listtv;
3443 vim_free(listtv);
3444 }
3445 else
3446 {
3447 rettv->v_type = VAR_SPECIAL;
3448 rettv->vval.v_number = VVAL_NONE;
3449 }
3450 }
3451 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003452
3453theend:
3454 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003455}
3456
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003457# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3458 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003459/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003460 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003461 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003462 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003463 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003464channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003465{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003466 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003467 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003468
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003469 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003470 for (channel = first_channel; channel != NULL;
3471 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003472 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003473 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003474 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003475 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003476 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003477 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003478 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003479 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003480 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003481}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003482# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003483
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003484# if defined(WIN32) || defined(PROTO)
3485/*
3486 * Check the channels for anything that is ready to be read.
3487 * The data is put in the read queue.
3488 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003489 void
3490channel_handle_events(void)
3491{
3492 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003493 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003494 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003495
3496 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3497 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003498 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003499 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003500 {
3501 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003502 if (fd != INVALID_FD)
3503 {
3504 int r = channel_wait(channel, fd, 0);
3505
3506 if (r == CW_READY)
3507 channel_read(channel, part, "channel_handle_events");
3508 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003509 ch_close_part_on_error(channel, part, TRUE,
3510 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003511 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003512 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003513 }
3514}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003515# endif
3516
Bram Moolenaard04a0202016-01-26 23:30:18 +01003517/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003518 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003519 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003520 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003521 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003522 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003523channel_send(
3524 channel_T *channel,
3525 ch_part_T part,
3526 char_u *buf,
3527 int len,
3528 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003529{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003530 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003531 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003532
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003533 fd = channel->ch_part[part].ch_fd;
3534 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003535 {
3536 if (!channel->ch_error && fun != NULL)
3537 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003538 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003539 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003540 }
3541 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003542 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003543 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003544
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003545 if (log_fd != NULL)
3546 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003547 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003548 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003549 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003550 fprintf(log_fd, "'\n");
3551 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003552 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003553 }
3554
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003555 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003556 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003557 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003558 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003559 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003560 {
3561 if (!channel->ch_error && fun != NULL)
3562 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003563 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003564 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003565 }
3566 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003567 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003568 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003569
3570 channel->ch_error = FALSE;
3571 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003572}
3573
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003574/*
3575 * Common for "ch_sendexpr()" and "ch_sendraw()".
3576 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003577 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003578 * Otherwise returns NULL.
3579 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003580 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003581send_common(
3582 typval_T *argvars,
3583 char_u *text,
3584 int id,
3585 int eval,
3586 jobopt_T *opt,
3587 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003588 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003589{
3590 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003591 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003592
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003593 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003594 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003595 if (channel == NULL)
3596 return NULL;
3597 part_send = channel_part_send(channel);
3598 *part_read = channel_part_read(channel);
3599
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003600 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3601 return NULL;
3602
3603 /* Set the callback. An empty callback means no callback and not reading
3604 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3605 * allowed. */
3606 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3607 {
3608 if (eval)
3609 {
3610 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3611 return NULL;
3612 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003613 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003614 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003615 }
3616
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003617 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003618 && opt->jo_callback == NULL)
3619 return channel;
3620 return NULL;
3621}
3622
3623/*
3624 * common for "ch_evalexpr()" and "ch_sendexpr()"
3625 */
3626 void
3627ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3628{
3629 char_u *text;
3630 typval_T *listtv;
3631 channel_T *channel;
3632 int id;
3633 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003634 ch_part_T part_send;
3635 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003636 jobopt_T opt;
3637 int timeout;
3638
3639 /* return an empty string by default */
3640 rettv->v_type = VAR_STRING;
3641 rettv->vval.v_string = NULL;
3642
Bram Moolenaar437905c2016-04-26 19:01:05 +02003643 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003644 if (channel == NULL)
3645 return;
3646 part_send = channel_part_send(channel);
3647
3648 ch_mode = channel_get_mode(channel, part_send);
3649 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3650 {
3651 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3652 return;
3653 }
3654
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003655 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003656 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003657 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003658 if (text == NULL)
3659 return;
3660
3661 channel = send_common(argvars, text, id, eval, &opt,
3662 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3663 vim_free(text);
3664 if (channel != NULL && eval)
3665 {
3666 if (opt.jo_set & JO_TIMEOUT)
3667 timeout = opt.jo_timeout;
3668 else
3669 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003670 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3671 == OK)
3672 {
3673 list_T *list = listtv->vval.v_list;
3674
3675 /* Move the item from the list and then change the type to
3676 * avoid the value being freed. */
3677 *rettv = list->lv_last->li_tv;
3678 list->lv_last->li_tv.v_type = VAR_NUMBER;
3679 free_tv(listtv);
3680 }
3681 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003682 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003683}
3684
3685/*
3686 * common for "ch_evalraw()" and "ch_sendraw()"
3687 */
3688 void
3689ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3690{
3691 char_u buf[NUMBUFLEN];
3692 char_u *text;
3693 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003694 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003695 jobopt_T opt;
3696 int timeout;
3697
3698 /* return an empty string by default */
3699 rettv->v_type = VAR_STRING;
3700 rettv->vval.v_string = NULL;
3701
3702 text = get_tv_string_buf(&argvars[1], buf);
3703 channel = send_common(argvars, text, 0, eval, &opt,
3704 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3705 if (channel != NULL && eval)
3706 {
3707 if (opt.jo_set & JO_TIMEOUT)
3708 timeout = opt.jo_timeout;
3709 else
3710 timeout = channel_get_timeout(channel, part_read);
3711 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3712 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003713 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003714}
3715
Bram Moolenaard04a0202016-01-26 23:30:18 +01003716# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003717/*
3718 * Add open channels to the poll struct.
3719 * Return the adjusted struct index.
3720 * The type of "fds" is hidden to avoid problems with the function proto.
3721 */
3722 int
3723channel_poll_setup(int nfd_in, void *fds_in)
3724{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003725 int nfd = nfd_in;
3726 channel_T *channel;
3727 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003728 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003729
Bram Moolenaar77073442016-02-13 23:23:53 +01003730 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003731 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003732 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003733 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003734 chanpart_T *ch_part = &channel->ch_part[part];
3735
3736 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003737 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003738 ch_part->ch_poll_idx = nfd;
3739 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003740 fds[nfd].events = POLLIN;
3741 nfd++;
3742 }
3743 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003744 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003745 }
3746 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003747
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003748 nfd = channel_fill_poll_write(nfd, fds);
3749
Bram Moolenaare0874f82016-01-24 20:36:41 +01003750 return nfd;
3751}
3752
3753/*
3754 * The type of "fds" is hidden to avoid problems with the function proto.
3755 */
3756 int
3757channel_poll_check(int ret_in, void *fds_in)
3758{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003759 int ret = ret_in;
3760 channel_T *channel;
3761 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003762 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003763 int idx;
3764 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003765
Bram Moolenaar77073442016-02-13 23:23:53 +01003766 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003767 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003768 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003769 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003770 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003771
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003772 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003773 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003774 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003775 --ret;
3776 }
3777 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003778
3779 in_part = &channel->ch_part[PART_IN];
3780 idx = in_part->ch_poll_idx;
3781 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3782 {
3783 if (in_part->ch_buf_append)
3784 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003785 if (in_part->ch_bufref.br_buf != NULL)
3786 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003787 }
3788 else
3789 channel_write_in(channel);
3790 --ret;
3791 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003792 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003793
3794 return ret;
3795}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003796# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003797
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003798# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003799/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003800 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003801 */
3802 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003803channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003804{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003805 int maxfd = maxfd_in;
3806 channel_T *channel;
3807 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003808 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003809 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003810
Bram Moolenaar77073442016-02-13 23:23:53 +01003811 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003812 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003813 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003814 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003815 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003816
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003817 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003818 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003819 FD_SET((int)fd, rfds);
3820 if (maxfd < (int)fd)
3821 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003822 }
3823 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003824 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003825
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003826 maxfd = channel_fill_wfds(maxfd, wfds);
3827
Bram Moolenaare0874f82016-01-24 20:36:41 +01003828 return maxfd;
3829}
3830
3831/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003832 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003833 */
3834 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003835channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003836{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003837 int ret = ret_in;
3838 channel_T *channel;
3839 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003840 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003841 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003842 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003843
Bram Moolenaar77073442016-02-13 23:23:53 +01003844 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003845 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003846 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003847 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003848 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003849
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003850 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003851 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003852 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003853 --ret;
3854 }
3855 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003856
3857 in_part = &channel->ch_part[PART_IN];
3858 if (ret > 0 && in_part->ch_fd != INVALID_FD
3859 && FD_ISSET(in_part->ch_fd, wfds))
3860 {
3861 if (in_part->ch_buf_append)
3862 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003863 if (in_part->ch_bufref.br_buf != NULL)
3864 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003865 }
3866 else
3867 channel_write_in(channel);
3868 --ret;
3869 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003870 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003871
3872 return ret;
3873}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003874# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003875
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003876/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003877 * Execute queued up commands.
3878 * Invoked from the main loop when it's safe to execute received commands.
3879 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003880 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003881 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003882channel_parse_messages(void)
3883{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003884 channel_T *channel = first_channel;
3885 int ret = FALSE;
3886 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003887 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003888#ifdef ELAPSED_FUNC
3889 ELAPSED_TYPE start_tv;
3890
3891 ELAPSED_INIT(start_tv);
3892#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003893
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003894 ++safe_to_invoke_callback;
3895
Bram Moolenaard0b65022016-03-06 21:50:33 +01003896 /* Only do this message when another message was given, otherwise we get
3897 * lots of them. */
3898 if (did_log_msg)
3899 {
3900 ch_log(NULL, "looking for messages on channels");
3901 did_log_msg = FALSE;
3902 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003903 while (channel != NULL)
3904 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003905 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003906 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003907 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003908 channel_close_now(channel);
3909 /* channel may have been freed, start over */
3910 channel = first_channel;
3911 continue;
3912 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003913 if (channel->ch_to_be_freed)
3914 {
3915 channel_free(channel);
3916 /* channel has been freed, start over */
3917 channel = first_channel;
3918 continue;
3919 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003920 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003921 {
3922 /* channel is no longer useful, free it */
3923 channel_free(channel);
3924 channel = first_channel;
3925 part = PART_SOCK;
3926 continue;
3927 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003928 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003929 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003930 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003931 /* Increase the refcount, in case the handler causes the channel
3932 * to be unreferenced or closed. */
3933 ++channel->ch_refcount;
3934 r = may_invoke_callback(channel, part);
3935 if (r == OK)
3936 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003937 if (channel_unref(channel) || (r == OK
3938#ifdef ELAPSED_FUNC
3939 /* Limit the time we loop here to 100 msec, otherwise
3940 * Vim becomes unresponsive when the callback takes
3941 * more than a bit of time. */
3942 && ELAPSED_FUNC(start_tv) < 100L
3943#endif
3944 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003945 {
3946 /* channel was freed or something was done, start over */
3947 channel = first_channel;
3948 part = PART_SOCK;
3949 continue;
3950 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003951 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003952 if (part < PART_ERR)
3953 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003954 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003955 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003956 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003957 part = PART_SOCK;
3958 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003959 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003960
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003961 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003962 {
3963 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003964 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003965 }
3966
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003967 --safe_to_invoke_callback;
3968
Bram Moolenaard7ece102016-02-02 23:23:02 +01003969 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003970}
3971
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003972/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01003973 * Return TRUE if any channel has readahead. That means we should not block on
3974 * waiting for input.
3975 */
3976 int
3977channel_any_readahead(void)
3978{
3979 channel_T *channel = first_channel;
3980 ch_part_T part = PART_SOCK;
3981
3982 while (channel != NULL)
3983 {
3984 if (channel_has_readahead(channel, part))
3985 return TRUE;
3986 if (part < PART_ERR)
3987 ++part;
3988 else
3989 {
3990 channel = channel->ch_next;
3991 part = PART_SOCK;
3992 }
3993 }
3994 return FALSE;
3995}
3996
3997/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003998 * Mark references to lists used in channels.
3999 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004000 int
4001set_ref_in_channel(int copyID)
4002{
Bram Moolenaar77073442016-02-13 23:23:53 +01004003 int abort = FALSE;
4004 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004005 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004006
Bram Moolenaar77073442016-02-13 23:23:53 +01004007 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004008 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004009 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004010 tv.v_type = VAR_CHANNEL;
4011 tv.vval.v_channel = channel;
4012 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004013 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004014 return abort;
4015}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004016
4017/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004018 * Return the "part" to write to for "channel".
4019 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004020 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004021channel_part_send(channel_T *channel)
4022{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004023 if (channel->CH_SOCK_FD == INVALID_FD)
4024 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004025 return PART_SOCK;
4026}
4027
4028/*
4029 * Return the default "part" to read from for "channel".
4030 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004031 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004032channel_part_read(channel_T *channel)
4033{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004034 if (channel->CH_SOCK_FD == INVALID_FD)
4035 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004036 return PART_SOCK;
4037}
4038
4039/*
4040 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004041 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004042 */
4043 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004044channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004045{
Bram Moolenaar77073442016-02-13 23:23:53 +01004046 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004047 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004048 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004049}
4050
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004051/*
4052 * Return the timeout of "channel"/"part"
4053 */
4054 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004055channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004056{
4057 return channel->ch_part[part].ch_timeout;
4058}
4059
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004060 static int
4061handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4062{
4063 char_u *val = get_tv_string(item);
4064
4065 opt->jo_set |= jo;
4066 if (STRCMP(val, "nl") == 0)
4067 *modep = MODE_NL;
4068 else if (STRCMP(val, "raw") == 0)
4069 *modep = MODE_RAW;
4070 else if (STRCMP(val, "js") == 0)
4071 *modep = MODE_JS;
4072 else if (STRCMP(val, "json") == 0)
4073 *modep = MODE_JSON;
4074 else
4075 {
4076 EMSG2(_(e_invarg2), val);
4077 return FAIL;
4078 }
4079 return OK;
4080}
4081
4082 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004083handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004084{
4085 char_u *val = get_tv_string(item);
4086
4087 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4088 if (STRCMP(val, "null") == 0)
4089 opt->jo_io[part] = JIO_NULL;
4090 else if (STRCMP(val, "pipe") == 0)
4091 opt->jo_io[part] = JIO_PIPE;
4092 else if (STRCMP(val, "file") == 0)
4093 opt->jo_io[part] = JIO_FILE;
4094 else if (STRCMP(val, "buffer") == 0)
4095 opt->jo_io[part] = JIO_BUFFER;
4096 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4097 opt->jo_io[part] = JIO_OUT;
4098 else
4099 {
4100 EMSG2(_(e_invarg2), val);
4101 return FAIL;
4102 }
4103 return OK;
4104}
4105
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004106/*
4107 * Clear a jobopt_T before using it.
4108 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004109 void
4110clear_job_options(jobopt_T *opt)
4111{
4112 vim_memset(opt, 0, sizeof(jobopt_T));
4113}
4114
4115/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004116 * Free any members of a jobopt_T.
4117 */
4118 void
4119free_job_options(jobopt_T *opt)
4120{
4121 if (opt->jo_partial != NULL)
4122 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004123 else if (opt->jo_callback != NULL)
4124 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004125 if (opt->jo_out_partial != NULL)
4126 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004127 else if (opt->jo_out_cb != NULL)
4128 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004129 if (opt->jo_err_partial != NULL)
4130 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004131 else if (opt->jo_err_cb != NULL)
4132 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004133 if (opt->jo_close_partial != NULL)
4134 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004135 else if (opt->jo_close_cb != NULL)
4136 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004137 if (opt->jo_exit_partial != NULL)
4138 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004139 else if (opt->jo_exit_cb != NULL)
4140 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004141}
4142
4143/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004144 * Get the PART_ number from the first character of an option name.
4145 */
4146 static int
4147part_from_char(int c)
4148{
4149 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4150}
4151
4152/*
4153 * Get the option entries from the dict in "tv", parse them and put the result
4154 * in "opt".
4155 * Only accept options in "supported".
4156 * If an option value is invalid return FAIL.
4157 */
4158 int
4159get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4160{
4161 typval_T *item;
4162 char_u *val;
4163 dict_T *dict;
4164 int todo;
4165 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004166 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004167
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004168 if (tv->v_type == VAR_UNKNOWN)
4169 return OK;
4170 if (tv->v_type != VAR_DICT)
4171 {
4172 EMSG(_(e_invarg));
4173 return FAIL;
4174 }
4175 dict = tv->vval.v_dict;
4176 if (dict == NULL)
4177 return OK;
4178
4179 todo = (int)dict->dv_hashtab.ht_used;
4180 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4181 if (!HASHITEM_EMPTY(hi))
4182 {
4183 item = &dict_lookup(hi)->di_tv;
4184
4185 if (STRCMP(hi->hi_key, "mode") == 0)
4186 {
4187 if (!(supported & JO_MODE))
4188 break;
4189 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4190 return FAIL;
4191 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004192 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004193 {
4194 if (!(supported & JO_IN_MODE))
4195 break;
4196 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4197 == FAIL)
4198 return FAIL;
4199 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004200 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004201 {
4202 if (!(supported & JO_OUT_MODE))
4203 break;
4204 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4205 == FAIL)
4206 return FAIL;
4207 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004208 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004209 {
4210 if (!(supported & JO_ERR_MODE))
4211 break;
4212 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4213 == FAIL)
4214 return FAIL;
4215 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004216 else if (STRCMP(hi->hi_key, "in_io") == 0
4217 || STRCMP(hi->hi_key, "out_io") == 0
4218 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004219 {
4220 if (!(supported & JO_OUT_IO))
4221 break;
4222 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4223 return FAIL;
4224 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004225 else if (STRCMP(hi->hi_key, "in_name") == 0
4226 || STRCMP(hi->hi_key, "out_name") == 0
4227 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004228 {
4229 part = part_from_char(*hi->hi_key);
4230
4231 if (!(supported & JO_OUT_IO))
4232 break;
4233 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4234 opt->jo_io_name[part] =
4235 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4236 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004237 else if (STRCMP(hi->hi_key, "pty") == 0)
4238 {
4239 if (!(supported & JO_MODE))
4240 break;
4241 opt->jo_pty = get_tv_number(item);
4242 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004243 else if (STRCMP(hi->hi_key, "in_buf") == 0
4244 || STRCMP(hi->hi_key, "out_buf") == 0
4245 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004246 {
4247 part = part_from_char(*hi->hi_key);
4248
4249 if (!(supported & JO_OUT_IO))
4250 break;
4251 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4252 opt->jo_io_buf[part] = get_tv_number(item);
4253 if (opt->jo_io_buf[part] <= 0)
4254 {
4255 EMSG2(_(e_invarg2), get_tv_string(item));
4256 return FAIL;
4257 }
4258 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4259 {
4260 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4261 return FAIL;
4262 }
4263 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004264 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4265 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4266 {
4267 part = part_from_char(*hi->hi_key);
4268
4269 if (!(supported & JO_OUT_IO))
4270 break;
4271 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4272 opt->jo_modifiable[part] = get_tv_number(item);
4273 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004274 else if (STRCMP(hi->hi_key, "out_msg") == 0
4275 || STRCMP(hi->hi_key, "err_msg") == 0)
4276 {
4277 part = part_from_char(*hi->hi_key);
4278
4279 if (!(supported & JO_OUT_IO))
4280 break;
4281 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4282 opt->jo_message[part] = get_tv_number(item);
4283 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004284 else if (STRCMP(hi->hi_key, "in_top") == 0
4285 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004286 {
4287 linenr_T *lp;
4288
4289 if (!(supported & JO_OUT_IO))
4290 break;
4291 if (hi->hi_key[3] == 't')
4292 {
4293 lp = &opt->jo_in_top;
4294 opt->jo_set |= JO_IN_TOP;
4295 }
4296 else
4297 {
4298 lp = &opt->jo_in_bot;
4299 opt->jo_set |= JO_IN_BOT;
4300 }
4301 *lp = get_tv_number(item);
4302 if (*lp < 0)
4303 {
4304 EMSG2(_(e_invarg2), get_tv_string(item));
4305 return FAIL;
4306 }
4307 }
4308 else if (STRCMP(hi->hi_key, "channel") == 0)
4309 {
4310 if (!(supported & JO_OUT_IO))
4311 break;
4312 opt->jo_set |= JO_CHANNEL;
4313 if (item->v_type != VAR_CHANNEL)
4314 {
4315 EMSG2(_(e_invarg2), "channel");
4316 return FAIL;
4317 }
4318 opt->jo_channel = item->vval.v_channel;
4319 }
4320 else if (STRCMP(hi->hi_key, "callback") == 0)
4321 {
4322 if (!(supported & JO_CALLBACK))
4323 break;
4324 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004325 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004326 if (opt->jo_callback == NULL)
4327 {
4328 EMSG2(_(e_invarg2), "callback");
4329 return FAIL;
4330 }
4331 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004332 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004333 {
4334 if (!(supported & JO_OUT_CALLBACK))
4335 break;
4336 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004337 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004338 if (opt->jo_out_cb == NULL)
4339 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004340 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004341 return FAIL;
4342 }
4343 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004344 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004345 {
4346 if (!(supported & JO_ERR_CALLBACK))
4347 break;
4348 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004349 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004350 if (opt->jo_err_cb == NULL)
4351 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004352 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004353 return FAIL;
4354 }
4355 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004356 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004357 {
4358 if (!(supported & JO_CLOSE_CALLBACK))
4359 break;
4360 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004361 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004362 if (opt->jo_close_cb == NULL)
4363 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004364 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004365 return FAIL;
4366 }
4367 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004368 else if (STRCMP(hi->hi_key, "drop") == 0)
4369 {
4370 int never = FALSE;
4371 val = get_tv_string(item);
4372
4373 if (STRCMP(val, "never") == 0)
4374 never = TRUE;
4375 else if (STRCMP(val, "auto") != 0)
4376 {
4377 EMSG2(_(e_invarg2), "drop");
4378 return FAIL;
4379 }
4380 opt->jo_drop_never = never;
4381 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004382 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4383 {
4384 if (!(supported & JO_EXIT_CB))
4385 break;
4386 opt->jo_set |= JO_EXIT_CB;
4387 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4388 if (opt->jo_exit_cb == NULL)
4389 {
4390 EMSG2(_(e_invarg2), "exit_cb");
4391 return FAIL;
4392 }
4393 }
Bram Moolenaar78712a72017-08-05 14:50:12 +02004394 else if (STRCMP(hi->hi_key, "term_name") == 0)
4395 {
4396 if (!(supported & JO2_TERM_NAME))
4397 break;
4398 opt->jo_set2 |= JO2_TERM_NAME;
4399 opt->jo_term_name = get_tv_string_chk(item);
4400 if (opt->jo_term_name == NULL)
4401 {
4402 EMSG2(_(e_invarg2), "term_name");
4403 return FAIL;
4404 }
4405 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004406 else if (STRCMP(hi->hi_key, "waittime") == 0)
4407 {
4408 if (!(supported & JO_WAITTIME))
4409 break;
4410 opt->jo_set |= JO_WAITTIME;
4411 opt->jo_waittime = get_tv_number(item);
4412 }
4413 else if (STRCMP(hi->hi_key, "timeout") == 0)
4414 {
4415 if (!(supported & JO_TIMEOUT))
4416 break;
4417 opt->jo_set |= JO_TIMEOUT;
4418 opt->jo_timeout = get_tv_number(item);
4419 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004420 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004421 {
4422 if (!(supported & JO_OUT_TIMEOUT))
4423 break;
4424 opt->jo_set |= JO_OUT_TIMEOUT;
4425 opt->jo_out_timeout = get_tv_number(item);
4426 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004427 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004428 {
4429 if (!(supported & JO_ERR_TIMEOUT))
4430 break;
4431 opt->jo_set |= JO_ERR_TIMEOUT;
4432 opt->jo_err_timeout = get_tv_number(item);
4433 }
4434 else if (STRCMP(hi->hi_key, "part") == 0)
4435 {
4436 if (!(supported & JO_PART))
4437 break;
4438 opt->jo_set |= JO_PART;
4439 val = get_tv_string(item);
4440 if (STRCMP(val, "err") == 0)
4441 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004442 else if (STRCMP(val, "out") == 0)
4443 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004444 else
4445 {
4446 EMSG2(_(e_invarg2), val);
4447 return FAIL;
4448 }
4449 }
4450 else if (STRCMP(hi->hi_key, "id") == 0)
4451 {
4452 if (!(supported & JO_ID))
4453 break;
4454 opt->jo_set |= JO_ID;
4455 opt->jo_id = get_tv_number(item);
4456 }
4457 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4458 {
4459 if (!(supported & JO_STOPONEXIT))
4460 break;
4461 opt->jo_set |= JO_STOPONEXIT;
4462 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4463 opt->jo_soe_buf);
4464 if (opt->jo_stoponexit == NULL)
4465 {
4466 EMSG2(_(e_invarg2), "stoponexit");
4467 return FAIL;
4468 }
4469 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004470 else if (STRCMP(hi->hi_key, "block_write") == 0)
4471 {
4472 if (!(supported & JO_BLOCK_WRITE))
4473 break;
4474 opt->jo_set |= JO_BLOCK_WRITE;
4475 opt->jo_block_write = get_tv_number(item);
4476 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004477 else
4478 break;
4479 --todo;
4480 }
4481 if (todo > 0)
4482 {
4483 EMSG2(_(e_invarg2), hi->hi_key);
4484 return FAIL;
4485 }
4486
4487 return OK;
4488}
4489
4490/*
4491 * Get the channel from the argument.
4492 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004493 * When "check_open" is TRUE check that the channel can be used.
4494 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004495 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004496 */
4497 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004498get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004499{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004500 channel_T *channel = NULL;
4501 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004502
4503 if (tv->v_type == VAR_JOB)
4504 {
4505 if (tv->vval.v_job != NULL)
4506 channel = tv->vval.v_job->jv_channel;
4507 }
4508 else if (tv->v_type == VAR_CHANNEL)
4509 {
4510 channel = tv->vval.v_channel;
4511 }
4512 else
4513 {
4514 EMSG2(_(e_invarg2), get_tv_string(tv));
4515 return NULL;
4516 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004517 if (channel != NULL && reading)
4518 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004519 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004520
Bram Moolenaar437905c2016-04-26 19:01:05 +02004521 if (check_open && (channel == NULL || (!channel_is_open(channel)
4522 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004523 {
4524 EMSG(_("E906: not an open channel"));
4525 return NULL;
4526 }
4527 return channel;
4528}
4529
4530static job_T *first_job = NULL;
4531
4532 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004533job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004534{
4535 ch_log(job->jv_channel, "Freeing job");
4536 if (job->jv_channel != NULL)
4537 {
4538 /* The link from the channel to the job doesn't count as a reference,
4539 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004540 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004541 * NULL the reference. We don't set ch_job_killed, unreferencing the
4542 * job doesn't mean it stops running. */
4543 job->jv_channel->ch_job = NULL;
4544 channel_unref(job->jv_channel);
4545 }
4546 mch_clear_job(job);
4547
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004548 vim_free(job->jv_tty_name);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004549 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004550 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004551}
4552
4553 static void
4554job_free_job(job_T *job)
4555{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004556 if (job->jv_next != NULL)
4557 job->jv_next->jv_prev = job->jv_prev;
4558 if (job->jv_prev == NULL)
4559 first_job = job->jv_next;
4560 else
4561 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004562 vim_free(job);
4563}
4564
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004565 static void
4566job_free(job_T *job)
4567{
4568 if (!in_free_unref_items)
4569 {
4570 job_free_contents(job);
4571 job_free_job(job);
4572 }
4573}
4574
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004575#if defined(EXITFREE) || defined(PROTO)
4576 void
4577job_free_all(void)
4578{
4579 while (first_job != NULL)
4580 job_free(first_job);
4581}
4582#endif
4583
4584/*
4585 * Return TRUE if we need to check if the process of "job" has ended.
4586 */
4587 static int
4588job_need_end_check(job_T *job)
4589{
4590 return job->jv_status == JOB_STARTED
4591 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4592}
4593
4594/*
4595 * Return TRUE if the channel of "job" is still useful.
4596 */
4597 static int
4598job_channel_still_useful(job_T *job)
4599{
4600 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4601}
4602
4603/*
4604 * Return TRUE if the job should not be freed yet. Do not free the job when
4605 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4606 * or when the associated channel will do something with the job output.
4607 */
4608 static int
4609job_still_useful(job_T *job)
4610{
4611 return job_need_end_check(job) || job_channel_still_useful(job);
4612}
4613
4614/*
4615 * NOTE: Must call job_cleanup() only once right after the status of "job"
4616 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4617 * mch_detect_ended_job() returned non-NULL).
4618 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004619 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02004620job_cleanup(job_T *job)
4621{
4622 if (job->jv_status != JOB_ENDED)
4623 return;
4624
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004625 /* Ready to cleanup the job. */
4626 job->jv_status = JOB_FINISHED;
4627
Bram Moolenaar97792de2016-10-15 18:36:49 +02004628 if (job->jv_exit_cb != NULL)
4629 {
4630 typval_T argv[3];
4631 typval_T rettv;
4632 int dummy;
4633
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004634 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02004635 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02004636 ++job->jv_refcount;
4637 argv[0].v_type = VAR_JOB;
4638 argv[0].vval.v_job = job;
4639 argv[1].v_type = VAR_NUMBER;
4640 argv[1].vval.v_number = job->jv_exitval;
4641 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4642 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4643 job->jv_exit_partial, NULL);
4644 clear_tv(&rettv);
4645 --job->jv_refcount;
4646 channel_need_redraw = TRUE;
4647 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004648
4649 /* Do not free the job in case the close callback of the associated channel
4650 * isn't invoked yet and may get information by job_info(). */
4651 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004652 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004653 /* The job was already unreferenced and the associated channel was
4654 * detached, now that it ended it can be freed. Careful: caller must
4655 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004656 job_free(job);
4657 }
4658}
4659
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004660/*
4661 * Mark references in jobs that are still useful.
4662 */
4663 int
4664set_ref_in_job(int copyID)
4665{
4666 int abort = FALSE;
4667 job_T *job;
4668 typval_T tv;
4669
4670 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004671 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004672 {
4673 tv.v_type = VAR_JOB;
4674 tv.vval.v_job = job;
4675 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4676 }
4677 return abort;
4678}
4679
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004680/*
4681 * Dereference "job". Note that after this "job" may have been freed.
4682 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004683 void
4684job_unref(job_T *job)
4685{
4686 if (job != NULL && --job->jv_refcount <= 0)
4687 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004688 /* Do not free the job if there is a channel where the close callback
4689 * may get the job info. */
4690 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004691 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004692 /* Do not free the job when it has not ended yet and there is a
4693 * "stoponexit" flag or an exit callback. */
4694 if (!job_need_end_check(job))
4695 {
4696 job_free(job);
4697 }
4698 else if (job->jv_channel != NULL)
4699 {
4700 /* Do remove the link to the channel, otherwise it hangs
4701 * around until Vim exits. See job_free() for refcount. */
4702 ch_log(job->jv_channel, "detaching channel from job");
4703 job->jv_channel->ch_job = NULL;
4704 channel_unref(job->jv_channel);
4705 job->jv_channel = NULL;
4706 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004707 }
4708 }
4709}
4710
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004711 int
4712free_unused_jobs_contents(int copyID, int mask)
4713{
4714 int did_free = FALSE;
4715 job_T *job;
4716
4717 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004718 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004719 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004720 {
4721 /* Free the channel and ordinary items it contains, but don't
4722 * recurse into Lists, Dictionaries etc. */
4723 job_free_contents(job);
4724 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004725 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004726 return did_free;
4727}
4728
4729 void
4730free_unused_jobs(int copyID, int mask)
4731{
4732 job_T *job;
4733 job_T *job_next;
4734
4735 for (job = first_job; job != NULL; job = job_next)
4736 {
4737 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004738 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004739 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004740 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004741 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004742 job_free_job(job);
4743 }
4744 }
4745}
4746
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004747/*
4748 * Allocate a job. Sets the refcount to one and sets options default.
4749 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02004750 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004751job_alloc(void)
4752{
4753 job_T *job;
4754
4755 job = (job_T *)alloc_clear(sizeof(job_T));
4756 if (job != NULL)
4757 {
4758 job->jv_refcount = 1;
4759 job->jv_stoponexit = vim_strsave((char_u *)"term");
4760
4761 if (first_job != NULL)
4762 {
4763 first_job->jv_prev = job;
4764 job->jv_next = first_job;
4765 }
4766 first_job = job;
4767 }
4768 return job;
4769}
4770
4771 void
4772job_set_options(job_T *job, jobopt_T *opt)
4773{
4774 if (opt->jo_set & JO_STOPONEXIT)
4775 {
4776 vim_free(job->jv_stoponexit);
4777 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4778 job->jv_stoponexit = NULL;
4779 else
4780 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4781 }
4782 if (opt->jo_set & JO_EXIT_CB)
4783 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004784 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004785 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004786 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004787 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004788 job->jv_exit_partial = NULL;
4789 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004790 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004791 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004792 job->jv_exit_partial = opt->jo_exit_partial;
4793 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004794 {
4795 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004796 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004797 }
4798 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004799 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004800 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004801 func_ref(job->jv_exit_cb);
4802 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004803 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004804 }
4805}
4806
4807/*
4808 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4809 */
4810 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004811job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004812{
4813 job_T *job;
4814
4815 for (job = first_job; job != NULL; job = job->jv_next)
4816 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4817 mch_stop_job(job, job->jv_stoponexit);
4818}
4819
4820/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004821 * Return TRUE when there is any job that has an exit callback and might exit,
4822 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004823 */
4824 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004825has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004826{
4827 job_T *job;
4828
4829 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004830 /* Only should check if the channel has been closed, if the channel is
4831 * open the job won't exit. */
4832 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004833 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004834 return TRUE;
4835 return FALSE;
4836}
4837
Bram Moolenaar97792de2016-10-15 18:36:49 +02004838#define MAX_CHECK_ENDED 8
4839
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004840/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004841 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004842 */
4843 void
4844job_check_ended(void)
4845{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004846 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004847
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004848 if (first_job == NULL)
4849 return;
4850
Bram Moolenaar97792de2016-10-15 18:36:49 +02004851 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004852 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004853 /* NOTE: mch_detect_ended_job() must only return a job of which the
4854 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004855 job_T *job = mch_detect_ended_job(first_job);
4856
4857 if (job == NULL)
4858 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004859 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004860 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004861
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004862 if (channel_need_redraw)
4863 {
4864 channel_need_redraw = FALSE;
4865 redraw_after_callback();
4866 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004867}
4868
4869/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02004870 * Create a job and return it. Implements job_start().
4871 * The returned job has a refcount of one.
4872 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004873 */
4874 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004875job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004876{
4877 job_T *job;
4878 char_u *cmd = NULL;
4879#if defined(UNIX)
4880# define USE_ARGV
4881 char **argv = NULL;
4882 int argc = 0;
4883#else
4884 garray_T ga;
4885#endif
4886 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004887 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004888
4889 job = job_alloc();
4890 if (job == NULL)
4891 return NULL;
4892
4893 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004894#ifndef USE_ARGV
4895 ga_init2(&ga, (int)sizeof(char*), 20);
4896#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004897
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004898 if (opt_arg != NULL)
4899 opt = *opt_arg;
4900 else
4901 {
4902 /* Default mode is NL. */
4903 clear_job_options(&opt);
4904 opt.jo_mode = MODE_NL;
4905 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004906 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4907 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02004908 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004909 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004910
4911 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004912 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004913 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4914 && opt.jo_io[part] == JIO_FILE
4915 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4916 || *opt.jo_io_name[part] == NUL))
4917 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004918 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004919 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004920 }
4921
4922 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4923 {
4924 buf_T *buf = NULL;
4925
4926 /* check that we can find the buffer before starting the job */
4927 if (opt.jo_set & JO_IN_BUF)
4928 {
4929 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4930 if (buf == NULL)
4931 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4932 }
4933 else if (!(opt.jo_set & JO_IN_NAME))
4934 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004935 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004936 }
4937 else
4938 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4939 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004940 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004941 if (buf->b_ml.ml_mfp == NULL)
4942 {
4943 char_u numbuf[NUMBUFLEN];
4944 char_u *s;
4945
4946 if (opt.jo_set & JO_IN_BUF)
4947 {
4948 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4949 s = numbuf;
4950 }
4951 else
4952 s = opt.jo_io_name[PART_IN];
4953 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004954 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004955 }
4956 job->jv_in_buf = buf;
4957 }
4958
4959 job_set_options(job, &opt);
4960
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004961 if (argvars[0].v_type == VAR_STRING)
4962 {
4963 /* Command is a string. */
4964 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004965 if (cmd == NULL || *cmd == NUL)
4966 {
4967 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004968 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004969 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004970#ifdef USE_ARGV
4971 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004972 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004973 argv[argc] = NULL;
4974#endif
4975 }
4976 else if (argvars[0].v_type != VAR_LIST
4977 || argvars[0].vval.v_list == NULL
4978 || argvars[0].vval.v_list->lv_len < 1)
4979 {
4980 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004981 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004982 }
4983 else
4984 {
4985 list_T *l = argvars[0].vval.v_list;
4986 listitem_T *li;
4987 char_u *s;
4988
4989#ifdef USE_ARGV
4990 /* Pass argv[] to mch_call_shell(). */
4991 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4992 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004993 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004994#endif
4995 for (li = l->lv_first; li != NULL; li = li->li_next)
4996 {
4997 s = get_tv_string_chk(&li->li_tv);
4998 if (s == NULL)
4999 goto theend;
5000#ifdef USE_ARGV
5001 argv[argc++] = (char *)s;
5002#else
5003 /* Only escape when needed, double quotes are not always allowed. */
5004 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
5005 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005006# ifdef WIN32
5007 int old_ssl = p_ssl;
5008
5009 /* This is using CreateProcess, not cmd.exe. Always use
5010 * double quote and backslashes. */
5011 p_ssl = 0;
5012# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005013 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005014# ifdef WIN32
5015 p_ssl = old_ssl;
5016# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005017 if (s == NULL)
5018 goto theend;
5019 ga_concat(&ga, s);
5020 vim_free(s);
5021 }
5022 else
5023 ga_concat(&ga, s);
5024 if (li->li_next != NULL)
5025 ga_append(&ga, ' ');
5026#endif
5027 }
5028#ifdef USE_ARGV
5029 argv[argc] = NULL;
5030#else
5031 cmd = ga.ga_data;
5032#endif
5033 }
5034
5035#ifdef USE_ARGV
5036 if (ch_log_active())
5037 {
5038 garray_T ga;
5039 int i;
5040
5041 ga_init2(&ga, (int)sizeof(char), 200);
5042 for (i = 0; i < argc; ++i)
5043 {
5044 if (i > 0)
5045 ga_concat(&ga, (char_u *)" ");
5046 ga_concat(&ga, (char_u *)argv[i]);
5047 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005048 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005049 ga_clear(&ga);
5050 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005051 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005052#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005053 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005054 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005055#endif
5056
5057 /* If the channel is reading from a buffer, write lines now. */
5058 if (job->jv_channel != NULL)
5059 channel_write_in(job->jv_channel);
5060
5061theend:
5062#ifdef USE_ARGV
5063 vim_free(argv);
5064#else
5065 vim_free(ga.ga_data);
5066#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005067 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005068 return job;
5069}
5070
5071/*
5072 * Get the status of "job" and invoke the exit callback when needed.
5073 * The returned string is not allocated.
5074 */
5075 char *
5076job_status(job_T *job)
5077{
5078 char *result;
5079
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005080 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005081 /* No need to check, dead is dead. */
5082 result = "dead";
5083 else if (job->jv_status == JOB_FAILED)
5084 result = "fail";
5085 else
5086 {
5087 result = mch_job_status(job);
5088 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005089 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005090 }
5091 return result;
5092}
5093
Bram Moolenaar8950a562016-03-12 15:22:55 +01005094/*
5095 * Implementation of job_info().
5096 */
5097 void
5098job_info(job_T *job, dict_T *dict)
5099{
5100 dictitem_T *item;
5101 varnumber_T nr;
5102
5103 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5104
5105 item = dictitem_alloc((char_u *)"channel");
5106 if (item == NULL)
5107 return;
5108 item->di_tv.v_lock = 0;
5109 item->di_tv.v_type = VAR_CHANNEL;
5110 item->di_tv.vval.v_channel = job->jv_channel;
5111 if (job->jv_channel != NULL)
5112 ++job->jv_channel->ch_refcount;
5113 if (dict_add(dict, item) == FAIL)
5114 dictitem_free(item);
5115
5116#ifdef UNIX
5117 nr = job->jv_pid;
5118#else
5119 nr = job->jv_proc_info.dwProcessId;
5120#endif
5121 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005122 dict_add_nr_str(dict, "tty", 0L,
5123 job->jv_tty_name != NULL ? job->jv_tty_name : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005124
5125 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005126 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005127 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5128}
5129
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005130/*
5131 * Send a signal to "job". Implements job_stop().
5132 * When "type" is not NULL use this for the type.
5133 * Otherwise use argvars[1] for the type.
5134 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005135 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005136job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005137{
5138 char_u *arg;
5139
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005140 if (type != NULL)
5141 arg = (char_u *)type;
5142 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005143 arg = (char_u *)"";
5144 else
5145 {
5146 arg = get_tv_string_chk(&argvars[1]);
5147 if (arg == NULL)
5148 {
5149 EMSG(_(e_invarg));
5150 return 0;
5151 }
5152 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005153 if (job->jv_status == JOB_FAILED)
5154 {
5155 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5156 return 0;
5157 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005158 if (job->jv_status == JOB_ENDED)
5159 {
5160 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5161 return 0;
5162 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005163 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005164 if (mch_stop_job(job, arg) == FAIL)
5165 return 0;
5166
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005167 /* Assume that only "kill" will kill the job. */
5168 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005169 job->jv_channel->ch_job_killed = TRUE;
5170
5171 /* We don't try freeing the job, obviously the caller still has a
5172 * reference to it. */
5173 return 1;
5174}
5175
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005176#endif /* FEAT_JOB_CHANNEL */