blob: c191c2a0ffbeef9385cb3d2cac776a3c2c4c724b [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 Moolenaard8070362016-02-15 21:56:54 +010066#ifdef WIN32
67 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010068fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010069{
70 HANDLE h = (HANDLE)fd;
71 DWORD nread;
72
73 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
74 return -1;
75 return (int)nread;
76}
77
78 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010079fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010080{
81 HANDLE h = (HANDLE)fd;
82 DWORD nwrite;
83
84 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
85 return -1;
86 return (int)nwrite;
87}
88
89 static void
90fd_close(sock_T fd)
91{
92 HANDLE h = (HANDLE)fd;
93
94 CloseHandle(h);
95}
96#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010097
Bram Moolenaar6463ca22016-02-13 17:04:46 +010098/* Log file opened with ch_logfile(). */
99static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100100#ifdef FEAT_RELTIME
101static proftime_T log_start;
102#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100103
104 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100105ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100106{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107 FILE *file = NULL;
108
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100109 if (log_fd != NULL)
110 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100111
112 if (*fname != NUL)
113 {
114 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
115 if (file == NULL)
116 {
117 EMSG2(_(e_notopen), fname);
118 return;
119 }
120 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100121 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100122
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100124 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126#ifdef FEAT_RELTIME
127 profile_start(&log_start);
128#endif
129 }
130}
131
132 int
133ch_log_active()
134{
135 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100136}
137
138 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100139ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100140{
141 if (log_fd != NULL)
142 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100143#ifdef FEAT_RELTIME
144 proftime_T log_now;
145
146 profile_start(&log_now);
147 profile_sub(&log_now, &log_start);
148 fprintf(log_fd, "%s ", profile_msg(&log_now));
149#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100150 if (ch != NULL)
151 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100152 else
153 fprintf(log_fd, "%s: ", what);
154 }
155}
156
Bram Moolenaard0b65022016-03-06 21:50:33 +0100157static int did_log_msg = TRUE;
158
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100159 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100160ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100161{
162 if (log_fd != NULL)
163 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100164 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100165 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100166 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100168 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 }
170}
171
172 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100173ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100174{
175 if (log_fd != NULL)
176 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100177 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100178 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100179 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100181 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 }
183}
184
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100185 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100186ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100187{
188 if (log_fd != NULL)
189 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100190 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100191 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100192 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100194 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 }
196}
197
198 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100199ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100200{
201 if (log_fd != NULL)
202 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100203 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100204 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100205 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100207 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 }
209}
210
211 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100212ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100213{
214 if (log_fd != NULL)
215 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100216 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100217 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100218 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100220 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 }
222}
223
224 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100225ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100226{
227 if (log_fd != NULL)
228 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100229 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100230 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100231 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100233 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 }
235}
236
237 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100238ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100239{
240 if (log_fd != NULL)
241 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100242 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100243 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100244 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100246 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 }
248}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100249
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100250#ifdef _WIN32
251# undef PERROR
252# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
253 (char_u *)msg, (char_u *)strerror_win32(errno))
254
255 static char *
256strerror_win32(int eno)
257{
258 static LPVOID msgbuf = NULL;
259 char_u *ptr;
260
261 if (msgbuf)
262 LocalFree(msgbuf);
263 FormatMessage(
264 FORMAT_MESSAGE_ALLOCATE_BUFFER |
265 FORMAT_MESSAGE_FROM_SYSTEM |
266 FORMAT_MESSAGE_IGNORE_INSERTS,
267 NULL,
268 eno,
269 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
270 (LPTSTR) &msgbuf,
271 0,
272 NULL);
273 /* chomp \r or \n */
274 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
275 switch (*ptr)
276 {
277 case '\r':
278 STRMOVE(ptr, ptr + 1);
279 ptr--;
280 break;
281 case '\n':
282 if (*(ptr + 1) == '\0')
283 *ptr = '\0';
284 else
285 *ptr = ' ';
286 break;
287 }
288 return msgbuf;
289}
290#endif
291
Bram Moolenaar77073442016-02-13 23:23:53 +0100292/*
293 * The list of all allocated channels.
294 */
295static channel_T *first_channel = NULL;
296static int next_ch_id = 0;
297
298/*
299 * Allocate a new channel. The refcount is set to 1.
300 * The channel isn't actually used until it is opened.
301 * Returns NULL if out of memory.
302 */
303 channel_T *
304add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100305{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100306 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100307 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100308
Bram Moolenaar77073442016-02-13 23:23:53 +0100309 if (channel == NULL)
310 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100311
Bram Moolenaar77073442016-02-13 23:23:53 +0100312 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100313 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100314
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100315 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100316 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100318#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#endif
321#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100322 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100323#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100324 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100325 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100326
Bram Moolenaar77073442016-02-13 23:23:53 +0100327 if (first_channel != NULL)
328 {
329 first_channel->ch_prev = channel;
330 channel->ch_next = first_channel;
331 }
332 first_channel = channel;
333
334 channel->ch_refcount = 1;
335 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100336}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100337
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100338/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100339 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100340 * Return TRUE if "channel" has a callback and the associated job wasn't
341 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100342 */
343 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100344channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100345{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100346 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100347 int has_out_msg;
348 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100349
350 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100351 if (channel->ch_job_killed && channel->ch_job == NULL)
352 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100353
354 /* If there is a close callback it may still need to be invoked. */
355 if (channel->ch_close_cb != NULL)
356 return TRUE;
357
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200358 /* If reading from or a buffer it's still useful. */
359 if (channel->ch_part[PART_IN].ch_buffer != NULL)
360 return TRUE;
361
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100362 /* If there is no callback then nobody can get readahead. If the fd is
363 * closed and there is no readahead then the callback won't be called. */
364 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
365 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
366 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100367 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
368 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
369 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
370 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
371 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
372 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100373 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100374 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200375 || ((channel->ch_part[PART_OUT].ch_callback != NULL
376 || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg)
377 || ((channel->ch_part[PART_ERR].ch_callback != NULL
378 || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100379}
380
381/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200382 * Close a channel and free all its resources.
383 */
384 static void
385channel_free_contents(channel_T *channel)
386{
387 channel_close(channel, TRUE);
388 channel_clear(channel);
389 ch_log(channel, "Freeing channel");
390}
391
392 static void
393channel_free_channel(channel_T *channel)
394{
395 if (channel->ch_next != NULL)
396 channel->ch_next->ch_prev = channel->ch_prev;
397 if (channel->ch_prev == NULL)
398 first_channel = channel->ch_next;
399 else
400 channel->ch_prev->ch_next = channel->ch_next;
401 vim_free(channel);
402}
403
404 static void
405channel_free(channel_T *channel)
406{
407 if (!in_free_unref_items)
408 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200409 if (safe_to_invoke_callback == 0)
410 {
411 channel->ch_to_be_freed = TRUE;
412 }
413 else
414 {
415 channel_free_contents(channel);
416 channel_free_channel(channel);
417 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200418 }
419}
420
421/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100422 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100423 * possible, there is no callback to be invoked or the associated job was
424 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100425 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100426 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100427 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100428channel_may_free(channel_T *channel)
429{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100430 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100431 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100432 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 return TRUE;
434 }
435 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436}
437
438/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100439 * Decrement the reference count on "channel" and maybe free it when it goes
440 * down to zero. Don't free it if there is a pending action.
441 * Returns TRUE when the channel is no longer referenced.
442 */
443 int
444channel_unref(channel_T *channel)
445{
446 if (channel != NULL && --channel->ch_refcount <= 0)
447 return channel_may_free(channel);
448 return FALSE;
449}
450
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200451 int
452free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100453{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200454 int did_free = FALSE;
455 channel_T *ch;
456
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200457 /* This is invoked from the garbage collector, which only runs at a safe
458 * point. */
459 ++safe_to_invoke_callback;
460
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200461 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200462 if (!channel_still_useful(ch)
463 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 {
465 /* Free the channel and ordinary items it contains, but don't
466 * recurse into Lists, Dictionaries etc. */
467 channel_free_contents(ch);
468 did_free = TRUE;
469 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200470
471 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200472 return did_free;
473}
474
475 void
476free_unused_channels(int copyID, int mask)
477{
478 channel_T *ch;
479 channel_T *ch_next;
480
481 for (ch = first_channel; ch != NULL; ch = ch_next)
482 {
483 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200484 if (!channel_still_useful(ch)
485 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200486 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200487 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200488 channel_free_channel(ch);
489 }
490 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100491}
492
Bram Moolenaard04a0202016-01-26 23:30:18 +0100493#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100494
495#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
496 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100497channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100498{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100499 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100500 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100501
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100502 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100503 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100505 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100506 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100507}
508#endif
509
Bram Moolenaare0874f82016-01-24 20:36:41 +0100510/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100511 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513#ifdef FEAT_GUI_X11
514 static void
515messageFromNetbeans(XtPointer clientData,
516 int *unused1 UNUSED,
517 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100519 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100521#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100524# if GTK_CHECK_VERSION(3,0,0)
525 static gboolean
526messageFromNetbeans(GIOChannel *unused1 UNUSED,
527 GIOCondition unused2 UNUSED,
528 gpointer clientData)
529{
530 channel_read_fd(GPOINTER_TO_INT(clientData));
531 return TRUE; /* Return FALSE instead in case the event source is to
532 * be removed after this function returns. */
533}
534# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535 static void
536messageFromNetbeans(gpointer clientData,
537 gint unused1 UNUSED,
538 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100539{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100540 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100541}
Bram Moolenaar98921892016-02-23 17:14:37 +0100542# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100543#endif
544
545 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100546channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100547{
Bram Moolenaarde279892016-03-11 22:19:44 +0100548 if (!CH_HAS_GUI)
549 return;
550
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100551# ifdef FEAT_GUI_X11
552 /* Tell notifier we are interested in being called
553 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100554 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
555 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100556 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100557 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100558 (XtPointer)(XtInputReadMask + XtInputExceptMask),
559 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100560 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100561# else
562# ifdef FEAT_GUI_GTK
563 /* Tell gdk we are interested in being called when there
564 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100565 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100566# if GTK_CHECK_VERSION(3,0,0)
567 {
568 GIOChannel *chnnl = g_io_channel_unix_new(
569 (gint)channel->ch_part[part].ch_fd);
570
571 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
572 chnnl,
573 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
574 messageFromNetbeans,
575 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
576
577 g_io_channel_unref(chnnl);
578 }
579# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100580 channel->ch_part[part].ch_inputHandler = gdk_input_add(
581 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100582 (GdkInputCondition)
583 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100584 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100585 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100586# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100587# endif
588# endif
589}
590
Bram Moolenaarde279892016-03-11 22:19:44 +0100591 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100592channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100593{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100594 if (channel->CH_SOCK_FD != INVALID_FD)
595 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100596 if (channel->CH_OUT_FD != INVALID_FD)
597 channel_gui_register_one(channel, PART_OUT);
598 if (channel->CH_ERR_FD != INVALID_FD)
599 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100600}
601
602/*
603 * Register any of our file descriptors with the GUI event handling system.
604 * Called when the GUI has started.
605 */
606 void
607channel_gui_register_all(void)
608{
Bram Moolenaar77073442016-02-13 23:23:53 +0100609 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100610
Bram Moolenaar77073442016-02-13 23:23:53 +0100611 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100612 channel_gui_register(channel);
613}
614
615 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100616channel_gui_unregister_one(channel_T *channel, int part)
617{
618# ifdef FEAT_GUI_X11
619 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
620 {
621 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
622 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
623 }
624# else
625# ifdef FEAT_GUI_GTK
626 if (channel->ch_part[part].ch_inputHandler != 0)
627 {
628# if GTK_CHECK_VERSION(3,0,0)
629 g_source_remove(channel->ch_part[part].ch_inputHandler);
630# else
631 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
632# endif
633 channel->ch_part[part].ch_inputHandler = 0;
634 }
635# endif
636# endif
637}
638
639 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100640channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100641{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100642 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100643
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100644 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100645 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646}
647
648#endif
649
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100650static char *e_cannot_connect = N_("E902: Cannot connect to port");
651
Bram Moolenaard04a0202016-01-26 23:30:18 +0100652/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100653 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100654 * "waittime" is the time in msec to wait for the connection.
655 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100656 * Returns the channel for success.
657 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100659 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100660channel_open(
661 char *hostname,
662 int port_in,
663 int waittime,
664 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100665{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100666 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100668 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100669#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100670 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100671 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100672#else
673 int port = port_in;
674#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100675 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100676 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100677
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100678#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 channel_init_winsock();
680#endif
681
Bram Moolenaar77073442016-02-13 23:23:53 +0100682 channel = add_channel();
683 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100684 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100685 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100686 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100687 }
688
689 /* Get the server internet address and put into addr structure */
690 /* fill in the socket address structure and connect to server */
691 vim_memset((char *)&server, 0, sizeof(server));
692 server.sin_family = AF_INET;
693 server.sin_port = htons(port);
694 if ((host = gethostbyname(hostname)) == NULL)
695 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100696 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100697 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100698 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100699 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100700 }
701 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
702
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100703 /* On Mac and Solaris a zero timeout almost never works. At least wait
704 * one millisecond. Let's do it for all systems, because we don't know why
705 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100706 if (waittime == 0)
707 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708
709 /*
710 * For Unix we need to call connect() again after connect() failed.
711 * On Win32 one time is sufficient.
712 */
713 while (TRUE)
714 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100715 long elapsed_msec = 0;
716 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100717
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100718 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100719 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 sd = socket(AF_INET, SOCK_STREAM, 0);
721 if (sd == -1)
722 {
723 ch_error(channel, "in socket() in channel_open().");
724 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100725 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100726 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100727 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100728
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100729 if (waittime >= 0)
730 {
731 /* Make connect() non-blocking. */
732 if (
733#ifdef _WIN32
734 ioctlsocket(sd, FIONBIO, &val) < 0
735#else
736 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
737#endif
738 )
739 {
740 SOCK_ERRNO;
741 ch_errorn(channel,
742 "channel_open: Connect failed with errno %d", errno);
743 sock_close(sd);
744 channel_free(channel);
745 return NULL;
746 }
747 }
748
749 /* Try connecting to the server. */
750 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
751 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
752
Bram Moolenaar045a2842016-03-08 22:33:07 +0100753 if (ret == 0)
754 /* The connection could be established. */
755 break;
756
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100757 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100758 if (waittime < 0 || (errno != EWOULDBLOCK
759 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100760#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100761 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100762#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763 ))
764 {
765 ch_errorn(channel,
766 "channel_open: Connect failed with errno %d", errno);
767 PERROR(_(e_cannot_connect));
768 sock_close(sd);
769 channel_free(channel);
770 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100771 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100772
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100773 /* Limit the waittime to 50 msec. If it doesn't work within this
774 * time we close the socket and try creating it again. */
775 waitnow = waittime > 50 ? 50 : waittime;
776
Bram Moolenaar045a2842016-03-08 22:33:07 +0100777 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100778 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100779#ifndef WIN32
780 if (errno != ECONNREFUSED)
781#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100782 {
783 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100784 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100785 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100786#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100787 int so_error = 0;
788 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 struct timeval start_tv;
790 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100791#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100792 FD_ZERO(&rfds);
793 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100794 FD_ZERO(&wfds);
795 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100796
Bram Moolenaar562ca712016-03-09 21:50:05 +0100797 tv.tv_sec = waitnow / 1000;
798 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100799#ifndef WIN32
800 gettimeofday(&start_tv, NULL);
801#endif
802 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100803 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100804 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100805
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 if (ret < 0)
807 {
808 SOCK_ERRNO;
809 ch_errorn(channel,
810 "channel_open: Connect failed with errno %d", errno);
811 PERROR(_(e_cannot_connect));
812 sock_close(sd);
813 channel_free(channel);
814 return NULL;
815 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100816
Bram Moolenaare081e212016-02-28 22:33:46 +0100817#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100818 /* On Win32: select() is expected to work and wait for up to
819 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100820 if (FD_ISSET(sd, &wfds))
821 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100822 elapsed_msec = waitnow;
823 if (waittime > 1 && elapsed_msec < waittime)
824 {
825 waittime -= elapsed_msec;
826 continue;
827 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100828#else
829 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100830 * After putting the socket in non-blocking mode, connect() will
831 * return EINPROGRESS, select() will not wait (as if writing is
832 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100833 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100834 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100835 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100836 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100837 {
838 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100839 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100840 if (ret < 0 || (so_error != 0
841 && so_error != EWOULDBLOCK
842 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100843# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100844 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100845# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100846 ))
847 {
848 ch_errorn(channel,
849 "channel_open: Connect failed with errno %d",
850 so_error);
851 PERROR(_(e_cannot_connect));
852 sock_close(sd);
853 channel_free(channel);
854 return NULL;
855 }
856 }
857
Bram Moolenaar045a2842016-03-08 22:33:07 +0100858 if (FD_ISSET(sd, &wfds) && so_error == 0)
859 /* Did not detect an error, connection is established. */
860 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100861
Bram Moolenaar045a2842016-03-08 22:33:07 +0100862 gettimeofday(&end_tv, NULL);
863 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
864 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100865#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100866 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100867
868#ifndef WIN32
869 if (waittime > 1 && elapsed_msec < waittime)
870 {
871 /* The port isn't ready but we also didn't get an error.
872 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100873 * yet. Select() may return early, wait until the remaining
874 * "waitnow" and try again. */
875 waitnow -= elapsed_msec;
876 waittime -= elapsed_msec;
877 if (waitnow > 0)
878 {
879 mch_delay((long)waitnow, TRUE);
880 ui_breakcheck();
881 waittime -= waitnow;
882 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100883 if (!got_int)
884 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100885 if (waittime <= 0)
886 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100887 waittime = 1;
888 continue;
889 }
890 /* we were interrupted, behave as if timed out */
891 }
892#endif
893
894 /* We timed out. */
895 ch_error(channel, "Connection timed out");
896 sock_close(sd);
897 channel_free(channel);
898 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100899 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100900
Bram Moolenaar045a2842016-03-08 22:33:07 +0100901 ch_log(channel, "Connection made");
902
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100903 if (waittime >= 0)
904 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100905#ifdef _WIN32
906 val = 0;
907 ioctlsocket(sd, FIONBIO, &val);
908#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100909 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100910#endif
911 }
912
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100913 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100914 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100915 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
916 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100917
918#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100919 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100920#endif
921
Bram Moolenaar77073442016-02-13 23:23:53 +0100922 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100923}
924
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100925/*
926 * Implements ch_open().
927 */
928 channel_T *
929channel_open_func(typval_T *argvars)
930{
931 char_u *address;
932 char_u *p;
933 char *rest;
934 int port;
935 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200936 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100937
938 address = get_tv_string(&argvars[0]);
939 if (argvars[1].v_type != VAR_UNKNOWN
940 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
941 {
942 EMSG(_(e_invarg));
943 return NULL;
944 }
945
946 /* parse address */
947 p = vim_strchr(address, ':');
948 if (p == NULL)
949 {
950 EMSG2(_(e_invarg2), address);
951 return NULL;
952 }
953 *p++ = NUL;
954 port = strtol((char *)p, &rest, 10);
955 if (*address == NUL || port <= 0 || *rest != NUL)
956 {
957 p[-1] = ':';
958 EMSG2(_(e_invarg2), address);
959 return NULL;
960 }
961
962 /* parse options */
963 clear_job_options(&opt);
964 opt.jo_mode = MODE_JSON;
965 opt.jo_timeout = 2000;
966 if (get_job_options(&argvars[1], &opt,
967 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200968 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100969 if (opt.jo_timeout < 0)
970 {
971 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200972 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100973 }
974
975 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
976 if (channel != NULL)
977 {
978 opt.jo_set = JO_ALL;
979 channel_set_options(channel, &opt);
980 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200981theend:
982 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100983 return channel;
984}
985
Bram Moolenaarde279892016-03-11 22:19:44 +0100986 static void
987may_close_part(sock_T *fd)
988{
989 if (*fd != INVALID_FD)
990 {
991 fd_close(*fd);
992 *fd = INVALID_FD;
993 }
994}
995
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100996 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100997channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100998{
Bram Moolenaarde279892016-03-11 22:19:44 +0100999 if (in != INVALID_FD)
1000 {
1001 may_close_part(&channel->CH_IN_FD);
1002 channel->CH_IN_FD = in;
1003 }
1004 if (out != INVALID_FD)
1005 {
1006# if defined(FEAT_GUI)
1007 channel_gui_unregister_one(channel, PART_OUT);
1008# endif
1009 may_close_part(&channel->CH_OUT_FD);
1010 channel->CH_OUT_FD = out;
1011# if defined(FEAT_GUI)
1012 channel_gui_register_one(channel, PART_OUT);
1013# endif
1014 }
1015 if (err != INVALID_FD)
1016 {
1017# if defined(FEAT_GUI)
1018 channel_gui_unregister_one(channel, PART_ERR);
1019# endif
1020 may_close_part(&channel->CH_ERR_FD);
1021 channel->CH_ERR_FD = err;
1022# if defined(FEAT_GUI)
1023 channel_gui_register_one(channel, PART_ERR);
1024# endif
1025 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001026}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001027
Bram Moolenaard6051b52016-02-28 15:49:03 +01001028/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001029 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001030 * This does not keep a refcount, when the job is freed ch_job is cleared.
1031 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001032 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001033channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001034{
Bram Moolenaar77073442016-02-13 23:23:53 +01001035 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001036
1037 channel_set_options(channel, options);
1038
1039 if (job->jv_in_buf != NULL)
1040 {
1041 chanpart_T *in_part = &channel->ch_part[PART_IN];
1042
1043 in_part->ch_buffer = job->jv_in_buf;
1044 ch_logs(channel, "reading from buffer '%s'",
1045 (char *)in_part->ch_buffer->b_ffname);
1046 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001047 {
1048 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1049 {
1050 /* Special mode: send last-but-one line when appending a line
1051 * to the buffer. */
1052 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001053 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001054 in_part->ch_buf_top =
1055 in_part->ch_buffer->b_ml.ml_line_count + 1;
1056 }
1057 else
1058 in_part->ch_buf_top = options->jo_in_top;
1059 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001060 else
1061 in_part->ch_buf_top = 1;
1062 if (options->jo_set & JO_IN_BOT)
1063 in_part->ch_buf_bot = options->jo_in_bot;
1064 else
1065 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1066 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001067}
1068
1069/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001070 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001071 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001072 */
1073 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001074find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001075{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001076 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001077 buf_T *save_curbuf = curbuf;
1078
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001079 if (name != NULL && *name != NUL)
1080 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001081 if (buf == NULL)
1082 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001083 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001084 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001085 if (buf == NULL)
1086 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001087 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001088 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001089#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001090 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1091 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001092#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001093 if (curbuf->b_ml.ml_mfp == NULL)
1094 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001095 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1096 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001097 changed_bytes(1, 0);
1098 curbuf = save_curbuf;
1099 }
1100
1101 return buf;
1102}
1103
1104/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001105 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001106 */
1107 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001108channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001109{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001110 int part;
1111 char_u **cbp;
1112 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001113
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001115 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001116 channel->ch_part[part].ch_mode = opt->jo_mode;
1117 if (opt->jo_set & JO_IN_MODE)
1118 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1119 if (opt->jo_set & JO_OUT_MODE)
1120 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1121 if (opt->jo_set & JO_ERR_MODE)
1122 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001123
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001124 if (opt->jo_set & JO_TIMEOUT)
1125 for (part = PART_SOCK; part <= PART_IN; ++part)
1126 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1127 if (opt->jo_set & JO_OUT_TIMEOUT)
1128 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1129 if (opt->jo_set & JO_ERR_TIMEOUT)
1130 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001131 if (opt->jo_set & JO_BLOCK_WRITE)
1132 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001133
1134 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001135 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001136 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001137 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001138 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001139 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1141 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001142 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001144 *pp = opt->jo_partial;
1145 if (*pp != NULL)
1146 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001147 }
1148 if (opt->jo_set & JO_OUT_CALLBACK)
1149 {
1150 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001151 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001152 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001153 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001154 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1155 *cbp = vim_strsave(opt->jo_out_cb);
1156 else
1157 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001158 *pp = opt->jo_out_partial;
1159 if (*pp != NULL)
1160 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001161 }
1162 if (opt->jo_set & JO_ERR_CALLBACK)
1163 {
1164 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001165 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001166 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001167 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001168 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1169 *cbp = vim_strsave(opt->jo_err_cb);
1170 else
1171 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001172 *pp = opt->jo_err_partial;
1173 if (*pp != NULL)
1174 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001175 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001176 if (opt->jo_set & JO_CLOSE_CALLBACK)
1177 {
1178 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001179 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001180 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001181 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001182 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1183 *cbp = vim_strsave(opt->jo_close_cb);
1184 else
1185 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001186 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001187 if (*pp != NULL)
1188 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001189 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001190
1191 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1192 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001193 buf_T *buf;
1194
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001195 /* writing output to a buffer. Default mode is NL. */
1196 if (!(opt->jo_set & JO_OUT_MODE))
1197 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001198 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001199 {
1200 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1201 if (buf == NULL)
1202 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1203 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001204 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001205 {
1206 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1207 }
1208 if (buf != NULL)
1209 {
1210 ch_logs(channel, "writing out to buffer '%s'",
1211 (char *)buf->b_ffname);
1212 channel->ch_part[PART_OUT].ch_buffer = buf;
1213 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001214 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001215
1216 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1217 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1218 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1219 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001220 buf_T *buf;
1221
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001222 /* writing err to a buffer. Default mode is NL. */
1223 if (!(opt->jo_set & JO_ERR_MODE))
1224 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1225 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001226 buf = channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001227 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001228 {
1229 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1230 if (buf == NULL)
1231 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1232 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001233 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001234 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1235 if (buf != NULL)
1236 {
1237 ch_logs(channel, "writing err to buffer '%s'",
1238 (char *)buf->b_ffname);
1239 channel->ch_part[PART_ERR].ch_buffer = buf;
1240 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001241 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001242
1243 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1244 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1245 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001246}
1247
1248/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001249 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001250 */
1251 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001252channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001253 channel_T *channel,
1254 int part,
1255 char_u *callback,
1256 partial_T *partial,
1257 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001258{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001259 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001260 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1261
1262 if (item != NULL)
1263 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001264 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001265 item->cq_partial = partial;
1266 if (partial != NULL)
1267 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001268 item->cq_seq_nr = id;
1269 item->cq_prev = head->cq_prev;
1270 head->cq_prev = item;
1271 item->cq_next = NULL;
1272 if (item->cq_prev == NULL)
1273 head->cq_next = item;
1274 else
1275 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001276 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001277}
1278
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001279 static void
1280write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1281{
1282 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001283 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001284 char_u *p;
1285
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001286 if ((p = alloc(len + 2)) == NULL)
1287 return;
1288 STRCPY(p, line);
1289 p[len] = NL;
1290 p[len + 1] = NUL;
1291 channel_send(channel, PART_IN, p, "write_buf_line()");
1292 vim_free(p);
1293}
1294
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001295/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001296 * Return TRUE if "channel" can be written to.
1297 * Returns FALSE if the input is closed or the write would block.
1298 */
1299 static int
1300can_write_buf_line(channel_T *channel)
1301{
1302 chanpart_T *in_part = &channel->ch_part[PART_IN];
1303
1304 if (in_part->ch_fd == INVALID_FD)
1305 return FALSE; /* pipe was closed */
1306
1307 /* for testing: block every other attempt to write */
1308 if (in_part->ch_block_write == 1)
1309 in_part->ch_block_write = -1;
1310 else if (in_part->ch_block_write == -1)
1311 in_part->ch_block_write = 1;
1312
1313 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1314#ifndef WIN32
1315 {
1316# if defined(HAVE_SELECT)
1317 struct timeval tval;
1318 fd_set wfds;
1319 int ret;
1320
1321 FD_ZERO(&wfds);
1322 FD_SET((int)in_part->ch_fd, &wfds);
1323 tval.tv_sec = 0;
1324 tval.tv_usec = 0;
1325 for (;;)
1326 {
1327 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1328# ifdef EINTR
1329 SOCK_ERRNO;
1330 if (ret == -1 && errno == EINTR)
1331 continue;
1332# endif
1333 if (ret <= 0 || in_part->ch_block_write == 1)
1334 {
1335 if (ret > 0)
1336 ch_log(channel, "FAKED Input not ready for writing");
1337 else
1338 ch_log(channel, "Input not ready for writing");
1339 return FALSE;
1340 }
1341 break;
1342 }
1343# else
1344 struct pollfd fds;
1345
1346 fds.fd = in_part->ch_fd;
1347 fds.events = POLLOUT;
1348 if (poll(&fds, 1, 0) <= 0)
1349 {
1350 ch_log(channel, "Input not ready for writing");
1351 return FALSE;
1352 }
1353 if (in_part->ch_block_write == 1)
1354 {
1355 ch_log(channel, "FAKED Input not ready for writing");
1356 return FALSE;
1357 }
1358# endif
1359 }
1360#endif
1361 return TRUE;
1362}
1363
1364/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001365 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001366 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001367 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001368channel_write_in(channel_T *channel)
1369{
1370 chanpart_T *in_part = &channel->ch_part[PART_IN];
1371 linenr_T lnum;
1372 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001373 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001374
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001375 if (buf == NULL || in_part->ch_buf_append)
1376 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001377 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1378 {
1379 /* buffer was wiped out or unloaded */
1380 in_part->ch_buffer = NULL;
1381 return;
1382 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001383
1384 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1385 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1386 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001387 if (!can_write_buf_line(channel))
1388 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001389 write_buf_line(buf, lnum, channel);
1390 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001391 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001392
1393 if (written == 1)
1394 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1395 else if (written > 1)
1396 ch_logn(channel, "written %d lines to channel", written);
1397
Bram Moolenaar014069a2016-03-03 22:51:40 +01001398 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001399 if (lnum > buf->b_ml.ml_line_count)
1400 {
1401 /* Writing is done, no longer need the buffer. */
1402 in_part->ch_buffer = NULL;
1403 ch_log(channel, "Finished writing all lines to channel");
1404 }
1405 else
1406 ch_logn(channel, "Still %d more lines to write",
1407 buf->b_ml.ml_line_count - lnum + 1);
1408}
1409
1410/*
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001411 * Handle buffer "buf" beeing freed, remove it from any channels.
1412 */
1413 void
1414channel_buffer_free(buf_T *buf)
1415{
1416 channel_T *channel;
1417 int part;
1418
1419 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1420 for (part = PART_SOCK; part <= PART_IN; ++part)
1421 {
1422 chanpart_T *ch_part = &channel->ch_part[part];
1423
1424 if (ch_part->ch_buffer == buf)
1425 ch_part->ch_buffer = NULL;
1426 }
1427}
1428
1429/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001430 * Write any lines waiting to be written to a channel.
1431 */
1432 void
1433channel_write_any_lines()
1434{
1435 channel_T *channel;
1436
1437 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1438 {
1439 chanpart_T *in_part = &channel->ch_part[PART_IN];
1440
1441 if (in_part->ch_buffer != NULL)
1442 {
1443 if (in_part->ch_buf_append)
1444 channel_write_new_lines(in_part->ch_buffer);
1445 else
1446 channel_write_in(channel);
1447 }
1448 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001449}
1450
1451/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001452 * Write appended lines above the last one in "buf" to the channel.
1453 */
1454 void
1455channel_write_new_lines(buf_T *buf)
1456{
1457 channel_T *channel;
1458 int found_one = FALSE;
1459
1460 /* There could be more than one channel for the buffer, loop over all of
1461 * them. */
1462 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1463 {
1464 chanpart_T *in_part = &channel->ch_part[PART_IN];
1465 linenr_T lnum;
1466 int written = 0;
1467
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001469 {
1470 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001471 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001472 found_one = TRUE;
1473 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1474 ++lnum)
1475 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001476 if (!can_write_buf_line(channel))
1477 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001478 write_buf_line(buf, lnum, channel);
1479 ++written;
1480 }
1481
1482 if (written == 1)
1483 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1484 else if (written > 1)
1485 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001486 if (lnum < buf->b_ml.ml_line_count)
1487 ch_logn(channel, "Still %d more lines to write",
1488 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001489
1490 in_part->ch_buf_bot = lnum;
1491 }
1492 }
1493 if (!found_one)
1494 buf->b_write_to_channel = FALSE;
1495}
1496
1497/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001498 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001499 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001500 */
1501 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001502invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1503 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001504{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001505 typval_T rettv;
1506 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001507
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001508 if (safe_to_invoke_callback == 0)
1509 EMSG("INTERNAL: Invoking callback when it is not safe");
1510
Bram Moolenaar77073442016-02-13 23:23:53 +01001511 argv[0].v_type = VAR_CHANNEL;
1512 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001513
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001514 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001515 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001516 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001517 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001518}
1519
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001520/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001521 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001522 * The caller must free it.
1523 * Returns NULL if there is nothing.
1524 */
1525 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001526channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001527{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001528 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001529 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001530 char_u *p;
1531
Bram Moolenaar77073442016-02-13 23:23:53 +01001532 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001533 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001534 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 p = node->rq_buffer;
1536 head->rq_next = node->rq_next;
1537 if (node->rq_next == NULL)
1538 head->rq_prev = NULL;
1539 else
1540 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001541 vim_free(node);
1542 return p;
1543}
1544
1545/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001546 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001547 */
1548 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001550{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001551 readq_T *head = &channel->ch_part[part].ch_head;
1552 readq_T *node = head->rq_next;
1553 long_u len = 1;
1554 char_u *res;
1555 char_u *p;
1556
1557 /* If there is only one buffer just get that one. */
1558 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1559 return channel_get(channel, part);
1560
1561 /* Concatenate everything into one buffer. */
1562 for (node = head->rq_next; node != NULL; node = node->rq_next)
1563 len += (long_u)STRLEN(node->rq_buffer);
1564 res = lalloc(len, TRUE);
1565 if (res == NULL)
1566 return NULL;
1567 *res = NUL;
1568 for (node = head->rq_next; node != NULL; node = node->rq_next)
1569 STRCAT(res, node->rq_buffer);
1570
1571 /* Free all buffers */
1572 do
1573 {
1574 p = channel_get(channel, part);
1575 vim_free(p);
1576 } while (p != NULL);
1577
1578 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001579}
1580
1581/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001582 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001583 * Returns FAIL if that is not possible.
1584 */
1585 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001586channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001587{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001588 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001589 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001590 char_u *p;
1591
Bram Moolenaar77073442016-02-13 23:23:53 +01001592 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001593 return FAIL;
1594
Bram Moolenaar77073442016-02-13 23:23:53 +01001595 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1596 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001597 if (p == NULL)
1598 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001599 STRCPY(p, node->rq_buffer);
1600 STRCAT(p, node->rq_next->rq_buffer);
1601 vim_free(node->rq_next->rq_buffer);
1602 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001603
Bram Moolenaar77073442016-02-13 23:23:53 +01001604 /* dispose of the node and its buffer */
1605 head->rq_next = node->rq_next;
1606 head->rq_next->rq_prev = NULL;
1607 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001608 vim_free(node);
1609 return OK;
1610}
1611
1612/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001613 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001614 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001615 * Returns OK or FAIL.
1616 */
1617 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001618channel_save(channel_T *channel, int part, char_u *buf, int len,
1619 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001620{
1621 readq_T *node;
1622 readq_T *head = &channel->ch_part[part].ch_head;
1623 char_u *p;
1624 int i;
1625
1626 node = (readq_T *)alloc(sizeof(readq_T));
1627 if (node == NULL)
1628 return FAIL; /* out of memory */
1629 node->rq_buffer = alloc(len + 1);
1630 if (node->rq_buffer == NULL)
1631 {
1632 vim_free(node);
1633 return FAIL; /* out of memory */
1634 }
1635
1636 if (channel->ch_part[part].ch_mode == MODE_NL)
1637 {
1638 /* Drop any CR before a NL. */
1639 p = node->rq_buffer;
1640 for (i = 0; i < len; ++i)
1641 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1642 *p++ = buf[i];
1643 *p = NUL;
1644 }
1645 else
1646 {
1647 mch_memmove(node->rq_buffer, buf, len);
1648 node->rq_buffer[len] = NUL;
1649 }
1650
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001651 if (prepend)
1652 {
1653 /* preend node to the head of the queue */
1654 node->rq_next = head->rq_next;
1655 node->rq_prev = NULL;
1656 if (head->rq_next == NULL)
1657 head->rq_prev = node;
1658 else
1659 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001660 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001661 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001662 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001663 {
1664 /* append node to the tail of the queue */
1665 node->rq_next = NULL;
1666 node->rq_prev = head->rq_prev;
1667 if (head->rq_prev == NULL)
1668 head->rq_next = node;
1669 else
1670 head->rq_prev->rq_next = node;
1671 head->rq_prev = node;
1672 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001673
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001674 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001675 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001676 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001677 fprintf(log_fd, "'");
1678 if (fwrite(buf, len, 1, log_fd) != 1)
1679 return FAIL;
1680 fprintf(log_fd, "'\n");
1681 }
1682 return OK;
1683}
1684
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001685 static int
1686channel_fill(js_read_T *reader)
1687{
1688 channel_T *channel = (channel_T *)reader->js_cookie;
1689 int part = reader->js_cookie_arg;
1690 char_u *next = channel_get(channel, part);
1691 int unused;
1692 int len;
1693 char_u *p;
1694
1695 if (next == NULL)
1696 return FALSE;
1697
1698 unused = reader->js_end - reader->js_buf - reader->js_used;
1699 if (unused > 0)
1700 {
1701 /* Prepend unused text. */
1702 len = (int)STRLEN(next);
1703 p = alloc(unused + len + 1);
1704 if (p == NULL)
1705 {
1706 vim_free(next);
1707 return FALSE;
1708 }
1709 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1710 mch_memmove(p + unused, next, len + 1);
1711 vim_free(next);
1712 next = p;
1713 }
1714
1715 vim_free(reader->js_buf);
1716 reader->js_buf = next;
1717 reader->js_used = 0;
1718 return TRUE;
1719}
1720
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001721/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001722 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001723 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001724 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001725 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001726 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001727channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001728{
1729 js_read_T reader;
1730 typval_T listtv;
1731 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001732 chanpart_T *chanpart = &channel->ch_part[part];
1733 jsonq_T *head = &chanpart->ch_json_head;
1734 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001735 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001736
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001737 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001738 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001739
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001740 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001741 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001742 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001743 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001744 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001745
1746 /* When a message is incomplete we wait for a short while for more to
1747 * arrive. After the delay drop the input, otherwise a truncated string
1748 * or list will make us hang. */
1749 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001750 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001751 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001752 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001753 /* Only accept the response when it is a list with at least two
1754 * items. */
1755 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001756 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001757 if (listtv.v_type != VAR_LIST)
1758 ch_error(channel, "Did not receive a list, discarding");
1759 else
1760 ch_errorn(channel, "Expected list with two items, got %d",
1761 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001762 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001763 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001764 else
1765 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001766 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1767 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001768 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001769 else
1770 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001771 item->jq_value = alloc_tv();
1772 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001773 {
1774 vim_free(item);
1775 clear_tv(&listtv);
1776 }
1777 else
1778 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001779 *item->jq_value = listtv;
1780 item->jq_prev = head->jq_prev;
1781 head->jq_prev = item;
1782 item->jq_next = NULL;
1783 if (item->jq_prev == NULL)
1784 head->jq_next = item;
1785 else
1786 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001787 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001788 }
1789 }
1790 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001791
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001792 if (status == OK)
1793 chanpart->ch_waiting = FALSE;
1794 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001795 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001796 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001797 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001798 /* First time encountering incomplete message, set a deadline of
1799 * 100 msec. */
1800 ch_log(channel, "Incomplete message - wait for more");
1801 reader.js_used = 0;
1802 chanpart->ch_waiting = TRUE;
1803#ifdef WIN32
1804 chanpart->ch_deadline = GetTickCount() + 100L;
1805#else
1806 gettimeofday(&chanpart->ch_deadline, NULL);
1807 chanpart->ch_deadline.tv_usec += 100 * 1000;
1808 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1809 {
1810 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1811 ++chanpart->ch_deadline.tv_sec;
1812 }
1813#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001814 }
1815 else
1816 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001817 int timeout;
1818#ifdef WIN32
1819 timeout = GetTickCount() > chanpart->ch_deadline;
1820#else
1821 {
1822 struct timeval now_tv;
1823
1824 gettimeofday(&now_tv, NULL);
1825 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1826 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1827 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1828 }
1829#endif
1830 if (timeout)
1831 {
1832 status = FAIL;
1833 chanpart->ch_waiting = FALSE;
1834 }
1835 else
1836 {
1837 reader.js_used = 0;
1838 ch_log(channel, "still waiting on incomplete message");
1839 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001840 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001841 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001842
1843 if (status == FAIL)
1844 {
1845 ch_error(channel, "Decoding failed - discarding input");
1846 ret = FALSE;
1847 chanpart->ch_waiting = FALSE;
1848 }
1849 else if (reader.js_buf[reader.js_used] != NUL)
1850 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001851 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001852 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001853 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1854 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001855 ret = status == MAYBE ? FALSE: TRUE;
1856 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001857 else
1858 ret = FALSE;
1859
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001860 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001861 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862}
1863
1864/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001865 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001866 */
1867 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001868remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001869{
Bram Moolenaar77073442016-02-13 23:23:53 +01001870 if (node->cq_prev == NULL)
1871 head->cq_next = node->cq_next;
1872 else
1873 node->cq_prev->cq_next = node->cq_next;
1874 if (node->cq_next == NULL)
1875 head->cq_prev = node->cq_prev;
1876 else
1877 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001878}
1879
1880/*
1881 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001882 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001883 */
1884 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001885remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001886{
Bram Moolenaar77073442016-02-13 23:23:53 +01001887 if (node->jq_prev == NULL)
1888 head->jq_next = node->jq_next;
1889 else
1890 node->jq_prev->jq_next = node->jq_next;
1891 if (node->jq_next == NULL)
1892 head->jq_prev = node->jq_prev;
1893 else
1894 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001895 vim_free(node);
1896}
1897
1898/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001899 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001900 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001901 * When "id" is zero or negative jut get the first message. But not the one
1902 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001903 * Return OK when found and return the value in "rettv".
1904 * Return FAIL otherwise.
1905 */
1906 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001907channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001908{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001909 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001910 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001911
Bram Moolenaar77073442016-02-13 23:23:53 +01001912 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001913 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001914 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001915 typval_T *tv = &l->lv_first->li_tv;
1916
1917 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001918 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001919 || tv->vval.v_number == 0
1920 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001921 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001922 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001923 if (tv->v_type == VAR_NUMBER)
1924 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001925 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001926 return OK;
1927 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001928 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001929 }
1930 return FAIL;
1931}
1932
Bram Moolenaarece61b02016-02-20 21:39:05 +01001933#define CH_JSON_MAX_ARGS 4
1934
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001935/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001936 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001937 * "argv[0]" is the command string.
1938 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001939 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001940 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001941channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001942{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001943 char_u *cmd = argv[0].vval.v_string;
1944 char_u *arg;
1945 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001946
Bram Moolenaarece61b02016-02-20 21:39:05 +01001947 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001948 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001949 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001950 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001951 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001952 return;
1953 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001954 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001955 if (arg == NULL)
1956 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001957
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001958 if (STRCMP(cmd, "ex") == 0)
1959 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001960 int save_called_emsg = called_emsg;
1961
1962 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001963 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001964 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001965 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001966 --emsg_silent;
1967 if (called_emsg)
1968 ch_logs(channel, "Ex command error: '%s'",
1969 (char *)get_vim_var_str(VV_ERRMSG));
1970 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001971 }
1972 else if (STRCMP(cmd, "normal") == 0)
1973 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001974 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001975
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001976 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001977 ea.arg = arg;
1978 ea.addr_count = 0;
1979 ea.forceit = TRUE; /* no mapping */
1980 ex_normal(&ea);
1981 }
1982 else if (STRCMP(cmd, "redraw") == 0)
1983 {
1984 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001985
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001986 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001987 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001988 ex_redraw(&ea);
1989 showruler(FALSE);
1990 setcursor();
1991 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001992#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001993 if (gui.in_use)
1994 {
1995 gui_update_cursor(FALSE, FALSE);
1996 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001997 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001998#endif
1999 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002000 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002001 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002002 int is_call = cmd[0] == 'c';
2003 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002004
Bram Moolenaarece61b02016-02-20 21:39:05 +01002005 if (argv[id_idx].v_type != VAR_UNKNOWN
2006 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002008 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002009 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002010 EMSG("E904: last argument for expr/call must be a number");
2011 }
2012 else if (is_call && argv[2].v_type != VAR_LIST)
2013 {
2014 ch_error(channel, "third argument for call must be a list");
2015 if (p_verbose > 2)
2016 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002017 }
2018 else
2019 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002020 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002021 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002022 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002023 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002024
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002025 /* Don't pollute the display with errors. */
2026 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002027 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002028 {
2029 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002030 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002031 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002032 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002033 {
2034 ch_logs(channel, "Calling '%s'", (char *)arg);
2035 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2036 tv = &res_tv;
2037 else
2038 tv = NULL;
2039 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002040
2041 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002042 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002043 int id = argv[id_idx].vval.v_number;
2044
Bram Moolenaar55fab432016-02-07 16:53:13 +01002045 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002046 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002047 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002048 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002049 /* If evaluation failed or the result can't be encoded
2050 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002051 vim_free(json);
2052 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002053 err_tv.v_type = VAR_STRING;
2054 err_tv.vval.v_string = (char_u *)"ERROR";
2055 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002056 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002057 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002058 if (json != NULL)
2059 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002060 channel_send(channel,
2061 part == PART_SOCK ? PART_SOCK : PART_IN,
2062 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002063 vim_free(json);
2064 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002065 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002066 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002067 if (tv == &res_tv)
2068 clear_tv(tv);
2069 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002070 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002071 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002072 }
2073 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002074 {
2075 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002076 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002077 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002078}
2079
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002080/*
2081 * Invoke the callback at "cbhead".
2082 * Does not redraw but sets channel_need_redraw.
2083 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002084 static void
2085invoke_one_time_callback(
2086 channel_T *channel,
2087 cbq_T *cbhead,
2088 cbq_T *item,
2089 typval_T *argv)
2090{
2091 ch_logs(channel, "Invoking one-time callback %s",
2092 (char *)item->cq_callback);
2093 /* Remove the item from the list first, if the callback
2094 * invokes ch_close() the list will be cleared. */
2095 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002096 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002097 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002098 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002099 vim_free(item);
2100}
2101
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002102 static void
2103append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2104{
2105 buf_T *save_curbuf = curbuf;
2106 linenr_T lnum = buffer->b_ml.ml_line_count;
2107 int save_write_to = buffer->b_write_to_channel;
2108
2109 /* If the buffer is also used as input insert above the last
2110 * line. Don't write these lines. */
2111 if (save_write_to)
2112 {
2113 --lnum;
2114 buffer->b_write_to_channel = FALSE;
2115 }
2116
2117 /* Append to the buffer */
2118 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2119
2120 curbuf = buffer;
2121 u_sync(TRUE);
2122 /* ignore undo failure, undo is not very useful here */
2123 ignored = u_save(lnum, lnum + 1);
2124
2125 ml_append(lnum, msg, 0, FALSE);
2126 appended_lines_mark(lnum, 1L);
2127 curbuf = save_curbuf;
2128
2129 if (buffer->b_nwindows > 0)
2130 {
2131 win_T *wp;
2132 win_T *save_curwin;
2133
2134 FOR_ALL_WINDOWS(wp)
2135 {
2136 if (wp->w_buffer == buffer
2137 && (save_write_to
2138 ? wp->w_cursor.lnum == lnum + 1
2139 : (wp->w_cursor.lnum == lnum
2140 && wp->w_cursor.col == 0)))
2141 {
2142 ++wp->w_cursor.lnum;
2143 save_curwin = curwin;
2144 curwin = wp;
2145 curbuf = curwin->w_buffer;
2146 scroll_cursor_bot(0, FALSE);
2147 curwin = save_curwin;
2148 curbuf = curwin->w_buffer;
2149 }
2150 }
2151 redraw_buf_later(buffer, VALID);
2152 channel_need_redraw = TRUE;
2153 }
2154
2155 if (save_write_to)
2156 {
2157 channel_T *ch;
2158
2159 /* Find channels reading from this buffer and adjust their
2160 * next-to-read line number. */
2161 buffer->b_write_to_channel = TRUE;
2162 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2163 {
2164 chanpart_T *in_part = &ch->ch_part[PART_IN];
2165
2166 if (in_part->ch_buffer == buffer)
2167 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2168 }
2169 }
2170}
2171
Bram Moolenaar437905c2016-04-26 19:01:05 +02002172 static void
2173drop_messages(channel_T *channel, int part)
2174{
2175 char_u *msg;
2176
2177 while ((msg = channel_get(channel, part)) != NULL)
2178 {
2179 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2180 vim_free(msg);
2181 }
2182}
2183
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002184/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002185 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002186 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002187 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002188 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002189 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002190may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002191{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002192 char_u *msg = NULL;
2193 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002194 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002195 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002196 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002197 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002198 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002199 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002200 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002201 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002202
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002203 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002204 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002205 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002206
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002207 /* Use a message-specific callback, part callback or channel callback */
2208 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2209 if (cbitem->cq_seq_nr == 0)
2210 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002211 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002212 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002213 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002214 partial = cbitem->cq_partial;
2215 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002216 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002217 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002218 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002219 partial = channel->ch_part[part].ch_partial;
2220 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002221 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002222 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002223 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002224 partial = channel->ch_partial;
2225 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002226
Bram Moolenaar187db502016-02-27 14:44:26 +01002227 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002228 if (buffer != NULL && !buf_valid(buffer))
2229 {
2230 /* buffer was wiped out */
2231 channel->ch_part[part].ch_buffer = NULL;
2232 buffer = NULL;
2233 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002234
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002235 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002236 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002237 listitem_T *item;
2238 int argc = 0;
2239
Bram Moolenaard7ece102016-02-02 23:23:02 +01002240 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002241 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002242 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002243 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002244 channel_parse_json(channel, part);
2245 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002246 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002247 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002248
Bram Moolenaarece61b02016-02-20 21:39:05 +01002249 for (item = listtv->vval.v_list->lv_first;
2250 item != NULL && argc < CH_JSON_MAX_ARGS;
2251 item = item->li_next)
2252 argv[argc++] = item->li_tv;
2253 while (argc < CH_JSON_MAX_ARGS)
2254 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002255
Bram Moolenaarece61b02016-02-20 21:39:05 +01002256 if (argv[0].v_type == VAR_STRING)
2257 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002258 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002259 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002260 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002261 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002262 }
2263
Bram Moolenaarece61b02016-02-20 21:39:05 +01002264 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002265 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002266 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002267 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002268 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002269 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002270 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002271 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002272 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002273 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002274 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002275 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002276 return FALSE;
2277 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002278 else
2279 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002280 /* If there is no callback or buffer drop the message. */
2281 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002282 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002283 /* If there is a close callback it may use ch_read() to get the
2284 * messages. */
2285 if (channel->ch_close_cb == NULL)
2286 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002287 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002288 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002289
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002290 if (ch_mode == MODE_NL)
2291 {
2292 char_u *nl;
2293 char_u *buf;
2294
2295 /* See if we have a message ending in NL in the first buffer. If
2296 * not try to concatenate the first and the second buffer. */
2297 while (TRUE)
2298 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002299 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002300 nl = vim_strchr(buf, NL);
2301 if (nl != NULL)
2302 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002303 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002304 return FALSE; /* incomplete message */
2305 }
2306 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002307 {
2308 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002309 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002310 *nl = NUL;
2311 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002312 else
2313 {
2314 /* Copy the message into allocated memory and remove it from
2315 * the buffer. */
2316 msg = vim_strnsave(buf, (int)(nl - buf));
2317 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2318 }
2319 }
2320 else
2321 /* For a raw channel we don't know where the message ends, just
2322 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002323 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002324
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002325 if (msg == NULL)
2326 return FALSE; /* out of memory (and avoids Coverity warning) */
2327
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002328 argv[1].v_type = VAR_STRING;
2329 argv[1].vval.v_string = msg;
2330 }
2331
Bram Moolenaara07fec92016-02-05 21:04:08 +01002332 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002333 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002334 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002335
2336 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002337 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002338 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002339 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002340 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002341 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002342 break;
2343 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002344 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002345 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002346 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002347 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002348 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002349 if (buffer != NULL)
2350 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002351 if (msg == NULL)
2352 /* JSON or JS mode: re-encode the message. */
2353 msg = json_encode(listtv, ch_mode);
2354 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002355 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002356 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002357
Bram Moolenaar187db502016-02-27 14:44:26 +01002358 if (callback != NULL)
2359 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002360 if (cbitem != NULL)
2361 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2362 else
2363 {
2364 /* invoke the channel callback */
2365 ch_logs(channel, "Invoking channel callback %s",
2366 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002367 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002368 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002369 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002370 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002371 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002372 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002373
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002374 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002375 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002376 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002377
2378 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002379}
2380
2381/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002382 * Return TRUE when channel "channel" is open for writing to.
2383 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002384 */
2385 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002386channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002387{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002388 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002389 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002390}
2391
2392/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002393 * Return TRUE when channel "channel" is open for reading or writing.
2394 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002395 */
2396 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002397channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002398{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002399 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002400 || channel->CH_IN_FD != INVALID_FD
2401 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002402 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002403}
2404
2405/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002406 * Return TRUE if "channel" has JSON or other typeahead.
2407 */
2408 static int
2409channel_has_readahead(channel_T *channel, int part)
2410{
2411 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2412
2413 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2414 {
2415 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2416 jsonq_T *item = head->jq_next;
2417
2418 return item != NULL;
2419 }
2420 return channel_peek(channel, part) != NULL;
2421}
2422
2423/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002424 * Return a string indicating the status of the channel.
2425 */
2426 char *
2427channel_status(channel_T *channel)
2428{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002429 int part;
2430 int has_readahead = FALSE;
2431
Bram Moolenaar77073442016-02-13 23:23:53 +01002432 if (channel == NULL)
2433 return "fail";
2434 if (channel_is_open(channel))
2435 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002436 for (part = PART_SOCK; part <= PART_ERR; ++part)
2437 if (channel_has_readahead(channel, part))
2438 {
2439 has_readahead = TRUE;
2440 break;
2441 }
2442
2443 if (has_readahead)
2444 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002445 return "closed";
2446}
2447
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002448 static void
2449channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2450{
2451 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002452 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002453 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002454 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002455
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002456 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002457 STRCAT(namebuf, "_");
2458 tail = STRLEN(namebuf);
2459
2460 STRCPY(namebuf + tail, "status");
2461 dict_add_nr_str(dict, namebuf, 0,
2462 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2463
2464 STRCPY(namebuf + tail, "mode");
2465 switch (chanpart->ch_mode)
2466 {
2467 case MODE_NL: s = "NL"; break;
2468 case MODE_RAW: s = "RAW"; break;
2469 case MODE_JSON: s = "JSON"; break;
2470 case MODE_JS: s = "JS"; break;
2471 }
2472 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2473
2474 STRCPY(namebuf + tail, "io");
2475 if (part == PART_SOCK)
2476 s = "socket";
2477 else switch (chanpart->ch_io)
2478 {
2479 case JIO_NULL: s = "null"; break;
2480 case JIO_PIPE: s = "pipe"; break;
2481 case JIO_FILE: s = "file"; break;
2482 case JIO_BUFFER: s = "buffer"; break;
2483 case JIO_OUT: s = "out"; break;
2484 }
2485 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2486
2487 STRCPY(namebuf + tail, "timeout");
2488 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2489}
2490
2491 void
2492channel_info(channel_T *channel, dict_T *dict)
2493{
2494 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2495 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2496
2497 if (channel->ch_hostname != NULL)
2498 {
2499 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2500 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2501 channel_part_info(channel, dict, "sock", PART_SOCK);
2502 }
2503 else
2504 {
2505 channel_part_info(channel, dict, "out", PART_OUT);
2506 channel_part_info(channel, dict, "err", PART_ERR);
2507 channel_part_info(channel, dict, "in", PART_IN);
2508 }
2509}
2510
Bram Moolenaar77073442016-02-13 23:23:53 +01002511/*
2512 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002513 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002514 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002515 */
2516 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002517channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002518{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002519 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002520
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002521#ifdef FEAT_GUI
2522 channel_gui_unregister(channel);
2523#endif
2524
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002525 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002526 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002527 sock_close(channel->CH_SOCK_FD);
2528 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002529 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002530 may_close_part(&channel->CH_IN_FD);
2531 may_close_part(&channel->CH_OUT_FD);
2532 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002533
Bram Moolenaar8b374212016-02-24 20:43:06 +01002534 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002535 {
2536 typval_T argv[1];
2537 typval_T rettv;
2538 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002539 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002540
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002541 /* Invoke callbacks before the close callback, since it's weird to
2542 * first invoke the close callback. Increment the refcount to avoid
2543 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002544 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002545 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002546 for (part = PART_SOCK; part <= PART_ERR; ++part)
2547 while (may_invoke_callback(channel, part))
2548 ;
2549
2550 /* Invoke the close callback, if still set. */
2551 if (channel->ch_close_cb != NULL)
2552 {
2553 ch_logs(channel, "Invoking close callback %s",
2554 (char *)channel->ch_close_cb);
2555 argv[0].v_type = VAR_CHANNEL;
2556 argv[0].vval.v_channel = channel;
2557 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002558 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2559 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002560 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002561 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002562 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002563 --channel->ch_refcount;
2564
2565 /* the callback is only called once */
2566 vim_free(channel->ch_close_cb);
2567 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002568 partial_unref(channel->ch_close_partial);
2569 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002570
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002571 if (channel_need_redraw)
2572 {
2573 channel_need_redraw = FALSE;
2574 redraw_after_callback();
2575 }
2576
Bram Moolenaar437905c2016-04-26 19:01:05 +02002577 /* any remaining messages are useless now */
2578 for (part = PART_SOCK; part <= PART_ERR; ++part)
2579 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002580 }
2581
2582 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002583}
2584
Bram Moolenaard04a0202016-01-26 23:30:18 +01002585/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002586 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002587 * Returns NULL if there is nothing.
2588 */
2589 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002590channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002591{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002592 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002593
Bram Moolenaar77073442016-02-13 23:23:53 +01002594 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002595 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002596 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002597}
2598
2599/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002600 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002601 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002602 static void
2603channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002604{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002605 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2606 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002607
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002608 while (channel_peek(channel, part) != NULL)
2609 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002610
2611 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002612 {
2613 cbq_T *node = cb_head->cq_next;
2614
2615 remove_cb_node(cb_head, node);
2616 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002617 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002618 vim_free(node);
2619 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002620
2621 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002622 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002623 free_tv(json_head->jq_next->jq_value);
2624 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002625 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002626
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002627 vim_free(channel->ch_part[part].ch_callback);
2628 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002629 partial_unref(channel->ch_part[part].ch_partial);
2630 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002631}
2632
2633/*
2634 * Clear all the read buffers on "channel".
2635 */
2636 void
2637channel_clear(channel_T *channel)
2638{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002639 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002640 vim_free(channel->ch_hostname);
2641 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002642 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002643 channel_clear_one(channel, PART_OUT);
2644 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002645 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002646 vim_free(channel->ch_callback);
2647 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002648 partial_unref(channel->ch_partial);
2649 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002650 vim_free(channel->ch_close_cb);
2651 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002652 partial_unref(channel->ch_close_partial);
2653 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002654}
2655
Bram Moolenaar77073442016-02-13 23:23:53 +01002656#if defined(EXITFREE) || defined(PROTO)
2657 void
2658channel_free_all(void)
2659{
2660 channel_T *channel;
2661
Bram Moolenaard6051b52016-02-28 15:49:03 +01002662 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002663 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2664 channel_clear(channel);
2665}
2666#endif
2667
2668
Bram Moolenaar715d2852016-04-30 17:06:31 +02002669/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002670#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002671
2672/* Buffer size for reading incoming messages. */
2673#define MAXMSGSIZE 4096
2674
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002675#if defined(HAVE_SELECT)
2676/*
2677 * Add write fds where we are waiting for writing to be possible.
2678 */
2679 static int
2680channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2681{
2682 int maxfd = maxfd_arg;
2683 channel_T *ch;
2684
2685 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2686 {
2687 chanpart_T *in_part = &ch->ch_part[PART_IN];
2688
2689 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2690 {
2691 FD_SET((int)in_part->ch_fd, wfds);
2692 if ((int)in_part->ch_fd >= maxfd)
2693 maxfd = (int)in_part->ch_fd + 1;
2694 }
2695 }
2696 return maxfd;
2697}
2698#else
2699/*
2700 * Add write fds where we are waiting for writing to be possible.
2701 */
2702 static int
2703channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2704{
2705 int nfd = nfd_in;
2706 channel_T *ch;
2707
2708 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2709 {
2710 chanpart_T *in_part = &ch->ch_part[PART_IN];
2711
2712 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2713 {
2714 in_part->ch_poll_idx = nfd;
2715 fds[nfd].fd = in_part->ch_fd;
2716 fds[nfd].events = POLLOUT;
2717 ++nfd;
2718 }
2719 else
2720 in_part->ch_poll_idx = -1;
2721 }
2722 return nfd;
2723}
2724#endif
2725
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002726typedef enum {
2727 CW_READY,
2728 CW_NOT_READY,
2729 CW_ERROR
2730} channel_wait_result;
2731
Bram Moolenaard04a0202016-01-26 23:30:18 +01002732/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002733 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002734 * Return CW_READY when there is something to read.
2735 * Return CW_NOT_READY when there is nothing to read.
2736 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002737 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002738 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002739channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002740{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002741 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002742 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002743
Bram Moolenaard8070362016-02-15 21:56:54 +01002744# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002745 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002746 {
2747 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002748 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002749 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002750 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002751
2752 /* reading from a pipe, not a socket */
2753 while (TRUE)
2754 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002755 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2756
2757 if (r && nread > 0)
2758 return CW_READY;
2759 if (r == 0)
2760 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002761
2762 /* perhaps write some buffer lines */
2763 channel_write_any_lines();
2764
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002765 sleep_time = deadline - GetTickCount();
2766 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002767 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002768 /* Wait for a little while. Very short at first, up to 10 msec
2769 * after looping a few times. */
2770 if (sleep_time > delay)
2771 sleep_time = delay;
2772 Sleep(sleep_time);
2773 delay = delay * 2;
2774 if (delay > 10)
2775 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002776 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002777 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002778 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002779#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002780 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002781#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002782 struct timeval tval;
2783 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002784 fd_set wfds;
2785 int ret;
2786 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002787
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002788 tval.tv_sec = timeout / 1000;
2789 tval.tv_usec = (timeout % 1000) * 1000;
2790 for (;;)
2791 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002792 FD_ZERO(&rfds);
2793 FD_SET((int)fd, &rfds);
2794
2795 /* Write lines to a pipe when a pipe can be written to. Need to
2796 * set this every time, some buffers may be done. */
2797 maxfd = (int)fd + 1;
2798 FD_ZERO(&wfds);
2799 maxfd = channel_fill_wfds(maxfd, &wfds);
2800
2801 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002802# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002803 SOCK_ERRNO;
2804 if (ret == -1 && errno == EINTR)
2805 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002806# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002807 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002808 {
2809 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002810 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002811 channel_write_any_lines();
2812 continue;
2813 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002814 break;
2815 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002816#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002817 for (;;)
2818 {
2819 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2820 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002821
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002822 fds[0].fd = fd;
2823 fds[0].events = POLLIN;
2824 nfd = channel_fill_poll_write(nfd, fds);
2825 if (poll(fds, nfd, timeout) > 0)
2826 {
2827 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002828 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002829 channel_write_any_lines();
2830 continue;
2831 }
2832 break;
2833 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002834#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002835 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002836 return CW_NOT_READY;
2837}
2838
2839 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002840channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002841{
2842 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002843 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2844 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002845
2846 /* Queue a "DETACH" netbeans message in the command queue in order to
2847 * terminate the netbeans session later. Do not end the session here
2848 * directly as we may be running in the context of a call to
2849 * netbeans_parse_messages():
2850 * netbeans_parse_messages
2851 * -> autocmd triggered while processing the netbeans cmd
2852 * -> ui_breakcheck
2853 * -> gui event loop or select loop
2854 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002855 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002856 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002857 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002858 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002859 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2860
2861 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002862 * died. Don't close the channel right away, it may be the wrong moment
2863 * to invoke callbacks. */
2864 channel->ch_to_be_closed = TRUE;
2865}
2866
2867 static void
2868channel_close_now(channel_T *channel)
2869{
2870 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002871 channel_close(channel, TRUE);
2872 if (channel->ch_nb_close_cb != NULL)
2873 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002874}
2875
2876/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002877 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002878 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002879 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002880 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002881 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002882channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002883{
2884 static char_u *buf = NULL;
2885 int len = 0;
2886 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002887 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002888 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002889
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002890 fd = channel->ch_part[part].ch_fd;
2891 if (fd == INVALID_FD)
2892 {
2893 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002894 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002895 }
2896 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002897
2898 /* Allocate a buffer to read into. */
2899 if (buf == NULL)
2900 {
2901 buf = alloc(MAXMSGSIZE);
2902 if (buf == NULL)
2903 return; /* out of memory! */
2904 }
2905
2906 /* Keep on reading for as long as there is something to read.
2907 * Use select() or poll() to avoid blocking on a message that is exactly
2908 * MAXMSGSIZE long. */
2909 for (;;)
2910 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002911 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002912 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002913 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002914 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002915 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002916 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002917 if (len <= 0)
2918 break; /* error or nothing more to read */
2919
2920 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002921 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002922 readlen += len;
2923 if (len < MAXMSGSIZE)
2924 break; /* did read everything that's available */
2925 }
2926
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002927 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002928 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002929 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002930
2931#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002932 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002933 if (CH_HAS_GUI && gtk_main_level() > 0)
2934 gtk_main_quit();
2935#endif
2936}
2937
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002938/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002939 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002940 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002941 * Returns what was read in allocated memory.
2942 * Returns NULL in case of error or timeout.
2943 */
2944 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002945channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002946{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002947 char_u *buf;
2948 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002949 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002950 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002951 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002952
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002953 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002954 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002955
2956 while (TRUE)
2957 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002958 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002959 if (buf != NULL && (mode == MODE_RAW
2960 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2961 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002962 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002963 continue;
2964
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002965 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002966 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002967 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002968 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002969 {
2970 ch_log(channel, "Timed out");
2971 return NULL;
2972 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002973 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002974 }
2975
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002976 if (mode == MODE_RAW)
2977 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002978 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002979 }
2980 else
2981 {
2982 nl = vim_strchr(buf, NL);
2983 if (nl[1] == NUL)
2984 {
2985 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002986 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002987 *nl = NUL;
2988 }
2989 else
2990 {
2991 /* Copy the message into allocated memory and remove it from the
2992 * buffer. */
2993 msg = vim_strnsave(buf, (int)(nl - buf));
2994 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2995 }
2996 }
2997 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002998 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002999 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003000}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003001
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003002/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003003 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003004 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003005 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003006 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003007 */
3008 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003009channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003010 channel_T *channel,
3011 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003012 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003013 int id,
3014 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003015{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003016 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003017 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003018 int timeout;
3019 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003020
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003021 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003022 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003023 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003024 for (;;)
3025 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003026 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003027
3028 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003029 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003030 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003031 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003032 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003033 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003034
Bram Moolenaard7ece102016-02-02 23:23:02 +01003035 if (!more)
3036 {
3037 /* Handle any other messages in the queue. If done some more
3038 * messages may have arrived. */
3039 if (channel_parse_messages())
3040 continue;
3041
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003042 /* Wait for up to the timeout. If there was an incomplete message
3043 * use the deadline for that. */
3044 timeout = timeout_arg;
3045 if (chanpart->ch_waiting)
3046 {
3047#ifdef WIN32
3048 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3049#else
3050 {
3051 struct timeval now_tv;
3052
3053 gettimeofday(&now_tv, NULL);
3054 timeout = (chanpart->ch_deadline.tv_sec
3055 - now_tv.tv_sec) * 1000
3056 + (chanpart->ch_deadline.tv_usec
3057 - now_tv.tv_usec) / 1000
3058 + 1;
3059 }
3060#endif
3061 if (timeout < 0)
3062 {
3063 /* Something went wrong, channel_parse_json() didn't
3064 * discard message. Cancel waiting. */
3065 chanpart->ch_waiting = FALSE;
3066 timeout = timeout_arg;
3067 }
3068 else if (timeout > timeout_arg)
3069 timeout = timeout_arg;
3070 }
3071 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003072 if (fd == INVALID_FD
3073 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003074 {
3075 if (timeout == timeout_arg)
3076 {
3077 if (fd != INVALID_FD)
3078 ch_log(channel, "Timed out");
3079 break;
3080 }
3081 }
3082 else
3083 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003084 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003085 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003086 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003087 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003088}
3089
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003090/*
3091 * Common for ch_read() and ch_readraw().
3092 */
3093 void
3094common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3095{
3096 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003097 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003098 jobopt_T opt;
3099 int mode;
3100 int timeout;
3101 int id = -1;
3102 typval_T *listtv = NULL;
3103
3104 /* return an empty string by default */
3105 rettv->v_type = VAR_STRING;
3106 rettv->vval.v_string = NULL;
3107
3108 clear_job_options(&opt);
3109 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3110 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003111 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003112
Bram Moolenaar437905c2016-04-26 19:01:05 +02003113 if (opt.jo_set & JO_PART)
3114 part = opt.jo_part;
3115 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003116 if (channel != NULL)
3117 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003118 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003119 part = channel_part_read(channel);
3120 mode = channel_get_mode(channel, part);
3121 timeout = channel_get_timeout(channel, part);
3122 if (opt.jo_set & JO_TIMEOUT)
3123 timeout = opt.jo_timeout;
3124
3125 if (raw || mode == MODE_RAW || mode == MODE_NL)
3126 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3127 else
3128 {
3129 if (opt.jo_set & JO_ID)
3130 id = opt.jo_id;
3131 channel_read_json_block(channel, part, timeout, id, &listtv);
3132 if (listtv != NULL)
3133 {
3134 *rettv = *listtv;
3135 vim_free(listtv);
3136 }
3137 else
3138 {
3139 rettv->v_type = VAR_SPECIAL;
3140 rettv->vval.v_number = VVAL_NONE;
3141 }
3142 }
3143 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003144
3145theend:
3146 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003147}
3148
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003149# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3150 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003151/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003152 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003153 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003154 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003155 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003156channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003157{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003158 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003159 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003160
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003161 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003162 for (channel = first_channel; channel != NULL;
3163 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003164 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003165 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003166 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003167 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003168 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003169 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003170 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003171 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003172 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003173}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003174# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003175
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003176# if defined(WIN32) || defined(PROTO)
3177/*
3178 * Check the channels for anything that is ready to be read.
3179 * The data is put in the read queue.
3180 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003181 void
3182channel_handle_events(void)
3183{
3184 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003185 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003186 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003187
3188 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3189 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003190 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003191 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003192 {
3193 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003194 if (fd != INVALID_FD)
3195 {
3196 int r = channel_wait(channel, fd, 0);
3197
3198 if (r == CW_READY)
3199 channel_read(channel, part, "channel_handle_events");
3200 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003201 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003202 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003203 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003204 }
3205}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003206# endif
3207
Bram Moolenaard04a0202016-01-26 23:30:18 +01003208/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003209 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003210 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003211 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003212 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003213 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003214channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003215{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003216 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003217 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003218 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003219
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003220 fd = channel->ch_part[part].ch_fd;
3221 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003222 {
3223 if (!channel->ch_error && fun != NULL)
3224 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003225 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003226 EMSG2("E630: %s(): write while not connected", fun);
3227 }
3228 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003229 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003230 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003231
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003232 if (log_fd != NULL)
3233 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003234 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003235 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003236 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003237 fprintf(log_fd, "'\n");
3238 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003239 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003240 }
3241
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003242 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003243 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003244 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003245 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003246 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003247 {
3248 if (!channel->ch_error && fun != NULL)
3249 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003250 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003251 EMSG2("E631: %s(): write failed", fun);
3252 }
3253 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003254 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003255 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003256
3257 channel->ch_error = FALSE;
3258 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003259}
3260
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003261/*
3262 * Common for "ch_sendexpr()" and "ch_sendraw()".
3263 * Returns the channel if the caller should read the response.
3264 * Sets "part_read" to the the read fd.
3265 * Otherwise returns NULL.
3266 */
3267 channel_T *
3268send_common(
3269 typval_T *argvars,
3270 char_u *text,
3271 int id,
3272 int eval,
3273 jobopt_T *opt,
3274 char *fun,
3275 int *part_read)
3276{
3277 channel_T *channel;
3278 int part_send;
3279
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003280 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003281 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003282 if (channel == NULL)
3283 return NULL;
3284 part_send = channel_part_send(channel);
3285 *part_read = channel_part_read(channel);
3286
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003287 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3288 return NULL;
3289
3290 /* Set the callback. An empty callback means no callback and not reading
3291 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3292 * allowed. */
3293 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3294 {
3295 if (eval)
3296 {
3297 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3298 return NULL;
3299 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003300 channel_set_req_callback(channel, part_send,
3301 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003302 }
3303
3304 if (channel_send(channel, part_send, text, fun) == OK
3305 && opt->jo_callback == NULL)
3306 return channel;
3307 return NULL;
3308}
3309
3310/*
3311 * common for "ch_evalexpr()" and "ch_sendexpr()"
3312 */
3313 void
3314ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3315{
3316 char_u *text;
3317 typval_T *listtv;
3318 channel_T *channel;
3319 int id;
3320 ch_mode_T ch_mode;
3321 int part_send;
3322 int part_read;
3323 jobopt_T opt;
3324 int timeout;
3325
3326 /* return an empty string by default */
3327 rettv->v_type = VAR_STRING;
3328 rettv->vval.v_string = NULL;
3329
Bram Moolenaar437905c2016-04-26 19:01:05 +02003330 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003331 if (channel == NULL)
3332 return;
3333 part_send = channel_part_send(channel);
3334
3335 ch_mode = channel_get_mode(channel, part_send);
3336 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3337 {
3338 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3339 return;
3340 }
3341
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003342 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003343 text = json_encode_nr_expr(id, &argvars[1],
3344 ch_mode == MODE_JS ? JSON_JS : 0);
3345 if (text == NULL)
3346 return;
3347
3348 channel = send_common(argvars, text, id, eval, &opt,
3349 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3350 vim_free(text);
3351 if (channel != NULL && eval)
3352 {
3353 if (opt.jo_set & JO_TIMEOUT)
3354 timeout = opt.jo_timeout;
3355 else
3356 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003357 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3358 == OK)
3359 {
3360 list_T *list = listtv->vval.v_list;
3361
3362 /* Move the item from the list and then change the type to
3363 * avoid the value being freed. */
3364 *rettv = list->lv_last->li_tv;
3365 list->lv_last->li_tv.v_type = VAR_NUMBER;
3366 free_tv(listtv);
3367 }
3368 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003369 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003370}
3371
3372/*
3373 * common for "ch_evalraw()" and "ch_sendraw()"
3374 */
3375 void
3376ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3377{
3378 char_u buf[NUMBUFLEN];
3379 char_u *text;
3380 channel_T *channel;
3381 int part_read;
3382 jobopt_T opt;
3383 int timeout;
3384
3385 /* return an empty string by default */
3386 rettv->v_type = VAR_STRING;
3387 rettv->vval.v_string = NULL;
3388
3389 text = get_tv_string_buf(&argvars[1], buf);
3390 channel = send_common(argvars, text, 0, eval, &opt,
3391 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3392 if (channel != NULL && eval)
3393 {
3394 if (opt.jo_set & JO_TIMEOUT)
3395 timeout = opt.jo_timeout;
3396 else
3397 timeout = channel_get_timeout(channel, part_read);
3398 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3399 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003400 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003401}
3402
Bram Moolenaard04a0202016-01-26 23:30:18 +01003403# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003404/*
3405 * Add open channels to the poll struct.
3406 * Return the adjusted struct index.
3407 * The type of "fds" is hidden to avoid problems with the function proto.
3408 */
3409 int
3410channel_poll_setup(int nfd_in, void *fds_in)
3411{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003412 int nfd = nfd_in;
3413 channel_T *channel;
3414 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003415 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003416
Bram Moolenaar77073442016-02-13 23:23:53 +01003417 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003418 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003419 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003420 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003421 chanpart_T *ch_part = &channel->ch_part[part];
3422
3423 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003424 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003425 ch_part->ch_poll_idx = nfd;
3426 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003427 fds[nfd].events = POLLIN;
3428 nfd++;
3429 }
3430 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003431 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003432 }
3433 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003434
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003435 nfd = channel_fill_poll_write(nfd, fds);
3436
Bram Moolenaare0874f82016-01-24 20:36:41 +01003437 return nfd;
3438}
3439
3440/*
3441 * The type of "fds" is hidden to avoid problems with the function proto.
3442 */
3443 int
3444channel_poll_check(int ret_in, void *fds_in)
3445{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003446 int ret = ret_in;
3447 channel_T *channel;
3448 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003449 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003450 int idx;
3451 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003452
Bram Moolenaar77073442016-02-13 23:23:53 +01003453 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003454 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003455 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003456 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003457 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003458
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003459 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003460 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003461 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003462 --ret;
3463 }
3464 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003465
3466 in_part = &channel->ch_part[PART_IN];
3467 idx = in_part->ch_poll_idx;
3468 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3469 {
3470 if (in_part->ch_buf_append)
3471 {
3472 if (in_part->ch_buffer != NULL)
3473 channel_write_new_lines(in_part->ch_buffer);
3474 }
3475 else
3476 channel_write_in(channel);
3477 --ret;
3478 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003479 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003480
3481 return ret;
3482}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003483# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003484
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003485# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003486/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003487 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003488 */
3489 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003490channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003491{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003492 int maxfd = maxfd_in;
3493 channel_T *channel;
3494 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003495 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003497
Bram Moolenaar77073442016-02-13 23:23:53 +01003498 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003499 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003500 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003501 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003502 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003503
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003504 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003505 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003506 FD_SET((int)fd, rfds);
3507 if (maxfd < (int)fd)
3508 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003509 }
3510 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003511 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003512
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003513 maxfd = channel_fill_wfds(maxfd, wfds);
3514
Bram Moolenaare0874f82016-01-24 20:36:41 +01003515 return maxfd;
3516}
3517
3518/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003519 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003520 */
3521 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003522channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003523{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003524 int ret = ret_in;
3525 channel_T *channel;
3526 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003527 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003528 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003529 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003530
Bram Moolenaar77073442016-02-13 23:23:53 +01003531 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003532 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003533 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003534 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003535 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003536
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003537 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003538 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003539 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003540 --ret;
3541 }
3542 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003543
3544 in_part = &channel->ch_part[PART_IN];
3545 if (ret > 0 && in_part->ch_fd != INVALID_FD
3546 && FD_ISSET(in_part->ch_fd, wfds))
3547 {
3548 if (in_part->ch_buf_append)
3549 {
3550 if (in_part->ch_buffer != NULL)
3551 channel_write_new_lines(in_part->ch_buffer);
3552 }
3553 else
3554 channel_write_in(channel);
3555 --ret;
3556 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003557 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003558
3559 return ret;
3560}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003561# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003562
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003563/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003564 * Execute queued up commands.
3565 * Invoked from the main loop when it's safe to execute received commands.
3566 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003567 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003568 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003569channel_parse_messages(void)
3570{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003571 channel_T *channel = first_channel;
3572 int ret = FALSE;
3573 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003574 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003575
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003576 ++safe_to_invoke_callback;
3577
Bram Moolenaard0b65022016-03-06 21:50:33 +01003578 /* Only do this message when another message was given, otherwise we get
3579 * lots of them. */
3580 if (did_log_msg)
3581 {
3582 ch_log(NULL, "looking for messages on channels");
3583 did_log_msg = FALSE;
3584 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003585 while (channel != NULL)
3586 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003587 if (channel->ch_to_be_closed)
3588 {
3589 channel->ch_to_be_closed = FALSE;
3590 channel_close_now(channel);
3591 /* channel may have been freed, start over */
3592 channel = first_channel;
3593 continue;
3594 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003595 if (channel->ch_to_be_freed)
3596 {
3597 channel_free(channel);
3598 /* channel has been freed, start over */
3599 channel = first_channel;
3600 continue;
3601 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003602 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003603 {
3604 /* channel is no longer useful, free it */
3605 channel_free(channel);
3606 channel = first_channel;
3607 part = PART_SOCK;
3608 continue;
3609 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003610 if (channel->ch_part[part].ch_fd != INVALID_FD
3611 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003612 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003613 /* Increase the refcount, in case the handler causes the channel
3614 * to be unreferenced or closed. */
3615 ++channel->ch_refcount;
3616 r = may_invoke_callback(channel, part);
3617 if (r == OK)
3618 ret = TRUE;
3619 if (channel_unref(channel) || r == OK)
3620 {
3621 /* channel was freed or something was done, start over */
3622 channel = first_channel;
3623 part = PART_SOCK;
3624 continue;
3625 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003626 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003627 if (part < PART_ERR)
3628 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003629 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003630 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003631 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003632 part = PART_SOCK;
3633 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003634 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003635
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003636 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003637 {
3638 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003639 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003640 }
3641
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003642 --safe_to_invoke_callback;
3643
Bram Moolenaard7ece102016-02-02 23:23:02 +01003644 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003645}
3646
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003647/*
3648 * Mark references to lists used in channels.
3649 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003650 int
3651set_ref_in_channel(int copyID)
3652{
Bram Moolenaar77073442016-02-13 23:23:53 +01003653 int abort = FALSE;
3654 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003655 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003656
Bram Moolenaar77073442016-02-13 23:23:53 +01003657 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003658 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003659 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003660 tv.v_type = VAR_CHANNEL;
3661 tv.vval.v_channel = channel;
3662 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003663 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003664 return abort;
3665}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003666
3667/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003668 * Return the "part" to write to for "channel".
3669 */
3670 int
3671channel_part_send(channel_T *channel)
3672{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003673 if (channel->CH_SOCK_FD == INVALID_FD)
3674 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003675 return PART_SOCK;
3676}
3677
3678/*
3679 * Return the default "part" to read from for "channel".
3680 */
3681 int
3682channel_part_read(channel_T *channel)
3683{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003684 if (channel->CH_SOCK_FD == INVALID_FD)
3685 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003686 return PART_SOCK;
3687}
3688
3689/*
3690 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003691 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003692 */
3693 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003694channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003695{
Bram Moolenaar77073442016-02-13 23:23:53 +01003696 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003697 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003698 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003699}
3700
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003701/*
3702 * Return the timeout of "channel"/"part"
3703 */
3704 int
3705channel_get_timeout(channel_T *channel, int part)
3706{
3707 return channel->ch_part[part].ch_timeout;
3708}
3709
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003710 static int
3711handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3712{
3713 char_u *val = get_tv_string(item);
3714
3715 opt->jo_set |= jo;
3716 if (STRCMP(val, "nl") == 0)
3717 *modep = MODE_NL;
3718 else if (STRCMP(val, "raw") == 0)
3719 *modep = MODE_RAW;
3720 else if (STRCMP(val, "js") == 0)
3721 *modep = MODE_JS;
3722 else if (STRCMP(val, "json") == 0)
3723 *modep = MODE_JSON;
3724 else
3725 {
3726 EMSG2(_(e_invarg2), val);
3727 return FAIL;
3728 }
3729 return OK;
3730}
3731
3732 static int
3733handle_io(typval_T *item, int part, jobopt_T *opt)
3734{
3735 char_u *val = get_tv_string(item);
3736
3737 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3738 if (STRCMP(val, "null") == 0)
3739 opt->jo_io[part] = JIO_NULL;
3740 else if (STRCMP(val, "pipe") == 0)
3741 opt->jo_io[part] = JIO_PIPE;
3742 else if (STRCMP(val, "file") == 0)
3743 opt->jo_io[part] = JIO_FILE;
3744 else if (STRCMP(val, "buffer") == 0)
3745 opt->jo_io[part] = JIO_BUFFER;
3746 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3747 opt->jo_io[part] = JIO_OUT;
3748 else
3749 {
3750 EMSG2(_(e_invarg2), val);
3751 return FAIL;
3752 }
3753 return OK;
3754}
3755
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003756/*
3757 * Clear a jobopt_T before using it.
3758 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003759 void
3760clear_job_options(jobopt_T *opt)
3761{
3762 vim_memset(opt, 0, sizeof(jobopt_T));
3763}
3764
3765/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003766 * Free any members of a jobopt_T.
3767 */
3768 void
3769free_job_options(jobopt_T *opt)
3770{
3771 if (opt->jo_partial != NULL)
3772 partial_unref(opt->jo_partial);
3773 if (opt->jo_out_partial != NULL)
3774 partial_unref(opt->jo_out_partial);
3775 if (opt->jo_err_partial != NULL)
3776 partial_unref(opt->jo_err_partial);
3777 if (opt->jo_close_partial != NULL)
3778 partial_unref(opt->jo_close_partial);
3779}
3780
3781/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003782 * Get the PART_ number from the first character of an option name.
3783 */
3784 static int
3785part_from_char(int c)
3786{
3787 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3788}
3789
3790/*
3791 * Get the option entries from the dict in "tv", parse them and put the result
3792 * in "opt".
3793 * Only accept options in "supported".
3794 * If an option value is invalid return FAIL.
3795 */
3796 int
3797get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3798{
3799 typval_T *item;
3800 char_u *val;
3801 dict_T *dict;
3802 int todo;
3803 hashitem_T *hi;
3804 int part;
3805
3806 opt->jo_set = 0;
3807 if (tv->v_type == VAR_UNKNOWN)
3808 return OK;
3809 if (tv->v_type != VAR_DICT)
3810 {
3811 EMSG(_(e_invarg));
3812 return FAIL;
3813 }
3814 dict = tv->vval.v_dict;
3815 if (dict == NULL)
3816 return OK;
3817
3818 todo = (int)dict->dv_hashtab.ht_used;
3819 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3820 if (!HASHITEM_EMPTY(hi))
3821 {
3822 item = &dict_lookup(hi)->di_tv;
3823
3824 if (STRCMP(hi->hi_key, "mode") == 0)
3825 {
3826 if (!(supported & JO_MODE))
3827 break;
3828 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3829 return FAIL;
3830 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003831 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003832 {
3833 if (!(supported & JO_IN_MODE))
3834 break;
3835 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3836 == FAIL)
3837 return FAIL;
3838 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003839 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003840 {
3841 if (!(supported & JO_OUT_MODE))
3842 break;
3843 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3844 == FAIL)
3845 return FAIL;
3846 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003847 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003848 {
3849 if (!(supported & JO_ERR_MODE))
3850 break;
3851 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3852 == FAIL)
3853 return FAIL;
3854 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003855 else if (STRCMP(hi->hi_key, "in_io") == 0
3856 || STRCMP(hi->hi_key, "out_io") == 0
3857 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003858 {
3859 if (!(supported & JO_OUT_IO))
3860 break;
3861 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3862 return FAIL;
3863 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003864 else if (STRCMP(hi->hi_key, "in_name") == 0
3865 || STRCMP(hi->hi_key, "out_name") == 0
3866 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003867 {
3868 part = part_from_char(*hi->hi_key);
3869
3870 if (!(supported & JO_OUT_IO))
3871 break;
3872 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3873 opt->jo_io_name[part] =
3874 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3875 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003876 else if (STRCMP(hi->hi_key, "in_buf") == 0
3877 || STRCMP(hi->hi_key, "out_buf") == 0
3878 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003879 {
3880 part = part_from_char(*hi->hi_key);
3881
3882 if (!(supported & JO_OUT_IO))
3883 break;
3884 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3885 opt->jo_io_buf[part] = get_tv_number(item);
3886 if (opt->jo_io_buf[part] <= 0)
3887 {
3888 EMSG2(_(e_invarg2), get_tv_string(item));
3889 return FAIL;
3890 }
3891 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3892 {
3893 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3894 return FAIL;
3895 }
3896 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003897 else if (STRCMP(hi->hi_key, "in_top") == 0
3898 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003899 {
3900 linenr_T *lp;
3901
3902 if (!(supported & JO_OUT_IO))
3903 break;
3904 if (hi->hi_key[3] == 't')
3905 {
3906 lp = &opt->jo_in_top;
3907 opt->jo_set |= JO_IN_TOP;
3908 }
3909 else
3910 {
3911 lp = &opt->jo_in_bot;
3912 opt->jo_set |= JO_IN_BOT;
3913 }
3914 *lp = get_tv_number(item);
3915 if (*lp < 0)
3916 {
3917 EMSG2(_(e_invarg2), get_tv_string(item));
3918 return FAIL;
3919 }
3920 }
3921 else if (STRCMP(hi->hi_key, "channel") == 0)
3922 {
3923 if (!(supported & JO_OUT_IO))
3924 break;
3925 opt->jo_set |= JO_CHANNEL;
3926 if (item->v_type != VAR_CHANNEL)
3927 {
3928 EMSG2(_(e_invarg2), "channel");
3929 return FAIL;
3930 }
3931 opt->jo_channel = item->vval.v_channel;
3932 }
3933 else if (STRCMP(hi->hi_key, "callback") == 0)
3934 {
3935 if (!(supported & JO_CALLBACK))
3936 break;
3937 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003938 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003939 if (opt->jo_callback == NULL)
3940 {
3941 EMSG2(_(e_invarg2), "callback");
3942 return FAIL;
3943 }
3944 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003945 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003946 {
3947 if (!(supported & JO_OUT_CALLBACK))
3948 break;
3949 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003950 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003951 if (opt->jo_out_cb == NULL)
3952 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003953 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003954 return FAIL;
3955 }
3956 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003957 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003958 {
3959 if (!(supported & JO_ERR_CALLBACK))
3960 break;
3961 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003962 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003963 if (opt->jo_err_cb == NULL)
3964 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003965 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003966 return FAIL;
3967 }
3968 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003969 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003970 {
3971 if (!(supported & JO_CLOSE_CALLBACK))
3972 break;
3973 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003974 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003975 if (opt->jo_close_cb == NULL)
3976 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003977 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003978 return FAIL;
3979 }
3980 }
3981 else if (STRCMP(hi->hi_key, "waittime") == 0)
3982 {
3983 if (!(supported & JO_WAITTIME))
3984 break;
3985 opt->jo_set |= JO_WAITTIME;
3986 opt->jo_waittime = get_tv_number(item);
3987 }
3988 else if (STRCMP(hi->hi_key, "timeout") == 0)
3989 {
3990 if (!(supported & JO_TIMEOUT))
3991 break;
3992 opt->jo_set |= JO_TIMEOUT;
3993 opt->jo_timeout = get_tv_number(item);
3994 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003995 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003996 {
3997 if (!(supported & JO_OUT_TIMEOUT))
3998 break;
3999 opt->jo_set |= JO_OUT_TIMEOUT;
4000 opt->jo_out_timeout = get_tv_number(item);
4001 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004002 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004003 {
4004 if (!(supported & JO_ERR_TIMEOUT))
4005 break;
4006 opt->jo_set |= JO_ERR_TIMEOUT;
4007 opt->jo_err_timeout = get_tv_number(item);
4008 }
4009 else if (STRCMP(hi->hi_key, "part") == 0)
4010 {
4011 if (!(supported & JO_PART))
4012 break;
4013 opt->jo_set |= JO_PART;
4014 val = get_tv_string(item);
4015 if (STRCMP(val, "err") == 0)
4016 opt->jo_part = PART_ERR;
4017 else
4018 {
4019 EMSG2(_(e_invarg2), val);
4020 return FAIL;
4021 }
4022 }
4023 else if (STRCMP(hi->hi_key, "id") == 0)
4024 {
4025 if (!(supported & JO_ID))
4026 break;
4027 opt->jo_set |= JO_ID;
4028 opt->jo_id = get_tv_number(item);
4029 }
4030 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4031 {
4032 if (!(supported & JO_STOPONEXIT))
4033 break;
4034 opt->jo_set |= JO_STOPONEXIT;
4035 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4036 opt->jo_soe_buf);
4037 if (opt->jo_stoponexit == NULL)
4038 {
4039 EMSG2(_(e_invarg2), "stoponexit");
4040 return FAIL;
4041 }
4042 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004043 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004044 {
4045 if (!(supported & JO_EXIT_CB))
4046 break;
4047 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004048 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4049 {
4050 opt->jo_exit_partial = item->vval.v_partial;
4051 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4052 }
4053 else
4054 opt->jo_exit_cb = get_tv_string_buf_chk(
4055 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004056 if (opt->jo_exit_cb == NULL)
4057 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004058 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004059 return FAIL;
4060 }
4061 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004062 else if (STRCMP(hi->hi_key, "block_write") == 0)
4063 {
4064 if (!(supported & JO_BLOCK_WRITE))
4065 break;
4066 opt->jo_set |= JO_BLOCK_WRITE;
4067 opt->jo_block_write = get_tv_number(item);
4068 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004069 else
4070 break;
4071 --todo;
4072 }
4073 if (todo > 0)
4074 {
4075 EMSG2(_(e_invarg2), hi->hi_key);
4076 return FAIL;
4077 }
4078
4079 return OK;
4080}
4081
4082/*
4083 * Get the channel from the argument.
4084 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004085 * When "check_open" is TRUE check that the channel can be used.
4086 * When "reading" is TRUE "check_open" considers typeahead useful.
4087 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 */
4089 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004090get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004091{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004092 channel_T *channel = NULL;
4093 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004094
4095 if (tv->v_type == VAR_JOB)
4096 {
4097 if (tv->vval.v_job != NULL)
4098 channel = tv->vval.v_job->jv_channel;
4099 }
4100 else if (tv->v_type == VAR_CHANNEL)
4101 {
4102 channel = tv->vval.v_channel;
4103 }
4104 else
4105 {
4106 EMSG2(_(e_invarg2), get_tv_string(tv));
4107 return NULL;
4108 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004109 if (channel != NULL && reading)
4110 has_readahead = channel_has_readahead(channel,
4111 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004112
Bram Moolenaar437905c2016-04-26 19:01:05 +02004113 if (check_open && (channel == NULL || (!channel_is_open(channel)
4114 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004115 {
4116 EMSG(_("E906: not an open channel"));
4117 return NULL;
4118 }
4119 return channel;
4120}
4121
4122static job_T *first_job = NULL;
4123
4124 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004125job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004126{
4127 ch_log(job->jv_channel, "Freeing job");
4128 if (job->jv_channel != NULL)
4129 {
4130 /* The link from the channel to the job doesn't count as a reference,
4131 * thus don't decrement the refcount of the job. The reference from
4132 * the job to the channel does count the refrence, decrement it and
4133 * NULL the reference. We don't set ch_job_killed, unreferencing the
4134 * job doesn't mean it stops running. */
4135 job->jv_channel->ch_job = NULL;
4136 channel_unref(job->jv_channel);
4137 }
4138 mch_clear_job(job);
4139
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004140 vim_free(job->jv_stoponexit);
4141 vim_free(job->jv_exit_cb);
4142 partial_unref(job->jv_exit_partial);
4143}
4144
4145 static void
4146job_free_job(job_T *job)
4147{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004148 if (job->jv_next != NULL)
4149 job->jv_next->jv_prev = job->jv_prev;
4150 if (job->jv_prev == NULL)
4151 first_job = job->jv_next;
4152 else
4153 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004154 vim_free(job);
4155}
4156
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004157 static void
4158job_free(job_T *job)
4159{
4160 if (!in_free_unref_items)
4161 {
4162 job_free_contents(job);
4163 job_free_job(job);
4164 }
4165}
4166
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004167/*
4168 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004169 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4170 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004171 */
4172 static int
4173job_still_useful(job_T *job)
4174{
4175 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004176 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4177 || (job->jv_channel != NULL
4178 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004179}
4180
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004181/*
4182 * Mark references in jobs that are still useful.
4183 */
4184 int
4185set_ref_in_job(int copyID)
4186{
4187 int abort = FALSE;
4188 job_T *job;
4189 typval_T tv;
4190
4191 for (job = first_job; job != NULL; job = job->jv_next)
4192 if (job_still_useful(job))
4193 {
4194 tv.v_type = VAR_JOB;
4195 tv.vval.v_job = job;
4196 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4197 }
4198 return abort;
4199}
4200
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004201 void
4202job_unref(job_T *job)
4203{
4204 if (job != NULL && --job->jv_refcount <= 0)
4205 {
4206 /* Do not free the job when it has not ended yet and there is a
4207 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004208 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004209 {
4210 job_free(job);
4211 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004212 else if (job->jv_channel != NULL
4213 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004214 {
4215 /* Do remove the link to the channel, otherwise it hangs
4216 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004217 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004218 job->jv_channel->ch_job = NULL;
4219 channel_unref(job->jv_channel);
4220 job->jv_channel = NULL;
4221 }
4222 }
4223}
4224
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004225 int
4226free_unused_jobs_contents(int copyID, int mask)
4227{
4228 int did_free = FALSE;
4229 job_T *job;
4230
4231 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004232 if ((job->jv_copyID & mask) != (copyID & mask)
4233 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004234 {
4235 /* Free the channel and ordinary items it contains, but don't
4236 * recurse into Lists, Dictionaries etc. */
4237 job_free_contents(job);
4238 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004239 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004240 return did_free;
4241}
4242
4243 void
4244free_unused_jobs(int copyID, int mask)
4245{
4246 job_T *job;
4247 job_T *job_next;
4248
4249 for (job = first_job; job != NULL; job = job_next)
4250 {
4251 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004252 if ((job->jv_copyID & mask) != (copyID & mask)
4253 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004254 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004255 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004256 job_free_job(job);
4257 }
4258 }
4259}
4260
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004261/*
4262 * Allocate a job. Sets the refcount to one and sets options default.
4263 */
4264 static job_T *
4265job_alloc(void)
4266{
4267 job_T *job;
4268
4269 job = (job_T *)alloc_clear(sizeof(job_T));
4270 if (job != NULL)
4271 {
4272 job->jv_refcount = 1;
4273 job->jv_stoponexit = vim_strsave((char_u *)"term");
4274
4275 if (first_job != NULL)
4276 {
4277 first_job->jv_prev = job;
4278 job->jv_next = first_job;
4279 }
4280 first_job = job;
4281 }
4282 return job;
4283}
4284
4285 void
4286job_set_options(job_T *job, jobopt_T *opt)
4287{
4288 if (opt->jo_set & JO_STOPONEXIT)
4289 {
4290 vim_free(job->jv_stoponexit);
4291 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4292 job->jv_stoponexit = NULL;
4293 else
4294 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4295 }
4296 if (opt->jo_set & JO_EXIT_CB)
4297 {
4298 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004299 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004300 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004301 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004302 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004303 job->jv_exit_partial = NULL;
4304 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004305 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004306 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004307 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004308 job->jv_exit_partial = opt->jo_exit_partial;
4309 if (job->jv_exit_partial != NULL)
4310 ++job->jv_exit_partial->pt_refcount;
4311 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004312 }
4313}
4314
4315/*
4316 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4317 */
4318 void
4319job_stop_on_exit()
4320{
4321 job_T *job;
4322
4323 for (job = first_job; job != NULL; job = job->jv_next)
4324 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4325 mch_stop_job(job, job->jv_stoponexit);
4326}
4327
4328/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004329 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004330 */
4331 void
4332job_check_ended(void)
4333{
4334 static time_t last_check = 0;
4335 time_t now;
4336 job_T *job;
4337 job_T *next;
4338
4339 /* Only do this once in 10 seconds. */
4340 now = time(NULL);
4341 if (last_check + 10 < now)
4342 {
4343 last_check = now;
4344 for (job = first_job; job != NULL; job = next)
4345 {
4346 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004347 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004348 job_status(job); /* may free "job" */
4349 }
4350 }
4351}
4352
4353/*
4354 * "job_start()" function
4355 */
4356 job_T *
4357job_start(typval_T *argvars)
4358{
4359 job_T *job;
4360 char_u *cmd = NULL;
4361#if defined(UNIX)
4362# define USE_ARGV
4363 char **argv = NULL;
4364 int argc = 0;
4365#else
4366 garray_T ga;
4367#endif
4368 jobopt_T opt;
4369 int part;
4370
4371 job = job_alloc();
4372 if (job == NULL)
4373 return NULL;
4374
4375 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004376#ifndef USE_ARGV
4377 ga_init2(&ga, (int)sizeof(char*), 20);
4378#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004379
4380 /* Default mode is NL. */
4381 clear_job_options(&opt);
4382 opt.jo_mode = MODE_NL;
4383 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004384 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4385 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004386 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004387
4388 /* Check that when io is "file" that there is a file name. */
4389 for (part = PART_OUT; part <= PART_IN; ++part)
4390 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4391 && opt.jo_io[part] == JIO_FILE
4392 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4393 || *opt.jo_io_name[part] == NUL))
4394 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004395 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004396 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004397 }
4398
4399 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4400 {
4401 buf_T *buf = NULL;
4402
4403 /* check that we can find the buffer before starting the job */
4404 if (opt.jo_set & JO_IN_BUF)
4405 {
4406 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4407 if (buf == NULL)
4408 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4409 }
4410 else if (!(opt.jo_set & JO_IN_NAME))
4411 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004412 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004413 }
4414 else
4415 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4416 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004417 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004418 if (buf->b_ml.ml_mfp == NULL)
4419 {
4420 char_u numbuf[NUMBUFLEN];
4421 char_u *s;
4422
4423 if (opt.jo_set & JO_IN_BUF)
4424 {
4425 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4426 s = numbuf;
4427 }
4428 else
4429 s = opt.jo_io_name[PART_IN];
4430 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004431 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004432 }
4433 job->jv_in_buf = buf;
4434 }
4435
4436 job_set_options(job, &opt);
4437
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004438 if (argvars[0].v_type == VAR_STRING)
4439 {
4440 /* Command is a string. */
4441 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004442 if (cmd == NULL || *cmd == NUL)
4443 {
4444 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004445 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004446 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004447#ifdef USE_ARGV
4448 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004449 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004450 argv[argc] = NULL;
4451#endif
4452 }
4453 else if (argvars[0].v_type != VAR_LIST
4454 || argvars[0].vval.v_list == NULL
4455 || argvars[0].vval.v_list->lv_len < 1)
4456 {
4457 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004458 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004459 }
4460 else
4461 {
4462 list_T *l = argvars[0].vval.v_list;
4463 listitem_T *li;
4464 char_u *s;
4465
4466#ifdef USE_ARGV
4467 /* Pass argv[] to mch_call_shell(). */
4468 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4469 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004470 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004471#endif
4472 for (li = l->lv_first; li != NULL; li = li->li_next)
4473 {
4474 s = get_tv_string_chk(&li->li_tv);
4475 if (s == NULL)
4476 goto theend;
4477#ifdef USE_ARGV
4478 argv[argc++] = (char *)s;
4479#else
4480 /* Only escape when needed, double quotes are not always allowed. */
4481 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4482 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004483# ifdef WIN32
4484 int old_ssl = p_ssl;
4485
4486 /* This is using CreateProcess, not cmd.exe. Always use
4487 * double quote and backslashes. */
4488 p_ssl = 0;
4489# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004490 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004491# ifdef WIN32
4492 p_ssl = old_ssl;
4493# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494 if (s == NULL)
4495 goto theend;
4496 ga_concat(&ga, s);
4497 vim_free(s);
4498 }
4499 else
4500 ga_concat(&ga, s);
4501 if (li->li_next != NULL)
4502 ga_append(&ga, ' ');
4503#endif
4504 }
4505#ifdef USE_ARGV
4506 argv[argc] = NULL;
4507#else
4508 cmd = ga.ga_data;
4509#endif
4510 }
4511
4512#ifdef USE_ARGV
4513 if (ch_log_active())
4514 {
4515 garray_T ga;
4516 int i;
4517
4518 ga_init2(&ga, (int)sizeof(char), 200);
4519 for (i = 0; i < argc; ++i)
4520 {
4521 if (i > 0)
4522 ga_concat(&ga, (char_u *)" ");
4523 ga_concat(&ga, (char_u *)argv[i]);
4524 }
4525 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4526 ga_clear(&ga);
4527 }
4528 mch_start_job(argv, job, &opt);
4529#else
4530 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4531 mch_start_job((char *)cmd, job, &opt);
4532#endif
4533
4534 /* If the channel is reading from a buffer, write lines now. */
4535 if (job->jv_channel != NULL)
4536 channel_write_in(job->jv_channel);
4537
4538theend:
4539#ifdef USE_ARGV
4540 vim_free(argv);
4541#else
4542 vim_free(ga.ga_data);
4543#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004544 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004545 return job;
4546}
4547
4548/*
4549 * Get the status of "job" and invoke the exit callback when needed.
4550 * The returned string is not allocated.
4551 */
4552 char *
4553job_status(job_T *job)
4554{
4555 char *result;
4556
4557 if (job->jv_status == JOB_ENDED)
4558 /* No need to check, dead is dead. */
4559 result = "dead";
4560 else if (job->jv_status == JOB_FAILED)
4561 result = "fail";
4562 else
4563 {
4564 result = mch_job_status(job);
4565 if (job->jv_status == JOB_ENDED)
4566 ch_log(job->jv_channel, "Job ended");
4567 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4568 {
4569 typval_T argv[3];
4570 typval_T rettv;
4571 int dummy;
4572
4573 /* invoke the exit callback; make sure the refcount is > 0 */
4574 ++job->jv_refcount;
4575 argv[0].v_type = VAR_JOB;
4576 argv[0].vval.v_job = job;
4577 argv[1].v_type = VAR_NUMBER;
4578 argv[1].vval.v_number = job->jv_exitval;
4579 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004580 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4581 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004582 clear_tv(&rettv);
4583 --job->jv_refcount;
4584 }
4585 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4586 {
4587 /* The job was already unreferenced, now that it ended it can be
4588 * freed. Careful: caller must not use "job" after this! */
4589 job_free(job);
4590 }
4591 }
4592 return result;
4593}
4594
Bram Moolenaar8950a562016-03-12 15:22:55 +01004595/*
4596 * Implementation of job_info().
4597 */
4598 void
4599job_info(job_T *job, dict_T *dict)
4600{
4601 dictitem_T *item;
4602 varnumber_T nr;
4603
4604 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4605
4606 item = dictitem_alloc((char_u *)"channel");
4607 if (item == NULL)
4608 return;
4609 item->di_tv.v_lock = 0;
4610 item->di_tv.v_type = VAR_CHANNEL;
4611 item->di_tv.vval.v_channel = job->jv_channel;
4612 if (job->jv_channel != NULL)
4613 ++job->jv_channel->ch_refcount;
4614 if (dict_add(dict, item) == FAIL)
4615 dictitem_free(item);
4616
4617#ifdef UNIX
4618 nr = job->jv_pid;
4619#else
4620 nr = job->jv_proc_info.dwProcessId;
4621#endif
4622 dict_add_nr_str(dict, "process", nr, NULL);
4623
4624 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004625 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004626 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4627}
4628
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004629 int
4630job_stop(job_T *job, typval_T *argvars)
4631{
4632 char_u *arg;
4633
4634 if (argvars[1].v_type == VAR_UNKNOWN)
4635 arg = (char_u *)"";
4636 else
4637 {
4638 arg = get_tv_string_chk(&argvars[1]);
4639 if (arg == NULL)
4640 {
4641 EMSG(_(e_invarg));
4642 return 0;
4643 }
4644 }
4645 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4646 if (mch_stop_job(job, arg) == FAIL)
4647 return 0;
4648
4649 /* Assume that "hup" does not kill the job. */
4650 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4651 job->jv_channel->ch_job_killed = TRUE;
4652
4653 /* We don't try freeing the job, obviously the caller still has a
4654 * reference to it. */
4655 return 1;
4656}
4657
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004658#endif /* FEAT_JOB_CHANNEL */