blob: ae894704ceaf85281dea76a34dd139d6ea1ccca4 [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)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001595 len += node->rq_buflen;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001596 res = lalloc(len, TRUE);
1597 if (res == NULL)
1598 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001599 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001600 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001601 {
1602 STRCPY(p, node->rq_buffer);
1603 p += node->rq_buflen;
1604 }
1605 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001606
1607 /* Free all buffers */
1608 do
1609 {
1610 p = channel_get(channel, part);
1611 vim_free(p);
1612 } while (p != NULL);
1613
1614 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615}
1616
1617/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001620 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001621 */
1622 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001623channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001625 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001626 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001627 readq_T *last_node;
1628 readq_T *n;
1629 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001630 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001631 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001632
Bram Moolenaar77073442016-02-13 23:23:53 +01001633 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001634 return FAIL;
1635
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001636 last_node = node->rq_next;
1637 len = node->rq_buflen + last_node->rq_buflen + 1;
1638 if (want_nl)
1639 while (last_node->rq_next != NULL
1640 && vim_strchr(last_node->rq_buffer, NL) == NULL)
1641 {
1642 last_node = last_node->rq_next;
1643 len += last_node->rq_buflen;
1644 }
1645
1646 p = newbuf = alloc(len);
1647 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001648 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001649 STRCPY(p, node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001650 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001651 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001652 node->rq_buffer = newbuf;
1653 for (n = node; n != last_node; )
1654 {
1655 n = n->rq_next;
1656 STRCPY(p, n->rq_buffer);
1657 p += n->rq_buflen;
1658 vim_free(n->rq_buffer);
1659 }
1660
1661 /* dispose of the collapsed nodes and their buffers */
1662 for (n = node->rq_next; n != last_node; )
1663 {
1664 n = n->rq_next;
1665 vim_free(n->rq_prev);
1666 }
1667 node->rq_next = last_node->rq_next;
1668 if (last_node->rq_next == NULL)
1669 head->rq_prev = node;
1670 else
1671 last_node->rq_next->rq_prev = node;
1672 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673 return OK;
1674}
1675
1676/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001677 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001678 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001679 * Returns OK or FAIL.
1680 */
1681 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001682channel_save(channel_T *channel, int part, char_u *buf, int len,
1683 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001684{
1685 readq_T *node;
1686 readq_T *head = &channel->ch_part[part].ch_head;
1687 char_u *p;
1688 int i;
1689
1690 node = (readq_T *)alloc(sizeof(readq_T));
1691 if (node == NULL)
1692 return FAIL; /* out of memory */
1693 node->rq_buffer = alloc(len + 1);
1694 if (node->rq_buffer == NULL)
1695 {
1696 vim_free(node);
1697 return FAIL; /* out of memory */
1698 }
1699
1700 if (channel->ch_part[part].ch_mode == MODE_NL)
1701 {
1702 /* Drop any CR before a NL. */
1703 p = node->rq_buffer;
1704 for (i = 0; i < len; ++i)
1705 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1706 *p++ = buf[i];
1707 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001708 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001709 }
1710 else
1711 {
1712 mch_memmove(node->rq_buffer, buf, len);
1713 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001714 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001715 }
1716
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001717 if (prepend)
1718 {
1719 /* preend node to the head of the queue */
1720 node->rq_next = head->rq_next;
1721 node->rq_prev = NULL;
1722 if (head->rq_next == NULL)
1723 head->rq_prev = node;
1724 else
1725 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001726 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001727 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001728 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001729 {
1730 /* append node to the tail of the queue */
1731 node->rq_next = NULL;
1732 node->rq_prev = head->rq_prev;
1733 if (head->rq_prev == NULL)
1734 head->rq_next = node;
1735 else
1736 head->rq_prev->rq_next = node;
1737 head->rq_prev = node;
1738 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001739
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001740 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001741 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001742 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001743 fprintf(log_fd, "'");
1744 if (fwrite(buf, len, 1, log_fd) != 1)
1745 return FAIL;
1746 fprintf(log_fd, "'\n");
1747 }
1748 return OK;
1749}
1750
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001751 static int
1752channel_fill(js_read_T *reader)
1753{
1754 channel_T *channel = (channel_T *)reader->js_cookie;
1755 int part = reader->js_cookie_arg;
1756 char_u *next = channel_get(channel, part);
1757 int unused;
1758 int len;
1759 char_u *p;
1760
1761 if (next == NULL)
1762 return FALSE;
1763
1764 unused = reader->js_end - reader->js_buf - reader->js_used;
1765 if (unused > 0)
1766 {
1767 /* Prepend unused text. */
1768 len = (int)STRLEN(next);
1769 p = alloc(unused + len + 1);
1770 if (p == NULL)
1771 {
1772 vim_free(next);
1773 return FALSE;
1774 }
1775 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1776 mch_memmove(p + unused, next, len + 1);
1777 vim_free(next);
1778 next = p;
1779 }
1780
1781 vim_free(reader->js_buf);
1782 reader->js_buf = next;
1783 reader->js_used = 0;
1784 return TRUE;
1785}
1786
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001787/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001788 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001789 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001790 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001791 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001792 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794{
1795 js_read_T reader;
1796 typval_T listtv;
1797 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001798 chanpart_T *chanpart = &channel->ch_part[part];
1799 jsonq_T *head = &chanpart->ch_json_head;
1800 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001801 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001802
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001803 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001804 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001805
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001806 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001807 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001808 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001809 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001810 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001811
1812 /* When a message is incomplete we wait for a short while for more to
1813 * arrive. After the delay drop the input, otherwise a truncated string
1814 * or list will make us hang. */
1815 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001816 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001817 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001818 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001819 /* Only accept the response when it is a list with at least two
1820 * items. */
1821 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001822 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001823 if (listtv.v_type != VAR_LIST)
1824 ch_error(channel, "Did not receive a list, discarding");
1825 else
1826 ch_errorn(channel, "Expected list with two items, got %d",
1827 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001828 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001829 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001830 else
1831 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001832 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1833 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001834 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001835 else
1836 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001837 item->jq_value = alloc_tv();
1838 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001839 {
1840 vim_free(item);
1841 clear_tv(&listtv);
1842 }
1843 else
1844 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001845 *item->jq_value = listtv;
1846 item->jq_prev = head->jq_prev;
1847 head->jq_prev = item;
1848 item->jq_next = NULL;
1849 if (item->jq_prev == NULL)
1850 head->jq_next = item;
1851 else
1852 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001853 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001854 }
1855 }
1856 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001857
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001858 if (status == OK)
1859 chanpart->ch_waiting = FALSE;
1860 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001861 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001862 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001863 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001864 /* First time encountering incomplete message, set a deadline of
1865 * 100 msec. */
1866 ch_log(channel, "Incomplete message - wait for more");
1867 reader.js_used = 0;
1868 chanpart->ch_waiting = TRUE;
1869#ifdef WIN32
1870 chanpart->ch_deadline = GetTickCount() + 100L;
1871#else
1872 gettimeofday(&chanpart->ch_deadline, NULL);
1873 chanpart->ch_deadline.tv_usec += 100 * 1000;
1874 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1875 {
1876 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1877 ++chanpart->ch_deadline.tv_sec;
1878 }
1879#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001880 }
1881 else
1882 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001883 int timeout;
1884#ifdef WIN32
1885 timeout = GetTickCount() > chanpart->ch_deadline;
1886#else
1887 {
1888 struct timeval now_tv;
1889
1890 gettimeofday(&now_tv, NULL);
1891 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1892 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1893 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1894 }
1895#endif
1896 if (timeout)
1897 {
1898 status = FAIL;
1899 chanpart->ch_waiting = FALSE;
1900 }
1901 else
1902 {
1903 reader.js_used = 0;
1904 ch_log(channel, "still waiting on incomplete message");
1905 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001906 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001907 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001908
1909 if (status == FAIL)
1910 {
1911 ch_error(channel, "Decoding failed - discarding input");
1912 ret = FALSE;
1913 chanpart->ch_waiting = FALSE;
1914 }
1915 else if (reader.js_buf[reader.js_used] != NUL)
1916 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001917 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001918 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001919 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1920 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001921 ret = status == MAYBE ? FALSE: TRUE;
1922 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001923 else
1924 ret = FALSE;
1925
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001926 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001927 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001928}
1929
1930/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001931 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001932 */
1933 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001934remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001935{
Bram Moolenaar77073442016-02-13 23:23:53 +01001936 if (node->cq_prev == NULL)
1937 head->cq_next = node->cq_next;
1938 else
1939 node->cq_prev->cq_next = node->cq_next;
1940 if (node->cq_next == NULL)
1941 head->cq_prev = node->cq_prev;
1942 else
1943 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001944}
1945
1946/*
1947 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001948 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949 */
1950 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001951remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001952{
Bram Moolenaar77073442016-02-13 23:23:53 +01001953 if (node->jq_prev == NULL)
1954 head->jq_next = node->jq_next;
1955 else
1956 node->jq_prev->jq_next = node->jq_next;
1957 if (node->jq_next == NULL)
1958 head->jq_prev = node->jq_prev;
1959 else
1960 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001961 vim_free(node);
1962}
1963
1964/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001965 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001966 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001967 * When "id" is zero or negative jut get the first message. But not the one
1968 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001969 * Return OK when found and return the value in "rettv".
1970 * Return FAIL otherwise.
1971 */
1972 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001973channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001974{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001975 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001976 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001977
Bram Moolenaar77073442016-02-13 23:23:53 +01001978 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001979 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001980 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001981 typval_T *tv = &l->lv_first->li_tv;
1982
1983 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001984 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001985 || tv->vval.v_number == 0
1986 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001987 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001988 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001989 if (tv->v_type == VAR_NUMBER)
1990 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001991 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001992 return OK;
1993 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001994 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001995 }
1996 return FAIL;
1997}
1998
Bram Moolenaarece61b02016-02-20 21:39:05 +01001999#define CH_JSON_MAX_ARGS 4
2000
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002001/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002002 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002003 * "argv[0]" is the command string.
2004 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002005 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002006 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002007channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002008{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002009 char_u *cmd = argv[0].vval.v_string;
2010 char_u *arg;
2011 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002012
Bram Moolenaarece61b02016-02-20 21:39:05 +01002013 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002014 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002015 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002016 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002017 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002018 return;
2019 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002020 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002021 if (arg == NULL)
2022 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002023
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002024 if (STRCMP(cmd, "ex") == 0)
2025 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002026 int save_called_emsg = called_emsg;
2027
2028 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002029 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002030 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002031 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002032 --emsg_silent;
2033 if (called_emsg)
2034 ch_logs(channel, "Ex command error: '%s'",
2035 (char *)get_vim_var_str(VV_ERRMSG));
2036 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002037 }
2038 else if (STRCMP(cmd, "normal") == 0)
2039 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002040 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002041
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002042 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002043 ea.arg = arg;
2044 ea.addr_count = 0;
2045 ea.forceit = TRUE; /* no mapping */
2046 ex_normal(&ea);
2047 }
2048 else if (STRCMP(cmd, "redraw") == 0)
2049 {
2050 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002051
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002052 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002053 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002054 ex_redraw(&ea);
2055 showruler(FALSE);
2056 setcursor();
2057 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002058#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002059 if (gui.in_use)
2060 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002061 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002062 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002063 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002064#endif
2065 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002066 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002067 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002068 int is_call = cmd[0] == 'c';
2069 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002070
Bram Moolenaarece61b02016-02-20 21:39:05 +01002071 if (argv[id_idx].v_type != VAR_UNKNOWN
2072 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002073 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002074 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002075 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002076 EMSG("E904: last argument for expr/call must be a number");
2077 }
2078 else if (is_call && argv[2].v_type != VAR_LIST)
2079 {
2080 ch_error(channel, "third argument for call must be a list");
2081 if (p_verbose > 2)
2082 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002083 }
2084 else
2085 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002086 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002087 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002088 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002089 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002090
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002091 /* Don't pollute the display with errors. */
2092 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002093 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002094 {
2095 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002096 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002097 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002098 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002099 {
2100 ch_logs(channel, "Calling '%s'", (char *)arg);
2101 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2102 tv = &res_tv;
2103 else
2104 tv = NULL;
2105 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002106
2107 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002108 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002109 int id = argv[id_idx].vval.v_number;
2110
Bram Moolenaar55fab432016-02-07 16:53:13 +01002111 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002112 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002113 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002114 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002115 /* If evaluation failed or the result can't be encoded
2116 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002117 vim_free(json);
2118 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002119 err_tv.v_type = VAR_STRING;
2120 err_tv.vval.v_string = (char_u *)"ERROR";
2121 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002122 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002123 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002124 if (json != NULL)
2125 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002126 channel_send(channel,
2127 part == PART_SOCK ? PART_SOCK : PART_IN,
2128 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002129 vim_free(json);
2130 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002131 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002132 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002133 if (tv == &res_tv)
2134 clear_tv(tv);
2135 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002136 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002137 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002138 }
2139 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002140 {
2141 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002142 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002143 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002144}
2145
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002146/*
2147 * Invoke the callback at "cbhead".
2148 * Does not redraw but sets channel_need_redraw.
2149 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002150 static void
2151invoke_one_time_callback(
2152 channel_T *channel,
2153 cbq_T *cbhead,
2154 cbq_T *item,
2155 typval_T *argv)
2156{
2157 ch_logs(channel, "Invoking one-time callback %s",
2158 (char *)item->cq_callback);
2159 /* Remove the item from the list first, if the callback
2160 * invokes ch_close() the list will be cleared. */
2161 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002162 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002163 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002164 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002165 vim_free(item);
2166}
2167
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002168 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002169append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002170{
2171 buf_T *save_curbuf = curbuf;
2172 linenr_T lnum = buffer->b_ml.ml_line_count;
2173 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002174 chanpart_T *ch_part = &channel->ch_part[part];
2175 int save_p_ma = buffer->b_p_ma;
2176
2177 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2178 {
2179 if (!ch_part->ch_nomod_error)
2180 {
2181 ch_error(channel, "Buffer is not modifiable, cannot append");
2182 ch_part->ch_nomod_error = TRUE;
2183 }
2184 return;
2185 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002186
2187 /* If the buffer is also used as input insert above the last
2188 * line. Don't write these lines. */
2189 if (save_write_to)
2190 {
2191 --lnum;
2192 buffer->b_write_to_channel = FALSE;
2193 }
2194
2195 /* Append to the buffer */
2196 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2197
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002198 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002199 curbuf = buffer;
2200 u_sync(TRUE);
2201 /* ignore undo failure, undo is not very useful here */
2202 ignored = u_save(lnum, lnum + 1);
2203
2204 ml_append(lnum, msg, 0, FALSE);
2205 appended_lines_mark(lnum, 1L);
2206 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002207 if (ch_part->ch_nomodifiable)
2208 buffer->b_p_ma = FALSE;
2209 else
2210 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002211
2212 if (buffer->b_nwindows > 0)
2213 {
2214 win_T *wp;
2215 win_T *save_curwin;
2216
2217 FOR_ALL_WINDOWS(wp)
2218 {
2219 if (wp->w_buffer == buffer
2220 && (save_write_to
2221 ? wp->w_cursor.lnum == lnum + 1
2222 : (wp->w_cursor.lnum == lnum
2223 && wp->w_cursor.col == 0)))
2224 {
2225 ++wp->w_cursor.lnum;
2226 save_curwin = curwin;
2227 curwin = wp;
2228 curbuf = curwin->w_buffer;
2229 scroll_cursor_bot(0, FALSE);
2230 curwin = save_curwin;
2231 curbuf = curwin->w_buffer;
2232 }
2233 }
2234 redraw_buf_later(buffer, VALID);
2235 channel_need_redraw = TRUE;
2236 }
2237
2238 if (save_write_to)
2239 {
2240 channel_T *ch;
2241
2242 /* Find channels reading from this buffer and adjust their
2243 * next-to-read line number. */
2244 buffer->b_write_to_channel = TRUE;
2245 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2246 {
2247 chanpart_T *in_part = &ch->ch_part[PART_IN];
2248
2249 if (in_part->ch_buffer == buffer)
2250 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2251 }
2252 }
2253}
2254
Bram Moolenaar437905c2016-04-26 19:01:05 +02002255 static void
2256drop_messages(channel_T *channel, int part)
2257{
2258 char_u *msg;
2259
2260 while ((msg = channel_get(channel, part)) != NULL)
2261 {
2262 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2263 vim_free(msg);
2264 }
2265}
2266
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002267/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002268 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002269 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002270 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002271 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002272 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002273may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002274{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002275 char_u *msg = NULL;
2276 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002278 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002279 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002280 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002281 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002282 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002283 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002284 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002285
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002286 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002287 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002288 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002289
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002290 /* Use a message-specific callback, part callback or channel callback */
2291 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2292 if (cbitem->cq_seq_nr == 0)
2293 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002294 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002295 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002296 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002297 partial = cbitem->cq_partial;
2298 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002299 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002300 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002301 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002302 partial = channel->ch_part[part].ch_partial;
2303 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002304 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002305 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002306 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002307 partial = channel->ch_partial;
2308 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002309
Bram Moolenaar187db502016-02-27 14:44:26 +01002310 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002311 if (buffer != NULL && !buf_valid(buffer))
2312 {
2313 /* buffer was wiped out */
2314 channel->ch_part[part].ch_buffer = NULL;
2315 buffer = NULL;
2316 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002317
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002318 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002319 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002320 listitem_T *item;
2321 int argc = 0;
2322
Bram Moolenaard7ece102016-02-02 23:23:02 +01002323 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002324 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002325 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002326 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002327 channel_parse_json(channel, part);
2328 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002329 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002330 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002331
Bram Moolenaarece61b02016-02-20 21:39:05 +01002332 for (item = listtv->vval.v_list->lv_first;
2333 item != NULL && argc < CH_JSON_MAX_ARGS;
2334 item = item->li_next)
2335 argv[argc++] = item->li_tv;
2336 while (argc < CH_JSON_MAX_ARGS)
2337 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002338
Bram Moolenaarece61b02016-02-20 21:39:05 +01002339 if (argv[0].v_type == VAR_STRING)
2340 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002341 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002342 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002343 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002344 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002345 }
2346
Bram Moolenaarece61b02016-02-20 21:39:05 +01002347 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002348 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002349 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002350 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002351 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002352 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002353 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002354 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002355 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002356 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002357 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002358 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002359 return FALSE;
2360 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002361 else
2362 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002363 /* If there is no callback or buffer drop the message. */
2364 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002365 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002366 /* If there is a close callback it may use ch_read() to get the
2367 * messages. */
2368 if (channel->ch_close_cb == NULL)
2369 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002370 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002371 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002372
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002373 if (ch_mode == MODE_NL)
2374 {
2375 char_u *nl;
2376 char_u *buf;
2377
2378 /* See if we have a message ending in NL in the first buffer. If
2379 * not try to concatenate the first and the second buffer. */
2380 while (TRUE)
2381 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002382 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002383 nl = vim_strchr(buf, NL);
2384 if (nl != NULL)
2385 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002386 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002387 return FALSE; /* incomplete message */
2388 }
2389 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002390 {
2391 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002392 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002393 *nl = NUL;
2394 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002395 else
2396 {
2397 /* Copy the message into allocated memory and remove it from
2398 * the buffer. */
2399 msg = vim_strnsave(buf, (int)(nl - buf));
2400 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2401 }
2402 }
2403 else
2404 /* For a raw channel we don't know where the message ends, just
2405 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002406 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002407
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002408 if (msg == NULL)
2409 return FALSE; /* out of memory (and avoids Coverity warning) */
2410
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002411 argv[1].v_type = VAR_STRING;
2412 argv[1].vval.v_string = msg;
2413 }
2414
Bram Moolenaara07fec92016-02-05 21:04:08 +01002415 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002416 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002417 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002418
2419 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002420 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002421 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002422 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002423 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002424 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002425 break;
2426 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002427 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002428 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002429 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002430 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002431 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002432 if (buffer != NULL)
2433 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002434 if (msg == NULL)
2435 /* JSON or JS mode: re-encode the message. */
2436 msg = json_encode(listtv, ch_mode);
2437 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002438 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002439 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002440
Bram Moolenaar187db502016-02-27 14:44:26 +01002441 if (callback != NULL)
2442 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002443 if (cbitem != NULL)
2444 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2445 else
2446 {
2447 /* invoke the channel callback */
2448 ch_logs(channel, "Invoking channel callback %s",
2449 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002450 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002451 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002452 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002453 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002454 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002455 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002456
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002457 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002458 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002459 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002460
2461 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002462}
2463
2464/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002465 * Return TRUE when channel "channel" is open for writing to.
2466 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002467 */
2468 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002469channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002470{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002471 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002472 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002473}
2474
2475/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002476 * Return TRUE when channel "channel" is open for reading or writing.
2477 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002478 */
2479 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002480channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002481{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002482 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002483 || channel->CH_IN_FD != INVALID_FD
2484 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002485 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002486}
2487
2488/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002489 * Return TRUE if "channel" has JSON or other typeahead.
2490 */
2491 static int
2492channel_has_readahead(channel_T *channel, int part)
2493{
2494 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2495
2496 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2497 {
2498 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2499 jsonq_T *item = head->jq_next;
2500
2501 return item != NULL;
2502 }
2503 return channel_peek(channel, part) != NULL;
2504}
2505
2506/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002507 * Return a string indicating the status of the channel.
2508 */
2509 char *
2510channel_status(channel_T *channel)
2511{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002512 int part;
2513 int has_readahead = FALSE;
2514
Bram Moolenaar77073442016-02-13 23:23:53 +01002515 if (channel == NULL)
2516 return "fail";
2517 if (channel_is_open(channel))
2518 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002519 for (part = PART_SOCK; part <= PART_ERR; ++part)
2520 if (channel_has_readahead(channel, part))
2521 {
2522 has_readahead = TRUE;
2523 break;
2524 }
2525
2526 if (has_readahead)
2527 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002528 return "closed";
2529}
2530
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002531 static void
2532channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2533{
2534 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002535 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002536 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002537 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002538
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002539 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002540 STRCAT(namebuf, "_");
2541 tail = STRLEN(namebuf);
2542
2543 STRCPY(namebuf + tail, "status");
2544 dict_add_nr_str(dict, namebuf, 0,
2545 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2546
2547 STRCPY(namebuf + tail, "mode");
2548 switch (chanpart->ch_mode)
2549 {
2550 case MODE_NL: s = "NL"; break;
2551 case MODE_RAW: s = "RAW"; break;
2552 case MODE_JSON: s = "JSON"; break;
2553 case MODE_JS: s = "JS"; break;
2554 }
2555 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2556
2557 STRCPY(namebuf + tail, "io");
2558 if (part == PART_SOCK)
2559 s = "socket";
2560 else switch (chanpart->ch_io)
2561 {
2562 case JIO_NULL: s = "null"; break;
2563 case JIO_PIPE: s = "pipe"; break;
2564 case JIO_FILE: s = "file"; break;
2565 case JIO_BUFFER: s = "buffer"; break;
2566 case JIO_OUT: s = "out"; break;
2567 }
2568 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2569
2570 STRCPY(namebuf + tail, "timeout");
2571 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2572}
2573
2574 void
2575channel_info(channel_T *channel, dict_T *dict)
2576{
2577 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2578 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2579
2580 if (channel->ch_hostname != NULL)
2581 {
2582 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2583 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2584 channel_part_info(channel, dict, "sock", PART_SOCK);
2585 }
2586 else
2587 {
2588 channel_part_info(channel, dict, "out", PART_OUT);
2589 channel_part_info(channel, dict, "err", PART_ERR);
2590 channel_part_info(channel, dict, "in", PART_IN);
2591 }
2592}
2593
Bram Moolenaar77073442016-02-13 23:23:53 +01002594/*
2595 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002596 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002597 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002598 */
2599 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002600channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002601{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002602 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002603
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002604#ifdef FEAT_GUI
2605 channel_gui_unregister(channel);
2606#endif
2607
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002608 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002609 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002610 sock_close(channel->CH_SOCK_FD);
2611 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002612 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002613 may_close_part(&channel->CH_IN_FD);
2614 may_close_part(&channel->CH_OUT_FD);
2615 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002616
Bram Moolenaar8b374212016-02-24 20:43:06 +01002617 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002618 {
2619 typval_T argv[1];
2620 typval_T rettv;
2621 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002622 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002623
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002624 /* Invoke callbacks before the close callback, since it's weird to
2625 * first invoke the close callback. Increment the refcount to avoid
2626 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002627 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002628 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002629 for (part = PART_SOCK; part <= PART_ERR; ++part)
2630 while (may_invoke_callback(channel, part))
2631 ;
2632
2633 /* Invoke the close callback, if still set. */
2634 if (channel->ch_close_cb != NULL)
2635 {
2636 ch_logs(channel, "Invoking close callback %s",
2637 (char *)channel->ch_close_cb);
2638 argv[0].v_type = VAR_CHANNEL;
2639 argv[0].vval.v_channel = channel;
2640 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002641 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2642 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002643 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002644 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002645 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002646
2647 /* the callback is only called once */
2648 vim_free(channel->ch_close_cb);
2649 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002650 partial_unref(channel->ch_close_partial);
2651 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002652
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002653 --channel->ch_refcount;
2654
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002655 if (channel_need_redraw)
2656 {
2657 channel_need_redraw = FALSE;
2658 redraw_after_callback();
2659 }
2660
Bram Moolenaar437905c2016-04-26 19:01:05 +02002661 /* any remaining messages are useless now */
2662 for (part = PART_SOCK; part <= PART_ERR; ++part)
2663 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002664 }
2665
2666 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002667}
2668
Bram Moolenaard04a0202016-01-26 23:30:18 +01002669/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002670 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002671 * Returns NULL if there is nothing.
2672 */
2673 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002675{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002676 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002677
Bram Moolenaar77073442016-02-13 23:23:53 +01002678 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002679 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002680 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002681}
2682
2683/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002684 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002685 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002686 static void
2687channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002688{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002689 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2690 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002691
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002692 while (channel_peek(channel, part) != NULL)
2693 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002694
2695 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002696 {
2697 cbq_T *node = cb_head->cq_next;
2698
2699 remove_cb_node(cb_head, node);
2700 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002701 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002702 vim_free(node);
2703 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002704
2705 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002706 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002707 free_tv(json_head->jq_next->jq_value);
2708 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002709 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002710
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002711 vim_free(channel->ch_part[part].ch_callback);
2712 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002713 partial_unref(channel->ch_part[part].ch_partial);
2714 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002715}
2716
2717/*
2718 * Clear all the read buffers on "channel".
2719 */
2720 void
2721channel_clear(channel_T *channel)
2722{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002723 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002724 vim_free(channel->ch_hostname);
2725 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002726 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002727 channel_clear_one(channel, PART_OUT);
2728 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002729 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002730 vim_free(channel->ch_callback);
2731 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002732 partial_unref(channel->ch_partial);
2733 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002734 vim_free(channel->ch_close_cb);
2735 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002736 partial_unref(channel->ch_close_partial);
2737 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002738}
2739
Bram Moolenaar77073442016-02-13 23:23:53 +01002740#if defined(EXITFREE) || defined(PROTO)
2741 void
2742channel_free_all(void)
2743{
2744 channel_T *channel;
2745
Bram Moolenaard6051b52016-02-28 15:49:03 +01002746 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002747 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2748 channel_clear(channel);
2749}
2750#endif
2751
2752
Bram Moolenaar715d2852016-04-30 17:06:31 +02002753/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002754#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002755
2756/* Buffer size for reading incoming messages. */
2757#define MAXMSGSIZE 4096
2758
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002759#if defined(HAVE_SELECT)
2760/*
2761 * Add write fds where we are waiting for writing to be possible.
2762 */
2763 static int
2764channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2765{
2766 int maxfd = maxfd_arg;
2767 channel_T *ch;
2768
2769 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2770 {
2771 chanpart_T *in_part = &ch->ch_part[PART_IN];
2772
2773 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2774 {
2775 FD_SET((int)in_part->ch_fd, wfds);
2776 if ((int)in_part->ch_fd >= maxfd)
2777 maxfd = (int)in_part->ch_fd + 1;
2778 }
2779 }
2780 return maxfd;
2781}
2782#else
2783/*
2784 * Add write fds where we are waiting for writing to be possible.
2785 */
2786 static int
2787channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2788{
2789 int nfd = nfd_in;
2790 channel_T *ch;
2791
2792 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2793 {
2794 chanpart_T *in_part = &ch->ch_part[PART_IN];
2795
2796 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2797 {
2798 in_part->ch_poll_idx = nfd;
2799 fds[nfd].fd = in_part->ch_fd;
2800 fds[nfd].events = POLLOUT;
2801 ++nfd;
2802 }
2803 else
2804 in_part->ch_poll_idx = -1;
2805 }
2806 return nfd;
2807}
2808#endif
2809
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002810typedef enum {
2811 CW_READY,
2812 CW_NOT_READY,
2813 CW_ERROR
2814} channel_wait_result;
2815
Bram Moolenaard04a0202016-01-26 23:30:18 +01002816/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002817 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002818 * Return CW_READY when there is something to read.
2819 * Return CW_NOT_READY when there is nothing to read.
2820 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002821 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002822 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002823channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002824{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002825 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002826 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002827
Bram Moolenaard8070362016-02-15 21:56:54 +01002828# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002829 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002830 {
2831 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002832 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002833 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002834 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002835
2836 /* reading from a pipe, not a socket */
2837 while (TRUE)
2838 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002839 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2840
2841 if (r && nread > 0)
2842 return CW_READY;
2843 if (r == 0)
2844 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002845
2846 /* perhaps write some buffer lines */
2847 channel_write_any_lines();
2848
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002849 sleep_time = deadline - GetTickCount();
2850 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002851 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002852 /* Wait for a little while. Very short at first, up to 10 msec
2853 * after looping a few times. */
2854 if (sleep_time > delay)
2855 sleep_time = delay;
2856 Sleep(sleep_time);
2857 delay = delay * 2;
2858 if (delay > 10)
2859 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002860 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002861 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002862 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002863#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002864 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002865#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002866 struct timeval tval;
2867 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002868 fd_set wfds;
2869 int ret;
2870 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002871
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002872 tval.tv_sec = timeout / 1000;
2873 tval.tv_usec = (timeout % 1000) * 1000;
2874 for (;;)
2875 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002876 FD_ZERO(&rfds);
2877 FD_SET((int)fd, &rfds);
2878
2879 /* Write lines to a pipe when a pipe can be written to. Need to
2880 * set this every time, some buffers may be done. */
2881 maxfd = (int)fd + 1;
2882 FD_ZERO(&wfds);
2883 maxfd = channel_fill_wfds(maxfd, &wfds);
2884
2885 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002886# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002887 SOCK_ERRNO;
2888 if (ret == -1 && errno == EINTR)
2889 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002890# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002891 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002892 {
2893 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002894 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002895 channel_write_any_lines();
2896 continue;
2897 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002898 break;
2899 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002900#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002901 for (;;)
2902 {
2903 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2904 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002905
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002906 fds[0].fd = fd;
2907 fds[0].events = POLLIN;
2908 nfd = channel_fill_poll_write(nfd, fds);
2909 if (poll(fds, nfd, timeout) > 0)
2910 {
2911 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002912 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002913 channel_write_any_lines();
2914 continue;
2915 }
2916 break;
2917 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002918#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002919 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002920 return CW_NOT_READY;
2921}
2922
2923 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002924channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002925{
2926 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002927 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2928 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002929
2930 /* Queue a "DETACH" netbeans message in the command queue in order to
2931 * terminate the netbeans session later. Do not end the session here
2932 * directly as we may be running in the context of a call to
2933 * netbeans_parse_messages():
2934 * netbeans_parse_messages
2935 * -> autocmd triggered while processing the netbeans cmd
2936 * -> ui_breakcheck
2937 * -> gui event loop or select loop
2938 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002939 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002940 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002941 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002942 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002943 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2944
2945 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002946 * died. Don't close the channel right away, it may be the wrong moment
2947 * to invoke callbacks. */
2948 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002949
2950#ifdef FEAT_GUI
2951 /* Stop listening to GUI events right away. */
2952 channel_gui_unregister(channel);
2953#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002954}
2955
2956 static void
2957channel_close_now(channel_T *channel)
2958{
2959 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002960 channel_close(channel, TRUE);
2961 if (channel->ch_nb_close_cb != NULL)
2962 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002963}
2964
2965/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002966 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002967 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02002968 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002969 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002970 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002971channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002972{
2973 static char_u *buf = NULL;
2974 int len = 0;
2975 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002976 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002977 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002978
Bram Moolenaar5850a762016-05-27 19:59:48 +02002979 /* If we detected a read error don't try reading again. */
2980 if (channel->ch_to_be_closed)
2981 return;
2982
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002983 fd = channel->ch_part[part].ch_fd;
2984 if (fd == INVALID_FD)
2985 {
2986 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002987 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002988 }
2989 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002990
2991 /* Allocate a buffer to read into. */
2992 if (buf == NULL)
2993 {
2994 buf = alloc(MAXMSGSIZE);
2995 if (buf == NULL)
2996 return; /* out of memory! */
2997 }
2998
2999 /* Keep on reading for as long as there is something to read.
3000 * Use select() or poll() to avoid blocking on a message that is exactly
3001 * MAXMSGSIZE long. */
3002 for (;;)
3003 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003004 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003005 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003006 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003007 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003008 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003009 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003010 if (len <= 0)
3011 break; /* error or nothing more to read */
3012
3013 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003014 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003015 readlen += len;
3016 if (len < MAXMSGSIZE)
3017 break; /* did read everything that's available */
3018 }
3019
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003020 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003021 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003022 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003023
3024#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003025 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003026 if (CH_HAS_GUI && gtk_main_level() > 0)
3027 gtk_main_quit();
3028#endif
3029}
3030
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003031/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003032 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003033 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003034 * Returns what was read in allocated memory.
3035 * Returns NULL in case of error or timeout.
3036 */
3037 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003038channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003039{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003040 char_u *buf;
3041 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003042 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003043 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003044 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003045
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003046 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003047 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003048
3049 while (TRUE)
3050 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003051 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003052 if (buf != NULL && (mode == MODE_RAW
3053 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
3054 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02003055 if (buf != NULL && channel_collapse(channel, part, mode == MODE_NL)
3056 == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003057 continue;
3058
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003059 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003060 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003061 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003062 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003063 {
3064 ch_log(channel, "Timed out");
3065 return NULL;
3066 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003067 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003068 }
3069
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003070 if (mode == MODE_RAW)
3071 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003072 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003073 }
3074 else
3075 {
3076 nl = vim_strchr(buf, NL);
3077 if (nl[1] == NUL)
3078 {
3079 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003080 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003081 *nl = NUL;
3082 }
3083 else
3084 {
3085 /* Copy the message into allocated memory and remove it from the
3086 * buffer. */
3087 msg = vim_strnsave(buf, (int)(nl - buf));
3088 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3089 }
3090 }
3091 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003092 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003093 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003094}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003095
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003096/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003097 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003098 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003099 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003100 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003101 */
3102 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003103channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003104 channel_T *channel,
3105 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003106 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003107 int id,
3108 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003109{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003110 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003111 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003112 int timeout;
3113 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003114
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003115 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003116 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003117 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003118 for (;;)
3119 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003120 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003121
3122 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003123 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003124 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003125 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003126 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003127 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003128
Bram Moolenaard7ece102016-02-02 23:23:02 +01003129 if (!more)
3130 {
3131 /* Handle any other messages in the queue. If done some more
3132 * messages may have arrived. */
3133 if (channel_parse_messages())
3134 continue;
3135
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003136 /* Wait for up to the timeout. If there was an incomplete message
3137 * use the deadline for that. */
3138 timeout = timeout_arg;
3139 if (chanpart->ch_waiting)
3140 {
3141#ifdef WIN32
3142 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3143#else
3144 {
3145 struct timeval now_tv;
3146
3147 gettimeofday(&now_tv, NULL);
3148 timeout = (chanpart->ch_deadline.tv_sec
3149 - now_tv.tv_sec) * 1000
3150 + (chanpart->ch_deadline.tv_usec
3151 - now_tv.tv_usec) / 1000
3152 + 1;
3153 }
3154#endif
3155 if (timeout < 0)
3156 {
3157 /* Something went wrong, channel_parse_json() didn't
3158 * discard message. Cancel waiting. */
3159 chanpart->ch_waiting = FALSE;
3160 timeout = timeout_arg;
3161 }
3162 else if (timeout > timeout_arg)
3163 timeout = timeout_arg;
3164 }
3165 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003166 if (fd == INVALID_FD
3167 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003168 {
3169 if (timeout == timeout_arg)
3170 {
3171 if (fd != INVALID_FD)
3172 ch_log(channel, "Timed out");
3173 break;
3174 }
3175 }
3176 else
3177 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003178 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003179 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003180 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003181 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003182}
3183
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003184/*
3185 * Common for ch_read() and ch_readraw().
3186 */
3187 void
3188common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3189{
3190 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003191 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003192 jobopt_T opt;
3193 int mode;
3194 int timeout;
3195 int id = -1;
3196 typval_T *listtv = NULL;
3197
3198 /* return an empty string by default */
3199 rettv->v_type = VAR_STRING;
3200 rettv->vval.v_string = NULL;
3201
3202 clear_job_options(&opt);
3203 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3204 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003205 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003206
Bram Moolenaar437905c2016-04-26 19:01:05 +02003207 if (opt.jo_set & JO_PART)
3208 part = opt.jo_part;
3209 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003210 if (channel != NULL)
3211 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003212 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003213 part = channel_part_read(channel);
3214 mode = channel_get_mode(channel, part);
3215 timeout = channel_get_timeout(channel, part);
3216 if (opt.jo_set & JO_TIMEOUT)
3217 timeout = opt.jo_timeout;
3218
3219 if (raw || mode == MODE_RAW || mode == MODE_NL)
3220 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3221 else
3222 {
3223 if (opt.jo_set & JO_ID)
3224 id = opt.jo_id;
3225 channel_read_json_block(channel, part, timeout, id, &listtv);
3226 if (listtv != NULL)
3227 {
3228 *rettv = *listtv;
3229 vim_free(listtv);
3230 }
3231 else
3232 {
3233 rettv->v_type = VAR_SPECIAL;
3234 rettv->vval.v_number = VVAL_NONE;
3235 }
3236 }
3237 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003238
3239theend:
3240 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003241}
3242
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003243# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3244 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003245/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003246 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003247 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003248 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003249 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003250channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003251{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003252 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003253 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003254
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003255 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003256 for (channel = first_channel; channel != NULL;
3257 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003258 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003259 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003260 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003261 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003262 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003263 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003264 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003265 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003266 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003267}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003268# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003269
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003270# if defined(WIN32) || defined(PROTO)
3271/*
3272 * Check the channels for anything that is ready to be read.
3273 * The data is put in the read queue.
3274 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003275 void
3276channel_handle_events(void)
3277{
3278 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003279 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003280 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003281
3282 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3283 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003284 /* If we detected a read error don't try reading again. */
3285 if (channel->ch_to_be_closed)
3286 continue;
3287
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003288 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003289 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003290 {
3291 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003292 if (fd != INVALID_FD)
3293 {
3294 int r = channel_wait(channel, fd, 0);
3295
3296 if (r == CW_READY)
3297 channel_read(channel, part, "channel_handle_events");
3298 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003299 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003300 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003301 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003302 }
3303}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003304# endif
3305
Bram Moolenaard04a0202016-01-26 23:30:18 +01003306/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003307 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003308 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003309 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003310 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003311 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003312channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003313{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003314 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003315 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003316 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003317
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003318 fd = channel->ch_part[part].ch_fd;
3319 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003320 {
3321 if (!channel->ch_error && fun != NULL)
3322 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003323 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003324 EMSG2("E630: %s(): write while not connected", fun);
3325 }
3326 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003327 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003328 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003329
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003330 if (log_fd != NULL)
3331 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003332 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003333 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003334 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003335 fprintf(log_fd, "'\n");
3336 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003337 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003338 }
3339
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003340 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003341 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003342 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003343 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003344 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003345 {
3346 if (!channel->ch_error && fun != NULL)
3347 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003348 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003349 EMSG2("E631: %s(): write failed", fun);
3350 }
3351 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003352 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003353 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003354
3355 channel->ch_error = FALSE;
3356 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003357}
3358
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003359/*
3360 * Common for "ch_sendexpr()" and "ch_sendraw()".
3361 * Returns the channel if the caller should read the response.
3362 * Sets "part_read" to the the read fd.
3363 * Otherwise returns NULL.
3364 */
3365 channel_T *
3366send_common(
3367 typval_T *argvars,
3368 char_u *text,
3369 int id,
3370 int eval,
3371 jobopt_T *opt,
3372 char *fun,
3373 int *part_read)
3374{
3375 channel_T *channel;
3376 int part_send;
3377
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003378 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003379 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003380 if (channel == NULL)
3381 return NULL;
3382 part_send = channel_part_send(channel);
3383 *part_read = channel_part_read(channel);
3384
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003385 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3386 return NULL;
3387
3388 /* Set the callback. An empty callback means no callback and not reading
3389 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3390 * allowed. */
3391 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3392 {
3393 if (eval)
3394 {
3395 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3396 return NULL;
3397 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003398 channel_set_req_callback(channel, part_send,
3399 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003400 }
3401
3402 if (channel_send(channel, part_send, text, fun) == OK
3403 && opt->jo_callback == NULL)
3404 return channel;
3405 return NULL;
3406}
3407
3408/*
3409 * common for "ch_evalexpr()" and "ch_sendexpr()"
3410 */
3411 void
3412ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3413{
3414 char_u *text;
3415 typval_T *listtv;
3416 channel_T *channel;
3417 int id;
3418 ch_mode_T ch_mode;
3419 int part_send;
3420 int part_read;
3421 jobopt_T opt;
3422 int timeout;
3423
3424 /* return an empty string by default */
3425 rettv->v_type = VAR_STRING;
3426 rettv->vval.v_string = NULL;
3427
Bram Moolenaar437905c2016-04-26 19:01:05 +02003428 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003429 if (channel == NULL)
3430 return;
3431 part_send = channel_part_send(channel);
3432
3433 ch_mode = channel_get_mode(channel, part_send);
3434 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3435 {
3436 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3437 return;
3438 }
3439
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003440 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003441 text = json_encode_nr_expr(id, &argvars[1],
3442 ch_mode == MODE_JS ? JSON_JS : 0);
3443 if (text == NULL)
3444 return;
3445
3446 channel = send_common(argvars, text, id, eval, &opt,
3447 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3448 vim_free(text);
3449 if (channel != NULL && eval)
3450 {
3451 if (opt.jo_set & JO_TIMEOUT)
3452 timeout = opt.jo_timeout;
3453 else
3454 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003455 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3456 == OK)
3457 {
3458 list_T *list = listtv->vval.v_list;
3459
3460 /* Move the item from the list and then change the type to
3461 * avoid the value being freed. */
3462 *rettv = list->lv_last->li_tv;
3463 list->lv_last->li_tv.v_type = VAR_NUMBER;
3464 free_tv(listtv);
3465 }
3466 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003467 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003468}
3469
3470/*
3471 * common for "ch_evalraw()" and "ch_sendraw()"
3472 */
3473 void
3474ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3475{
3476 char_u buf[NUMBUFLEN];
3477 char_u *text;
3478 channel_T *channel;
3479 int part_read;
3480 jobopt_T opt;
3481 int timeout;
3482
3483 /* return an empty string by default */
3484 rettv->v_type = VAR_STRING;
3485 rettv->vval.v_string = NULL;
3486
3487 text = get_tv_string_buf(&argvars[1], buf);
3488 channel = send_common(argvars, text, 0, eval, &opt,
3489 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3490 if (channel != NULL && eval)
3491 {
3492 if (opt.jo_set & JO_TIMEOUT)
3493 timeout = opt.jo_timeout;
3494 else
3495 timeout = channel_get_timeout(channel, part_read);
3496 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3497 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003498 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003499}
3500
Bram Moolenaard04a0202016-01-26 23:30:18 +01003501# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003502/*
3503 * Add open channels to the poll struct.
3504 * Return the adjusted struct index.
3505 * The type of "fds" is hidden to avoid problems with the function proto.
3506 */
3507 int
3508channel_poll_setup(int nfd_in, void *fds_in)
3509{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003510 int nfd = nfd_in;
3511 channel_T *channel;
3512 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003513 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003514
Bram Moolenaar77073442016-02-13 23:23:53 +01003515 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003516 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003517 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003518 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003519 chanpart_T *ch_part = &channel->ch_part[part];
3520
3521 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003522 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003523 ch_part->ch_poll_idx = nfd;
3524 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003525 fds[nfd].events = POLLIN;
3526 nfd++;
3527 }
3528 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003529 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003530 }
3531 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003532
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003533 nfd = channel_fill_poll_write(nfd, fds);
3534
Bram Moolenaare0874f82016-01-24 20:36:41 +01003535 return nfd;
3536}
3537
3538/*
3539 * The type of "fds" is hidden to avoid problems with the function proto.
3540 */
3541 int
3542channel_poll_check(int ret_in, void *fds_in)
3543{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003544 int ret = ret_in;
3545 channel_T *channel;
3546 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003547 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003548 int idx;
3549 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003550
Bram Moolenaar77073442016-02-13 23:23:53 +01003551 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003552 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003553 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003554 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003555 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003556
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003557 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003558 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003560 --ret;
3561 }
3562 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003563
3564 in_part = &channel->ch_part[PART_IN];
3565 idx = in_part->ch_poll_idx;
3566 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3567 {
3568 if (in_part->ch_buf_append)
3569 {
3570 if (in_part->ch_buffer != NULL)
3571 channel_write_new_lines(in_part->ch_buffer);
3572 }
3573 else
3574 channel_write_in(channel);
3575 --ret;
3576 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003577 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003578
3579 return ret;
3580}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003581# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003582
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003583# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003584/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003585 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003586 */
3587 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003588channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003589{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003590 int maxfd = maxfd_in;
3591 channel_T *channel;
3592 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003593 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003594 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003595
Bram Moolenaar77073442016-02-13 23:23:53 +01003596 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003597 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003599 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003600 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003601
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003602 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003603 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003604 FD_SET((int)fd, rfds);
3605 if (maxfd < (int)fd)
3606 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003607 }
3608 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003609 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003610
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003611 maxfd = channel_fill_wfds(maxfd, wfds);
3612
Bram Moolenaare0874f82016-01-24 20:36:41 +01003613 return maxfd;
3614}
3615
3616/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003617 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003618 */
3619 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003620channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003621{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003622 int ret = ret_in;
3623 channel_T *channel;
3624 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003625 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003626 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003627 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003628
Bram Moolenaar77073442016-02-13 23:23:53 +01003629 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003630 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003631 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003632 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003633 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003634
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003635 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003636 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003637 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003638 --ret;
3639 }
3640 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003641
3642 in_part = &channel->ch_part[PART_IN];
3643 if (ret > 0 && in_part->ch_fd != INVALID_FD
3644 && FD_ISSET(in_part->ch_fd, wfds))
3645 {
3646 if (in_part->ch_buf_append)
3647 {
3648 if (in_part->ch_buffer != NULL)
3649 channel_write_new_lines(in_part->ch_buffer);
3650 }
3651 else
3652 channel_write_in(channel);
3653 --ret;
3654 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003655 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003656
3657 return ret;
3658}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003659# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003660
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003661/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003662 * Execute queued up commands.
3663 * Invoked from the main loop when it's safe to execute received commands.
3664 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003665 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003666 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003667channel_parse_messages(void)
3668{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003669 channel_T *channel = first_channel;
3670 int ret = FALSE;
3671 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003672 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003673
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003674 ++safe_to_invoke_callback;
3675
Bram Moolenaard0b65022016-03-06 21:50:33 +01003676 /* Only do this message when another message was given, otherwise we get
3677 * lots of them. */
3678 if (did_log_msg)
3679 {
3680 ch_log(NULL, "looking for messages on channels");
3681 did_log_msg = FALSE;
3682 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003683 while (channel != NULL)
3684 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003685 if (channel->ch_to_be_closed)
3686 {
3687 channel->ch_to_be_closed = FALSE;
3688 channel_close_now(channel);
3689 /* channel may have been freed, start over */
3690 channel = first_channel;
3691 continue;
3692 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003693 if (channel->ch_to_be_freed)
3694 {
3695 channel_free(channel);
3696 /* channel has been freed, start over */
3697 channel = first_channel;
3698 continue;
3699 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003700 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003701 {
3702 /* channel is no longer useful, free it */
3703 channel_free(channel);
3704 channel = first_channel;
3705 part = PART_SOCK;
3706 continue;
3707 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003708 if (channel->ch_part[part].ch_fd != INVALID_FD
3709 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003710 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003711 /* Increase the refcount, in case the handler causes the channel
3712 * to be unreferenced or closed. */
3713 ++channel->ch_refcount;
3714 r = may_invoke_callback(channel, part);
3715 if (r == OK)
3716 ret = TRUE;
3717 if (channel_unref(channel) || r == OK)
3718 {
3719 /* channel was freed or something was done, start over */
3720 channel = first_channel;
3721 part = PART_SOCK;
3722 continue;
3723 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003724 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003725 if (part < PART_ERR)
3726 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003727 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003728 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003729 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003730 part = PART_SOCK;
3731 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003732 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003733
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003734 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003735 {
3736 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003737 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003738 }
3739
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003740 --safe_to_invoke_callback;
3741
Bram Moolenaard7ece102016-02-02 23:23:02 +01003742 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003743}
3744
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003745/*
3746 * Mark references to lists used in channels.
3747 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003748 int
3749set_ref_in_channel(int copyID)
3750{
Bram Moolenaar77073442016-02-13 23:23:53 +01003751 int abort = FALSE;
3752 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003753 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003754
Bram Moolenaar77073442016-02-13 23:23:53 +01003755 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003756 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003757 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003758 tv.v_type = VAR_CHANNEL;
3759 tv.vval.v_channel = channel;
3760 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003761 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003762 return abort;
3763}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003764
3765/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003766 * Return the "part" to write to for "channel".
3767 */
3768 int
3769channel_part_send(channel_T *channel)
3770{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003771 if (channel->CH_SOCK_FD == INVALID_FD)
3772 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003773 return PART_SOCK;
3774}
3775
3776/*
3777 * Return the default "part" to read from for "channel".
3778 */
3779 int
3780channel_part_read(channel_T *channel)
3781{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003782 if (channel->CH_SOCK_FD == INVALID_FD)
3783 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003784 return PART_SOCK;
3785}
3786
3787/*
3788 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003789 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003790 */
3791 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003792channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003793{
Bram Moolenaar77073442016-02-13 23:23:53 +01003794 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003795 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003796 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003797}
3798
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003799/*
3800 * Return the timeout of "channel"/"part"
3801 */
3802 int
3803channel_get_timeout(channel_T *channel, int part)
3804{
3805 return channel->ch_part[part].ch_timeout;
3806}
3807
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003808 static int
3809handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3810{
3811 char_u *val = get_tv_string(item);
3812
3813 opt->jo_set |= jo;
3814 if (STRCMP(val, "nl") == 0)
3815 *modep = MODE_NL;
3816 else if (STRCMP(val, "raw") == 0)
3817 *modep = MODE_RAW;
3818 else if (STRCMP(val, "js") == 0)
3819 *modep = MODE_JS;
3820 else if (STRCMP(val, "json") == 0)
3821 *modep = MODE_JSON;
3822 else
3823 {
3824 EMSG2(_(e_invarg2), val);
3825 return FAIL;
3826 }
3827 return OK;
3828}
3829
3830 static int
3831handle_io(typval_T *item, int part, jobopt_T *opt)
3832{
3833 char_u *val = get_tv_string(item);
3834
3835 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3836 if (STRCMP(val, "null") == 0)
3837 opt->jo_io[part] = JIO_NULL;
3838 else if (STRCMP(val, "pipe") == 0)
3839 opt->jo_io[part] = JIO_PIPE;
3840 else if (STRCMP(val, "file") == 0)
3841 opt->jo_io[part] = JIO_FILE;
3842 else if (STRCMP(val, "buffer") == 0)
3843 opt->jo_io[part] = JIO_BUFFER;
3844 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3845 opt->jo_io[part] = JIO_OUT;
3846 else
3847 {
3848 EMSG2(_(e_invarg2), val);
3849 return FAIL;
3850 }
3851 return OK;
3852}
3853
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003854/*
3855 * Clear a jobopt_T before using it.
3856 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003857 void
3858clear_job_options(jobopt_T *opt)
3859{
3860 vim_memset(opt, 0, sizeof(jobopt_T));
3861}
3862
3863/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003864 * Free any members of a jobopt_T.
3865 */
3866 void
3867free_job_options(jobopt_T *opt)
3868{
3869 if (opt->jo_partial != NULL)
3870 partial_unref(opt->jo_partial);
3871 if (opt->jo_out_partial != NULL)
3872 partial_unref(opt->jo_out_partial);
3873 if (opt->jo_err_partial != NULL)
3874 partial_unref(opt->jo_err_partial);
3875 if (opt->jo_close_partial != NULL)
3876 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003877 if (opt->jo_exit_partial != NULL)
3878 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003879}
3880
3881/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003882 * Get the PART_ number from the first character of an option name.
3883 */
3884 static int
3885part_from_char(int c)
3886{
3887 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3888}
3889
3890/*
3891 * Get the option entries from the dict in "tv", parse them and put the result
3892 * in "opt".
3893 * Only accept options in "supported".
3894 * If an option value is invalid return FAIL.
3895 */
3896 int
3897get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3898{
3899 typval_T *item;
3900 char_u *val;
3901 dict_T *dict;
3902 int todo;
3903 hashitem_T *hi;
3904 int part;
3905
3906 opt->jo_set = 0;
3907 if (tv->v_type == VAR_UNKNOWN)
3908 return OK;
3909 if (tv->v_type != VAR_DICT)
3910 {
3911 EMSG(_(e_invarg));
3912 return FAIL;
3913 }
3914 dict = tv->vval.v_dict;
3915 if (dict == NULL)
3916 return OK;
3917
3918 todo = (int)dict->dv_hashtab.ht_used;
3919 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3920 if (!HASHITEM_EMPTY(hi))
3921 {
3922 item = &dict_lookup(hi)->di_tv;
3923
3924 if (STRCMP(hi->hi_key, "mode") == 0)
3925 {
3926 if (!(supported & JO_MODE))
3927 break;
3928 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3929 return FAIL;
3930 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003931 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003932 {
3933 if (!(supported & JO_IN_MODE))
3934 break;
3935 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3936 == FAIL)
3937 return FAIL;
3938 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003939 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003940 {
3941 if (!(supported & JO_OUT_MODE))
3942 break;
3943 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3944 == FAIL)
3945 return FAIL;
3946 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003947 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003948 {
3949 if (!(supported & JO_ERR_MODE))
3950 break;
3951 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3952 == FAIL)
3953 return FAIL;
3954 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003955 else if (STRCMP(hi->hi_key, "in_io") == 0
3956 || STRCMP(hi->hi_key, "out_io") == 0
3957 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003958 {
3959 if (!(supported & JO_OUT_IO))
3960 break;
3961 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3962 return FAIL;
3963 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003964 else if (STRCMP(hi->hi_key, "in_name") == 0
3965 || STRCMP(hi->hi_key, "out_name") == 0
3966 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003967 {
3968 part = part_from_char(*hi->hi_key);
3969
3970 if (!(supported & JO_OUT_IO))
3971 break;
3972 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3973 opt->jo_io_name[part] =
3974 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3975 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003976 else if (STRCMP(hi->hi_key, "in_buf") == 0
3977 || STRCMP(hi->hi_key, "out_buf") == 0
3978 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003979 {
3980 part = part_from_char(*hi->hi_key);
3981
3982 if (!(supported & JO_OUT_IO))
3983 break;
3984 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3985 opt->jo_io_buf[part] = get_tv_number(item);
3986 if (opt->jo_io_buf[part] <= 0)
3987 {
3988 EMSG2(_(e_invarg2), get_tv_string(item));
3989 return FAIL;
3990 }
3991 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3992 {
3993 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3994 return FAIL;
3995 }
3996 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02003997 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
3998 || STRCMP(hi->hi_key, "err_modifiable") == 0)
3999 {
4000 part = part_from_char(*hi->hi_key);
4001
4002 if (!(supported & JO_OUT_IO))
4003 break;
4004 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4005 opt->jo_modifiable[part] = get_tv_number(item);
4006 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004007 else if (STRCMP(hi->hi_key, "in_top") == 0
4008 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004009 {
4010 linenr_T *lp;
4011
4012 if (!(supported & JO_OUT_IO))
4013 break;
4014 if (hi->hi_key[3] == 't')
4015 {
4016 lp = &opt->jo_in_top;
4017 opt->jo_set |= JO_IN_TOP;
4018 }
4019 else
4020 {
4021 lp = &opt->jo_in_bot;
4022 opt->jo_set |= JO_IN_BOT;
4023 }
4024 *lp = get_tv_number(item);
4025 if (*lp < 0)
4026 {
4027 EMSG2(_(e_invarg2), get_tv_string(item));
4028 return FAIL;
4029 }
4030 }
4031 else if (STRCMP(hi->hi_key, "channel") == 0)
4032 {
4033 if (!(supported & JO_OUT_IO))
4034 break;
4035 opt->jo_set |= JO_CHANNEL;
4036 if (item->v_type != VAR_CHANNEL)
4037 {
4038 EMSG2(_(e_invarg2), "channel");
4039 return FAIL;
4040 }
4041 opt->jo_channel = item->vval.v_channel;
4042 }
4043 else if (STRCMP(hi->hi_key, "callback") == 0)
4044 {
4045 if (!(supported & JO_CALLBACK))
4046 break;
4047 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004048 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004049 if (opt->jo_callback == NULL)
4050 {
4051 EMSG2(_(e_invarg2), "callback");
4052 return FAIL;
4053 }
4054 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004055 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004056 {
4057 if (!(supported & JO_OUT_CALLBACK))
4058 break;
4059 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004060 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004061 if (opt->jo_out_cb == NULL)
4062 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004063 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004064 return FAIL;
4065 }
4066 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004067 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004068 {
4069 if (!(supported & JO_ERR_CALLBACK))
4070 break;
4071 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004072 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004073 if (opt->jo_err_cb == NULL)
4074 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004075 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 return FAIL;
4077 }
4078 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004079 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004080 {
4081 if (!(supported & JO_CLOSE_CALLBACK))
4082 break;
4083 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004084 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 if (opt->jo_close_cb == NULL)
4086 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004087 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 return FAIL;
4089 }
4090 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004091 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4092 {
4093 if (!(supported & JO_EXIT_CB))
4094 break;
4095 opt->jo_set |= JO_EXIT_CB;
4096 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4097 if (opt->jo_exit_cb == NULL)
4098 {
4099 EMSG2(_(e_invarg2), "exit_cb");
4100 return FAIL;
4101 }
4102 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004103 else if (STRCMP(hi->hi_key, "waittime") == 0)
4104 {
4105 if (!(supported & JO_WAITTIME))
4106 break;
4107 opt->jo_set |= JO_WAITTIME;
4108 opt->jo_waittime = get_tv_number(item);
4109 }
4110 else if (STRCMP(hi->hi_key, "timeout") == 0)
4111 {
4112 if (!(supported & JO_TIMEOUT))
4113 break;
4114 opt->jo_set |= JO_TIMEOUT;
4115 opt->jo_timeout = get_tv_number(item);
4116 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004117 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 {
4119 if (!(supported & JO_OUT_TIMEOUT))
4120 break;
4121 opt->jo_set |= JO_OUT_TIMEOUT;
4122 opt->jo_out_timeout = get_tv_number(item);
4123 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004124 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004125 {
4126 if (!(supported & JO_ERR_TIMEOUT))
4127 break;
4128 opt->jo_set |= JO_ERR_TIMEOUT;
4129 opt->jo_err_timeout = get_tv_number(item);
4130 }
4131 else if (STRCMP(hi->hi_key, "part") == 0)
4132 {
4133 if (!(supported & JO_PART))
4134 break;
4135 opt->jo_set |= JO_PART;
4136 val = get_tv_string(item);
4137 if (STRCMP(val, "err") == 0)
4138 opt->jo_part = PART_ERR;
4139 else
4140 {
4141 EMSG2(_(e_invarg2), val);
4142 return FAIL;
4143 }
4144 }
4145 else if (STRCMP(hi->hi_key, "id") == 0)
4146 {
4147 if (!(supported & JO_ID))
4148 break;
4149 opt->jo_set |= JO_ID;
4150 opt->jo_id = get_tv_number(item);
4151 }
4152 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4153 {
4154 if (!(supported & JO_STOPONEXIT))
4155 break;
4156 opt->jo_set |= JO_STOPONEXIT;
4157 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4158 opt->jo_soe_buf);
4159 if (opt->jo_stoponexit == NULL)
4160 {
4161 EMSG2(_(e_invarg2), "stoponexit");
4162 return FAIL;
4163 }
4164 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004165 else if (STRCMP(hi->hi_key, "block_write") == 0)
4166 {
4167 if (!(supported & JO_BLOCK_WRITE))
4168 break;
4169 opt->jo_set |= JO_BLOCK_WRITE;
4170 opt->jo_block_write = get_tv_number(item);
4171 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004172 else
4173 break;
4174 --todo;
4175 }
4176 if (todo > 0)
4177 {
4178 EMSG2(_(e_invarg2), hi->hi_key);
4179 return FAIL;
4180 }
4181
4182 return OK;
4183}
4184
4185/*
4186 * Get the channel from the argument.
4187 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004188 * When "check_open" is TRUE check that the channel can be used.
4189 * When "reading" is TRUE "check_open" considers typeahead useful.
4190 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004191 */
4192 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004193get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004194{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004195 channel_T *channel = NULL;
4196 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004197
4198 if (tv->v_type == VAR_JOB)
4199 {
4200 if (tv->vval.v_job != NULL)
4201 channel = tv->vval.v_job->jv_channel;
4202 }
4203 else if (tv->v_type == VAR_CHANNEL)
4204 {
4205 channel = tv->vval.v_channel;
4206 }
4207 else
4208 {
4209 EMSG2(_(e_invarg2), get_tv_string(tv));
4210 return NULL;
4211 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004212 if (channel != NULL && reading)
4213 has_readahead = channel_has_readahead(channel,
4214 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004215
Bram Moolenaar437905c2016-04-26 19:01:05 +02004216 if (check_open && (channel == NULL || (!channel_is_open(channel)
4217 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004218 {
4219 EMSG(_("E906: not an open channel"));
4220 return NULL;
4221 }
4222 return channel;
4223}
4224
4225static job_T *first_job = NULL;
4226
4227 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004228job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004229{
4230 ch_log(job->jv_channel, "Freeing job");
4231 if (job->jv_channel != NULL)
4232 {
4233 /* The link from the channel to the job doesn't count as a reference,
4234 * thus don't decrement the refcount of the job. The reference from
4235 * the job to the channel does count the refrence, decrement it and
4236 * NULL the reference. We don't set ch_job_killed, unreferencing the
4237 * job doesn't mean it stops running. */
4238 job->jv_channel->ch_job = NULL;
4239 channel_unref(job->jv_channel);
4240 }
4241 mch_clear_job(job);
4242
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004243 vim_free(job->jv_stoponexit);
4244 vim_free(job->jv_exit_cb);
4245 partial_unref(job->jv_exit_partial);
4246}
4247
4248 static void
4249job_free_job(job_T *job)
4250{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004251 if (job->jv_next != NULL)
4252 job->jv_next->jv_prev = job->jv_prev;
4253 if (job->jv_prev == NULL)
4254 first_job = job->jv_next;
4255 else
4256 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004257 vim_free(job);
4258}
4259
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004260 static void
4261job_free(job_T *job)
4262{
4263 if (!in_free_unref_items)
4264 {
4265 job_free_contents(job);
4266 job_free_job(job);
4267 }
4268}
4269
Bram Moolenaar655da312016-05-28 22:22:34 +02004270#if defined(EXITFREE) || defined(PROTO)
4271 void
4272job_free_all(void)
4273{
4274 while (first_job != NULL)
4275 job_free(first_job);
4276}
4277#endif
4278
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004279/*
4280 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004281 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4282 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004283 */
4284 static int
4285job_still_useful(job_T *job)
4286{
4287 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004288 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4289 || (job->jv_channel != NULL
4290 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004291}
4292
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004293/*
4294 * Mark references in jobs that are still useful.
4295 */
4296 int
4297set_ref_in_job(int copyID)
4298{
4299 int abort = FALSE;
4300 job_T *job;
4301 typval_T tv;
4302
4303 for (job = first_job; job != NULL; job = job->jv_next)
4304 if (job_still_useful(job))
4305 {
4306 tv.v_type = VAR_JOB;
4307 tv.vval.v_job = job;
4308 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4309 }
4310 return abort;
4311}
4312
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004313 void
4314job_unref(job_T *job)
4315{
4316 if (job != NULL && --job->jv_refcount <= 0)
4317 {
4318 /* Do not free the job when it has not ended yet and there is a
4319 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004320 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004321 {
4322 job_free(job);
4323 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004324 else if (job->jv_channel != NULL
4325 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004326 {
4327 /* Do remove the link to the channel, otherwise it hangs
4328 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004329 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004330 job->jv_channel->ch_job = NULL;
4331 channel_unref(job->jv_channel);
4332 job->jv_channel = NULL;
4333 }
4334 }
4335}
4336
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004337 int
4338free_unused_jobs_contents(int copyID, int mask)
4339{
4340 int did_free = FALSE;
4341 job_T *job;
4342
4343 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004344 if ((job->jv_copyID & mask) != (copyID & mask)
4345 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004346 {
4347 /* Free the channel and ordinary items it contains, but don't
4348 * recurse into Lists, Dictionaries etc. */
4349 job_free_contents(job);
4350 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004351 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004352 return did_free;
4353}
4354
4355 void
4356free_unused_jobs(int copyID, int mask)
4357{
4358 job_T *job;
4359 job_T *job_next;
4360
4361 for (job = first_job; job != NULL; job = job_next)
4362 {
4363 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004364 if ((job->jv_copyID & mask) != (copyID & mask)
4365 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004366 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004367 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004368 job_free_job(job);
4369 }
4370 }
4371}
4372
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004373/*
4374 * Allocate a job. Sets the refcount to one and sets options default.
4375 */
4376 static job_T *
4377job_alloc(void)
4378{
4379 job_T *job;
4380
4381 job = (job_T *)alloc_clear(sizeof(job_T));
4382 if (job != NULL)
4383 {
4384 job->jv_refcount = 1;
4385 job->jv_stoponexit = vim_strsave((char_u *)"term");
4386
4387 if (first_job != NULL)
4388 {
4389 first_job->jv_prev = job;
4390 job->jv_next = first_job;
4391 }
4392 first_job = job;
4393 }
4394 return job;
4395}
4396
4397 void
4398job_set_options(job_T *job, jobopt_T *opt)
4399{
4400 if (opt->jo_set & JO_STOPONEXIT)
4401 {
4402 vim_free(job->jv_stoponexit);
4403 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4404 job->jv_stoponexit = NULL;
4405 else
4406 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4407 }
4408 if (opt->jo_set & JO_EXIT_CB)
4409 {
4410 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004411 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004412 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004413 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004414 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004415 job->jv_exit_partial = NULL;
4416 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004417 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004418 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004419 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004420 job->jv_exit_partial = opt->jo_exit_partial;
4421 if (job->jv_exit_partial != NULL)
4422 ++job->jv_exit_partial->pt_refcount;
4423 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004424 }
4425}
4426
4427/*
4428 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4429 */
4430 void
4431job_stop_on_exit()
4432{
4433 job_T *job;
4434
4435 for (job = first_job; job != NULL; job = job->jv_next)
4436 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4437 mch_stop_job(job, job->jv_stoponexit);
4438}
4439
4440/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004441 * Return TRUE when there is any job that might exit, which means
4442 * job_check_ended() should be called once in a while.
4443 */
4444 int
4445has_pending_job()
4446{
4447 job_T *job;
4448
4449 for (job = first_job; job != NULL; job = job->jv_next)
4450 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4451 return TRUE;
4452 return FALSE;
4453}
4454
4455/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004456 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004457 */
4458 void
4459job_check_ended(void)
4460{
4461 static time_t last_check = 0;
4462 time_t now;
4463 job_T *job;
4464 job_T *next;
4465
4466 /* Only do this once in 10 seconds. */
4467 now = time(NULL);
4468 if (last_check + 10 < now)
4469 {
4470 last_check = now;
4471 for (job = first_job; job != NULL; job = next)
4472 {
4473 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004474 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004475 job_status(job); /* may free "job" */
4476 }
4477 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004478 if (channel_need_redraw)
4479 {
4480 channel_need_redraw = FALSE;
4481 redraw_after_callback();
4482 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004483}
4484
4485/*
4486 * "job_start()" function
4487 */
4488 job_T *
4489job_start(typval_T *argvars)
4490{
4491 job_T *job;
4492 char_u *cmd = NULL;
4493#if defined(UNIX)
4494# define USE_ARGV
4495 char **argv = NULL;
4496 int argc = 0;
4497#else
4498 garray_T ga;
4499#endif
4500 jobopt_T opt;
4501 int part;
4502
4503 job = job_alloc();
4504 if (job == NULL)
4505 return NULL;
4506
4507 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004508#ifndef USE_ARGV
4509 ga_init2(&ga, (int)sizeof(char*), 20);
4510#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004511
4512 /* Default mode is NL. */
4513 clear_job_options(&opt);
4514 opt.jo_mode = MODE_NL;
4515 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004516 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4517 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004518 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004519
4520 /* Check that when io is "file" that there is a file name. */
4521 for (part = PART_OUT; part <= PART_IN; ++part)
4522 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4523 && opt.jo_io[part] == JIO_FILE
4524 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4525 || *opt.jo_io_name[part] == NUL))
4526 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004527 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004528 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004529 }
4530
4531 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4532 {
4533 buf_T *buf = NULL;
4534
4535 /* check that we can find the buffer before starting the job */
4536 if (opt.jo_set & JO_IN_BUF)
4537 {
4538 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4539 if (buf == NULL)
4540 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4541 }
4542 else if (!(opt.jo_set & JO_IN_NAME))
4543 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004544 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004545 }
4546 else
4547 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4548 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004549 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004550 if (buf->b_ml.ml_mfp == NULL)
4551 {
4552 char_u numbuf[NUMBUFLEN];
4553 char_u *s;
4554
4555 if (opt.jo_set & JO_IN_BUF)
4556 {
4557 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4558 s = numbuf;
4559 }
4560 else
4561 s = opt.jo_io_name[PART_IN];
4562 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004563 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004564 }
4565 job->jv_in_buf = buf;
4566 }
4567
4568 job_set_options(job, &opt);
4569
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004570 if (argvars[0].v_type == VAR_STRING)
4571 {
4572 /* Command is a string. */
4573 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004574 if (cmd == NULL || *cmd == NUL)
4575 {
4576 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004577 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004578 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004579#ifdef USE_ARGV
4580 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004581 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004582 argv[argc] = NULL;
4583#endif
4584 }
4585 else if (argvars[0].v_type != VAR_LIST
4586 || argvars[0].vval.v_list == NULL
4587 || argvars[0].vval.v_list->lv_len < 1)
4588 {
4589 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004590 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004591 }
4592 else
4593 {
4594 list_T *l = argvars[0].vval.v_list;
4595 listitem_T *li;
4596 char_u *s;
4597
4598#ifdef USE_ARGV
4599 /* Pass argv[] to mch_call_shell(). */
4600 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4601 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004602 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004603#endif
4604 for (li = l->lv_first; li != NULL; li = li->li_next)
4605 {
4606 s = get_tv_string_chk(&li->li_tv);
4607 if (s == NULL)
4608 goto theend;
4609#ifdef USE_ARGV
4610 argv[argc++] = (char *)s;
4611#else
4612 /* Only escape when needed, double quotes are not always allowed. */
4613 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4614 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004615# ifdef WIN32
4616 int old_ssl = p_ssl;
4617
4618 /* This is using CreateProcess, not cmd.exe. Always use
4619 * double quote and backslashes. */
4620 p_ssl = 0;
4621# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004622 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004623# ifdef WIN32
4624 p_ssl = old_ssl;
4625# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004626 if (s == NULL)
4627 goto theend;
4628 ga_concat(&ga, s);
4629 vim_free(s);
4630 }
4631 else
4632 ga_concat(&ga, s);
4633 if (li->li_next != NULL)
4634 ga_append(&ga, ' ');
4635#endif
4636 }
4637#ifdef USE_ARGV
4638 argv[argc] = NULL;
4639#else
4640 cmd = ga.ga_data;
4641#endif
4642 }
4643
4644#ifdef USE_ARGV
4645 if (ch_log_active())
4646 {
4647 garray_T ga;
4648 int i;
4649
4650 ga_init2(&ga, (int)sizeof(char), 200);
4651 for (i = 0; i < argc; ++i)
4652 {
4653 if (i > 0)
4654 ga_concat(&ga, (char_u *)" ");
4655 ga_concat(&ga, (char_u *)argv[i]);
4656 }
4657 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4658 ga_clear(&ga);
4659 }
4660 mch_start_job(argv, job, &opt);
4661#else
4662 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4663 mch_start_job((char *)cmd, job, &opt);
4664#endif
4665
4666 /* If the channel is reading from a buffer, write lines now. */
4667 if (job->jv_channel != NULL)
4668 channel_write_in(job->jv_channel);
4669
4670theend:
4671#ifdef USE_ARGV
4672 vim_free(argv);
4673#else
4674 vim_free(ga.ga_data);
4675#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004676 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004677 return job;
4678}
4679
4680/*
4681 * Get the status of "job" and invoke the exit callback when needed.
4682 * The returned string is not allocated.
4683 */
4684 char *
4685job_status(job_T *job)
4686{
4687 char *result;
4688
4689 if (job->jv_status == JOB_ENDED)
4690 /* No need to check, dead is dead. */
4691 result = "dead";
4692 else if (job->jv_status == JOB_FAILED)
4693 result = "fail";
4694 else
4695 {
4696 result = mch_job_status(job);
4697 if (job->jv_status == JOB_ENDED)
4698 ch_log(job->jv_channel, "Job ended");
4699 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4700 {
4701 typval_T argv[3];
4702 typval_T rettv;
4703 int dummy;
4704
4705 /* invoke the exit callback; make sure the refcount is > 0 */
4706 ++job->jv_refcount;
4707 argv[0].v_type = VAR_JOB;
4708 argv[0].vval.v_job = job;
4709 argv[1].v_type = VAR_NUMBER;
4710 argv[1].vval.v_number = job->jv_exitval;
4711 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004712 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4713 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004714 clear_tv(&rettv);
4715 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004716 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004717 }
4718 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4719 {
4720 /* The job was already unreferenced, now that it ended it can be
4721 * freed. Careful: caller must not use "job" after this! */
4722 job_free(job);
4723 }
4724 }
4725 return result;
4726}
4727
Bram Moolenaar8950a562016-03-12 15:22:55 +01004728/*
4729 * Implementation of job_info().
4730 */
4731 void
4732job_info(job_T *job, dict_T *dict)
4733{
4734 dictitem_T *item;
4735 varnumber_T nr;
4736
4737 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4738
4739 item = dictitem_alloc((char_u *)"channel");
4740 if (item == NULL)
4741 return;
4742 item->di_tv.v_lock = 0;
4743 item->di_tv.v_type = VAR_CHANNEL;
4744 item->di_tv.vval.v_channel = job->jv_channel;
4745 if (job->jv_channel != NULL)
4746 ++job->jv_channel->ch_refcount;
4747 if (dict_add(dict, item) == FAIL)
4748 dictitem_free(item);
4749
4750#ifdef UNIX
4751 nr = job->jv_pid;
4752#else
4753 nr = job->jv_proc_info.dwProcessId;
4754#endif
4755 dict_add_nr_str(dict, "process", nr, NULL);
4756
4757 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004758 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004759 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4760}
4761
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004762 int
4763job_stop(job_T *job, typval_T *argvars)
4764{
4765 char_u *arg;
4766
4767 if (argvars[1].v_type == VAR_UNKNOWN)
4768 arg = (char_u *)"";
4769 else
4770 {
4771 arg = get_tv_string_chk(&argvars[1]);
4772 if (arg == NULL)
4773 {
4774 EMSG(_(e_invarg));
4775 return 0;
4776 }
4777 }
4778 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4779 if (mch_stop_job(job, arg) == FAIL)
4780 return 0;
4781
4782 /* Assume that "hup" does not kill the job. */
4783 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4784 job->jv_channel->ch_job_killed = TRUE;
4785
4786 /* We don't try freeing the job, obviously the caller still has a
4787 * reference to it. */
4788 return 1;
4789}
4790
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004791#endif /* FEAT_JOB_CHANNEL */