blob: a7f4c4b8cb0fefc6b3dedc3562480f75b5081061 [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.
1071 */
1072 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001073find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001074{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001075 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001076 buf_T *save_curbuf = curbuf;
1077
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001078 if (name != NULL && *name != NUL)
1079 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001080 if (buf == NULL)
1081 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001082 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001083 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001084 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001085 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001086#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001087 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1088 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001089#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001090 if (curbuf->b_ml.ml_mfp == NULL)
1091 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001092 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1093 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001094 changed_bytes(1, 0);
1095 curbuf = save_curbuf;
1096 }
1097
1098 return buf;
1099}
1100
1101/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001102 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001103 */
1104 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001105channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001106{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001107 int part;
1108 char_u **cbp;
1109 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001110
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001112 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001113 channel->ch_part[part].ch_mode = opt->jo_mode;
1114 if (opt->jo_set & JO_IN_MODE)
1115 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1116 if (opt->jo_set & JO_OUT_MODE)
1117 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1118 if (opt->jo_set & JO_ERR_MODE)
1119 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001120
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001121 if (opt->jo_set & JO_TIMEOUT)
1122 for (part = PART_SOCK; part <= PART_IN; ++part)
1123 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1124 if (opt->jo_set & JO_OUT_TIMEOUT)
1125 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1126 if (opt->jo_set & JO_ERR_TIMEOUT)
1127 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001128 if (opt->jo_set & JO_BLOCK_WRITE)
1129 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001130
1131 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001132 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001133 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001134 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001135 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001136 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001137 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1138 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001139 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001141 *pp = opt->jo_partial;
1142 if (*pp != NULL)
1143 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001144 }
1145 if (opt->jo_set & JO_OUT_CALLBACK)
1146 {
1147 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001148 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001150 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001151 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1152 *cbp = vim_strsave(opt->jo_out_cb);
1153 else
1154 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001155 *pp = opt->jo_out_partial;
1156 if (*pp != NULL)
1157 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001158 }
1159 if (opt->jo_set & JO_ERR_CALLBACK)
1160 {
1161 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001162 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001163 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001164 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001165 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1166 *cbp = vim_strsave(opt->jo_err_cb);
1167 else
1168 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001169 *pp = opt->jo_err_partial;
1170 if (*pp != NULL)
1171 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001172 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001173 if (opt->jo_set & JO_CLOSE_CALLBACK)
1174 {
1175 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001176 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001177 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001178 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001179 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1180 *cbp = vim_strsave(opt->jo_close_cb);
1181 else
1182 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001183 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001184 if (*pp != NULL)
1185 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001186 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001187
1188 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1189 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001190 /* writing output to a buffer. Default mode is NL. */
1191 if (!(opt->jo_set & JO_OUT_MODE))
1192 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001193 if (opt->jo_set & JO_OUT_BUF)
1194 channel->ch_part[PART_OUT].ch_buffer =
1195 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1196 else
1197 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001198 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1199 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001200 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1201 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001202
1203 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1204 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1205 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1206 {
1207 /* writing err to a buffer. Default mode is NL. */
1208 if (!(opt->jo_set & JO_ERR_MODE))
1209 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1210 if (opt->jo_io[PART_ERR] == JIO_OUT)
1211 channel->ch_part[PART_ERR].ch_buffer =
1212 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001213 else if (opt->jo_set & JO_ERR_BUF)
1214 channel->ch_part[PART_ERR].ch_buffer =
1215 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001216 else
1217 channel->ch_part[PART_ERR].ch_buffer =
1218 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1219 ch_logs(channel, "writing err to buffer '%s'",
1220 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1221 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001222
1223 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1224 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1225 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001226}
1227
1228/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001229 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001230 */
1231 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001232channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001233 channel_T *channel,
1234 int part,
1235 char_u *callback,
1236 partial_T *partial,
1237 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001238{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001239 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001240 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1241
1242 if (item != NULL)
1243 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001244 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001245 item->cq_partial = partial;
1246 if (partial != NULL)
1247 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001248 item->cq_seq_nr = id;
1249 item->cq_prev = head->cq_prev;
1250 head->cq_prev = item;
1251 item->cq_next = NULL;
1252 if (item->cq_prev == NULL)
1253 head->cq_next = item;
1254 else
1255 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001256 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001257}
1258
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001259 static void
1260write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1261{
1262 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001263 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001264 char_u *p;
1265
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001266 if ((p = alloc(len + 2)) == NULL)
1267 return;
1268 STRCPY(p, line);
1269 p[len] = NL;
1270 p[len + 1] = NUL;
1271 channel_send(channel, PART_IN, p, "write_buf_line()");
1272 vim_free(p);
1273}
1274
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001275/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001276 * Return TRUE if "channel" can be written to.
1277 * Returns FALSE if the input is closed or the write would block.
1278 */
1279 static int
1280can_write_buf_line(channel_T *channel)
1281{
1282 chanpart_T *in_part = &channel->ch_part[PART_IN];
1283
1284 if (in_part->ch_fd == INVALID_FD)
1285 return FALSE; /* pipe was closed */
1286
1287 /* for testing: block every other attempt to write */
1288 if (in_part->ch_block_write == 1)
1289 in_part->ch_block_write = -1;
1290 else if (in_part->ch_block_write == -1)
1291 in_part->ch_block_write = 1;
1292
1293 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1294#ifndef WIN32
1295 {
1296# if defined(HAVE_SELECT)
1297 struct timeval tval;
1298 fd_set wfds;
1299 int ret;
1300
1301 FD_ZERO(&wfds);
1302 FD_SET((int)in_part->ch_fd, &wfds);
1303 tval.tv_sec = 0;
1304 tval.tv_usec = 0;
1305 for (;;)
1306 {
1307 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1308# ifdef EINTR
1309 SOCK_ERRNO;
1310 if (ret == -1 && errno == EINTR)
1311 continue;
1312# endif
1313 if (ret <= 0 || in_part->ch_block_write == 1)
1314 {
1315 if (ret > 0)
1316 ch_log(channel, "FAKED Input not ready for writing");
1317 else
1318 ch_log(channel, "Input not ready for writing");
1319 return FALSE;
1320 }
1321 break;
1322 }
1323# else
1324 struct pollfd fds;
1325
1326 fds.fd = in_part->ch_fd;
1327 fds.events = POLLOUT;
1328 if (poll(&fds, 1, 0) <= 0)
1329 {
1330 ch_log(channel, "Input not ready for writing");
1331 return FALSE;
1332 }
1333 if (in_part->ch_block_write == 1)
1334 {
1335 ch_log(channel, "FAKED Input not ready for writing");
1336 return FALSE;
1337 }
1338# endif
1339 }
1340#endif
1341 return TRUE;
1342}
1343
1344/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001345 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001346 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001347 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001348channel_write_in(channel_T *channel)
1349{
1350 chanpart_T *in_part = &channel->ch_part[PART_IN];
1351 linenr_T lnum;
1352 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001353 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001354
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001355 if (buf == NULL || in_part->ch_buf_append)
1356 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001357 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1358 {
1359 /* buffer was wiped out or unloaded */
1360 in_part->ch_buffer = NULL;
1361 return;
1362 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001363
1364 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1365 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1366 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001367 if (!can_write_buf_line(channel))
1368 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001369 write_buf_line(buf, lnum, channel);
1370 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001371 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001372
1373 if (written == 1)
1374 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1375 else if (written > 1)
1376 ch_logn(channel, "written %d lines to channel", written);
1377
Bram Moolenaar014069a2016-03-03 22:51:40 +01001378 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001379 if (lnum > buf->b_ml.ml_line_count)
1380 {
1381 /* Writing is done, no longer need the buffer. */
1382 in_part->ch_buffer = NULL;
1383 ch_log(channel, "Finished writing all lines to channel");
1384 }
1385 else
1386 ch_logn(channel, "Still %d more lines to write",
1387 buf->b_ml.ml_line_count - lnum + 1);
1388}
1389
1390/*
1391 * Write any lines waiting to be written to a channel.
1392 */
1393 void
1394channel_write_any_lines()
1395{
1396 channel_T *channel;
1397
1398 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1399 {
1400 chanpart_T *in_part = &channel->ch_part[PART_IN];
1401
1402 if (in_part->ch_buffer != NULL)
1403 {
1404 if (in_part->ch_buf_append)
1405 channel_write_new_lines(in_part->ch_buffer);
1406 else
1407 channel_write_in(channel);
1408 }
1409 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001410}
1411
1412/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001413 * Write appended lines above the last one in "buf" to the channel.
1414 */
1415 void
1416channel_write_new_lines(buf_T *buf)
1417{
1418 channel_T *channel;
1419 int found_one = FALSE;
1420
1421 /* There could be more than one channel for the buffer, loop over all of
1422 * them. */
1423 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1424 {
1425 chanpart_T *in_part = &channel->ch_part[PART_IN];
1426 linenr_T lnum;
1427 int written = 0;
1428
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001429 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001430 {
1431 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001432 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001433 found_one = TRUE;
1434 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1435 ++lnum)
1436 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001437 if (!can_write_buf_line(channel))
1438 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001439 write_buf_line(buf, lnum, channel);
1440 ++written;
1441 }
1442
1443 if (written == 1)
1444 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1445 else if (written > 1)
1446 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001447 if (lnum < buf->b_ml.ml_line_count)
1448 ch_logn(channel, "Still %d more lines to write",
1449 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001450
1451 in_part->ch_buf_bot = lnum;
1452 }
1453 }
1454 if (!found_one)
1455 buf->b_write_to_channel = FALSE;
1456}
1457
1458/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001459 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001460 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001461 */
1462 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001463invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1464 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001465{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001466 typval_T rettv;
1467 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001468
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001469 if (safe_to_invoke_callback == 0)
1470 EMSG("INTERNAL: Invoking callback when it is not safe");
1471
Bram Moolenaar77073442016-02-13 23:23:53 +01001472 argv[0].v_type = VAR_CHANNEL;
1473 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001474
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001475 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001476 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001477 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001478 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001479}
1480
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001481/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001482 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001483 * The caller must free it.
1484 * Returns NULL if there is nothing.
1485 */
1486 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001487channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001488{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001489 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001490 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001491 char_u *p;
1492
Bram Moolenaar77073442016-02-13 23:23:53 +01001493 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001494 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001495 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001496 p = node->rq_buffer;
1497 head->rq_next = node->rq_next;
1498 if (node->rq_next == NULL)
1499 head->rq_prev = NULL;
1500 else
1501 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001502 vim_free(node);
1503 return p;
1504}
1505
1506/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001507 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001508 */
1509 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001510channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001511{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001512 readq_T *head = &channel->ch_part[part].ch_head;
1513 readq_T *node = head->rq_next;
1514 long_u len = 1;
1515 char_u *res;
1516 char_u *p;
1517
1518 /* If there is only one buffer just get that one. */
1519 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1520 return channel_get(channel, part);
1521
1522 /* Concatenate everything into one buffer. */
1523 for (node = head->rq_next; node != NULL; node = node->rq_next)
1524 len += (long_u)STRLEN(node->rq_buffer);
1525 res = lalloc(len, TRUE);
1526 if (res == NULL)
1527 return NULL;
1528 *res = NUL;
1529 for (node = head->rq_next; node != NULL; node = node->rq_next)
1530 STRCAT(res, node->rq_buffer);
1531
1532 /* Free all buffers */
1533 do
1534 {
1535 p = channel_get(channel, part);
1536 vim_free(p);
1537 } while (p != NULL);
1538
1539 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001540}
1541
1542/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001543 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001544 * Returns FAIL if that is not possible.
1545 */
1546 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001547channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001548{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001550 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001551 char_u *p;
1552
Bram Moolenaar77073442016-02-13 23:23:53 +01001553 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001554 return FAIL;
1555
Bram Moolenaar77073442016-02-13 23:23:53 +01001556 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1557 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001558 if (p == NULL)
1559 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001560 STRCPY(p, node->rq_buffer);
1561 STRCAT(p, node->rq_next->rq_buffer);
1562 vim_free(node->rq_next->rq_buffer);
1563 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001564
Bram Moolenaar77073442016-02-13 23:23:53 +01001565 /* dispose of the node and its buffer */
1566 head->rq_next = node->rq_next;
1567 head->rq_next->rq_prev = NULL;
1568 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001569 vim_free(node);
1570 return OK;
1571}
1572
1573/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001574 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001575 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001576 * Returns OK or FAIL.
1577 */
1578 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001579channel_save(channel_T *channel, int part, char_u *buf, int len,
1580 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001581{
1582 readq_T *node;
1583 readq_T *head = &channel->ch_part[part].ch_head;
1584 char_u *p;
1585 int i;
1586
1587 node = (readq_T *)alloc(sizeof(readq_T));
1588 if (node == NULL)
1589 return FAIL; /* out of memory */
1590 node->rq_buffer = alloc(len + 1);
1591 if (node->rq_buffer == NULL)
1592 {
1593 vim_free(node);
1594 return FAIL; /* out of memory */
1595 }
1596
1597 if (channel->ch_part[part].ch_mode == MODE_NL)
1598 {
1599 /* Drop any CR before a NL. */
1600 p = node->rq_buffer;
1601 for (i = 0; i < len; ++i)
1602 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1603 *p++ = buf[i];
1604 *p = NUL;
1605 }
1606 else
1607 {
1608 mch_memmove(node->rq_buffer, buf, len);
1609 node->rq_buffer[len] = NUL;
1610 }
1611
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001612 if (prepend)
1613 {
1614 /* preend node to the head of the queue */
1615 node->rq_next = head->rq_next;
1616 node->rq_prev = NULL;
1617 if (head->rq_next == NULL)
1618 head->rq_prev = node;
1619 else
1620 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001621 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001622 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001623 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001624 {
1625 /* append node to the tail of the queue */
1626 node->rq_next = NULL;
1627 node->rq_prev = head->rq_prev;
1628 if (head->rq_prev == NULL)
1629 head->rq_next = node;
1630 else
1631 head->rq_prev->rq_next = node;
1632 head->rq_prev = node;
1633 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001634
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001635 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001636 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001637 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001638 fprintf(log_fd, "'");
1639 if (fwrite(buf, len, 1, log_fd) != 1)
1640 return FAIL;
1641 fprintf(log_fd, "'\n");
1642 }
1643 return OK;
1644}
1645
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001646 static int
1647channel_fill(js_read_T *reader)
1648{
1649 channel_T *channel = (channel_T *)reader->js_cookie;
1650 int part = reader->js_cookie_arg;
1651 char_u *next = channel_get(channel, part);
1652 int unused;
1653 int len;
1654 char_u *p;
1655
1656 if (next == NULL)
1657 return FALSE;
1658
1659 unused = reader->js_end - reader->js_buf - reader->js_used;
1660 if (unused > 0)
1661 {
1662 /* Prepend unused text. */
1663 len = (int)STRLEN(next);
1664 p = alloc(unused + len + 1);
1665 if (p == NULL)
1666 {
1667 vim_free(next);
1668 return FALSE;
1669 }
1670 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1671 mch_memmove(p + unused, next, len + 1);
1672 vim_free(next);
1673 next = p;
1674 }
1675
1676 vim_free(reader->js_buf);
1677 reader->js_buf = next;
1678 reader->js_used = 0;
1679 return TRUE;
1680}
1681
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001682/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001683 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001684 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001685 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001687 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001688channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689{
1690 js_read_T reader;
1691 typval_T listtv;
1692 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001693 chanpart_T *chanpart = &channel->ch_part[part];
1694 jsonq_T *head = &chanpart->ch_json_head;
1695 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001696 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001698 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001699 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001701 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001703 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001704 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001705 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001706
1707 /* When a message is incomplete we wait for a short while for more to
1708 * arrive. After the delay drop the input, otherwise a truncated string
1709 * or list will make us hang. */
1710 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001711 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001712 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001714 /* Only accept the response when it is a list with at least two
1715 * items. */
1716 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001717 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001718 if (listtv.v_type != VAR_LIST)
1719 ch_error(channel, "Did not receive a list, discarding");
1720 else
1721 ch_errorn(channel, "Expected list with two items, got %d",
1722 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001723 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001724 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001725 else
1726 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001727 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1728 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001730 else
1731 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001732 item->jq_value = alloc_tv();
1733 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001734 {
1735 vim_free(item);
1736 clear_tv(&listtv);
1737 }
1738 else
1739 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001740 *item->jq_value = listtv;
1741 item->jq_prev = head->jq_prev;
1742 head->jq_prev = item;
1743 item->jq_next = NULL;
1744 if (item->jq_prev == NULL)
1745 head->jq_next = item;
1746 else
1747 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001748 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001749 }
1750 }
1751 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001752
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001753 if (status == OK)
1754 chanpart->ch_waiting = FALSE;
1755 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001756 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001757 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001758 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001759 /* First time encountering incomplete message, set a deadline of
1760 * 100 msec. */
1761 ch_log(channel, "Incomplete message - wait for more");
1762 reader.js_used = 0;
1763 chanpart->ch_waiting = TRUE;
1764#ifdef WIN32
1765 chanpart->ch_deadline = GetTickCount() + 100L;
1766#else
1767 gettimeofday(&chanpart->ch_deadline, NULL);
1768 chanpart->ch_deadline.tv_usec += 100 * 1000;
1769 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1770 {
1771 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1772 ++chanpart->ch_deadline.tv_sec;
1773 }
1774#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001775 }
1776 else
1777 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001778 int timeout;
1779#ifdef WIN32
1780 timeout = GetTickCount() > chanpart->ch_deadline;
1781#else
1782 {
1783 struct timeval now_tv;
1784
1785 gettimeofday(&now_tv, NULL);
1786 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1787 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1788 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1789 }
1790#endif
1791 if (timeout)
1792 {
1793 status = FAIL;
1794 chanpart->ch_waiting = FALSE;
1795 }
1796 else
1797 {
1798 reader.js_used = 0;
1799 ch_log(channel, "still waiting on incomplete message");
1800 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001801 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001802 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001803
1804 if (status == FAIL)
1805 {
1806 ch_error(channel, "Decoding failed - discarding input");
1807 ret = FALSE;
1808 chanpart->ch_waiting = FALSE;
1809 }
1810 else if (reader.js_buf[reader.js_used] != NUL)
1811 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001812 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001813 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001814 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1815 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001816 ret = status == MAYBE ? FALSE: TRUE;
1817 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001818 else
1819 ret = FALSE;
1820
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001821 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001822 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001823}
1824
1825/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001826 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001827 */
1828 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001829remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001830{
Bram Moolenaar77073442016-02-13 23:23:53 +01001831 if (node->cq_prev == NULL)
1832 head->cq_next = node->cq_next;
1833 else
1834 node->cq_prev->cq_next = node->cq_next;
1835 if (node->cq_next == NULL)
1836 head->cq_prev = node->cq_prev;
1837 else
1838 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001839}
1840
1841/*
1842 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001843 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844 */
1845 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001846remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847{
Bram Moolenaar77073442016-02-13 23:23:53 +01001848 if (node->jq_prev == NULL)
1849 head->jq_next = node->jq_next;
1850 else
1851 node->jq_prev->jq_next = node->jq_next;
1852 if (node->jq_next == NULL)
1853 head->jq_prev = node->jq_prev;
1854 else
1855 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001856 vim_free(node);
1857}
1858
1859/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001860 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001861 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001862 * When "id" is zero or negative jut get the first message. But not the one
1863 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001864 * Return OK when found and return the value in "rettv".
1865 * Return FAIL otherwise.
1866 */
1867 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001868channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001869{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001871 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001872
Bram Moolenaar77073442016-02-13 23:23:53 +01001873 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001874 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001875 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 typval_T *tv = &l->lv_first->li_tv;
1877
1878 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001879 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001880 || tv->vval.v_number == 0
1881 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001882 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001883 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001884 if (tv->v_type == VAR_NUMBER)
1885 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001886 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001887 return OK;
1888 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001889 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890 }
1891 return FAIL;
1892}
1893
Bram Moolenaarece61b02016-02-20 21:39:05 +01001894#define CH_JSON_MAX_ARGS 4
1895
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001896/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001897 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001898 * "argv[0]" is the command string.
1899 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001900 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001901 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001902channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001903{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001904 char_u *cmd = argv[0].vval.v_string;
1905 char_u *arg;
1906 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001907
Bram Moolenaarece61b02016-02-20 21:39:05 +01001908 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001909 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001910 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001911 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001912 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001913 return;
1914 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001915 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001916 if (arg == NULL)
1917 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001918
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001919 if (STRCMP(cmd, "ex") == 0)
1920 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001921 int save_called_emsg = called_emsg;
1922
1923 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001924 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001925 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001926 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001927 --emsg_silent;
1928 if (called_emsg)
1929 ch_logs(channel, "Ex command error: '%s'",
1930 (char *)get_vim_var_str(VV_ERRMSG));
1931 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001932 }
1933 else if (STRCMP(cmd, "normal") == 0)
1934 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001935 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001936
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001937 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001938 ea.arg = arg;
1939 ea.addr_count = 0;
1940 ea.forceit = TRUE; /* no mapping */
1941 ex_normal(&ea);
1942 }
1943 else if (STRCMP(cmd, "redraw") == 0)
1944 {
1945 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001946
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001947 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001948 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001949 ex_redraw(&ea);
1950 showruler(FALSE);
1951 setcursor();
1952 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001953#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001954 if (gui.in_use)
1955 {
1956 gui_update_cursor(FALSE, FALSE);
1957 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001958 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001959#endif
1960 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001961 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001962 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001963 int is_call = cmd[0] == 'c';
1964 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001965
Bram Moolenaarece61b02016-02-20 21:39:05 +01001966 if (argv[id_idx].v_type != VAR_UNKNOWN
1967 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001968 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001969 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001970 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001971 EMSG("E904: last argument for expr/call must be a number");
1972 }
1973 else if (is_call && argv[2].v_type != VAR_LIST)
1974 {
1975 ch_error(channel, "third argument for call must be a list");
1976 if (p_verbose > 2)
1977 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001978 }
1979 else
1980 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001981 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001982 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001983 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001984 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001985
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001986 /* Don't pollute the display with errors. */
1987 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001988 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001989 {
1990 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001991 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001992 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001993 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001994 {
1995 ch_logs(channel, "Calling '%s'", (char *)arg);
1996 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1997 tv = &res_tv;
1998 else
1999 tv = NULL;
2000 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002001
2002 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002003 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002004 int id = argv[id_idx].vval.v_number;
2005
Bram Moolenaar55fab432016-02-07 16:53:13 +01002006 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002007 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002008 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002009 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002010 /* If evaluation failed or the result can't be encoded
2011 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002012 vim_free(json);
2013 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002014 err_tv.v_type = VAR_STRING;
2015 err_tv.vval.v_string = (char_u *)"ERROR";
2016 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002017 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002018 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002019 if (json != NULL)
2020 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002021 channel_send(channel,
2022 part == PART_SOCK ? PART_SOCK : PART_IN,
2023 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002024 vim_free(json);
2025 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002026 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002027 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002028 if (tv == &res_tv)
2029 clear_tv(tv);
2030 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002031 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002032 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002033 }
2034 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002035 {
2036 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002037 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002038 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002039}
2040
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002041/*
2042 * Invoke the callback at "cbhead".
2043 * Does not redraw but sets channel_need_redraw.
2044 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002045 static void
2046invoke_one_time_callback(
2047 channel_T *channel,
2048 cbq_T *cbhead,
2049 cbq_T *item,
2050 typval_T *argv)
2051{
2052 ch_logs(channel, "Invoking one-time callback %s",
2053 (char *)item->cq_callback);
2054 /* Remove the item from the list first, if the callback
2055 * invokes ch_close() the list will be cleared. */
2056 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002057 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002058 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002059 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002060 vim_free(item);
2061}
2062
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002063 static void
2064append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2065{
2066 buf_T *save_curbuf = curbuf;
2067 linenr_T lnum = buffer->b_ml.ml_line_count;
2068 int save_write_to = buffer->b_write_to_channel;
2069
2070 /* If the buffer is also used as input insert above the last
2071 * line. Don't write these lines. */
2072 if (save_write_to)
2073 {
2074 --lnum;
2075 buffer->b_write_to_channel = FALSE;
2076 }
2077
2078 /* Append to the buffer */
2079 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2080
2081 curbuf = buffer;
2082 u_sync(TRUE);
2083 /* ignore undo failure, undo is not very useful here */
2084 ignored = u_save(lnum, lnum + 1);
2085
2086 ml_append(lnum, msg, 0, FALSE);
2087 appended_lines_mark(lnum, 1L);
2088 curbuf = save_curbuf;
2089
2090 if (buffer->b_nwindows > 0)
2091 {
2092 win_T *wp;
2093 win_T *save_curwin;
2094
2095 FOR_ALL_WINDOWS(wp)
2096 {
2097 if (wp->w_buffer == buffer
2098 && (save_write_to
2099 ? wp->w_cursor.lnum == lnum + 1
2100 : (wp->w_cursor.lnum == lnum
2101 && wp->w_cursor.col == 0)))
2102 {
2103 ++wp->w_cursor.lnum;
2104 save_curwin = curwin;
2105 curwin = wp;
2106 curbuf = curwin->w_buffer;
2107 scroll_cursor_bot(0, FALSE);
2108 curwin = save_curwin;
2109 curbuf = curwin->w_buffer;
2110 }
2111 }
2112 redraw_buf_later(buffer, VALID);
2113 channel_need_redraw = TRUE;
2114 }
2115
2116 if (save_write_to)
2117 {
2118 channel_T *ch;
2119
2120 /* Find channels reading from this buffer and adjust their
2121 * next-to-read line number. */
2122 buffer->b_write_to_channel = TRUE;
2123 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2124 {
2125 chanpart_T *in_part = &ch->ch_part[PART_IN];
2126
2127 if (in_part->ch_buffer == buffer)
2128 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2129 }
2130 }
2131}
2132
Bram Moolenaar437905c2016-04-26 19:01:05 +02002133 static void
2134drop_messages(channel_T *channel, int part)
2135{
2136 char_u *msg;
2137
2138 while ((msg = channel_get(channel, part)) != NULL)
2139 {
2140 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2141 vim_free(msg);
2142 }
2143}
2144
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002145/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002146 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002147 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002148 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002149 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002150 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002151may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002152{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002153 char_u *msg = NULL;
2154 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002155 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002156 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002157 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002158 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002159 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002160 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002161 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002162 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002163
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002164 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002165 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002166 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002167
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002168 /* Use a message-specific callback, part callback or channel callback */
2169 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2170 if (cbitem->cq_seq_nr == 0)
2171 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002172 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002173 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002174 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002175 partial = cbitem->cq_partial;
2176 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002177 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002178 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002179 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002180 partial = channel->ch_part[part].ch_partial;
2181 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002182 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002183 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002184 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002185 partial = channel->ch_partial;
2186 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002187
Bram Moolenaar187db502016-02-27 14:44:26 +01002188 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002189 if (buffer != NULL && !buf_valid(buffer))
2190 {
2191 /* buffer was wiped out */
2192 channel->ch_part[part].ch_buffer = NULL;
2193 buffer = NULL;
2194 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002195
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002196 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002197 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 listitem_T *item;
2199 int argc = 0;
2200
Bram Moolenaard7ece102016-02-02 23:23:02 +01002201 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002202 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002203 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002204 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002205 channel_parse_json(channel, part);
2206 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002207 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002209
Bram Moolenaarece61b02016-02-20 21:39:05 +01002210 for (item = listtv->vval.v_list->lv_first;
2211 item != NULL && argc < CH_JSON_MAX_ARGS;
2212 item = item->li_next)
2213 argv[argc++] = item->li_tv;
2214 while (argc < CH_JSON_MAX_ARGS)
2215 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002216
Bram Moolenaarece61b02016-02-20 21:39:05 +01002217 if (argv[0].v_type == VAR_STRING)
2218 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002219 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002220 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002221 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002222 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002223 }
2224
Bram Moolenaarece61b02016-02-20 21:39:05 +01002225 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002226 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002227 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002228 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002229 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002230 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002231 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002232 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002233 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002234 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002235 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002236 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002237 return FALSE;
2238 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002239 else
2240 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002241 /* If there is no callback or buffer drop the message. */
2242 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002243 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002244 /* If there is a close callback it may use ch_read() to get the
2245 * messages. */
2246 if (channel->ch_close_cb == NULL)
2247 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002248 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002249 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002250
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002251 if (ch_mode == MODE_NL)
2252 {
2253 char_u *nl;
2254 char_u *buf;
2255
2256 /* See if we have a message ending in NL in the first buffer. If
2257 * not try to concatenate the first and the second buffer. */
2258 while (TRUE)
2259 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002260 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002261 nl = vim_strchr(buf, NL);
2262 if (nl != NULL)
2263 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002264 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002265 return FALSE; /* incomplete message */
2266 }
2267 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002268 {
2269 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002270 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002271 *nl = NUL;
2272 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002273 else
2274 {
2275 /* Copy the message into allocated memory and remove it from
2276 * the buffer. */
2277 msg = vim_strnsave(buf, (int)(nl - buf));
2278 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2279 }
2280 }
2281 else
2282 /* For a raw channel we don't know where the message ends, just
2283 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002284 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002285
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002286 if (msg == NULL)
2287 return FALSE; /* out of memory (and avoids Coverity warning) */
2288
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002289 argv[1].v_type = VAR_STRING;
2290 argv[1].vval.v_string = msg;
2291 }
2292
Bram Moolenaara07fec92016-02-05 21:04:08 +01002293 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002294 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002295 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002296
2297 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002298 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002299 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002300 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002301 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002302 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002303 break;
2304 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002305 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002306 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002307 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002308 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002309 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002310 if (buffer != NULL)
2311 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002312 if (msg == NULL)
2313 /* JSON or JS mode: re-encode the message. */
2314 msg = json_encode(listtv, ch_mode);
2315 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002316 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002317 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002318
Bram Moolenaar187db502016-02-27 14:44:26 +01002319 if (callback != NULL)
2320 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002321 if (cbitem != NULL)
2322 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2323 else
2324 {
2325 /* invoke the channel callback */
2326 ch_logs(channel, "Invoking channel callback %s",
2327 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002328 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002329 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002330 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002331 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002332 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002333 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002334
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002335 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002336 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002337 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002338
2339 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002340}
2341
2342/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002343 * Return TRUE when channel "channel" is open for writing to.
2344 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002345 */
2346 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002347channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002348{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002349 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002350 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002351}
2352
2353/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002354 * Return TRUE when channel "channel" is open for reading or writing.
2355 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002356 */
2357 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002358channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002359{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002360 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002361 || channel->CH_IN_FD != INVALID_FD
2362 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002363 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002364}
2365
2366/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002367 * Return TRUE if "channel" has JSON or other typeahead.
2368 */
2369 static int
2370channel_has_readahead(channel_T *channel, int part)
2371{
2372 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2373
2374 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2375 {
2376 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2377 jsonq_T *item = head->jq_next;
2378
2379 return item != NULL;
2380 }
2381 return channel_peek(channel, part) != NULL;
2382}
2383
2384/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002385 * Return a string indicating the status of the channel.
2386 */
2387 char *
2388channel_status(channel_T *channel)
2389{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002390 int part;
2391 int has_readahead = FALSE;
2392
Bram Moolenaar77073442016-02-13 23:23:53 +01002393 if (channel == NULL)
2394 return "fail";
2395 if (channel_is_open(channel))
2396 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002397 for (part = PART_SOCK; part <= PART_ERR; ++part)
2398 if (channel_has_readahead(channel, part))
2399 {
2400 has_readahead = TRUE;
2401 break;
2402 }
2403
2404 if (has_readahead)
2405 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002406 return "closed";
2407}
2408
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002409 static void
2410channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2411{
2412 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002413 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002414 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002415 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002416
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002417 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002418 STRCAT(namebuf, "_");
2419 tail = STRLEN(namebuf);
2420
2421 STRCPY(namebuf + tail, "status");
2422 dict_add_nr_str(dict, namebuf, 0,
2423 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2424
2425 STRCPY(namebuf + tail, "mode");
2426 switch (chanpart->ch_mode)
2427 {
2428 case MODE_NL: s = "NL"; break;
2429 case MODE_RAW: s = "RAW"; break;
2430 case MODE_JSON: s = "JSON"; break;
2431 case MODE_JS: s = "JS"; break;
2432 }
2433 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2434
2435 STRCPY(namebuf + tail, "io");
2436 if (part == PART_SOCK)
2437 s = "socket";
2438 else switch (chanpart->ch_io)
2439 {
2440 case JIO_NULL: s = "null"; break;
2441 case JIO_PIPE: s = "pipe"; break;
2442 case JIO_FILE: s = "file"; break;
2443 case JIO_BUFFER: s = "buffer"; break;
2444 case JIO_OUT: s = "out"; break;
2445 }
2446 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2447
2448 STRCPY(namebuf + tail, "timeout");
2449 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2450}
2451
2452 void
2453channel_info(channel_T *channel, dict_T *dict)
2454{
2455 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2456 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2457
2458 if (channel->ch_hostname != NULL)
2459 {
2460 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2461 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2462 channel_part_info(channel, dict, "sock", PART_SOCK);
2463 }
2464 else
2465 {
2466 channel_part_info(channel, dict, "out", PART_OUT);
2467 channel_part_info(channel, dict, "err", PART_ERR);
2468 channel_part_info(channel, dict, "in", PART_IN);
2469 }
2470}
2471
Bram Moolenaar77073442016-02-13 23:23:53 +01002472/*
2473 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002474 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002475 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002476 */
2477 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002478channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002479{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002480 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002481
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002482#ifdef FEAT_GUI
2483 channel_gui_unregister(channel);
2484#endif
2485
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002486 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002487 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002488 sock_close(channel->CH_SOCK_FD);
2489 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002490 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002491 may_close_part(&channel->CH_IN_FD);
2492 may_close_part(&channel->CH_OUT_FD);
2493 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002494
Bram Moolenaar8b374212016-02-24 20:43:06 +01002495 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002496 {
2497 typval_T argv[1];
2498 typval_T rettv;
2499 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002500 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002501
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002502 /* Invoke callbacks before the close callback, since it's weird to
2503 * first invoke the close callback. Increment the refcount to avoid
2504 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002505 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002506 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002507 for (part = PART_SOCK; part <= PART_ERR; ++part)
2508 while (may_invoke_callback(channel, part))
2509 ;
2510
2511 /* Invoke the close callback, if still set. */
2512 if (channel->ch_close_cb != NULL)
2513 {
2514 ch_logs(channel, "Invoking close callback %s",
2515 (char *)channel->ch_close_cb);
2516 argv[0].v_type = VAR_CHANNEL;
2517 argv[0].vval.v_channel = channel;
2518 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002519 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2520 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002521 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002522 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002523 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002524 --channel->ch_refcount;
2525
2526 /* the callback is only called once */
2527 vim_free(channel->ch_close_cb);
2528 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002529 partial_unref(channel->ch_close_partial);
2530 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002531
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002532 if (channel_need_redraw)
2533 {
2534 channel_need_redraw = FALSE;
2535 redraw_after_callback();
2536 }
2537
Bram Moolenaar437905c2016-04-26 19:01:05 +02002538 /* any remaining messages are useless now */
2539 for (part = PART_SOCK; part <= PART_ERR; ++part)
2540 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002541 }
2542
2543 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002544}
2545
Bram Moolenaard04a0202016-01-26 23:30:18 +01002546/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002547 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002548 * Returns NULL if there is nothing.
2549 */
2550 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002551channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002552{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002553 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002554
Bram Moolenaar77073442016-02-13 23:23:53 +01002555 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002556 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002557 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002558}
2559
2560/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002561 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002562 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002563 static void
2564channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002565{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002566 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2567 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002568
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002569 while (channel_peek(channel, part) != NULL)
2570 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002571
2572 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002573 {
2574 cbq_T *node = cb_head->cq_next;
2575
2576 remove_cb_node(cb_head, node);
2577 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002578 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002579 vim_free(node);
2580 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002581
2582 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002583 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002584 free_tv(json_head->jq_next->jq_value);
2585 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002586 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002587
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002588 vim_free(channel->ch_part[part].ch_callback);
2589 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002590 partial_unref(channel->ch_part[part].ch_partial);
2591 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002592}
2593
2594/*
2595 * Clear all the read buffers on "channel".
2596 */
2597 void
2598channel_clear(channel_T *channel)
2599{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002600 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002601 vim_free(channel->ch_hostname);
2602 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002603 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002604 channel_clear_one(channel, PART_OUT);
2605 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002606 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002607 vim_free(channel->ch_callback);
2608 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002609 partial_unref(channel->ch_partial);
2610 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002611 vim_free(channel->ch_close_cb);
2612 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002613 partial_unref(channel->ch_close_partial);
2614 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002615}
2616
Bram Moolenaar77073442016-02-13 23:23:53 +01002617#if defined(EXITFREE) || defined(PROTO)
2618 void
2619channel_free_all(void)
2620{
2621 channel_T *channel;
2622
Bram Moolenaard6051b52016-02-28 15:49:03 +01002623 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002624 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2625 channel_clear(channel);
2626}
2627#endif
2628
2629
Bram Moolenaar715d2852016-04-30 17:06:31 +02002630/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002631#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002632
2633/* Buffer size for reading incoming messages. */
2634#define MAXMSGSIZE 4096
2635
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002636#if defined(HAVE_SELECT)
2637/*
2638 * Add write fds where we are waiting for writing to be possible.
2639 */
2640 static int
2641channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2642{
2643 int maxfd = maxfd_arg;
2644 channel_T *ch;
2645
2646 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2647 {
2648 chanpart_T *in_part = &ch->ch_part[PART_IN];
2649
2650 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2651 {
2652 FD_SET((int)in_part->ch_fd, wfds);
2653 if ((int)in_part->ch_fd >= maxfd)
2654 maxfd = (int)in_part->ch_fd + 1;
2655 }
2656 }
2657 return maxfd;
2658}
2659#else
2660/*
2661 * Add write fds where we are waiting for writing to be possible.
2662 */
2663 static int
2664channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2665{
2666 int nfd = nfd_in;
2667 channel_T *ch;
2668
2669 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2670 {
2671 chanpart_T *in_part = &ch->ch_part[PART_IN];
2672
2673 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2674 {
2675 in_part->ch_poll_idx = nfd;
2676 fds[nfd].fd = in_part->ch_fd;
2677 fds[nfd].events = POLLOUT;
2678 ++nfd;
2679 }
2680 else
2681 in_part->ch_poll_idx = -1;
2682 }
2683 return nfd;
2684}
2685#endif
2686
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002687typedef enum {
2688 CW_READY,
2689 CW_NOT_READY,
2690 CW_ERROR
2691} channel_wait_result;
2692
Bram Moolenaard04a0202016-01-26 23:30:18 +01002693/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002694 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002695 * Return CW_READY when there is something to read.
2696 * Return CW_NOT_READY when there is nothing to read.
2697 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002698 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002699 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002700channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002701{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002702 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002703 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002704
Bram Moolenaard8070362016-02-15 21:56:54 +01002705# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002706 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002707 {
2708 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002709 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002710 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002711 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002712
2713 /* reading from a pipe, not a socket */
2714 while (TRUE)
2715 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002716 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2717
2718 if (r && nread > 0)
2719 return CW_READY;
2720 if (r == 0)
2721 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002722
2723 /* perhaps write some buffer lines */
2724 channel_write_any_lines();
2725
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002726 sleep_time = deadline - GetTickCount();
2727 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002728 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002729 /* Wait for a little while. Very short at first, up to 10 msec
2730 * after looping a few times. */
2731 if (sleep_time > delay)
2732 sleep_time = delay;
2733 Sleep(sleep_time);
2734 delay = delay * 2;
2735 if (delay > 10)
2736 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002737 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002738 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002739 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002740#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002741 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002742#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002743 struct timeval tval;
2744 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002745 fd_set wfds;
2746 int ret;
2747 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002748
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002749 tval.tv_sec = timeout / 1000;
2750 tval.tv_usec = (timeout % 1000) * 1000;
2751 for (;;)
2752 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002753 FD_ZERO(&rfds);
2754 FD_SET((int)fd, &rfds);
2755
2756 /* Write lines to a pipe when a pipe can be written to. Need to
2757 * set this every time, some buffers may be done. */
2758 maxfd = (int)fd + 1;
2759 FD_ZERO(&wfds);
2760 maxfd = channel_fill_wfds(maxfd, &wfds);
2761
2762 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002763# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002764 SOCK_ERRNO;
2765 if (ret == -1 && errno == EINTR)
2766 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002767# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002768 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002769 {
2770 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002771 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002772 channel_write_any_lines();
2773 continue;
2774 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002775 break;
2776 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002777#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002778 for (;;)
2779 {
2780 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2781 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002782
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002783 fds[0].fd = fd;
2784 fds[0].events = POLLIN;
2785 nfd = channel_fill_poll_write(nfd, fds);
2786 if (poll(fds, nfd, timeout) > 0)
2787 {
2788 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002789 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002790 channel_write_any_lines();
2791 continue;
2792 }
2793 break;
2794 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002795#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002796 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002797 return CW_NOT_READY;
2798}
2799
2800 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002801channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002802{
2803 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002804 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2805 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002806
2807 /* Queue a "DETACH" netbeans message in the command queue in order to
2808 * terminate the netbeans session later. Do not end the session here
2809 * directly as we may be running in the context of a call to
2810 * netbeans_parse_messages():
2811 * netbeans_parse_messages
2812 * -> autocmd triggered while processing the netbeans cmd
2813 * -> ui_breakcheck
2814 * -> gui event loop or select loop
2815 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002816 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002817 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002818 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002819 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002820 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2821
2822 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002823 * died. Don't close the channel right away, it may be the wrong moment
2824 * to invoke callbacks. */
2825 channel->ch_to_be_closed = TRUE;
2826}
2827
2828 static void
2829channel_close_now(channel_T *channel)
2830{
2831 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002832 channel_close(channel, TRUE);
2833 if (channel->ch_nb_close_cb != NULL)
2834 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002835}
2836
2837/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002838 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002839 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002840 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002841 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002842 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002843channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002844{
2845 static char_u *buf = NULL;
2846 int len = 0;
2847 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002848 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002849 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002850
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002851 fd = channel->ch_part[part].ch_fd;
2852 if (fd == INVALID_FD)
2853 {
2854 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002855 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002856 }
2857 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002858
2859 /* Allocate a buffer to read into. */
2860 if (buf == NULL)
2861 {
2862 buf = alloc(MAXMSGSIZE);
2863 if (buf == NULL)
2864 return; /* out of memory! */
2865 }
2866
2867 /* Keep on reading for as long as there is something to read.
2868 * Use select() or poll() to avoid blocking on a message that is exactly
2869 * MAXMSGSIZE long. */
2870 for (;;)
2871 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002872 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002873 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002874 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002875 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002876 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002877 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002878 if (len <= 0)
2879 break; /* error or nothing more to read */
2880
2881 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002882 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002883 readlen += len;
2884 if (len < MAXMSGSIZE)
2885 break; /* did read everything that's available */
2886 }
2887
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002888 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002889 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002890 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002891
2892#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002893 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002894 if (CH_HAS_GUI && gtk_main_level() > 0)
2895 gtk_main_quit();
2896#endif
2897}
2898
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002899/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002900 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002901 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002902 * Returns what was read in allocated memory.
2903 * Returns NULL in case of error or timeout.
2904 */
2905 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002906channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002907{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002908 char_u *buf;
2909 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002910 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002911 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002912 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002913
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002914 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002915 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002916
2917 while (TRUE)
2918 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002919 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002920 if (buf != NULL && (mode == MODE_RAW
2921 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2922 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002923 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002924 continue;
2925
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002926 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002927 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002928 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002929 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002930 {
2931 ch_log(channel, "Timed out");
2932 return NULL;
2933 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002934 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002935 }
2936
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002937 if (mode == MODE_RAW)
2938 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002939 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002940 }
2941 else
2942 {
2943 nl = vim_strchr(buf, NL);
2944 if (nl[1] == NUL)
2945 {
2946 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002947 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002948 *nl = NUL;
2949 }
2950 else
2951 {
2952 /* Copy the message into allocated memory and remove it from the
2953 * buffer. */
2954 msg = vim_strnsave(buf, (int)(nl - buf));
2955 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2956 }
2957 }
2958 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002959 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002960 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002961}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002962
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002963/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002964 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002965 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002966 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002967 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002968 */
2969 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002970channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002971 channel_T *channel,
2972 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002973 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002974 int id,
2975 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002976{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002977 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002978 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002979 int timeout;
2980 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002981
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002982 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002983 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002984 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002985 for (;;)
2986 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002987 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002988
2989 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002990 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002991 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002992 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002993 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002994 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002995
Bram Moolenaard7ece102016-02-02 23:23:02 +01002996 if (!more)
2997 {
2998 /* Handle any other messages in the queue. If done some more
2999 * messages may have arrived. */
3000 if (channel_parse_messages())
3001 continue;
3002
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003003 /* Wait for up to the timeout. If there was an incomplete message
3004 * use the deadline for that. */
3005 timeout = timeout_arg;
3006 if (chanpart->ch_waiting)
3007 {
3008#ifdef WIN32
3009 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3010#else
3011 {
3012 struct timeval now_tv;
3013
3014 gettimeofday(&now_tv, NULL);
3015 timeout = (chanpart->ch_deadline.tv_sec
3016 - now_tv.tv_sec) * 1000
3017 + (chanpart->ch_deadline.tv_usec
3018 - now_tv.tv_usec) / 1000
3019 + 1;
3020 }
3021#endif
3022 if (timeout < 0)
3023 {
3024 /* Something went wrong, channel_parse_json() didn't
3025 * discard message. Cancel waiting. */
3026 chanpart->ch_waiting = FALSE;
3027 timeout = timeout_arg;
3028 }
3029 else if (timeout > timeout_arg)
3030 timeout = timeout_arg;
3031 }
3032 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003033 if (fd == INVALID_FD
3034 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003035 {
3036 if (timeout == timeout_arg)
3037 {
3038 if (fd != INVALID_FD)
3039 ch_log(channel, "Timed out");
3040 break;
3041 }
3042 }
3043 else
3044 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003045 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003046 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003047 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003048 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003049}
3050
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003051/*
3052 * Common for ch_read() and ch_readraw().
3053 */
3054 void
3055common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3056{
3057 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003058 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003059 jobopt_T opt;
3060 int mode;
3061 int timeout;
3062 int id = -1;
3063 typval_T *listtv = NULL;
3064
3065 /* return an empty string by default */
3066 rettv->v_type = VAR_STRING;
3067 rettv->vval.v_string = NULL;
3068
3069 clear_job_options(&opt);
3070 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3071 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003072 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003073
Bram Moolenaar437905c2016-04-26 19:01:05 +02003074 if (opt.jo_set & JO_PART)
3075 part = opt.jo_part;
3076 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003077 if (channel != NULL)
3078 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003079 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003080 part = channel_part_read(channel);
3081 mode = channel_get_mode(channel, part);
3082 timeout = channel_get_timeout(channel, part);
3083 if (opt.jo_set & JO_TIMEOUT)
3084 timeout = opt.jo_timeout;
3085
3086 if (raw || mode == MODE_RAW || mode == MODE_NL)
3087 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3088 else
3089 {
3090 if (opt.jo_set & JO_ID)
3091 id = opt.jo_id;
3092 channel_read_json_block(channel, part, timeout, id, &listtv);
3093 if (listtv != NULL)
3094 {
3095 *rettv = *listtv;
3096 vim_free(listtv);
3097 }
3098 else
3099 {
3100 rettv->v_type = VAR_SPECIAL;
3101 rettv->vval.v_number = VVAL_NONE;
3102 }
3103 }
3104 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003105
3106theend:
3107 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003108}
3109
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003110# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3111 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003112/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003113 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003114 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003115 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003116 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003117channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003118{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003119 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003120 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003121
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003122 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003123 for (channel = first_channel; channel != NULL;
3124 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003125 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003126 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003127 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003128 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003129 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003130 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003131 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003132 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003133 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003134}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003135# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003136
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003137# if defined(WIN32) || defined(PROTO)
3138/*
3139 * Check the channels for anything that is ready to be read.
3140 * The data is put in the read queue.
3141 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003142 void
3143channel_handle_events(void)
3144{
3145 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003146 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003147 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003148
3149 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3150 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003151 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003152 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003153 {
3154 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003155 if (fd != INVALID_FD)
3156 {
3157 int r = channel_wait(channel, fd, 0);
3158
3159 if (r == CW_READY)
3160 channel_read(channel, part, "channel_handle_events");
3161 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003162 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003163 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003164 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003165 }
3166}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003167# endif
3168
Bram Moolenaard04a0202016-01-26 23:30:18 +01003169/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003170 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003171 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003172 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003173 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003174 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003175channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003176{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003177 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003178 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003179 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003180
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003181 fd = channel->ch_part[part].ch_fd;
3182 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003183 {
3184 if (!channel->ch_error && fun != NULL)
3185 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003186 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003187 EMSG2("E630: %s(): write while not connected", fun);
3188 }
3189 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003190 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003191 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003192
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003193 if (log_fd != NULL)
3194 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003195 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003196 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003197 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003198 fprintf(log_fd, "'\n");
3199 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003200 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003201 }
3202
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003203 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003204 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003205 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003206 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003207 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003208 {
3209 if (!channel->ch_error && fun != NULL)
3210 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003211 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003212 EMSG2("E631: %s(): write failed", fun);
3213 }
3214 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003215 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003216 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003217
3218 channel->ch_error = FALSE;
3219 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003220}
3221
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003222/*
3223 * Common for "ch_sendexpr()" and "ch_sendraw()".
3224 * Returns the channel if the caller should read the response.
3225 * Sets "part_read" to the the read fd.
3226 * Otherwise returns NULL.
3227 */
3228 channel_T *
3229send_common(
3230 typval_T *argvars,
3231 char_u *text,
3232 int id,
3233 int eval,
3234 jobopt_T *opt,
3235 char *fun,
3236 int *part_read)
3237{
3238 channel_T *channel;
3239 int part_send;
3240
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003241 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003242 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003243 if (channel == NULL)
3244 return NULL;
3245 part_send = channel_part_send(channel);
3246 *part_read = channel_part_read(channel);
3247
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003248 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3249 return NULL;
3250
3251 /* Set the callback. An empty callback means no callback and not reading
3252 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3253 * allowed. */
3254 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3255 {
3256 if (eval)
3257 {
3258 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3259 return NULL;
3260 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003261 channel_set_req_callback(channel, part_send,
3262 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003263 }
3264
3265 if (channel_send(channel, part_send, text, fun) == OK
3266 && opt->jo_callback == NULL)
3267 return channel;
3268 return NULL;
3269}
3270
3271/*
3272 * common for "ch_evalexpr()" and "ch_sendexpr()"
3273 */
3274 void
3275ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3276{
3277 char_u *text;
3278 typval_T *listtv;
3279 channel_T *channel;
3280 int id;
3281 ch_mode_T ch_mode;
3282 int part_send;
3283 int part_read;
3284 jobopt_T opt;
3285 int timeout;
3286
3287 /* return an empty string by default */
3288 rettv->v_type = VAR_STRING;
3289 rettv->vval.v_string = NULL;
3290
Bram Moolenaar437905c2016-04-26 19:01:05 +02003291 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003292 if (channel == NULL)
3293 return;
3294 part_send = channel_part_send(channel);
3295
3296 ch_mode = channel_get_mode(channel, part_send);
3297 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3298 {
3299 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3300 return;
3301 }
3302
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003303 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003304 text = json_encode_nr_expr(id, &argvars[1],
3305 ch_mode == MODE_JS ? JSON_JS : 0);
3306 if (text == NULL)
3307 return;
3308
3309 channel = send_common(argvars, text, id, eval, &opt,
3310 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3311 vim_free(text);
3312 if (channel != NULL && eval)
3313 {
3314 if (opt.jo_set & JO_TIMEOUT)
3315 timeout = opt.jo_timeout;
3316 else
3317 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003318 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3319 == OK)
3320 {
3321 list_T *list = listtv->vval.v_list;
3322
3323 /* Move the item from the list and then change the type to
3324 * avoid the value being freed. */
3325 *rettv = list->lv_last->li_tv;
3326 list->lv_last->li_tv.v_type = VAR_NUMBER;
3327 free_tv(listtv);
3328 }
3329 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003330 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003331}
3332
3333/*
3334 * common for "ch_evalraw()" and "ch_sendraw()"
3335 */
3336 void
3337ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3338{
3339 char_u buf[NUMBUFLEN];
3340 char_u *text;
3341 channel_T *channel;
3342 int part_read;
3343 jobopt_T opt;
3344 int timeout;
3345
3346 /* return an empty string by default */
3347 rettv->v_type = VAR_STRING;
3348 rettv->vval.v_string = NULL;
3349
3350 text = get_tv_string_buf(&argvars[1], buf);
3351 channel = send_common(argvars, text, 0, eval, &opt,
3352 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3353 if (channel != NULL && eval)
3354 {
3355 if (opt.jo_set & JO_TIMEOUT)
3356 timeout = opt.jo_timeout;
3357 else
3358 timeout = channel_get_timeout(channel, part_read);
3359 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3360 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003361 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003362}
3363
Bram Moolenaard04a0202016-01-26 23:30:18 +01003364# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003365/*
3366 * Add open channels to the poll struct.
3367 * Return the adjusted struct index.
3368 * The type of "fds" is hidden to avoid problems with the function proto.
3369 */
3370 int
3371channel_poll_setup(int nfd_in, void *fds_in)
3372{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003373 int nfd = nfd_in;
3374 channel_T *channel;
3375 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003376 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003377
Bram Moolenaar77073442016-02-13 23:23:53 +01003378 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003379 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003380 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003381 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003382 chanpart_T *ch_part = &channel->ch_part[part];
3383
3384 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003385 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003386 ch_part->ch_poll_idx = nfd;
3387 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003388 fds[nfd].events = POLLIN;
3389 nfd++;
3390 }
3391 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003392 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003393 }
3394 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003395
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003396 nfd = channel_fill_poll_write(nfd, fds);
3397
Bram Moolenaare0874f82016-01-24 20:36:41 +01003398 return nfd;
3399}
3400
3401/*
3402 * The type of "fds" is hidden to avoid problems with the function proto.
3403 */
3404 int
3405channel_poll_check(int ret_in, void *fds_in)
3406{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003407 int ret = ret_in;
3408 channel_T *channel;
3409 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003410 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003411 int idx;
3412 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003413
Bram Moolenaar77073442016-02-13 23:23:53 +01003414 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003415 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003416 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003417 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003418 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003419
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003420 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003421 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003422 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003423 --ret;
3424 }
3425 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003426
3427 in_part = &channel->ch_part[PART_IN];
3428 idx = in_part->ch_poll_idx;
3429 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3430 {
3431 if (in_part->ch_buf_append)
3432 {
3433 if (in_part->ch_buffer != NULL)
3434 channel_write_new_lines(in_part->ch_buffer);
3435 }
3436 else
3437 channel_write_in(channel);
3438 --ret;
3439 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003440 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003441
3442 return ret;
3443}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003444# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003445
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003446# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003447/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003448 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003449 */
3450 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003451channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003452{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003453 int maxfd = maxfd_in;
3454 channel_T *channel;
3455 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003456 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003457 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003458
Bram Moolenaar77073442016-02-13 23:23:53 +01003459 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003460 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003461 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003462 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003463 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003464
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003465 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003466 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003467 FD_SET((int)fd, rfds);
3468 if (maxfd < (int)fd)
3469 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003470 }
3471 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003472 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003473
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003474 maxfd = channel_fill_wfds(maxfd, wfds);
3475
Bram Moolenaare0874f82016-01-24 20:36:41 +01003476 return maxfd;
3477}
3478
3479/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003480 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003481 */
3482 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003483channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003484{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003485 int ret = ret_in;
3486 channel_T *channel;
3487 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003488 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003489 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003490 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003491
Bram Moolenaar77073442016-02-13 23:23:53 +01003492 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003493 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003494 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003495 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003497
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003498 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003499 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003500 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003501 --ret;
3502 }
3503 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003504
3505 in_part = &channel->ch_part[PART_IN];
3506 if (ret > 0 && in_part->ch_fd != INVALID_FD
3507 && FD_ISSET(in_part->ch_fd, wfds))
3508 {
3509 if (in_part->ch_buf_append)
3510 {
3511 if (in_part->ch_buffer != NULL)
3512 channel_write_new_lines(in_part->ch_buffer);
3513 }
3514 else
3515 channel_write_in(channel);
3516 --ret;
3517 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003518 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003519
3520 return ret;
3521}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003522# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003523
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003524/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003525 * Execute queued up commands.
3526 * Invoked from the main loop when it's safe to execute received commands.
3527 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003528 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003529 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003530channel_parse_messages(void)
3531{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003532 channel_T *channel = first_channel;
3533 int ret = FALSE;
3534 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003535 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003536
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003537 ++safe_to_invoke_callback;
3538
Bram Moolenaard0b65022016-03-06 21:50:33 +01003539 /* Only do this message when another message was given, otherwise we get
3540 * lots of them. */
3541 if (did_log_msg)
3542 {
3543 ch_log(NULL, "looking for messages on channels");
3544 did_log_msg = FALSE;
3545 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003546 while (channel != NULL)
3547 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003548 if (channel->ch_to_be_closed)
3549 {
3550 channel->ch_to_be_closed = FALSE;
3551 channel_close_now(channel);
3552 /* channel may have been freed, start over */
3553 channel = first_channel;
3554 continue;
3555 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003556 if (channel->ch_to_be_freed)
3557 {
3558 channel_free(channel);
3559 /* channel has been freed, start over */
3560 channel = first_channel;
3561 continue;
3562 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003563 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003564 {
3565 /* channel is no longer useful, free it */
3566 channel_free(channel);
3567 channel = first_channel;
3568 part = PART_SOCK;
3569 continue;
3570 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003571 if (channel->ch_part[part].ch_fd != INVALID_FD
3572 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003573 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003574 /* Increase the refcount, in case the handler causes the channel
3575 * to be unreferenced or closed. */
3576 ++channel->ch_refcount;
3577 r = may_invoke_callback(channel, part);
3578 if (r == OK)
3579 ret = TRUE;
3580 if (channel_unref(channel) || r == OK)
3581 {
3582 /* channel was freed or something was done, start over */
3583 channel = first_channel;
3584 part = PART_SOCK;
3585 continue;
3586 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003587 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003588 if (part < PART_ERR)
3589 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003590 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003591 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003592 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003593 part = PART_SOCK;
3594 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003595 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003596
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003597 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003598 {
3599 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003600 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003601 }
3602
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003603 --safe_to_invoke_callback;
3604
Bram Moolenaard7ece102016-02-02 23:23:02 +01003605 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003606}
3607
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003608/*
3609 * Mark references to lists used in channels.
3610 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003611 int
3612set_ref_in_channel(int copyID)
3613{
Bram Moolenaar77073442016-02-13 23:23:53 +01003614 int abort = FALSE;
3615 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003616 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003617
Bram Moolenaar77073442016-02-13 23:23:53 +01003618 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003619 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003620 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003621 tv.v_type = VAR_CHANNEL;
3622 tv.vval.v_channel = channel;
3623 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003624 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003625 return abort;
3626}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003627
3628/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003629 * Return the "part" to write to for "channel".
3630 */
3631 int
3632channel_part_send(channel_T *channel)
3633{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003634 if (channel->CH_SOCK_FD == INVALID_FD)
3635 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003636 return PART_SOCK;
3637}
3638
3639/*
3640 * Return the default "part" to read from for "channel".
3641 */
3642 int
3643channel_part_read(channel_T *channel)
3644{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003645 if (channel->CH_SOCK_FD == INVALID_FD)
3646 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003647 return PART_SOCK;
3648}
3649
3650/*
3651 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003652 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003653 */
3654 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003655channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003656{
Bram Moolenaar77073442016-02-13 23:23:53 +01003657 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003658 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003659 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003660}
3661
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003662/*
3663 * Return the timeout of "channel"/"part"
3664 */
3665 int
3666channel_get_timeout(channel_T *channel, int part)
3667{
3668 return channel->ch_part[part].ch_timeout;
3669}
3670
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003671 static int
3672handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3673{
3674 char_u *val = get_tv_string(item);
3675
3676 opt->jo_set |= jo;
3677 if (STRCMP(val, "nl") == 0)
3678 *modep = MODE_NL;
3679 else if (STRCMP(val, "raw") == 0)
3680 *modep = MODE_RAW;
3681 else if (STRCMP(val, "js") == 0)
3682 *modep = MODE_JS;
3683 else if (STRCMP(val, "json") == 0)
3684 *modep = MODE_JSON;
3685 else
3686 {
3687 EMSG2(_(e_invarg2), val);
3688 return FAIL;
3689 }
3690 return OK;
3691}
3692
3693 static int
3694handle_io(typval_T *item, int part, jobopt_T *opt)
3695{
3696 char_u *val = get_tv_string(item);
3697
3698 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3699 if (STRCMP(val, "null") == 0)
3700 opt->jo_io[part] = JIO_NULL;
3701 else if (STRCMP(val, "pipe") == 0)
3702 opt->jo_io[part] = JIO_PIPE;
3703 else if (STRCMP(val, "file") == 0)
3704 opt->jo_io[part] = JIO_FILE;
3705 else if (STRCMP(val, "buffer") == 0)
3706 opt->jo_io[part] = JIO_BUFFER;
3707 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3708 opt->jo_io[part] = JIO_OUT;
3709 else
3710 {
3711 EMSG2(_(e_invarg2), val);
3712 return FAIL;
3713 }
3714 return OK;
3715}
3716
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003717/*
3718 * Clear a jobopt_T before using it.
3719 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003720 void
3721clear_job_options(jobopt_T *opt)
3722{
3723 vim_memset(opt, 0, sizeof(jobopt_T));
3724}
3725
3726/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003727 * Free any members of a jobopt_T.
3728 */
3729 void
3730free_job_options(jobopt_T *opt)
3731{
3732 if (opt->jo_partial != NULL)
3733 partial_unref(opt->jo_partial);
3734 if (opt->jo_out_partial != NULL)
3735 partial_unref(opt->jo_out_partial);
3736 if (opt->jo_err_partial != NULL)
3737 partial_unref(opt->jo_err_partial);
3738 if (opt->jo_close_partial != NULL)
3739 partial_unref(opt->jo_close_partial);
3740}
3741
3742/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003743 * Get the PART_ number from the first character of an option name.
3744 */
3745 static int
3746part_from_char(int c)
3747{
3748 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3749}
3750
3751/*
3752 * Get the option entries from the dict in "tv", parse them and put the result
3753 * in "opt".
3754 * Only accept options in "supported".
3755 * If an option value is invalid return FAIL.
3756 */
3757 int
3758get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3759{
3760 typval_T *item;
3761 char_u *val;
3762 dict_T *dict;
3763 int todo;
3764 hashitem_T *hi;
3765 int part;
3766
3767 opt->jo_set = 0;
3768 if (tv->v_type == VAR_UNKNOWN)
3769 return OK;
3770 if (tv->v_type != VAR_DICT)
3771 {
3772 EMSG(_(e_invarg));
3773 return FAIL;
3774 }
3775 dict = tv->vval.v_dict;
3776 if (dict == NULL)
3777 return OK;
3778
3779 todo = (int)dict->dv_hashtab.ht_used;
3780 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3781 if (!HASHITEM_EMPTY(hi))
3782 {
3783 item = &dict_lookup(hi)->di_tv;
3784
3785 if (STRCMP(hi->hi_key, "mode") == 0)
3786 {
3787 if (!(supported & JO_MODE))
3788 break;
3789 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3790 return FAIL;
3791 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003792 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003793 {
3794 if (!(supported & JO_IN_MODE))
3795 break;
3796 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3797 == FAIL)
3798 return FAIL;
3799 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003800 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003801 {
3802 if (!(supported & JO_OUT_MODE))
3803 break;
3804 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3805 == FAIL)
3806 return FAIL;
3807 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003808 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003809 {
3810 if (!(supported & JO_ERR_MODE))
3811 break;
3812 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3813 == FAIL)
3814 return FAIL;
3815 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003816 else if (STRCMP(hi->hi_key, "in_io") == 0
3817 || STRCMP(hi->hi_key, "out_io") == 0
3818 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003819 {
3820 if (!(supported & JO_OUT_IO))
3821 break;
3822 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3823 return FAIL;
3824 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003825 else if (STRCMP(hi->hi_key, "in_name") == 0
3826 || STRCMP(hi->hi_key, "out_name") == 0
3827 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003828 {
3829 part = part_from_char(*hi->hi_key);
3830
3831 if (!(supported & JO_OUT_IO))
3832 break;
3833 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3834 opt->jo_io_name[part] =
3835 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3836 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003837 else if (STRCMP(hi->hi_key, "in_buf") == 0
3838 || STRCMP(hi->hi_key, "out_buf") == 0
3839 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003840 {
3841 part = part_from_char(*hi->hi_key);
3842
3843 if (!(supported & JO_OUT_IO))
3844 break;
3845 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3846 opt->jo_io_buf[part] = get_tv_number(item);
3847 if (opt->jo_io_buf[part] <= 0)
3848 {
3849 EMSG2(_(e_invarg2), get_tv_string(item));
3850 return FAIL;
3851 }
3852 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3853 {
3854 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3855 return FAIL;
3856 }
3857 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003858 else if (STRCMP(hi->hi_key, "in_top") == 0
3859 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003860 {
3861 linenr_T *lp;
3862
3863 if (!(supported & JO_OUT_IO))
3864 break;
3865 if (hi->hi_key[3] == 't')
3866 {
3867 lp = &opt->jo_in_top;
3868 opt->jo_set |= JO_IN_TOP;
3869 }
3870 else
3871 {
3872 lp = &opt->jo_in_bot;
3873 opt->jo_set |= JO_IN_BOT;
3874 }
3875 *lp = get_tv_number(item);
3876 if (*lp < 0)
3877 {
3878 EMSG2(_(e_invarg2), get_tv_string(item));
3879 return FAIL;
3880 }
3881 }
3882 else if (STRCMP(hi->hi_key, "channel") == 0)
3883 {
3884 if (!(supported & JO_OUT_IO))
3885 break;
3886 opt->jo_set |= JO_CHANNEL;
3887 if (item->v_type != VAR_CHANNEL)
3888 {
3889 EMSG2(_(e_invarg2), "channel");
3890 return FAIL;
3891 }
3892 opt->jo_channel = item->vval.v_channel;
3893 }
3894 else if (STRCMP(hi->hi_key, "callback") == 0)
3895 {
3896 if (!(supported & JO_CALLBACK))
3897 break;
3898 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003899 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003900 if (opt->jo_callback == NULL)
3901 {
3902 EMSG2(_(e_invarg2), "callback");
3903 return FAIL;
3904 }
3905 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003906 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003907 {
3908 if (!(supported & JO_OUT_CALLBACK))
3909 break;
3910 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003911 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003912 if (opt->jo_out_cb == NULL)
3913 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003914 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003915 return FAIL;
3916 }
3917 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003918 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003919 {
3920 if (!(supported & JO_ERR_CALLBACK))
3921 break;
3922 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003923 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003924 if (opt->jo_err_cb == NULL)
3925 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003926 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003927 return FAIL;
3928 }
3929 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003930 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003931 {
3932 if (!(supported & JO_CLOSE_CALLBACK))
3933 break;
3934 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003935 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003936 if (opt->jo_close_cb == NULL)
3937 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003938 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003939 return FAIL;
3940 }
3941 }
3942 else if (STRCMP(hi->hi_key, "waittime") == 0)
3943 {
3944 if (!(supported & JO_WAITTIME))
3945 break;
3946 opt->jo_set |= JO_WAITTIME;
3947 opt->jo_waittime = get_tv_number(item);
3948 }
3949 else if (STRCMP(hi->hi_key, "timeout") == 0)
3950 {
3951 if (!(supported & JO_TIMEOUT))
3952 break;
3953 opt->jo_set |= JO_TIMEOUT;
3954 opt->jo_timeout = get_tv_number(item);
3955 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003956 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003957 {
3958 if (!(supported & JO_OUT_TIMEOUT))
3959 break;
3960 opt->jo_set |= JO_OUT_TIMEOUT;
3961 opt->jo_out_timeout = get_tv_number(item);
3962 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003963 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003964 {
3965 if (!(supported & JO_ERR_TIMEOUT))
3966 break;
3967 opt->jo_set |= JO_ERR_TIMEOUT;
3968 opt->jo_err_timeout = get_tv_number(item);
3969 }
3970 else if (STRCMP(hi->hi_key, "part") == 0)
3971 {
3972 if (!(supported & JO_PART))
3973 break;
3974 opt->jo_set |= JO_PART;
3975 val = get_tv_string(item);
3976 if (STRCMP(val, "err") == 0)
3977 opt->jo_part = PART_ERR;
3978 else
3979 {
3980 EMSG2(_(e_invarg2), val);
3981 return FAIL;
3982 }
3983 }
3984 else if (STRCMP(hi->hi_key, "id") == 0)
3985 {
3986 if (!(supported & JO_ID))
3987 break;
3988 opt->jo_set |= JO_ID;
3989 opt->jo_id = get_tv_number(item);
3990 }
3991 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3992 {
3993 if (!(supported & JO_STOPONEXIT))
3994 break;
3995 opt->jo_set |= JO_STOPONEXIT;
3996 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3997 opt->jo_soe_buf);
3998 if (opt->jo_stoponexit == NULL)
3999 {
4000 EMSG2(_(e_invarg2), "stoponexit");
4001 return FAIL;
4002 }
4003 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004004 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004005 {
4006 if (!(supported & JO_EXIT_CB))
4007 break;
4008 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004009 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
4010 {
4011 opt->jo_exit_partial = item->vval.v_partial;
4012 opt->jo_exit_cb = item->vval.v_partial->pt_name;
4013 }
4014 else
4015 opt->jo_exit_cb = get_tv_string_buf_chk(
4016 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004017 if (opt->jo_exit_cb == NULL)
4018 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004019 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004020 return FAIL;
4021 }
4022 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004023 else if (STRCMP(hi->hi_key, "block_write") == 0)
4024 {
4025 if (!(supported & JO_BLOCK_WRITE))
4026 break;
4027 opt->jo_set |= JO_BLOCK_WRITE;
4028 opt->jo_block_write = get_tv_number(item);
4029 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004030 else
4031 break;
4032 --todo;
4033 }
4034 if (todo > 0)
4035 {
4036 EMSG2(_(e_invarg2), hi->hi_key);
4037 return FAIL;
4038 }
4039
4040 return OK;
4041}
4042
4043/*
4044 * Get the channel from the argument.
4045 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004046 * When "check_open" is TRUE check that the channel can be used.
4047 * When "reading" is TRUE "check_open" considers typeahead useful.
4048 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004049 */
4050 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004051get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004052{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004053 channel_T *channel = NULL;
4054 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004055
4056 if (tv->v_type == VAR_JOB)
4057 {
4058 if (tv->vval.v_job != NULL)
4059 channel = tv->vval.v_job->jv_channel;
4060 }
4061 else if (tv->v_type == VAR_CHANNEL)
4062 {
4063 channel = tv->vval.v_channel;
4064 }
4065 else
4066 {
4067 EMSG2(_(e_invarg2), get_tv_string(tv));
4068 return NULL;
4069 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004070 if (channel != NULL && reading)
4071 has_readahead = channel_has_readahead(channel,
4072 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004073
Bram Moolenaar437905c2016-04-26 19:01:05 +02004074 if (check_open && (channel == NULL || (!channel_is_open(channel)
4075 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 {
4077 EMSG(_("E906: not an open channel"));
4078 return NULL;
4079 }
4080 return channel;
4081}
4082
4083static job_T *first_job = NULL;
4084
4085 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004086job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004087{
4088 ch_log(job->jv_channel, "Freeing job");
4089 if (job->jv_channel != NULL)
4090 {
4091 /* The link from the channel to the job doesn't count as a reference,
4092 * thus don't decrement the refcount of the job. The reference from
4093 * the job to the channel does count the refrence, decrement it and
4094 * NULL the reference. We don't set ch_job_killed, unreferencing the
4095 * job doesn't mean it stops running. */
4096 job->jv_channel->ch_job = NULL;
4097 channel_unref(job->jv_channel);
4098 }
4099 mch_clear_job(job);
4100
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004101 vim_free(job->jv_stoponexit);
4102 vim_free(job->jv_exit_cb);
4103 partial_unref(job->jv_exit_partial);
4104}
4105
4106 static void
4107job_free_job(job_T *job)
4108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004109 if (job->jv_next != NULL)
4110 job->jv_next->jv_prev = job->jv_prev;
4111 if (job->jv_prev == NULL)
4112 first_job = job->jv_next;
4113 else
4114 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004115 vim_free(job);
4116}
4117
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004118 static void
4119job_free(job_T *job)
4120{
4121 if (!in_free_unref_items)
4122 {
4123 job_free_contents(job);
4124 job_free_job(job);
4125 }
4126}
4127
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004128/*
4129 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004130 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4131 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004132 */
4133 static int
4134job_still_useful(job_T *job)
4135{
4136 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004137 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4138 || (job->jv_channel != NULL
4139 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004140}
4141
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004142/*
4143 * Mark references in jobs that are still useful.
4144 */
4145 int
4146set_ref_in_job(int copyID)
4147{
4148 int abort = FALSE;
4149 job_T *job;
4150 typval_T tv;
4151
4152 for (job = first_job; job != NULL; job = job->jv_next)
4153 if (job_still_useful(job))
4154 {
4155 tv.v_type = VAR_JOB;
4156 tv.vval.v_job = job;
4157 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4158 }
4159 return abort;
4160}
4161
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004162 void
4163job_unref(job_T *job)
4164{
4165 if (job != NULL && --job->jv_refcount <= 0)
4166 {
4167 /* Do not free the job when it has not ended yet and there is a
4168 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004169 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004170 {
4171 job_free(job);
4172 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004173 else if (job->jv_channel != NULL
4174 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004175 {
4176 /* Do remove the link to the channel, otherwise it hangs
4177 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004178 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004179 job->jv_channel->ch_job = NULL;
4180 channel_unref(job->jv_channel);
4181 job->jv_channel = NULL;
4182 }
4183 }
4184}
4185
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004186 int
4187free_unused_jobs_contents(int copyID, int mask)
4188{
4189 int did_free = FALSE;
4190 job_T *job;
4191
4192 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004193 if ((job->jv_copyID & mask) != (copyID & mask)
4194 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004195 {
4196 /* Free the channel and ordinary items it contains, but don't
4197 * recurse into Lists, Dictionaries etc. */
4198 job_free_contents(job);
4199 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004200 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004201 return did_free;
4202}
4203
4204 void
4205free_unused_jobs(int copyID, int mask)
4206{
4207 job_T *job;
4208 job_T *job_next;
4209
4210 for (job = first_job; job != NULL; job = job_next)
4211 {
4212 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004213 if ((job->jv_copyID & mask) != (copyID & mask)
4214 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004215 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004216 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004217 job_free_job(job);
4218 }
4219 }
4220}
4221
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004222/*
4223 * Allocate a job. Sets the refcount to one and sets options default.
4224 */
4225 static job_T *
4226job_alloc(void)
4227{
4228 job_T *job;
4229
4230 job = (job_T *)alloc_clear(sizeof(job_T));
4231 if (job != NULL)
4232 {
4233 job->jv_refcount = 1;
4234 job->jv_stoponexit = vim_strsave((char_u *)"term");
4235
4236 if (first_job != NULL)
4237 {
4238 first_job->jv_prev = job;
4239 job->jv_next = first_job;
4240 }
4241 first_job = job;
4242 }
4243 return job;
4244}
4245
4246 void
4247job_set_options(job_T *job, jobopt_T *opt)
4248{
4249 if (opt->jo_set & JO_STOPONEXIT)
4250 {
4251 vim_free(job->jv_stoponexit);
4252 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4253 job->jv_stoponexit = NULL;
4254 else
4255 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4256 }
4257 if (opt->jo_set & JO_EXIT_CB)
4258 {
4259 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004260 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004261 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004262 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004263 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004264 job->jv_exit_partial = NULL;
4265 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004266 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004267 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004268 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004269 job->jv_exit_partial = opt->jo_exit_partial;
4270 if (job->jv_exit_partial != NULL)
4271 ++job->jv_exit_partial->pt_refcount;
4272 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004273 }
4274}
4275
4276/*
4277 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4278 */
4279 void
4280job_stop_on_exit()
4281{
4282 job_T *job;
4283
4284 for (job = first_job; job != NULL; job = job->jv_next)
4285 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4286 mch_stop_job(job, job->jv_stoponexit);
4287}
4288
4289/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004290 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004291 */
4292 void
4293job_check_ended(void)
4294{
4295 static time_t last_check = 0;
4296 time_t now;
4297 job_T *job;
4298 job_T *next;
4299
4300 /* Only do this once in 10 seconds. */
4301 now = time(NULL);
4302 if (last_check + 10 < now)
4303 {
4304 last_check = now;
4305 for (job = first_job; job != NULL; job = next)
4306 {
4307 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004308 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004309 job_status(job); /* may free "job" */
4310 }
4311 }
4312}
4313
4314/*
4315 * "job_start()" function
4316 */
4317 job_T *
4318job_start(typval_T *argvars)
4319{
4320 job_T *job;
4321 char_u *cmd = NULL;
4322#if defined(UNIX)
4323# define USE_ARGV
4324 char **argv = NULL;
4325 int argc = 0;
4326#else
4327 garray_T ga;
4328#endif
4329 jobopt_T opt;
4330 int part;
4331
4332 job = job_alloc();
4333 if (job == NULL)
4334 return NULL;
4335
4336 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004337#ifndef USE_ARGV
4338 ga_init2(&ga, (int)sizeof(char*), 20);
4339#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004340
4341 /* Default mode is NL. */
4342 clear_job_options(&opt);
4343 opt.jo_mode = MODE_NL;
4344 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004345 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4346 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004347 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004348
4349 /* Check that when io is "file" that there is a file name. */
4350 for (part = PART_OUT; part <= PART_IN; ++part)
4351 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4352 && opt.jo_io[part] == JIO_FILE
4353 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4354 || *opt.jo_io_name[part] == NUL))
4355 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004356 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004357 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 }
4359
4360 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4361 {
4362 buf_T *buf = NULL;
4363
4364 /* check that we can find the buffer before starting the job */
4365 if (opt.jo_set & JO_IN_BUF)
4366 {
4367 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4368 if (buf == NULL)
4369 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4370 }
4371 else if (!(opt.jo_set & JO_IN_NAME))
4372 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004373 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004374 }
4375 else
4376 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4377 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004378 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004379 if (buf->b_ml.ml_mfp == NULL)
4380 {
4381 char_u numbuf[NUMBUFLEN];
4382 char_u *s;
4383
4384 if (opt.jo_set & JO_IN_BUF)
4385 {
4386 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4387 s = numbuf;
4388 }
4389 else
4390 s = opt.jo_io_name[PART_IN];
4391 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004392 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004393 }
4394 job->jv_in_buf = buf;
4395 }
4396
4397 job_set_options(job, &opt);
4398
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004399 if (argvars[0].v_type == VAR_STRING)
4400 {
4401 /* Command is a string. */
4402 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004403 if (cmd == NULL || *cmd == NUL)
4404 {
4405 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004406 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004407 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004408#ifdef USE_ARGV
4409 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004410 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004411 argv[argc] = NULL;
4412#endif
4413 }
4414 else if (argvars[0].v_type != VAR_LIST
4415 || argvars[0].vval.v_list == NULL
4416 || argvars[0].vval.v_list->lv_len < 1)
4417 {
4418 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004419 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004420 }
4421 else
4422 {
4423 list_T *l = argvars[0].vval.v_list;
4424 listitem_T *li;
4425 char_u *s;
4426
4427#ifdef USE_ARGV
4428 /* Pass argv[] to mch_call_shell(). */
4429 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4430 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004431 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004432#endif
4433 for (li = l->lv_first; li != NULL; li = li->li_next)
4434 {
4435 s = get_tv_string_chk(&li->li_tv);
4436 if (s == NULL)
4437 goto theend;
4438#ifdef USE_ARGV
4439 argv[argc++] = (char *)s;
4440#else
4441 /* Only escape when needed, double quotes are not always allowed. */
4442 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4443 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004444# ifdef WIN32
4445 int old_ssl = p_ssl;
4446
4447 /* This is using CreateProcess, not cmd.exe. Always use
4448 * double quote and backslashes. */
4449 p_ssl = 0;
4450# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004451 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004452# ifdef WIN32
4453 p_ssl = old_ssl;
4454# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004455 if (s == NULL)
4456 goto theend;
4457 ga_concat(&ga, s);
4458 vim_free(s);
4459 }
4460 else
4461 ga_concat(&ga, s);
4462 if (li->li_next != NULL)
4463 ga_append(&ga, ' ');
4464#endif
4465 }
4466#ifdef USE_ARGV
4467 argv[argc] = NULL;
4468#else
4469 cmd = ga.ga_data;
4470#endif
4471 }
4472
4473#ifdef USE_ARGV
4474 if (ch_log_active())
4475 {
4476 garray_T ga;
4477 int i;
4478
4479 ga_init2(&ga, (int)sizeof(char), 200);
4480 for (i = 0; i < argc; ++i)
4481 {
4482 if (i > 0)
4483 ga_concat(&ga, (char_u *)" ");
4484 ga_concat(&ga, (char_u *)argv[i]);
4485 }
4486 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4487 ga_clear(&ga);
4488 }
4489 mch_start_job(argv, job, &opt);
4490#else
4491 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4492 mch_start_job((char *)cmd, job, &opt);
4493#endif
4494
4495 /* If the channel is reading from a buffer, write lines now. */
4496 if (job->jv_channel != NULL)
4497 channel_write_in(job->jv_channel);
4498
4499theend:
4500#ifdef USE_ARGV
4501 vim_free(argv);
4502#else
4503 vim_free(ga.ga_data);
4504#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004505 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004506 return job;
4507}
4508
4509/*
4510 * Get the status of "job" and invoke the exit callback when needed.
4511 * The returned string is not allocated.
4512 */
4513 char *
4514job_status(job_T *job)
4515{
4516 char *result;
4517
4518 if (job->jv_status == JOB_ENDED)
4519 /* No need to check, dead is dead. */
4520 result = "dead";
4521 else if (job->jv_status == JOB_FAILED)
4522 result = "fail";
4523 else
4524 {
4525 result = mch_job_status(job);
4526 if (job->jv_status == JOB_ENDED)
4527 ch_log(job->jv_channel, "Job ended");
4528 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4529 {
4530 typval_T argv[3];
4531 typval_T rettv;
4532 int dummy;
4533
4534 /* invoke the exit callback; make sure the refcount is > 0 */
4535 ++job->jv_refcount;
4536 argv[0].v_type = VAR_JOB;
4537 argv[0].vval.v_job = job;
4538 argv[1].v_type = VAR_NUMBER;
4539 argv[1].vval.v_number = job->jv_exitval;
4540 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004541 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4542 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004543 clear_tv(&rettv);
4544 --job->jv_refcount;
4545 }
4546 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4547 {
4548 /* The job was already unreferenced, now that it ended it can be
4549 * freed. Careful: caller must not use "job" after this! */
4550 job_free(job);
4551 }
4552 }
4553 return result;
4554}
4555
Bram Moolenaar8950a562016-03-12 15:22:55 +01004556/*
4557 * Implementation of job_info().
4558 */
4559 void
4560job_info(job_T *job, dict_T *dict)
4561{
4562 dictitem_T *item;
4563 varnumber_T nr;
4564
4565 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4566
4567 item = dictitem_alloc((char_u *)"channel");
4568 if (item == NULL)
4569 return;
4570 item->di_tv.v_lock = 0;
4571 item->di_tv.v_type = VAR_CHANNEL;
4572 item->di_tv.vval.v_channel = job->jv_channel;
4573 if (job->jv_channel != NULL)
4574 ++job->jv_channel->ch_refcount;
4575 if (dict_add(dict, item) == FAIL)
4576 dictitem_free(item);
4577
4578#ifdef UNIX
4579 nr = job->jv_pid;
4580#else
4581 nr = job->jv_proc_info.dwProcessId;
4582#endif
4583 dict_add_nr_str(dict, "process", nr, NULL);
4584
4585 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004586 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004587 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4588}
4589
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004590 int
4591job_stop(job_T *job, typval_T *argvars)
4592{
4593 char_u *arg;
4594
4595 if (argvars[1].v_type == VAR_UNKNOWN)
4596 arg = (char_u *)"";
4597 else
4598 {
4599 arg = get_tv_string_chk(&argvars[1]);
4600 if (arg == NULL)
4601 {
4602 EMSG(_(e_invarg));
4603 return 0;
4604 }
4605 }
4606 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4607 if (mch_stop_job(job, arg) == FAIL)
4608 return 0;
4609
4610 /* Assume that "hup" does not kill the job. */
4611 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4612 job->jv_channel->ch_job_killed = TRUE;
4613
4614 /* We don't try freeing the job, obviously the caller still has a
4615 * reference to it. */
4616 return 1;
4617}
4618
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004619#endif /* FEAT_JOB_CHANNEL */