blob: a5a84dc9c4b63c39b26d51534d88793065465f74 [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)
1082 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001083 if (buf == NULL)
1084 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001085 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001086 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001087 if (buf == NULL)
1088 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001089 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001090 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001091#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001092 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1093 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001094#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001095 if (curbuf->b_ml.ml_mfp == NULL)
1096 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001097 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1098 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 changed_bytes(1, 0);
1100 curbuf = save_curbuf;
1101 }
1102
1103 return buf;
1104}
1105
1106/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001107 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001108 */
1109 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001110channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001111{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 int part;
1113 char_u **cbp;
1114 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001115
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001116 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001117 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 channel->ch_part[part].ch_mode = opt->jo_mode;
1119 if (opt->jo_set & JO_IN_MODE)
1120 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1121 if (opt->jo_set & JO_OUT_MODE)
1122 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1123 if (opt->jo_set & JO_ERR_MODE)
1124 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001125
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001126 if (opt->jo_set & JO_TIMEOUT)
1127 for (part = PART_SOCK; part <= PART_IN; ++part)
1128 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1129 if (opt->jo_set & JO_OUT_TIMEOUT)
1130 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1131 if (opt->jo_set & JO_ERR_TIMEOUT)
1132 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001133 if (opt->jo_set & JO_BLOCK_WRITE)
1134 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001135
1136 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001137 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001138 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001139 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001141 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1143 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001144 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001145 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001146 *pp = opt->jo_partial;
1147 if (*pp != NULL)
1148 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 }
1150 if (opt->jo_set & JO_OUT_CALLBACK)
1151 {
1152 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001153 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001154 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001155 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001156 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1157 *cbp = vim_strsave(opt->jo_out_cb);
1158 else
1159 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 *pp = opt->jo_out_partial;
1161 if (*pp != NULL)
1162 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001163 }
1164 if (opt->jo_set & JO_ERR_CALLBACK)
1165 {
1166 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001167 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001168 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001169 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001170 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1171 *cbp = vim_strsave(opt->jo_err_cb);
1172 else
1173 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001174 *pp = opt->jo_err_partial;
1175 if (*pp != NULL)
1176 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001177 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001178 if (opt->jo_set & JO_CLOSE_CALLBACK)
1179 {
1180 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001181 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001182 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001183 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001184 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1185 *cbp = vim_strsave(opt->jo_close_cb);
1186 else
1187 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001188 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001189 if (*pp != NULL)
1190 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001191 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001192
1193 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1194 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001195 buf_T *buf;
1196
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001197 /* writing output to a buffer. Default mode is NL. */
1198 if (!(opt->jo_set & JO_OUT_MODE))
1199 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001200 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001201 {
1202 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1203 if (buf == NULL)
1204 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1205 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001206 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001207 {
1208 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1209 }
1210 if (buf != NULL)
1211 {
1212 ch_logs(channel, "writing out to buffer '%s'",
1213 (char *)buf->b_ffname);
1214 channel->ch_part[PART_OUT].ch_buffer = buf;
1215 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001216 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001217
1218 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1219 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1220 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1221 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 buf_T *buf;
1223
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001224 /* writing err to a buffer. Default mode is NL. */
1225 if (!(opt->jo_set & JO_ERR_MODE))
1226 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1227 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001228 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001229 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001230 {
1231 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1232 if (buf == NULL)
1233 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1234 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001235 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001236 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1237 if (buf != NULL)
1238 {
1239 ch_logs(channel, "writing err to buffer '%s'",
1240 (char *)buf->b_ffname);
1241 channel->ch_part[PART_ERR].ch_buffer = buf;
1242 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001243 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001244
1245 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1246 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1247 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001248}
1249
1250/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001251 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001252 */
1253 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001254channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001255 channel_T *channel,
1256 int part,
1257 char_u *callback,
1258 partial_T *partial,
1259 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001260{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001261 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001262 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1263
1264 if (item != NULL)
1265 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001266 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001267 item->cq_partial = partial;
1268 if (partial != NULL)
1269 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001270 item->cq_seq_nr = id;
1271 item->cq_prev = head->cq_prev;
1272 head->cq_prev = item;
1273 item->cq_next = NULL;
1274 if (item->cq_prev == NULL)
1275 head->cq_next = item;
1276 else
1277 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001278 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001279}
1280
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001281 static void
1282write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1283{
1284 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001285 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001286 char_u *p;
1287
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001288 if ((p = alloc(len + 2)) == NULL)
1289 return;
1290 STRCPY(p, line);
1291 p[len] = NL;
1292 p[len + 1] = NUL;
1293 channel_send(channel, PART_IN, p, "write_buf_line()");
1294 vim_free(p);
1295}
1296
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001297/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001298 * Return TRUE if "channel" can be written to.
1299 * Returns FALSE if the input is closed or the write would block.
1300 */
1301 static int
1302can_write_buf_line(channel_T *channel)
1303{
1304 chanpart_T *in_part = &channel->ch_part[PART_IN];
1305
1306 if (in_part->ch_fd == INVALID_FD)
1307 return FALSE; /* pipe was closed */
1308
1309 /* for testing: block every other attempt to write */
1310 if (in_part->ch_block_write == 1)
1311 in_part->ch_block_write = -1;
1312 else if (in_part->ch_block_write == -1)
1313 in_part->ch_block_write = 1;
1314
1315 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1316#ifndef WIN32
1317 {
1318# if defined(HAVE_SELECT)
1319 struct timeval tval;
1320 fd_set wfds;
1321 int ret;
1322
1323 FD_ZERO(&wfds);
1324 FD_SET((int)in_part->ch_fd, &wfds);
1325 tval.tv_sec = 0;
1326 tval.tv_usec = 0;
1327 for (;;)
1328 {
1329 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1330# ifdef EINTR
1331 SOCK_ERRNO;
1332 if (ret == -1 && errno == EINTR)
1333 continue;
1334# endif
1335 if (ret <= 0 || in_part->ch_block_write == 1)
1336 {
1337 if (ret > 0)
1338 ch_log(channel, "FAKED Input not ready for writing");
1339 else
1340 ch_log(channel, "Input not ready for writing");
1341 return FALSE;
1342 }
1343 break;
1344 }
1345# else
1346 struct pollfd fds;
1347
1348 fds.fd = in_part->ch_fd;
1349 fds.events = POLLOUT;
1350 if (poll(&fds, 1, 0) <= 0)
1351 {
1352 ch_log(channel, "Input not ready for writing");
1353 return FALSE;
1354 }
1355 if (in_part->ch_block_write == 1)
1356 {
1357 ch_log(channel, "FAKED Input not ready for writing");
1358 return FALSE;
1359 }
1360# endif
1361 }
1362#endif
1363 return TRUE;
1364}
1365
1366/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001367 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001368 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001369 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001370channel_write_in(channel_T *channel)
1371{
1372 chanpart_T *in_part = &channel->ch_part[PART_IN];
1373 linenr_T lnum;
1374 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001376
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001377 if (buf == NULL || in_part->ch_buf_append)
1378 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001379 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1380 {
1381 /* buffer was wiped out or unloaded */
1382 in_part->ch_buffer = NULL;
1383 return;
1384 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001385
1386 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1387 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1388 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001389 if (!can_write_buf_line(channel))
1390 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 write_buf_line(buf, lnum, channel);
1392 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001393 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001394
1395 if (written == 1)
1396 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1397 else if (written > 1)
1398 ch_logn(channel, "written %d lines to channel", written);
1399
Bram Moolenaar014069a2016-03-03 22:51:40 +01001400 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001401 if (lnum > buf->b_ml.ml_line_count)
1402 {
1403 /* Writing is done, no longer need the buffer. */
1404 in_part->ch_buffer = NULL;
1405 ch_log(channel, "Finished writing all lines to channel");
1406 }
1407 else
1408 ch_logn(channel, "Still %d more lines to write",
1409 buf->b_ml.ml_line_count - lnum + 1);
1410}
1411
1412/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001413 * Handle buffer "buf" beeing freed, remove it from any channels.
1414 */
1415 void
1416channel_buffer_free(buf_T *buf)
1417{
1418 channel_T *channel;
1419 int part;
1420
1421 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1422 for (part = PART_SOCK; part <= PART_IN; ++part)
1423 {
1424 chanpart_T *ch_part = &channel->ch_part[part];
1425
1426 if (ch_part->ch_buffer == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001427 {
1428 ch_logs(channel, "%s buffer has been wiped out",
1429 part_names[part]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001430 ch_part->ch_buffer = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001431 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001432 }
1433}
1434
1435/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001436 * Write any lines waiting to be written to a channel.
1437 */
1438 void
1439channel_write_any_lines()
1440{
1441 channel_T *channel;
1442
1443 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1444 {
1445 chanpart_T *in_part = &channel->ch_part[PART_IN];
1446
1447 if (in_part->ch_buffer != NULL)
1448 {
1449 if (in_part->ch_buf_append)
1450 channel_write_new_lines(in_part->ch_buffer);
1451 else
1452 channel_write_in(channel);
1453 }
1454 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001455}
1456
1457/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001458 * Write appended lines above the last one in "buf" to the channel.
1459 */
1460 void
1461channel_write_new_lines(buf_T *buf)
1462{
1463 channel_T *channel;
1464 int found_one = FALSE;
1465
1466 /* There could be more than one channel for the buffer, loop over all of
1467 * them. */
1468 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1469 {
1470 chanpart_T *in_part = &channel->ch_part[PART_IN];
1471 linenr_T lnum;
1472 int written = 0;
1473
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001474 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001475 {
1476 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001477 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001478 found_one = TRUE;
1479 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1480 ++lnum)
1481 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001482 if (!can_write_buf_line(channel))
1483 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001484 write_buf_line(buf, lnum, channel);
1485 ++written;
1486 }
1487
1488 if (written == 1)
1489 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1490 else if (written > 1)
1491 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001492 if (lnum < buf->b_ml.ml_line_count)
1493 ch_logn(channel, "Still %d more lines to write",
1494 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001495
1496 in_part->ch_buf_bot = lnum;
1497 }
1498 }
1499 if (!found_one)
1500 buf->b_write_to_channel = FALSE;
1501}
1502
1503/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001504 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001505 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001506 */
1507 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001508invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1509 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001510{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001511 typval_T rettv;
1512 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001513
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001514 if (safe_to_invoke_callback == 0)
1515 EMSG("INTERNAL: Invoking callback when it is not safe");
1516
Bram Moolenaar77073442016-02-13 23:23:53 +01001517 argv[0].v_type = VAR_CHANNEL;
1518 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001519
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001520 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001521 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001522 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001523 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001524}
1525
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001526/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001527 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001528 * The caller must free it.
1529 * Returns NULL if there is nothing.
1530 */
1531 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001532channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001534 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001536 char_u *p;
1537
Bram Moolenaar77073442016-02-13 23:23:53 +01001538 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001540 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001541 p = node->rq_buffer;
1542 head->rq_next = node->rq_next;
1543 if (node->rq_next == NULL)
1544 head->rq_prev = NULL;
1545 else
1546 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001547 vim_free(node);
1548 return p;
1549}
1550
1551/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001552 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001553 */
1554 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001555channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001556{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001557 readq_T *head = &channel->ch_part[part].ch_head;
1558 readq_T *node = head->rq_next;
1559 long_u len = 1;
1560 char_u *res;
1561 char_u *p;
1562
1563 /* If there is only one buffer just get that one. */
1564 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1565 return channel_get(channel, part);
1566
1567 /* Concatenate everything into one buffer. */
1568 for (node = head->rq_next; node != NULL; node = node->rq_next)
1569 len += (long_u)STRLEN(node->rq_buffer);
1570 res = lalloc(len, TRUE);
1571 if (res == NULL)
1572 return NULL;
1573 *res = NUL;
1574 for (node = head->rq_next; node != NULL; node = node->rq_next)
1575 STRCAT(res, node->rq_buffer);
1576
1577 /* Free all buffers */
1578 do
1579 {
1580 p = channel_get(channel, part);
1581 vim_free(p);
1582 } while (p != NULL);
1583
1584 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001585}
1586
1587/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001588 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001589 * Returns FAIL if that is not possible.
1590 */
1591 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001593{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001594 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001595 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001596 char_u *p;
1597
Bram Moolenaar77073442016-02-13 23:23:53 +01001598 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001599 return FAIL;
1600
Bram Moolenaar77073442016-02-13 23:23:53 +01001601 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1602 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001603 if (p == NULL)
1604 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001605 STRCPY(p, node->rq_buffer);
1606 STRCAT(p, node->rq_next->rq_buffer);
1607 vim_free(node->rq_next->rq_buffer);
1608 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001609
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 /* dispose of the node and its buffer */
1611 head->rq_next = node->rq_next;
1612 head->rq_next->rq_prev = NULL;
1613 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001614 vim_free(node);
1615 return OK;
1616}
1617
1618/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001619 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001620 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001621 * Returns OK or FAIL.
1622 */
1623 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001624channel_save(channel_T *channel, int part, char_u *buf, int len,
1625 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001626{
1627 readq_T *node;
1628 readq_T *head = &channel->ch_part[part].ch_head;
1629 char_u *p;
1630 int i;
1631
1632 node = (readq_T *)alloc(sizeof(readq_T));
1633 if (node == NULL)
1634 return FAIL; /* out of memory */
1635 node->rq_buffer = alloc(len + 1);
1636 if (node->rq_buffer == NULL)
1637 {
1638 vim_free(node);
1639 return FAIL; /* out of memory */
1640 }
1641
1642 if (channel->ch_part[part].ch_mode == MODE_NL)
1643 {
1644 /* Drop any CR before a NL. */
1645 p = node->rq_buffer;
1646 for (i = 0; i < len; ++i)
1647 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1648 *p++ = buf[i];
1649 *p = NUL;
1650 }
1651 else
1652 {
1653 mch_memmove(node->rq_buffer, buf, len);
1654 node->rq_buffer[len] = NUL;
1655 }
1656
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001657 if (prepend)
1658 {
1659 /* preend node to the head of the queue */
1660 node->rq_next = head->rq_next;
1661 node->rq_prev = NULL;
1662 if (head->rq_next == NULL)
1663 head->rq_prev = node;
1664 else
1665 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001666 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001667 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001668 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001669 {
1670 /* append node to the tail of the queue */
1671 node->rq_next = NULL;
1672 node->rq_prev = head->rq_prev;
1673 if (head->rq_prev == NULL)
1674 head->rq_next = node;
1675 else
1676 head->rq_prev->rq_next = node;
1677 head->rq_prev = node;
1678 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001679
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001680 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001681 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001682 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001683 fprintf(log_fd, "'");
1684 if (fwrite(buf, len, 1, log_fd) != 1)
1685 return FAIL;
1686 fprintf(log_fd, "'\n");
1687 }
1688 return OK;
1689}
1690
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001691 static int
1692channel_fill(js_read_T *reader)
1693{
1694 channel_T *channel = (channel_T *)reader->js_cookie;
1695 int part = reader->js_cookie_arg;
1696 char_u *next = channel_get(channel, part);
1697 int unused;
1698 int len;
1699 char_u *p;
1700
1701 if (next == NULL)
1702 return FALSE;
1703
1704 unused = reader->js_end - reader->js_buf - reader->js_used;
1705 if (unused > 0)
1706 {
1707 /* Prepend unused text. */
1708 len = (int)STRLEN(next);
1709 p = alloc(unused + len + 1);
1710 if (p == NULL)
1711 {
1712 vim_free(next);
1713 return FALSE;
1714 }
1715 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1716 mch_memmove(p + unused, next, len + 1);
1717 vim_free(next);
1718 next = p;
1719 }
1720
1721 vim_free(reader->js_buf);
1722 reader->js_buf = next;
1723 reader->js_used = 0;
1724 return TRUE;
1725}
1726
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001730 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001731 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001732 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001733channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001734{
1735 js_read_T reader;
1736 typval_T listtv;
1737 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001738 chanpart_T *chanpart = &channel->ch_part[part];
1739 jsonq_T *head = &chanpart->ch_json_head;
1740 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001741 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001742
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001743 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001744 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001745
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001746 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001747 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001748 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001749 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001750 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001751
1752 /* When a message is incomplete we wait for a short while for more to
1753 * arrive. After the delay drop the input, otherwise a truncated string
1754 * or list will make us hang. */
1755 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001756 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001757 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001758 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001759 /* Only accept the response when it is a list with at least two
1760 * items. */
1761 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001762 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001763 if (listtv.v_type != VAR_LIST)
1764 ch_error(channel, "Did not receive a list, discarding");
1765 else
1766 ch_errorn(channel, "Expected list with two items, got %d",
1767 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001768 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001769 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001770 else
1771 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001772 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1773 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001774 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001775 else
1776 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001777 item->jq_value = alloc_tv();
1778 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001779 {
1780 vim_free(item);
1781 clear_tv(&listtv);
1782 }
1783 else
1784 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001785 *item->jq_value = listtv;
1786 item->jq_prev = head->jq_prev;
1787 head->jq_prev = item;
1788 item->jq_next = NULL;
1789 if (item->jq_prev == NULL)
1790 head->jq_next = item;
1791 else
1792 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794 }
1795 }
1796 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001797
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001798 if (status == OK)
1799 chanpart->ch_waiting = FALSE;
1800 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001801 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001802 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001803 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001804 /* First time encountering incomplete message, set a deadline of
1805 * 100 msec. */
1806 ch_log(channel, "Incomplete message - wait for more");
1807 reader.js_used = 0;
1808 chanpart->ch_waiting = TRUE;
1809#ifdef WIN32
1810 chanpart->ch_deadline = GetTickCount() + 100L;
1811#else
1812 gettimeofday(&chanpart->ch_deadline, NULL);
1813 chanpart->ch_deadline.tv_usec += 100 * 1000;
1814 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1815 {
1816 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1817 ++chanpart->ch_deadline.tv_sec;
1818 }
1819#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001820 }
1821 else
1822 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001823 int timeout;
1824#ifdef WIN32
1825 timeout = GetTickCount() > chanpart->ch_deadline;
1826#else
1827 {
1828 struct timeval now_tv;
1829
1830 gettimeofday(&now_tv, NULL);
1831 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1832 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1833 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1834 }
1835#endif
1836 if (timeout)
1837 {
1838 status = FAIL;
1839 chanpart->ch_waiting = FALSE;
1840 }
1841 else
1842 {
1843 reader.js_used = 0;
1844 ch_log(channel, "still waiting on incomplete message");
1845 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001846 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001847 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001848
1849 if (status == FAIL)
1850 {
1851 ch_error(channel, "Decoding failed - discarding input");
1852 ret = FALSE;
1853 chanpart->ch_waiting = FALSE;
1854 }
1855 else if (reader.js_buf[reader.js_used] != NUL)
1856 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001858 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001859 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1860 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001861 ret = status == MAYBE ? FALSE: TRUE;
1862 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001863 else
1864 ret = FALSE;
1865
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001866 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001867 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001868}
1869
1870/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001871 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001872 */
1873 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001874remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001875{
Bram Moolenaar77073442016-02-13 23:23:53 +01001876 if (node->cq_prev == NULL)
1877 head->cq_next = node->cq_next;
1878 else
1879 node->cq_prev->cq_next = node->cq_next;
1880 if (node->cq_next == NULL)
1881 head->cq_prev = node->cq_prev;
1882 else
1883 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001884}
1885
1886/*
1887 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001888 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001889 */
1890 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001891remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892{
Bram Moolenaar77073442016-02-13 23:23:53 +01001893 if (node->jq_prev == NULL)
1894 head->jq_next = node->jq_next;
1895 else
1896 node->jq_prev->jq_next = node->jq_next;
1897 if (node->jq_next == NULL)
1898 head->jq_prev = node->jq_prev;
1899 else
1900 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001901 vim_free(node);
1902}
1903
1904/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001905 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001906 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001907 * When "id" is zero or negative jut get the first message. But not the one
1908 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001909 * Return OK when found and return the value in "rettv".
1910 * Return FAIL otherwise.
1911 */
1912 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001913channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001914{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001915 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001916 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001917
Bram Moolenaar77073442016-02-13 23:23:53 +01001918 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001919 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001920 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001921 typval_T *tv = &l->lv_first->li_tv;
1922
1923 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001924 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001925 || tv->vval.v_number == 0
1926 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001927 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001928 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001929 if (tv->v_type == VAR_NUMBER)
1930 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001931 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001932 return OK;
1933 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001934 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001935 }
1936 return FAIL;
1937}
1938
Bram Moolenaarece61b02016-02-20 21:39:05 +01001939#define CH_JSON_MAX_ARGS 4
1940
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001941/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001942 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 * "argv[0]" is the command string.
1944 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001945 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001946 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001947channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001948{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001949 char_u *cmd = argv[0].vval.v_string;
1950 char_u *arg;
1951 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001952
Bram Moolenaarece61b02016-02-20 21:39:05 +01001953 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001954 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001955 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001956 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001957 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958 return;
1959 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001960 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001961 if (arg == NULL)
1962 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001963
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001964 if (STRCMP(cmd, "ex") == 0)
1965 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001966 int save_called_emsg = called_emsg;
1967
1968 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001969 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001970 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001971 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001972 --emsg_silent;
1973 if (called_emsg)
1974 ch_logs(channel, "Ex command error: '%s'",
1975 (char *)get_vim_var_str(VV_ERRMSG));
1976 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001977 }
1978 else if (STRCMP(cmd, "normal") == 0)
1979 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001980 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001981
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001982 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001983 ea.arg = arg;
1984 ea.addr_count = 0;
1985 ea.forceit = TRUE; /* no mapping */
1986 ex_normal(&ea);
1987 }
1988 else if (STRCMP(cmd, "redraw") == 0)
1989 {
1990 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001991
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001992 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001993 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001994 ex_redraw(&ea);
1995 showruler(FALSE);
1996 setcursor();
1997 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001998#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001999 if (gui.in_use)
2000 {
2001 gui_update_cursor(FALSE, FALSE);
2002 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002003 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002004#endif
2005 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002006 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002008 int is_call = cmd[0] == 'c';
2009 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002010
Bram Moolenaarece61b02016-02-20 21:39:05 +01002011 if (argv[id_idx].v_type != VAR_UNKNOWN
2012 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002013 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002014 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002015 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002016 EMSG("E904: last argument for expr/call must be a number");
2017 }
2018 else if (is_call && argv[2].v_type != VAR_LIST)
2019 {
2020 ch_error(channel, "third argument for call must be a list");
2021 if (p_verbose > 2)
2022 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002023 }
2024 else
2025 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002026 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002027 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002028 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002029 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002030
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002031 /* Don't pollute the display with errors. */
2032 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002033 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002034 {
2035 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002036 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002037 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002038 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002039 {
2040 ch_logs(channel, "Calling '%s'", (char *)arg);
2041 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2042 tv = &res_tv;
2043 else
2044 tv = NULL;
2045 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002046
2047 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002048 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002049 int id = argv[id_idx].vval.v_number;
2050
Bram Moolenaar55fab432016-02-07 16:53:13 +01002051 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002052 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002053 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002054 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002055 /* If evaluation failed or the result can't be encoded
2056 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002057 vim_free(json);
2058 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002059 err_tv.v_type = VAR_STRING;
2060 err_tv.vval.v_string = (char_u *)"ERROR";
2061 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002062 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002063 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002064 if (json != NULL)
2065 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002066 channel_send(channel,
2067 part == PART_SOCK ? PART_SOCK : PART_IN,
2068 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002069 vim_free(json);
2070 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002071 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002072 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002073 if (tv == &res_tv)
2074 clear_tv(tv);
2075 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002076 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002077 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002078 }
2079 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002080 {
2081 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002082 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002083 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002084}
2085
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002086/*
2087 * Invoke the callback at "cbhead".
2088 * Does not redraw but sets channel_need_redraw.
2089 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002090 static void
2091invoke_one_time_callback(
2092 channel_T *channel,
2093 cbq_T *cbhead,
2094 cbq_T *item,
2095 typval_T *argv)
2096{
2097 ch_logs(channel, "Invoking one-time callback %s",
2098 (char *)item->cq_callback);
2099 /* Remove the item from the list first, if the callback
2100 * invokes ch_close() the list will be cleared. */
2101 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002102 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002103 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002104 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002105 vim_free(item);
2106}
2107
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002108 static void
2109append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2110{
2111 buf_T *save_curbuf = curbuf;
2112 linenr_T lnum = buffer->b_ml.ml_line_count;
2113 int save_write_to = buffer->b_write_to_channel;
2114
2115 /* If the buffer is also used as input insert above the last
2116 * line. Don't write these lines. */
2117 if (save_write_to)
2118 {
2119 --lnum;
2120 buffer->b_write_to_channel = FALSE;
2121 }
2122
2123 /* Append to the buffer */
2124 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2125
2126 curbuf = buffer;
2127 u_sync(TRUE);
2128 /* ignore undo failure, undo is not very useful here */
2129 ignored = u_save(lnum, lnum + 1);
2130
2131 ml_append(lnum, msg, 0, FALSE);
2132 appended_lines_mark(lnum, 1L);
2133 curbuf = save_curbuf;
2134
2135 if (buffer->b_nwindows > 0)
2136 {
2137 win_T *wp;
2138 win_T *save_curwin;
2139
2140 FOR_ALL_WINDOWS(wp)
2141 {
2142 if (wp->w_buffer == buffer
2143 && (save_write_to
2144 ? wp->w_cursor.lnum == lnum + 1
2145 : (wp->w_cursor.lnum == lnum
2146 && wp->w_cursor.col == 0)))
2147 {
2148 ++wp->w_cursor.lnum;
2149 save_curwin = curwin;
2150 curwin = wp;
2151 curbuf = curwin->w_buffer;
2152 scroll_cursor_bot(0, FALSE);
2153 curwin = save_curwin;
2154 curbuf = curwin->w_buffer;
2155 }
2156 }
2157 redraw_buf_later(buffer, VALID);
2158 channel_need_redraw = TRUE;
2159 }
2160
2161 if (save_write_to)
2162 {
2163 channel_T *ch;
2164
2165 /* Find channels reading from this buffer and adjust their
2166 * next-to-read line number. */
2167 buffer->b_write_to_channel = TRUE;
2168 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2169 {
2170 chanpart_T *in_part = &ch->ch_part[PART_IN];
2171
2172 if (in_part->ch_buffer == buffer)
2173 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2174 }
2175 }
2176}
2177
Bram Moolenaar437905c2016-04-26 19:01:05 +02002178 static void
2179drop_messages(channel_T *channel, int part)
2180{
2181 char_u *msg;
2182
2183 while ((msg = channel_get(channel, part)) != NULL)
2184 {
2185 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2186 vim_free(msg);
2187 }
2188}
2189
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002190/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002191 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002192 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002193 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002195 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002196may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002197{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002198 char_u *msg = NULL;
2199 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002201 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002202 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002203 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002204 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002205 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002206 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002207 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002209 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002210 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002211 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002212
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002213 /* Use a message-specific callback, part callback or channel callback */
2214 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2215 if (cbitem->cq_seq_nr == 0)
2216 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002217 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002218 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002219 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002220 partial = cbitem->cq_partial;
2221 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002222 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002223 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002224 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002225 partial = channel->ch_part[part].ch_partial;
2226 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002227 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002228 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002229 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002230 partial = channel->ch_partial;
2231 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002232
Bram Moolenaar187db502016-02-27 14:44:26 +01002233 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002234 if (buffer != NULL && !buf_valid(buffer))
2235 {
2236 /* buffer was wiped out */
2237 channel->ch_part[part].ch_buffer = NULL;
2238 buffer = NULL;
2239 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002240
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002241 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002242 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 listitem_T *item;
2244 int argc = 0;
2245
Bram Moolenaard7ece102016-02-02 23:23:02 +01002246 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002247 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002248 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002249 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002250 channel_parse_json(channel, part);
2251 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002252 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002253 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002254
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 for (item = listtv->vval.v_list->lv_first;
2256 item != NULL && argc < CH_JSON_MAX_ARGS;
2257 item = item->li_next)
2258 argv[argc++] = item->li_tv;
2259 while (argc < CH_JSON_MAX_ARGS)
2260 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002261
Bram Moolenaarece61b02016-02-20 21:39:05 +01002262 if (argv[0].v_type == VAR_STRING)
2263 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002265 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002266 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002267 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002268 }
2269
Bram Moolenaarece61b02016-02-20 21:39:05 +01002270 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002271 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002272 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002273 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002274 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002275 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002276 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002278 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002279 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002280 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002281 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002282 return FALSE;
2283 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002284 else
2285 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002286 /* If there is no callback or buffer drop the message. */
2287 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002288 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002289 /* If there is a close callback it may use ch_read() to get the
2290 * messages. */
2291 if (channel->ch_close_cb == NULL)
2292 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002293 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002294 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002295
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002296 if (ch_mode == MODE_NL)
2297 {
2298 char_u *nl;
2299 char_u *buf;
2300
2301 /* See if we have a message ending in NL in the first buffer. If
2302 * not try to concatenate the first and the second buffer. */
2303 while (TRUE)
2304 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002305 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002306 nl = vim_strchr(buf, NL);
2307 if (nl != NULL)
2308 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002309 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002310 return FALSE; /* incomplete message */
2311 }
2312 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002313 {
2314 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002315 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002316 *nl = NUL;
2317 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002318 else
2319 {
2320 /* Copy the message into allocated memory and remove it from
2321 * the buffer. */
2322 msg = vim_strnsave(buf, (int)(nl - buf));
2323 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2324 }
2325 }
2326 else
2327 /* For a raw channel we don't know where the message ends, just
2328 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002329 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002330
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002331 if (msg == NULL)
2332 return FALSE; /* out of memory (and avoids Coverity warning) */
2333
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002334 argv[1].v_type = VAR_STRING;
2335 argv[1].vval.v_string = msg;
2336 }
2337
Bram Moolenaara07fec92016-02-05 21:04:08 +01002338 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002339 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002340 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002341
2342 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002343 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002344 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002345 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002346 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002347 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002348 break;
2349 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002350 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002351 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002352 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002353 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002354 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002355 if (buffer != NULL)
2356 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002357 if (msg == NULL)
2358 /* JSON or JS mode: re-encode the message. */
2359 msg = json_encode(listtv, ch_mode);
2360 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002361 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002362 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002363
Bram Moolenaar187db502016-02-27 14:44:26 +01002364 if (callback != NULL)
2365 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002366 if (cbitem != NULL)
2367 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2368 else
2369 {
2370 /* invoke the channel callback */
2371 ch_logs(channel, "Invoking channel callback %s",
2372 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002373 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002374 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002375 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002376 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002377 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002378 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002379
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002380 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002381 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002382 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002383
2384 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002385}
2386
2387/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002388 * Return TRUE when channel "channel" is open for writing to.
2389 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002390 */
2391 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002392channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002393{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002394 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002395 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002396}
2397
2398/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002399 * Return TRUE when channel "channel" is open for reading or writing.
2400 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002401 */
2402 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002403channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002404{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002405 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002406 || channel->CH_IN_FD != INVALID_FD
2407 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002408 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002409}
2410
2411/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002412 * Return TRUE if "channel" has JSON or other typeahead.
2413 */
2414 static int
2415channel_has_readahead(channel_T *channel, int part)
2416{
2417 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2418
2419 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2420 {
2421 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2422 jsonq_T *item = head->jq_next;
2423
2424 return item != NULL;
2425 }
2426 return channel_peek(channel, part) != NULL;
2427}
2428
2429/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002430 * Return a string indicating the status of the channel.
2431 */
2432 char *
2433channel_status(channel_T *channel)
2434{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002435 int part;
2436 int has_readahead = FALSE;
2437
Bram Moolenaar77073442016-02-13 23:23:53 +01002438 if (channel == NULL)
2439 return "fail";
2440 if (channel_is_open(channel))
2441 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002442 for (part = PART_SOCK; part <= PART_ERR; ++part)
2443 if (channel_has_readahead(channel, part))
2444 {
2445 has_readahead = TRUE;
2446 break;
2447 }
2448
2449 if (has_readahead)
2450 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002451 return "closed";
2452}
2453
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002454 static void
2455channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2456{
2457 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002458 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002459 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002460 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002461
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002462 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002463 STRCAT(namebuf, "_");
2464 tail = STRLEN(namebuf);
2465
2466 STRCPY(namebuf + tail, "status");
2467 dict_add_nr_str(dict, namebuf, 0,
2468 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2469
2470 STRCPY(namebuf + tail, "mode");
2471 switch (chanpart->ch_mode)
2472 {
2473 case MODE_NL: s = "NL"; break;
2474 case MODE_RAW: s = "RAW"; break;
2475 case MODE_JSON: s = "JSON"; break;
2476 case MODE_JS: s = "JS"; break;
2477 }
2478 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2479
2480 STRCPY(namebuf + tail, "io");
2481 if (part == PART_SOCK)
2482 s = "socket";
2483 else switch (chanpart->ch_io)
2484 {
2485 case JIO_NULL: s = "null"; break;
2486 case JIO_PIPE: s = "pipe"; break;
2487 case JIO_FILE: s = "file"; break;
2488 case JIO_BUFFER: s = "buffer"; break;
2489 case JIO_OUT: s = "out"; break;
2490 }
2491 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2492
2493 STRCPY(namebuf + tail, "timeout");
2494 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2495}
2496
2497 void
2498channel_info(channel_T *channel, dict_T *dict)
2499{
2500 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2501 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2502
2503 if (channel->ch_hostname != NULL)
2504 {
2505 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2506 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2507 channel_part_info(channel, dict, "sock", PART_SOCK);
2508 }
2509 else
2510 {
2511 channel_part_info(channel, dict, "out", PART_OUT);
2512 channel_part_info(channel, dict, "err", PART_ERR);
2513 channel_part_info(channel, dict, "in", PART_IN);
2514 }
2515}
2516
Bram Moolenaar77073442016-02-13 23:23:53 +01002517/*
2518 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002519 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002520 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002521 */
2522 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002523channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002524{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002525 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002526
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002527#ifdef FEAT_GUI
2528 channel_gui_unregister(channel);
2529#endif
2530
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002531 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002532 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002533 sock_close(channel->CH_SOCK_FD);
2534 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002535 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002536 may_close_part(&channel->CH_IN_FD);
2537 may_close_part(&channel->CH_OUT_FD);
2538 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002539
Bram Moolenaar8b374212016-02-24 20:43:06 +01002540 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002541 {
2542 typval_T argv[1];
2543 typval_T rettv;
2544 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002545 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002546
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002547 /* Invoke callbacks before the close callback, since it's weird to
2548 * first invoke the close callback. Increment the refcount to avoid
2549 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002550 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002551 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002552 for (part = PART_SOCK; part <= PART_ERR; ++part)
2553 while (may_invoke_callback(channel, part))
2554 ;
2555
2556 /* Invoke the close callback, if still set. */
2557 if (channel->ch_close_cb != NULL)
2558 {
2559 ch_logs(channel, "Invoking close callback %s",
2560 (char *)channel->ch_close_cb);
2561 argv[0].v_type = VAR_CHANNEL;
2562 argv[0].vval.v_channel = channel;
2563 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002564 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2565 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002566 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002567 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002568 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002569 --channel->ch_refcount;
2570
2571 /* the callback is only called once */
2572 vim_free(channel->ch_close_cb);
2573 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002574 partial_unref(channel->ch_close_partial);
2575 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002576
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002577 if (channel_need_redraw)
2578 {
2579 channel_need_redraw = FALSE;
2580 redraw_after_callback();
2581 }
2582
Bram Moolenaar437905c2016-04-26 19:01:05 +02002583 /* any remaining messages are useless now */
2584 for (part = PART_SOCK; part <= PART_ERR; ++part)
2585 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002586 }
2587
2588 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002589}
2590
Bram Moolenaard04a0202016-01-26 23:30:18 +01002591/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002592 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002593 * Returns NULL if there is nothing.
2594 */
2595 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002596channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002597{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002598 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002599
Bram Moolenaar77073442016-02-13 23:23:53 +01002600 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002601 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002602 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002603}
2604
2605/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002606 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002607 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002608 static void
2609channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002610{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002611 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2612 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002613
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002614 while (channel_peek(channel, part) != NULL)
2615 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002616
2617 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002618 {
2619 cbq_T *node = cb_head->cq_next;
2620
2621 remove_cb_node(cb_head, node);
2622 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002623 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002624 vim_free(node);
2625 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002626
2627 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002628 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002629 free_tv(json_head->jq_next->jq_value);
2630 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002631 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002632
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002633 vim_free(channel->ch_part[part].ch_callback);
2634 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002635 partial_unref(channel->ch_part[part].ch_partial);
2636 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002637}
2638
2639/*
2640 * Clear all the read buffers on "channel".
2641 */
2642 void
2643channel_clear(channel_T *channel)
2644{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002645 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002646 vim_free(channel->ch_hostname);
2647 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002648 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002649 channel_clear_one(channel, PART_OUT);
2650 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002651 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002652 vim_free(channel->ch_callback);
2653 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002654 partial_unref(channel->ch_partial);
2655 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002656 vim_free(channel->ch_close_cb);
2657 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002658 partial_unref(channel->ch_close_partial);
2659 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660}
2661
Bram Moolenaar77073442016-02-13 23:23:53 +01002662#if defined(EXITFREE) || defined(PROTO)
2663 void
2664channel_free_all(void)
2665{
2666 channel_T *channel;
2667
Bram Moolenaard6051b52016-02-28 15:49:03 +01002668 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002669 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2670 channel_clear(channel);
2671}
2672#endif
2673
2674
Bram Moolenaar715d2852016-04-30 17:06:31 +02002675/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002676#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002677
2678/* Buffer size for reading incoming messages. */
2679#define MAXMSGSIZE 4096
2680
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002681#if defined(HAVE_SELECT)
2682/*
2683 * Add write fds where we are waiting for writing to be possible.
2684 */
2685 static int
2686channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2687{
2688 int maxfd = maxfd_arg;
2689 channel_T *ch;
2690
2691 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2692 {
2693 chanpart_T *in_part = &ch->ch_part[PART_IN];
2694
2695 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2696 {
2697 FD_SET((int)in_part->ch_fd, wfds);
2698 if ((int)in_part->ch_fd >= maxfd)
2699 maxfd = (int)in_part->ch_fd + 1;
2700 }
2701 }
2702 return maxfd;
2703}
2704#else
2705/*
2706 * Add write fds where we are waiting for writing to be possible.
2707 */
2708 static int
2709channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2710{
2711 int nfd = nfd_in;
2712 channel_T *ch;
2713
2714 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2715 {
2716 chanpart_T *in_part = &ch->ch_part[PART_IN];
2717
2718 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2719 {
2720 in_part->ch_poll_idx = nfd;
2721 fds[nfd].fd = in_part->ch_fd;
2722 fds[nfd].events = POLLOUT;
2723 ++nfd;
2724 }
2725 else
2726 in_part->ch_poll_idx = -1;
2727 }
2728 return nfd;
2729}
2730#endif
2731
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002732typedef enum {
2733 CW_READY,
2734 CW_NOT_READY,
2735 CW_ERROR
2736} channel_wait_result;
2737
Bram Moolenaard04a0202016-01-26 23:30:18 +01002738/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002739 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002740 * Return CW_READY when there is something to read.
2741 * Return CW_NOT_READY when there is nothing to read.
2742 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002743 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002744 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002745channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002746{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002747 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002748 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002749
Bram Moolenaard8070362016-02-15 21:56:54 +01002750# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002751 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002752 {
2753 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002754 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002755 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002756 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002757
2758 /* reading from a pipe, not a socket */
2759 while (TRUE)
2760 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002761 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2762
2763 if (r && nread > 0)
2764 return CW_READY;
2765 if (r == 0)
2766 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002767
2768 /* perhaps write some buffer lines */
2769 channel_write_any_lines();
2770
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002771 sleep_time = deadline - GetTickCount();
2772 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002773 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002774 /* Wait for a little while. Very short at first, up to 10 msec
2775 * after looping a few times. */
2776 if (sleep_time > delay)
2777 sleep_time = delay;
2778 Sleep(sleep_time);
2779 delay = delay * 2;
2780 if (delay > 10)
2781 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002782 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002783 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002784 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002785#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002786 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002787#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002788 struct timeval tval;
2789 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002790 fd_set wfds;
2791 int ret;
2792 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002793
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002794 tval.tv_sec = timeout / 1000;
2795 tval.tv_usec = (timeout % 1000) * 1000;
2796 for (;;)
2797 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002798 FD_ZERO(&rfds);
2799 FD_SET((int)fd, &rfds);
2800
2801 /* Write lines to a pipe when a pipe can be written to. Need to
2802 * set this every time, some buffers may be done. */
2803 maxfd = (int)fd + 1;
2804 FD_ZERO(&wfds);
2805 maxfd = channel_fill_wfds(maxfd, &wfds);
2806
2807 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002808# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002809 SOCK_ERRNO;
2810 if (ret == -1 && errno == EINTR)
2811 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002812# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002813 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002814 {
2815 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002816 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002817 channel_write_any_lines();
2818 continue;
2819 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002820 break;
2821 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002822#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002823 for (;;)
2824 {
2825 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2826 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002827
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002828 fds[0].fd = fd;
2829 fds[0].events = POLLIN;
2830 nfd = channel_fill_poll_write(nfd, fds);
2831 if (poll(fds, nfd, timeout) > 0)
2832 {
2833 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002834 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002835 channel_write_any_lines();
2836 continue;
2837 }
2838 break;
2839 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002840#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002841 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002842 return CW_NOT_READY;
2843}
2844
2845 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002846channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002847{
2848 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002849 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2850 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002851
2852 /* Queue a "DETACH" netbeans message in the command queue in order to
2853 * terminate the netbeans session later. Do not end the session here
2854 * directly as we may be running in the context of a call to
2855 * netbeans_parse_messages():
2856 * netbeans_parse_messages
2857 * -> autocmd triggered while processing the netbeans cmd
2858 * -> ui_breakcheck
2859 * -> gui event loop or select loop
2860 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002861 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002862 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002863 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002864 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002865 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2866
2867 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002868 * died. Don't close the channel right away, it may be the wrong moment
2869 * to invoke callbacks. */
2870 channel->ch_to_be_closed = TRUE;
2871}
2872
2873 static void
2874channel_close_now(channel_T *channel)
2875{
2876 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002877 channel_close(channel, TRUE);
2878 if (channel->ch_nb_close_cb != NULL)
2879 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002880}
2881
2882/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002883 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002884 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002885 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002886 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002887 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002888channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002889{
2890 static char_u *buf = NULL;
2891 int len = 0;
2892 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002893 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002894 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002895
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002896 fd = channel->ch_part[part].ch_fd;
2897 if (fd == INVALID_FD)
2898 {
2899 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002900 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002901 }
2902 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002903
2904 /* Allocate a buffer to read into. */
2905 if (buf == NULL)
2906 {
2907 buf = alloc(MAXMSGSIZE);
2908 if (buf == NULL)
2909 return; /* out of memory! */
2910 }
2911
2912 /* Keep on reading for as long as there is something to read.
2913 * Use select() or poll() to avoid blocking on a message that is exactly
2914 * MAXMSGSIZE long. */
2915 for (;;)
2916 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002917 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002918 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002919 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002920 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002921 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002922 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002923 if (len <= 0)
2924 break; /* error or nothing more to read */
2925
2926 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002927 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002928 readlen += len;
2929 if (len < MAXMSGSIZE)
2930 break; /* did read everything that's available */
2931 }
2932
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002933 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002934 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002935 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002936
2937#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002938 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002939 if (CH_HAS_GUI && gtk_main_level() > 0)
2940 gtk_main_quit();
2941#endif
2942}
2943
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002944/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002945 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002946 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002947 * Returns what was read in allocated memory.
2948 * Returns NULL in case of error or timeout.
2949 */
2950 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002951channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002952{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002953 char_u *buf;
2954 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002955 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002956 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002957 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002958
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002959 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002960 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002961
2962 while (TRUE)
2963 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002964 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002965 if (buf != NULL && (mode == MODE_RAW
2966 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2967 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002968 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002969 continue;
2970
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002971 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002972 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002973 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002974 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002975 {
2976 ch_log(channel, "Timed out");
2977 return NULL;
2978 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002979 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002980 }
2981
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002982 if (mode == MODE_RAW)
2983 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002984 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002985 }
2986 else
2987 {
2988 nl = vim_strchr(buf, NL);
2989 if (nl[1] == NUL)
2990 {
2991 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002992 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002993 *nl = NUL;
2994 }
2995 else
2996 {
2997 /* Copy the message into allocated memory and remove it from the
2998 * buffer. */
2999 msg = vim_strnsave(buf, (int)(nl - buf));
3000 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
3001 }
3002 }
3003 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003004 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003005 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003006}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003007
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003008/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003009 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003010 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003011 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003012 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003013 */
3014 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003015channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003016 channel_T *channel,
3017 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003018 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003019 int id,
3020 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003021{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003022 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003023 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003024 int timeout;
3025 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003026
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003027 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003028 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003029 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003030 for (;;)
3031 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003032 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003033
3034 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003035 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003036 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003037 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003038 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003039 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003040
Bram Moolenaard7ece102016-02-02 23:23:02 +01003041 if (!more)
3042 {
3043 /* Handle any other messages in the queue. If done some more
3044 * messages may have arrived. */
3045 if (channel_parse_messages())
3046 continue;
3047
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003048 /* Wait for up to the timeout. If there was an incomplete message
3049 * use the deadline for that. */
3050 timeout = timeout_arg;
3051 if (chanpart->ch_waiting)
3052 {
3053#ifdef WIN32
3054 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3055#else
3056 {
3057 struct timeval now_tv;
3058
3059 gettimeofday(&now_tv, NULL);
3060 timeout = (chanpart->ch_deadline.tv_sec
3061 - now_tv.tv_sec) * 1000
3062 + (chanpart->ch_deadline.tv_usec
3063 - now_tv.tv_usec) / 1000
3064 + 1;
3065 }
3066#endif
3067 if (timeout < 0)
3068 {
3069 /* Something went wrong, channel_parse_json() didn't
3070 * discard message. Cancel waiting. */
3071 chanpart->ch_waiting = FALSE;
3072 timeout = timeout_arg;
3073 }
3074 else if (timeout > timeout_arg)
3075 timeout = timeout_arg;
3076 }
3077 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003078 if (fd == INVALID_FD
3079 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003080 {
3081 if (timeout == timeout_arg)
3082 {
3083 if (fd != INVALID_FD)
3084 ch_log(channel, "Timed out");
3085 break;
3086 }
3087 }
3088 else
3089 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003090 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003091 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003092 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003093 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003094}
3095
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003096/*
3097 * Common for ch_read() and ch_readraw().
3098 */
3099 void
3100common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3101{
3102 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003103 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003104 jobopt_T opt;
3105 int mode;
3106 int timeout;
3107 int id = -1;
3108 typval_T *listtv = NULL;
3109
3110 /* return an empty string by default */
3111 rettv->v_type = VAR_STRING;
3112 rettv->vval.v_string = NULL;
3113
3114 clear_job_options(&opt);
3115 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3116 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003117 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003118
Bram Moolenaar437905c2016-04-26 19:01:05 +02003119 if (opt.jo_set & JO_PART)
3120 part = opt.jo_part;
3121 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003122 if (channel != NULL)
3123 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003124 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003125 part = channel_part_read(channel);
3126 mode = channel_get_mode(channel, part);
3127 timeout = channel_get_timeout(channel, part);
3128 if (opt.jo_set & JO_TIMEOUT)
3129 timeout = opt.jo_timeout;
3130
3131 if (raw || mode == MODE_RAW || mode == MODE_NL)
3132 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3133 else
3134 {
3135 if (opt.jo_set & JO_ID)
3136 id = opt.jo_id;
3137 channel_read_json_block(channel, part, timeout, id, &listtv);
3138 if (listtv != NULL)
3139 {
3140 *rettv = *listtv;
3141 vim_free(listtv);
3142 }
3143 else
3144 {
3145 rettv->v_type = VAR_SPECIAL;
3146 rettv->vval.v_number = VVAL_NONE;
3147 }
3148 }
3149 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003150
3151theend:
3152 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003153}
3154
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003155# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3156 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003157/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003158 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003159 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003160 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003161 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003162channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003163{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003164 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003165 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003166
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003167 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003168 for (channel = first_channel; channel != NULL;
3169 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003170 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003171 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003172 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003173 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003174 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003175 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003176 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003177 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003178 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003179}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003180# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003181
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003182# if defined(WIN32) || defined(PROTO)
3183/*
3184 * Check the channels for anything that is ready to be read.
3185 * The data is put in the read queue.
3186 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003187 void
3188channel_handle_events(void)
3189{
3190 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003191 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003192 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003193
3194 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3195 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003196 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003197 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003198 {
3199 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003200 if (fd != INVALID_FD)
3201 {
3202 int r = channel_wait(channel, fd, 0);
3203
3204 if (r == CW_READY)
3205 channel_read(channel, part, "channel_handle_events");
3206 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003207 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003208 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003209 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003210 }
3211}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003212# endif
3213
Bram Moolenaard04a0202016-01-26 23:30:18 +01003214/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003215 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003216 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003217 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003218 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003219 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003220channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003221{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003222 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003223 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003224 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003225
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003226 fd = channel->ch_part[part].ch_fd;
3227 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003228 {
3229 if (!channel->ch_error && fun != NULL)
3230 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003231 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003232 EMSG2("E630: %s(): write while not connected", fun);
3233 }
3234 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003235 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003237
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003238 if (log_fd != NULL)
3239 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003240 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003241 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003242 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003243 fprintf(log_fd, "'\n");
3244 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003245 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003246 }
3247
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003248 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003249 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003250 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003251 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003252 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003253 {
3254 if (!channel->ch_error && fun != NULL)
3255 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003256 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003257 EMSG2("E631: %s(): write failed", fun);
3258 }
3259 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003260 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003261 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003262
3263 channel->ch_error = FALSE;
3264 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003265}
3266
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003267/*
3268 * Common for "ch_sendexpr()" and "ch_sendraw()".
3269 * Returns the channel if the caller should read the response.
3270 * Sets "part_read" to the the read fd.
3271 * Otherwise returns NULL.
3272 */
3273 channel_T *
3274send_common(
3275 typval_T *argvars,
3276 char_u *text,
3277 int id,
3278 int eval,
3279 jobopt_T *opt,
3280 char *fun,
3281 int *part_read)
3282{
3283 channel_T *channel;
3284 int part_send;
3285
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003286 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003287 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003288 if (channel == NULL)
3289 return NULL;
3290 part_send = channel_part_send(channel);
3291 *part_read = channel_part_read(channel);
3292
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003293 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3294 return NULL;
3295
3296 /* Set the callback. An empty callback means no callback and not reading
3297 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3298 * allowed. */
3299 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3300 {
3301 if (eval)
3302 {
3303 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3304 return NULL;
3305 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003306 channel_set_req_callback(channel, part_send,
3307 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003308 }
3309
3310 if (channel_send(channel, part_send, text, fun) == OK
3311 && opt->jo_callback == NULL)
3312 return channel;
3313 return NULL;
3314}
3315
3316/*
3317 * common for "ch_evalexpr()" and "ch_sendexpr()"
3318 */
3319 void
3320ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3321{
3322 char_u *text;
3323 typval_T *listtv;
3324 channel_T *channel;
3325 int id;
3326 ch_mode_T ch_mode;
3327 int part_send;
3328 int part_read;
3329 jobopt_T opt;
3330 int timeout;
3331
3332 /* return an empty string by default */
3333 rettv->v_type = VAR_STRING;
3334 rettv->vval.v_string = NULL;
3335
Bram Moolenaar437905c2016-04-26 19:01:05 +02003336 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003337 if (channel == NULL)
3338 return;
3339 part_send = channel_part_send(channel);
3340
3341 ch_mode = channel_get_mode(channel, part_send);
3342 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3343 {
3344 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3345 return;
3346 }
3347
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003348 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003349 text = json_encode_nr_expr(id, &argvars[1],
3350 ch_mode == MODE_JS ? JSON_JS : 0);
3351 if (text == NULL)
3352 return;
3353
3354 channel = send_common(argvars, text, id, eval, &opt,
3355 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3356 vim_free(text);
3357 if (channel != NULL && eval)
3358 {
3359 if (opt.jo_set & JO_TIMEOUT)
3360 timeout = opt.jo_timeout;
3361 else
3362 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003363 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3364 == OK)
3365 {
3366 list_T *list = listtv->vval.v_list;
3367
3368 /* Move the item from the list and then change the type to
3369 * avoid the value being freed. */
3370 *rettv = list->lv_last->li_tv;
3371 list->lv_last->li_tv.v_type = VAR_NUMBER;
3372 free_tv(listtv);
3373 }
3374 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003375 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003376}
3377
3378/*
3379 * common for "ch_evalraw()" and "ch_sendraw()"
3380 */
3381 void
3382ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3383{
3384 char_u buf[NUMBUFLEN];
3385 char_u *text;
3386 channel_T *channel;
3387 int part_read;
3388 jobopt_T opt;
3389 int timeout;
3390
3391 /* return an empty string by default */
3392 rettv->v_type = VAR_STRING;
3393 rettv->vval.v_string = NULL;
3394
3395 text = get_tv_string_buf(&argvars[1], buf);
3396 channel = send_common(argvars, text, 0, eval, &opt,
3397 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3398 if (channel != NULL && eval)
3399 {
3400 if (opt.jo_set & JO_TIMEOUT)
3401 timeout = opt.jo_timeout;
3402 else
3403 timeout = channel_get_timeout(channel, part_read);
3404 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3405 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003406 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003407}
3408
Bram Moolenaard04a0202016-01-26 23:30:18 +01003409# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003410/*
3411 * Add open channels to the poll struct.
3412 * Return the adjusted struct index.
3413 * The type of "fds" is hidden to avoid problems with the function proto.
3414 */
3415 int
3416channel_poll_setup(int nfd_in, void *fds_in)
3417{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003418 int nfd = nfd_in;
3419 channel_T *channel;
3420 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003421 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003422
Bram Moolenaar77073442016-02-13 23:23:53 +01003423 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003424 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003425 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003426 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003427 chanpart_T *ch_part = &channel->ch_part[part];
3428
3429 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003430 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003431 ch_part->ch_poll_idx = nfd;
3432 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003433 fds[nfd].events = POLLIN;
3434 nfd++;
3435 }
3436 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003437 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003438 }
3439 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003440
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003441 nfd = channel_fill_poll_write(nfd, fds);
3442
Bram Moolenaare0874f82016-01-24 20:36:41 +01003443 return nfd;
3444}
3445
3446/*
3447 * The type of "fds" is hidden to avoid problems with the function proto.
3448 */
3449 int
3450channel_poll_check(int ret_in, void *fds_in)
3451{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003452 int ret = ret_in;
3453 channel_T *channel;
3454 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003455 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003456 int idx;
3457 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003458
Bram Moolenaar77073442016-02-13 23:23:53 +01003459 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003460 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003461 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003462 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003463 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003464
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003465 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003466 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003467 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003468 --ret;
3469 }
3470 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003471
3472 in_part = &channel->ch_part[PART_IN];
3473 idx = in_part->ch_poll_idx;
3474 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3475 {
3476 if (in_part->ch_buf_append)
3477 {
3478 if (in_part->ch_buffer != NULL)
3479 channel_write_new_lines(in_part->ch_buffer);
3480 }
3481 else
3482 channel_write_in(channel);
3483 --ret;
3484 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003485 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003486
3487 return ret;
3488}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003489# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003490
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003491# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003492/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003493 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003494 */
3495 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003496channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003497{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003498 int maxfd = maxfd_in;
3499 channel_T *channel;
3500 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003501 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003502 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003503
Bram Moolenaar77073442016-02-13 23:23:53 +01003504 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003505 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003506 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003507 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003508 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003509
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003510 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003511 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003512 FD_SET((int)fd, rfds);
3513 if (maxfd < (int)fd)
3514 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003515 }
3516 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003517 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003518
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003519 maxfd = channel_fill_wfds(maxfd, wfds);
3520
Bram Moolenaare0874f82016-01-24 20:36:41 +01003521 return maxfd;
3522}
3523
3524/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003525 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003526 */
3527 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003528channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003529{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003530 int ret = ret_in;
3531 channel_T *channel;
3532 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003533 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003534 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003535 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003536
Bram Moolenaar77073442016-02-13 23:23:53 +01003537 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003538 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003539 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003540 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003541 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003542
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003543 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003544 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003545 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003546 --ret;
3547 }
3548 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003549
3550 in_part = &channel->ch_part[PART_IN];
3551 if (ret > 0 && in_part->ch_fd != INVALID_FD
3552 && FD_ISSET(in_part->ch_fd, wfds))
3553 {
3554 if (in_part->ch_buf_append)
3555 {
3556 if (in_part->ch_buffer != NULL)
3557 channel_write_new_lines(in_part->ch_buffer);
3558 }
3559 else
3560 channel_write_in(channel);
3561 --ret;
3562 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003563 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003564
3565 return ret;
3566}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003567# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003568
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003569/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003570 * Execute queued up commands.
3571 * Invoked from the main loop when it's safe to execute received commands.
3572 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003573 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003574 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003575channel_parse_messages(void)
3576{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003577 channel_T *channel = first_channel;
3578 int ret = FALSE;
3579 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003580 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003581
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003582 ++safe_to_invoke_callback;
3583
Bram Moolenaard0b65022016-03-06 21:50:33 +01003584 /* Only do this message when another message was given, otherwise we get
3585 * lots of them. */
3586 if (did_log_msg)
3587 {
3588 ch_log(NULL, "looking for messages on channels");
3589 did_log_msg = FALSE;
3590 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003591 while (channel != NULL)
3592 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003593 if (channel->ch_to_be_closed)
3594 {
3595 channel->ch_to_be_closed = FALSE;
3596 channel_close_now(channel);
3597 /* channel may have been freed, start over */
3598 channel = first_channel;
3599 continue;
3600 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003601 if (channel->ch_to_be_freed)
3602 {
3603 channel_free(channel);
3604 /* channel has been freed, start over */
3605 channel = first_channel;
3606 continue;
3607 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003608 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003609 {
3610 /* channel is no longer useful, free it */
3611 channel_free(channel);
3612 channel = first_channel;
3613 part = PART_SOCK;
3614 continue;
3615 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003616 if (channel->ch_part[part].ch_fd != INVALID_FD
3617 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003618 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003619 /* Increase the refcount, in case the handler causes the channel
3620 * to be unreferenced or closed. */
3621 ++channel->ch_refcount;
3622 r = may_invoke_callback(channel, part);
3623 if (r == OK)
3624 ret = TRUE;
3625 if (channel_unref(channel) || r == OK)
3626 {
3627 /* channel was freed or something was done, start over */
3628 channel = first_channel;
3629 part = PART_SOCK;
3630 continue;
3631 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003632 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003633 if (part < PART_ERR)
3634 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003635 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003636 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003637 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003638 part = PART_SOCK;
3639 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003640 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003641
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003642 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003643 {
3644 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003645 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003646 }
3647
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003648 --safe_to_invoke_callback;
3649
Bram Moolenaard7ece102016-02-02 23:23:02 +01003650 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003651}
3652
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003653/*
3654 * Mark references to lists used in channels.
3655 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003656 int
3657set_ref_in_channel(int copyID)
3658{
Bram Moolenaar77073442016-02-13 23:23:53 +01003659 int abort = FALSE;
3660 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003661 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003662
Bram Moolenaar77073442016-02-13 23:23:53 +01003663 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003664 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003665 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003666 tv.v_type = VAR_CHANNEL;
3667 tv.vval.v_channel = channel;
3668 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003669 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003670 return abort;
3671}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003672
3673/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003674 * Return the "part" to write to for "channel".
3675 */
3676 int
3677channel_part_send(channel_T *channel)
3678{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003679 if (channel->CH_SOCK_FD == INVALID_FD)
3680 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003681 return PART_SOCK;
3682}
3683
3684/*
3685 * Return the default "part" to read from for "channel".
3686 */
3687 int
3688channel_part_read(channel_T *channel)
3689{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003690 if (channel->CH_SOCK_FD == INVALID_FD)
3691 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003692 return PART_SOCK;
3693}
3694
3695/*
3696 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003697 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003698 */
3699 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003700channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003701{
Bram Moolenaar77073442016-02-13 23:23:53 +01003702 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003703 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003704 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003705}
3706
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003707/*
3708 * Return the timeout of "channel"/"part"
3709 */
3710 int
3711channel_get_timeout(channel_T *channel, int part)
3712{
3713 return channel->ch_part[part].ch_timeout;
3714}
3715
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003716 static int
3717handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3718{
3719 char_u *val = get_tv_string(item);
3720
3721 opt->jo_set |= jo;
3722 if (STRCMP(val, "nl") == 0)
3723 *modep = MODE_NL;
3724 else if (STRCMP(val, "raw") == 0)
3725 *modep = MODE_RAW;
3726 else if (STRCMP(val, "js") == 0)
3727 *modep = MODE_JS;
3728 else if (STRCMP(val, "json") == 0)
3729 *modep = MODE_JSON;
3730 else
3731 {
3732 EMSG2(_(e_invarg2), val);
3733 return FAIL;
3734 }
3735 return OK;
3736}
3737
3738 static int
3739handle_io(typval_T *item, int part, jobopt_T *opt)
3740{
3741 char_u *val = get_tv_string(item);
3742
3743 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3744 if (STRCMP(val, "null") == 0)
3745 opt->jo_io[part] = JIO_NULL;
3746 else if (STRCMP(val, "pipe") == 0)
3747 opt->jo_io[part] = JIO_PIPE;
3748 else if (STRCMP(val, "file") == 0)
3749 opt->jo_io[part] = JIO_FILE;
3750 else if (STRCMP(val, "buffer") == 0)
3751 opt->jo_io[part] = JIO_BUFFER;
3752 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3753 opt->jo_io[part] = JIO_OUT;
3754 else
3755 {
3756 EMSG2(_(e_invarg2), val);
3757 return FAIL;
3758 }
3759 return OK;
3760}
3761
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003762/*
3763 * Clear a jobopt_T before using it.
3764 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003765 void
3766clear_job_options(jobopt_T *opt)
3767{
3768 vim_memset(opt, 0, sizeof(jobopt_T));
3769}
3770
3771/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003772 * Free any members of a jobopt_T.
3773 */
3774 void
3775free_job_options(jobopt_T *opt)
3776{
3777 if (opt->jo_partial != NULL)
3778 partial_unref(opt->jo_partial);
3779 if (opt->jo_out_partial != NULL)
3780 partial_unref(opt->jo_out_partial);
3781 if (opt->jo_err_partial != NULL)
3782 partial_unref(opt->jo_err_partial);
3783 if (opt->jo_close_partial != NULL)
3784 partial_unref(opt->jo_close_partial);
3785}
3786
3787/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003788 * Get the PART_ number from the first character of an option name.
3789 */
3790 static int
3791part_from_char(int c)
3792{
3793 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3794}
3795
3796/*
3797 * Get the option entries from the dict in "tv", parse them and put the result
3798 * in "opt".
3799 * Only accept options in "supported".
3800 * If an option value is invalid return FAIL.
3801 */
3802 int
3803get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3804{
3805 typval_T *item;
3806 char_u *val;
3807 dict_T *dict;
3808 int todo;
3809 hashitem_T *hi;
3810 int part;
3811
3812 opt->jo_set = 0;
3813 if (tv->v_type == VAR_UNKNOWN)
3814 return OK;
3815 if (tv->v_type != VAR_DICT)
3816 {
3817 EMSG(_(e_invarg));
3818 return FAIL;
3819 }
3820 dict = tv->vval.v_dict;
3821 if (dict == NULL)
3822 return OK;
3823
3824 todo = (int)dict->dv_hashtab.ht_used;
3825 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3826 if (!HASHITEM_EMPTY(hi))
3827 {
3828 item = &dict_lookup(hi)->di_tv;
3829
3830 if (STRCMP(hi->hi_key, "mode") == 0)
3831 {
3832 if (!(supported & JO_MODE))
3833 break;
3834 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3835 return FAIL;
3836 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003837 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003838 {
3839 if (!(supported & JO_IN_MODE))
3840 break;
3841 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3842 == FAIL)
3843 return FAIL;
3844 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003845 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003846 {
3847 if (!(supported & JO_OUT_MODE))
3848 break;
3849 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3850 == FAIL)
3851 return FAIL;
3852 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003853 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003854 {
3855 if (!(supported & JO_ERR_MODE))
3856 break;
3857 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3858 == FAIL)
3859 return FAIL;
3860 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003861 else if (STRCMP(hi->hi_key, "in_io") == 0
3862 || STRCMP(hi->hi_key, "out_io") == 0
3863 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003864 {
3865 if (!(supported & JO_OUT_IO))
3866 break;
3867 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3868 return FAIL;
3869 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003870 else if (STRCMP(hi->hi_key, "in_name") == 0
3871 || STRCMP(hi->hi_key, "out_name") == 0
3872 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003873 {
3874 part = part_from_char(*hi->hi_key);
3875
3876 if (!(supported & JO_OUT_IO))
3877 break;
3878 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3879 opt->jo_io_name[part] =
3880 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3881 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003882 else if (STRCMP(hi->hi_key, "in_buf") == 0
3883 || STRCMP(hi->hi_key, "out_buf") == 0
3884 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003885 {
3886 part = part_from_char(*hi->hi_key);
3887
3888 if (!(supported & JO_OUT_IO))
3889 break;
3890 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3891 opt->jo_io_buf[part] = get_tv_number(item);
3892 if (opt->jo_io_buf[part] <= 0)
3893 {
3894 EMSG2(_(e_invarg2), get_tv_string(item));
3895 return FAIL;
3896 }
3897 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3898 {
3899 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3900 return FAIL;
3901 }
3902 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003903 else if (STRCMP(hi->hi_key, "in_top") == 0
3904 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003905 {
3906 linenr_T *lp;
3907
3908 if (!(supported & JO_OUT_IO))
3909 break;
3910 if (hi->hi_key[3] == 't')
3911 {
3912 lp = &opt->jo_in_top;
3913 opt->jo_set |= JO_IN_TOP;
3914 }
3915 else
3916 {
3917 lp = &opt->jo_in_bot;
3918 opt->jo_set |= JO_IN_BOT;
3919 }
3920 *lp = get_tv_number(item);
3921 if (*lp < 0)
3922 {
3923 EMSG2(_(e_invarg2), get_tv_string(item));
3924 return FAIL;
3925 }
3926 }
3927 else if (STRCMP(hi->hi_key, "channel") == 0)
3928 {
3929 if (!(supported & JO_OUT_IO))
3930 break;
3931 opt->jo_set |= JO_CHANNEL;
3932 if (item->v_type != VAR_CHANNEL)
3933 {
3934 EMSG2(_(e_invarg2), "channel");
3935 return FAIL;
3936 }
3937 opt->jo_channel = item->vval.v_channel;
3938 }
3939 else if (STRCMP(hi->hi_key, "callback") == 0)
3940 {
3941 if (!(supported & JO_CALLBACK))
3942 break;
3943 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003944 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003945 if (opt->jo_callback == NULL)
3946 {
3947 EMSG2(_(e_invarg2), "callback");
3948 return FAIL;
3949 }
3950 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003951 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003952 {
3953 if (!(supported & JO_OUT_CALLBACK))
3954 break;
3955 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003956 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003957 if (opt->jo_out_cb == NULL)
3958 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003959 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003960 return FAIL;
3961 }
3962 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003963 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003964 {
3965 if (!(supported & JO_ERR_CALLBACK))
3966 break;
3967 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003968 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003969 if (opt->jo_err_cb == NULL)
3970 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003971 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003972 return FAIL;
3973 }
3974 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003975 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003976 {
3977 if (!(supported & JO_CLOSE_CALLBACK))
3978 break;
3979 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003980 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003981 if (opt->jo_close_cb == NULL)
3982 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003983 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003984 return FAIL;
3985 }
3986 }
3987 else if (STRCMP(hi->hi_key, "waittime") == 0)
3988 {
3989 if (!(supported & JO_WAITTIME))
3990 break;
3991 opt->jo_set |= JO_WAITTIME;
3992 opt->jo_waittime = get_tv_number(item);
3993 }
3994 else if (STRCMP(hi->hi_key, "timeout") == 0)
3995 {
3996 if (!(supported & JO_TIMEOUT))
3997 break;
3998 opt->jo_set |= JO_TIMEOUT;
3999 opt->jo_timeout = get_tv_number(item);
4000 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004001 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004002 {
4003 if (!(supported & JO_OUT_TIMEOUT))
4004 break;
4005 opt->jo_set |= JO_OUT_TIMEOUT;
4006 opt->jo_out_timeout = get_tv_number(item);
4007 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004008 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004009 {
4010 if (!(supported & JO_ERR_TIMEOUT))
4011 break;
4012 opt->jo_set |= JO_ERR_TIMEOUT;
4013 opt->jo_err_timeout = get_tv_number(item);
4014 }
4015 else if (STRCMP(hi->hi_key, "part") == 0)
4016 {
4017 if (!(supported & JO_PART))
4018 break;
4019 opt->jo_set |= JO_PART;
4020 val = get_tv_string(item);
4021 if (STRCMP(val, "err") == 0)
4022 opt->jo_part = PART_ERR;
4023 else
4024 {
4025 EMSG2(_(e_invarg2), val);
4026 return FAIL;
4027 }
4028 }
4029 else if (STRCMP(hi->hi_key, "id") == 0)
4030 {
4031 if (!(supported & JO_ID))
4032 break;
4033 opt->jo_set |= JO_ID;
4034 opt->jo_id = get_tv_number(item);
4035 }
4036 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4037 {
4038 if (!(supported & JO_STOPONEXIT))
4039 break;
4040 opt->jo_set |= JO_STOPONEXIT;
4041 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4042 opt->jo_soe_buf);
4043 if (opt->jo_stoponexit == NULL)
4044 {
4045 EMSG2(_(e_invarg2), "stoponexit");
4046 return FAIL;
4047 }
4048 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004049 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004050 {
4051 if (!(supported & JO_EXIT_CB))
4052 break;
4053 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004054 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4055 {
4056 opt->jo_exit_partial = item->vval.v_partial;
4057 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4058 }
4059 else
4060 opt->jo_exit_cb = get_tv_string_buf_chk(
4061 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062 if (opt->jo_exit_cb == NULL)
4063 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004064 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004065 return FAIL;
4066 }
4067 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004068 else if (STRCMP(hi->hi_key, "block_write") == 0)
4069 {
4070 if (!(supported & JO_BLOCK_WRITE))
4071 break;
4072 opt->jo_set |= JO_BLOCK_WRITE;
4073 opt->jo_block_write = get_tv_number(item);
4074 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004075 else
4076 break;
4077 --todo;
4078 }
4079 if (todo > 0)
4080 {
4081 EMSG2(_(e_invarg2), hi->hi_key);
4082 return FAIL;
4083 }
4084
4085 return OK;
4086}
4087
4088/*
4089 * Get the channel from the argument.
4090 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004091 * When "check_open" is TRUE check that the channel can be used.
4092 * When "reading" is TRUE "check_open" considers typeahead useful.
4093 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004094 */
4095 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004096get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004097{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004098 channel_T *channel = NULL;
4099 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004100
4101 if (tv->v_type == VAR_JOB)
4102 {
4103 if (tv->vval.v_job != NULL)
4104 channel = tv->vval.v_job->jv_channel;
4105 }
4106 else if (tv->v_type == VAR_CHANNEL)
4107 {
4108 channel = tv->vval.v_channel;
4109 }
4110 else
4111 {
4112 EMSG2(_(e_invarg2), get_tv_string(tv));
4113 return NULL;
4114 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004115 if (channel != NULL && reading)
4116 has_readahead = channel_has_readahead(channel,
4117 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118
Bram Moolenaar437905c2016-04-26 19:01:05 +02004119 if (check_open && (channel == NULL || (!channel_is_open(channel)
4120 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004121 {
4122 EMSG(_("E906: not an open channel"));
4123 return NULL;
4124 }
4125 return channel;
4126}
4127
4128static job_T *first_job = NULL;
4129
4130 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004131job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132{
4133 ch_log(job->jv_channel, "Freeing job");
4134 if (job->jv_channel != NULL)
4135 {
4136 /* The link from the channel to the job doesn't count as a reference,
4137 * thus don't decrement the refcount of the job. The reference from
4138 * the job to the channel does count the refrence, decrement it and
4139 * NULL the reference. We don't set ch_job_killed, unreferencing the
4140 * job doesn't mean it stops running. */
4141 job->jv_channel->ch_job = NULL;
4142 channel_unref(job->jv_channel);
4143 }
4144 mch_clear_job(job);
4145
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004146 vim_free(job->jv_stoponexit);
4147 vim_free(job->jv_exit_cb);
4148 partial_unref(job->jv_exit_partial);
4149}
4150
4151 static void
4152job_free_job(job_T *job)
4153{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004154 if (job->jv_next != NULL)
4155 job->jv_next->jv_prev = job->jv_prev;
4156 if (job->jv_prev == NULL)
4157 first_job = job->jv_next;
4158 else
4159 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004160 vim_free(job);
4161}
4162
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004163 static void
4164job_free(job_T *job)
4165{
4166 if (!in_free_unref_items)
4167 {
4168 job_free_contents(job);
4169 job_free_job(job);
4170 }
4171}
4172
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004173/*
4174 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004175 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4176 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004177 */
4178 static int
4179job_still_useful(job_T *job)
4180{
4181 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004182 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4183 || (job->jv_channel != NULL
4184 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004185}
4186
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004187/*
4188 * Mark references in jobs that are still useful.
4189 */
4190 int
4191set_ref_in_job(int copyID)
4192{
4193 int abort = FALSE;
4194 job_T *job;
4195 typval_T tv;
4196
4197 for (job = first_job; job != NULL; job = job->jv_next)
4198 if (job_still_useful(job))
4199 {
4200 tv.v_type = VAR_JOB;
4201 tv.vval.v_job = job;
4202 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4203 }
4204 return abort;
4205}
4206
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004207 void
4208job_unref(job_T *job)
4209{
4210 if (job != NULL && --job->jv_refcount <= 0)
4211 {
4212 /* Do not free the job when it has not ended yet and there is a
4213 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004214 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004215 {
4216 job_free(job);
4217 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004218 else if (job->jv_channel != NULL
4219 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004220 {
4221 /* Do remove the link to the channel, otherwise it hangs
4222 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004223 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004224 job->jv_channel->ch_job = NULL;
4225 channel_unref(job->jv_channel);
4226 job->jv_channel = NULL;
4227 }
4228 }
4229}
4230
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004231 int
4232free_unused_jobs_contents(int copyID, int mask)
4233{
4234 int did_free = FALSE;
4235 job_T *job;
4236
4237 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004238 if ((job->jv_copyID & mask) != (copyID & mask)
4239 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004240 {
4241 /* Free the channel and ordinary items it contains, but don't
4242 * recurse into Lists, Dictionaries etc. */
4243 job_free_contents(job);
4244 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004245 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004246 return did_free;
4247}
4248
4249 void
4250free_unused_jobs(int copyID, int mask)
4251{
4252 job_T *job;
4253 job_T *job_next;
4254
4255 for (job = first_job; job != NULL; job = job_next)
4256 {
4257 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004258 if ((job->jv_copyID & mask) != (copyID & mask)
4259 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004260 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004261 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004262 job_free_job(job);
4263 }
4264 }
4265}
4266
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004267/*
4268 * Allocate a job. Sets the refcount to one and sets options default.
4269 */
4270 static job_T *
4271job_alloc(void)
4272{
4273 job_T *job;
4274
4275 job = (job_T *)alloc_clear(sizeof(job_T));
4276 if (job != NULL)
4277 {
4278 job->jv_refcount = 1;
4279 job->jv_stoponexit = vim_strsave((char_u *)"term");
4280
4281 if (first_job != NULL)
4282 {
4283 first_job->jv_prev = job;
4284 job->jv_next = first_job;
4285 }
4286 first_job = job;
4287 }
4288 return job;
4289}
4290
4291 void
4292job_set_options(job_T *job, jobopt_T *opt)
4293{
4294 if (opt->jo_set & JO_STOPONEXIT)
4295 {
4296 vim_free(job->jv_stoponexit);
4297 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4298 job->jv_stoponexit = NULL;
4299 else
4300 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4301 }
4302 if (opt->jo_set & JO_EXIT_CB)
4303 {
4304 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004305 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004306 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004307 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004308 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004309 job->jv_exit_partial = NULL;
4310 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004311 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004312 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004313 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004314 job->jv_exit_partial = opt->jo_exit_partial;
4315 if (job->jv_exit_partial != NULL)
4316 ++job->jv_exit_partial->pt_refcount;
4317 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004318 }
4319}
4320
4321/*
4322 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4323 */
4324 void
4325job_stop_on_exit()
4326{
4327 job_T *job;
4328
4329 for (job = first_job; job != NULL; job = job->jv_next)
4330 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4331 mch_stop_job(job, job->jv_stoponexit);
4332}
4333
4334/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004335 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004336 */
4337 void
4338job_check_ended(void)
4339{
4340 static time_t last_check = 0;
4341 time_t now;
4342 job_T *job;
4343 job_T *next;
4344
4345 /* Only do this once in 10 seconds. */
4346 now = time(NULL);
4347 if (last_check + 10 < now)
4348 {
4349 last_check = now;
4350 for (job = first_job; job != NULL; job = next)
4351 {
4352 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004353 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004354 job_status(job); /* may free "job" */
4355 }
4356 }
4357}
4358
4359/*
4360 * "job_start()" function
4361 */
4362 job_T *
4363job_start(typval_T *argvars)
4364{
4365 job_T *job;
4366 char_u *cmd = NULL;
4367#if defined(UNIX)
4368# define USE_ARGV
4369 char **argv = NULL;
4370 int argc = 0;
4371#else
4372 garray_T ga;
4373#endif
4374 jobopt_T opt;
4375 int part;
4376
4377 job = job_alloc();
4378 if (job == NULL)
4379 return NULL;
4380
4381 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004382#ifndef USE_ARGV
4383 ga_init2(&ga, (int)sizeof(char*), 20);
4384#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004385
4386 /* Default mode is NL. */
4387 clear_job_options(&opt);
4388 opt.jo_mode = MODE_NL;
4389 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004390 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4391 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004392 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004393
4394 /* Check that when io is "file" that there is a file name. */
4395 for (part = PART_OUT; part <= PART_IN; ++part)
4396 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4397 && opt.jo_io[part] == JIO_FILE
4398 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4399 || *opt.jo_io_name[part] == NUL))
4400 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004401 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004402 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004403 }
4404
4405 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4406 {
4407 buf_T *buf = NULL;
4408
4409 /* check that we can find the buffer before starting the job */
4410 if (opt.jo_set & JO_IN_BUF)
4411 {
4412 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4413 if (buf == NULL)
4414 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4415 }
4416 else if (!(opt.jo_set & JO_IN_NAME))
4417 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004418 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004419 }
4420 else
4421 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4422 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004423 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004424 if (buf->b_ml.ml_mfp == NULL)
4425 {
4426 char_u numbuf[NUMBUFLEN];
4427 char_u *s;
4428
4429 if (opt.jo_set & JO_IN_BUF)
4430 {
4431 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4432 s = numbuf;
4433 }
4434 else
4435 s = opt.jo_io_name[PART_IN];
4436 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004437 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004438 }
4439 job->jv_in_buf = buf;
4440 }
4441
4442 job_set_options(job, &opt);
4443
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004444 if (argvars[0].v_type == VAR_STRING)
4445 {
4446 /* Command is a string. */
4447 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004448 if (cmd == NULL || *cmd == NUL)
4449 {
4450 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004451 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004452 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004453#ifdef USE_ARGV
4454 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004455 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004456 argv[argc] = NULL;
4457#endif
4458 }
4459 else if (argvars[0].v_type != VAR_LIST
4460 || argvars[0].vval.v_list == NULL
4461 || argvars[0].vval.v_list->lv_len < 1)
4462 {
4463 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004464 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004465 }
4466 else
4467 {
4468 list_T *l = argvars[0].vval.v_list;
4469 listitem_T *li;
4470 char_u *s;
4471
4472#ifdef USE_ARGV
4473 /* Pass argv[] to mch_call_shell(). */
4474 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4475 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004476 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004477#endif
4478 for (li = l->lv_first; li != NULL; li = li->li_next)
4479 {
4480 s = get_tv_string_chk(&li->li_tv);
4481 if (s == NULL)
4482 goto theend;
4483#ifdef USE_ARGV
4484 argv[argc++] = (char *)s;
4485#else
4486 /* Only escape when needed, double quotes are not always allowed. */
4487 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4488 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004489# ifdef WIN32
4490 int old_ssl = p_ssl;
4491
4492 /* This is using CreateProcess, not cmd.exe. Always use
4493 * double quote and backslashes. */
4494 p_ssl = 0;
4495# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004496 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004497# ifdef WIN32
4498 p_ssl = old_ssl;
4499# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004500 if (s == NULL)
4501 goto theend;
4502 ga_concat(&ga, s);
4503 vim_free(s);
4504 }
4505 else
4506 ga_concat(&ga, s);
4507 if (li->li_next != NULL)
4508 ga_append(&ga, ' ');
4509#endif
4510 }
4511#ifdef USE_ARGV
4512 argv[argc] = NULL;
4513#else
4514 cmd = ga.ga_data;
4515#endif
4516 }
4517
4518#ifdef USE_ARGV
4519 if (ch_log_active())
4520 {
4521 garray_T ga;
4522 int i;
4523
4524 ga_init2(&ga, (int)sizeof(char), 200);
4525 for (i = 0; i < argc; ++i)
4526 {
4527 if (i > 0)
4528 ga_concat(&ga, (char_u *)" ");
4529 ga_concat(&ga, (char_u *)argv[i]);
4530 }
4531 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4532 ga_clear(&ga);
4533 }
4534 mch_start_job(argv, job, &opt);
4535#else
4536 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4537 mch_start_job((char *)cmd, job, &opt);
4538#endif
4539
4540 /* If the channel is reading from a buffer, write lines now. */
4541 if (job->jv_channel != NULL)
4542 channel_write_in(job->jv_channel);
4543
4544theend:
4545#ifdef USE_ARGV
4546 vim_free(argv);
4547#else
4548 vim_free(ga.ga_data);
4549#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004550 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 return job;
4552}
4553
4554/*
4555 * Get the status of "job" and invoke the exit callback when needed.
4556 * The returned string is not allocated.
4557 */
4558 char *
4559job_status(job_T *job)
4560{
4561 char *result;
4562
4563 if (job->jv_status == JOB_ENDED)
4564 /* No need to check, dead is dead. */
4565 result = "dead";
4566 else if (job->jv_status == JOB_FAILED)
4567 result = "fail";
4568 else
4569 {
4570 result = mch_job_status(job);
4571 if (job->jv_status == JOB_ENDED)
4572 ch_log(job->jv_channel, "Job ended");
4573 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4574 {
4575 typval_T argv[3];
4576 typval_T rettv;
4577 int dummy;
4578
4579 /* invoke the exit callback; make sure the refcount is > 0 */
4580 ++job->jv_refcount;
4581 argv[0].v_type = VAR_JOB;
4582 argv[0].vval.v_job = job;
4583 argv[1].v_type = VAR_NUMBER;
4584 argv[1].vval.v_number = job->jv_exitval;
4585 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004586 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4587 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004588 clear_tv(&rettv);
4589 --job->jv_refcount;
4590 }
4591 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4592 {
4593 /* The job was already unreferenced, now that it ended it can be
4594 * freed. Careful: caller must not use "job" after this! */
4595 job_free(job);
4596 }
4597 }
4598 return result;
4599}
4600
Bram Moolenaar8950a562016-03-12 15:22:55 +01004601/*
4602 * Implementation of job_info().
4603 */
4604 void
4605job_info(job_T *job, dict_T *dict)
4606{
4607 dictitem_T *item;
4608 varnumber_T nr;
4609
4610 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4611
4612 item = dictitem_alloc((char_u *)"channel");
4613 if (item == NULL)
4614 return;
4615 item->di_tv.v_lock = 0;
4616 item->di_tv.v_type = VAR_CHANNEL;
4617 item->di_tv.vval.v_channel = job->jv_channel;
4618 if (job->jv_channel != NULL)
4619 ++job->jv_channel->ch_refcount;
4620 if (dict_add(dict, item) == FAIL)
4621 dictitem_free(item);
4622
4623#ifdef UNIX
4624 nr = job->jv_pid;
4625#else
4626 nr = job->jv_proc_info.dwProcessId;
4627#endif
4628 dict_add_nr_str(dict, "process", nr, NULL);
4629
4630 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004631 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004632 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4633}
4634
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004635 int
4636job_stop(job_T *job, typval_T *argvars)
4637{
4638 char_u *arg;
4639
4640 if (argvars[1].v_type == VAR_UNKNOWN)
4641 arg = (char_u *)"";
4642 else
4643 {
4644 arg = get_tv_string_chk(&argvars[1]);
4645 if (arg == NULL)
4646 {
4647 EMSG(_(e_invarg));
4648 return 0;
4649 }
4650 }
4651 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4652 if (mch_stop_job(job, arg) == FAIL)
4653 return 0;
4654
4655 /* Assume that "hup" does not kill the job. */
4656 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4657 job->jv_channel->ch_job_killed = TRUE;
4658
4659 /* We don't try freeing the job, obviously the caller still has a
4660 * reference to it. */
4661 return 1;
4662}
4663
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004664#endif /* FEAT_JOB_CHANNEL */