blob: e60d49e4cf1afcbfcb5818acafe12f506ad119cf [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 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001660 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001661
1662 /* dispose of the collapsed nodes and their buffers */
1663 for (n = node->rq_next; n != last_node; )
1664 {
1665 n = n->rq_next;
1666 vim_free(n->rq_prev);
1667 }
1668 node->rq_next = last_node->rq_next;
1669 if (last_node->rq_next == NULL)
1670 head->rq_prev = node;
1671 else
1672 last_node->rq_next->rq_prev = node;
1673 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001674 return OK;
1675}
1676
1677/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001678 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001679 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001680 * Returns OK or FAIL.
1681 */
1682 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001683channel_save(channel_T *channel, int part, char_u *buf, int len,
1684 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001685{
1686 readq_T *node;
1687 readq_T *head = &channel->ch_part[part].ch_head;
1688 char_u *p;
1689 int i;
1690
1691 node = (readq_T *)alloc(sizeof(readq_T));
1692 if (node == NULL)
1693 return FAIL; /* out of memory */
1694 node->rq_buffer = alloc(len + 1);
1695 if (node->rq_buffer == NULL)
1696 {
1697 vim_free(node);
1698 return FAIL; /* out of memory */
1699 }
1700
1701 if (channel->ch_part[part].ch_mode == MODE_NL)
1702 {
1703 /* Drop any CR before a NL. */
1704 p = node->rq_buffer;
1705 for (i = 0; i < len; ++i)
1706 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1707 *p++ = buf[i];
1708 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001710 }
1711 else
1712 {
1713 mch_memmove(node->rq_buffer, buf, len);
1714 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001715 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001716 }
1717
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001718 if (prepend)
1719 {
1720 /* preend node to the head of the queue */
1721 node->rq_next = head->rq_next;
1722 node->rq_prev = NULL;
1723 if (head->rq_next == NULL)
1724 head->rq_prev = node;
1725 else
1726 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001728 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001729 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001730 {
1731 /* append node to the tail of the queue */
1732 node->rq_next = NULL;
1733 node->rq_prev = head->rq_prev;
1734 if (head->rq_prev == NULL)
1735 head->rq_next = node;
1736 else
1737 head->rq_prev->rq_next = node;
1738 head->rq_prev = node;
1739 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001740
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001741 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001742 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001743 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001744 fprintf(log_fd, "'");
1745 if (fwrite(buf, len, 1, log_fd) != 1)
1746 return FAIL;
1747 fprintf(log_fd, "'\n");
1748 }
1749 return OK;
1750}
1751
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001752 static int
1753channel_fill(js_read_T *reader)
1754{
1755 channel_T *channel = (channel_T *)reader->js_cookie;
1756 int part = reader->js_cookie_arg;
1757 char_u *next = channel_get(channel, part);
1758 int unused;
1759 int len;
1760 char_u *p;
1761
1762 if (next == NULL)
1763 return FALSE;
1764
1765 unused = reader->js_end - reader->js_buf - reader->js_used;
1766 if (unused > 0)
1767 {
1768 /* Prepend unused text. */
1769 len = (int)STRLEN(next);
1770 p = alloc(unused + len + 1);
1771 if (p == NULL)
1772 {
1773 vim_free(next);
1774 return FALSE;
1775 }
1776 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1777 mch_memmove(p + unused, next, len + 1);
1778 vim_free(next);
1779 next = p;
1780 }
1781
1782 vim_free(reader->js_buf);
1783 reader->js_buf = next;
1784 reader->js_used = 0;
1785 return TRUE;
1786}
1787
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001788/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001789 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001790 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001791 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001792 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001794channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001795{
1796 js_read_T reader;
1797 typval_T listtv;
1798 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001799 chanpart_T *chanpart = &channel->ch_part[part];
1800 jsonq_T *head = &chanpart->ch_json_head;
1801 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001802 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001803
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001804 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001805 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001806
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001807 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001808 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001809 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001810 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001811 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001812
1813 /* When a message is incomplete we wait for a short while for more to
1814 * arrive. After the delay drop the input, otherwise a truncated string
1815 * or list will make us hang. */
1816 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001817 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001818 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001819 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001820 /* Only accept the response when it is a list with at least two
1821 * items. */
1822 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001823 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001824 if (listtv.v_type != VAR_LIST)
1825 ch_error(channel, "Did not receive a list, discarding");
1826 else
1827 ch_errorn(channel, "Expected list with two items, got %d",
1828 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001829 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001830 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001831 else
1832 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001833 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1834 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001835 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836 else
1837 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001838 item->jq_value = alloc_tv();
1839 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001840 {
1841 vim_free(item);
1842 clear_tv(&listtv);
1843 }
1844 else
1845 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 *item->jq_value = listtv;
1847 item->jq_prev = head->jq_prev;
1848 head->jq_prev = item;
1849 item->jq_next = NULL;
1850 if (item->jq_prev == NULL)
1851 head->jq_next = item;
1852 else
1853 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001854 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001855 }
1856 }
1857 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001858
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001859 if (status == OK)
1860 chanpart->ch_waiting = FALSE;
1861 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001862 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001863 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001864 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001865 /* First time encountering incomplete message, set a deadline of
1866 * 100 msec. */
1867 ch_log(channel, "Incomplete message - wait for more");
1868 reader.js_used = 0;
1869 chanpart->ch_waiting = TRUE;
1870#ifdef WIN32
1871 chanpart->ch_deadline = GetTickCount() + 100L;
1872#else
1873 gettimeofday(&chanpart->ch_deadline, NULL);
1874 chanpart->ch_deadline.tv_usec += 100 * 1000;
1875 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1876 {
1877 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1878 ++chanpart->ch_deadline.tv_sec;
1879 }
1880#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001881 }
1882 else
1883 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001884 int timeout;
1885#ifdef WIN32
1886 timeout = GetTickCount() > chanpart->ch_deadline;
1887#else
1888 {
1889 struct timeval now_tv;
1890
1891 gettimeofday(&now_tv, NULL);
1892 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1893 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1894 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1895 }
1896#endif
1897 if (timeout)
1898 {
1899 status = FAIL;
1900 chanpart->ch_waiting = FALSE;
1901 }
1902 else
1903 {
1904 reader.js_used = 0;
1905 ch_log(channel, "still waiting on incomplete message");
1906 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001907 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001908 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001909
1910 if (status == FAIL)
1911 {
1912 ch_error(channel, "Decoding failed - discarding input");
1913 ret = FALSE;
1914 chanpart->ch_waiting = FALSE;
1915 }
1916 else if (reader.js_buf[reader.js_used] != NUL)
1917 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001918 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001919 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001920 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1921 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001922 ret = status == MAYBE ? FALSE: TRUE;
1923 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001924 else
1925 ret = FALSE;
1926
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001927 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001928 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001929}
1930
1931/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001932 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001933 */
1934 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001935remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001936{
Bram Moolenaar77073442016-02-13 23:23:53 +01001937 if (node->cq_prev == NULL)
1938 head->cq_next = node->cq_next;
1939 else
1940 node->cq_prev->cq_next = node->cq_next;
1941 if (node->cq_next == NULL)
1942 head->cq_prev = node->cq_prev;
1943 else
1944 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001945}
1946
1947/*
1948 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001949 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001950 */
1951 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001952remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001953{
Bram Moolenaar77073442016-02-13 23:23:53 +01001954 if (node->jq_prev == NULL)
1955 head->jq_next = node->jq_next;
1956 else
1957 node->jq_prev->jq_next = node->jq_next;
1958 if (node->jq_next == NULL)
1959 head->jq_prev = node->jq_prev;
1960 else
1961 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001962 vim_free(node);
1963}
1964
1965/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001966 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001967 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001968 * When "id" is zero or negative jut get the first message. But not the one
1969 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001970 * Return OK when found and return the value in "rettv".
1971 * Return FAIL otherwise.
1972 */
1973 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001974channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001975{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001976 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001977 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001978
Bram Moolenaar77073442016-02-13 23:23:53 +01001979 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001980 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001981 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001982 typval_T *tv = &l->lv_first->li_tv;
1983
1984 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001985 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001986 || tv->vval.v_number == 0
1987 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001988 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001989 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001990 if (tv->v_type == VAR_NUMBER)
1991 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001992 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001993 return OK;
1994 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001995 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001996 }
1997 return FAIL;
1998}
1999
Bram Moolenaarece61b02016-02-20 21:39:05 +01002000#define CH_JSON_MAX_ARGS 4
2001
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002002/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002003 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002004 * "argv[0]" is the command string.
2005 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002006 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002007 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002008channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002009{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002010 char_u *cmd = argv[0].vval.v_string;
2011 char_u *arg;
2012 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002013
Bram Moolenaarece61b02016-02-20 21:39:05 +01002014 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002015 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002016 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002017 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002018 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002019 return;
2020 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002021 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002022 if (arg == NULL)
2023 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002024
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002025 if (STRCMP(cmd, "ex") == 0)
2026 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002027 int save_called_emsg = called_emsg;
2028
2029 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002030 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002031 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002032 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002033 --emsg_silent;
2034 if (called_emsg)
2035 ch_logs(channel, "Ex command error: '%s'",
2036 (char *)get_vim_var_str(VV_ERRMSG));
2037 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002038 }
2039 else if (STRCMP(cmd, "normal") == 0)
2040 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002041 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002042
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002043 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002044 ea.arg = arg;
2045 ea.addr_count = 0;
2046 ea.forceit = TRUE; /* no mapping */
2047 ex_normal(&ea);
2048 }
2049 else if (STRCMP(cmd, "redraw") == 0)
2050 {
2051 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002052
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002053 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002054 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002055 ex_redraw(&ea);
2056 showruler(FALSE);
2057 setcursor();
2058 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002059#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002060 if (gui.in_use)
2061 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002062 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002063 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002064 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002065#endif
2066 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002067 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002068 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002069 int is_call = cmd[0] == 'c';
2070 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002071
Bram Moolenaarece61b02016-02-20 21:39:05 +01002072 if (argv[id_idx].v_type != VAR_UNKNOWN
2073 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002074 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002075 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002076 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002077 EMSG("E904: last argument for expr/call must be a number");
2078 }
2079 else if (is_call && argv[2].v_type != VAR_LIST)
2080 {
2081 ch_error(channel, "third argument for call must be a list");
2082 if (p_verbose > 2)
2083 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002084 }
2085 else
2086 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002087 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002088 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002089 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002090 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002091
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002092 /* Don't pollute the display with errors. */
2093 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002094 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002095 {
2096 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002097 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002098 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002099 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002100 {
2101 ch_logs(channel, "Calling '%s'", (char *)arg);
2102 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2103 tv = &res_tv;
2104 else
2105 tv = NULL;
2106 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002107
2108 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002109 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002110 int id = argv[id_idx].vval.v_number;
2111
Bram Moolenaar55fab432016-02-07 16:53:13 +01002112 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002113 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002114 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002115 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002116 /* If evaluation failed or the result can't be encoded
2117 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002118 vim_free(json);
2119 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002120 err_tv.v_type = VAR_STRING;
2121 err_tv.vval.v_string = (char_u *)"ERROR";
2122 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002123 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002124 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002125 if (json != NULL)
2126 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002127 channel_send(channel,
2128 part == PART_SOCK ? PART_SOCK : PART_IN,
2129 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002130 vim_free(json);
2131 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002132 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002133 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002134 if (tv == &res_tv)
2135 clear_tv(tv);
2136 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002137 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002138 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002139 }
2140 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002141 {
2142 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002143 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002144 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002145}
2146
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002147/*
2148 * Invoke the callback at "cbhead".
2149 * Does not redraw but sets channel_need_redraw.
2150 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002151 static void
2152invoke_one_time_callback(
2153 channel_T *channel,
2154 cbq_T *cbhead,
2155 cbq_T *item,
2156 typval_T *argv)
2157{
2158 ch_logs(channel, "Invoking one-time callback %s",
2159 (char *)item->cq_callback);
2160 /* Remove the item from the list first, if the callback
2161 * invokes ch_close() the list will be cleared. */
2162 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002163 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002164 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002165 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002166 vim_free(item);
2167}
2168
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002169 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002170append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002171{
2172 buf_T *save_curbuf = curbuf;
2173 linenr_T lnum = buffer->b_ml.ml_line_count;
2174 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002175 chanpart_T *ch_part = &channel->ch_part[part];
2176 int save_p_ma = buffer->b_p_ma;
2177
2178 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2179 {
2180 if (!ch_part->ch_nomod_error)
2181 {
2182 ch_error(channel, "Buffer is not modifiable, cannot append");
2183 ch_part->ch_nomod_error = TRUE;
2184 }
2185 return;
2186 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002187
2188 /* If the buffer is also used as input insert above the last
2189 * line. Don't write these lines. */
2190 if (save_write_to)
2191 {
2192 --lnum;
2193 buffer->b_write_to_channel = FALSE;
2194 }
2195
2196 /* Append to the buffer */
2197 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2198
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002199 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002200 curbuf = buffer;
2201 u_sync(TRUE);
2202 /* ignore undo failure, undo is not very useful here */
2203 ignored = u_save(lnum, lnum + 1);
2204
2205 ml_append(lnum, msg, 0, FALSE);
2206 appended_lines_mark(lnum, 1L);
2207 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002208 if (ch_part->ch_nomodifiable)
2209 buffer->b_p_ma = FALSE;
2210 else
2211 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002212
2213 if (buffer->b_nwindows > 0)
2214 {
2215 win_T *wp;
2216 win_T *save_curwin;
2217
2218 FOR_ALL_WINDOWS(wp)
2219 {
2220 if (wp->w_buffer == buffer
2221 && (save_write_to
2222 ? wp->w_cursor.lnum == lnum + 1
2223 : (wp->w_cursor.lnum == lnum
2224 && wp->w_cursor.col == 0)))
2225 {
2226 ++wp->w_cursor.lnum;
2227 save_curwin = curwin;
2228 curwin = wp;
2229 curbuf = curwin->w_buffer;
2230 scroll_cursor_bot(0, FALSE);
2231 curwin = save_curwin;
2232 curbuf = curwin->w_buffer;
2233 }
2234 }
2235 redraw_buf_later(buffer, VALID);
2236 channel_need_redraw = TRUE;
2237 }
2238
2239 if (save_write_to)
2240 {
2241 channel_T *ch;
2242
2243 /* Find channels reading from this buffer and adjust their
2244 * next-to-read line number. */
2245 buffer->b_write_to_channel = TRUE;
2246 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2247 {
2248 chanpart_T *in_part = &ch->ch_part[PART_IN];
2249
2250 if (in_part->ch_buffer == buffer)
2251 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2252 }
2253 }
2254}
2255
Bram Moolenaar437905c2016-04-26 19:01:05 +02002256 static void
2257drop_messages(channel_T *channel, int part)
2258{
2259 char_u *msg;
2260
2261 while ((msg = channel_get(channel, part)) != NULL)
2262 {
2263 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2264 vim_free(msg);
2265 }
2266}
2267
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002268/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002269 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002270 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002271 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002272 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002273 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002274may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002275{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002276 char_u *msg = NULL;
2277 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002278 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002279 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002280 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002281 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002282 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002283 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002284 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002285 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002286
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002287 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002288 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002289 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002290
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002291 /* Use a message-specific callback, part callback or channel callback */
2292 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2293 if (cbitem->cq_seq_nr == 0)
2294 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002295 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002296 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002297 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002298 partial = cbitem->cq_partial;
2299 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002300 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002301 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002302 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002303 partial = channel->ch_part[part].ch_partial;
2304 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002305 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002306 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002307 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002308 partial = channel->ch_partial;
2309 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002310
Bram Moolenaar187db502016-02-27 14:44:26 +01002311 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002312 if (buffer != NULL && !buf_valid(buffer))
2313 {
2314 /* buffer was wiped out */
2315 channel->ch_part[part].ch_buffer = NULL;
2316 buffer = NULL;
2317 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002318
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002319 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002320 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002321 listitem_T *item;
2322 int argc = 0;
2323
Bram Moolenaard7ece102016-02-02 23:23:02 +01002324 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002325 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002326 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002327 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002328 channel_parse_json(channel, part);
2329 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002330 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002331 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002332
Bram Moolenaarece61b02016-02-20 21:39:05 +01002333 for (item = listtv->vval.v_list->lv_first;
2334 item != NULL && argc < CH_JSON_MAX_ARGS;
2335 item = item->li_next)
2336 argv[argc++] = item->li_tv;
2337 while (argc < CH_JSON_MAX_ARGS)
2338 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002339
Bram Moolenaarece61b02016-02-20 21:39:05 +01002340 if (argv[0].v_type == VAR_STRING)
2341 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002342 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002343 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002344 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002345 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002346 }
2347
Bram Moolenaarece61b02016-02-20 21:39:05 +01002348 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002349 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002350 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002351 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002352 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002353 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002354 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002355 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002356 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002357 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002358 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002359 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002360 return FALSE;
2361 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002362 else
2363 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002364 /* If there is no callback or buffer drop the message. */
2365 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002366 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002367 /* If there is a close callback it may use ch_read() to get the
2368 * messages. */
2369 if (channel->ch_close_cb == NULL)
2370 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002371 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002372 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002373
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002374 if (ch_mode == MODE_NL)
2375 {
2376 char_u *nl;
2377 char_u *buf;
2378
2379 /* See if we have a message ending in NL in the first buffer. If
2380 * not try to concatenate the first and the second buffer. */
2381 while (TRUE)
2382 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002383 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002384 nl = vim_strchr(buf, NL);
2385 if (nl != NULL)
2386 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002387 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002388 return FALSE; /* incomplete message */
2389 }
2390 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002391 {
2392 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002393 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002394 *nl = NUL;
2395 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002396 else
2397 {
2398 /* Copy the message into allocated memory and remove it from
2399 * the buffer. */
2400 msg = vim_strnsave(buf, (int)(nl - buf));
2401 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2402 }
2403 }
2404 else
2405 /* For a raw channel we don't know where the message ends, just
2406 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002407 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002408
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002409 if (msg == NULL)
2410 return FALSE; /* out of memory (and avoids Coverity warning) */
2411
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002412 argv[1].v_type = VAR_STRING;
2413 argv[1].vval.v_string = msg;
2414 }
2415
Bram Moolenaara07fec92016-02-05 21:04:08 +01002416 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002417 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002418 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002419
2420 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002421 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002422 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002423 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002424 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002425 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002426 break;
2427 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002428 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002429 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002430 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002431 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002432 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002433 if (buffer != NULL)
2434 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002435 if (msg == NULL)
2436 /* JSON or JS mode: re-encode the message. */
2437 msg = json_encode(listtv, ch_mode);
2438 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002439 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002440 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002441
Bram Moolenaar187db502016-02-27 14:44:26 +01002442 if (callback != NULL)
2443 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002444 if (cbitem != NULL)
2445 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2446 else
2447 {
2448 /* invoke the channel callback */
2449 ch_logs(channel, "Invoking channel callback %s",
2450 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002451 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002452 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002453 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002454 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002455 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002456 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002457
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002458 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002459 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002460 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002461
2462 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002463}
2464
2465/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002466 * Return TRUE when channel "channel" is open for writing to.
2467 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002468 */
2469 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002470channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002471{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002472 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002473 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002474}
2475
2476/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002477 * Return TRUE when channel "channel" is open for reading or writing.
2478 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002479 */
2480 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002481channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002482{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002483 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002484 || channel->CH_IN_FD != INVALID_FD
2485 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002486 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002487}
2488
2489/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002490 * Return TRUE if "channel" has JSON or other typeahead.
2491 */
2492 static int
2493channel_has_readahead(channel_T *channel, int part)
2494{
2495 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2496
2497 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2498 {
2499 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2500 jsonq_T *item = head->jq_next;
2501
2502 return item != NULL;
2503 }
2504 return channel_peek(channel, part) != NULL;
2505}
2506
2507/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002508 * Return a string indicating the status of the channel.
2509 */
2510 char *
2511channel_status(channel_T *channel)
2512{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002513 int part;
2514 int has_readahead = FALSE;
2515
Bram Moolenaar77073442016-02-13 23:23:53 +01002516 if (channel == NULL)
2517 return "fail";
2518 if (channel_is_open(channel))
2519 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002520 for (part = PART_SOCK; part <= PART_ERR; ++part)
2521 if (channel_has_readahead(channel, part))
2522 {
2523 has_readahead = TRUE;
2524 break;
2525 }
2526
2527 if (has_readahead)
2528 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002529 return "closed";
2530}
2531
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002532 static void
2533channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2534{
2535 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002536 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002537 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002538 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002539
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002540 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002541 STRCAT(namebuf, "_");
2542 tail = STRLEN(namebuf);
2543
2544 STRCPY(namebuf + tail, "status");
2545 dict_add_nr_str(dict, namebuf, 0,
2546 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2547
2548 STRCPY(namebuf + tail, "mode");
2549 switch (chanpart->ch_mode)
2550 {
2551 case MODE_NL: s = "NL"; break;
2552 case MODE_RAW: s = "RAW"; break;
2553 case MODE_JSON: s = "JSON"; break;
2554 case MODE_JS: s = "JS"; break;
2555 }
2556 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2557
2558 STRCPY(namebuf + tail, "io");
2559 if (part == PART_SOCK)
2560 s = "socket";
2561 else switch (chanpart->ch_io)
2562 {
2563 case JIO_NULL: s = "null"; break;
2564 case JIO_PIPE: s = "pipe"; break;
2565 case JIO_FILE: s = "file"; break;
2566 case JIO_BUFFER: s = "buffer"; break;
2567 case JIO_OUT: s = "out"; break;
2568 }
2569 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2570
2571 STRCPY(namebuf + tail, "timeout");
2572 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2573}
2574
2575 void
2576channel_info(channel_T *channel, dict_T *dict)
2577{
2578 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2579 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2580
2581 if (channel->ch_hostname != NULL)
2582 {
2583 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2584 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2585 channel_part_info(channel, dict, "sock", PART_SOCK);
2586 }
2587 else
2588 {
2589 channel_part_info(channel, dict, "out", PART_OUT);
2590 channel_part_info(channel, dict, "err", PART_ERR);
2591 channel_part_info(channel, dict, "in", PART_IN);
2592 }
2593}
2594
Bram Moolenaar77073442016-02-13 23:23:53 +01002595/*
2596 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002597 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002598 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002599 */
2600 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002601channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002602{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002603 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002604
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002605#ifdef FEAT_GUI
2606 channel_gui_unregister(channel);
2607#endif
2608
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002609 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002610 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002611 sock_close(channel->CH_SOCK_FD);
2612 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002613 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002614 may_close_part(&channel->CH_IN_FD);
2615 may_close_part(&channel->CH_OUT_FD);
2616 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002617
Bram Moolenaar8b374212016-02-24 20:43:06 +01002618 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002619 {
2620 typval_T argv[1];
2621 typval_T rettv;
2622 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002623 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002624
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002625 /* Invoke callbacks before the close callback, since it's weird to
2626 * first invoke the close callback. Increment the refcount to avoid
2627 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002628 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002629 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002630 for (part = PART_SOCK; part <= PART_ERR; ++part)
2631 while (may_invoke_callback(channel, part))
2632 ;
2633
2634 /* Invoke the close callback, if still set. */
2635 if (channel->ch_close_cb != NULL)
2636 {
2637 ch_logs(channel, "Invoking close callback %s",
2638 (char *)channel->ch_close_cb);
2639 argv[0].v_type = VAR_CHANNEL;
2640 argv[0].vval.v_channel = channel;
2641 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002642 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2643 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002644 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002645 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002646 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002647
2648 /* the callback is only called once */
2649 vim_free(channel->ch_close_cb);
2650 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002651 partial_unref(channel->ch_close_partial);
2652 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002653
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002654 --channel->ch_refcount;
2655
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002656 if (channel_need_redraw)
2657 {
2658 channel_need_redraw = FALSE;
2659 redraw_after_callback();
2660 }
2661
Bram Moolenaar437905c2016-04-26 19:01:05 +02002662 /* any remaining messages are useless now */
2663 for (part = PART_SOCK; part <= PART_ERR; ++part)
2664 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002665 }
2666
2667 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002668}
2669
Bram Moolenaard04a0202016-01-26 23:30:18 +01002670/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002671 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002672 * Returns NULL if there is nothing.
2673 */
2674 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002675channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002676{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002677 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002678
Bram Moolenaar77073442016-02-13 23:23:53 +01002679 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002680 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002681 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002682}
2683
2684/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002685 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002686 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002687 static void
2688channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002689{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002690 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2691 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002692
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002693 while (channel_peek(channel, part) != NULL)
2694 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002695
2696 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002697 {
2698 cbq_T *node = cb_head->cq_next;
2699
2700 remove_cb_node(cb_head, node);
2701 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002702 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002703 vim_free(node);
2704 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002705
2706 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002707 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002708 free_tv(json_head->jq_next->jq_value);
2709 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002710 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002711
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002712 vim_free(channel->ch_part[part].ch_callback);
2713 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002714 partial_unref(channel->ch_part[part].ch_partial);
2715 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002716}
2717
2718/*
2719 * Clear all the read buffers on "channel".
2720 */
2721 void
2722channel_clear(channel_T *channel)
2723{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002724 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002725 vim_free(channel->ch_hostname);
2726 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002727 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002728 channel_clear_one(channel, PART_OUT);
2729 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002730 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002731 vim_free(channel->ch_callback);
2732 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002733 partial_unref(channel->ch_partial);
2734 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002735 vim_free(channel->ch_close_cb);
2736 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002737 partial_unref(channel->ch_close_partial);
2738 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002739}
2740
Bram Moolenaar77073442016-02-13 23:23:53 +01002741#if defined(EXITFREE) || defined(PROTO)
2742 void
2743channel_free_all(void)
2744{
2745 channel_T *channel;
2746
Bram Moolenaard6051b52016-02-28 15:49:03 +01002747 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002748 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2749 channel_clear(channel);
2750}
2751#endif
2752
2753
Bram Moolenaar715d2852016-04-30 17:06:31 +02002754/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002755#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002756
2757/* Buffer size for reading incoming messages. */
2758#define MAXMSGSIZE 4096
2759
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002760#if defined(HAVE_SELECT)
2761/*
2762 * Add write fds where we are waiting for writing to be possible.
2763 */
2764 static int
2765channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2766{
2767 int maxfd = maxfd_arg;
2768 channel_T *ch;
2769
2770 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2771 {
2772 chanpart_T *in_part = &ch->ch_part[PART_IN];
2773
2774 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2775 {
2776 FD_SET((int)in_part->ch_fd, wfds);
2777 if ((int)in_part->ch_fd >= maxfd)
2778 maxfd = (int)in_part->ch_fd + 1;
2779 }
2780 }
2781 return maxfd;
2782}
2783#else
2784/*
2785 * Add write fds where we are waiting for writing to be possible.
2786 */
2787 static int
2788channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2789{
2790 int nfd = nfd_in;
2791 channel_T *ch;
2792
2793 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2794 {
2795 chanpart_T *in_part = &ch->ch_part[PART_IN];
2796
2797 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2798 {
2799 in_part->ch_poll_idx = nfd;
2800 fds[nfd].fd = in_part->ch_fd;
2801 fds[nfd].events = POLLOUT;
2802 ++nfd;
2803 }
2804 else
2805 in_part->ch_poll_idx = -1;
2806 }
2807 return nfd;
2808}
2809#endif
2810
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002811typedef enum {
2812 CW_READY,
2813 CW_NOT_READY,
2814 CW_ERROR
2815} channel_wait_result;
2816
Bram Moolenaard04a0202016-01-26 23:30:18 +01002817/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002818 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002819 * Return CW_READY when there is something to read.
2820 * Return CW_NOT_READY when there is nothing to read.
2821 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002822 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002823 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002824channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002825{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002826 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002827 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002828
Bram Moolenaard8070362016-02-15 21:56:54 +01002829# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002830 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002831 {
2832 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002833 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002834 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002835 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002836
2837 /* reading from a pipe, not a socket */
2838 while (TRUE)
2839 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002840 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2841
2842 if (r && nread > 0)
2843 return CW_READY;
2844 if (r == 0)
2845 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002846
2847 /* perhaps write some buffer lines */
2848 channel_write_any_lines();
2849
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002850 sleep_time = deadline - GetTickCount();
2851 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002852 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002853 /* Wait for a little while. Very short at first, up to 10 msec
2854 * after looping a few times. */
2855 if (sleep_time > delay)
2856 sleep_time = delay;
2857 Sleep(sleep_time);
2858 delay = delay * 2;
2859 if (delay > 10)
2860 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002861 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002862 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002863 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002864#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002865 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002866#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002867 struct timeval tval;
2868 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002869 fd_set wfds;
2870 int ret;
2871 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002872
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002873 tval.tv_sec = timeout / 1000;
2874 tval.tv_usec = (timeout % 1000) * 1000;
2875 for (;;)
2876 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002877 FD_ZERO(&rfds);
2878 FD_SET((int)fd, &rfds);
2879
2880 /* Write lines to a pipe when a pipe can be written to. Need to
2881 * set this every time, some buffers may be done. */
2882 maxfd = (int)fd + 1;
2883 FD_ZERO(&wfds);
2884 maxfd = channel_fill_wfds(maxfd, &wfds);
2885
2886 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002887# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002888 SOCK_ERRNO;
2889 if (ret == -1 && errno == EINTR)
2890 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002891# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002892 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002893 {
2894 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002895 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002896 channel_write_any_lines();
2897 continue;
2898 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002899 break;
2900 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002901#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002902 for (;;)
2903 {
2904 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2905 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002906
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002907 fds[0].fd = fd;
2908 fds[0].events = POLLIN;
2909 nfd = channel_fill_poll_write(nfd, fds);
2910 if (poll(fds, nfd, timeout) > 0)
2911 {
2912 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002913 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002914 channel_write_any_lines();
2915 continue;
2916 }
2917 break;
2918 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002919#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002920 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002921 return CW_NOT_READY;
2922}
2923
2924 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002925channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002926{
2927 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002928 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2929 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002930
2931 /* Queue a "DETACH" netbeans message in the command queue in order to
2932 * terminate the netbeans session later. Do not end the session here
2933 * directly as we may be running in the context of a call to
2934 * netbeans_parse_messages():
2935 * netbeans_parse_messages
2936 * -> autocmd triggered while processing the netbeans cmd
2937 * -> ui_breakcheck
2938 * -> gui event loop or select loop
2939 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002940 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002941 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002942 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002943 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002944 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2945
2946 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002947 * died. Don't close the channel right away, it may be the wrong moment
2948 * to invoke callbacks. */
2949 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02002950
2951#ifdef FEAT_GUI
2952 /* Stop listening to GUI events right away. */
2953 channel_gui_unregister(channel);
2954#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002955}
2956
2957 static void
2958channel_close_now(channel_T *channel)
2959{
2960 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002961 channel_close(channel, TRUE);
2962 if (channel->ch_nb_close_cb != NULL)
2963 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002964}
2965
2966/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002967 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002968 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02002969 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002970 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002971 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002972channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002973{
2974 static char_u *buf = NULL;
2975 int len = 0;
2976 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002977 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002978 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002979
Bram Moolenaar5850a762016-05-27 19:59:48 +02002980 /* If we detected a read error don't try reading again. */
2981 if (channel->ch_to_be_closed)
2982 return;
2983
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002984 fd = channel->ch_part[part].ch_fd;
2985 if (fd == INVALID_FD)
2986 {
2987 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002988 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002989 }
2990 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002991
2992 /* Allocate a buffer to read into. */
2993 if (buf == NULL)
2994 {
2995 buf = alloc(MAXMSGSIZE);
2996 if (buf == NULL)
2997 return; /* out of memory! */
2998 }
2999
3000 /* Keep on reading for as long as there is something to read.
3001 * Use select() or poll() to avoid blocking on a message that is exactly
3002 * MAXMSGSIZE long. */
3003 for (;;)
3004 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003005 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003006 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003007 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003008 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003009 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003010 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003011 if (len <= 0)
3012 break; /* error or nothing more to read */
3013
3014 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003015 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003016 readlen += len;
3017 if (len < MAXMSGSIZE)
3018 break; /* did read everything that's available */
3019 }
3020
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003021 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003022 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003023 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003024
3025#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003026 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003027 if (CH_HAS_GUI && gtk_main_level() > 0)
3028 gtk_main_quit();
3029#endif
3030}
3031
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003032/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003033 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003034 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003035 * Returns what was read in allocated memory.
3036 * Returns NULL in case of error or timeout.
3037 */
3038 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003039channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003040{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003041 char_u *buf;
3042 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003043 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003044 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003045 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003046
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003047 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003048 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003049
3050 while (TRUE)
3051 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003052 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003053 if (buf != NULL && (mode == MODE_RAW
3054 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
3055 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02003056 if (buf != NULL && channel_collapse(channel, part, mode == MODE_NL)
3057 == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003058 continue;
3059
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003060 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003061 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003062 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003063 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003064 {
3065 ch_log(channel, "Timed out");
3066 return NULL;
3067 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003068 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003069 }
3070
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003071 if (mode == MODE_RAW)
3072 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003073 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003074 }
3075 else
3076 {
3077 nl = vim_strchr(buf, NL);
3078 if (nl[1] == NUL)
3079 {
3080 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003081 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003082 *nl = NUL;
3083 }
3084 else
3085 {
3086 /* Copy the message into allocated memory and remove it from the
3087 * buffer. */
3088 msg = vim_strnsave(buf, (int)(nl - buf));
3089 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3090 }
3091 }
3092 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003093 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003094 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003095}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003096
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003097/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003098 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003099 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003100 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003101 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003102 */
3103 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003104channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003105 channel_T *channel,
3106 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003107 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003108 int id,
3109 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003110{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003111 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003112 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003113 int timeout;
3114 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003115
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003116 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003117 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003118 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003119 for (;;)
3120 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003121 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003122
3123 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003124 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003125 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003126 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003127 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003128 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003129
Bram Moolenaard7ece102016-02-02 23:23:02 +01003130 if (!more)
3131 {
3132 /* Handle any other messages in the queue. If done some more
3133 * messages may have arrived. */
3134 if (channel_parse_messages())
3135 continue;
3136
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003137 /* Wait for up to the timeout. If there was an incomplete message
3138 * use the deadline for that. */
3139 timeout = timeout_arg;
3140 if (chanpart->ch_waiting)
3141 {
3142#ifdef WIN32
3143 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3144#else
3145 {
3146 struct timeval now_tv;
3147
3148 gettimeofday(&now_tv, NULL);
3149 timeout = (chanpart->ch_deadline.tv_sec
3150 - now_tv.tv_sec) * 1000
3151 + (chanpart->ch_deadline.tv_usec
3152 - now_tv.tv_usec) / 1000
3153 + 1;
3154 }
3155#endif
3156 if (timeout < 0)
3157 {
3158 /* Something went wrong, channel_parse_json() didn't
3159 * discard message. Cancel waiting. */
3160 chanpart->ch_waiting = FALSE;
3161 timeout = timeout_arg;
3162 }
3163 else if (timeout > timeout_arg)
3164 timeout = timeout_arg;
3165 }
3166 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003167 if (fd == INVALID_FD
3168 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003169 {
3170 if (timeout == timeout_arg)
3171 {
3172 if (fd != INVALID_FD)
3173 ch_log(channel, "Timed out");
3174 break;
3175 }
3176 }
3177 else
3178 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003179 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003180 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003181 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003182 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003183}
3184
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003185/*
3186 * Common for ch_read() and ch_readraw().
3187 */
3188 void
3189common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3190{
3191 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003192 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003193 jobopt_T opt;
3194 int mode;
3195 int timeout;
3196 int id = -1;
3197 typval_T *listtv = NULL;
3198
3199 /* return an empty string by default */
3200 rettv->v_type = VAR_STRING;
3201 rettv->vval.v_string = NULL;
3202
3203 clear_job_options(&opt);
3204 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3205 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003206 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003207
Bram Moolenaar437905c2016-04-26 19:01:05 +02003208 if (opt.jo_set & JO_PART)
3209 part = opt.jo_part;
3210 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003211 if (channel != NULL)
3212 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003213 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003214 part = channel_part_read(channel);
3215 mode = channel_get_mode(channel, part);
3216 timeout = channel_get_timeout(channel, part);
3217 if (opt.jo_set & JO_TIMEOUT)
3218 timeout = opt.jo_timeout;
3219
3220 if (raw || mode == MODE_RAW || mode == MODE_NL)
3221 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3222 else
3223 {
3224 if (opt.jo_set & JO_ID)
3225 id = opt.jo_id;
3226 channel_read_json_block(channel, part, timeout, id, &listtv);
3227 if (listtv != NULL)
3228 {
3229 *rettv = *listtv;
3230 vim_free(listtv);
3231 }
3232 else
3233 {
3234 rettv->v_type = VAR_SPECIAL;
3235 rettv->vval.v_number = VVAL_NONE;
3236 }
3237 }
3238 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003239
3240theend:
3241 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003242}
3243
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003244# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3245 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003246/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003247 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003248 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003249 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003250 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003251channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003252{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003253 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003254 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003255
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003256 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003257 for (channel = first_channel; channel != NULL;
3258 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003259 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003260 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003261 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003262 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003263 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003264 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003265 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003266 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003267 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003268}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003269# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003270
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003271# if defined(WIN32) || defined(PROTO)
3272/*
3273 * Check the channels for anything that is ready to be read.
3274 * The data is put in the read queue.
3275 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003276 void
3277channel_handle_events(void)
3278{
3279 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003280 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003281 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003282
3283 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3284 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003285 /* If we detected a read error don't try reading again. */
3286 if (channel->ch_to_be_closed)
3287 continue;
3288
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003289 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003290 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003291 {
3292 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003293 if (fd != INVALID_FD)
3294 {
3295 int r = channel_wait(channel, fd, 0);
3296
3297 if (r == CW_READY)
3298 channel_read(channel, part, "channel_handle_events");
3299 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003300 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003301 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003302 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003303 }
3304}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003305# endif
3306
Bram Moolenaard04a0202016-01-26 23:30:18 +01003307/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003308 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003309 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003310 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003311 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003312 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003313channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003314{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003315 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003316 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003317 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003318
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003319 fd = channel->ch_part[part].ch_fd;
3320 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003321 {
3322 if (!channel->ch_error && fun != NULL)
3323 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003324 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003325 EMSG2("E630: %s(): write while not connected", fun);
3326 }
3327 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003328 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003329 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003330
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003331 if (log_fd != NULL)
3332 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003333 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003334 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003335 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003336 fprintf(log_fd, "'\n");
3337 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003338 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003339 }
3340
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003341 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003342 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003343 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003344 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003345 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003346 {
3347 if (!channel->ch_error && fun != NULL)
3348 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003349 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003350 EMSG2("E631: %s(): write failed", fun);
3351 }
3352 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003353 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003354 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003355
3356 channel->ch_error = FALSE;
3357 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003358}
3359
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003360/*
3361 * Common for "ch_sendexpr()" and "ch_sendraw()".
3362 * Returns the channel if the caller should read the response.
3363 * Sets "part_read" to the the read fd.
3364 * Otherwise returns NULL.
3365 */
3366 channel_T *
3367send_common(
3368 typval_T *argvars,
3369 char_u *text,
3370 int id,
3371 int eval,
3372 jobopt_T *opt,
3373 char *fun,
3374 int *part_read)
3375{
3376 channel_T *channel;
3377 int part_send;
3378
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003379 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003380 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003381 if (channel == NULL)
3382 return NULL;
3383 part_send = channel_part_send(channel);
3384 *part_read = channel_part_read(channel);
3385
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003386 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3387 return NULL;
3388
3389 /* Set the callback. An empty callback means no callback and not reading
3390 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3391 * allowed. */
3392 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3393 {
3394 if (eval)
3395 {
3396 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3397 return NULL;
3398 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003399 channel_set_req_callback(channel, part_send,
3400 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003401 }
3402
3403 if (channel_send(channel, part_send, text, fun) == OK
3404 && opt->jo_callback == NULL)
3405 return channel;
3406 return NULL;
3407}
3408
3409/*
3410 * common for "ch_evalexpr()" and "ch_sendexpr()"
3411 */
3412 void
3413ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3414{
3415 char_u *text;
3416 typval_T *listtv;
3417 channel_T *channel;
3418 int id;
3419 ch_mode_T ch_mode;
3420 int part_send;
3421 int part_read;
3422 jobopt_T opt;
3423 int timeout;
3424
3425 /* return an empty string by default */
3426 rettv->v_type = VAR_STRING;
3427 rettv->vval.v_string = NULL;
3428
Bram Moolenaar437905c2016-04-26 19:01:05 +02003429 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003430 if (channel == NULL)
3431 return;
3432 part_send = channel_part_send(channel);
3433
3434 ch_mode = channel_get_mode(channel, part_send);
3435 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3436 {
3437 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3438 return;
3439 }
3440
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003441 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003442 text = json_encode_nr_expr(id, &argvars[1],
3443 ch_mode == MODE_JS ? JSON_JS : 0);
3444 if (text == NULL)
3445 return;
3446
3447 channel = send_common(argvars, text, id, eval, &opt,
3448 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3449 vim_free(text);
3450 if (channel != NULL && eval)
3451 {
3452 if (opt.jo_set & JO_TIMEOUT)
3453 timeout = opt.jo_timeout;
3454 else
3455 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003456 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3457 == OK)
3458 {
3459 list_T *list = listtv->vval.v_list;
3460
3461 /* Move the item from the list and then change the type to
3462 * avoid the value being freed. */
3463 *rettv = list->lv_last->li_tv;
3464 list->lv_last->li_tv.v_type = VAR_NUMBER;
3465 free_tv(listtv);
3466 }
3467 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003468 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003469}
3470
3471/*
3472 * common for "ch_evalraw()" and "ch_sendraw()"
3473 */
3474 void
3475ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3476{
3477 char_u buf[NUMBUFLEN];
3478 char_u *text;
3479 channel_T *channel;
3480 int part_read;
3481 jobopt_T opt;
3482 int timeout;
3483
3484 /* return an empty string by default */
3485 rettv->v_type = VAR_STRING;
3486 rettv->vval.v_string = NULL;
3487
3488 text = get_tv_string_buf(&argvars[1], buf);
3489 channel = send_common(argvars, text, 0, eval, &opt,
3490 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3491 if (channel != NULL && eval)
3492 {
3493 if (opt.jo_set & JO_TIMEOUT)
3494 timeout = opt.jo_timeout;
3495 else
3496 timeout = channel_get_timeout(channel, part_read);
3497 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3498 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003499 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003500}
3501
Bram Moolenaard04a0202016-01-26 23:30:18 +01003502# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003503/*
3504 * Add open channels to the poll struct.
3505 * Return the adjusted struct index.
3506 * The type of "fds" is hidden to avoid problems with the function proto.
3507 */
3508 int
3509channel_poll_setup(int nfd_in, void *fds_in)
3510{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003511 int nfd = nfd_in;
3512 channel_T *channel;
3513 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003514 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003515
Bram Moolenaar77073442016-02-13 23:23:53 +01003516 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003517 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003518 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003519 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003520 chanpart_T *ch_part = &channel->ch_part[part];
3521
3522 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003523 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003524 ch_part->ch_poll_idx = nfd;
3525 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003526 fds[nfd].events = POLLIN;
3527 nfd++;
3528 }
3529 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003530 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003531 }
3532 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003533
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003534 nfd = channel_fill_poll_write(nfd, fds);
3535
Bram Moolenaare0874f82016-01-24 20:36:41 +01003536 return nfd;
3537}
3538
3539/*
3540 * The type of "fds" is hidden to avoid problems with the function proto.
3541 */
3542 int
3543channel_poll_check(int ret_in, void *fds_in)
3544{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003545 int ret = ret_in;
3546 channel_T *channel;
3547 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003548 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003549 int idx;
3550 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003551
Bram Moolenaar77073442016-02-13 23:23:53 +01003552 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003553 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003554 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003555 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003556 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003557
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003558 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003559 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003560 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003561 --ret;
3562 }
3563 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003564
3565 in_part = &channel->ch_part[PART_IN];
3566 idx = in_part->ch_poll_idx;
3567 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3568 {
3569 if (in_part->ch_buf_append)
3570 {
3571 if (in_part->ch_buffer != NULL)
3572 channel_write_new_lines(in_part->ch_buffer);
3573 }
3574 else
3575 channel_write_in(channel);
3576 --ret;
3577 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003578 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003579
3580 return ret;
3581}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003582# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003583
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003584# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003585/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003586 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003587 */
3588 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003589channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003590{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003591 int maxfd = maxfd_in;
3592 channel_T *channel;
3593 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003594 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003595 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003596
Bram Moolenaar77073442016-02-13 23:23:53 +01003597 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003598 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003599 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003600 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003601 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003602
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003603 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003604 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003605 FD_SET((int)fd, rfds);
3606 if (maxfd < (int)fd)
3607 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003608 }
3609 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003610 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003611
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003612 maxfd = channel_fill_wfds(maxfd, wfds);
3613
Bram Moolenaare0874f82016-01-24 20:36:41 +01003614 return maxfd;
3615}
3616
3617/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003618 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003619 */
3620 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003621channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003622{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003623 int ret = ret_in;
3624 channel_T *channel;
3625 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003626 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003627 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003628 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003629
Bram Moolenaar77073442016-02-13 23:23:53 +01003630 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003631 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003632 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003633 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003634 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003635
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003636 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003637 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003638 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003639 --ret;
3640 }
3641 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003642
3643 in_part = &channel->ch_part[PART_IN];
3644 if (ret > 0 && in_part->ch_fd != INVALID_FD
3645 && FD_ISSET(in_part->ch_fd, wfds))
3646 {
3647 if (in_part->ch_buf_append)
3648 {
3649 if (in_part->ch_buffer != NULL)
3650 channel_write_new_lines(in_part->ch_buffer);
3651 }
3652 else
3653 channel_write_in(channel);
3654 --ret;
3655 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003656 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003657
3658 return ret;
3659}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003660# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003661
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003662/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003663 * Execute queued up commands.
3664 * Invoked from the main loop when it's safe to execute received commands.
3665 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003666 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003667 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003668channel_parse_messages(void)
3669{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003670 channel_T *channel = first_channel;
3671 int ret = FALSE;
3672 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003673 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003674
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003675 ++safe_to_invoke_callback;
3676
Bram Moolenaard0b65022016-03-06 21:50:33 +01003677 /* Only do this message when another message was given, otherwise we get
3678 * lots of them. */
3679 if (did_log_msg)
3680 {
3681 ch_log(NULL, "looking for messages on channels");
3682 did_log_msg = FALSE;
3683 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003684 while (channel != NULL)
3685 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003686 if (channel->ch_to_be_closed)
3687 {
3688 channel->ch_to_be_closed = FALSE;
3689 channel_close_now(channel);
3690 /* channel may have been freed, start over */
3691 channel = first_channel;
3692 continue;
3693 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003694 if (channel->ch_to_be_freed)
3695 {
3696 channel_free(channel);
3697 /* channel has been freed, start over */
3698 channel = first_channel;
3699 continue;
3700 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003701 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003702 {
3703 /* channel is no longer useful, free it */
3704 channel_free(channel);
3705 channel = first_channel;
3706 part = PART_SOCK;
3707 continue;
3708 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003709 if (channel->ch_part[part].ch_fd != INVALID_FD
3710 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003711 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003712 /* Increase the refcount, in case the handler causes the channel
3713 * to be unreferenced or closed. */
3714 ++channel->ch_refcount;
3715 r = may_invoke_callback(channel, part);
3716 if (r == OK)
3717 ret = TRUE;
3718 if (channel_unref(channel) || r == OK)
3719 {
3720 /* channel was freed or something was done, start over */
3721 channel = first_channel;
3722 part = PART_SOCK;
3723 continue;
3724 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003725 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003726 if (part < PART_ERR)
3727 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003728 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003729 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003730 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003731 part = PART_SOCK;
3732 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003733 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003734
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003735 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003736 {
3737 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003738 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003739 }
3740
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003741 --safe_to_invoke_callback;
3742
Bram Moolenaard7ece102016-02-02 23:23:02 +01003743 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003744}
3745
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003746/*
3747 * Mark references to lists used in channels.
3748 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003749 int
3750set_ref_in_channel(int copyID)
3751{
Bram Moolenaar77073442016-02-13 23:23:53 +01003752 int abort = FALSE;
3753 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003754 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003755
Bram Moolenaar77073442016-02-13 23:23:53 +01003756 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003757 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003758 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003759 tv.v_type = VAR_CHANNEL;
3760 tv.vval.v_channel = channel;
3761 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003762 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003763 return abort;
3764}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003765
3766/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003767 * Return the "part" to write to for "channel".
3768 */
3769 int
3770channel_part_send(channel_T *channel)
3771{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003772 if (channel->CH_SOCK_FD == INVALID_FD)
3773 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003774 return PART_SOCK;
3775}
3776
3777/*
3778 * Return the default "part" to read from for "channel".
3779 */
3780 int
3781channel_part_read(channel_T *channel)
3782{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003783 if (channel->CH_SOCK_FD == INVALID_FD)
3784 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003785 return PART_SOCK;
3786}
3787
3788/*
3789 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003790 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003791 */
3792 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003793channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003794{
Bram Moolenaar77073442016-02-13 23:23:53 +01003795 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003796 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003797 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003798}
3799
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003800/*
3801 * Return the timeout of "channel"/"part"
3802 */
3803 int
3804channel_get_timeout(channel_T *channel, int part)
3805{
3806 return channel->ch_part[part].ch_timeout;
3807}
3808
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003809 static int
3810handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3811{
3812 char_u *val = get_tv_string(item);
3813
3814 opt->jo_set |= jo;
3815 if (STRCMP(val, "nl") == 0)
3816 *modep = MODE_NL;
3817 else if (STRCMP(val, "raw") == 0)
3818 *modep = MODE_RAW;
3819 else if (STRCMP(val, "js") == 0)
3820 *modep = MODE_JS;
3821 else if (STRCMP(val, "json") == 0)
3822 *modep = MODE_JSON;
3823 else
3824 {
3825 EMSG2(_(e_invarg2), val);
3826 return FAIL;
3827 }
3828 return OK;
3829}
3830
3831 static int
3832handle_io(typval_T *item, int part, jobopt_T *opt)
3833{
3834 char_u *val = get_tv_string(item);
3835
3836 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3837 if (STRCMP(val, "null") == 0)
3838 opt->jo_io[part] = JIO_NULL;
3839 else if (STRCMP(val, "pipe") == 0)
3840 opt->jo_io[part] = JIO_PIPE;
3841 else if (STRCMP(val, "file") == 0)
3842 opt->jo_io[part] = JIO_FILE;
3843 else if (STRCMP(val, "buffer") == 0)
3844 opt->jo_io[part] = JIO_BUFFER;
3845 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3846 opt->jo_io[part] = JIO_OUT;
3847 else
3848 {
3849 EMSG2(_(e_invarg2), val);
3850 return FAIL;
3851 }
3852 return OK;
3853}
3854
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003855/*
3856 * Clear a jobopt_T before using it.
3857 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003858 void
3859clear_job_options(jobopt_T *opt)
3860{
3861 vim_memset(opt, 0, sizeof(jobopt_T));
3862}
3863
3864/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003865 * Free any members of a jobopt_T.
3866 */
3867 void
3868free_job_options(jobopt_T *opt)
3869{
3870 if (opt->jo_partial != NULL)
3871 partial_unref(opt->jo_partial);
3872 if (opt->jo_out_partial != NULL)
3873 partial_unref(opt->jo_out_partial);
3874 if (opt->jo_err_partial != NULL)
3875 partial_unref(opt->jo_err_partial);
3876 if (opt->jo_close_partial != NULL)
3877 partial_unref(opt->jo_close_partial);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003878 if (opt->jo_exit_partial != NULL)
3879 partial_unref(opt->jo_exit_partial);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003880}
3881
3882/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003883 * Get the PART_ number from the first character of an option name.
3884 */
3885 static int
3886part_from_char(int c)
3887{
3888 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3889}
3890
3891/*
3892 * Get the option entries from the dict in "tv", parse them and put the result
3893 * in "opt".
3894 * Only accept options in "supported".
3895 * If an option value is invalid return FAIL.
3896 */
3897 int
3898get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3899{
3900 typval_T *item;
3901 char_u *val;
3902 dict_T *dict;
3903 int todo;
3904 hashitem_T *hi;
3905 int part;
3906
3907 opt->jo_set = 0;
3908 if (tv->v_type == VAR_UNKNOWN)
3909 return OK;
3910 if (tv->v_type != VAR_DICT)
3911 {
3912 EMSG(_(e_invarg));
3913 return FAIL;
3914 }
3915 dict = tv->vval.v_dict;
3916 if (dict == NULL)
3917 return OK;
3918
3919 todo = (int)dict->dv_hashtab.ht_used;
3920 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3921 if (!HASHITEM_EMPTY(hi))
3922 {
3923 item = &dict_lookup(hi)->di_tv;
3924
3925 if (STRCMP(hi->hi_key, "mode") == 0)
3926 {
3927 if (!(supported & JO_MODE))
3928 break;
3929 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3930 return FAIL;
3931 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003932 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003933 {
3934 if (!(supported & JO_IN_MODE))
3935 break;
3936 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3937 == FAIL)
3938 return FAIL;
3939 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003940 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003941 {
3942 if (!(supported & JO_OUT_MODE))
3943 break;
3944 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3945 == FAIL)
3946 return FAIL;
3947 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003948 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003949 {
3950 if (!(supported & JO_ERR_MODE))
3951 break;
3952 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3953 == FAIL)
3954 return FAIL;
3955 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003956 else if (STRCMP(hi->hi_key, "in_io") == 0
3957 || STRCMP(hi->hi_key, "out_io") == 0
3958 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003959 {
3960 if (!(supported & JO_OUT_IO))
3961 break;
3962 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3963 return FAIL;
3964 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003965 else if (STRCMP(hi->hi_key, "in_name") == 0
3966 || STRCMP(hi->hi_key, "out_name") == 0
3967 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003968 {
3969 part = part_from_char(*hi->hi_key);
3970
3971 if (!(supported & JO_OUT_IO))
3972 break;
3973 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3974 opt->jo_io_name[part] =
3975 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3976 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003977 else if (STRCMP(hi->hi_key, "in_buf") == 0
3978 || STRCMP(hi->hi_key, "out_buf") == 0
3979 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003980 {
3981 part = part_from_char(*hi->hi_key);
3982
3983 if (!(supported & JO_OUT_IO))
3984 break;
3985 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3986 opt->jo_io_buf[part] = get_tv_number(item);
3987 if (opt->jo_io_buf[part] <= 0)
3988 {
3989 EMSG2(_(e_invarg2), get_tv_string(item));
3990 return FAIL;
3991 }
3992 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3993 {
3994 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3995 return FAIL;
3996 }
3997 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02003998 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
3999 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4000 {
4001 part = part_from_char(*hi->hi_key);
4002
4003 if (!(supported & JO_OUT_IO))
4004 break;
4005 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4006 opt->jo_modifiable[part] = get_tv_number(item);
4007 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004008 else if (STRCMP(hi->hi_key, "in_top") == 0
4009 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004010 {
4011 linenr_T *lp;
4012
4013 if (!(supported & JO_OUT_IO))
4014 break;
4015 if (hi->hi_key[3] == 't')
4016 {
4017 lp = &opt->jo_in_top;
4018 opt->jo_set |= JO_IN_TOP;
4019 }
4020 else
4021 {
4022 lp = &opt->jo_in_bot;
4023 opt->jo_set |= JO_IN_BOT;
4024 }
4025 *lp = get_tv_number(item);
4026 if (*lp < 0)
4027 {
4028 EMSG2(_(e_invarg2), get_tv_string(item));
4029 return FAIL;
4030 }
4031 }
4032 else if (STRCMP(hi->hi_key, "channel") == 0)
4033 {
4034 if (!(supported & JO_OUT_IO))
4035 break;
4036 opt->jo_set |= JO_CHANNEL;
4037 if (item->v_type != VAR_CHANNEL)
4038 {
4039 EMSG2(_(e_invarg2), "channel");
4040 return FAIL;
4041 }
4042 opt->jo_channel = item->vval.v_channel;
4043 }
4044 else if (STRCMP(hi->hi_key, "callback") == 0)
4045 {
4046 if (!(supported & JO_CALLBACK))
4047 break;
4048 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004049 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004050 if (opt->jo_callback == NULL)
4051 {
4052 EMSG2(_(e_invarg2), "callback");
4053 return FAIL;
4054 }
4055 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004056 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004057 {
4058 if (!(supported & JO_OUT_CALLBACK))
4059 break;
4060 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004061 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062 if (opt->jo_out_cb == NULL)
4063 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004064 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004065 return FAIL;
4066 }
4067 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004068 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004069 {
4070 if (!(supported & JO_ERR_CALLBACK))
4071 break;
4072 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004073 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004074 if (opt->jo_err_cb == NULL)
4075 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004076 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004077 return FAIL;
4078 }
4079 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004080 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004081 {
4082 if (!(supported & JO_CLOSE_CALLBACK))
4083 break;
4084 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004085 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004086 if (opt->jo_close_cb == NULL)
4087 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004088 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004089 return FAIL;
4090 }
4091 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004092 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4093 {
4094 if (!(supported & JO_EXIT_CB))
4095 break;
4096 opt->jo_set |= JO_EXIT_CB;
4097 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4098 if (opt->jo_exit_cb == NULL)
4099 {
4100 EMSG2(_(e_invarg2), "exit_cb");
4101 return FAIL;
4102 }
4103 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004104 else if (STRCMP(hi->hi_key, "waittime") == 0)
4105 {
4106 if (!(supported & JO_WAITTIME))
4107 break;
4108 opt->jo_set |= JO_WAITTIME;
4109 opt->jo_waittime = get_tv_number(item);
4110 }
4111 else if (STRCMP(hi->hi_key, "timeout") == 0)
4112 {
4113 if (!(supported & JO_TIMEOUT))
4114 break;
4115 opt->jo_set |= JO_TIMEOUT;
4116 opt->jo_timeout = get_tv_number(item);
4117 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004118 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004119 {
4120 if (!(supported & JO_OUT_TIMEOUT))
4121 break;
4122 opt->jo_set |= JO_OUT_TIMEOUT;
4123 opt->jo_out_timeout = get_tv_number(item);
4124 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004125 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004126 {
4127 if (!(supported & JO_ERR_TIMEOUT))
4128 break;
4129 opt->jo_set |= JO_ERR_TIMEOUT;
4130 opt->jo_err_timeout = get_tv_number(item);
4131 }
4132 else if (STRCMP(hi->hi_key, "part") == 0)
4133 {
4134 if (!(supported & JO_PART))
4135 break;
4136 opt->jo_set |= JO_PART;
4137 val = get_tv_string(item);
4138 if (STRCMP(val, "err") == 0)
4139 opt->jo_part = PART_ERR;
4140 else
4141 {
4142 EMSG2(_(e_invarg2), val);
4143 return FAIL;
4144 }
4145 }
4146 else if (STRCMP(hi->hi_key, "id") == 0)
4147 {
4148 if (!(supported & JO_ID))
4149 break;
4150 opt->jo_set |= JO_ID;
4151 opt->jo_id = get_tv_number(item);
4152 }
4153 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4154 {
4155 if (!(supported & JO_STOPONEXIT))
4156 break;
4157 opt->jo_set |= JO_STOPONEXIT;
4158 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4159 opt->jo_soe_buf);
4160 if (opt->jo_stoponexit == NULL)
4161 {
4162 EMSG2(_(e_invarg2), "stoponexit");
4163 return FAIL;
4164 }
4165 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004166 else if (STRCMP(hi->hi_key, "block_write") == 0)
4167 {
4168 if (!(supported & JO_BLOCK_WRITE))
4169 break;
4170 opt->jo_set |= JO_BLOCK_WRITE;
4171 opt->jo_block_write = get_tv_number(item);
4172 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004173 else
4174 break;
4175 --todo;
4176 }
4177 if (todo > 0)
4178 {
4179 EMSG2(_(e_invarg2), hi->hi_key);
4180 return FAIL;
4181 }
4182
4183 return OK;
4184}
4185
4186/*
4187 * Get the channel from the argument.
4188 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004189 * When "check_open" is TRUE check that the channel can be used.
4190 * When "reading" is TRUE "check_open" considers typeahead useful.
4191 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004192 */
4193 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004194get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004195{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004196 channel_T *channel = NULL;
4197 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004198
4199 if (tv->v_type == VAR_JOB)
4200 {
4201 if (tv->vval.v_job != NULL)
4202 channel = tv->vval.v_job->jv_channel;
4203 }
4204 else if (tv->v_type == VAR_CHANNEL)
4205 {
4206 channel = tv->vval.v_channel;
4207 }
4208 else
4209 {
4210 EMSG2(_(e_invarg2), get_tv_string(tv));
4211 return NULL;
4212 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004213 if (channel != NULL && reading)
4214 has_readahead = channel_has_readahead(channel,
4215 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004216
Bram Moolenaar437905c2016-04-26 19:01:05 +02004217 if (check_open && (channel == NULL || (!channel_is_open(channel)
4218 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004219 {
4220 EMSG(_("E906: not an open channel"));
4221 return NULL;
4222 }
4223 return channel;
4224}
4225
4226static job_T *first_job = NULL;
4227
4228 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004229job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004230{
4231 ch_log(job->jv_channel, "Freeing job");
4232 if (job->jv_channel != NULL)
4233 {
4234 /* The link from the channel to the job doesn't count as a reference,
4235 * thus don't decrement the refcount of the job. The reference from
4236 * the job to the channel does count the refrence, decrement it and
4237 * NULL the reference. We don't set ch_job_killed, unreferencing the
4238 * job doesn't mean it stops running. */
4239 job->jv_channel->ch_job = NULL;
4240 channel_unref(job->jv_channel);
4241 }
4242 mch_clear_job(job);
4243
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004244 vim_free(job->jv_stoponexit);
4245 vim_free(job->jv_exit_cb);
4246 partial_unref(job->jv_exit_partial);
4247}
4248
4249 static void
4250job_free_job(job_T *job)
4251{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004252 if (job->jv_next != NULL)
4253 job->jv_next->jv_prev = job->jv_prev;
4254 if (job->jv_prev == NULL)
4255 first_job = job->jv_next;
4256 else
4257 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004258 vim_free(job);
4259}
4260
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004261 static void
4262job_free(job_T *job)
4263{
4264 if (!in_free_unref_items)
4265 {
4266 job_free_contents(job);
4267 job_free_job(job);
4268 }
4269}
4270
Bram Moolenaar655da312016-05-28 22:22:34 +02004271#if defined(EXITFREE) || defined(PROTO)
4272 void
4273job_free_all(void)
4274{
4275 while (first_job != NULL)
4276 job_free(first_job);
4277}
4278#endif
4279
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004280/*
4281 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004282 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4283 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004284 */
4285 static int
4286job_still_useful(job_T *job)
4287{
4288 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004289 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4290 || (job->jv_channel != NULL
4291 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004292}
4293
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004294/*
4295 * Mark references in jobs that are still useful.
4296 */
4297 int
4298set_ref_in_job(int copyID)
4299{
4300 int abort = FALSE;
4301 job_T *job;
4302 typval_T tv;
4303
4304 for (job = first_job; job != NULL; job = job->jv_next)
4305 if (job_still_useful(job))
4306 {
4307 tv.v_type = VAR_JOB;
4308 tv.vval.v_job = job;
4309 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4310 }
4311 return abort;
4312}
4313
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004314 void
4315job_unref(job_T *job)
4316{
4317 if (job != NULL && --job->jv_refcount <= 0)
4318 {
4319 /* Do not free the job when it has not ended yet and there is a
4320 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004321 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004322 {
4323 job_free(job);
4324 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004325 else if (job->jv_channel != NULL
4326 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004327 {
4328 /* Do remove the link to the channel, otherwise it hangs
4329 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004330 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004331 job->jv_channel->ch_job = NULL;
4332 channel_unref(job->jv_channel);
4333 job->jv_channel = NULL;
4334 }
4335 }
4336}
4337
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004338 int
4339free_unused_jobs_contents(int copyID, int mask)
4340{
4341 int did_free = FALSE;
4342 job_T *job;
4343
4344 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004345 if ((job->jv_copyID & mask) != (copyID & mask)
4346 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004347 {
4348 /* Free the channel and ordinary items it contains, but don't
4349 * recurse into Lists, Dictionaries etc. */
4350 job_free_contents(job);
4351 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004352 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004353 return did_free;
4354}
4355
4356 void
4357free_unused_jobs(int copyID, int mask)
4358{
4359 job_T *job;
4360 job_T *job_next;
4361
4362 for (job = first_job; job != NULL; job = job_next)
4363 {
4364 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004365 if ((job->jv_copyID & mask) != (copyID & mask)
4366 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004367 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004368 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004369 job_free_job(job);
4370 }
4371 }
4372}
4373
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004374/*
4375 * Allocate a job. Sets the refcount to one and sets options default.
4376 */
4377 static job_T *
4378job_alloc(void)
4379{
4380 job_T *job;
4381
4382 job = (job_T *)alloc_clear(sizeof(job_T));
4383 if (job != NULL)
4384 {
4385 job->jv_refcount = 1;
4386 job->jv_stoponexit = vim_strsave((char_u *)"term");
4387
4388 if (first_job != NULL)
4389 {
4390 first_job->jv_prev = job;
4391 job->jv_next = first_job;
4392 }
4393 first_job = job;
4394 }
4395 return job;
4396}
4397
4398 void
4399job_set_options(job_T *job, jobopt_T *opt)
4400{
4401 if (opt->jo_set & JO_STOPONEXIT)
4402 {
4403 vim_free(job->jv_stoponexit);
4404 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4405 job->jv_stoponexit = NULL;
4406 else
4407 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4408 }
4409 if (opt->jo_set & JO_EXIT_CB)
4410 {
4411 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004412 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004413 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004414 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004415 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004416 job->jv_exit_partial = NULL;
4417 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004418 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004419 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004420 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004421 job->jv_exit_partial = opt->jo_exit_partial;
4422 if (job->jv_exit_partial != NULL)
4423 ++job->jv_exit_partial->pt_refcount;
4424 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004425 }
4426}
4427
4428/*
4429 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4430 */
4431 void
4432job_stop_on_exit()
4433{
4434 job_T *job;
4435
4436 for (job = first_job; job != NULL; job = job->jv_next)
4437 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4438 mch_stop_job(job, job->jv_stoponexit);
4439}
4440
4441/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004442 * Return TRUE when there is any job that might exit, which means
4443 * job_check_ended() should be called once in a while.
4444 */
4445 int
4446has_pending_job()
4447{
4448 job_T *job;
4449
4450 for (job = first_job; job != NULL; job = job->jv_next)
4451 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4452 return TRUE;
4453 return FALSE;
4454}
4455
4456/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004457 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004458 */
4459 void
4460job_check_ended(void)
4461{
4462 static time_t last_check = 0;
4463 time_t now;
4464 job_T *job;
4465 job_T *next;
4466
4467 /* Only do this once in 10 seconds. */
4468 now = time(NULL);
4469 if (last_check + 10 < now)
4470 {
4471 last_check = now;
4472 for (job = first_job; job != NULL; job = next)
4473 {
4474 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004475 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004476 job_status(job); /* may free "job" */
4477 }
4478 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004479 if (channel_need_redraw)
4480 {
4481 channel_need_redraw = FALSE;
4482 redraw_after_callback();
4483 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004484}
4485
4486/*
4487 * "job_start()" function
4488 */
4489 job_T *
4490job_start(typval_T *argvars)
4491{
4492 job_T *job;
4493 char_u *cmd = NULL;
4494#if defined(UNIX)
4495# define USE_ARGV
4496 char **argv = NULL;
4497 int argc = 0;
4498#else
4499 garray_T ga;
4500#endif
4501 jobopt_T opt;
4502 int part;
4503
4504 job = job_alloc();
4505 if (job == NULL)
4506 return NULL;
4507
4508 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004509#ifndef USE_ARGV
4510 ga_init2(&ga, (int)sizeof(char*), 20);
4511#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004512
4513 /* Default mode is NL. */
4514 clear_job_options(&opt);
4515 opt.jo_mode = MODE_NL;
4516 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004517 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4518 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004519 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004520
4521 /* Check that when io is "file" that there is a file name. */
4522 for (part = PART_OUT; part <= PART_IN; ++part)
4523 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4524 && opt.jo_io[part] == JIO_FILE
4525 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4526 || *opt.jo_io_name[part] == NUL))
4527 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004528 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004529 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004530 }
4531
4532 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4533 {
4534 buf_T *buf = NULL;
4535
4536 /* check that we can find the buffer before starting the job */
4537 if (opt.jo_set & JO_IN_BUF)
4538 {
4539 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4540 if (buf == NULL)
4541 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4542 }
4543 else if (!(opt.jo_set & JO_IN_NAME))
4544 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004545 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004546 }
4547 else
4548 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4549 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004550 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 if (buf->b_ml.ml_mfp == NULL)
4552 {
4553 char_u numbuf[NUMBUFLEN];
4554 char_u *s;
4555
4556 if (opt.jo_set & JO_IN_BUF)
4557 {
4558 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4559 s = numbuf;
4560 }
4561 else
4562 s = opt.jo_io_name[PART_IN];
4563 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004564 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004565 }
4566 job->jv_in_buf = buf;
4567 }
4568
4569 job_set_options(job, &opt);
4570
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004571 if (argvars[0].v_type == VAR_STRING)
4572 {
4573 /* Command is a string. */
4574 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004575 if (cmd == NULL || *cmd == NUL)
4576 {
4577 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004578 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004579 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004580#ifdef USE_ARGV
4581 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004582 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004583 argv[argc] = NULL;
4584#endif
4585 }
4586 else if (argvars[0].v_type != VAR_LIST
4587 || argvars[0].vval.v_list == NULL
4588 || argvars[0].vval.v_list->lv_len < 1)
4589 {
4590 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004591 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004592 }
4593 else
4594 {
4595 list_T *l = argvars[0].vval.v_list;
4596 listitem_T *li;
4597 char_u *s;
4598
4599#ifdef USE_ARGV
4600 /* Pass argv[] to mch_call_shell(). */
4601 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4602 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004603 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004604#endif
4605 for (li = l->lv_first; li != NULL; li = li->li_next)
4606 {
4607 s = get_tv_string_chk(&li->li_tv);
4608 if (s == NULL)
4609 goto theend;
4610#ifdef USE_ARGV
4611 argv[argc++] = (char *)s;
4612#else
4613 /* Only escape when needed, double quotes are not always allowed. */
4614 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4615 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004616# ifdef WIN32
4617 int old_ssl = p_ssl;
4618
4619 /* This is using CreateProcess, not cmd.exe. Always use
4620 * double quote and backslashes. */
4621 p_ssl = 0;
4622# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004623 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004624# ifdef WIN32
4625 p_ssl = old_ssl;
4626# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004627 if (s == NULL)
4628 goto theend;
4629 ga_concat(&ga, s);
4630 vim_free(s);
4631 }
4632 else
4633 ga_concat(&ga, s);
4634 if (li->li_next != NULL)
4635 ga_append(&ga, ' ');
4636#endif
4637 }
4638#ifdef USE_ARGV
4639 argv[argc] = NULL;
4640#else
4641 cmd = ga.ga_data;
4642#endif
4643 }
4644
4645#ifdef USE_ARGV
4646 if (ch_log_active())
4647 {
4648 garray_T ga;
4649 int i;
4650
4651 ga_init2(&ga, (int)sizeof(char), 200);
4652 for (i = 0; i < argc; ++i)
4653 {
4654 if (i > 0)
4655 ga_concat(&ga, (char_u *)" ");
4656 ga_concat(&ga, (char_u *)argv[i]);
4657 }
4658 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4659 ga_clear(&ga);
4660 }
4661 mch_start_job(argv, job, &opt);
4662#else
4663 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4664 mch_start_job((char *)cmd, job, &opt);
4665#endif
4666
4667 /* If the channel is reading from a buffer, write lines now. */
4668 if (job->jv_channel != NULL)
4669 channel_write_in(job->jv_channel);
4670
4671theend:
4672#ifdef USE_ARGV
4673 vim_free(argv);
4674#else
4675 vim_free(ga.ga_data);
4676#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004677 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004678 return job;
4679}
4680
4681/*
4682 * Get the status of "job" and invoke the exit callback when needed.
4683 * The returned string is not allocated.
4684 */
4685 char *
4686job_status(job_T *job)
4687{
4688 char *result;
4689
4690 if (job->jv_status == JOB_ENDED)
4691 /* No need to check, dead is dead. */
4692 result = "dead";
4693 else if (job->jv_status == JOB_FAILED)
4694 result = "fail";
4695 else
4696 {
4697 result = mch_job_status(job);
4698 if (job->jv_status == JOB_ENDED)
4699 ch_log(job->jv_channel, "Job ended");
4700 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4701 {
4702 typval_T argv[3];
4703 typval_T rettv;
4704 int dummy;
4705
4706 /* invoke the exit callback; make sure the refcount is > 0 */
4707 ++job->jv_refcount;
4708 argv[0].v_type = VAR_JOB;
4709 argv[0].vval.v_job = job;
4710 argv[1].v_type = VAR_NUMBER;
4711 argv[1].vval.v_number = job->jv_exitval;
4712 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004713 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4714 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004715 clear_tv(&rettv);
4716 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004717 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004718 }
4719 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4720 {
4721 /* The job was already unreferenced, now that it ended it can be
4722 * freed. Careful: caller must not use "job" after this! */
4723 job_free(job);
4724 }
4725 }
4726 return result;
4727}
4728
Bram Moolenaar8950a562016-03-12 15:22:55 +01004729/*
4730 * Implementation of job_info().
4731 */
4732 void
4733job_info(job_T *job, dict_T *dict)
4734{
4735 dictitem_T *item;
4736 varnumber_T nr;
4737
4738 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4739
4740 item = dictitem_alloc((char_u *)"channel");
4741 if (item == NULL)
4742 return;
4743 item->di_tv.v_lock = 0;
4744 item->di_tv.v_type = VAR_CHANNEL;
4745 item->di_tv.vval.v_channel = job->jv_channel;
4746 if (job->jv_channel != NULL)
4747 ++job->jv_channel->ch_refcount;
4748 if (dict_add(dict, item) == FAIL)
4749 dictitem_free(item);
4750
4751#ifdef UNIX
4752 nr = job->jv_pid;
4753#else
4754 nr = job->jv_proc_info.dwProcessId;
4755#endif
4756 dict_add_nr_str(dict, "process", nr, NULL);
4757
4758 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004759 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004760 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4761}
4762
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 int
4764job_stop(job_T *job, typval_T *argvars)
4765{
4766 char_u *arg;
4767
4768 if (argvars[1].v_type == VAR_UNKNOWN)
4769 arg = (char_u *)"";
4770 else
4771 {
4772 arg = get_tv_string_chk(&argvars[1]);
4773 if (arg == NULL)
4774 {
4775 EMSG(_(e_invarg));
4776 return 0;
4777 }
4778 }
4779 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4780 if (mch_stop_job(job, arg) == FAIL)
4781 return 0;
4782
4783 /* Assume that "hup" does not kill the job. */
4784 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4785 job->jv_channel->ch_job_killed = TRUE;
4786
4787 /* We don't try freeing the job, obviously the caller still has a
4788 * reference to it. */
4789 return 1;
4790}
4791
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004792#endif /* FEAT_JOB_CHANNEL */