blob: 8433236227cf61d4a8e11eb9cfcae7df94d2040c [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
62
Bram Moolenaard8070362016-02-15 21:56:54 +010063#ifdef WIN32
64 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010065fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010066{
67 HANDLE h = (HANDLE)fd;
68 DWORD nread;
69
70 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
71 return -1;
72 return (int)nread;
73}
74
75 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010076fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010077{
78 HANDLE h = (HANDLE)fd;
79 DWORD nwrite;
80
81 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
82 return -1;
83 return (int)nwrite;
84}
85
86 static void
87fd_close(sock_T fd)
88{
89 HANDLE h = (HANDLE)fd;
90
91 CloseHandle(h);
92}
93#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010094
Bram Moolenaar6463ca22016-02-13 17:04:46 +010095/* Log file opened with ch_logfile(). */
96static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +010097#ifdef FEAT_RELTIME
98static proftime_T log_start;
99#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100
101 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100102ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100103{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100104 FILE *file = NULL;
105
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100106 if (log_fd != NULL)
107 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100108
109 if (*fname != NUL)
110 {
111 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
112 if (file == NULL)
113 {
114 EMSG2(_(e_notopen), fname);
115 return;
116 }
117 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100118 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100119
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100121 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100122 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100123#ifdef FEAT_RELTIME
124 profile_start(&log_start);
125#endif
126 }
127}
128
129 int
130ch_log_active()
131{
132 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100133}
134
135 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100136ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137{
138 if (log_fd != NULL)
139 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100140#ifdef FEAT_RELTIME
141 proftime_T log_now;
142
143 profile_start(&log_now);
144 profile_sub(&log_now, &log_start);
145 fprintf(log_fd, "%s ", profile_msg(&log_now));
146#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100147 if (ch != NULL)
148 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100149 else
150 fprintf(log_fd, "%s: ", what);
151 }
152}
153
Bram Moolenaard0b65022016-03-06 21:50:33 +0100154static int did_log_msg = TRUE;
155
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100156 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100157ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100158{
159 if (log_fd != NULL)
160 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100161 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100162 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100165 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100166 }
167}
168
169 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100170ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171{
172 if (log_fd != NULL)
173 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100174 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100176 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100177 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100178 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100179 }
180}
181
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100182 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100183ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184{
185 if (log_fd != NULL)
186 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100187 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100188 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
194
195 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100196ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197{
198 if (log_fd != NULL)
199 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100200 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100202 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100203 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100204 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100205 }
206}
207
208 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100209ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210{
211 if (log_fd != NULL)
212 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100213 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100215 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100216 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100217 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100218 }
219}
220
221 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100222ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223{
224 if (log_fd != NULL)
225 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100226 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100228 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100229 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100230 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100231 }
232}
233
234 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100235ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236{
237 if (log_fd != NULL)
238 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100239 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100240 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100241 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100242 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100243 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100244 }
245}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100246
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100247#ifdef _WIN32
248# undef PERROR
249# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
250 (char_u *)msg, (char_u *)strerror_win32(errno))
251
252 static char *
253strerror_win32(int eno)
254{
255 static LPVOID msgbuf = NULL;
256 char_u *ptr;
257
258 if (msgbuf)
259 LocalFree(msgbuf);
260 FormatMessage(
261 FORMAT_MESSAGE_ALLOCATE_BUFFER |
262 FORMAT_MESSAGE_FROM_SYSTEM |
263 FORMAT_MESSAGE_IGNORE_INSERTS,
264 NULL,
265 eno,
266 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
267 (LPTSTR) &msgbuf,
268 0,
269 NULL);
270 /* chomp \r or \n */
271 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
272 switch (*ptr)
273 {
274 case '\r':
275 STRMOVE(ptr, ptr + 1);
276 ptr--;
277 break;
278 case '\n':
279 if (*(ptr + 1) == '\0')
280 *ptr = '\0';
281 else
282 *ptr = ' ';
283 break;
284 }
285 return msgbuf;
286}
287#endif
288
Bram Moolenaar77073442016-02-13 23:23:53 +0100289/*
290 * The list of all allocated channels.
291 */
292static channel_T *first_channel = NULL;
293static int next_ch_id = 0;
294
295/*
296 * Allocate a new channel. The refcount is set to 1.
297 * The channel isn't actually used until it is opened.
298 * Returns NULL if out of memory.
299 */
300 channel_T *
301add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100302{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100303 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100304 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100305
Bram Moolenaar77073442016-02-13 23:23:53 +0100306 if (channel == NULL)
307 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308
Bram Moolenaar77073442016-02-13 23:23:53 +0100309 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100310 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100311
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100316 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317#endif
318#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100320#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100323
Bram Moolenaar77073442016-02-13 23:23:53 +0100324 if (first_channel != NULL)
325 {
326 first_channel->ch_prev = channel;
327 channel->ch_next = first_channel;
328 }
329 first_channel = channel;
330
331 channel->ch_refcount = 1;
332 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100333}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100334
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100335/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100336 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100337 * Return TRUE if "channel" has a callback and the associated job wasn't
338 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100339 */
340 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100341channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100342{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100344 int has_out_msg;
345 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100346
347 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100348 if (channel->ch_job_killed && channel->ch_job == NULL)
349 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100350
351 /* If there is a close callback it may still need to be invoked. */
352 if (channel->ch_close_cb != NULL)
353 return TRUE;
354
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200355 /* If reading from or a buffer it's still useful. */
356 if (channel->ch_part[PART_IN].ch_buffer != NULL)
357 return TRUE;
358
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100359 /* If there is no callback then nobody can get readahead. If the fd is
360 * closed and there is no readahead then the callback won't be called. */
361 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
362 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
363 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100364 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
365 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
366 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
367 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
368 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
369 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100370 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100371 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200372 || ((channel->ch_part[PART_OUT].ch_callback != NULL
373 || channel->ch_part[PART_OUT].ch_buffer) && has_out_msg)
374 || ((channel->ch_part[PART_ERR].ch_callback != NULL
375 || channel->ch_part[PART_ERR].ch_buffer) && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100376}
377
378/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200379 * Close a channel and free all its resources.
380 */
381 static void
382channel_free_contents(channel_T *channel)
383{
384 channel_close(channel, TRUE);
385 channel_clear(channel);
386 ch_log(channel, "Freeing channel");
387}
388
389 static void
390channel_free_channel(channel_T *channel)
391{
392 if (channel->ch_next != NULL)
393 channel->ch_next->ch_prev = channel->ch_prev;
394 if (channel->ch_prev == NULL)
395 first_channel = channel->ch_next;
396 else
397 channel->ch_prev->ch_next = channel->ch_next;
398 vim_free(channel);
399}
400
401 static void
402channel_free(channel_T *channel)
403{
404 if (!in_free_unref_items)
405 {
406 channel_free_contents(channel);
407 channel_free_channel(channel);
408 }
409}
410
411/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100412 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100413 * possible, there is no callback to be invoked or the associated job was
414 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100416 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100417 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100418channel_may_free(channel_T *channel)
419{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100420 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100421 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100422 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100423 return TRUE;
424 }
425 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100426}
427
428/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100429 * Decrement the reference count on "channel" and maybe free it when it goes
430 * down to zero. Don't free it if there is a pending action.
431 * Returns TRUE when the channel is no longer referenced.
432 */
433 int
434channel_unref(channel_T *channel)
435{
436 if (channel != NULL && --channel->ch_refcount <= 0)
437 return channel_may_free(channel);
438 return FALSE;
439}
440
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200441 int
442free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100443{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200444 int did_free = FALSE;
445 channel_T *ch;
446
447 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200448 if (!channel_still_useful(ch)
449 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200450 {
451 /* Free the channel and ordinary items it contains, but don't
452 * recurse into Lists, Dictionaries etc. */
453 channel_free_contents(ch);
454 did_free = TRUE;
455 }
456 return did_free;
457}
458
459 void
460free_unused_channels(int copyID, int mask)
461{
462 channel_T *ch;
463 channel_T *ch_next;
464
465 for (ch = first_channel; ch != NULL; ch = ch_next)
466 {
467 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200468 if (!channel_still_useful(ch)
469 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200471 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200472 channel_free_channel(ch);
473 }
474 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100475}
476
Bram Moolenaard04a0202016-01-26 23:30:18 +0100477#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100478
479#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
480 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100481channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100482{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100483 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100484 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100485
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100486 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100487 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100488 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100489 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100490 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100491}
492#endif
493
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100495 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100497#ifdef FEAT_GUI_X11
498 static void
499messageFromNetbeans(XtPointer clientData,
500 int *unused1 UNUSED,
501 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100502{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100503 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100504}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100505#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100506
Bram Moolenaard04a0202016-01-26 23:30:18 +0100507#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100508# if GTK_CHECK_VERSION(3,0,0)
509 static gboolean
510messageFromNetbeans(GIOChannel *unused1 UNUSED,
511 GIOCondition unused2 UNUSED,
512 gpointer clientData)
513{
514 channel_read_fd(GPOINTER_TO_INT(clientData));
515 return TRUE; /* Return FALSE instead in case the event source is to
516 * be removed after this function returns. */
517}
518# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100519 static void
520messageFromNetbeans(gpointer clientData,
521 gint unused1 UNUSED,
522 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100523{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100524 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100525}
Bram Moolenaar98921892016-02-23 17:14:37 +0100526# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100527#endif
528
529 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100530channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100531{
Bram Moolenaarde279892016-03-11 22:19:44 +0100532 if (!CH_HAS_GUI)
533 return;
534
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100535# ifdef FEAT_GUI_X11
536 /* Tell notifier we are interested in being called
537 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100538 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
539 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100540 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100541 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100542 (XtPointer)(XtInputReadMask + XtInputExceptMask),
543 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100544 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100545# else
546# ifdef FEAT_GUI_GTK
547 /* Tell gdk we are interested in being called when there
548 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100549 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100550# if GTK_CHECK_VERSION(3,0,0)
551 {
552 GIOChannel *chnnl = g_io_channel_unix_new(
553 (gint)channel->ch_part[part].ch_fd);
554
555 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
556 chnnl,
557 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
558 messageFromNetbeans,
559 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
560
561 g_io_channel_unref(chnnl);
562 }
563# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100564 channel->ch_part[part].ch_inputHandler = gdk_input_add(
565 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100566 (GdkInputCondition)
567 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100568 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100569 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100570# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100571# endif
572# endif
573}
574
Bram Moolenaarde279892016-03-11 22:19:44 +0100575 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100576channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100577{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100578 if (channel->CH_SOCK_FD != INVALID_FD)
579 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100580 if (channel->CH_OUT_FD != INVALID_FD)
581 channel_gui_register_one(channel, PART_OUT);
582 if (channel->CH_ERR_FD != INVALID_FD)
583 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100584}
585
586/*
587 * Register any of our file descriptors with the GUI event handling system.
588 * Called when the GUI has started.
589 */
590 void
591channel_gui_register_all(void)
592{
Bram Moolenaar77073442016-02-13 23:23:53 +0100593 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100594
Bram Moolenaar77073442016-02-13 23:23:53 +0100595 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100596 channel_gui_register(channel);
597}
598
599 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100600channel_gui_unregister_one(channel_T *channel, int part)
601{
602# ifdef FEAT_GUI_X11
603 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
604 {
605 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
606 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
607 }
608# else
609# ifdef FEAT_GUI_GTK
610 if (channel->ch_part[part].ch_inputHandler != 0)
611 {
612# if GTK_CHECK_VERSION(3,0,0)
613 g_source_remove(channel->ch_part[part].ch_inputHandler);
614# else
615 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
616# endif
617 channel->ch_part[part].ch_inputHandler = 0;
618 }
619# endif
620# endif
621}
622
623 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100624channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100625{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100626 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100627
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100628 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100629 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100630}
631
632#endif
633
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100634static char *e_cannot_connect = N_("E902: Cannot connect to port");
635
Bram Moolenaard04a0202016-01-26 23:30:18 +0100636/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100638 * "waittime" is the time in msec to wait for the connection.
639 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100640 * Returns the channel for success.
641 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100642 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100643 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100644channel_open(
645 char *hostname,
646 int port_in,
647 int waittime,
648 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100649{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100650 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100651 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100652 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100653#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100655 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100656#else
657 int port = port_in;
658#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100659 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100660 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100661
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100662#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100663 channel_init_winsock();
664#endif
665
Bram Moolenaar77073442016-02-13 23:23:53 +0100666 channel = add_channel();
667 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100668 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100669 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100670 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671 }
672
673 /* Get the server internet address and put into addr structure */
674 /* fill in the socket address structure and connect to server */
675 vim_memset((char *)&server, 0, sizeof(server));
676 server.sin_family = AF_INET;
677 server.sin_port = htons(port);
678 if ((host = gethostbyname(hostname)) == NULL)
679 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100681 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100682 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100683 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100684 }
685 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
686
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100687 /* On Mac and Solaris a zero timeout almost never works. At least wait
688 * one millisecond. Let's do it for all systems, because we don't know why
689 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100690 if (waittime == 0)
691 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100692
693 /*
694 * For Unix we need to call connect() again after connect() failed.
695 * On Win32 one time is sufficient.
696 */
697 while (TRUE)
698 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100699 long elapsed_msec = 0;
700 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100701
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100702 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100703 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100704 sd = socket(AF_INET, SOCK_STREAM, 0);
705 if (sd == -1)
706 {
707 ch_error(channel, "in socket() in channel_open().");
708 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100709 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100710 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100711 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100712
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100713 if (waittime >= 0)
714 {
715 /* Make connect() non-blocking. */
716 if (
717#ifdef _WIN32
718 ioctlsocket(sd, FIONBIO, &val) < 0
719#else
720 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
721#endif
722 )
723 {
724 SOCK_ERRNO;
725 ch_errorn(channel,
726 "channel_open: Connect failed with errno %d", errno);
727 sock_close(sd);
728 channel_free(channel);
729 return NULL;
730 }
731 }
732
733 /* Try connecting to the server. */
734 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
735 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
736
Bram Moolenaar045a2842016-03-08 22:33:07 +0100737 if (ret == 0)
738 /* The connection could be established. */
739 break;
740
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100741 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100742 if (waittime < 0 || (errno != EWOULDBLOCK
743 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100744#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100745 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100746#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100747 ))
748 {
749 ch_errorn(channel,
750 "channel_open: Connect failed with errno %d", errno);
751 PERROR(_(e_cannot_connect));
752 sock_close(sd);
753 channel_free(channel);
754 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100755 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100756
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100757 /* Limit the waittime to 50 msec. If it doesn't work within this
758 * time we close the socket and try creating it again. */
759 waitnow = waittime > 50 ? 50 : waittime;
760
Bram Moolenaar045a2842016-03-08 22:33:07 +0100761 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100762 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100763#ifndef WIN32
764 if (errno != ECONNREFUSED)
765#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100766 {
767 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100768 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100769 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100770#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100771 int so_error = 0;
772 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100773 struct timeval start_tv;
774 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100775#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100776 FD_ZERO(&rfds);
777 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100778 FD_ZERO(&wfds);
779 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100780
Bram Moolenaar562ca712016-03-09 21:50:05 +0100781 tv.tv_sec = waitnow / 1000;
782 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100783#ifndef WIN32
784 gettimeofday(&start_tv, NULL);
785#endif
786 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100787 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100788 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100789
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100790 if (ret < 0)
791 {
792 SOCK_ERRNO;
793 ch_errorn(channel,
794 "channel_open: Connect failed with errno %d", errno);
795 PERROR(_(e_cannot_connect));
796 sock_close(sd);
797 channel_free(channel);
798 return NULL;
799 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100800
Bram Moolenaare081e212016-02-28 22:33:46 +0100801#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100802 /* On Win32: select() is expected to work and wait for up to
803 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100804 if (FD_ISSET(sd, &wfds))
805 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100806 elapsed_msec = waitnow;
807 if (waittime > 1 && elapsed_msec < waittime)
808 {
809 waittime -= elapsed_msec;
810 continue;
811 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100812#else
813 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100814 * After putting the socket in non-blocking mode, connect() will
815 * return EINPROGRESS, select() will not wait (as if writing is
816 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100817 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100818 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100819 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100820 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100821 {
822 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100823 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100824 if (ret < 0 || (so_error != 0
825 && so_error != EWOULDBLOCK
826 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100827# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100828 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100829# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100830 ))
831 {
832 ch_errorn(channel,
833 "channel_open: Connect failed with errno %d",
834 so_error);
835 PERROR(_(e_cannot_connect));
836 sock_close(sd);
837 channel_free(channel);
838 return NULL;
839 }
840 }
841
Bram Moolenaar045a2842016-03-08 22:33:07 +0100842 if (FD_ISSET(sd, &wfds) && so_error == 0)
843 /* Did not detect an error, connection is established. */
844 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100845
Bram Moolenaar045a2842016-03-08 22:33:07 +0100846 gettimeofday(&end_tv, NULL);
847 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
848 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100849#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100850 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100851
852#ifndef WIN32
853 if (waittime > 1 && elapsed_msec < waittime)
854 {
855 /* The port isn't ready but we also didn't get an error.
856 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100857 * yet. Select() may return early, wait until the remaining
858 * "waitnow" and try again. */
859 waitnow -= elapsed_msec;
860 waittime -= elapsed_msec;
861 if (waitnow > 0)
862 {
863 mch_delay((long)waitnow, TRUE);
864 ui_breakcheck();
865 waittime -= waitnow;
866 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100867 if (!got_int)
868 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100869 if (waittime <= 0)
870 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100871 waittime = 1;
872 continue;
873 }
874 /* we were interrupted, behave as if timed out */
875 }
876#endif
877
878 /* We timed out. */
879 ch_error(channel, "Connection timed out");
880 sock_close(sd);
881 channel_free(channel);
882 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100883 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100884
Bram Moolenaar045a2842016-03-08 22:33:07 +0100885 ch_log(channel, "Connection made");
886
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100887 if (waittime >= 0)
888 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100889#ifdef _WIN32
890 val = 0;
891 ioctlsocket(sd, FIONBIO, &val);
892#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100893 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100894#endif
895 }
896
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100897 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100898 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100899 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
900 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100901
902#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100903 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100904#endif
905
Bram Moolenaar77073442016-02-13 23:23:53 +0100906 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100907}
908
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100909/*
910 * Implements ch_open().
911 */
912 channel_T *
913channel_open_func(typval_T *argvars)
914{
915 char_u *address;
916 char_u *p;
917 char *rest;
918 int port;
919 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200920 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100921
922 address = get_tv_string(&argvars[0]);
923 if (argvars[1].v_type != VAR_UNKNOWN
924 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
925 {
926 EMSG(_(e_invarg));
927 return NULL;
928 }
929
930 /* parse address */
931 p = vim_strchr(address, ':');
932 if (p == NULL)
933 {
934 EMSG2(_(e_invarg2), address);
935 return NULL;
936 }
937 *p++ = NUL;
938 port = strtol((char *)p, &rest, 10);
939 if (*address == NUL || port <= 0 || *rest != NUL)
940 {
941 p[-1] = ':';
942 EMSG2(_(e_invarg2), address);
943 return NULL;
944 }
945
946 /* parse options */
947 clear_job_options(&opt);
948 opt.jo_mode = MODE_JSON;
949 opt.jo_timeout = 2000;
950 if (get_job_options(&argvars[1], &opt,
951 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200952 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100953 if (opt.jo_timeout < 0)
954 {
955 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200956 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957 }
958
959 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
960 if (channel != NULL)
961 {
962 opt.jo_set = JO_ALL;
963 channel_set_options(channel, &opt);
964 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200965theend:
966 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100967 return channel;
968}
969
Bram Moolenaarde279892016-03-11 22:19:44 +0100970 static void
971may_close_part(sock_T *fd)
972{
973 if (*fd != INVALID_FD)
974 {
975 fd_close(*fd);
976 *fd = INVALID_FD;
977 }
978}
979
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100980 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100981channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100982{
Bram Moolenaarde279892016-03-11 22:19:44 +0100983 if (in != INVALID_FD)
984 {
985 may_close_part(&channel->CH_IN_FD);
986 channel->CH_IN_FD = in;
987 }
988 if (out != INVALID_FD)
989 {
990# if defined(FEAT_GUI)
991 channel_gui_unregister_one(channel, PART_OUT);
992# endif
993 may_close_part(&channel->CH_OUT_FD);
994 channel->CH_OUT_FD = out;
995# if defined(FEAT_GUI)
996 channel_gui_register_one(channel, PART_OUT);
997# endif
998 }
999 if (err != INVALID_FD)
1000 {
1001# if defined(FEAT_GUI)
1002 channel_gui_unregister_one(channel, PART_ERR);
1003# endif
1004 may_close_part(&channel->CH_ERR_FD);
1005 channel->CH_ERR_FD = err;
1006# if defined(FEAT_GUI)
1007 channel_gui_register_one(channel, PART_ERR);
1008# endif
1009 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001010}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001011
Bram Moolenaard6051b52016-02-28 15:49:03 +01001012/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001013 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001014 * This does not keep a refcount, when the job is freed ch_job is cleared.
1015 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001016 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001017channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001018{
Bram Moolenaar77073442016-02-13 23:23:53 +01001019 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001020
1021 channel_set_options(channel, options);
1022
1023 if (job->jv_in_buf != NULL)
1024 {
1025 chanpart_T *in_part = &channel->ch_part[PART_IN];
1026
1027 in_part->ch_buffer = job->jv_in_buf;
1028 ch_logs(channel, "reading from buffer '%s'",
1029 (char *)in_part->ch_buffer->b_ffname);
1030 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001031 {
1032 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1033 {
1034 /* Special mode: send last-but-one line when appending a line
1035 * to the buffer. */
1036 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001037 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001038 in_part->ch_buf_top =
1039 in_part->ch_buffer->b_ml.ml_line_count + 1;
1040 }
1041 else
1042 in_part->ch_buf_top = options->jo_in_top;
1043 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001044 else
1045 in_part->ch_buf_top = 1;
1046 if (options->jo_set & JO_IN_BOT)
1047 in_part->ch_buf_bot = options->jo_in_bot;
1048 else
1049 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1050 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001051}
1052
1053/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001054 * Find a buffer matching "name" or create a new one.
1055 */
1056 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001057find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001058{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001059 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001060 buf_T *save_curbuf = curbuf;
1061
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001062 if (name != NULL && *name != NUL)
1063 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001064 if (buf == NULL)
1065 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001066 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001067 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001068 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001069 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001070#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001071 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1072 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001073#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001074 if (curbuf->b_ml.ml_mfp == NULL)
1075 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001076 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1077 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001078 changed_bytes(1, 0);
1079 curbuf = save_curbuf;
1080 }
1081
1082 return buf;
1083}
1084
1085/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001086 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001087 */
1088 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001089channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001090{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001091 int part;
1092 char_u **cbp;
1093 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001094
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001095 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001096 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001097 channel->ch_part[part].ch_mode = opt->jo_mode;
1098 if (opt->jo_set & JO_IN_MODE)
1099 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1100 if (opt->jo_set & JO_OUT_MODE)
1101 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1102 if (opt->jo_set & JO_ERR_MODE)
1103 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001104
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001105 if (opt->jo_set & JO_TIMEOUT)
1106 for (part = PART_SOCK; part <= PART_IN; ++part)
1107 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1108 if (opt->jo_set & JO_OUT_TIMEOUT)
1109 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1110 if (opt->jo_set & JO_ERR_TIMEOUT)
1111 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001112 if (opt->jo_set & JO_BLOCK_WRITE)
1113 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001114
1115 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001116 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001117 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001118 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001119 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001120 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001121 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1122 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001123 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001124 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001125 *pp = opt->jo_partial;
1126 if (*pp != NULL)
1127 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001128 }
1129 if (opt->jo_set & JO_OUT_CALLBACK)
1130 {
1131 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001132 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001133 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001134 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001135 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1136 *cbp = vim_strsave(opt->jo_out_cb);
1137 else
1138 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001139 *pp = opt->jo_out_partial;
1140 if (*pp != NULL)
1141 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142 }
1143 if (opt->jo_set & JO_ERR_CALLBACK)
1144 {
1145 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001146 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001147 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001148 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1150 *cbp = vim_strsave(opt->jo_err_cb);
1151 else
1152 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001153 *pp = opt->jo_err_partial;
1154 if (*pp != NULL)
1155 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001156 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001157 if (opt->jo_set & JO_CLOSE_CALLBACK)
1158 {
1159 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001160 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001161 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001162 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001163 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1164 *cbp = vim_strsave(opt->jo_close_cb);
1165 else
1166 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001167 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001168 if (*pp != NULL)
1169 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001170 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001171
1172 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1173 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001174 /* writing output to a buffer. Default mode is NL. */
1175 if (!(opt->jo_set & JO_OUT_MODE))
1176 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001177 if (opt->jo_set & JO_OUT_BUF)
1178 channel->ch_part[PART_OUT].ch_buffer =
1179 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1180 else
1181 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001182 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1183 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001184 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1185 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001186
1187 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1188 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1189 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1190 {
1191 /* writing err to a buffer. Default mode is NL. */
1192 if (!(opt->jo_set & JO_ERR_MODE))
1193 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1194 if (opt->jo_io[PART_ERR] == JIO_OUT)
1195 channel->ch_part[PART_ERR].ch_buffer =
1196 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001197 else if (opt->jo_set & JO_ERR_BUF)
1198 channel->ch_part[PART_ERR].ch_buffer =
1199 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001200 else
1201 channel->ch_part[PART_ERR].ch_buffer =
1202 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1203 ch_logs(channel, "writing err to buffer '%s'",
1204 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1205 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001206
1207 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1208 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1209 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001210}
1211
1212/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001213 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001214 */
1215 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001216channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001217 channel_T *channel,
1218 int part,
1219 char_u *callback,
1220 partial_T *partial,
1221 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001222{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001223 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001224 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1225
1226 if (item != NULL)
1227 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001228 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001229 item->cq_partial = partial;
1230 if (partial != NULL)
1231 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001232 item->cq_seq_nr = id;
1233 item->cq_prev = head->cq_prev;
1234 head->cq_prev = item;
1235 item->cq_next = NULL;
1236 if (item->cq_prev == NULL)
1237 head->cq_next = item;
1238 else
1239 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001240 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001241}
1242
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001243 static void
1244write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1245{
1246 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001247 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001248 char_u *p;
1249
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001250 if ((p = alloc(len + 2)) == NULL)
1251 return;
1252 STRCPY(p, line);
1253 p[len] = NL;
1254 p[len + 1] = NUL;
1255 channel_send(channel, PART_IN, p, "write_buf_line()");
1256 vim_free(p);
1257}
1258
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001259/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001260 * Return TRUE if "channel" can be written to.
1261 * Returns FALSE if the input is closed or the write would block.
1262 */
1263 static int
1264can_write_buf_line(channel_T *channel)
1265{
1266 chanpart_T *in_part = &channel->ch_part[PART_IN];
1267
1268 if (in_part->ch_fd == INVALID_FD)
1269 return FALSE; /* pipe was closed */
1270
1271 /* for testing: block every other attempt to write */
1272 if (in_part->ch_block_write == 1)
1273 in_part->ch_block_write = -1;
1274 else if (in_part->ch_block_write == -1)
1275 in_part->ch_block_write = 1;
1276
1277 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1278#ifndef WIN32
1279 {
1280# if defined(HAVE_SELECT)
1281 struct timeval tval;
1282 fd_set wfds;
1283 int ret;
1284
1285 FD_ZERO(&wfds);
1286 FD_SET((int)in_part->ch_fd, &wfds);
1287 tval.tv_sec = 0;
1288 tval.tv_usec = 0;
1289 for (;;)
1290 {
1291 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1292# ifdef EINTR
1293 SOCK_ERRNO;
1294 if (ret == -1 && errno == EINTR)
1295 continue;
1296# endif
1297 if (ret <= 0 || in_part->ch_block_write == 1)
1298 {
1299 if (ret > 0)
1300 ch_log(channel, "FAKED Input not ready for writing");
1301 else
1302 ch_log(channel, "Input not ready for writing");
1303 return FALSE;
1304 }
1305 break;
1306 }
1307# else
1308 struct pollfd fds;
1309
1310 fds.fd = in_part->ch_fd;
1311 fds.events = POLLOUT;
1312 if (poll(&fds, 1, 0) <= 0)
1313 {
1314 ch_log(channel, "Input not ready for writing");
1315 return FALSE;
1316 }
1317 if (in_part->ch_block_write == 1)
1318 {
1319 ch_log(channel, "FAKED Input not ready for writing");
1320 return FALSE;
1321 }
1322# endif
1323 }
1324#endif
1325 return TRUE;
1326}
1327
1328/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001329 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001330 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001331 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001332channel_write_in(channel_T *channel)
1333{
1334 chanpart_T *in_part = &channel->ch_part[PART_IN];
1335 linenr_T lnum;
1336 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001337 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001338
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001339 if (buf == NULL || in_part->ch_buf_append)
1340 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001341 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1342 {
1343 /* buffer was wiped out or unloaded */
1344 in_part->ch_buffer = NULL;
1345 return;
1346 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001347
1348 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1349 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1350 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001351 if (!can_write_buf_line(channel))
1352 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001353 write_buf_line(buf, lnum, channel);
1354 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001355 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001356
1357 if (written == 1)
1358 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1359 else if (written > 1)
1360 ch_logn(channel, "written %d lines to channel", written);
1361
Bram Moolenaar014069a2016-03-03 22:51:40 +01001362 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001363 if (lnum > buf->b_ml.ml_line_count)
1364 {
1365 /* Writing is done, no longer need the buffer. */
1366 in_part->ch_buffer = NULL;
1367 ch_log(channel, "Finished writing all lines to channel");
1368 }
1369 else
1370 ch_logn(channel, "Still %d more lines to write",
1371 buf->b_ml.ml_line_count - lnum + 1);
1372}
1373
1374/*
1375 * Write any lines waiting to be written to a channel.
1376 */
1377 void
1378channel_write_any_lines()
1379{
1380 channel_T *channel;
1381
1382 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1383 {
1384 chanpart_T *in_part = &channel->ch_part[PART_IN];
1385
1386 if (in_part->ch_buffer != NULL)
1387 {
1388 if (in_part->ch_buf_append)
1389 channel_write_new_lines(in_part->ch_buffer);
1390 else
1391 channel_write_in(channel);
1392 }
1393 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001394}
1395
1396/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001397 * Write appended lines above the last one in "buf" to the channel.
1398 */
1399 void
1400channel_write_new_lines(buf_T *buf)
1401{
1402 channel_T *channel;
1403 int found_one = FALSE;
1404
1405 /* There could be more than one channel for the buffer, loop over all of
1406 * them. */
1407 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1408 {
1409 chanpart_T *in_part = &channel->ch_part[PART_IN];
1410 linenr_T lnum;
1411 int written = 0;
1412
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001413 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001414 {
1415 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001416 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001417 found_one = TRUE;
1418 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1419 ++lnum)
1420 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001421 if (!can_write_buf_line(channel))
1422 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001423 write_buf_line(buf, lnum, channel);
1424 ++written;
1425 }
1426
1427 if (written == 1)
1428 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1429 else if (written > 1)
1430 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001431 if (lnum < buf->b_ml.ml_line_count)
1432 ch_logn(channel, "Still %d more lines to write",
1433 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001434
1435 in_part->ch_buf_bot = lnum;
1436 }
1437 }
1438 if (!found_one)
1439 buf->b_write_to_channel = FALSE;
1440}
1441
1442/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001443 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001444 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001445 */
1446 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001447invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1448 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001449{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001450 typval_T rettv;
1451 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001452
Bram Moolenaar77073442016-02-13 23:23:53 +01001453 argv[0].v_type = VAR_CHANNEL;
1454 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001455
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001456 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001457 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001458 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001459 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001460}
1461
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001462/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001463 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001464 * The caller must free it.
1465 * Returns NULL if there is nothing.
1466 */
1467 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001468channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001469{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001470 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001471 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001472 char_u *p;
1473
Bram Moolenaar77073442016-02-13 23:23:53 +01001474 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001475 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001476 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001477 p = node->rq_buffer;
1478 head->rq_next = node->rq_next;
1479 if (node->rq_next == NULL)
1480 head->rq_prev = NULL;
1481 else
1482 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001483 vim_free(node);
1484 return p;
1485}
1486
1487/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001488 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001489 */
1490 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001491channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001492{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001493 readq_T *head = &channel->ch_part[part].ch_head;
1494 readq_T *node = head->rq_next;
1495 long_u len = 1;
1496 char_u *res;
1497 char_u *p;
1498
1499 /* If there is only one buffer just get that one. */
1500 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1501 return channel_get(channel, part);
1502
1503 /* Concatenate everything into one buffer. */
1504 for (node = head->rq_next; node != NULL; node = node->rq_next)
1505 len += (long_u)STRLEN(node->rq_buffer);
1506 res = lalloc(len, TRUE);
1507 if (res == NULL)
1508 return NULL;
1509 *res = NUL;
1510 for (node = head->rq_next; node != NULL; node = node->rq_next)
1511 STRCAT(res, node->rq_buffer);
1512
1513 /* Free all buffers */
1514 do
1515 {
1516 p = channel_get(channel, part);
1517 vim_free(p);
1518 } while (p != NULL);
1519
1520 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001521}
1522
1523/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001524 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001525 * Returns FAIL if that is not possible.
1526 */
1527 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001528channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001529{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001530 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001532 char_u *p;
1533
Bram Moolenaar77073442016-02-13 23:23:53 +01001534 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001535 return FAIL;
1536
Bram Moolenaar77073442016-02-13 23:23:53 +01001537 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1538 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539 if (p == NULL)
1540 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001541 STRCPY(p, node->rq_buffer);
1542 STRCAT(p, node->rq_next->rq_buffer);
1543 vim_free(node->rq_next->rq_buffer);
1544 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001545
Bram Moolenaar77073442016-02-13 23:23:53 +01001546 /* dispose of the node and its buffer */
1547 head->rq_next = node->rq_next;
1548 head->rq_next->rq_prev = NULL;
1549 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001550 vim_free(node);
1551 return OK;
1552}
1553
1554/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001555 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001556 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001557 * Returns OK or FAIL.
1558 */
1559 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001560channel_save(channel_T *channel, int part, char_u *buf, int len,
1561 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001562{
1563 readq_T *node;
1564 readq_T *head = &channel->ch_part[part].ch_head;
1565 char_u *p;
1566 int i;
1567
1568 node = (readq_T *)alloc(sizeof(readq_T));
1569 if (node == NULL)
1570 return FAIL; /* out of memory */
1571 node->rq_buffer = alloc(len + 1);
1572 if (node->rq_buffer == NULL)
1573 {
1574 vim_free(node);
1575 return FAIL; /* out of memory */
1576 }
1577
1578 if (channel->ch_part[part].ch_mode == MODE_NL)
1579 {
1580 /* Drop any CR before a NL. */
1581 p = node->rq_buffer;
1582 for (i = 0; i < len; ++i)
1583 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1584 *p++ = buf[i];
1585 *p = NUL;
1586 }
1587 else
1588 {
1589 mch_memmove(node->rq_buffer, buf, len);
1590 node->rq_buffer[len] = NUL;
1591 }
1592
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001593 if (prepend)
1594 {
1595 /* preend node to the head of the queue */
1596 node->rq_next = head->rq_next;
1597 node->rq_prev = NULL;
1598 if (head->rq_next == NULL)
1599 head->rq_prev = node;
1600 else
1601 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001602 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001603 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001604 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001605 {
1606 /* append node to the tail of the queue */
1607 node->rq_next = NULL;
1608 node->rq_prev = head->rq_prev;
1609 if (head->rq_prev == NULL)
1610 head->rq_next = node;
1611 else
1612 head->rq_prev->rq_next = node;
1613 head->rq_prev = node;
1614 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001615
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001616 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001617 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001618 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001619 fprintf(log_fd, "'");
1620 if (fwrite(buf, len, 1, log_fd) != 1)
1621 return FAIL;
1622 fprintf(log_fd, "'\n");
1623 }
1624 return OK;
1625}
1626
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001627 static int
1628channel_fill(js_read_T *reader)
1629{
1630 channel_T *channel = (channel_T *)reader->js_cookie;
1631 int part = reader->js_cookie_arg;
1632 char_u *next = channel_get(channel, part);
1633 int unused;
1634 int len;
1635 char_u *p;
1636
1637 if (next == NULL)
1638 return FALSE;
1639
1640 unused = reader->js_end - reader->js_buf - reader->js_used;
1641 if (unused > 0)
1642 {
1643 /* Prepend unused text. */
1644 len = (int)STRLEN(next);
1645 p = alloc(unused + len + 1);
1646 if (p == NULL)
1647 {
1648 vim_free(next);
1649 return FALSE;
1650 }
1651 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1652 mch_memmove(p + unused, next, len + 1);
1653 vim_free(next);
1654 next = p;
1655 }
1656
1657 vim_free(reader->js_buf);
1658 reader->js_buf = next;
1659 reader->js_used = 0;
1660 return TRUE;
1661}
1662
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001663/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001664 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001665 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001666 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001667 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001668 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001669channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001670{
1671 js_read_T reader;
1672 typval_T listtv;
1673 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001674 chanpart_T *chanpart = &channel->ch_part[part];
1675 jsonq_T *head = &chanpart->ch_json_head;
1676 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001677 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001678
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001679 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001680 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001682 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001683 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001684 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001685 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001686 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001687
1688 /* When a message is incomplete we wait for a short while for more to
1689 * arrive. After the delay drop the input, otherwise a truncated string
1690 * or list will make us hang. */
1691 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001692 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001693 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001694 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001695 /* Only accept the response when it is a list with at least two
1696 * items. */
1697 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001698 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001699 if (listtv.v_type != VAR_LIST)
1700 ch_error(channel, "Did not receive a list, discarding");
1701 else
1702 ch_errorn(channel, "Expected list with two items, got %d",
1703 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001704 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001705 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706 else
1707 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001708 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1709 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001710 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001711 else
1712 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001713 item->jq_value = alloc_tv();
1714 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001715 {
1716 vim_free(item);
1717 clear_tv(&listtv);
1718 }
1719 else
1720 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001721 *item->jq_value = listtv;
1722 item->jq_prev = head->jq_prev;
1723 head->jq_prev = item;
1724 item->jq_next = NULL;
1725 if (item->jq_prev == NULL)
1726 head->jq_next = item;
1727 else
1728 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001729 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001730 }
1731 }
1732 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001733
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001734 if (status == OK)
1735 chanpart->ch_waiting = FALSE;
1736 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001737 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001738 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001739 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001740 /* First time encountering incomplete message, set a deadline of
1741 * 100 msec. */
1742 ch_log(channel, "Incomplete message - wait for more");
1743 reader.js_used = 0;
1744 chanpart->ch_waiting = TRUE;
1745#ifdef WIN32
1746 chanpart->ch_deadline = GetTickCount() + 100L;
1747#else
1748 gettimeofday(&chanpart->ch_deadline, NULL);
1749 chanpart->ch_deadline.tv_usec += 100 * 1000;
1750 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1751 {
1752 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1753 ++chanpart->ch_deadline.tv_sec;
1754 }
1755#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001756 }
1757 else
1758 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001759 int timeout;
1760#ifdef WIN32
1761 timeout = GetTickCount() > chanpart->ch_deadline;
1762#else
1763 {
1764 struct timeval now_tv;
1765
1766 gettimeofday(&now_tv, NULL);
1767 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1768 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1769 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1770 }
1771#endif
1772 if (timeout)
1773 {
1774 status = FAIL;
1775 chanpart->ch_waiting = FALSE;
1776 }
1777 else
1778 {
1779 reader.js_used = 0;
1780 ch_log(channel, "still waiting on incomplete message");
1781 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001782 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001783 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001784
1785 if (status == FAIL)
1786 {
1787 ch_error(channel, "Decoding failed - discarding input");
1788 ret = FALSE;
1789 chanpart->ch_waiting = FALSE;
1790 }
1791 else if (reader.js_buf[reader.js_used] != NUL)
1792 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001793 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001794 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001795 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1796 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001797 ret = status == MAYBE ? FALSE: TRUE;
1798 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001799 else
1800 ret = FALSE;
1801
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001802 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001803 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001804}
1805
1806/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001807 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001808 */
1809 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001810remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001811{
Bram Moolenaar77073442016-02-13 23:23:53 +01001812 if (node->cq_prev == NULL)
1813 head->cq_next = node->cq_next;
1814 else
1815 node->cq_prev->cq_next = node->cq_next;
1816 if (node->cq_next == NULL)
1817 head->cq_prev = node->cq_prev;
1818 else
1819 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001820}
1821
1822/*
1823 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001824 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001825 */
1826 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001827remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001828{
Bram Moolenaar77073442016-02-13 23:23:53 +01001829 if (node->jq_prev == NULL)
1830 head->jq_next = node->jq_next;
1831 else
1832 node->jq_prev->jq_next = node->jq_next;
1833 if (node->jq_next == NULL)
1834 head->jq_prev = node->jq_prev;
1835 else
1836 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001837 vim_free(node);
1838}
1839
1840/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001841 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001842 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001843 * When "id" is zero or negative jut get the first message. But not the one
1844 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001845 * Return OK when found and return the value in "rettv".
1846 * Return FAIL otherwise.
1847 */
1848 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001849channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001850{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001851 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001852 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001853
Bram Moolenaar77073442016-02-13 23:23:53 +01001854 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001855 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001856 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857 typval_T *tv = &l->lv_first->li_tv;
1858
1859 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001860 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001861 || tv->vval.v_number == 0
1862 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001863 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001864 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001865 if (tv->v_type == VAR_NUMBER)
1866 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001867 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001868 return OK;
1869 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001870 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001871 }
1872 return FAIL;
1873}
1874
Bram Moolenaarece61b02016-02-20 21:39:05 +01001875#define CH_JSON_MAX_ARGS 4
1876
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001877/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001878 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001879 * "argv[0]" is the command string.
1880 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001881 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001882 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001884{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001885 char_u *cmd = argv[0].vval.v_string;
1886 char_u *arg;
1887 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001888
Bram Moolenaarece61b02016-02-20 21:39:05 +01001889 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001890 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001891 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001892 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001893 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001894 return;
1895 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001896 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001897 if (arg == NULL)
1898 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001899
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001900 if (STRCMP(cmd, "ex") == 0)
1901 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001902 int save_called_emsg = called_emsg;
1903
1904 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001905 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001906 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001907 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001908 --emsg_silent;
1909 if (called_emsg)
1910 ch_logs(channel, "Ex command error: '%s'",
1911 (char *)get_vim_var_str(VV_ERRMSG));
1912 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001913 }
1914 else if (STRCMP(cmd, "normal") == 0)
1915 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001916 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001917
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001918 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001919 ea.arg = arg;
1920 ea.addr_count = 0;
1921 ea.forceit = TRUE; /* no mapping */
1922 ex_normal(&ea);
1923 }
1924 else if (STRCMP(cmd, "redraw") == 0)
1925 {
1926 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001927
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001928 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001929 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001930 ex_redraw(&ea);
1931 showruler(FALSE);
1932 setcursor();
1933 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001934#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001935 if (gui.in_use)
1936 {
1937 gui_update_cursor(FALSE, FALSE);
1938 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001939 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001940#endif
1941 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001942 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001943 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001944 int is_call = cmd[0] == 'c';
1945 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001946
Bram Moolenaarece61b02016-02-20 21:39:05 +01001947 if (argv[id_idx].v_type != VAR_UNKNOWN
1948 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001949 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001950 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001951 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001952 EMSG("E904: last argument for expr/call must be a number");
1953 }
1954 else if (is_call && argv[2].v_type != VAR_LIST)
1955 {
1956 ch_error(channel, "third argument for call must be a list");
1957 if (p_verbose > 2)
1958 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001959 }
1960 else
1961 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001962 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001963 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001964 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001965 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001966
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001967 /* Don't pollute the display with errors. */
1968 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001969 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001970 {
1971 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001972 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001973 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001974 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001975 {
1976 ch_logs(channel, "Calling '%s'", (char *)arg);
1977 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1978 tv = &res_tv;
1979 else
1980 tv = NULL;
1981 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001982
1983 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001984 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001985 int id = argv[id_idx].vval.v_number;
1986
Bram Moolenaar55fab432016-02-07 16:53:13 +01001987 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001988 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001989 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001990 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001991 /* If evaluation failed or the result can't be encoded
1992 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001993 vim_free(json);
1994 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001995 err_tv.v_type = VAR_STRING;
1996 err_tv.vval.v_string = (char_u *)"ERROR";
1997 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001998 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001999 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002000 if (json != NULL)
2001 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002002 channel_send(channel,
2003 part == PART_SOCK ? PART_SOCK : PART_IN,
2004 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002005 vim_free(json);
2006 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002008 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002009 if (tv == &res_tv)
2010 clear_tv(tv);
2011 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002012 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002013 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002014 }
2015 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002016 {
2017 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002018 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002019 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002020}
2021
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002022/*
2023 * Invoke the callback at "cbhead".
2024 * Does not redraw but sets channel_need_redraw.
2025 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002026 static void
2027invoke_one_time_callback(
2028 channel_T *channel,
2029 cbq_T *cbhead,
2030 cbq_T *item,
2031 typval_T *argv)
2032{
2033 ch_logs(channel, "Invoking one-time callback %s",
2034 (char *)item->cq_callback);
2035 /* Remove the item from the list first, if the callback
2036 * invokes ch_close() the list will be cleared. */
2037 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002038 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002039 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002040 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002041 vim_free(item);
2042}
2043
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002044 static void
2045append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2046{
2047 buf_T *save_curbuf = curbuf;
2048 linenr_T lnum = buffer->b_ml.ml_line_count;
2049 int save_write_to = buffer->b_write_to_channel;
2050
2051 /* If the buffer is also used as input insert above the last
2052 * line. Don't write these lines. */
2053 if (save_write_to)
2054 {
2055 --lnum;
2056 buffer->b_write_to_channel = FALSE;
2057 }
2058
2059 /* Append to the buffer */
2060 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2061
2062 curbuf = buffer;
2063 u_sync(TRUE);
2064 /* ignore undo failure, undo is not very useful here */
2065 ignored = u_save(lnum, lnum + 1);
2066
2067 ml_append(lnum, msg, 0, FALSE);
2068 appended_lines_mark(lnum, 1L);
2069 curbuf = save_curbuf;
2070
2071 if (buffer->b_nwindows > 0)
2072 {
2073 win_T *wp;
2074 win_T *save_curwin;
2075
2076 FOR_ALL_WINDOWS(wp)
2077 {
2078 if (wp->w_buffer == buffer
2079 && (save_write_to
2080 ? wp->w_cursor.lnum == lnum + 1
2081 : (wp->w_cursor.lnum == lnum
2082 && wp->w_cursor.col == 0)))
2083 {
2084 ++wp->w_cursor.lnum;
2085 save_curwin = curwin;
2086 curwin = wp;
2087 curbuf = curwin->w_buffer;
2088 scroll_cursor_bot(0, FALSE);
2089 curwin = save_curwin;
2090 curbuf = curwin->w_buffer;
2091 }
2092 }
2093 redraw_buf_later(buffer, VALID);
2094 channel_need_redraw = TRUE;
2095 }
2096
2097 if (save_write_to)
2098 {
2099 channel_T *ch;
2100
2101 /* Find channels reading from this buffer and adjust their
2102 * next-to-read line number. */
2103 buffer->b_write_to_channel = TRUE;
2104 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2105 {
2106 chanpart_T *in_part = &ch->ch_part[PART_IN];
2107
2108 if (in_part->ch_buffer == buffer)
2109 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2110 }
2111 }
2112}
2113
Bram Moolenaar437905c2016-04-26 19:01:05 +02002114 static void
2115drop_messages(channel_T *channel, int part)
2116{
2117 char_u *msg;
2118
2119 while ((msg = channel_get(channel, part)) != NULL)
2120 {
2121 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2122 vim_free(msg);
2123 }
2124}
2125
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002126/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002127 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002128 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002129 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002130 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002131 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002132may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002133{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002134 char_u *msg = NULL;
2135 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002136 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002137 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002138 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002139 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002140 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002141 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002142 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002143 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002144
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002145 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002146 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002147 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002148
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002149 /* Use a message-specific callback, part callback or channel callback */
2150 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2151 if (cbitem->cq_seq_nr == 0)
2152 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002153 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002154 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002155 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002156 partial = cbitem->cq_partial;
2157 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002158 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002159 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002160 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002161 partial = channel->ch_part[part].ch_partial;
2162 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002163 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002164 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002165 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002166 partial = channel->ch_partial;
2167 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002168
Bram Moolenaar187db502016-02-27 14:44:26 +01002169 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002170 if (buffer != NULL && !buf_valid(buffer))
2171 {
2172 /* buffer was wiped out */
2173 channel->ch_part[part].ch_buffer = NULL;
2174 buffer = NULL;
2175 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002176
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002177 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002178 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002179 listitem_T *item;
2180 int argc = 0;
2181
Bram Moolenaard7ece102016-02-02 23:23:02 +01002182 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002183 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002184 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002185 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002186 channel_parse_json(channel, part);
2187 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002188 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002189 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002190
Bram Moolenaarece61b02016-02-20 21:39:05 +01002191 for (item = listtv->vval.v_list->lv_first;
2192 item != NULL && argc < CH_JSON_MAX_ARGS;
2193 item = item->li_next)
2194 argv[argc++] = item->li_tv;
2195 while (argc < CH_JSON_MAX_ARGS)
2196 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002197
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 if (argv[0].v_type == VAR_STRING)
2199 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002201 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002202 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002203 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002204 }
2205
Bram Moolenaarece61b02016-02-20 21:39:05 +01002206 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002207 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002208 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002209 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002210 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002211 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002212 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002213 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002214 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002215 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002216 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002217 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002218 return FALSE;
2219 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002220 else
2221 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002222 /* If there is no callback or buffer drop the message. */
2223 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002224 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002225 /* If there is a close callback it may use ch_read() to get the
2226 * messages. */
2227 if (channel->ch_close_cb == NULL)
2228 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002229 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002230 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002231
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002232 if (ch_mode == MODE_NL)
2233 {
2234 char_u *nl;
2235 char_u *buf;
2236
2237 /* See if we have a message ending in NL in the first buffer. If
2238 * not try to concatenate the first and the second buffer. */
2239 while (TRUE)
2240 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002241 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002242 nl = vim_strchr(buf, NL);
2243 if (nl != NULL)
2244 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002245 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002246 return FALSE; /* incomplete message */
2247 }
2248 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002249 {
2250 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002251 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002252 *nl = NUL;
2253 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002254 else
2255 {
2256 /* Copy the message into allocated memory and remove it from
2257 * the buffer. */
2258 msg = vim_strnsave(buf, (int)(nl - buf));
2259 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2260 }
2261 }
2262 else
2263 /* For a raw channel we don't know where the message ends, just
2264 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002265 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002266
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002267 if (msg == NULL)
2268 return FALSE; /* out of memory (and avoids Coverity warning) */
2269
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002270 argv[1].v_type = VAR_STRING;
2271 argv[1].vval.v_string = msg;
2272 }
2273
Bram Moolenaara07fec92016-02-05 21:04:08 +01002274 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002275 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002276 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002277
2278 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002279 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002280 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002281 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002282 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002283 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002284 break;
2285 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002286 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002287 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002288 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002289 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002290 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002291 if (buffer != NULL)
2292 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002293 if (msg == NULL)
2294 /* JSON or JS mode: re-encode the message. */
2295 msg = json_encode(listtv, ch_mode);
2296 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002297 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002298 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002299
Bram Moolenaar187db502016-02-27 14:44:26 +01002300 if (callback != NULL)
2301 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002302 if (cbitem != NULL)
2303 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2304 else
2305 {
2306 /* invoke the channel callback */
2307 ch_logs(channel, "Invoking channel callback %s",
2308 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002309 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002310 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002311 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002312 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002313 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002314 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002315
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002316 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002317 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002318 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002319
2320 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002321}
2322
2323/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002324 * Return TRUE when channel "channel" is open for writing to.
2325 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002326 */
2327 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002328channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002329{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002330 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002331 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002332}
2333
2334/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002335 * Return TRUE when channel "channel" is open for reading or writing.
2336 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002337 */
2338 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002339channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002340{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002341 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002342 || channel->CH_IN_FD != INVALID_FD
2343 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002344 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002345}
2346
2347/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002348 * Return TRUE if "channel" has JSON or other typeahead.
2349 */
2350 static int
2351channel_has_readahead(channel_T *channel, int part)
2352{
2353 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2354
2355 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2356 {
2357 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2358 jsonq_T *item = head->jq_next;
2359
2360 return item != NULL;
2361 }
2362 return channel_peek(channel, part) != NULL;
2363}
2364
2365/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002366 * Return a string indicating the status of the channel.
2367 */
2368 char *
2369channel_status(channel_T *channel)
2370{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002371 int part;
2372 int has_readahead = FALSE;
2373
Bram Moolenaar77073442016-02-13 23:23:53 +01002374 if (channel == NULL)
2375 return "fail";
2376 if (channel_is_open(channel))
2377 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002378 for (part = PART_SOCK; part <= PART_ERR; ++part)
2379 if (channel_has_readahead(channel, part))
2380 {
2381 has_readahead = TRUE;
2382 break;
2383 }
2384
2385 if (has_readahead)
2386 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002387 return "closed";
2388}
2389
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002390 static void
2391channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2392{
2393 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002394 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002395 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002396 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002397
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002398 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002399 STRCAT(namebuf, "_");
2400 tail = STRLEN(namebuf);
2401
2402 STRCPY(namebuf + tail, "status");
2403 dict_add_nr_str(dict, namebuf, 0,
2404 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2405
2406 STRCPY(namebuf + tail, "mode");
2407 switch (chanpart->ch_mode)
2408 {
2409 case MODE_NL: s = "NL"; break;
2410 case MODE_RAW: s = "RAW"; break;
2411 case MODE_JSON: s = "JSON"; break;
2412 case MODE_JS: s = "JS"; break;
2413 }
2414 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2415
2416 STRCPY(namebuf + tail, "io");
2417 if (part == PART_SOCK)
2418 s = "socket";
2419 else switch (chanpart->ch_io)
2420 {
2421 case JIO_NULL: s = "null"; break;
2422 case JIO_PIPE: s = "pipe"; break;
2423 case JIO_FILE: s = "file"; break;
2424 case JIO_BUFFER: s = "buffer"; break;
2425 case JIO_OUT: s = "out"; break;
2426 }
2427 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2428
2429 STRCPY(namebuf + tail, "timeout");
2430 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2431}
2432
2433 void
2434channel_info(channel_T *channel, dict_T *dict)
2435{
2436 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2437 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2438
2439 if (channel->ch_hostname != NULL)
2440 {
2441 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2442 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2443 channel_part_info(channel, dict, "sock", PART_SOCK);
2444 }
2445 else
2446 {
2447 channel_part_info(channel, dict, "out", PART_OUT);
2448 channel_part_info(channel, dict, "err", PART_ERR);
2449 channel_part_info(channel, dict, "in", PART_IN);
2450 }
2451}
2452
Bram Moolenaar77073442016-02-13 23:23:53 +01002453/*
2454 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002455 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002456 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002457 */
2458 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002459channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002460{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002461 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002462
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002463#ifdef FEAT_GUI
2464 channel_gui_unregister(channel);
2465#endif
2466
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002467 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002468 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002469 sock_close(channel->CH_SOCK_FD);
2470 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002471 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002472 may_close_part(&channel->CH_IN_FD);
2473 may_close_part(&channel->CH_OUT_FD);
2474 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002475
Bram Moolenaar8b374212016-02-24 20:43:06 +01002476 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002477 {
2478 typval_T argv[1];
2479 typval_T rettv;
2480 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002481 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002482
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002483 /* Invoke callbacks before the close callback, since it's weird to
2484 * first invoke the close callback. Increment the refcount to avoid
2485 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002486 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002487 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002488 for (part = PART_SOCK; part <= PART_ERR; ++part)
2489 while (may_invoke_callback(channel, part))
2490 ;
2491
2492 /* Invoke the close callback, if still set. */
2493 if (channel->ch_close_cb != NULL)
2494 {
2495 ch_logs(channel, "Invoking close callback %s",
2496 (char *)channel->ch_close_cb);
2497 argv[0].v_type = VAR_CHANNEL;
2498 argv[0].vval.v_channel = channel;
2499 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002500 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2501 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002502 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002503 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002504 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002505 --channel->ch_refcount;
2506
2507 /* the callback is only called once */
2508 vim_free(channel->ch_close_cb);
2509 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002510 partial_unref(channel->ch_close_partial);
2511 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002512
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002513 if (channel_need_redraw)
2514 {
2515 channel_need_redraw = FALSE;
2516 redraw_after_callback();
2517 }
2518
Bram Moolenaar437905c2016-04-26 19:01:05 +02002519 /* any remaining messages are useless now */
2520 for (part = PART_SOCK; part <= PART_ERR; ++part)
2521 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002522 }
2523
2524 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002525}
2526
Bram Moolenaard04a0202016-01-26 23:30:18 +01002527/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002528 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002529 * Returns NULL if there is nothing.
2530 */
2531 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002532channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002534 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002535
Bram Moolenaar77073442016-02-13 23:23:53 +01002536 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002537 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002538 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002539}
2540
2541/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002542 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002543 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002544 static void
2545channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002546{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002547 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2548 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002549
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002550 while (channel_peek(channel, part) != NULL)
2551 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002552
2553 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002554 {
2555 cbq_T *node = cb_head->cq_next;
2556
2557 remove_cb_node(cb_head, node);
2558 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002559 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002560 vim_free(node);
2561 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002562
2563 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002564 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002565 free_tv(json_head->jq_next->jq_value);
2566 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002567 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002568
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002569 vim_free(channel->ch_part[part].ch_callback);
2570 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002571 partial_unref(channel->ch_part[part].ch_partial);
2572 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002573}
2574
2575/*
2576 * Clear all the read buffers on "channel".
2577 */
2578 void
2579channel_clear(channel_T *channel)
2580{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002581 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002582 vim_free(channel->ch_hostname);
2583 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002584 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002585 channel_clear_one(channel, PART_OUT);
2586 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002587 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002588 vim_free(channel->ch_callback);
2589 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002590 partial_unref(channel->ch_partial);
2591 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002592 vim_free(channel->ch_close_cb);
2593 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002594 partial_unref(channel->ch_close_partial);
2595 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002596}
2597
Bram Moolenaar77073442016-02-13 23:23:53 +01002598#if defined(EXITFREE) || defined(PROTO)
2599 void
2600channel_free_all(void)
2601{
2602 channel_T *channel;
2603
Bram Moolenaard6051b52016-02-28 15:49:03 +01002604 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002605 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2606 channel_clear(channel);
2607}
2608#endif
2609
2610
Bram Moolenaar715d2852016-04-30 17:06:31 +02002611/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002612#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002613
2614/* Buffer size for reading incoming messages. */
2615#define MAXMSGSIZE 4096
2616
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002617#if defined(HAVE_SELECT)
2618/*
2619 * Add write fds where we are waiting for writing to be possible.
2620 */
2621 static int
2622channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2623{
2624 int maxfd = maxfd_arg;
2625 channel_T *ch;
2626
2627 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2628 {
2629 chanpart_T *in_part = &ch->ch_part[PART_IN];
2630
2631 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2632 {
2633 FD_SET((int)in_part->ch_fd, wfds);
2634 if ((int)in_part->ch_fd >= maxfd)
2635 maxfd = (int)in_part->ch_fd + 1;
2636 }
2637 }
2638 return maxfd;
2639}
2640#else
2641/*
2642 * Add write fds where we are waiting for writing to be possible.
2643 */
2644 static int
2645channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2646{
2647 int nfd = nfd_in;
2648 channel_T *ch;
2649
2650 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2651 {
2652 chanpart_T *in_part = &ch->ch_part[PART_IN];
2653
2654 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2655 {
2656 in_part->ch_poll_idx = nfd;
2657 fds[nfd].fd = in_part->ch_fd;
2658 fds[nfd].events = POLLOUT;
2659 ++nfd;
2660 }
2661 else
2662 in_part->ch_poll_idx = -1;
2663 }
2664 return nfd;
2665}
2666#endif
2667
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002668typedef enum {
2669 CW_READY,
2670 CW_NOT_READY,
2671 CW_ERROR
2672} channel_wait_result;
2673
Bram Moolenaard04a0202016-01-26 23:30:18 +01002674/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002675 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002676 * Return CW_READY when there is something to read.
2677 * Return CW_NOT_READY when there is nothing to read.
2678 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002679 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002680 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002681channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002682{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002683 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002684 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002685
Bram Moolenaard8070362016-02-15 21:56:54 +01002686# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002687 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002688 {
2689 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002690 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002691 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002692 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002693
2694 /* reading from a pipe, not a socket */
2695 while (TRUE)
2696 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002697 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2698
2699 if (r && nread > 0)
2700 return CW_READY;
2701 if (r == 0)
2702 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002703
2704 /* perhaps write some buffer lines */
2705 channel_write_any_lines();
2706
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002707 sleep_time = deadline - GetTickCount();
2708 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002709 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002710 /* Wait for a little while. Very short at first, up to 10 msec
2711 * after looping a few times. */
2712 if (sleep_time > delay)
2713 sleep_time = delay;
2714 Sleep(sleep_time);
2715 delay = delay * 2;
2716 if (delay > 10)
2717 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002718 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002719 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002720 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002721#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002722 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002723#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002724 struct timeval tval;
2725 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002726 fd_set wfds;
2727 int ret;
2728 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002729
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002730 tval.tv_sec = timeout / 1000;
2731 tval.tv_usec = (timeout % 1000) * 1000;
2732 for (;;)
2733 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002734 FD_ZERO(&rfds);
2735 FD_SET((int)fd, &rfds);
2736
2737 /* Write lines to a pipe when a pipe can be written to. Need to
2738 * set this every time, some buffers may be done. */
2739 maxfd = (int)fd + 1;
2740 FD_ZERO(&wfds);
2741 maxfd = channel_fill_wfds(maxfd, &wfds);
2742
2743 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002744# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002745 SOCK_ERRNO;
2746 if (ret == -1 && errno == EINTR)
2747 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002748# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002749 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002750 {
2751 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002752 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002753 channel_write_any_lines();
2754 continue;
2755 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002756 break;
2757 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002758#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002759 for (;;)
2760 {
2761 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2762 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002763
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002764 fds[0].fd = fd;
2765 fds[0].events = POLLIN;
2766 nfd = channel_fill_poll_write(nfd, fds);
2767 if (poll(fds, nfd, timeout) > 0)
2768 {
2769 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002770 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002771 channel_write_any_lines();
2772 continue;
2773 }
2774 break;
2775 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002776#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002777 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002778 return CW_NOT_READY;
2779}
2780
2781 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002782channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002783{
2784 /* Do not call emsg(), most likely the other end just exited. */
2785 ch_errors(channel, "%s(): Cannot read from channel", func);
2786
2787 /* Queue a "DETACH" netbeans message in the command queue in order to
2788 * terminate the netbeans session later. Do not end the session here
2789 * directly as we may be running in the context of a call to
2790 * netbeans_parse_messages():
2791 * netbeans_parse_messages
2792 * -> autocmd triggered while processing the netbeans cmd
2793 * -> ui_breakcheck
2794 * -> gui event loop or select loop
2795 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002796 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002797 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002798 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002799 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002800 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2801
2802 /* When reading from stdout is not possible, assume the other side has
2803 * died. */
2804 channel_close(channel, TRUE);
2805 if (channel->ch_nb_close_cb != NULL)
2806 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002807}
2808
2809/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002810 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002811 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002812 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002813 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002814 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002815channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002816{
2817 static char_u *buf = NULL;
2818 int len = 0;
2819 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002820 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002821 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002822
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002823 fd = channel->ch_part[part].ch_fd;
2824 if (fd == INVALID_FD)
2825 {
2826 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002827 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002828 }
2829 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002830
2831 /* Allocate a buffer to read into. */
2832 if (buf == NULL)
2833 {
2834 buf = alloc(MAXMSGSIZE);
2835 if (buf == NULL)
2836 return; /* out of memory! */
2837 }
2838
2839 /* Keep on reading for as long as there is something to read.
2840 * Use select() or poll() to avoid blocking on a message that is exactly
2841 * MAXMSGSIZE long. */
2842 for (;;)
2843 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002844 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002845 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002846 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002847 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002848 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002849 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002850 if (len <= 0)
2851 break; /* error or nothing more to read */
2852
2853 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002854 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002855 readlen += len;
2856 if (len < MAXMSGSIZE)
2857 break; /* did read everything that's available */
2858 }
2859
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002860 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002861 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002862 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002863
2864#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002865 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002866 if (CH_HAS_GUI && gtk_main_level() > 0)
2867 gtk_main_quit();
2868#endif
2869}
2870
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002871/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002872 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002873 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002874 * Returns what was read in allocated memory.
2875 * Returns NULL in case of error or timeout.
2876 */
2877 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002878channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002879{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002880 char_u *buf;
2881 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002882 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002883 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002884 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002885
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002886 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002887 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002888
2889 while (TRUE)
2890 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002891 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002892 if (buf != NULL && (mode == MODE_RAW
2893 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2894 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002895 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002896 continue;
2897
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002898 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002899 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002900 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002901 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002902 {
2903 ch_log(channel, "Timed out");
2904 return NULL;
2905 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002906 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002907 }
2908
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002909 if (mode == MODE_RAW)
2910 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002911 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002912 }
2913 else
2914 {
2915 nl = vim_strchr(buf, NL);
2916 if (nl[1] == NUL)
2917 {
2918 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002919 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002920 *nl = NUL;
2921 }
2922 else
2923 {
2924 /* Copy the message into allocated memory and remove it from the
2925 * buffer. */
2926 msg = vim_strnsave(buf, (int)(nl - buf));
2927 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2928 }
2929 }
2930 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002931 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002932 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002933}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002934
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002935/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002936 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002937 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002938 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002939 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002940 */
2941 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002942channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002943 channel_T *channel,
2944 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002945 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002946 int id,
2947 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002948{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002949 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002950 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002951 int timeout;
2952 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002953
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002954 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002955 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002956 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002957 for (;;)
2958 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002959 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002960
2961 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002962 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002963 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002964 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002965 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002966 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002967
Bram Moolenaard7ece102016-02-02 23:23:02 +01002968 if (!more)
2969 {
2970 /* Handle any other messages in the queue. If done some more
2971 * messages may have arrived. */
2972 if (channel_parse_messages())
2973 continue;
2974
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002975 /* Wait for up to the timeout. If there was an incomplete message
2976 * use the deadline for that. */
2977 timeout = timeout_arg;
2978 if (chanpart->ch_waiting)
2979 {
2980#ifdef WIN32
2981 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2982#else
2983 {
2984 struct timeval now_tv;
2985
2986 gettimeofday(&now_tv, NULL);
2987 timeout = (chanpart->ch_deadline.tv_sec
2988 - now_tv.tv_sec) * 1000
2989 + (chanpart->ch_deadline.tv_usec
2990 - now_tv.tv_usec) / 1000
2991 + 1;
2992 }
2993#endif
2994 if (timeout < 0)
2995 {
2996 /* Something went wrong, channel_parse_json() didn't
2997 * discard message. Cancel waiting. */
2998 chanpart->ch_waiting = FALSE;
2999 timeout = timeout_arg;
3000 }
3001 else if (timeout > timeout_arg)
3002 timeout = timeout_arg;
3003 }
3004 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003005 if (fd == INVALID_FD
3006 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003007 {
3008 if (timeout == timeout_arg)
3009 {
3010 if (fd != INVALID_FD)
3011 ch_log(channel, "Timed out");
3012 break;
3013 }
3014 }
3015 else
3016 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003017 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003018 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003019 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003020 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003021}
3022
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003023/*
3024 * Common for ch_read() and ch_readraw().
3025 */
3026 void
3027common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3028{
3029 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003030 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003031 jobopt_T opt;
3032 int mode;
3033 int timeout;
3034 int id = -1;
3035 typval_T *listtv = NULL;
3036
3037 /* return an empty string by default */
3038 rettv->v_type = VAR_STRING;
3039 rettv->vval.v_string = NULL;
3040
3041 clear_job_options(&opt);
3042 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3043 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003044 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003045
Bram Moolenaar437905c2016-04-26 19:01:05 +02003046 if (opt.jo_set & JO_PART)
3047 part = opt.jo_part;
3048 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003049 if (channel != NULL)
3050 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003051 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003052 part = channel_part_read(channel);
3053 mode = channel_get_mode(channel, part);
3054 timeout = channel_get_timeout(channel, part);
3055 if (opt.jo_set & JO_TIMEOUT)
3056 timeout = opt.jo_timeout;
3057
3058 if (raw || mode == MODE_RAW || mode == MODE_NL)
3059 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3060 else
3061 {
3062 if (opt.jo_set & JO_ID)
3063 id = opt.jo_id;
3064 channel_read_json_block(channel, part, timeout, id, &listtv);
3065 if (listtv != NULL)
3066 {
3067 *rettv = *listtv;
3068 vim_free(listtv);
3069 }
3070 else
3071 {
3072 rettv->v_type = VAR_SPECIAL;
3073 rettv->vval.v_number = VVAL_NONE;
3074 }
3075 }
3076 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003077
3078theend:
3079 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003080}
3081
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003082# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3083 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003084/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003085 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003086 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003087 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003088 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003089channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003090{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003091 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003092 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003093
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003094 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003095 for (channel = first_channel; channel != NULL;
3096 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003097 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003098 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003099 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003100 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003101 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003102 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003103 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003104 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003105 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003106}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003107# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003108
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003109# if defined(WIN32) || defined(PROTO)
3110/*
3111 * Check the channels for anything that is ready to be read.
3112 * The data is put in the read queue.
3113 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003114 void
3115channel_handle_events(void)
3116{
3117 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003118 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003119 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003120
3121 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3122 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003123 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003124 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003125 {
3126 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003127 if (fd != INVALID_FD)
3128 {
3129 int r = channel_wait(channel, fd, 0);
3130
3131 if (r == CW_READY)
3132 channel_read(channel, part, "channel_handle_events");
3133 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003134 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003135 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003136 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003137 }
3138}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003139# endif
3140
Bram Moolenaard04a0202016-01-26 23:30:18 +01003141/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003142 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003143 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003144 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003145 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003146 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003147channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003148{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003149 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003150 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003151 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003152
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003153 fd = channel->ch_part[part].ch_fd;
3154 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003155 {
3156 if (!channel->ch_error && fun != NULL)
3157 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003158 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003159 EMSG2("E630: %s(): write while not connected", fun);
3160 }
3161 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003162 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003163 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003164
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003165 if (log_fd != NULL)
3166 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003167 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003168 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003169 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003170 fprintf(log_fd, "'\n");
3171 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003172 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003173 }
3174
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003175 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003176 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003177 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003178 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003179 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003180 {
3181 if (!channel->ch_error && fun != NULL)
3182 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003183 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003184 EMSG2("E631: %s(): write failed", fun);
3185 }
3186 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003187 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003188 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003189
3190 channel->ch_error = FALSE;
3191 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003192}
3193
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003194/*
3195 * Common for "ch_sendexpr()" and "ch_sendraw()".
3196 * Returns the channel if the caller should read the response.
3197 * Sets "part_read" to the the read fd.
3198 * Otherwise returns NULL.
3199 */
3200 channel_T *
3201send_common(
3202 typval_T *argvars,
3203 char_u *text,
3204 int id,
3205 int eval,
3206 jobopt_T *opt,
3207 char *fun,
3208 int *part_read)
3209{
3210 channel_T *channel;
3211 int part_send;
3212
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003213 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003214 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003215 if (channel == NULL)
3216 return NULL;
3217 part_send = channel_part_send(channel);
3218 *part_read = channel_part_read(channel);
3219
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003220 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3221 return NULL;
3222
3223 /* Set the callback. An empty callback means no callback and not reading
3224 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3225 * allowed. */
3226 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3227 {
3228 if (eval)
3229 {
3230 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3231 return NULL;
3232 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003233 channel_set_req_callback(channel, part_send,
3234 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003235 }
3236
3237 if (channel_send(channel, part_send, text, fun) == OK
3238 && opt->jo_callback == NULL)
3239 return channel;
3240 return NULL;
3241}
3242
3243/*
3244 * common for "ch_evalexpr()" and "ch_sendexpr()"
3245 */
3246 void
3247ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3248{
3249 char_u *text;
3250 typval_T *listtv;
3251 channel_T *channel;
3252 int id;
3253 ch_mode_T ch_mode;
3254 int part_send;
3255 int part_read;
3256 jobopt_T opt;
3257 int timeout;
3258
3259 /* return an empty string by default */
3260 rettv->v_type = VAR_STRING;
3261 rettv->vval.v_string = NULL;
3262
Bram Moolenaar437905c2016-04-26 19:01:05 +02003263 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003264 if (channel == NULL)
3265 return;
3266 part_send = channel_part_send(channel);
3267
3268 ch_mode = channel_get_mode(channel, part_send);
3269 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3270 {
3271 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3272 return;
3273 }
3274
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003275 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003276 text = json_encode_nr_expr(id, &argvars[1],
3277 ch_mode == MODE_JS ? JSON_JS : 0);
3278 if (text == NULL)
3279 return;
3280
3281 channel = send_common(argvars, text, id, eval, &opt,
3282 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3283 vim_free(text);
3284 if (channel != NULL && eval)
3285 {
3286 if (opt.jo_set & JO_TIMEOUT)
3287 timeout = opt.jo_timeout;
3288 else
3289 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003290 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3291 == OK)
3292 {
3293 list_T *list = listtv->vval.v_list;
3294
3295 /* Move the item from the list and then change the type to
3296 * avoid the value being freed. */
3297 *rettv = list->lv_last->li_tv;
3298 list->lv_last->li_tv.v_type = VAR_NUMBER;
3299 free_tv(listtv);
3300 }
3301 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003302 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003303}
3304
3305/*
3306 * common for "ch_evalraw()" and "ch_sendraw()"
3307 */
3308 void
3309ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3310{
3311 char_u buf[NUMBUFLEN];
3312 char_u *text;
3313 channel_T *channel;
3314 int part_read;
3315 jobopt_T opt;
3316 int timeout;
3317
3318 /* return an empty string by default */
3319 rettv->v_type = VAR_STRING;
3320 rettv->vval.v_string = NULL;
3321
3322 text = get_tv_string_buf(&argvars[1], buf);
3323 channel = send_common(argvars, text, 0, eval, &opt,
3324 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3325 if (channel != NULL && eval)
3326 {
3327 if (opt.jo_set & JO_TIMEOUT)
3328 timeout = opt.jo_timeout;
3329 else
3330 timeout = channel_get_timeout(channel, part_read);
3331 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3332 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003333 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003334}
3335
Bram Moolenaard04a0202016-01-26 23:30:18 +01003336# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003337/*
3338 * Add open channels to the poll struct.
3339 * Return the adjusted struct index.
3340 * The type of "fds" is hidden to avoid problems with the function proto.
3341 */
3342 int
3343channel_poll_setup(int nfd_in, void *fds_in)
3344{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003345 int nfd = nfd_in;
3346 channel_T *channel;
3347 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003348 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003349
Bram Moolenaar77073442016-02-13 23:23:53 +01003350 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003351 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003352 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003353 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003354 chanpart_T *ch_part = &channel->ch_part[part];
3355
3356 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003357 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003358 ch_part->ch_poll_idx = nfd;
3359 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003360 fds[nfd].events = POLLIN;
3361 nfd++;
3362 }
3363 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003364 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003365 }
3366 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003367
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003368 nfd = channel_fill_poll_write(nfd, fds);
3369
Bram Moolenaare0874f82016-01-24 20:36:41 +01003370 return nfd;
3371}
3372
3373/*
3374 * The type of "fds" is hidden to avoid problems with the function proto.
3375 */
3376 int
3377channel_poll_check(int ret_in, void *fds_in)
3378{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003379 int ret = ret_in;
3380 channel_T *channel;
3381 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003382 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003383 int idx;
3384 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003385
Bram Moolenaar77073442016-02-13 23:23:53 +01003386 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003387 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003388 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003389 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003390 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003391
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003392 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003393 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003394 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003395 --ret;
3396 }
3397 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003398
3399 in_part = &channel->ch_part[PART_IN];
3400 idx = in_part->ch_poll_idx;
3401 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3402 {
3403 if (in_part->ch_buf_append)
3404 {
3405 if (in_part->ch_buffer != NULL)
3406 channel_write_new_lines(in_part->ch_buffer);
3407 }
3408 else
3409 channel_write_in(channel);
3410 --ret;
3411 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003412 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003413
3414 return ret;
3415}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003416# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003417
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003418# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003419/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003420 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003421 */
3422 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003423channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003424{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003425 int maxfd = maxfd_in;
3426 channel_T *channel;
3427 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003428 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003429 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003430
Bram Moolenaar77073442016-02-13 23:23:53 +01003431 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003432 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003433 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003434 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003435 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003436
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003437 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003438 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003439 FD_SET((int)fd, rfds);
3440 if (maxfd < (int)fd)
3441 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003442 }
3443 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003444 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003445
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003446 maxfd = channel_fill_wfds(maxfd, wfds);
3447
Bram Moolenaare0874f82016-01-24 20:36:41 +01003448 return maxfd;
3449}
3450
3451/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003452 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003453 */
3454 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003455channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003456{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003457 int ret = ret_in;
3458 channel_T *channel;
3459 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003460 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003461 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003462 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003463
Bram Moolenaar77073442016-02-13 23:23:53 +01003464 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003465 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003466 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003467 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003468 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003469
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003470 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003471 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003472 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003473 --ret;
3474 }
3475 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003476
3477 in_part = &channel->ch_part[PART_IN];
3478 if (ret > 0 && in_part->ch_fd != INVALID_FD
3479 && FD_ISSET(in_part->ch_fd, wfds))
3480 {
3481 if (in_part->ch_buf_append)
3482 {
3483 if (in_part->ch_buffer != NULL)
3484 channel_write_new_lines(in_part->ch_buffer);
3485 }
3486 else
3487 channel_write_in(channel);
3488 --ret;
3489 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003490 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003491
3492 return ret;
3493}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003494# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003495
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003496/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003497 * Execute queued up commands.
3498 * Invoked from the main loop when it's safe to execute received commands.
3499 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003500 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003501 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003502channel_parse_messages(void)
3503{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003504 channel_T *channel = first_channel;
3505 int ret = FALSE;
3506 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003507 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003508
Bram Moolenaard0b65022016-03-06 21:50:33 +01003509 /* Only do this message when another message was given, otherwise we get
3510 * lots of them. */
3511 if (did_log_msg)
3512 {
3513 ch_log(NULL, "looking for messages on channels");
3514 did_log_msg = FALSE;
3515 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003516 while (channel != NULL)
3517 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003518 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003519 {
3520 /* channel is no longer useful, free it */
3521 channel_free(channel);
3522 channel = first_channel;
3523 part = PART_SOCK;
3524 continue;
3525 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003526 if (channel->ch_part[part].ch_fd != INVALID_FD
3527 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003528 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003529 /* Increase the refcount, in case the handler causes the channel
3530 * to be unreferenced or closed. */
3531 ++channel->ch_refcount;
3532 r = may_invoke_callback(channel, part);
3533 if (r == OK)
3534 ret = TRUE;
3535 if (channel_unref(channel) || r == OK)
3536 {
3537 /* channel was freed or something was done, start over */
3538 channel = first_channel;
3539 part = PART_SOCK;
3540 continue;
3541 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003542 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003543 if (part < PART_ERR)
3544 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003545 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003546 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003547 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003548 part = PART_SOCK;
3549 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003550 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003551
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003552 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003553 {
3554 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003555 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003556 }
3557
Bram Moolenaard7ece102016-02-02 23:23:02 +01003558 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003559}
3560
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003561/*
3562 * Mark references to lists used in channels.
3563 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003564 int
3565set_ref_in_channel(int copyID)
3566{
Bram Moolenaar77073442016-02-13 23:23:53 +01003567 int abort = FALSE;
3568 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003569 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003570
Bram Moolenaar77073442016-02-13 23:23:53 +01003571 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003572 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003573 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003574 tv.v_type = VAR_CHANNEL;
3575 tv.vval.v_channel = channel;
3576 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003577 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003578 return abort;
3579}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003580
3581/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003582 * Return the "part" to write to for "channel".
3583 */
3584 int
3585channel_part_send(channel_T *channel)
3586{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003587 if (channel->CH_SOCK_FD == INVALID_FD)
3588 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003589 return PART_SOCK;
3590}
3591
3592/*
3593 * Return the default "part" to read from for "channel".
3594 */
3595 int
3596channel_part_read(channel_T *channel)
3597{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 if (channel->CH_SOCK_FD == INVALID_FD)
3599 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003600 return PART_SOCK;
3601}
3602
3603/*
3604 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003605 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003606 */
3607 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003608channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003609{
Bram Moolenaar77073442016-02-13 23:23:53 +01003610 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003611 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003612 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003613}
3614
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003615/*
3616 * Return the timeout of "channel"/"part"
3617 */
3618 int
3619channel_get_timeout(channel_T *channel, int part)
3620{
3621 return channel->ch_part[part].ch_timeout;
3622}
3623
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003624 static int
3625handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3626{
3627 char_u *val = get_tv_string(item);
3628
3629 opt->jo_set |= jo;
3630 if (STRCMP(val, "nl") == 0)
3631 *modep = MODE_NL;
3632 else if (STRCMP(val, "raw") == 0)
3633 *modep = MODE_RAW;
3634 else if (STRCMP(val, "js") == 0)
3635 *modep = MODE_JS;
3636 else if (STRCMP(val, "json") == 0)
3637 *modep = MODE_JSON;
3638 else
3639 {
3640 EMSG2(_(e_invarg2), val);
3641 return FAIL;
3642 }
3643 return OK;
3644}
3645
3646 static int
3647handle_io(typval_T *item, int part, jobopt_T *opt)
3648{
3649 char_u *val = get_tv_string(item);
3650
3651 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3652 if (STRCMP(val, "null") == 0)
3653 opt->jo_io[part] = JIO_NULL;
3654 else if (STRCMP(val, "pipe") == 0)
3655 opt->jo_io[part] = JIO_PIPE;
3656 else if (STRCMP(val, "file") == 0)
3657 opt->jo_io[part] = JIO_FILE;
3658 else if (STRCMP(val, "buffer") == 0)
3659 opt->jo_io[part] = JIO_BUFFER;
3660 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3661 opt->jo_io[part] = JIO_OUT;
3662 else
3663 {
3664 EMSG2(_(e_invarg2), val);
3665 return FAIL;
3666 }
3667 return OK;
3668}
3669
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003670/*
3671 * Clear a jobopt_T before using it.
3672 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003673 void
3674clear_job_options(jobopt_T *opt)
3675{
3676 vim_memset(opt, 0, sizeof(jobopt_T));
3677}
3678
3679/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003680 * Free any members of a jobopt_T.
3681 */
3682 void
3683free_job_options(jobopt_T *opt)
3684{
3685 if (opt->jo_partial != NULL)
3686 partial_unref(opt->jo_partial);
3687 if (opt->jo_out_partial != NULL)
3688 partial_unref(opt->jo_out_partial);
3689 if (opt->jo_err_partial != NULL)
3690 partial_unref(opt->jo_err_partial);
3691 if (opt->jo_close_partial != NULL)
3692 partial_unref(opt->jo_close_partial);
3693}
3694
3695/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003696 * Get the PART_ number from the first character of an option name.
3697 */
3698 static int
3699part_from_char(int c)
3700{
3701 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3702}
3703
3704/*
3705 * Get the option entries from the dict in "tv", parse them and put the result
3706 * in "opt".
3707 * Only accept options in "supported".
3708 * If an option value is invalid return FAIL.
3709 */
3710 int
3711get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3712{
3713 typval_T *item;
3714 char_u *val;
3715 dict_T *dict;
3716 int todo;
3717 hashitem_T *hi;
3718 int part;
3719
3720 opt->jo_set = 0;
3721 if (tv->v_type == VAR_UNKNOWN)
3722 return OK;
3723 if (tv->v_type != VAR_DICT)
3724 {
3725 EMSG(_(e_invarg));
3726 return FAIL;
3727 }
3728 dict = tv->vval.v_dict;
3729 if (dict == NULL)
3730 return OK;
3731
3732 todo = (int)dict->dv_hashtab.ht_used;
3733 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3734 if (!HASHITEM_EMPTY(hi))
3735 {
3736 item = &dict_lookup(hi)->di_tv;
3737
3738 if (STRCMP(hi->hi_key, "mode") == 0)
3739 {
3740 if (!(supported & JO_MODE))
3741 break;
3742 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3743 return FAIL;
3744 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003745 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003746 {
3747 if (!(supported & JO_IN_MODE))
3748 break;
3749 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3750 == FAIL)
3751 return FAIL;
3752 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003753 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003754 {
3755 if (!(supported & JO_OUT_MODE))
3756 break;
3757 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3758 == FAIL)
3759 return FAIL;
3760 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003761 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003762 {
3763 if (!(supported & JO_ERR_MODE))
3764 break;
3765 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3766 == FAIL)
3767 return FAIL;
3768 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003769 else if (STRCMP(hi->hi_key, "in_io") == 0
3770 || STRCMP(hi->hi_key, "out_io") == 0
3771 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003772 {
3773 if (!(supported & JO_OUT_IO))
3774 break;
3775 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3776 return FAIL;
3777 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003778 else if (STRCMP(hi->hi_key, "in_name") == 0
3779 || STRCMP(hi->hi_key, "out_name") == 0
3780 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003781 {
3782 part = part_from_char(*hi->hi_key);
3783
3784 if (!(supported & JO_OUT_IO))
3785 break;
3786 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3787 opt->jo_io_name[part] =
3788 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3789 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003790 else if (STRCMP(hi->hi_key, "in_buf") == 0
3791 || STRCMP(hi->hi_key, "out_buf") == 0
3792 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003793 {
3794 part = part_from_char(*hi->hi_key);
3795
3796 if (!(supported & JO_OUT_IO))
3797 break;
3798 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3799 opt->jo_io_buf[part] = get_tv_number(item);
3800 if (opt->jo_io_buf[part] <= 0)
3801 {
3802 EMSG2(_(e_invarg2), get_tv_string(item));
3803 return FAIL;
3804 }
3805 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3806 {
3807 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3808 return FAIL;
3809 }
3810 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003811 else if (STRCMP(hi->hi_key, "in_top") == 0
3812 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003813 {
3814 linenr_T *lp;
3815
3816 if (!(supported & JO_OUT_IO))
3817 break;
3818 if (hi->hi_key[3] == 't')
3819 {
3820 lp = &opt->jo_in_top;
3821 opt->jo_set |= JO_IN_TOP;
3822 }
3823 else
3824 {
3825 lp = &opt->jo_in_bot;
3826 opt->jo_set |= JO_IN_BOT;
3827 }
3828 *lp = get_tv_number(item);
3829 if (*lp < 0)
3830 {
3831 EMSG2(_(e_invarg2), get_tv_string(item));
3832 return FAIL;
3833 }
3834 }
3835 else if (STRCMP(hi->hi_key, "channel") == 0)
3836 {
3837 if (!(supported & JO_OUT_IO))
3838 break;
3839 opt->jo_set |= JO_CHANNEL;
3840 if (item->v_type != VAR_CHANNEL)
3841 {
3842 EMSG2(_(e_invarg2), "channel");
3843 return FAIL;
3844 }
3845 opt->jo_channel = item->vval.v_channel;
3846 }
3847 else if (STRCMP(hi->hi_key, "callback") == 0)
3848 {
3849 if (!(supported & JO_CALLBACK))
3850 break;
3851 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003852 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003853 if (opt->jo_callback == NULL)
3854 {
3855 EMSG2(_(e_invarg2), "callback");
3856 return FAIL;
3857 }
3858 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003859 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003860 {
3861 if (!(supported & JO_OUT_CALLBACK))
3862 break;
3863 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003864 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003865 if (opt->jo_out_cb == NULL)
3866 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003867 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003868 return FAIL;
3869 }
3870 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003871 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003872 {
3873 if (!(supported & JO_ERR_CALLBACK))
3874 break;
3875 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003876 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003877 if (opt->jo_err_cb == NULL)
3878 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003879 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003880 return FAIL;
3881 }
3882 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003883 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003884 {
3885 if (!(supported & JO_CLOSE_CALLBACK))
3886 break;
3887 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003888 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003889 if (opt->jo_close_cb == NULL)
3890 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003891 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003892 return FAIL;
3893 }
3894 }
3895 else if (STRCMP(hi->hi_key, "waittime") == 0)
3896 {
3897 if (!(supported & JO_WAITTIME))
3898 break;
3899 opt->jo_set |= JO_WAITTIME;
3900 opt->jo_waittime = get_tv_number(item);
3901 }
3902 else if (STRCMP(hi->hi_key, "timeout") == 0)
3903 {
3904 if (!(supported & JO_TIMEOUT))
3905 break;
3906 opt->jo_set |= JO_TIMEOUT;
3907 opt->jo_timeout = get_tv_number(item);
3908 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003909 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003910 {
3911 if (!(supported & JO_OUT_TIMEOUT))
3912 break;
3913 opt->jo_set |= JO_OUT_TIMEOUT;
3914 opt->jo_out_timeout = get_tv_number(item);
3915 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003916 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003917 {
3918 if (!(supported & JO_ERR_TIMEOUT))
3919 break;
3920 opt->jo_set |= JO_ERR_TIMEOUT;
3921 opt->jo_err_timeout = get_tv_number(item);
3922 }
3923 else if (STRCMP(hi->hi_key, "part") == 0)
3924 {
3925 if (!(supported & JO_PART))
3926 break;
3927 opt->jo_set |= JO_PART;
3928 val = get_tv_string(item);
3929 if (STRCMP(val, "err") == 0)
3930 opt->jo_part = PART_ERR;
3931 else
3932 {
3933 EMSG2(_(e_invarg2), val);
3934 return FAIL;
3935 }
3936 }
3937 else if (STRCMP(hi->hi_key, "id") == 0)
3938 {
3939 if (!(supported & JO_ID))
3940 break;
3941 opt->jo_set |= JO_ID;
3942 opt->jo_id = get_tv_number(item);
3943 }
3944 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3945 {
3946 if (!(supported & JO_STOPONEXIT))
3947 break;
3948 opt->jo_set |= JO_STOPONEXIT;
3949 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3950 opt->jo_soe_buf);
3951 if (opt->jo_stoponexit == NULL)
3952 {
3953 EMSG2(_(e_invarg2), "stoponexit");
3954 return FAIL;
3955 }
3956 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003957 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003958 {
3959 if (!(supported & JO_EXIT_CB))
3960 break;
3961 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003962 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3963 {
3964 opt->jo_exit_partial = item->vval.v_partial;
3965 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3966 }
3967 else
3968 opt->jo_exit_cb = get_tv_string_buf_chk(
3969 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003970 if (opt->jo_exit_cb == NULL)
3971 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003972 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003973 return FAIL;
3974 }
3975 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003976 else if (STRCMP(hi->hi_key, "block_write") == 0)
3977 {
3978 if (!(supported & JO_BLOCK_WRITE))
3979 break;
3980 opt->jo_set |= JO_BLOCK_WRITE;
3981 opt->jo_block_write = get_tv_number(item);
3982 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003983 else
3984 break;
3985 --todo;
3986 }
3987 if (todo > 0)
3988 {
3989 EMSG2(_(e_invarg2), hi->hi_key);
3990 return FAIL;
3991 }
3992
3993 return OK;
3994}
3995
3996/*
3997 * Get the channel from the argument.
3998 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02003999 * When "check_open" is TRUE check that the channel can be used.
4000 * When "reading" is TRUE "check_open" considers typeahead useful.
4001 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004002 */
4003 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004004get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004005{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004006 channel_T *channel = NULL;
4007 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004008
4009 if (tv->v_type == VAR_JOB)
4010 {
4011 if (tv->vval.v_job != NULL)
4012 channel = tv->vval.v_job->jv_channel;
4013 }
4014 else if (tv->v_type == VAR_CHANNEL)
4015 {
4016 channel = tv->vval.v_channel;
4017 }
4018 else
4019 {
4020 EMSG2(_(e_invarg2), get_tv_string(tv));
4021 return NULL;
4022 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004023 if (channel != NULL && reading)
4024 has_readahead = channel_has_readahead(channel,
4025 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004026
Bram Moolenaar437905c2016-04-26 19:01:05 +02004027 if (check_open && (channel == NULL || (!channel_is_open(channel)
4028 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004029 {
4030 EMSG(_("E906: not an open channel"));
4031 return NULL;
4032 }
4033 return channel;
4034}
4035
4036static job_T *first_job = NULL;
4037
4038 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004039job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004040{
4041 ch_log(job->jv_channel, "Freeing job");
4042 if (job->jv_channel != NULL)
4043 {
4044 /* The link from the channel to the job doesn't count as a reference,
4045 * thus don't decrement the refcount of the job. The reference from
4046 * the job to the channel does count the refrence, decrement it and
4047 * NULL the reference. We don't set ch_job_killed, unreferencing the
4048 * job doesn't mean it stops running. */
4049 job->jv_channel->ch_job = NULL;
4050 channel_unref(job->jv_channel);
4051 }
4052 mch_clear_job(job);
4053
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004054 vim_free(job->jv_stoponexit);
4055 vim_free(job->jv_exit_cb);
4056 partial_unref(job->jv_exit_partial);
4057}
4058
4059 static void
4060job_free_job(job_T *job)
4061{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062 if (job->jv_next != NULL)
4063 job->jv_next->jv_prev = job->jv_prev;
4064 if (job->jv_prev == NULL)
4065 first_job = job->jv_next;
4066 else
4067 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004068 vim_free(job);
4069}
4070
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004071 static void
4072job_free(job_T *job)
4073{
4074 if (!in_free_unref_items)
4075 {
4076 job_free_contents(job);
4077 job_free_job(job);
4078 }
4079}
4080
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004081/*
4082 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004083 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4084 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004085 */
4086 static int
4087job_still_useful(job_T *job)
4088{
4089 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004090 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4091 || (job->jv_channel != NULL
4092 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004093}
4094
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004095/*
4096 * Mark references in jobs that are still useful.
4097 */
4098 int
4099set_ref_in_job(int copyID)
4100{
4101 int abort = FALSE;
4102 job_T *job;
4103 typval_T tv;
4104
4105 for (job = first_job; job != NULL; job = job->jv_next)
4106 if (job_still_useful(job))
4107 {
4108 tv.v_type = VAR_JOB;
4109 tv.vval.v_job = job;
4110 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4111 }
4112 return abort;
4113}
4114
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004115 void
4116job_unref(job_T *job)
4117{
4118 if (job != NULL && --job->jv_refcount <= 0)
4119 {
4120 /* Do not free the job when it has not ended yet and there is a
4121 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004122 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004123 {
4124 job_free(job);
4125 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004126 else if (job->jv_channel != NULL
4127 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 {
4129 /* Do remove the link to the channel, otherwise it hangs
4130 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004131 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132 job->jv_channel->ch_job = NULL;
4133 channel_unref(job->jv_channel);
4134 job->jv_channel = NULL;
4135 }
4136 }
4137}
4138
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139 int
4140free_unused_jobs_contents(int copyID, int mask)
4141{
4142 int did_free = FALSE;
4143 job_T *job;
4144
4145 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004146 if ((job->jv_copyID & mask) != (copyID & mask)
4147 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004148 {
4149 /* Free the channel and ordinary items it contains, but don't
4150 * recurse into Lists, Dictionaries etc. */
4151 job_free_contents(job);
4152 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004153 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004154 return did_free;
4155}
4156
4157 void
4158free_unused_jobs(int copyID, int mask)
4159{
4160 job_T *job;
4161 job_T *job_next;
4162
4163 for (job = first_job; job != NULL; job = job_next)
4164 {
4165 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004166 if ((job->jv_copyID & mask) != (copyID & mask)
4167 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004168 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004169 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004170 job_free_job(job);
4171 }
4172 }
4173}
4174
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004175/*
4176 * Allocate a job. Sets the refcount to one and sets options default.
4177 */
4178 static job_T *
4179job_alloc(void)
4180{
4181 job_T *job;
4182
4183 job = (job_T *)alloc_clear(sizeof(job_T));
4184 if (job != NULL)
4185 {
4186 job->jv_refcount = 1;
4187 job->jv_stoponexit = vim_strsave((char_u *)"term");
4188
4189 if (first_job != NULL)
4190 {
4191 first_job->jv_prev = job;
4192 job->jv_next = first_job;
4193 }
4194 first_job = job;
4195 }
4196 return job;
4197}
4198
4199 void
4200job_set_options(job_T *job, jobopt_T *opt)
4201{
4202 if (opt->jo_set & JO_STOPONEXIT)
4203 {
4204 vim_free(job->jv_stoponexit);
4205 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4206 job->jv_stoponexit = NULL;
4207 else
4208 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4209 }
4210 if (opt->jo_set & JO_EXIT_CB)
4211 {
4212 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004213 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004214 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004215 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004216 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004217 job->jv_exit_partial = NULL;
4218 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004219 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004220 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004221 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004222 job->jv_exit_partial = opt->jo_exit_partial;
4223 if (job->jv_exit_partial != NULL)
4224 ++job->jv_exit_partial->pt_refcount;
4225 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004226 }
4227}
4228
4229/*
4230 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4231 */
4232 void
4233job_stop_on_exit()
4234{
4235 job_T *job;
4236
4237 for (job = first_job; job != NULL; job = job->jv_next)
4238 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4239 mch_stop_job(job, job->jv_stoponexit);
4240}
4241
4242/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004243 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004244 */
4245 void
4246job_check_ended(void)
4247{
4248 static time_t last_check = 0;
4249 time_t now;
4250 job_T *job;
4251 job_T *next;
4252
4253 /* Only do this once in 10 seconds. */
4254 now = time(NULL);
4255 if (last_check + 10 < now)
4256 {
4257 last_check = now;
4258 for (job = first_job; job != NULL; job = next)
4259 {
4260 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004261 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004262 job_status(job); /* may free "job" */
4263 }
4264 }
4265}
4266
4267/*
4268 * "job_start()" function
4269 */
4270 job_T *
4271job_start(typval_T *argvars)
4272{
4273 job_T *job;
4274 char_u *cmd = NULL;
4275#if defined(UNIX)
4276# define USE_ARGV
4277 char **argv = NULL;
4278 int argc = 0;
4279#else
4280 garray_T ga;
4281#endif
4282 jobopt_T opt;
4283 int part;
4284
4285 job = job_alloc();
4286 if (job == NULL)
4287 return NULL;
4288
4289 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004290#ifndef USE_ARGV
4291 ga_init2(&ga, (int)sizeof(char*), 20);
4292#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004293
4294 /* Default mode is NL. */
4295 clear_job_options(&opt);
4296 opt.jo_mode = MODE_NL;
4297 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004298 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4299 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004300 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004301
4302 /* Check that when io is "file" that there is a file name. */
4303 for (part = PART_OUT; part <= PART_IN; ++part)
4304 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4305 && opt.jo_io[part] == JIO_FILE
4306 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4307 || *opt.jo_io_name[part] == NUL))
4308 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004309 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004310 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004311 }
4312
4313 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4314 {
4315 buf_T *buf = NULL;
4316
4317 /* check that we can find the buffer before starting the job */
4318 if (opt.jo_set & JO_IN_BUF)
4319 {
4320 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4321 if (buf == NULL)
4322 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4323 }
4324 else if (!(opt.jo_set & JO_IN_NAME))
4325 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004326 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004327 }
4328 else
4329 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4330 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004331 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004332 if (buf->b_ml.ml_mfp == NULL)
4333 {
4334 char_u numbuf[NUMBUFLEN];
4335 char_u *s;
4336
4337 if (opt.jo_set & JO_IN_BUF)
4338 {
4339 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4340 s = numbuf;
4341 }
4342 else
4343 s = opt.jo_io_name[PART_IN];
4344 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004345 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004346 }
4347 job->jv_in_buf = buf;
4348 }
4349
4350 job_set_options(job, &opt);
4351
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004352 if (argvars[0].v_type == VAR_STRING)
4353 {
4354 /* Command is a string. */
4355 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004356 if (cmd == NULL || *cmd == NUL)
4357 {
4358 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004359 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004360 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004361#ifdef USE_ARGV
4362 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004363 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004364 argv[argc] = NULL;
4365#endif
4366 }
4367 else if (argvars[0].v_type != VAR_LIST
4368 || argvars[0].vval.v_list == NULL
4369 || argvars[0].vval.v_list->lv_len < 1)
4370 {
4371 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004372 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004373 }
4374 else
4375 {
4376 list_T *l = argvars[0].vval.v_list;
4377 listitem_T *li;
4378 char_u *s;
4379
4380#ifdef USE_ARGV
4381 /* Pass argv[] to mch_call_shell(). */
4382 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4383 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004384 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004385#endif
4386 for (li = l->lv_first; li != NULL; li = li->li_next)
4387 {
4388 s = get_tv_string_chk(&li->li_tv);
4389 if (s == NULL)
4390 goto theend;
4391#ifdef USE_ARGV
4392 argv[argc++] = (char *)s;
4393#else
4394 /* Only escape when needed, double quotes are not always allowed. */
4395 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4396 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004397# ifdef WIN32
4398 int old_ssl = p_ssl;
4399
4400 /* This is using CreateProcess, not cmd.exe. Always use
4401 * double quote and backslashes. */
4402 p_ssl = 0;
4403# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004404 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004405# ifdef WIN32
4406 p_ssl = old_ssl;
4407# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004408 if (s == NULL)
4409 goto theend;
4410 ga_concat(&ga, s);
4411 vim_free(s);
4412 }
4413 else
4414 ga_concat(&ga, s);
4415 if (li->li_next != NULL)
4416 ga_append(&ga, ' ');
4417#endif
4418 }
4419#ifdef USE_ARGV
4420 argv[argc] = NULL;
4421#else
4422 cmd = ga.ga_data;
4423#endif
4424 }
4425
4426#ifdef USE_ARGV
4427 if (ch_log_active())
4428 {
4429 garray_T ga;
4430 int i;
4431
4432 ga_init2(&ga, (int)sizeof(char), 200);
4433 for (i = 0; i < argc; ++i)
4434 {
4435 if (i > 0)
4436 ga_concat(&ga, (char_u *)" ");
4437 ga_concat(&ga, (char_u *)argv[i]);
4438 }
4439 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4440 ga_clear(&ga);
4441 }
4442 mch_start_job(argv, job, &opt);
4443#else
4444 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4445 mch_start_job((char *)cmd, job, &opt);
4446#endif
4447
4448 /* If the channel is reading from a buffer, write lines now. */
4449 if (job->jv_channel != NULL)
4450 channel_write_in(job->jv_channel);
4451
4452theend:
4453#ifdef USE_ARGV
4454 vim_free(argv);
4455#else
4456 vim_free(ga.ga_data);
4457#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004458 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004459 return job;
4460}
4461
4462/*
4463 * Get the status of "job" and invoke the exit callback when needed.
4464 * The returned string is not allocated.
4465 */
4466 char *
4467job_status(job_T *job)
4468{
4469 char *result;
4470
4471 if (job->jv_status == JOB_ENDED)
4472 /* No need to check, dead is dead. */
4473 result = "dead";
4474 else if (job->jv_status == JOB_FAILED)
4475 result = "fail";
4476 else
4477 {
4478 result = mch_job_status(job);
4479 if (job->jv_status == JOB_ENDED)
4480 ch_log(job->jv_channel, "Job ended");
4481 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4482 {
4483 typval_T argv[3];
4484 typval_T rettv;
4485 int dummy;
4486
4487 /* invoke the exit callback; make sure the refcount is > 0 */
4488 ++job->jv_refcount;
4489 argv[0].v_type = VAR_JOB;
4490 argv[0].vval.v_job = job;
4491 argv[1].v_type = VAR_NUMBER;
4492 argv[1].vval.v_number = job->jv_exitval;
4493 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004494 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4495 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004496 clear_tv(&rettv);
4497 --job->jv_refcount;
4498 }
4499 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4500 {
4501 /* The job was already unreferenced, now that it ended it can be
4502 * freed. Careful: caller must not use "job" after this! */
4503 job_free(job);
4504 }
4505 }
4506 return result;
4507}
4508
Bram Moolenaar8950a562016-03-12 15:22:55 +01004509/*
4510 * Implementation of job_info().
4511 */
4512 void
4513job_info(job_T *job, dict_T *dict)
4514{
4515 dictitem_T *item;
4516 varnumber_T nr;
4517
4518 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4519
4520 item = dictitem_alloc((char_u *)"channel");
4521 if (item == NULL)
4522 return;
4523 item->di_tv.v_lock = 0;
4524 item->di_tv.v_type = VAR_CHANNEL;
4525 item->di_tv.vval.v_channel = job->jv_channel;
4526 if (job->jv_channel != NULL)
4527 ++job->jv_channel->ch_refcount;
4528 if (dict_add(dict, item) == FAIL)
4529 dictitem_free(item);
4530
4531#ifdef UNIX
4532 nr = job->jv_pid;
4533#else
4534 nr = job->jv_proc_info.dwProcessId;
4535#endif
4536 dict_add_nr_str(dict, "process", nr, NULL);
4537
4538 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004539 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004540 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4541}
4542
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004543 int
4544job_stop(job_T *job, typval_T *argvars)
4545{
4546 char_u *arg;
4547
4548 if (argvars[1].v_type == VAR_UNKNOWN)
4549 arg = (char_u *)"";
4550 else
4551 {
4552 arg = get_tv_string_chk(&argvars[1]);
4553 if (arg == NULL)
4554 {
4555 EMSG(_(e_invarg));
4556 return 0;
4557 }
4558 }
4559 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4560 if (mch_stop_job(job, arg) == FAIL)
4561 return 0;
4562
4563 /* Assume that "hup" does not kill the job. */
4564 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4565 job->jv_channel->ch_job_killed = TRUE;
4566
4567 /* We don't try freeing the job, obviously the caller still has a
4568 * reference to it. */
4569 return 1;
4570}
4571
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004572#endif /* FEAT_JOB_CHANNEL */