blob: 5168f7410d7def0eb57f2ddf99e1ef5d74101257 [file] [log] [blame]
Bram Moolenaare0874f82016-01-24 20:36:41 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
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
22/* Note: when making changes here also adjust configure.in. */
23#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 Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
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
135ch_log_active()
136{
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 Moolenaar77073442016-02-13 23:23:53 +0100162ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100170 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171 }
172}
173
174 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100175ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176{
177 if (log_fd != NULL)
178 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100181 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100183 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184 }
185}
186
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100188ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100192 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100196 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197 }
198}
199
200 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100201ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100209 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 }
211}
212
213 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100214ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215{
216 if (log_fd != NULL)
217 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100222 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223 }
224}
225
226 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100227ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100228{
229 if (log_fd != NULL)
230 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100235 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236 }
237}
238
239 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100240ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100241{
242 if (log_fd != NULL)
243 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100248 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100249 }
250}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100251
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252#ifdef _WIN32
253# undef PERROR
254# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
255 (char_u *)msg, (char_u *)strerror_win32(errno))
256
257 static char *
258strerror_win32(int eno)
259{
260 static LPVOID msgbuf = NULL;
261 char_u *ptr;
262
263 if (msgbuf)
264 LocalFree(msgbuf);
265 FormatMessage(
266 FORMAT_MESSAGE_ALLOCATE_BUFFER |
267 FORMAT_MESSAGE_FROM_SYSTEM |
268 FORMAT_MESSAGE_IGNORE_INSERTS,
269 NULL,
270 eno,
271 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
272 (LPTSTR) &msgbuf,
273 0,
274 NULL);
275 /* chomp \r or \n */
276 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
277 switch (*ptr)
278 {
279 case '\r':
280 STRMOVE(ptr, ptr + 1);
281 ptr--;
282 break;
283 case '\n':
284 if (*(ptr + 1) == '\0')
285 *ptr = '\0';
286 else
287 *ptr = ' ';
288 break;
289 }
290 return msgbuf;
291}
292#endif
293
Bram Moolenaar77073442016-02-13 23:23:53 +0100294/*
295 * The list of all allocated channels.
296 */
297static channel_T *first_channel = NULL;
298static int next_ch_id = 0;
299
300/*
301 * Allocate a new channel. The refcount is set to 1.
302 * The channel isn't actually used until it is opened.
303 * Returns NULL if out of memory.
304 */
305 channel_T *
306add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100307{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100308 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100309 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100310
Bram Moolenaar77073442016-02-13 23:23:53 +0100311 if (channel == NULL)
312 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313
Bram Moolenaar77073442016-02-13 23:23:53 +0100314 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100315 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100316
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100318 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100322#endif
323#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100324 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100325#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100326 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100327 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100328
Bram Moolenaar77073442016-02-13 23:23:53 +0100329 if (first_channel != NULL)
330 {
331 first_channel->ch_prev = channel;
332 channel->ch_next = first_channel;
333 }
334 first_channel = channel;
335
336 channel->ch_refcount = 1;
337 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100338}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100339
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100340/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100341 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100342 * Return TRUE if "channel" has a callback and the associated job wasn't
343 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100344 */
345 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100347{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100349 int has_out_msg;
350 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100351
352 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100353 if (channel->ch_job_killed && channel->ch_job == NULL)
354 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100355
356 /* If there is a close callback it may still need to be invoked. */
357 if (channel->ch_close_cb != NULL)
358 return TRUE;
359
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200360 /* If reading from or a buffer it's still useful. */
361 if (channel->ch_part[PART_IN].ch_buffer != NULL)
362 return TRUE;
363
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 /* If there is no callback then nobody can get readahead. If the fd is
365 * closed and there is no readahead then the callback won't be called. */
366 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
367 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
368 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100369 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
370 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
371 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
372 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
373 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
374 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100375 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100376 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200377 || ((channel->ch_part[PART_OUT].ch_callback != NULL
378 || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg)
379 || ((channel->ch_part[PART_ERR].ch_callback != NULL
380 || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381}
382
383/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200384 * Close a channel and free all its resources.
385 */
386 static void
387channel_free_contents(channel_T *channel)
388{
389 channel_close(channel, TRUE);
390 channel_clear(channel);
391 ch_log(channel, "Freeing channel");
392}
393
394 static void
395channel_free_channel(channel_T *channel)
396{
397 if (channel->ch_next != NULL)
398 channel->ch_next->ch_prev = channel->ch_prev;
399 if (channel->ch_prev == NULL)
400 first_channel = channel->ch_next;
401 else
402 channel->ch_prev->ch_next = channel->ch_next;
403 vim_free(channel);
404}
405
406 static void
407channel_free(channel_T *channel)
408{
409 if (!in_free_unref_items)
410 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200411 if (safe_to_invoke_callback == 0)
412 {
413 channel->ch_to_be_freed = TRUE;
414 }
415 else
416 {
417 channel_free_contents(channel);
418 channel_free_channel(channel);
419 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200420 }
421}
422
423/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100424 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100425 * possible, there is no callback to be invoked or the associated job was
426 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100427 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100428 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100429 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100430channel_may_free(channel_T *channel)
431{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100432 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100435 return TRUE;
436 }
437 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438}
439
440/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100441 * Decrement the reference count on "channel" and maybe free it when it goes
442 * down to zero. Don't free it if there is a pending action.
443 * Returns TRUE when the channel is no longer referenced.
444 */
445 int
446channel_unref(channel_T *channel)
447{
448 if (channel != NULL && --channel->ch_refcount <= 0)
449 return channel_may_free(channel);
450 return FALSE;
451}
452
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200453 int
454free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100455{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200456 int did_free = FALSE;
457 channel_T *ch;
458
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200459 /* This is invoked from the garbage collector, which only runs at a safe
460 * point. */
461 ++safe_to_invoke_callback;
462
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200463 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200464 if (!channel_still_useful(ch)
465 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 {
467 /* Free the channel and ordinary items it contains, but don't
468 * recurse into Lists, Dictionaries etc. */
469 channel_free_contents(ch);
470 did_free = TRUE;
471 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200472
473 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200474 return did_free;
475}
476
477 void
478free_unused_channels(int copyID, int mask)
479{
480 channel_T *ch;
481 channel_T *ch_next;
482
483 for (ch = first_channel; ch != NULL; ch = ch_next)
484 {
485 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200486 if (!channel_still_useful(ch)
487 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200488 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200489 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200490 channel_free_channel(ch);
491 }
492 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100493}
494
Bram Moolenaard04a0202016-01-26 23:30:18 +0100495#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100496
497#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
498 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100499channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100500{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100501 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100502 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100503
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100505 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100506 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100507 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200508 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100509}
510#endif
511
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100514 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100515#ifdef FEAT_GUI_X11
516 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200517messageFromServer(XtPointer clientData,
518 int *unused1 UNUSED,
519 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100521 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100526# if GTK_CHECK_VERSION(3,0,0)
527 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200528messageFromServer(GIOChannel *unused1 UNUSED,
529 GIOCondition unused2 UNUSED,
530 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100531{
532 channel_read_fd(GPOINTER_TO_INT(clientData));
533 return TRUE; /* Return FALSE instead in case the event source is to
534 * be removed after this function returns. */
535}
536# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100537 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538messageFromServer(gpointer clientData,
539 gint unused1 UNUSED,
540 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100541{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100542 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100543}
Bram Moolenaar98921892016-02-23 17:14:37 +0100544# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100545#endif
546
547 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100548channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100549{
Bram Moolenaarde279892016-03-11 22:19:44 +0100550 if (!CH_HAS_GUI)
551 return;
552
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100553# ifdef FEAT_GUI_X11
554 /* Tell notifier we are interested in being called
555 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100556 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
557 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100558 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100559 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200561 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100562 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# else
564# ifdef FEAT_GUI_GTK
565 /* Tell gdk we are interested in being called when there
566 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100567 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100568# if GTK_CHECK_VERSION(3,0,0)
569 {
570 GIOChannel *chnnl = g_io_channel_unix_new(
571 (gint)channel->ch_part[part].ch_fd);
572
573 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
574 chnnl,
575 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200576 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100577 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
578
579 g_io_channel_unref(chnnl);
580 }
581# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100582 channel->ch_part[part].ch_inputHandler = gdk_input_add(
583 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100584 (GdkInputCondition)
585 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200586 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100587 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100588# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100589# endif
590# endif
591}
592
Bram Moolenaarde279892016-03-11 22:19:44 +0100593 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100594channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100595{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100596 if (channel->CH_SOCK_FD != INVALID_FD)
597 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100598 if (channel->CH_OUT_FD != INVALID_FD)
599 channel_gui_register_one(channel, PART_OUT);
600 if (channel->CH_ERR_FD != INVALID_FD)
601 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100602}
603
604/*
605 * Register any of our file descriptors with the GUI event handling system.
606 * Called when the GUI has started.
607 */
608 void
609channel_gui_register_all(void)
610{
Bram Moolenaar77073442016-02-13 23:23:53 +0100611 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612
Bram Moolenaar77073442016-02-13 23:23:53 +0100613 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100614 channel_gui_register(channel);
615}
616
617 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100618channel_gui_unregister_one(channel_T *channel, int part)
619{
620# ifdef FEAT_GUI_X11
621 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
622 {
623 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
624 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
625 }
626# else
627# ifdef FEAT_GUI_GTK
628 if (channel->ch_part[part].ch_inputHandler != 0)
629 {
630# if GTK_CHECK_VERSION(3,0,0)
631 g_source_remove(channel->ch_part[part].ch_inputHandler);
632# else
633 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
634# endif
635 channel->ch_part[part].ch_inputHandler = 0;
636 }
637# endif
638# endif
639}
640
641 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100642channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100643{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100644 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100645
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100646 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100647 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648}
649
650#endif
651
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100652static char *e_cannot_connect = N_("E902: Cannot connect to port");
653
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100655 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100656 * "waittime" is the time in msec to wait for the connection.
657 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100658 * Returns the channel for success.
659 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100661 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100662channel_open(
663 char *hostname,
664 int port_in,
665 int waittime,
666 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100668 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100669 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100670 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100671#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100672 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100673 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100674#else
675 int port = port_in;
676#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100678 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100680#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681 channel_init_winsock();
682#endif
683
Bram Moolenaar77073442016-02-13 23:23:53 +0100684 channel = add_channel();
685 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689 }
690
691 /* Get the server internet address and put into addr structure */
692 /* fill in the socket address structure and connect to server */
693 vim_memset((char *)&server, 0, sizeof(server));
694 server.sin_family = AF_INET;
695 server.sin_port = htons(port);
696 if ((host = gethostbyname(hostname)) == NULL)
697 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100699 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100700 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100701 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100702 }
703 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
704
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100705 /* On Mac and Solaris a zero timeout almost never works. At least wait
706 * one millisecond. Let's do it for all systems, because we don't know why
707 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 if (waittime == 0)
709 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100710
711 /*
712 * For Unix we need to call connect() again after connect() failed.
713 * On Win32 one time is sufficient.
714 */
715 while (TRUE)
716 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100717 long elapsed_msec = 0;
718 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100719
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100722 sd = socket(AF_INET, SOCK_STREAM, 0);
723 if (sd == -1)
724 {
725 ch_error(channel, "in socket() in channel_open().");
726 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100727 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100728 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100729 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 if (waittime >= 0)
732 {
733 /* Make connect() non-blocking. */
734 if (
735#ifdef _WIN32
736 ioctlsocket(sd, FIONBIO, &val) < 0
737#else
738 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
739#endif
740 )
741 {
742 SOCK_ERRNO;
743 ch_errorn(channel,
744 "channel_open: Connect failed with errno %d", errno);
745 sock_close(sd);
746 channel_free(channel);
747 return NULL;
748 }
749 }
750
751 /* Try connecting to the server. */
752 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
753 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 if (ret == 0)
756 /* The connection could be established. */
757 break;
758
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100759 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100760 if (waittime < 0 || (errno != EWOULDBLOCK
761 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100762#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100764#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 ))
766 {
767 ch_errorn(channel,
768 "channel_open: Connect failed with errno %d", errno);
769 PERROR(_(e_cannot_connect));
770 sock_close(sd);
771 channel_free(channel);
772 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100773 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100774
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100775 /* Limit the waittime to 50 msec. If it doesn't work within this
776 * time we close the socket and try creating it again. */
777 waitnow = waittime > 50 ? 50 : waittime;
778
Bram Moolenaar045a2842016-03-08 22:33:07 +0100779 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100780 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781#ifndef WIN32
782 if (errno != ECONNREFUSED)
783#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 {
785 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100786 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100787 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100788#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100789 int so_error = 0;
790 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 struct timeval start_tv;
792 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100793#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100794 FD_ZERO(&rfds);
795 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100796 FD_ZERO(&wfds);
797 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100798
Bram Moolenaar562ca712016-03-09 21:50:05 +0100799 tv.tv_sec = waitnow / 1000;
800 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100801#ifndef WIN32
802 gettimeofday(&start_tv, NULL);
803#endif
804 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100805 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100807
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 if (ret < 0)
809 {
810 SOCK_ERRNO;
811 ch_errorn(channel,
812 "channel_open: Connect failed with errno %d", errno);
813 PERROR(_(e_cannot_connect));
814 sock_close(sd);
815 channel_free(channel);
816 return NULL;
817 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818
Bram Moolenaare081e212016-02-28 22:33:46 +0100819#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100820 /* On Win32: select() is expected to work and wait for up to
821 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100822 if (FD_ISSET(sd, &wfds))
823 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100824 elapsed_msec = waitnow;
825 if (waittime > 1 && elapsed_msec < waittime)
826 {
827 waittime -= elapsed_msec;
828 continue;
829 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100830#else
831 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100832 * After putting the socket in non-blocking mode, connect() will
833 * return EINPROGRESS, select() will not wait (as if writing is
834 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100835 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100837 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100838 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100839 {
840 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100841 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100842 if (ret < 0 || (so_error != 0
843 && so_error != EWOULDBLOCK
844 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100845# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100846 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100847# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 ))
849 {
850 ch_errorn(channel,
851 "channel_open: Connect failed with errno %d",
852 so_error);
853 PERROR(_(e_cannot_connect));
854 sock_close(sd);
855 channel_free(channel);
856 return NULL;
857 }
858 }
859
Bram Moolenaar045a2842016-03-08 22:33:07 +0100860 if (FD_ISSET(sd, &wfds) && so_error == 0)
861 /* Did not detect an error, connection is established. */
862 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100863
Bram Moolenaar045a2842016-03-08 22:33:07 +0100864 gettimeofday(&end_tv, NULL);
865 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
866 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100867#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100868 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100869
870#ifndef WIN32
871 if (waittime > 1 && elapsed_msec < waittime)
872 {
873 /* The port isn't ready but we also didn't get an error.
874 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100875 * yet. Select() may return early, wait until the remaining
876 * "waitnow" and try again. */
877 waitnow -= elapsed_msec;
878 waittime -= elapsed_msec;
879 if (waitnow > 0)
880 {
881 mch_delay((long)waitnow, TRUE);
882 ui_breakcheck();
883 waittime -= waitnow;
884 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100885 if (!got_int)
886 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100887 if (waittime <= 0)
888 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100889 waittime = 1;
890 continue;
891 }
892 /* we were interrupted, behave as if timed out */
893 }
894#endif
895
896 /* We timed out. */
897 ch_error(channel, "Connection timed out");
898 sock_close(sd);
899 channel_free(channel);
900 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100901 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100902
Bram Moolenaar045a2842016-03-08 22:33:07 +0100903 ch_log(channel, "Connection made");
904
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100905 if (waittime >= 0)
906 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100907#ifdef _WIN32
908 val = 0;
909 ioctlsocket(sd, FIONBIO, &val);
910#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100911 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100912#endif
913 }
914
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100915 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100916 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100917 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
918 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100919
920#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100921 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100922#endif
923
Bram Moolenaar77073442016-02-13 23:23:53 +0100924 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100925}
926
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100927/*
928 * Implements ch_open().
929 */
930 channel_T *
931channel_open_func(typval_T *argvars)
932{
933 char_u *address;
934 char_u *p;
935 char *rest;
936 int port;
937 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200938 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939
940 address = get_tv_string(&argvars[0]);
941 if (argvars[1].v_type != VAR_UNKNOWN
942 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
943 {
944 EMSG(_(e_invarg));
945 return NULL;
946 }
947
948 /* parse address */
949 p = vim_strchr(address, ':');
950 if (p == NULL)
951 {
952 EMSG2(_(e_invarg2), address);
953 return NULL;
954 }
955 *p++ = NUL;
956 port = strtol((char *)p, &rest, 10);
957 if (*address == NUL || port <= 0 || *rest != NUL)
958 {
959 p[-1] = ':';
960 EMSG2(_(e_invarg2), address);
961 return NULL;
962 }
963
964 /* parse options */
965 clear_job_options(&opt);
966 opt.jo_mode = MODE_JSON;
967 opt.jo_timeout = 2000;
968 if (get_job_options(&argvars[1], &opt,
969 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200970 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100971 if (opt.jo_timeout < 0)
972 {
973 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200974 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100975 }
976
977 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
978 if (channel != NULL)
979 {
980 opt.jo_set = JO_ALL;
981 channel_set_options(channel, &opt);
982 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200983theend:
984 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100985 return channel;
986}
987
Bram Moolenaarde279892016-03-11 22:19:44 +0100988 static void
989may_close_part(sock_T *fd)
990{
991 if (*fd != INVALID_FD)
992 {
993 fd_close(*fd);
994 *fd = INVALID_FD;
995 }
996}
997
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100998 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100999channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001000{
Bram Moolenaarde279892016-03-11 22:19:44 +01001001 if (in != INVALID_FD)
1002 {
1003 may_close_part(&channel->CH_IN_FD);
1004 channel->CH_IN_FD = in;
1005 }
1006 if (out != INVALID_FD)
1007 {
1008# if defined(FEAT_GUI)
1009 channel_gui_unregister_one(channel, PART_OUT);
1010# endif
1011 may_close_part(&channel->CH_OUT_FD);
1012 channel->CH_OUT_FD = out;
1013# if defined(FEAT_GUI)
1014 channel_gui_register_one(channel, PART_OUT);
1015# endif
1016 }
1017 if (err != INVALID_FD)
1018 {
1019# if defined(FEAT_GUI)
1020 channel_gui_unregister_one(channel, PART_ERR);
1021# endif
1022 may_close_part(&channel->CH_ERR_FD);
1023 channel->CH_ERR_FD = err;
1024# if defined(FEAT_GUI)
1025 channel_gui_register_one(channel, PART_ERR);
1026# endif
1027 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001028}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001029
Bram Moolenaard6051b52016-02-28 15:49:03 +01001030/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001031 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001032 * This does not keep a refcount, when the job is freed ch_job is cleared.
1033 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001034 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001035channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001036{
Bram Moolenaar77073442016-02-13 23:23:53 +01001037 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001038
1039 channel_set_options(channel, options);
1040
1041 if (job->jv_in_buf != NULL)
1042 {
1043 chanpart_T *in_part = &channel->ch_part[PART_IN];
1044
1045 in_part->ch_buffer = job->jv_in_buf;
1046 ch_logs(channel, "reading from buffer '%s'",
1047 (char *)in_part->ch_buffer->b_ffname);
1048 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001049 {
1050 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1051 {
1052 /* Special mode: send last-but-one line when appending a line
1053 * to the buffer. */
1054 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001055 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001056 in_part->ch_buf_top =
1057 in_part->ch_buffer->b_ml.ml_line_count + 1;
1058 }
1059 else
1060 in_part->ch_buf_top = options->jo_in_top;
1061 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001062 else
1063 in_part->ch_buf_top = 1;
1064 if (options->jo_set & JO_IN_BOT)
1065 in_part->ch_buf_bot = options->jo_in_bot;
1066 else
1067 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1068 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001069}
1070
1071/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001072 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001073 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001074 */
1075 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001076find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001077{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001078 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001079 buf_T *save_curbuf = curbuf;
1080
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001081 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001082 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001083 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001084 if (buf == NULL)
1085 buf = buflist_findname_exp(name);
1086 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001087 if (buf == NULL)
1088 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001089 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001090 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001091 if (buf == NULL)
1092 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001093 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001094 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001095#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001096 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1097 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001098#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001099 if (curbuf->b_ml.ml_mfp == NULL)
1100 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001101 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1102 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001103 changed_bytes(1, 0);
1104 curbuf = save_curbuf;
1105 }
1106
1107 return buf;
1108}
1109
1110/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001112 */
1113 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001115{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001116 int part;
1117 char_u **cbp;
1118 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001119
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001120 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001121 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001122 channel->ch_part[part].ch_mode = opt->jo_mode;
1123 if (opt->jo_set & JO_IN_MODE)
1124 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1125 if (opt->jo_set & JO_OUT_MODE)
1126 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1127 if (opt->jo_set & JO_ERR_MODE)
1128 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001129
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001130 if (opt->jo_set & JO_TIMEOUT)
1131 for (part = PART_SOCK; part <= PART_IN; ++part)
1132 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1133 if (opt->jo_set & JO_OUT_TIMEOUT)
1134 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1135 if (opt->jo_set & JO_ERR_TIMEOUT)
1136 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001137 if (opt->jo_set & JO_BLOCK_WRITE)
1138 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139
1140 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001141 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001143 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001144 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001145 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001146 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1147 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001148 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001150 *pp = opt->jo_partial;
1151 if (*pp != NULL)
1152 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001153 }
1154 if (opt->jo_set & JO_OUT_CALLBACK)
1155 {
1156 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001157 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001158 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001159 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001160 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1161 *cbp = vim_strsave(opt->jo_out_cb);
1162 else
1163 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001164 *pp = opt->jo_out_partial;
1165 if (*pp != NULL)
1166 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001167 }
1168 if (opt->jo_set & JO_ERR_CALLBACK)
1169 {
1170 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001171 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001172 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001173 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001174 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1175 *cbp = vim_strsave(opt->jo_err_cb);
1176 else
1177 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001178 *pp = opt->jo_err_partial;
1179 if (*pp != NULL)
1180 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001181 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001182 if (opt->jo_set & JO_CLOSE_CALLBACK)
1183 {
1184 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001185 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001186 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001187 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001188 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1189 *cbp = vim_strsave(opt->jo_close_cb);
1190 else
1191 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001192 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001193 if (*pp != NULL)
1194 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001195 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001196
1197 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1198 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001199 buf_T *buf;
1200
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001201 /* writing output to a buffer. Default mode is NL. */
1202 if (!(opt->jo_set & JO_OUT_MODE))
1203 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001204 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001205 {
1206 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1207 if (buf == NULL)
1208 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1209 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001210 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001211 {
1212 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1213 }
1214 if (buf != NULL)
1215 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001216 if (opt->jo_set & JO_OUT_MODIFIABLE)
1217 channel->ch_part[PART_OUT].ch_nomodifiable =
1218 !opt->jo_modifiable[PART_OUT];
1219
1220 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1221 {
1222 EMSG(_(e_modifiable));
1223 }
1224 else
1225 {
1226 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001227 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001228 channel->ch_part[PART_OUT].ch_buffer = buf;
1229 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001231 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001232
1233 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1234 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1235 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1236 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001237 buf_T *buf;
1238
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001239 /* writing err to a buffer. Default mode is NL. */
1240 if (!(opt->jo_set & JO_ERR_MODE))
1241 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1242 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001243 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001244 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001245 {
1246 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1247 if (buf == NULL)
1248 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1249 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001250 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1252 if (buf != NULL)
1253 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001254 if (opt->jo_set & JO_ERR_MODIFIABLE)
1255 channel->ch_part[PART_ERR].ch_nomodifiable =
1256 !opt->jo_modifiable[PART_ERR];
1257 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1258 {
1259 EMSG(_(e_modifiable));
1260 }
1261 else
1262 {
1263 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001264 (char *)buf->b_ffname);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001265 channel->ch_part[PART_ERR].ch_buffer = buf;
1266 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001267 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001268 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001269
1270 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1271 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1272 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001273}
1274
1275/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001276 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001277 */
1278 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001279channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001280 channel_T *channel,
1281 int part,
1282 char_u *callback,
1283 partial_T *partial,
1284 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001285{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001286 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001287 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1288
1289 if (item != NULL)
1290 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001291 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001292 item->cq_partial = partial;
1293 if (partial != NULL)
1294 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001295 item->cq_seq_nr = id;
1296 item->cq_prev = head->cq_prev;
1297 head->cq_prev = item;
1298 item->cq_next = NULL;
1299 if (item->cq_prev == NULL)
1300 head->cq_next = item;
1301 else
1302 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001303 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001304}
1305
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001306 static void
1307write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1308{
1309 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001310 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001311 char_u *p;
1312
Bram Moolenaar655da312016-05-28 22:22:34 +02001313 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001314 if ((p = alloc(len + 2)) == NULL)
1315 return;
1316 STRCPY(p, line);
1317 p[len] = NL;
1318 p[len + 1] = NUL;
1319 channel_send(channel, PART_IN, p, "write_buf_line()");
1320 vim_free(p);
1321}
1322
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001323/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001324 * Return TRUE if "channel" can be written to.
1325 * Returns FALSE if the input is closed or the write would block.
1326 */
1327 static int
1328can_write_buf_line(channel_T *channel)
1329{
1330 chanpart_T *in_part = &channel->ch_part[PART_IN];
1331
1332 if (in_part->ch_fd == INVALID_FD)
1333 return FALSE; /* pipe was closed */
1334
1335 /* for testing: block every other attempt to write */
1336 if (in_part->ch_block_write == 1)
1337 in_part->ch_block_write = -1;
1338 else if (in_part->ch_block_write == -1)
1339 in_part->ch_block_write = 1;
1340
1341 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1342#ifndef WIN32
1343 {
1344# if defined(HAVE_SELECT)
1345 struct timeval tval;
1346 fd_set wfds;
1347 int ret;
1348
1349 FD_ZERO(&wfds);
1350 FD_SET((int)in_part->ch_fd, &wfds);
1351 tval.tv_sec = 0;
1352 tval.tv_usec = 0;
1353 for (;;)
1354 {
1355 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1356# ifdef EINTR
1357 SOCK_ERRNO;
1358 if (ret == -1 && errno == EINTR)
1359 continue;
1360# endif
1361 if (ret <= 0 || in_part->ch_block_write == 1)
1362 {
1363 if (ret > 0)
1364 ch_log(channel, "FAKED Input not ready for writing");
1365 else
1366 ch_log(channel, "Input not ready for writing");
1367 return FALSE;
1368 }
1369 break;
1370 }
1371# else
1372 struct pollfd fds;
1373
1374 fds.fd = in_part->ch_fd;
1375 fds.events = POLLOUT;
1376 if (poll(&fds, 1, 0) <= 0)
1377 {
1378 ch_log(channel, "Input not ready for writing");
1379 return FALSE;
1380 }
1381 if (in_part->ch_block_write == 1)
1382 {
1383 ch_log(channel, "FAKED Input not ready for writing");
1384 return FALSE;
1385 }
1386# endif
1387 }
1388#endif
1389 return TRUE;
1390}
1391
1392/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001393 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001394 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001395 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001396channel_write_in(channel_T *channel)
1397{
1398 chanpart_T *in_part = &channel->ch_part[PART_IN];
1399 linenr_T lnum;
1400 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001401 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001402
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001403 if (buf == NULL || in_part->ch_buf_append)
1404 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001405 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1406 {
1407 /* buffer was wiped out or unloaded */
1408 in_part->ch_buffer = NULL;
1409 return;
1410 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001411
1412 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1413 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1414 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 if (!can_write_buf_line(channel))
1416 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001417 write_buf_line(buf, lnum, channel);
1418 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001419 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001420
1421 if (written == 1)
1422 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1423 else if (written > 1)
1424 ch_logn(channel, "written %d lines to channel", written);
1425
Bram Moolenaar014069a2016-03-03 22:51:40 +01001426 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001427 if (lnum > buf->b_ml.ml_line_count)
1428 {
1429 /* Writing is done, no longer need the buffer. */
1430 in_part->ch_buffer = NULL;
1431 ch_log(channel, "Finished writing all lines to channel");
1432 }
1433 else
1434 ch_logn(channel, "Still %d more lines to write",
1435 buf->b_ml.ml_line_count - lnum + 1);
1436}
1437
1438/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001439 * Handle buffer "buf" beeing freed, remove it from any channels.
1440 */
1441 void
1442channel_buffer_free(buf_T *buf)
1443{
1444 channel_T *channel;
1445 int part;
1446
1447 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1448 for (part = PART_SOCK; part <= PART_IN; ++part)
1449 {
1450 chanpart_T *ch_part = &channel->ch_part[part];
1451
1452 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001453 {
1454 ch_logs(channel, "%s buffer has been wiped out",
1455 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001456 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001457 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001458 }
1459}
1460
1461/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001462 * Write any lines waiting to be written to a channel.
1463 */
1464 void
1465channel_write_any_lines()
1466{
1467 channel_T *channel;
1468
1469 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1470 {
1471 chanpart_T *in_part = &channel->ch_part[PART_IN];
1472
1473 if (in_part->ch_buffer != NULL)
1474 {
1475 if (in_part->ch_buf_append)
1476 channel_write_new_lines(in_part->ch_buffer);
1477 else
1478 channel_write_in(channel);
1479 }
1480 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001481}
1482
1483/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001484 * Write appended lines above the last one in "buf" to the channel.
1485 */
1486 void
1487channel_write_new_lines(buf_T *buf)
1488{
1489 channel_T *channel;
1490 int found_one = FALSE;
1491
1492 /* There could be more than one channel for the buffer, loop over all of
1493 * them. */
1494 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1495 {
1496 chanpart_T *in_part = &channel->ch_part[PART_IN];
1497 linenr_T lnum;
1498 int written = 0;
1499
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001500 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001501 {
1502 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001503 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 found_one = TRUE;
1505 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1506 ++lnum)
1507 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001508 if (!can_write_buf_line(channel))
1509 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001510 write_buf_line(buf, lnum, channel);
1511 ++written;
1512 }
1513
1514 if (written == 1)
1515 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1516 else if (written > 1)
1517 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001518 if (lnum < buf->b_ml.ml_line_count)
1519 ch_logn(channel, "Still %d more lines to write",
1520 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001521
1522 in_part->ch_buf_bot = lnum;
1523 }
1524 }
1525 if (!found_one)
1526 buf->b_write_to_channel = FALSE;
1527}
1528
1529/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001530 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001531 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001532 */
1533 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001534invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1535 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001536{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001537 typval_T rettv;
1538 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001539
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001540 if (safe_to_invoke_callback == 0)
1541 EMSG("INTERNAL: Invoking callback when it is not safe");
1542
Bram Moolenaar77073442016-02-13 23:23:53 +01001543 argv[0].v_type = VAR_CHANNEL;
1544 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001545
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001546 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001547 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001548 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001549 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001550}
1551
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001552/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001553 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001554 * The caller must free it.
1555 * Returns NULL if there is nothing.
1556 */
1557 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001558channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001559{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001560 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001561 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001562 char_u *p;
1563
Bram Moolenaar77073442016-02-13 23:23:53 +01001564 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001565 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001566 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001567 p = node->rq_buffer;
1568 head->rq_next = node->rq_next;
1569 if (node->rq_next == NULL)
1570 head->rq_prev = NULL;
1571 else
1572 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001573 vim_free(node);
1574 return p;
1575}
1576
1577/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001578 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001579 */
1580 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001581channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001582{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001583 readq_T *head = &channel->ch_part[part].ch_head;
1584 readq_T *node = head->rq_next;
1585 long_u len = 1;
1586 char_u *res;
1587 char_u *p;
1588
1589 /* If there is only one buffer just get that one. */
1590 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1591 return channel_get(channel, part);
1592
1593 /* Concatenate everything into one buffer. */
1594 for (node = head->rq_next; node != NULL; node = node->rq_next)
1595 len += (long_u)STRLEN(node->rq_buffer);
1596 res = lalloc(len, TRUE);
1597 if (res == NULL)
1598 return NULL;
1599 *res = NUL;
1600 for (node = head->rq_next; node != NULL; node = node->rq_next)
1601 STRCAT(res, node->rq_buffer);
1602
1603 /* Free all buffers */
1604 do
1605 {
1606 p = channel_get(channel, part);
1607 vim_free(p);
1608 } while (p != NULL);
1609
1610 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001611}
1612
1613/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001614 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615 * Returns FAIL if that is not possible.
1616 */
1617 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001620 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001621 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001622 char_u *p;
1623
Bram Moolenaar77073442016-02-13 23:23:53 +01001624 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001625 return FAIL;
1626
Bram Moolenaar77073442016-02-13 23:23:53 +01001627 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1628 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001629 if (p == NULL)
1630 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001631 STRCPY(p, node->rq_buffer);
1632 STRCAT(p, node->rq_next->rq_buffer);
1633 vim_free(node->rq_next->rq_buffer);
1634 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001635
Bram Moolenaar77073442016-02-13 23:23:53 +01001636 /* dispose of the node and its buffer */
1637 head->rq_next = node->rq_next;
1638 head->rq_next->rq_prev = NULL;
1639 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001640 vim_free(node);
1641 return OK;
1642}
1643
1644/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001645 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001646 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001647 * Returns OK or FAIL.
1648 */
1649 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001650channel_save(channel_T *channel, int part, char_u *buf, int len,
1651 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001652{
1653 readq_T *node;
1654 readq_T *head = &channel->ch_part[part].ch_head;
1655 char_u *p;
1656 int i;
1657
1658 node = (readq_T *)alloc(sizeof(readq_T));
1659 if (node == NULL)
1660 return FAIL; /* out of memory */
1661 node->rq_buffer = alloc(len + 1);
1662 if (node->rq_buffer == NULL)
1663 {
1664 vim_free(node);
1665 return FAIL; /* out of memory */
1666 }
1667
1668 if (channel->ch_part[part].ch_mode == MODE_NL)
1669 {
1670 /* Drop any CR before a NL. */
1671 p = node->rq_buffer;
1672 for (i = 0; i < len; ++i)
1673 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1674 *p++ = buf[i];
1675 *p = NUL;
1676 }
1677 else
1678 {
1679 mch_memmove(node->rq_buffer, buf, len);
1680 node->rq_buffer[len] = NUL;
1681 }
1682
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001683 if (prepend)
1684 {
1685 /* preend node to the head of the queue */
1686 node->rq_next = head->rq_next;
1687 node->rq_prev = NULL;
1688 if (head->rq_next == NULL)
1689 head->rq_prev = node;
1690 else
1691 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001692 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001693 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001694 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001695 {
1696 /* append node to the tail of the queue */
1697 node->rq_next = NULL;
1698 node->rq_prev = head->rq_prev;
1699 if (head->rq_prev == NULL)
1700 head->rq_next = node;
1701 else
1702 head->rq_prev->rq_next = node;
1703 head->rq_prev = node;
1704 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001705
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001706 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001708 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001709 fprintf(log_fd, "'");
1710 if (fwrite(buf, len, 1, log_fd) != 1)
1711 return FAIL;
1712 fprintf(log_fd, "'\n");
1713 }
1714 return OK;
1715}
1716
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001717 static int
1718channel_fill(js_read_T *reader)
1719{
1720 channel_T *channel = (channel_T *)reader->js_cookie;
1721 int part = reader->js_cookie_arg;
1722 char_u *next = channel_get(channel, part);
1723 int unused;
1724 int len;
1725 char_u *p;
1726
1727 if (next == NULL)
1728 return FALSE;
1729
1730 unused = reader->js_end - reader->js_buf - reader->js_used;
1731 if (unused > 0)
1732 {
1733 /* Prepend unused text. */
1734 len = (int)STRLEN(next);
1735 p = alloc(unused + len + 1);
1736 if (p == NULL)
1737 {
1738 vim_free(next);
1739 return FALSE;
1740 }
1741 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1742 mch_memmove(p + unused, next, len + 1);
1743 vim_free(next);
1744 next = p;
1745 }
1746
1747 vim_free(reader->js_buf);
1748 reader->js_buf = next;
1749 reader->js_used = 0;
1750 return TRUE;
1751}
1752
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001753/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001754 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001755 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001756 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001757 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001758 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001759channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001760{
1761 js_read_T reader;
1762 typval_T listtv;
1763 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001764 chanpart_T *chanpart = &channel->ch_part[part];
1765 jsonq_T *head = &chanpart->ch_json_head;
1766 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001767 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001768
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001770 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001771
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001772 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001773 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001774 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001775 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001776 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001777
1778 /* When a message is incomplete we wait for a short while for more to
1779 * arrive. After the delay drop the input, otherwise a truncated string
1780 * or list will make us hang. */
1781 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001782 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001783 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001784 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001785 /* Only accept the response when it is a list with at least two
1786 * items. */
1787 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001788 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001789 if (listtv.v_type != VAR_LIST)
1790 ch_error(channel, "Did not receive a list, discarding");
1791 else
1792 ch_errorn(channel, "Expected list with two items, got %d",
1793 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001795 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 else
1797 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001798 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1799 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001800 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801 else
1802 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001803 item->jq_value = alloc_tv();
1804 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001805 {
1806 vim_free(item);
1807 clear_tv(&listtv);
1808 }
1809 else
1810 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001811 *item->jq_value = listtv;
1812 item->jq_prev = head->jq_prev;
1813 head->jq_prev = item;
1814 item->jq_next = NULL;
1815 if (item->jq_prev == NULL)
1816 head->jq_next = item;
1817 else
1818 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001819 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001820 }
1821 }
1822 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001823
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001824 if (status == OK)
1825 chanpart->ch_waiting = FALSE;
1826 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001827 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001828 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001829 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001830 /* First time encountering incomplete message, set a deadline of
1831 * 100 msec. */
1832 ch_log(channel, "Incomplete message - wait for more");
1833 reader.js_used = 0;
1834 chanpart->ch_waiting = TRUE;
1835#ifdef WIN32
1836 chanpart->ch_deadline = GetTickCount() + 100L;
1837#else
1838 gettimeofday(&chanpart->ch_deadline, NULL);
1839 chanpart->ch_deadline.tv_usec += 100 * 1000;
1840 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1841 {
1842 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1843 ++chanpart->ch_deadline.tv_sec;
1844 }
1845#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001846 }
1847 else
1848 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001849 int timeout;
1850#ifdef WIN32
1851 timeout = GetTickCount() > chanpart->ch_deadline;
1852#else
1853 {
1854 struct timeval now_tv;
1855
1856 gettimeofday(&now_tv, NULL);
1857 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1858 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1859 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1860 }
1861#endif
1862 if (timeout)
1863 {
1864 status = FAIL;
1865 chanpart->ch_waiting = FALSE;
1866 }
1867 else
1868 {
1869 reader.js_used = 0;
1870 ch_log(channel, "still waiting on incomplete message");
1871 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001872 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001873 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001874
1875 if (status == FAIL)
1876 {
1877 ch_error(channel, "Decoding failed - discarding input");
1878 ret = FALSE;
1879 chanpart->ch_waiting = FALSE;
1880 }
1881 else if (reader.js_buf[reader.js_used] != NUL)
1882 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001883 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001884 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001885 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1886 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001887 ret = status == MAYBE ? FALSE: TRUE;
1888 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001889 else
1890 ret = FALSE;
1891
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001892 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001893 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894}
1895
1896/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001897 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001898 */
1899 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001900remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001901{
Bram Moolenaar77073442016-02-13 23:23:53 +01001902 if (node->cq_prev == NULL)
1903 head->cq_next = node->cq_next;
1904 else
1905 node->cq_prev->cq_next = node->cq_next;
1906 if (node->cq_next == NULL)
1907 head->cq_prev = node->cq_prev;
1908 else
1909 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001910}
1911
1912/*
1913 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001914 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001915 */
1916 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001917remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001918{
Bram Moolenaar77073442016-02-13 23:23:53 +01001919 if (node->jq_prev == NULL)
1920 head->jq_next = node->jq_next;
1921 else
1922 node->jq_prev->jq_next = node->jq_next;
1923 if (node->jq_next == NULL)
1924 head->jq_prev = node->jq_prev;
1925 else
1926 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001927 vim_free(node);
1928}
1929
1930/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001931 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001932 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001933 * When "id" is zero or negative jut get the first message. But not the one
1934 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001935 * Return OK when found and return the value in "rettv".
1936 * Return FAIL otherwise.
1937 */
1938 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001939channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001940{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001941 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001942 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001943
Bram Moolenaar77073442016-02-13 23:23:53 +01001944 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001945 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001946 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001947 typval_T *tv = &l->lv_first->li_tv;
1948
1949 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001950 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001951 || tv->vval.v_number == 0
1952 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001953 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001954 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001955 if (tv->v_type == VAR_NUMBER)
1956 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001957 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001958 return OK;
1959 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001960 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001961 }
1962 return FAIL;
1963}
1964
Bram Moolenaarece61b02016-02-20 21:39:05 +01001965#define CH_JSON_MAX_ARGS 4
1966
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001967/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001968 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001969 * "argv[0]" is the command string.
1970 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001971 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001972 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001973channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001974{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001975 char_u *cmd = argv[0].vval.v_string;
1976 char_u *arg;
1977 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001978
Bram Moolenaarece61b02016-02-20 21:39:05 +01001979 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001980 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001981 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001982 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001983 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001984 return;
1985 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001986 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001987 if (arg == NULL)
1988 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001989
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001990 if (STRCMP(cmd, "ex") == 0)
1991 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001992 int save_called_emsg = called_emsg;
1993
1994 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001995 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001996 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001997 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001998 --emsg_silent;
1999 if (called_emsg)
2000 ch_logs(channel, "Ex command error: '%s'",
2001 (char *)get_vim_var_str(VV_ERRMSG));
2002 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002003 }
2004 else if (STRCMP(cmd, "normal") == 0)
2005 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002006 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002007
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002008 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002009 ea.arg = arg;
2010 ea.addr_count = 0;
2011 ea.forceit = TRUE; /* no mapping */
2012 ex_normal(&ea);
2013 }
2014 else if (STRCMP(cmd, "redraw") == 0)
2015 {
2016 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002017
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002018 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002019 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002020 ex_redraw(&ea);
2021 showruler(FALSE);
2022 setcursor();
2023 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002024#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002025 if (gui.in_use)
2026 {
2027 gui_update_cursor(FALSE, FALSE);
2028 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002029 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002030#endif
2031 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002032 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002033 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002034 int is_call = cmd[0] == 'c';
2035 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002036
Bram Moolenaarece61b02016-02-20 21:39:05 +01002037 if (argv[id_idx].v_type != VAR_UNKNOWN
2038 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002039 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002040 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002041 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002042 EMSG("E904: last argument for expr/call must be a number");
2043 }
2044 else if (is_call && argv[2].v_type != VAR_LIST)
2045 {
2046 ch_error(channel, "third argument for call must be a list");
2047 if (p_verbose > 2)
2048 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002049 }
2050 else
2051 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002052 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002053 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002054 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002055 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002056
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002057 /* Don't pollute the display with errors. */
2058 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002059 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002060 {
2061 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002062 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002063 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002064 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002065 {
2066 ch_logs(channel, "Calling '%s'", (char *)arg);
2067 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2068 tv = &res_tv;
2069 else
2070 tv = NULL;
2071 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002072
2073 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002074 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002075 int id = argv[id_idx].vval.v_number;
2076
Bram Moolenaar55fab432016-02-07 16:53:13 +01002077 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002078 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002079 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002080 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002081 /* If evaluation failed or the result can't be encoded
2082 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002083 vim_free(json);
2084 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002085 err_tv.v_type = VAR_STRING;
2086 err_tv.vval.v_string = (char_u *)"ERROR";
2087 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002088 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002089 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002090 if (json != NULL)
2091 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002092 channel_send(channel,
2093 part == PART_SOCK ? PART_SOCK : PART_IN,
2094 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002095 vim_free(json);
2096 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002097 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002098 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002099 if (tv == &res_tv)
2100 clear_tv(tv);
2101 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002102 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002103 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002104 }
2105 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002106 {
2107 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002108 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002109 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002110}
2111
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002112/*
2113 * Invoke the callback at "cbhead".
2114 * Does not redraw but sets channel_need_redraw.
2115 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002116 static void
2117invoke_one_time_callback(
2118 channel_T *channel,
2119 cbq_T *cbhead,
2120 cbq_T *item,
2121 typval_T *argv)
2122{
2123 ch_logs(channel, "Invoking one-time callback %s",
2124 (char *)item->cq_callback);
2125 /* Remove the item from the list first, if the callback
2126 * invokes ch_close() the list will be cleared. */
2127 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002128 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002129 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002130 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002131 vim_free(item);
2132}
2133
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002134 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002135append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002136{
2137 buf_T *save_curbuf = curbuf;
2138 linenr_T lnum = buffer->b_ml.ml_line_count;
2139 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002140 chanpart_T *ch_part = &channel->ch_part[part];
2141 int save_p_ma = buffer->b_p_ma;
2142
2143 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2144 {
2145 if (!ch_part->ch_nomod_error)
2146 {
2147 ch_error(channel, "Buffer is not modifiable, cannot append");
2148 ch_part->ch_nomod_error = TRUE;
2149 }
2150 return;
2151 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002152
2153 /* If the buffer is also used as input insert above the last
2154 * line. Don't write these lines. */
2155 if (save_write_to)
2156 {
2157 --lnum;
2158 buffer->b_write_to_channel = FALSE;
2159 }
2160
2161 /* Append to the buffer */
2162 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2163
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002164 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002165 curbuf = buffer;
2166 u_sync(TRUE);
2167 /* ignore undo failure, undo is not very useful here */
2168 ignored = u_save(lnum, lnum + 1);
2169
2170 ml_append(lnum, msg, 0, FALSE);
2171 appended_lines_mark(lnum, 1L);
2172 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002173 if (ch_part->ch_nomodifiable)
2174 buffer->b_p_ma = FALSE;
2175 else
2176 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002177
2178 if (buffer->b_nwindows > 0)
2179 {
2180 win_T *wp;
2181 win_T *save_curwin;
2182
2183 FOR_ALL_WINDOWS(wp)
2184 {
2185 if (wp->w_buffer == buffer
2186 && (save_write_to
2187 ? wp->w_cursor.lnum == lnum + 1
2188 : (wp->w_cursor.lnum == lnum
2189 && wp->w_cursor.col == 0)))
2190 {
2191 ++wp->w_cursor.lnum;
2192 save_curwin = curwin;
2193 curwin = wp;
2194 curbuf = curwin->w_buffer;
2195 scroll_cursor_bot(0, FALSE);
2196 curwin = save_curwin;
2197 curbuf = curwin->w_buffer;
2198 }
2199 }
2200 redraw_buf_later(buffer, VALID);
2201 channel_need_redraw = TRUE;
2202 }
2203
2204 if (save_write_to)
2205 {
2206 channel_T *ch;
2207
2208 /* Find channels reading from this buffer and adjust their
2209 * next-to-read line number. */
2210 buffer->b_write_to_channel = TRUE;
2211 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2212 {
2213 chanpart_T *in_part = &ch->ch_part[PART_IN];
2214
2215 if (in_part->ch_buffer == buffer)
2216 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2217 }
2218 }
2219}
2220
Bram Moolenaar437905c2016-04-26 19:01:05 +02002221 static void
2222drop_messages(channel_T *channel, int part)
2223{
2224 char_u *msg;
2225
2226 while ((msg = channel_get(channel, part)) != NULL)
2227 {
2228 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2229 vim_free(msg);
2230 }
2231}
2232
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002233/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002234 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002235 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002236 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002237 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002238 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002239may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002240{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002241 char_u *msg = NULL;
2242 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002244 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002245 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002246 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002247 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002248 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002249 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002250 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002251
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002252 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002253 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002254 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002255
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002256 /* Use a message-specific callback, part callback or channel callback */
2257 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2258 if (cbitem->cq_seq_nr == 0)
2259 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002260 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002261 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002262 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002263 partial = cbitem->cq_partial;
2264 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002265 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002266 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002267 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002268 partial = channel->ch_part[part].ch_partial;
2269 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002270 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002271 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002272 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002273 partial = channel->ch_partial;
2274 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002275
Bram Moolenaar187db502016-02-27 14:44:26 +01002276 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002277 if (buffer != NULL && !buf_valid(buffer))
2278 {
2279 /* buffer was wiped out */
2280 channel->ch_part[part].ch_buffer = NULL;
2281 buffer = NULL;
2282 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002283
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002284 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002285 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002286 listitem_T *item;
2287 int argc = 0;
2288
Bram Moolenaard7ece102016-02-02 23:23:02 +01002289 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002290 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002291 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002292 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002293 channel_parse_json(channel, part);
2294 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002295 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002296 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002297
Bram Moolenaarece61b02016-02-20 21:39:05 +01002298 for (item = listtv->vval.v_list->lv_first;
2299 item != NULL && argc < CH_JSON_MAX_ARGS;
2300 item = item->li_next)
2301 argv[argc++] = item->li_tv;
2302 while (argc < CH_JSON_MAX_ARGS)
2303 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002304
Bram Moolenaarece61b02016-02-20 21:39:05 +01002305 if (argv[0].v_type == VAR_STRING)
2306 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002307 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002308 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002309 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002310 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002311 }
2312
Bram Moolenaarece61b02016-02-20 21:39:05 +01002313 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002314 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002315 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002316 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002317 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002318 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002319 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002320 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002321 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002323 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002324 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002325 return FALSE;
2326 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002327 else
2328 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002329 /* If there is no callback or buffer drop the message. */
2330 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002331 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002332 /* If there is a close callback it may use ch_read() to get the
2333 * messages. */
2334 if (channel->ch_close_cb == NULL)
2335 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002336 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002337 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002338
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002339 if (ch_mode == MODE_NL)
2340 {
2341 char_u *nl;
2342 char_u *buf;
2343
2344 /* See if we have a message ending in NL in the first buffer. If
2345 * not try to concatenate the first and the second buffer. */
2346 while (TRUE)
2347 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002348 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002349 nl = vim_strchr(buf, NL);
2350 if (nl != NULL)
2351 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002352 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002353 return FALSE; /* incomplete message */
2354 }
2355 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002356 {
2357 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002358 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002359 *nl = NUL;
2360 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002361 else
2362 {
2363 /* Copy the message into allocated memory and remove it from
2364 * the buffer. */
2365 msg = vim_strnsave(buf, (int)(nl - buf));
2366 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2367 }
2368 }
2369 else
2370 /* For a raw channel we don't know where the message ends, just
2371 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002372 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002373
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002374 if (msg == NULL)
2375 return FALSE; /* out of memory (and avoids Coverity warning) */
2376
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002377 argv[1].v_type = VAR_STRING;
2378 argv[1].vval.v_string = msg;
2379 }
2380
Bram Moolenaara07fec92016-02-05 21:04:08 +01002381 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002382 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002383 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002384
2385 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002386 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002387 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002388 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002389 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002390 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002391 break;
2392 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002393 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002394 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002395 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002396 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002397 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002398 if (buffer != NULL)
2399 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002400 if (msg == NULL)
2401 /* JSON or JS mode: re-encode the message. */
2402 msg = json_encode(listtv, ch_mode);
2403 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002404 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002405 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002406
Bram Moolenaar187db502016-02-27 14:44:26 +01002407 if (callback != NULL)
2408 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002409 if (cbitem != NULL)
2410 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2411 else
2412 {
2413 /* invoke the channel callback */
2414 ch_logs(channel, "Invoking channel callback %s",
2415 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002416 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002417 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002418 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002419 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002420 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002421 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002422
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002423 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002424 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002425 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002426
2427 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002428}
2429
2430/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002431 * Return TRUE when channel "channel" is open for writing to.
2432 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002433 */
2434 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002435channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002436{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002437 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002438 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002439}
2440
2441/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002442 * Return TRUE when channel "channel" is open for reading or writing.
2443 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002444 */
2445 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002446channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002447{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002448 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002449 || channel->CH_IN_FD != INVALID_FD
2450 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002451 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002452}
2453
2454/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002455 * Return TRUE if "channel" has JSON or other typeahead.
2456 */
2457 static int
2458channel_has_readahead(channel_T *channel, int part)
2459{
2460 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2461
2462 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2463 {
2464 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2465 jsonq_T *item = head->jq_next;
2466
2467 return item != NULL;
2468 }
2469 return channel_peek(channel, part) != NULL;
2470}
2471
2472/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002473 * Return a string indicating the status of the channel.
2474 */
2475 char *
2476channel_status(channel_T *channel)
2477{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002478 int part;
2479 int has_readahead = FALSE;
2480
Bram Moolenaar77073442016-02-13 23:23:53 +01002481 if (channel == NULL)
2482 return "fail";
2483 if (channel_is_open(channel))
2484 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002485 for (part = PART_SOCK; part <= PART_ERR; ++part)
2486 if (channel_has_readahead(channel, part))
2487 {
2488 has_readahead = TRUE;
2489 break;
2490 }
2491
2492 if (has_readahead)
2493 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002494 return "closed";
2495}
2496
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002497 static void
2498channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2499{
2500 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002501 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002502 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002503 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002504
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002505 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002506 STRCAT(namebuf, "_");
2507 tail = STRLEN(namebuf);
2508
2509 STRCPY(namebuf + tail, "status");
2510 dict_add_nr_str(dict, namebuf, 0,
2511 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2512
2513 STRCPY(namebuf + tail, "mode");
2514 switch (chanpart->ch_mode)
2515 {
2516 case MODE_NL: s = "NL"; break;
2517 case MODE_RAW: s = "RAW"; break;
2518 case MODE_JSON: s = "JSON"; break;
2519 case MODE_JS: s = "JS"; break;
2520 }
2521 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2522
2523 STRCPY(namebuf + tail, "io");
2524 if (part == PART_SOCK)
2525 s = "socket";
2526 else switch (chanpart->ch_io)
2527 {
2528 case JIO_NULL: s = "null"; break;
2529 case JIO_PIPE: s = "pipe"; break;
2530 case JIO_FILE: s = "file"; break;
2531 case JIO_BUFFER: s = "buffer"; break;
2532 case JIO_OUT: s = "out"; break;
2533 }
2534 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2535
2536 STRCPY(namebuf + tail, "timeout");
2537 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2538}
2539
2540 void
2541channel_info(channel_T *channel, dict_T *dict)
2542{
2543 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2544 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2545
2546 if (channel->ch_hostname != NULL)
2547 {
2548 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2549 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2550 channel_part_info(channel, dict, "sock", PART_SOCK);
2551 }
2552 else
2553 {
2554 channel_part_info(channel, dict, "out", PART_OUT);
2555 channel_part_info(channel, dict, "err", PART_ERR);
2556 channel_part_info(channel, dict, "in", PART_IN);
2557 }
2558}
2559
Bram Moolenaar77073442016-02-13 23:23:53 +01002560/*
2561 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002562 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002563 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002564 */
2565 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002566channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002567{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002568 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002569
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002570#ifdef FEAT_GUI
2571 channel_gui_unregister(channel);
2572#endif
2573
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002574 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002575 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002576 sock_close(channel->CH_SOCK_FD);
2577 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002578 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002579 may_close_part(&channel->CH_IN_FD);
2580 may_close_part(&channel->CH_OUT_FD);
2581 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002582
Bram Moolenaar8b374212016-02-24 20:43:06 +01002583 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002584 {
2585 typval_T argv[1];
2586 typval_T rettv;
2587 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002588 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002589
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002590 /* Invoke callbacks before the close callback, since it's weird to
2591 * first invoke the close callback. Increment the refcount to avoid
2592 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002593 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002594 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002595 for (part = PART_SOCK; part <= PART_ERR; ++part)
2596 while (may_invoke_callback(channel, part))
2597 ;
2598
2599 /* Invoke the close callback, if still set. */
2600 if (channel->ch_close_cb != NULL)
2601 {
2602 ch_logs(channel, "Invoking close callback %s",
2603 (char *)channel->ch_close_cb);
2604 argv[0].v_type = VAR_CHANNEL;
2605 argv[0].vval.v_channel = channel;
2606 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002607 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2608 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002609 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002610 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002611 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002612
2613 /* the callback is only called once */
2614 vim_free(channel->ch_close_cb);
2615 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002616 partial_unref(channel->ch_close_partial);
2617 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002618
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002619 --channel->ch_refcount;
2620
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002621 if (channel_need_redraw)
2622 {
2623 channel_need_redraw = FALSE;
2624 redraw_after_callback();
2625 }
2626
Bram Moolenaar437905c2016-04-26 19:01:05 +02002627 /* any remaining messages are useless now */
2628 for (part = PART_SOCK; part <= PART_ERR; ++part)
2629 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002630 }
2631
2632 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002633}
2634
Bram Moolenaard04a0202016-01-26 23:30:18 +01002635/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002636 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002637 * Returns NULL if there is nothing.
2638 */
2639 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002640channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002641{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002642 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002643
Bram Moolenaar77073442016-02-13 23:23:53 +01002644 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002645 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002646 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002647}
2648
2649/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002650 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002651 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002652 static void
2653channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002654{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002655 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2656 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002657
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002658 while (channel_peek(channel, part) != NULL)
2659 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002660
2661 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002662 {
2663 cbq_T *node = cb_head->cq_next;
2664
2665 remove_cb_node(cb_head, node);
2666 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002667 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002668 vim_free(node);
2669 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002670
2671 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002672 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002673 free_tv(json_head->jq_next->jq_value);
2674 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002675 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002676
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002677 vim_free(channel->ch_part[part].ch_callback);
2678 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002679 partial_unref(channel->ch_part[part].ch_partial);
2680 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002681}
2682
2683/*
2684 * Clear all the read buffers on "channel".
2685 */
2686 void
2687channel_clear(channel_T *channel)
2688{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002689 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002690 vim_free(channel->ch_hostname);
2691 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002692 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002693 channel_clear_one(channel, PART_OUT);
2694 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002695 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002696 vim_free(channel->ch_callback);
2697 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002698 partial_unref(channel->ch_partial);
2699 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002700 vim_free(channel->ch_close_cb);
2701 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002702 partial_unref(channel->ch_close_partial);
2703 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002704}
2705
Bram Moolenaar77073442016-02-13 23:23:53 +01002706#if defined(EXITFREE) || defined(PROTO)
2707 void
2708channel_free_all(void)
2709{
2710 channel_T *channel;
2711
Bram Moolenaard6051b52016-02-28 15:49:03 +01002712 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002713 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2714 channel_clear(channel);
2715}
2716#endif
2717
2718
Bram Moolenaar715d2852016-04-30 17:06:31 +02002719/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002720#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002721
2722/* Buffer size for reading incoming messages. */
2723#define MAXMSGSIZE 4096
2724
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002725#if defined(HAVE_SELECT)
2726/*
2727 * Add write fds where we are waiting for writing to be possible.
2728 */
2729 static int
2730channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2731{
2732 int maxfd = maxfd_arg;
2733 channel_T *ch;
2734
2735 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2736 {
2737 chanpart_T *in_part = &ch->ch_part[PART_IN];
2738
2739 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2740 {
2741 FD_SET((int)in_part->ch_fd, wfds);
2742 if ((int)in_part->ch_fd >= maxfd)
2743 maxfd = (int)in_part->ch_fd + 1;
2744 }
2745 }
2746 return maxfd;
2747}
2748#else
2749/*
2750 * Add write fds where we are waiting for writing to be possible.
2751 */
2752 static int
2753channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2754{
2755 int nfd = nfd_in;
2756 channel_T *ch;
2757
2758 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2759 {
2760 chanpart_T *in_part = &ch->ch_part[PART_IN];
2761
2762 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2763 {
2764 in_part->ch_poll_idx = nfd;
2765 fds[nfd].fd = in_part->ch_fd;
2766 fds[nfd].events = POLLOUT;
2767 ++nfd;
2768 }
2769 else
2770 in_part->ch_poll_idx = -1;
2771 }
2772 return nfd;
2773}
2774#endif
2775
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002776typedef enum {
2777 CW_READY,
2778 CW_NOT_READY,
2779 CW_ERROR
2780} channel_wait_result;
2781
Bram Moolenaard04a0202016-01-26 23:30:18 +01002782/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002783 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002784 * Return CW_READY when there is something to read.
2785 * Return CW_NOT_READY when there is nothing to read.
2786 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002787 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002788 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002789channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002790{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002791 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002792 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002793
Bram Moolenaard8070362016-02-15 21:56:54 +01002794# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002795 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002796 {
2797 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002798 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002799 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002800 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002801
2802 /* reading from a pipe, not a socket */
2803 while (TRUE)
2804 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002805 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2806
2807 if (r && nread > 0)
2808 return CW_READY;
2809 if (r == 0)
2810 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002811
2812 /* perhaps write some buffer lines */
2813 channel_write_any_lines();
2814
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002815 sleep_time = deadline - GetTickCount();
2816 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002817 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002818 /* Wait for a little while. Very short at first, up to 10 msec
2819 * after looping a few times. */
2820 if (sleep_time > delay)
2821 sleep_time = delay;
2822 Sleep(sleep_time);
2823 delay = delay * 2;
2824 if (delay > 10)
2825 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002826 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002827 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002828 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002829#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002830 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002831#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002832 struct timeval tval;
2833 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002834 fd_set wfds;
2835 int ret;
2836 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002837
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002838 tval.tv_sec = timeout / 1000;
2839 tval.tv_usec = (timeout % 1000) * 1000;
2840 for (;;)
2841 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002842 FD_ZERO(&rfds);
2843 FD_SET((int)fd, &rfds);
2844
2845 /* Write lines to a pipe when a pipe can be written to. Need to
2846 * set this every time, some buffers may be done. */
2847 maxfd = (int)fd + 1;
2848 FD_ZERO(&wfds);
2849 maxfd = channel_fill_wfds(maxfd, &wfds);
2850
2851 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002852# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002853 SOCK_ERRNO;
2854 if (ret == -1 && errno == EINTR)
2855 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002856# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002857 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002858 {
2859 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002860 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002861 channel_write_any_lines();
2862 continue;
2863 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002864 break;
2865 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002866#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002867 for (;;)
2868 {
2869 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2870 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002871
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002872 fds[0].fd = fd;
2873 fds[0].events = POLLIN;
2874 nfd = channel_fill_poll_write(nfd, fds);
2875 if (poll(fds, nfd, timeout) > 0)
2876 {
2877 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002878 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002879 channel_write_any_lines();
2880 continue;
2881 }
2882 break;
2883 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002884#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002885 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002886 return CW_NOT_READY;
2887}
2888
2889 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002890channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002891{
2892 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002893 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2894 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002895
2896 /* Queue a "DETACH" netbeans message in the command queue in order to
2897 * terminate the netbeans session later. Do not end the session here
2898 * directly as we may be running in the context of a call to
2899 * netbeans_parse_messages():
2900 * netbeans_parse_messages
2901 * -> autocmd triggered while processing the netbeans cmd
2902 * -> ui_breakcheck
2903 * -> gui event loop or select loop
2904 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002905 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002906 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002907 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002908 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002909 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2910
2911 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002912 * died. Don't close the channel right away, it may be the wrong moment
2913 * to invoke callbacks. */
2914 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002915
2916#ifdef FEAT_GUI
2917 /* Stop listening to GUI events right away. */
2918 channel_gui_unregister(channel);
2919#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002920}
2921
2922 static void
2923channel_close_now(channel_T *channel)
2924{
2925 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002926 channel_close(channel, TRUE);
2927 if (channel->ch_nb_close_cb != NULL)
2928 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002929}
2930
2931/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002932 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002933 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02002934 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002935 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002936 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002937channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002938{
2939 static char_u *buf = NULL;
2940 int len = 0;
2941 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002942 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002943 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002944
Bram Moolenaar5850a762016-05-27 19:59:48 +02002945 /* If we detected a read error don't try reading again. */
2946 if (channel->ch_to_be_closed)
2947 return;
2948
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002949 fd = channel->ch_part[part].ch_fd;
2950 if (fd == INVALID_FD)
2951 {
2952 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002953 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002954 }
2955 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002956
2957 /* Allocate a buffer to read into. */
2958 if (buf == NULL)
2959 {
2960 buf = alloc(MAXMSGSIZE);
2961 if (buf == NULL)
2962 return; /* out of memory! */
2963 }
2964
2965 /* Keep on reading for as long as there is something to read.
2966 * Use select() or poll() to avoid blocking on a message that is exactly
2967 * MAXMSGSIZE long. */
2968 for (;;)
2969 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002970 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002971 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002972 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002973 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002974 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002975 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002976 if (len <= 0)
2977 break; /* error or nothing more to read */
2978
2979 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002980 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002981 readlen += len;
2982 if (len < MAXMSGSIZE)
2983 break; /* did read everything that's available */
2984 }
2985
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002986 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002987 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002988 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002989
2990#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002991 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002992 if (CH_HAS_GUI && gtk_main_level() > 0)
2993 gtk_main_quit();
2994#endif
2995}
2996
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002997/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002998 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002999 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003000 * Returns what was read in allocated memory.
3001 * Returns NULL in case of error or timeout.
3002 */
3003 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003004channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003005{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003006 char_u *buf;
3007 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003008 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003009 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003010 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003011
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003012 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003013 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003014
3015 while (TRUE)
3016 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003017 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003018 if (buf != NULL && (mode == MODE_RAW
3019 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
3020 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003021 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003022 continue;
3023
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003024 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003025 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003026 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003027 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003028 {
3029 ch_log(channel, "Timed out");
3030 return NULL;
3031 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003032 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003033 }
3034
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003035 if (mode == MODE_RAW)
3036 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003037 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003038 }
3039 else
3040 {
3041 nl = vim_strchr(buf, NL);
3042 if (nl[1] == NUL)
3043 {
3044 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003045 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003046 *nl = NUL;
3047 }
3048 else
3049 {
3050 /* Copy the message into allocated memory and remove it from the
3051 * buffer. */
3052 msg = vim_strnsave(buf, (int)(nl - buf));
3053 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3054 }
3055 }
3056 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003057 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003058 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003059}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003060
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003061/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003062 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003063 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003064 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003065 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003066 */
3067 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003068channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003069 channel_T *channel,
3070 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003071 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003072 int id,
3073 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003074{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003075 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003076 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003077 int timeout;
3078 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003079
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003080 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003081 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003082 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003083 for (;;)
3084 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003085 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003086
3087 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003088 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003089 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003090 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003091 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003092 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003093
Bram Moolenaard7ece102016-02-02 23:23:02 +01003094 if (!more)
3095 {
3096 /* Handle any other messages in the queue. If done some more
3097 * messages may have arrived. */
3098 if (channel_parse_messages())
3099 continue;
3100
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003101 /* Wait for up to the timeout. If there was an incomplete message
3102 * use the deadline for that. */
3103 timeout = timeout_arg;
3104 if (chanpart->ch_waiting)
3105 {
3106#ifdef WIN32
3107 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3108#else
3109 {
3110 struct timeval now_tv;
3111
3112 gettimeofday(&now_tv, NULL);
3113 timeout = (chanpart->ch_deadline.tv_sec
3114 - now_tv.tv_sec) * 1000
3115 + (chanpart->ch_deadline.tv_usec
3116 - now_tv.tv_usec) / 1000
3117 + 1;
3118 }
3119#endif
3120 if (timeout < 0)
3121 {
3122 /* Something went wrong, channel_parse_json() didn't
3123 * discard message. Cancel waiting. */
3124 chanpart->ch_waiting = FALSE;
3125 timeout = timeout_arg;
3126 }
3127 else if (timeout > timeout_arg)
3128 timeout = timeout_arg;
3129 }
3130 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003131 if (fd == INVALID_FD
3132 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003133 {
3134 if (timeout == timeout_arg)
3135 {
3136 if (fd != INVALID_FD)
3137 ch_log(channel, "Timed out");
3138 break;
3139 }
3140 }
3141 else
3142 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003143 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003144 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003145 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003146 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003147}
3148
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003149/*
3150 * Common for ch_read() and ch_readraw().
3151 */
3152 void
3153common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3154{
3155 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003156 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003157 jobopt_T opt;
3158 int mode;
3159 int timeout;
3160 int id = -1;
3161 typval_T *listtv = NULL;
3162
3163 /* return an empty string by default */
3164 rettv->v_type = VAR_STRING;
3165 rettv->vval.v_string = NULL;
3166
3167 clear_job_options(&opt);
3168 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3169 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003170 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003171
Bram Moolenaar437905c2016-04-26 19:01:05 +02003172 if (opt.jo_set & JO_PART)
3173 part = opt.jo_part;
3174 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003175 if (channel != NULL)
3176 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003177 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003178 part = channel_part_read(channel);
3179 mode = channel_get_mode(channel, part);
3180 timeout = channel_get_timeout(channel, part);
3181 if (opt.jo_set & JO_TIMEOUT)
3182 timeout = opt.jo_timeout;
3183
3184 if (raw || mode == MODE_RAW || mode == MODE_NL)
3185 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3186 else
3187 {
3188 if (opt.jo_set & JO_ID)
3189 id = opt.jo_id;
3190 channel_read_json_block(channel, part, timeout, id, &listtv);
3191 if (listtv != NULL)
3192 {
3193 *rettv = *listtv;
3194 vim_free(listtv);
3195 }
3196 else
3197 {
3198 rettv->v_type = VAR_SPECIAL;
3199 rettv->vval.v_number = VVAL_NONE;
3200 }
3201 }
3202 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003203
3204theend:
3205 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003206}
3207
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003208# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3209 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003210/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003211 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003212 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003213 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003214 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003215channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003216{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003217 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003218 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003219
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003220 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003221 for (channel = first_channel; channel != NULL;
3222 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003223 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003224 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003225 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003226 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003227 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003228 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003229 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003230 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003231 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003232}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003233# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003234
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003235# if defined(WIN32) || defined(PROTO)
3236/*
3237 * Check the channels for anything that is ready to be read.
3238 * The data is put in the read queue.
3239 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003240 void
3241channel_handle_events(void)
3242{
3243 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003244 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003245 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003246
3247 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3248 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003249 /* If we detected a read error don't try reading again. */
3250 if (channel->ch_to_be_closed)
3251 continue;
3252
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003253 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003254 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003255 {
3256 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003257 if (fd != INVALID_FD)
3258 {
3259 int r = channel_wait(channel, fd, 0);
3260
3261 if (r == CW_READY)
3262 channel_read(channel, part, "channel_handle_events");
3263 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003264 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003265 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003266 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003267 }
3268}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003269# endif
3270
Bram Moolenaard04a0202016-01-26 23:30:18 +01003271/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003272 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003273 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003274 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003275 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003276 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003277channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003278{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003279 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003280 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003281 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003282
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003283 fd = channel->ch_part[part].ch_fd;
3284 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003285 {
3286 if (!channel->ch_error && fun != NULL)
3287 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003288 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003289 EMSG2("E630: %s(): write while not connected", fun);
3290 }
3291 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003292 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003293 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003294
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003295 if (log_fd != NULL)
3296 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003297 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003298 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003299 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003300 fprintf(log_fd, "'\n");
3301 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003302 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003303 }
3304
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003305 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003306 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003307 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003308 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003309 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003310 {
3311 if (!channel->ch_error && fun != NULL)
3312 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003313 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003314 EMSG2("E631: %s(): write failed", fun);
3315 }
3316 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003317 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003318 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003319
3320 channel->ch_error = FALSE;
3321 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003322}
3323
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003324/*
3325 * Common for "ch_sendexpr()" and "ch_sendraw()".
3326 * Returns the channel if the caller should read the response.
3327 * Sets "part_read" to the the read fd.
3328 * Otherwise returns NULL.
3329 */
3330 channel_T *
3331send_common(
3332 typval_T *argvars,
3333 char_u *text,
3334 int id,
3335 int eval,
3336 jobopt_T *opt,
3337 char *fun,
3338 int *part_read)
3339{
3340 channel_T *channel;
3341 int part_send;
3342
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003343 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003344 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003345 if (channel == NULL)
3346 return NULL;
3347 part_send = channel_part_send(channel);
3348 *part_read = channel_part_read(channel);
3349
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003350 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3351 return NULL;
3352
3353 /* Set the callback. An empty callback means no callback and not reading
3354 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3355 * allowed. */
3356 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3357 {
3358 if (eval)
3359 {
3360 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3361 return NULL;
3362 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003363 channel_set_req_callback(channel, part_send,
3364 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003365 }
3366
3367 if (channel_send(channel, part_send, text, fun) == OK
3368 && opt->jo_callback == NULL)
3369 return channel;
3370 return NULL;
3371}
3372
3373/*
3374 * common for "ch_evalexpr()" and "ch_sendexpr()"
3375 */
3376 void
3377ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3378{
3379 char_u *text;
3380 typval_T *listtv;
3381 channel_T *channel;
3382 int id;
3383 ch_mode_T ch_mode;
3384 int part_send;
3385 int part_read;
3386 jobopt_T opt;
3387 int timeout;
3388
3389 /* return an empty string by default */
3390 rettv->v_type = VAR_STRING;
3391 rettv->vval.v_string = NULL;
3392
Bram Moolenaar437905c2016-04-26 19:01:05 +02003393 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003394 if (channel == NULL)
3395 return;
3396 part_send = channel_part_send(channel);
3397
3398 ch_mode = channel_get_mode(channel, part_send);
3399 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3400 {
3401 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3402 return;
3403 }
3404
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003405 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003406 text = json_encode_nr_expr(id, &argvars[1],
3407 ch_mode == MODE_JS ? JSON_JS : 0);
3408 if (text == NULL)
3409 return;
3410
3411 channel = send_common(argvars, text, id, eval, &opt,
3412 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3413 vim_free(text);
3414 if (channel != NULL && eval)
3415 {
3416 if (opt.jo_set & JO_TIMEOUT)
3417 timeout = opt.jo_timeout;
3418 else
3419 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003420 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3421 == OK)
3422 {
3423 list_T *list = listtv->vval.v_list;
3424
3425 /* Move the item from the list and then change the type to
3426 * avoid the value being freed. */
3427 *rettv = list->lv_last->li_tv;
3428 list->lv_last->li_tv.v_type = VAR_NUMBER;
3429 free_tv(listtv);
3430 }
3431 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003432 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003433}
3434
3435/*
3436 * common for "ch_evalraw()" and "ch_sendraw()"
3437 */
3438 void
3439ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3440{
3441 char_u buf[NUMBUFLEN];
3442 char_u *text;
3443 channel_T *channel;
3444 int part_read;
3445 jobopt_T opt;
3446 int timeout;
3447
3448 /* return an empty string by default */
3449 rettv->v_type = VAR_STRING;
3450 rettv->vval.v_string = NULL;
3451
3452 text = get_tv_string_buf(&argvars[1], buf);
3453 channel = send_common(argvars, text, 0, eval, &opt,
3454 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3455 if (channel != NULL && eval)
3456 {
3457 if (opt.jo_set & JO_TIMEOUT)
3458 timeout = opt.jo_timeout;
3459 else
3460 timeout = channel_get_timeout(channel, part_read);
3461 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3462 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003463 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003464}
3465
Bram Moolenaard04a0202016-01-26 23:30:18 +01003466# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003467/*
3468 * Add open channels to the poll struct.
3469 * Return the adjusted struct index.
3470 * The type of "fds" is hidden to avoid problems with the function proto.
3471 */
3472 int
3473channel_poll_setup(int nfd_in, void *fds_in)
3474{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003475 int nfd = nfd_in;
3476 channel_T *channel;
3477 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003478 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003479
Bram Moolenaar77073442016-02-13 23:23:53 +01003480 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003481 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003482 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003483 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003484 chanpart_T *ch_part = &channel->ch_part[part];
3485
3486 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003487 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003488 ch_part->ch_poll_idx = nfd;
3489 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003490 fds[nfd].events = POLLIN;
3491 nfd++;
3492 }
3493 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003494 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003495 }
3496 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003497
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003498 nfd = channel_fill_poll_write(nfd, fds);
3499
Bram Moolenaare0874f82016-01-24 20:36:41 +01003500 return nfd;
3501}
3502
3503/*
3504 * The type of "fds" is hidden to avoid problems with the function proto.
3505 */
3506 int
3507channel_poll_check(int ret_in, void *fds_in)
3508{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003509 int ret = ret_in;
3510 channel_T *channel;
3511 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003512 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003513 int idx;
3514 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003515
Bram Moolenaar77073442016-02-13 23:23:53 +01003516 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003517 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003518 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003519 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003520 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003521
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003522 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003523 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003524 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003525 --ret;
3526 }
3527 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003528
3529 in_part = &channel->ch_part[PART_IN];
3530 idx = in_part->ch_poll_idx;
3531 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3532 {
3533 if (in_part->ch_buf_append)
3534 {
3535 if (in_part->ch_buffer != NULL)
3536 channel_write_new_lines(in_part->ch_buffer);
3537 }
3538 else
3539 channel_write_in(channel);
3540 --ret;
3541 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003542 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003543
3544 return ret;
3545}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003546# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003547
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003548# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003549/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003550 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003551 */
3552 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003553channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003554{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003555 int maxfd = maxfd_in;
3556 channel_T *channel;
3557 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003558 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003560
Bram Moolenaar77073442016-02-13 23:23:53 +01003561 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003562 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003563 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003564 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003565 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003566
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003567 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003568 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003569 FD_SET((int)fd, rfds);
3570 if (maxfd < (int)fd)
3571 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003572 }
3573 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003574 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003575
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003576 maxfd = channel_fill_wfds(maxfd, wfds);
3577
Bram Moolenaare0874f82016-01-24 20:36:41 +01003578 return maxfd;
3579}
3580
3581/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003582 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003583 */
3584 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003585channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003586{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003587 int ret = ret_in;
3588 channel_T *channel;
3589 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003590 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003591 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003592 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003593
Bram Moolenaar77073442016-02-13 23:23:53 +01003594 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003595 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003596 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003597 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003599
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003600 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003601 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003602 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003603 --ret;
3604 }
3605 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003606
3607 in_part = &channel->ch_part[PART_IN];
3608 if (ret > 0 && in_part->ch_fd != INVALID_FD
3609 && FD_ISSET(in_part->ch_fd, wfds))
3610 {
3611 if (in_part->ch_buf_append)
3612 {
3613 if (in_part->ch_buffer != NULL)
3614 channel_write_new_lines(in_part->ch_buffer);
3615 }
3616 else
3617 channel_write_in(channel);
3618 --ret;
3619 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003620 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003621
3622 return ret;
3623}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003624# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003625
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003626/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003627 * Execute queued up commands.
3628 * Invoked from the main loop when it's safe to execute received commands.
3629 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003630 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003631 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003632channel_parse_messages(void)
3633{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003634 channel_T *channel = first_channel;
3635 int ret = FALSE;
3636 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003637 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003638
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003639 ++safe_to_invoke_callback;
3640
Bram Moolenaard0b65022016-03-06 21:50:33 +01003641 /* Only do this message when another message was given, otherwise we get
3642 * lots of them. */
3643 if (did_log_msg)
3644 {
3645 ch_log(NULL, "looking for messages on channels");
3646 did_log_msg = FALSE;
3647 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003648 while (channel != NULL)
3649 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003650 if (channel->ch_to_be_closed)
3651 {
3652 channel->ch_to_be_closed = FALSE;
3653 channel_close_now(channel);
3654 /* channel may have been freed, start over */
3655 channel = first_channel;
3656 continue;
3657 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003658 if (channel->ch_to_be_freed)
3659 {
3660 channel_free(channel);
3661 /* channel has been freed, start over */
3662 channel = first_channel;
3663 continue;
3664 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003665 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003666 {
3667 /* channel is no longer useful, free it */
3668 channel_free(channel);
3669 channel = first_channel;
3670 part = PART_SOCK;
3671 continue;
3672 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003673 if (channel->ch_part[part].ch_fd != INVALID_FD
3674 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003675 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003676 /* Increase the refcount, in case the handler causes the channel
3677 * to be unreferenced or closed. */
3678 ++channel->ch_refcount;
3679 r = may_invoke_callback(channel, part);
3680 if (r == OK)
3681 ret = TRUE;
3682 if (channel_unref(channel) || r == OK)
3683 {
3684 /* channel was freed or something was done, start over */
3685 channel = first_channel;
3686 part = PART_SOCK;
3687 continue;
3688 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003689 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003690 if (part < PART_ERR)
3691 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003692 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003693 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003694 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003695 part = PART_SOCK;
3696 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003697 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003698
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003699 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003700 {
3701 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003702 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003703 }
3704
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003705 --safe_to_invoke_callback;
3706
Bram Moolenaard7ece102016-02-02 23:23:02 +01003707 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003708}
3709
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003710/*
3711 * Mark references to lists used in channels.
3712 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003713 int
3714set_ref_in_channel(int copyID)
3715{
Bram Moolenaar77073442016-02-13 23:23:53 +01003716 int abort = FALSE;
3717 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003718 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003719
Bram Moolenaar77073442016-02-13 23:23:53 +01003720 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003721 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003722 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003723 tv.v_type = VAR_CHANNEL;
3724 tv.vval.v_channel = channel;
3725 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003726 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003727 return abort;
3728}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003729
3730/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003731 * Return the "part" to write to for "channel".
3732 */
3733 int
3734channel_part_send(channel_T *channel)
3735{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003736 if (channel->CH_SOCK_FD == INVALID_FD)
3737 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003738 return PART_SOCK;
3739}
3740
3741/*
3742 * Return the default "part" to read from for "channel".
3743 */
3744 int
3745channel_part_read(channel_T *channel)
3746{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003747 if (channel->CH_SOCK_FD == INVALID_FD)
3748 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003749 return PART_SOCK;
3750}
3751
3752/*
3753 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003754 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003755 */
3756 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003757channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003758{
Bram Moolenaar77073442016-02-13 23:23:53 +01003759 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003760 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003761 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003762}
3763
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003764/*
3765 * Return the timeout of "channel"/"part"
3766 */
3767 int
3768channel_get_timeout(channel_T *channel, int part)
3769{
3770 return channel->ch_part[part].ch_timeout;
3771}
3772
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003773 static int
3774handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3775{
3776 char_u *val = get_tv_string(item);
3777
3778 opt->jo_set |= jo;
3779 if (STRCMP(val, "nl") == 0)
3780 *modep = MODE_NL;
3781 else if (STRCMP(val, "raw") == 0)
3782 *modep = MODE_RAW;
3783 else if (STRCMP(val, "js") == 0)
3784 *modep = MODE_JS;
3785 else if (STRCMP(val, "json") == 0)
3786 *modep = MODE_JSON;
3787 else
3788 {
3789 EMSG2(_(e_invarg2), val);
3790 return FAIL;
3791 }
3792 return OK;
3793}
3794
3795 static int
3796handle_io(typval_T *item, int part, jobopt_T *opt)
3797{
3798 char_u *val = get_tv_string(item);
3799
3800 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3801 if (STRCMP(val, "null") == 0)
3802 opt->jo_io[part] = JIO_NULL;
3803 else if (STRCMP(val, "pipe") == 0)
3804 opt->jo_io[part] = JIO_PIPE;
3805 else if (STRCMP(val, "file") == 0)
3806 opt->jo_io[part] = JIO_FILE;
3807 else if (STRCMP(val, "buffer") == 0)
3808 opt->jo_io[part] = JIO_BUFFER;
3809 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3810 opt->jo_io[part] = JIO_OUT;
3811 else
3812 {
3813 EMSG2(_(e_invarg2), val);
3814 return FAIL;
3815 }
3816 return OK;
3817}
3818
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003819/*
3820 * Clear a jobopt_T before using it.
3821 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003822 void
3823clear_job_options(jobopt_T *opt)
3824{
3825 vim_memset(opt, 0, sizeof(jobopt_T));
3826}
3827
3828/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003829 * Free any members of a jobopt_T.
3830 */
3831 void
3832free_job_options(jobopt_T *opt)
3833{
3834 if (opt->jo_partial != NULL)
3835 partial_unref(opt->jo_partial);
3836 if (opt->jo_out_partial != NULL)
3837 partial_unref(opt->jo_out_partial);
3838 if (opt->jo_err_partial != NULL)
3839 partial_unref(opt->jo_err_partial);
3840 if (opt->jo_close_partial != NULL)
3841 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003842 if (opt->jo_exit_partial != NULL)
3843 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003844}
3845
3846/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003847 * Get the PART_ number from the first character of an option name.
3848 */
3849 static int
3850part_from_char(int c)
3851{
3852 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3853}
3854
3855/*
3856 * Get the option entries from the dict in "tv", parse them and put the result
3857 * in "opt".
3858 * Only accept options in "supported".
3859 * If an option value is invalid return FAIL.
3860 */
3861 int
3862get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3863{
3864 typval_T *item;
3865 char_u *val;
3866 dict_T *dict;
3867 int todo;
3868 hashitem_T *hi;
3869 int part;
3870
3871 opt->jo_set = 0;
3872 if (tv->v_type == VAR_UNKNOWN)
3873 return OK;
3874 if (tv->v_type != VAR_DICT)
3875 {
3876 EMSG(_(e_invarg));
3877 return FAIL;
3878 }
3879 dict = tv->vval.v_dict;
3880 if (dict == NULL)
3881 return OK;
3882
3883 todo = (int)dict->dv_hashtab.ht_used;
3884 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3885 if (!HASHITEM_EMPTY(hi))
3886 {
3887 item = &dict_lookup(hi)->di_tv;
3888
3889 if (STRCMP(hi->hi_key, "mode") == 0)
3890 {
3891 if (!(supported & JO_MODE))
3892 break;
3893 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3894 return FAIL;
3895 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003896 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003897 {
3898 if (!(supported & JO_IN_MODE))
3899 break;
3900 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3901 == FAIL)
3902 return FAIL;
3903 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003904 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003905 {
3906 if (!(supported & JO_OUT_MODE))
3907 break;
3908 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3909 == FAIL)
3910 return FAIL;
3911 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003912 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003913 {
3914 if (!(supported & JO_ERR_MODE))
3915 break;
3916 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3917 == FAIL)
3918 return FAIL;
3919 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003920 else if (STRCMP(hi->hi_key, "in_io") == 0
3921 || STRCMP(hi->hi_key, "out_io") == 0
3922 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003923 {
3924 if (!(supported & JO_OUT_IO))
3925 break;
3926 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3927 return FAIL;
3928 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003929 else if (STRCMP(hi->hi_key, "in_name") == 0
3930 || STRCMP(hi->hi_key, "out_name") == 0
3931 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003932 {
3933 part = part_from_char(*hi->hi_key);
3934
3935 if (!(supported & JO_OUT_IO))
3936 break;
3937 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3938 opt->jo_io_name[part] =
3939 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3940 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003941 else if (STRCMP(hi->hi_key, "in_buf") == 0
3942 || STRCMP(hi->hi_key, "out_buf") == 0
3943 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003944 {
3945 part = part_from_char(*hi->hi_key);
3946
3947 if (!(supported & JO_OUT_IO))
3948 break;
3949 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3950 opt->jo_io_buf[part] = get_tv_number(item);
3951 if (opt->jo_io_buf[part] <= 0)
3952 {
3953 EMSG2(_(e_invarg2), get_tv_string(item));
3954 return FAIL;
3955 }
3956 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3957 {
3958 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3959 return FAIL;
3960 }
3961 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02003962 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
3963 || STRCMP(hi->hi_key, "err_modifiable") == 0)
3964 {
3965 part = part_from_char(*hi->hi_key);
3966
3967 if (!(supported & JO_OUT_IO))
3968 break;
3969 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
3970 opt->jo_modifiable[part] = get_tv_number(item);
3971 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003972 else if (STRCMP(hi->hi_key, "in_top") == 0
3973 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003974 {
3975 linenr_T *lp;
3976
3977 if (!(supported & JO_OUT_IO))
3978 break;
3979 if (hi->hi_key[3] == 't')
3980 {
3981 lp = &opt->jo_in_top;
3982 opt->jo_set |= JO_IN_TOP;
3983 }
3984 else
3985 {
3986 lp = &opt->jo_in_bot;
3987 opt->jo_set |= JO_IN_BOT;
3988 }
3989 *lp = get_tv_number(item);
3990 if (*lp < 0)
3991 {
3992 EMSG2(_(e_invarg2), get_tv_string(item));
3993 return FAIL;
3994 }
3995 }
3996 else if (STRCMP(hi->hi_key, "channel") == 0)
3997 {
3998 if (!(supported & JO_OUT_IO))
3999 break;
4000 opt->jo_set |= JO_CHANNEL;
4001 if (item->v_type != VAR_CHANNEL)
4002 {
4003 EMSG2(_(e_invarg2), "channel");
4004 return FAIL;
4005 }
4006 opt->jo_channel = item->vval.v_channel;
4007 }
4008 else if (STRCMP(hi->hi_key, "callback") == 0)
4009 {
4010 if (!(supported & JO_CALLBACK))
4011 break;
4012 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004013 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004014 if (opt->jo_callback == NULL)
4015 {
4016 EMSG2(_(e_invarg2), "callback");
4017 return FAIL;
4018 }
4019 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004020 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004021 {
4022 if (!(supported & JO_OUT_CALLBACK))
4023 break;
4024 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004025 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004026 if (opt->jo_out_cb == NULL)
4027 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004028 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004029 return FAIL;
4030 }
4031 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004032 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004033 {
4034 if (!(supported & JO_ERR_CALLBACK))
4035 break;
4036 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004037 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004038 if (opt->jo_err_cb == NULL)
4039 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004040 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004041 return FAIL;
4042 }
4043 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004044 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004045 {
4046 if (!(supported & JO_CLOSE_CALLBACK))
4047 break;
4048 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004049 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004050 if (opt->jo_close_cb == NULL)
4051 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004052 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004053 return FAIL;
4054 }
4055 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004056 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4057 {
4058 if (!(supported & JO_EXIT_CB))
4059 break;
4060 opt->jo_set |= JO_EXIT_CB;
4061 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4062 if (opt->jo_exit_cb == NULL)
4063 {
4064 EMSG2(_(e_invarg2), "exit_cb");
4065 return FAIL;
4066 }
4067 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004068 else if (STRCMP(hi->hi_key, "waittime") == 0)
4069 {
4070 if (!(supported & JO_WAITTIME))
4071 break;
4072 opt->jo_set |= JO_WAITTIME;
4073 opt->jo_waittime = get_tv_number(item);
4074 }
4075 else if (STRCMP(hi->hi_key, "timeout") == 0)
4076 {
4077 if (!(supported & JO_TIMEOUT))
4078 break;
4079 opt->jo_set |= JO_TIMEOUT;
4080 opt->jo_timeout = get_tv_number(item);
4081 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004082 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004083 {
4084 if (!(supported & JO_OUT_TIMEOUT))
4085 break;
4086 opt->jo_set |= JO_OUT_TIMEOUT;
4087 opt->jo_out_timeout = get_tv_number(item);
4088 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004089 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004090 {
4091 if (!(supported & JO_ERR_TIMEOUT))
4092 break;
4093 opt->jo_set |= JO_ERR_TIMEOUT;
4094 opt->jo_err_timeout = get_tv_number(item);
4095 }
4096 else if (STRCMP(hi->hi_key, "part") == 0)
4097 {
4098 if (!(supported & JO_PART))
4099 break;
4100 opt->jo_set |= JO_PART;
4101 val = get_tv_string(item);
4102 if (STRCMP(val, "err") == 0)
4103 opt->jo_part = PART_ERR;
4104 else
4105 {
4106 EMSG2(_(e_invarg2), val);
4107 return FAIL;
4108 }
4109 }
4110 else if (STRCMP(hi->hi_key, "id") == 0)
4111 {
4112 if (!(supported & JO_ID))
4113 break;
4114 opt->jo_set |= JO_ID;
4115 opt->jo_id = get_tv_number(item);
4116 }
4117 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4118 {
4119 if (!(supported & JO_STOPONEXIT))
4120 break;
4121 opt->jo_set |= JO_STOPONEXIT;
4122 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4123 opt->jo_soe_buf);
4124 if (opt->jo_stoponexit == NULL)
4125 {
4126 EMSG2(_(e_invarg2), "stoponexit");
4127 return FAIL;
4128 }
4129 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004130 else if (STRCMP(hi->hi_key, "block_write") == 0)
4131 {
4132 if (!(supported & JO_BLOCK_WRITE))
4133 break;
4134 opt->jo_set |= JO_BLOCK_WRITE;
4135 opt->jo_block_write = get_tv_number(item);
4136 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004137 else
4138 break;
4139 --todo;
4140 }
4141 if (todo > 0)
4142 {
4143 EMSG2(_(e_invarg2), hi->hi_key);
4144 return FAIL;
4145 }
4146
4147 return OK;
4148}
4149
4150/*
4151 * Get the channel from the argument.
4152 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004153 * When "check_open" is TRUE check that the channel can be used.
4154 * When "reading" is TRUE "check_open" considers typeahead useful.
4155 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156 */
4157 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004158get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004159{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004160 channel_T *channel = NULL;
4161 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004162
4163 if (tv->v_type == VAR_JOB)
4164 {
4165 if (tv->vval.v_job != NULL)
4166 channel = tv->vval.v_job->jv_channel;
4167 }
4168 else if (tv->v_type == VAR_CHANNEL)
4169 {
4170 channel = tv->vval.v_channel;
4171 }
4172 else
4173 {
4174 EMSG2(_(e_invarg2), get_tv_string(tv));
4175 return NULL;
4176 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004177 if (channel != NULL && reading)
4178 has_readahead = channel_has_readahead(channel,
4179 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004180
Bram Moolenaar437905c2016-04-26 19:01:05 +02004181 if (check_open && (channel == NULL || (!channel_is_open(channel)
4182 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004183 {
4184 EMSG(_("E906: not an open channel"));
4185 return NULL;
4186 }
4187 return channel;
4188}
4189
4190static job_T *first_job = NULL;
4191
4192 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004193job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004194{
4195 ch_log(job->jv_channel, "Freeing job");
4196 if (job->jv_channel != NULL)
4197 {
4198 /* The link from the channel to the job doesn't count as a reference,
4199 * thus don't decrement the refcount of the job. The reference from
4200 * the job to the channel does count the refrence, decrement it and
4201 * NULL the reference. We don't set ch_job_killed, unreferencing the
4202 * job doesn't mean it stops running. */
4203 job->jv_channel->ch_job = NULL;
4204 channel_unref(job->jv_channel);
4205 }
4206 mch_clear_job(job);
4207
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004208 vim_free(job->jv_stoponexit);
4209 vim_free(job->jv_exit_cb);
4210 partial_unref(job->jv_exit_partial);
4211}
4212
4213 static void
4214job_free_job(job_T *job)
4215{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004216 if (job->jv_next != NULL)
4217 job->jv_next->jv_prev = job->jv_prev;
4218 if (job->jv_prev == NULL)
4219 first_job = job->jv_next;
4220 else
4221 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004222 vim_free(job);
4223}
4224
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004225 static void
4226job_free(job_T *job)
4227{
4228 if (!in_free_unref_items)
4229 {
4230 job_free_contents(job);
4231 job_free_job(job);
4232 }
4233}
4234
Bram Moolenaar655da312016-05-28 22:22:34 +02004235#if defined(EXITFREE) || defined(PROTO)
4236 void
4237job_free_all(void)
4238{
4239 while (first_job != NULL)
4240 job_free(first_job);
4241}
4242#endif
4243
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004244/*
4245 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004246 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4247 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004248 */
4249 static int
4250job_still_useful(job_T *job)
4251{
4252 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004253 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4254 || (job->jv_channel != NULL
4255 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004256}
4257
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004258/*
4259 * Mark references in jobs that are still useful.
4260 */
4261 int
4262set_ref_in_job(int copyID)
4263{
4264 int abort = FALSE;
4265 job_T *job;
4266 typval_T tv;
4267
4268 for (job = first_job; job != NULL; job = job->jv_next)
4269 if (job_still_useful(job))
4270 {
4271 tv.v_type = VAR_JOB;
4272 tv.vval.v_job = job;
4273 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4274 }
4275 return abort;
4276}
4277
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004278 void
4279job_unref(job_T *job)
4280{
4281 if (job != NULL && --job->jv_refcount <= 0)
4282 {
4283 /* Do not free the job when it has not ended yet and there is a
4284 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004285 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004286 {
4287 job_free(job);
4288 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004289 else if (job->jv_channel != NULL
4290 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004291 {
4292 /* Do remove the link to the channel, otherwise it hangs
4293 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004294 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004295 job->jv_channel->ch_job = NULL;
4296 channel_unref(job->jv_channel);
4297 job->jv_channel = NULL;
4298 }
4299 }
4300}
4301
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004302 int
4303free_unused_jobs_contents(int copyID, int mask)
4304{
4305 int did_free = FALSE;
4306 job_T *job;
4307
4308 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004309 if ((job->jv_copyID & mask) != (copyID & mask)
4310 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311 {
4312 /* Free the channel and ordinary items it contains, but don't
4313 * recurse into Lists, Dictionaries etc. */
4314 job_free_contents(job);
4315 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004316 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004317 return did_free;
4318}
4319
4320 void
4321free_unused_jobs(int copyID, int mask)
4322{
4323 job_T *job;
4324 job_T *job_next;
4325
4326 for (job = first_job; job != NULL; job = job_next)
4327 {
4328 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004329 if ((job->jv_copyID & mask) != (copyID & mask)
4330 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004331 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004332 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004333 job_free_job(job);
4334 }
4335 }
4336}
4337
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004338/*
4339 * Allocate a job. Sets the refcount to one and sets options default.
4340 */
4341 static job_T *
4342job_alloc(void)
4343{
4344 job_T *job;
4345
4346 job = (job_T *)alloc_clear(sizeof(job_T));
4347 if (job != NULL)
4348 {
4349 job->jv_refcount = 1;
4350 job->jv_stoponexit = vim_strsave((char_u *)"term");
4351
4352 if (first_job != NULL)
4353 {
4354 first_job->jv_prev = job;
4355 job->jv_next = first_job;
4356 }
4357 first_job = job;
4358 }
4359 return job;
4360}
4361
4362 void
4363job_set_options(job_T *job, jobopt_T *opt)
4364{
4365 if (opt->jo_set & JO_STOPONEXIT)
4366 {
4367 vim_free(job->jv_stoponexit);
4368 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4369 job->jv_stoponexit = NULL;
4370 else
4371 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4372 }
4373 if (opt->jo_set & JO_EXIT_CB)
4374 {
4375 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004376 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004377 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004378 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004379 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004380 job->jv_exit_partial = NULL;
4381 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004382 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004383 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004384 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004385 job->jv_exit_partial = opt->jo_exit_partial;
4386 if (job->jv_exit_partial != NULL)
4387 ++job->jv_exit_partial->pt_refcount;
4388 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004389 }
4390}
4391
4392/*
4393 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4394 */
4395 void
4396job_stop_on_exit()
4397{
4398 job_T *job;
4399
4400 for (job = first_job; job != NULL; job = job->jv_next)
4401 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4402 mch_stop_job(job, job->jv_stoponexit);
4403}
4404
4405/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004406 * Return TRUE when there is any job that might exit, which means
4407 * job_check_ended() should be called once in a while.
4408 */
4409 int
4410has_pending_job()
4411{
4412 job_T *job;
4413
4414 for (job = first_job; job != NULL; job = job->jv_next)
4415 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4416 return TRUE;
4417 return FALSE;
4418}
4419
4420/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004421 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004422 */
4423 void
4424job_check_ended(void)
4425{
4426 static time_t last_check = 0;
4427 time_t now;
4428 job_T *job;
4429 job_T *next;
4430
4431 /* Only do this once in 10 seconds. */
4432 now = time(NULL);
4433 if (last_check + 10 < now)
4434 {
4435 last_check = now;
4436 for (job = first_job; job != NULL; job = next)
4437 {
4438 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004439 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004440 job_status(job); /* may free "job" */
4441 }
4442 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004443 if (channel_need_redraw)
4444 {
4445 channel_need_redraw = FALSE;
4446 redraw_after_callback();
4447 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004448}
4449
4450/*
4451 * "job_start()" function
4452 */
4453 job_T *
4454job_start(typval_T *argvars)
4455{
4456 job_T *job;
4457 char_u *cmd = NULL;
4458#if defined(UNIX)
4459# define USE_ARGV
4460 char **argv = NULL;
4461 int argc = 0;
4462#else
4463 garray_T ga;
4464#endif
4465 jobopt_T opt;
4466 int part;
4467
4468 job = job_alloc();
4469 if (job == NULL)
4470 return NULL;
4471
4472 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004473#ifndef USE_ARGV
4474 ga_init2(&ga, (int)sizeof(char*), 20);
4475#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004476
4477 /* Default mode is NL. */
4478 clear_job_options(&opt);
4479 opt.jo_mode = MODE_NL;
4480 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004481 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4482 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004483 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004484
4485 /* Check that when io is "file" that there is a file name. */
4486 for (part = PART_OUT; part <= PART_IN; ++part)
4487 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4488 && opt.jo_io[part] == JIO_FILE
4489 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4490 || *opt.jo_io_name[part] == NUL))
4491 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004492 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004493 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494 }
4495
4496 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4497 {
4498 buf_T *buf = NULL;
4499
4500 /* check that we can find the buffer before starting the job */
4501 if (opt.jo_set & JO_IN_BUF)
4502 {
4503 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4504 if (buf == NULL)
4505 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4506 }
4507 else if (!(opt.jo_set & JO_IN_NAME))
4508 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004509 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004510 }
4511 else
4512 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4513 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004514 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004515 if (buf->b_ml.ml_mfp == NULL)
4516 {
4517 char_u numbuf[NUMBUFLEN];
4518 char_u *s;
4519
4520 if (opt.jo_set & JO_IN_BUF)
4521 {
4522 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4523 s = numbuf;
4524 }
4525 else
4526 s = opt.jo_io_name[PART_IN];
4527 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004528 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004529 }
4530 job->jv_in_buf = buf;
4531 }
4532
4533 job_set_options(job, &opt);
4534
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004535 if (argvars[0].v_type == VAR_STRING)
4536 {
4537 /* Command is a string. */
4538 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004539 if (cmd == NULL || *cmd == NUL)
4540 {
4541 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004542 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004543 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004544#ifdef USE_ARGV
4545 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004546 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004547 argv[argc] = NULL;
4548#endif
4549 }
4550 else if (argvars[0].v_type != VAR_LIST
4551 || argvars[0].vval.v_list == NULL
4552 || argvars[0].vval.v_list->lv_len < 1)
4553 {
4554 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004555 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004556 }
4557 else
4558 {
4559 list_T *l = argvars[0].vval.v_list;
4560 listitem_T *li;
4561 char_u *s;
4562
4563#ifdef USE_ARGV
4564 /* Pass argv[] to mch_call_shell(). */
4565 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4566 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004567 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568#endif
4569 for (li = l->lv_first; li != NULL; li = li->li_next)
4570 {
4571 s = get_tv_string_chk(&li->li_tv);
4572 if (s == NULL)
4573 goto theend;
4574#ifdef USE_ARGV
4575 argv[argc++] = (char *)s;
4576#else
4577 /* Only escape when needed, double quotes are not always allowed. */
4578 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4579 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004580# ifdef WIN32
4581 int old_ssl = p_ssl;
4582
4583 /* This is using CreateProcess, not cmd.exe. Always use
4584 * double quote and backslashes. */
4585 p_ssl = 0;
4586# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004587 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004588# ifdef WIN32
4589 p_ssl = old_ssl;
4590# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004591 if (s == NULL)
4592 goto theend;
4593 ga_concat(&ga, s);
4594 vim_free(s);
4595 }
4596 else
4597 ga_concat(&ga, s);
4598 if (li->li_next != NULL)
4599 ga_append(&ga, ' ');
4600#endif
4601 }
4602#ifdef USE_ARGV
4603 argv[argc] = NULL;
4604#else
4605 cmd = ga.ga_data;
4606#endif
4607 }
4608
4609#ifdef USE_ARGV
4610 if (ch_log_active())
4611 {
4612 garray_T ga;
4613 int i;
4614
4615 ga_init2(&ga, (int)sizeof(char), 200);
4616 for (i = 0; i < argc; ++i)
4617 {
4618 if (i > 0)
4619 ga_concat(&ga, (char_u *)" ");
4620 ga_concat(&ga, (char_u *)argv[i]);
4621 }
4622 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4623 ga_clear(&ga);
4624 }
4625 mch_start_job(argv, job, &opt);
4626#else
4627 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4628 mch_start_job((char *)cmd, job, &opt);
4629#endif
4630
4631 /* If the channel is reading from a buffer, write lines now. */
4632 if (job->jv_channel != NULL)
4633 channel_write_in(job->jv_channel);
4634
4635theend:
4636#ifdef USE_ARGV
4637 vim_free(argv);
4638#else
4639 vim_free(ga.ga_data);
4640#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004641 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004642 return job;
4643}
4644
4645/*
4646 * Get the status of "job" and invoke the exit callback when needed.
4647 * The returned string is not allocated.
4648 */
4649 char *
4650job_status(job_T *job)
4651{
4652 char *result;
4653
4654 if (job->jv_status == JOB_ENDED)
4655 /* No need to check, dead is dead. */
4656 result = "dead";
4657 else if (job->jv_status == JOB_FAILED)
4658 result = "fail";
4659 else
4660 {
4661 result = mch_job_status(job);
4662 if (job->jv_status == JOB_ENDED)
4663 ch_log(job->jv_channel, "Job ended");
4664 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4665 {
4666 typval_T argv[3];
4667 typval_T rettv;
4668 int dummy;
4669
4670 /* invoke the exit callback; make sure the refcount is > 0 */
4671 ++job->jv_refcount;
4672 argv[0].v_type = VAR_JOB;
4673 argv[0].vval.v_job = job;
4674 argv[1].v_type = VAR_NUMBER;
4675 argv[1].vval.v_number = job->jv_exitval;
4676 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004677 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4678 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004679 clear_tv(&rettv);
4680 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004681 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004682 }
4683 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4684 {
4685 /* The job was already unreferenced, now that it ended it can be
4686 * freed. Careful: caller must not use "job" after this! */
4687 job_free(job);
4688 }
4689 }
4690 return result;
4691}
4692
Bram Moolenaar8950a562016-03-12 15:22:55 +01004693/*
4694 * Implementation of job_info().
4695 */
4696 void
4697job_info(job_T *job, dict_T *dict)
4698{
4699 dictitem_T *item;
4700 varnumber_T nr;
4701
4702 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4703
4704 item = dictitem_alloc((char_u *)"channel");
4705 if (item == NULL)
4706 return;
4707 item->di_tv.v_lock = 0;
4708 item->di_tv.v_type = VAR_CHANNEL;
4709 item->di_tv.vval.v_channel = job->jv_channel;
4710 if (job->jv_channel != NULL)
4711 ++job->jv_channel->ch_refcount;
4712 if (dict_add(dict, item) == FAIL)
4713 dictitem_free(item);
4714
4715#ifdef UNIX
4716 nr = job->jv_pid;
4717#else
4718 nr = job->jv_proc_info.dwProcessId;
4719#endif
4720 dict_add_nr_str(dict, "process", nr, NULL);
4721
4722 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004723 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004724 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4725}
4726
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727 int
4728job_stop(job_T *job, typval_T *argvars)
4729{
4730 char_u *arg;
4731
4732 if (argvars[1].v_type == VAR_UNKNOWN)
4733 arg = (char_u *)"";
4734 else
4735 {
4736 arg = get_tv_string_chk(&argvars[1]);
4737 if (arg == NULL)
4738 {
4739 EMSG(_(e_invarg));
4740 return 0;
4741 }
4742 }
4743 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4744 if (mch_stop_job(job, arg) == FAIL)
4745 return 0;
4746
4747 /* Assume that "hup" does not kill the job. */
4748 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4749 job->jv_channel->ch_job_killed = TRUE;
4750
4751 /* We don't try freeing the job, obviously the caller still has a
4752 * reference to it. */
4753 return 1;
4754}
4755
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004756#endif /* FEAT_JOB_CHANNEL */