blob: 159a3a036802dd80bad5561709167997203812e4 [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. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002785 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2786 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002787
2788 /* Queue a "DETACH" netbeans message in the command queue in order to
2789 * terminate the netbeans session later. Do not end the session here
2790 * directly as we may be running in the context of a call to
2791 * netbeans_parse_messages():
2792 * netbeans_parse_messages
2793 * -> autocmd triggered while processing the netbeans cmd
2794 * -> ui_breakcheck
2795 * -> gui event loop or select loop
2796 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002797 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002798 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002799 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002800 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002801 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2802
2803 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002804 * died. Don't close the channel right away, it may be the wrong moment
2805 * to invoke callbacks. */
2806 channel->ch_to_be_closed = TRUE;
2807}
2808
2809 static void
2810channel_close_now(channel_T *channel)
2811{
2812 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002813 channel_close(channel, TRUE);
2814 if (channel->ch_nb_close_cb != NULL)
2815 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002816}
2817
2818/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002819 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002820 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002821 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002822 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002823 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002824channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002825{
2826 static char_u *buf = NULL;
2827 int len = 0;
2828 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002829 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002830 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002831
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002832 fd = channel->ch_part[part].ch_fd;
2833 if (fd == INVALID_FD)
2834 {
2835 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002836 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002837 }
2838 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002839
2840 /* Allocate a buffer to read into. */
2841 if (buf == NULL)
2842 {
2843 buf = alloc(MAXMSGSIZE);
2844 if (buf == NULL)
2845 return; /* out of memory! */
2846 }
2847
2848 /* Keep on reading for as long as there is something to read.
2849 * Use select() or poll() to avoid blocking on a message that is exactly
2850 * MAXMSGSIZE long. */
2851 for (;;)
2852 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002853 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002854 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002855 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002856 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002857 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002858 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002859 if (len <= 0)
2860 break; /* error or nothing more to read */
2861
2862 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002863 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002864 readlen += len;
2865 if (len < MAXMSGSIZE)
2866 break; /* did read everything that's available */
2867 }
2868
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002869 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002870 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02002871 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002872
2873#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002874 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002875 if (CH_HAS_GUI && gtk_main_level() > 0)
2876 gtk_main_quit();
2877#endif
2878}
2879
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002880/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002881 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002882 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002883 * Returns what was read in allocated memory.
2884 * Returns NULL in case of error or timeout.
2885 */
2886 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002887channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002888{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002889 char_u *buf;
2890 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002891 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002892 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002893 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002894
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002895 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002896 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002897
2898 while (TRUE)
2899 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002900 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002901 if (buf != NULL && (mode == MODE_RAW
2902 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2903 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002904 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002905 continue;
2906
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002907 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002908 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002909 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002910 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002911 {
2912 ch_log(channel, "Timed out");
2913 return NULL;
2914 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002915 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002916 }
2917
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002918 if (mode == MODE_RAW)
2919 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002920 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002921 }
2922 else
2923 {
2924 nl = vim_strchr(buf, NL);
2925 if (nl[1] == NUL)
2926 {
2927 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002928 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002929 *nl = NUL;
2930 }
2931 else
2932 {
2933 /* Copy the message into allocated memory and remove it from the
2934 * buffer. */
2935 msg = vim_strnsave(buf, (int)(nl - buf));
2936 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2937 }
2938 }
2939 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002940 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002941 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002942}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002943
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002944/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002945 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002946 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002947 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002948 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002949 */
2950 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002951channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002952 channel_T *channel,
2953 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002954 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002955 int id,
2956 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002957{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002958 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002959 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002960 int timeout;
2961 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002962
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002963 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002964 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002965 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002966 for (;;)
2967 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002968 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002969
2970 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002971 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002972 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002973 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002974 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002975 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002976
Bram Moolenaard7ece102016-02-02 23:23:02 +01002977 if (!more)
2978 {
2979 /* Handle any other messages in the queue. If done some more
2980 * messages may have arrived. */
2981 if (channel_parse_messages())
2982 continue;
2983
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002984 /* Wait for up to the timeout. If there was an incomplete message
2985 * use the deadline for that. */
2986 timeout = timeout_arg;
2987 if (chanpart->ch_waiting)
2988 {
2989#ifdef WIN32
2990 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2991#else
2992 {
2993 struct timeval now_tv;
2994
2995 gettimeofday(&now_tv, NULL);
2996 timeout = (chanpart->ch_deadline.tv_sec
2997 - now_tv.tv_sec) * 1000
2998 + (chanpart->ch_deadline.tv_usec
2999 - now_tv.tv_usec) / 1000
3000 + 1;
3001 }
3002#endif
3003 if (timeout < 0)
3004 {
3005 /* Something went wrong, channel_parse_json() didn't
3006 * discard message. Cancel waiting. */
3007 chanpart->ch_waiting = FALSE;
3008 timeout = timeout_arg;
3009 }
3010 else if (timeout > timeout_arg)
3011 timeout = timeout_arg;
3012 }
3013 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003014 if (fd == INVALID_FD
3015 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003016 {
3017 if (timeout == timeout_arg)
3018 {
3019 if (fd != INVALID_FD)
3020 ch_log(channel, "Timed out");
3021 break;
3022 }
3023 }
3024 else
3025 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003026 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003027 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003028 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003029 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003030}
3031
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003032/*
3033 * Common for ch_read() and ch_readraw().
3034 */
3035 void
3036common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3037{
3038 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003039 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003040 jobopt_T opt;
3041 int mode;
3042 int timeout;
3043 int id = -1;
3044 typval_T *listtv = NULL;
3045
3046 /* return an empty string by default */
3047 rettv->v_type = VAR_STRING;
3048 rettv->vval.v_string = NULL;
3049
3050 clear_job_options(&opt);
3051 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3052 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003053 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003054
Bram Moolenaar437905c2016-04-26 19:01:05 +02003055 if (opt.jo_set & JO_PART)
3056 part = opt.jo_part;
3057 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003058 if (channel != NULL)
3059 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003060 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003061 part = channel_part_read(channel);
3062 mode = channel_get_mode(channel, part);
3063 timeout = channel_get_timeout(channel, part);
3064 if (opt.jo_set & JO_TIMEOUT)
3065 timeout = opt.jo_timeout;
3066
3067 if (raw || mode == MODE_RAW || mode == MODE_NL)
3068 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3069 else
3070 {
3071 if (opt.jo_set & JO_ID)
3072 id = opt.jo_id;
3073 channel_read_json_block(channel, part, timeout, id, &listtv);
3074 if (listtv != NULL)
3075 {
3076 *rettv = *listtv;
3077 vim_free(listtv);
3078 }
3079 else
3080 {
3081 rettv->v_type = VAR_SPECIAL;
3082 rettv->vval.v_number = VVAL_NONE;
3083 }
3084 }
3085 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003086
3087theend:
3088 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003089}
3090
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003091# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3092 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003093/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003094 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003095 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003096 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003097 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003098channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003099{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003100 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003101 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003102
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003103 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003104 for (channel = first_channel; channel != NULL;
3105 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003106 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003107 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003108 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003109 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003110 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003111 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003112 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003113 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003114 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003115}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003116# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003117
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003118# if defined(WIN32) || defined(PROTO)
3119/*
3120 * Check the channels for anything that is ready to be read.
3121 * The data is put in the read queue.
3122 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003123 void
3124channel_handle_events(void)
3125{
3126 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003127 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003128 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003129
3130 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3131 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003132 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003133 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003134 {
3135 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003136 if (fd != INVALID_FD)
3137 {
3138 int r = channel_wait(channel, fd, 0);
3139
3140 if (r == CW_READY)
3141 channel_read(channel, part, "channel_handle_events");
3142 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003143 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003144 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003145 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003146 }
3147}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003148# endif
3149
Bram Moolenaard04a0202016-01-26 23:30:18 +01003150/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003151 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003152 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003153 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003154 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003155 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003156channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003157{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003158 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003159 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003160 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003161
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003162 fd = channel->ch_part[part].ch_fd;
3163 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003164 {
3165 if (!channel->ch_error && fun != NULL)
3166 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003167 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003168 EMSG2("E630: %s(): write while not connected", fun);
3169 }
3170 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003171 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003172 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003173
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003174 if (log_fd != NULL)
3175 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003176 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003177 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003178 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003179 fprintf(log_fd, "'\n");
3180 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003181 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003182 }
3183
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003184 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003185 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003186 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003187 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003188 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003189 {
3190 if (!channel->ch_error && fun != NULL)
3191 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003192 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003193 EMSG2("E631: %s(): write failed", fun);
3194 }
3195 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003196 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003197 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003198
3199 channel->ch_error = FALSE;
3200 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003201}
3202
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003203/*
3204 * Common for "ch_sendexpr()" and "ch_sendraw()".
3205 * Returns the channel if the caller should read the response.
3206 * Sets "part_read" to the the read fd.
3207 * Otherwise returns NULL.
3208 */
3209 channel_T *
3210send_common(
3211 typval_T *argvars,
3212 char_u *text,
3213 int id,
3214 int eval,
3215 jobopt_T *opt,
3216 char *fun,
3217 int *part_read)
3218{
3219 channel_T *channel;
3220 int part_send;
3221
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003222 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003223 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003224 if (channel == NULL)
3225 return NULL;
3226 part_send = channel_part_send(channel);
3227 *part_read = channel_part_read(channel);
3228
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003229 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3230 return NULL;
3231
3232 /* Set the callback. An empty callback means no callback and not reading
3233 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3234 * allowed. */
3235 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3236 {
3237 if (eval)
3238 {
3239 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3240 return NULL;
3241 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003242 channel_set_req_callback(channel, part_send,
3243 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003244 }
3245
3246 if (channel_send(channel, part_send, text, fun) == OK
3247 && opt->jo_callback == NULL)
3248 return channel;
3249 return NULL;
3250}
3251
3252/*
3253 * common for "ch_evalexpr()" and "ch_sendexpr()"
3254 */
3255 void
3256ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3257{
3258 char_u *text;
3259 typval_T *listtv;
3260 channel_T *channel;
3261 int id;
3262 ch_mode_T ch_mode;
3263 int part_send;
3264 int part_read;
3265 jobopt_T opt;
3266 int timeout;
3267
3268 /* return an empty string by default */
3269 rettv->v_type = VAR_STRING;
3270 rettv->vval.v_string = NULL;
3271
Bram Moolenaar437905c2016-04-26 19:01:05 +02003272 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003273 if (channel == NULL)
3274 return;
3275 part_send = channel_part_send(channel);
3276
3277 ch_mode = channel_get_mode(channel, part_send);
3278 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3279 {
3280 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3281 return;
3282 }
3283
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003284 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003285 text = json_encode_nr_expr(id, &argvars[1],
3286 ch_mode == MODE_JS ? JSON_JS : 0);
3287 if (text == NULL)
3288 return;
3289
3290 channel = send_common(argvars, text, id, eval, &opt,
3291 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3292 vim_free(text);
3293 if (channel != NULL && eval)
3294 {
3295 if (opt.jo_set & JO_TIMEOUT)
3296 timeout = opt.jo_timeout;
3297 else
3298 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003299 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3300 == OK)
3301 {
3302 list_T *list = listtv->vval.v_list;
3303
3304 /* Move the item from the list and then change the type to
3305 * avoid the value being freed. */
3306 *rettv = list->lv_last->li_tv;
3307 list->lv_last->li_tv.v_type = VAR_NUMBER;
3308 free_tv(listtv);
3309 }
3310 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003311 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003312}
3313
3314/*
3315 * common for "ch_evalraw()" and "ch_sendraw()"
3316 */
3317 void
3318ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3319{
3320 char_u buf[NUMBUFLEN];
3321 char_u *text;
3322 channel_T *channel;
3323 int part_read;
3324 jobopt_T opt;
3325 int timeout;
3326
3327 /* return an empty string by default */
3328 rettv->v_type = VAR_STRING;
3329 rettv->vval.v_string = NULL;
3330
3331 text = get_tv_string_buf(&argvars[1], buf);
3332 channel = send_common(argvars, text, 0, eval, &opt,
3333 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3334 if (channel != NULL && eval)
3335 {
3336 if (opt.jo_set & JO_TIMEOUT)
3337 timeout = opt.jo_timeout;
3338 else
3339 timeout = channel_get_timeout(channel, part_read);
3340 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3341 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003342 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003343}
3344
Bram Moolenaard04a0202016-01-26 23:30:18 +01003345# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003346/*
3347 * Add open channels to the poll struct.
3348 * Return the adjusted struct index.
3349 * The type of "fds" is hidden to avoid problems with the function proto.
3350 */
3351 int
3352channel_poll_setup(int nfd_in, void *fds_in)
3353{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003354 int nfd = nfd_in;
3355 channel_T *channel;
3356 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003357 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003358
Bram Moolenaar77073442016-02-13 23:23:53 +01003359 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003360 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003361 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003362 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003363 chanpart_T *ch_part = &channel->ch_part[part];
3364
3365 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003366 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003367 ch_part->ch_poll_idx = nfd;
3368 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003369 fds[nfd].events = POLLIN;
3370 nfd++;
3371 }
3372 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003373 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003374 }
3375 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003376
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003377 nfd = channel_fill_poll_write(nfd, fds);
3378
Bram Moolenaare0874f82016-01-24 20:36:41 +01003379 return nfd;
3380}
3381
3382/*
3383 * The type of "fds" is hidden to avoid problems with the function proto.
3384 */
3385 int
3386channel_poll_check(int ret_in, void *fds_in)
3387{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003388 int ret = ret_in;
3389 channel_T *channel;
3390 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003391 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003392 int idx;
3393 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003394
Bram Moolenaar77073442016-02-13 23:23:53 +01003395 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003396 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003397 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003398 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003399 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003400
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003401 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003402 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003403 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003404 --ret;
3405 }
3406 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003407
3408 in_part = &channel->ch_part[PART_IN];
3409 idx = in_part->ch_poll_idx;
3410 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3411 {
3412 if (in_part->ch_buf_append)
3413 {
3414 if (in_part->ch_buffer != NULL)
3415 channel_write_new_lines(in_part->ch_buffer);
3416 }
3417 else
3418 channel_write_in(channel);
3419 --ret;
3420 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003421 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003422
3423 return ret;
3424}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003425# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003426
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003427# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003428/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003429 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003430 */
3431 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003432channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003433{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003434 int maxfd = maxfd_in;
3435 channel_T *channel;
3436 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003437 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003438 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003439
Bram Moolenaar77073442016-02-13 23:23:53 +01003440 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003441 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003442 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003443 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003444 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003445
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003446 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003447 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003448 FD_SET((int)fd, rfds);
3449 if (maxfd < (int)fd)
3450 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003451 }
3452 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003453 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003454
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003455 maxfd = channel_fill_wfds(maxfd, wfds);
3456
Bram Moolenaare0874f82016-01-24 20:36:41 +01003457 return maxfd;
3458}
3459
3460/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003461 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003462 */
3463 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003464channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003465{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003466 int ret = ret_in;
3467 channel_T *channel;
3468 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003469 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003470 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003471 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003472
Bram Moolenaar77073442016-02-13 23:23:53 +01003473 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003474 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003475 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003476 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003477 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003478
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003479 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003480 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003481 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003482 --ret;
3483 }
3484 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003485
3486 in_part = &channel->ch_part[PART_IN];
3487 if (ret > 0 && in_part->ch_fd != INVALID_FD
3488 && FD_ISSET(in_part->ch_fd, wfds))
3489 {
3490 if (in_part->ch_buf_append)
3491 {
3492 if (in_part->ch_buffer != NULL)
3493 channel_write_new_lines(in_part->ch_buffer);
3494 }
3495 else
3496 channel_write_in(channel);
3497 --ret;
3498 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003499 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003500
3501 return ret;
3502}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003503# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003504
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003505/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003506 * Execute queued up commands.
3507 * Invoked from the main loop when it's safe to execute received commands.
3508 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003509 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003510 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003511channel_parse_messages(void)
3512{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003513 channel_T *channel = first_channel;
3514 int ret = FALSE;
3515 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003516 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003517
Bram Moolenaard0b65022016-03-06 21:50:33 +01003518 /* Only do this message when another message was given, otherwise we get
3519 * lots of them. */
3520 if (did_log_msg)
3521 {
3522 ch_log(NULL, "looking for messages on channels");
3523 did_log_msg = FALSE;
3524 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003525 while (channel != NULL)
3526 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003527 if (channel->ch_to_be_closed)
3528 {
3529 channel->ch_to_be_closed = FALSE;
3530 channel_close_now(channel);
3531 /* channel may have been freed, start over */
3532 channel = first_channel;
3533 continue;
3534 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003535 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003536 {
3537 /* channel is no longer useful, free it */
3538 channel_free(channel);
3539 channel = first_channel;
3540 part = PART_SOCK;
3541 continue;
3542 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003543 if (channel->ch_part[part].ch_fd != INVALID_FD
3544 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003545 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003546 /* Increase the refcount, in case the handler causes the channel
3547 * to be unreferenced or closed. */
3548 ++channel->ch_refcount;
3549 r = may_invoke_callback(channel, part);
3550 if (r == OK)
3551 ret = TRUE;
3552 if (channel_unref(channel) || r == OK)
3553 {
3554 /* channel was freed or something was done, start over */
3555 channel = first_channel;
3556 part = PART_SOCK;
3557 continue;
3558 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003559 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003560 if (part < PART_ERR)
3561 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003562 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003563 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003564 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003565 part = PART_SOCK;
3566 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003567 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003568
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003569 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003570 {
3571 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003572 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003573 }
3574
Bram Moolenaard7ece102016-02-02 23:23:02 +01003575 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003576}
3577
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003578/*
3579 * Mark references to lists used in channels.
3580 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003581 int
3582set_ref_in_channel(int copyID)
3583{
Bram Moolenaar77073442016-02-13 23:23:53 +01003584 int abort = FALSE;
3585 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003586 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003587
Bram Moolenaar77073442016-02-13 23:23:53 +01003588 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003589 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003590 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003591 tv.v_type = VAR_CHANNEL;
3592 tv.vval.v_channel = channel;
3593 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003594 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003595 return abort;
3596}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003597
3598/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003599 * Return the "part" to write to for "channel".
3600 */
3601 int
3602channel_part_send(channel_T *channel)
3603{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003604 if (channel->CH_SOCK_FD == INVALID_FD)
3605 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003606 return PART_SOCK;
3607}
3608
3609/*
3610 * Return the default "part" to read from for "channel".
3611 */
3612 int
3613channel_part_read(channel_T *channel)
3614{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003615 if (channel->CH_SOCK_FD == INVALID_FD)
3616 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003617 return PART_SOCK;
3618}
3619
3620/*
3621 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003622 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003623 */
3624 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003625channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003626{
Bram Moolenaar77073442016-02-13 23:23:53 +01003627 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003628 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003629 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003630}
3631
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003632/*
3633 * Return the timeout of "channel"/"part"
3634 */
3635 int
3636channel_get_timeout(channel_T *channel, int part)
3637{
3638 return channel->ch_part[part].ch_timeout;
3639}
3640
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003641 static int
3642handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3643{
3644 char_u *val = get_tv_string(item);
3645
3646 opt->jo_set |= jo;
3647 if (STRCMP(val, "nl") == 0)
3648 *modep = MODE_NL;
3649 else if (STRCMP(val, "raw") == 0)
3650 *modep = MODE_RAW;
3651 else if (STRCMP(val, "js") == 0)
3652 *modep = MODE_JS;
3653 else if (STRCMP(val, "json") == 0)
3654 *modep = MODE_JSON;
3655 else
3656 {
3657 EMSG2(_(e_invarg2), val);
3658 return FAIL;
3659 }
3660 return OK;
3661}
3662
3663 static int
3664handle_io(typval_T *item, int part, jobopt_T *opt)
3665{
3666 char_u *val = get_tv_string(item);
3667
3668 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3669 if (STRCMP(val, "null") == 0)
3670 opt->jo_io[part] = JIO_NULL;
3671 else if (STRCMP(val, "pipe") == 0)
3672 opt->jo_io[part] = JIO_PIPE;
3673 else if (STRCMP(val, "file") == 0)
3674 opt->jo_io[part] = JIO_FILE;
3675 else if (STRCMP(val, "buffer") == 0)
3676 opt->jo_io[part] = JIO_BUFFER;
3677 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3678 opt->jo_io[part] = JIO_OUT;
3679 else
3680 {
3681 EMSG2(_(e_invarg2), val);
3682 return FAIL;
3683 }
3684 return OK;
3685}
3686
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003687/*
3688 * Clear a jobopt_T before using it.
3689 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003690 void
3691clear_job_options(jobopt_T *opt)
3692{
3693 vim_memset(opt, 0, sizeof(jobopt_T));
3694}
3695
3696/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003697 * Free any members of a jobopt_T.
3698 */
3699 void
3700free_job_options(jobopt_T *opt)
3701{
3702 if (opt->jo_partial != NULL)
3703 partial_unref(opt->jo_partial);
3704 if (opt->jo_out_partial != NULL)
3705 partial_unref(opt->jo_out_partial);
3706 if (opt->jo_err_partial != NULL)
3707 partial_unref(opt->jo_err_partial);
3708 if (opt->jo_close_partial != NULL)
3709 partial_unref(opt->jo_close_partial);
3710}
3711
3712/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003713 * Get the PART_ number from the first character of an option name.
3714 */
3715 static int
3716part_from_char(int c)
3717{
3718 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3719}
3720
3721/*
3722 * Get the option entries from the dict in "tv", parse them and put the result
3723 * in "opt".
3724 * Only accept options in "supported".
3725 * If an option value is invalid return FAIL.
3726 */
3727 int
3728get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3729{
3730 typval_T *item;
3731 char_u *val;
3732 dict_T *dict;
3733 int todo;
3734 hashitem_T *hi;
3735 int part;
3736
3737 opt->jo_set = 0;
3738 if (tv->v_type == VAR_UNKNOWN)
3739 return OK;
3740 if (tv->v_type != VAR_DICT)
3741 {
3742 EMSG(_(e_invarg));
3743 return FAIL;
3744 }
3745 dict = tv->vval.v_dict;
3746 if (dict == NULL)
3747 return OK;
3748
3749 todo = (int)dict->dv_hashtab.ht_used;
3750 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3751 if (!HASHITEM_EMPTY(hi))
3752 {
3753 item = &dict_lookup(hi)->di_tv;
3754
3755 if (STRCMP(hi->hi_key, "mode") == 0)
3756 {
3757 if (!(supported & JO_MODE))
3758 break;
3759 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3760 return FAIL;
3761 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003762 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003763 {
3764 if (!(supported & JO_IN_MODE))
3765 break;
3766 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3767 == FAIL)
3768 return FAIL;
3769 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003770 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003771 {
3772 if (!(supported & JO_OUT_MODE))
3773 break;
3774 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3775 == FAIL)
3776 return FAIL;
3777 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003778 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003779 {
3780 if (!(supported & JO_ERR_MODE))
3781 break;
3782 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3783 == FAIL)
3784 return FAIL;
3785 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003786 else if (STRCMP(hi->hi_key, "in_io") == 0
3787 || STRCMP(hi->hi_key, "out_io") == 0
3788 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003789 {
3790 if (!(supported & JO_OUT_IO))
3791 break;
3792 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3793 return FAIL;
3794 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003795 else if (STRCMP(hi->hi_key, "in_name") == 0
3796 || STRCMP(hi->hi_key, "out_name") == 0
3797 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003798 {
3799 part = part_from_char(*hi->hi_key);
3800
3801 if (!(supported & JO_OUT_IO))
3802 break;
3803 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3804 opt->jo_io_name[part] =
3805 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3806 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003807 else if (STRCMP(hi->hi_key, "in_buf") == 0
3808 || STRCMP(hi->hi_key, "out_buf") == 0
3809 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003810 {
3811 part = part_from_char(*hi->hi_key);
3812
3813 if (!(supported & JO_OUT_IO))
3814 break;
3815 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3816 opt->jo_io_buf[part] = get_tv_number(item);
3817 if (opt->jo_io_buf[part] <= 0)
3818 {
3819 EMSG2(_(e_invarg2), get_tv_string(item));
3820 return FAIL;
3821 }
3822 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3823 {
3824 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3825 return FAIL;
3826 }
3827 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003828 else if (STRCMP(hi->hi_key, "in_top") == 0
3829 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003830 {
3831 linenr_T *lp;
3832
3833 if (!(supported & JO_OUT_IO))
3834 break;
3835 if (hi->hi_key[3] == 't')
3836 {
3837 lp = &opt->jo_in_top;
3838 opt->jo_set |= JO_IN_TOP;
3839 }
3840 else
3841 {
3842 lp = &opt->jo_in_bot;
3843 opt->jo_set |= JO_IN_BOT;
3844 }
3845 *lp = get_tv_number(item);
3846 if (*lp < 0)
3847 {
3848 EMSG2(_(e_invarg2), get_tv_string(item));
3849 return FAIL;
3850 }
3851 }
3852 else if (STRCMP(hi->hi_key, "channel") == 0)
3853 {
3854 if (!(supported & JO_OUT_IO))
3855 break;
3856 opt->jo_set |= JO_CHANNEL;
3857 if (item->v_type != VAR_CHANNEL)
3858 {
3859 EMSG2(_(e_invarg2), "channel");
3860 return FAIL;
3861 }
3862 opt->jo_channel = item->vval.v_channel;
3863 }
3864 else if (STRCMP(hi->hi_key, "callback") == 0)
3865 {
3866 if (!(supported & JO_CALLBACK))
3867 break;
3868 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003869 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003870 if (opt->jo_callback == NULL)
3871 {
3872 EMSG2(_(e_invarg2), "callback");
3873 return FAIL;
3874 }
3875 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003876 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003877 {
3878 if (!(supported & JO_OUT_CALLBACK))
3879 break;
3880 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003881 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003882 if (opt->jo_out_cb == NULL)
3883 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003884 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003885 return FAIL;
3886 }
3887 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003888 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003889 {
3890 if (!(supported & JO_ERR_CALLBACK))
3891 break;
3892 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003893 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003894 if (opt->jo_err_cb == NULL)
3895 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003896 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003897 return FAIL;
3898 }
3899 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003900 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003901 {
3902 if (!(supported & JO_CLOSE_CALLBACK))
3903 break;
3904 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003905 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003906 if (opt->jo_close_cb == NULL)
3907 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003908 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003909 return FAIL;
3910 }
3911 }
3912 else if (STRCMP(hi->hi_key, "waittime") == 0)
3913 {
3914 if (!(supported & JO_WAITTIME))
3915 break;
3916 opt->jo_set |= JO_WAITTIME;
3917 opt->jo_waittime = get_tv_number(item);
3918 }
3919 else if (STRCMP(hi->hi_key, "timeout") == 0)
3920 {
3921 if (!(supported & JO_TIMEOUT))
3922 break;
3923 opt->jo_set |= JO_TIMEOUT;
3924 opt->jo_timeout = get_tv_number(item);
3925 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003926 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003927 {
3928 if (!(supported & JO_OUT_TIMEOUT))
3929 break;
3930 opt->jo_set |= JO_OUT_TIMEOUT;
3931 opt->jo_out_timeout = get_tv_number(item);
3932 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003933 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003934 {
3935 if (!(supported & JO_ERR_TIMEOUT))
3936 break;
3937 opt->jo_set |= JO_ERR_TIMEOUT;
3938 opt->jo_err_timeout = get_tv_number(item);
3939 }
3940 else if (STRCMP(hi->hi_key, "part") == 0)
3941 {
3942 if (!(supported & JO_PART))
3943 break;
3944 opt->jo_set |= JO_PART;
3945 val = get_tv_string(item);
3946 if (STRCMP(val, "err") == 0)
3947 opt->jo_part = PART_ERR;
3948 else
3949 {
3950 EMSG2(_(e_invarg2), val);
3951 return FAIL;
3952 }
3953 }
3954 else if (STRCMP(hi->hi_key, "id") == 0)
3955 {
3956 if (!(supported & JO_ID))
3957 break;
3958 opt->jo_set |= JO_ID;
3959 opt->jo_id = get_tv_number(item);
3960 }
3961 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3962 {
3963 if (!(supported & JO_STOPONEXIT))
3964 break;
3965 opt->jo_set |= JO_STOPONEXIT;
3966 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3967 opt->jo_soe_buf);
3968 if (opt->jo_stoponexit == NULL)
3969 {
3970 EMSG2(_(e_invarg2), "stoponexit");
3971 return FAIL;
3972 }
3973 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003974 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003975 {
3976 if (!(supported & JO_EXIT_CB))
3977 break;
3978 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003979 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3980 {
3981 opt->jo_exit_partial = item->vval.v_partial;
3982 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3983 }
3984 else
3985 opt->jo_exit_cb = get_tv_string_buf_chk(
3986 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003987 if (opt->jo_exit_cb == NULL)
3988 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003989 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003990 return FAIL;
3991 }
3992 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003993 else if (STRCMP(hi->hi_key, "block_write") == 0)
3994 {
3995 if (!(supported & JO_BLOCK_WRITE))
3996 break;
3997 opt->jo_set |= JO_BLOCK_WRITE;
3998 opt->jo_block_write = get_tv_number(item);
3999 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004000 else
4001 break;
4002 --todo;
4003 }
4004 if (todo > 0)
4005 {
4006 EMSG2(_(e_invarg2), hi->hi_key);
4007 return FAIL;
4008 }
4009
4010 return OK;
4011}
4012
4013/*
4014 * Get the channel from the argument.
4015 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004016 * When "check_open" is TRUE check that the channel can be used.
4017 * When "reading" is TRUE "check_open" considers typeahead useful.
4018 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004019 */
4020 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004021get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004022{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004023 channel_T *channel = NULL;
4024 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004025
4026 if (tv->v_type == VAR_JOB)
4027 {
4028 if (tv->vval.v_job != NULL)
4029 channel = tv->vval.v_job->jv_channel;
4030 }
4031 else if (tv->v_type == VAR_CHANNEL)
4032 {
4033 channel = tv->vval.v_channel;
4034 }
4035 else
4036 {
4037 EMSG2(_(e_invarg2), get_tv_string(tv));
4038 return NULL;
4039 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004040 if (channel != NULL && reading)
4041 has_readahead = channel_has_readahead(channel,
4042 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004043
Bram Moolenaar437905c2016-04-26 19:01:05 +02004044 if (check_open && (channel == NULL || (!channel_is_open(channel)
4045 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004046 {
4047 EMSG(_("E906: not an open channel"));
4048 return NULL;
4049 }
4050 return channel;
4051}
4052
4053static job_T *first_job = NULL;
4054
4055 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004056job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004057{
4058 ch_log(job->jv_channel, "Freeing job");
4059 if (job->jv_channel != NULL)
4060 {
4061 /* The link from the channel to the job doesn't count as a reference,
4062 * thus don't decrement the refcount of the job. The reference from
4063 * the job to the channel does count the refrence, decrement it and
4064 * NULL the reference. We don't set ch_job_killed, unreferencing the
4065 * job doesn't mean it stops running. */
4066 job->jv_channel->ch_job = NULL;
4067 channel_unref(job->jv_channel);
4068 }
4069 mch_clear_job(job);
4070
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004071 vim_free(job->jv_stoponexit);
4072 vim_free(job->jv_exit_cb);
4073 partial_unref(job->jv_exit_partial);
4074}
4075
4076 static void
4077job_free_job(job_T *job)
4078{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004079 if (job->jv_next != NULL)
4080 job->jv_next->jv_prev = job->jv_prev;
4081 if (job->jv_prev == NULL)
4082 first_job = job->jv_next;
4083 else
4084 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 vim_free(job);
4086}
4087
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004088 static void
4089job_free(job_T *job)
4090{
4091 if (!in_free_unref_items)
4092 {
4093 job_free_contents(job);
4094 job_free_job(job);
4095 }
4096}
4097
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004098/*
4099 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004100 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4101 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004102 */
4103 static int
4104job_still_useful(job_T *job)
4105{
4106 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004107 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4108 || (job->jv_channel != NULL
4109 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004110}
4111
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004112/*
4113 * Mark references in jobs that are still useful.
4114 */
4115 int
4116set_ref_in_job(int copyID)
4117{
4118 int abort = FALSE;
4119 job_T *job;
4120 typval_T tv;
4121
4122 for (job = first_job; job != NULL; job = job->jv_next)
4123 if (job_still_useful(job))
4124 {
4125 tv.v_type = VAR_JOB;
4126 tv.vval.v_job = job;
4127 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4128 }
4129 return abort;
4130}
4131
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132 void
4133job_unref(job_T *job)
4134{
4135 if (job != NULL && --job->jv_refcount <= 0)
4136 {
4137 /* Do not free the job when it has not ended yet and there is a
4138 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004139 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140 {
4141 job_free(job);
4142 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004143 else if (job->jv_channel != NULL
4144 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004145 {
4146 /* Do remove the link to the channel, otherwise it hangs
4147 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004148 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004149 job->jv_channel->ch_job = NULL;
4150 channel_unref(job->jv_channel);
4151 job->jv_channel = NULL;
4152 }
4153 }
4154}
4155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004156 int
4157free_unused_jobs_contents(int copyID, int mask)
4158{
4159 int did_free = FALSE;
4160 job_T *job;
4161
4162 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004163 if ((job->jv_copyID & mask) != (copyID & mask)
4164 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004165 {
4166 /* Free the channel and ordinary items it contains, but don't
4167 * recurse into Lists, Dictionaries etc. */
4168 job_free_contents(job);
4169 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004170 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004171 return did_free;
4172}
4173
4174 void
4175free_unused_jobs(int copyID, int mask)
4176{
4177 job_T *job;
4178 job_T *job_next;
4179
4180 for (job = first_job; job != NULL; job = job_next)
4181 {
4182 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004183 if ((job->jv_copyID & mask) != (copyID & mask)
4184 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004185 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004186 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004187 job_free_job(job);
4188 }
4189 }
4190}
4191
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004192/*
4193 * Allocate a job. Sets the refcount to one and sets options default.
4194 */
4195 static job_T *
4196job_alloc(void)
4197{
4198 job_T *job;
4199
4200 job = (job_T *)alloc_clear(sizeof(job_T));
4201 if (job != NULL)
4202 {
4203 job->jv_refcount = 1;
4204 job->jv_stoponexit = vim_strsave((char_u *)"term");
4205
4206 if (first_job != NULL)
4207 {
4208 first_job->jv_prev = job;
4209 job->jv_next = first_job;
4210 }
4211 first_job = job;
4212 }
4213 return job;
4214}
4215
4216 void
4217job_set_options(job_T *job, jobopt_T *opt)
4218{
4219 if (opt->jo_set & JO_STOPONEXIT)
4220 {
4221 vim_free(job->jv_stoponexit);
4222 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4223 job->jv_stoponexit = NULL;
4224 else
4225 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4226 }
4227 if (opt->jo_set & JO_EXIT_CB)
4228 {
4229 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004230 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004231 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004232 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004233 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004234 job->jv_exit_partial = NULL;
4235 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004236 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004237 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004238 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004239 job->jv_exit_partial = opt->jo_exit_partial;
4240 if (job->jv_exit_partial != NULL)
4241 ++job->jv_exit_partial->pt_refcount;
4242 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004243 }
4244}
4245
4246/*
4247 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4248 */
4249 void
4250job_stop_on_exit()
4251{
4252 job_T *job;
4253
4254 for (job = first_job; job != NULL; job = job->jv_next)
4255 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4256 mch_stop_job(job, job->jv_stoponexit);
4257}
4258
4259/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004260 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004261 */
4262 void
4263job_check_ended(void)
4264{
4265 static time_t last_check = 0;
4266 time_t now;
4267 job_T *job;
4268 job_T *next;
4269
4270 /* Only do this once in 10 seconds. */
4271 now = time(NULL);
4272 if (last_check + 10 < now)
4273 {
4274 last_check = now;
4275 for (job = first_job; job != NULL; job = next)
4276 {
4277 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004278 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004279 job_status(job); /* may free "job" */
4280 }
4281 }
4282}
4283
4284/*
4285 * "job_start()" function
4286 */
4287 job_T *
4288job_start(typval_T *argvars)
4289{
4290 job_T *job;
4291 char_u *cmd = NULL;
4292#if defined(UNIX)
4293# define USE_ARGV
4294 char **argv = NULL;
4295 int argc = 0;
4296#else
4297 garray_T ga;
4298#endif
4299 jobopt_T opt;
4300 int part;
4301
4302 job = job_alloc();
4303 if (job == NULL)
4304 return NULL;
4305
4306 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004307#ifndef USE_ARGV
4308 ga_init2(&ga, (int)sizeof(char*), 20);
4309#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004310
4311 /* Default mode is NL. */
4312 clear_job_options(&opt);
4313 opt.jo_mode = MODE_NL;
4314 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004315 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4316 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004317 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004318
4319 /* Check that when io is "file" that there is a file name. */
4320 for (part = PART_OUT; part <= PART_IN; ++part)
4321 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4322 && opt.jo_io[part] == JIO_FILE
4323 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4324 || *opt.jo_io_name[part] == NUL))
4325 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004326 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004327 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004328 }
4329
4330 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4331 {
4332 buf_T *buf = NULL;
4333
4334 /* check that we can find the buffer before starting the job */
4335 if (opt.jo_set & JO_IN_BUF)
4336 {
4337 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4338 if (buf == NULL)
4339 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4340 }
4341 else if (!(opt.jo_set & JO_IN_NAME))
4342 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004343 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004344 }
4345 else
4346 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4347 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004348 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004349 if (buf->b_ml.ml_mfp == NULL)
4350 {
4351 char_u numbuf[NUMBUFLEN];
4352 char_u *s;
4353
4354 if (opt.jo_set & JO_IN_BUF)
4355 {
4356 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4357 s = numbuf;
4358 }
4359 else
4360 s = opt.jo_io_name[PART_IN];
4361 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004362 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004363 }
4364 job->jv_in_buf = buf;
4365 }
4366
4367 job_set_options(job, &opt);
4368
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004369 if (argvars[0].v_type == VAR_STRING)
4370 {
4371 /* Command is a string. */
4372 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004373 if (cmd == NULL || *cmd == NUL)
4374 {
4375 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004376 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004377 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004378#ifdef USE_ARGV
4379 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004380 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004381 argv[argc] = NULL;
4382#endif
4383 }
4384 else if (argvars[0].v_type != VAR_LIST
4385 || argvars[0].vval.v_list == NULL
4386 || argvars[0].vval.v_list->lv_len < 1)
4387 {
4388 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004389 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004390 }
4391 else
4392 {
4393 list_T *l = argvars[0].vval.v_list;
4394 listitem_T *li;
4395 char_u *s;
4396
4397#ifdef USE_ARGV
4398 /* Pass argv[] to mch_call_shell(). */
4399 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4400 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004401 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004402#endif
4403 for (li = l->lv_first; li != NULL; li = li->li_next)
4404 {
4405 s = get_tv_string_chk(&li->li_tv);
4406 if (s == NULL)
4407 goto theend;
4408#ifdef USE_ARGV
4409 argv[argc++] = (char *)s;
4410#else
4411 /* Only escape when needed, double quotes are not always allowed. */
4412 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4413 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004414# ifdef WIN32
4415 int old_ssl = p_ssl;
4416
4417 /* This is using CreateProcess, not cmd.exe. Always use
4418 * double quote and backslashes. */
4419 p_ssl = 0;
4420# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004421 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004422# ifdef WIN32
4423 p_ssl = old_ssl;
4424# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004425 if (s == NULL)
4426 goto theend;
4427 ga_concat(&ga, s);
4428 vim_free(s);
4429 }
4430 else
4431 ga_concat(&ga, s);
4432 if (li->li_next != NULL)
4433 ga_append(&ga, ' ');
4434#endif
4435 }
4436#ifdef USE_ARGV
4437 argv[argc] = NULL;
4438#else
4439 cmd = ga.ga_data;
4440#endif
4441 }
4442
4443#ifdef USE_ARGV
4444 if (ch_log_active())
4445 {
4446 garray_T ga;
4447 int i;
4448
4449 ga_init2(&ga, (int)sizeof(char), 200);
4450 for (i = 0; i < argc; ++i)
4451 {
4452 if (i > 0)
4453 ga_concat(&ga, (char_u *)" ");
4454 ga_concat(&ga, (char_u *)argv[i]);
4455 }
4456 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4457 ga_clear(&ga);
4458 }
4459 mch_start_job(argv, job, &opt);
4460#else
4461 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4462 mch_start_job((char *)cmd, job, &opt);
4463#endif
4464
4465 /* If the channel is reading from a buffer, write lines now. */
4466 if (job->jv_channel != NULL)
4467 channel_write_in(job->jv_channel);
4468
4469theend:
4470#ifdef USE_ARGV
4471 vim_free(argv);
4472#else
4473 vim_free(ga.ga_data);
4474#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004475 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004476 return job;
4477}
4478
4479/*
4480 * Get the status of "job" and invoke the exit callback when needed.
4481 * The returned string is not allocated.
4482 */
4483 char *
4484job_status(job_T *job)
4485{
4486 char *result;
4487
4488 if (job->jv_status == JOB_ENDED)
4489 /* No need to check, dead is dead. */
4490 result = "dead";
4491 else if (job->jv_status == JOB_FAILED)
4492 result = "fail";
4493 else
4494 {
4495 result = mch_job_status(job);
4496 if (job->jv_status == JOB_ENDED)
4497 ch_log(job->jv_channel, "Job ended");
4498 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4499 {
4500 typval_T argv[3];
4501 typval_T rettv;
4502 int dummy;
4503
4504 /* invoke the exit callback; make sure the refcount is > 0 */
4505 ++job->jv_refcount;
4506 argv[0].v_type = VAR_JOB;
4507 argv[0].vval.v_job = job;
4508 argv[1].v_type = VAR_NUMBER;
4509 argv[1].vval.v_number = job->jv_exitval;
4510 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004511 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4512 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004513 clear_tv(&rettv);
4514 --job->jv_refcount;
4515 }
4516 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4517 {
4518 /* The job was already unreferenced, now that it ended it can be
4519 * freed. Careful: caller must not use "job" after this! */
4520 job_free(job);
4521 }
4522 }
4523 return result;
4524}
4525
Bram Moolenaar8950a562016-03-12 15:22:55 +01004526/*
4527 * Implementation of job_info().
4528 */
4529 void
4530job_info(job_T *job, dict_T *dict)
4531{
4532 dictitem_T *item;
4533 varnumber_T nr;
4534
4535 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4536
4537 item = dictitem_alloc((char_u *)"channel");
4538 if (item == NULL)
4539 return;
4540 item->di_tv.v_lock = 0;
4541 item->di_tv.v_type = VAR_CHANNEL;
4542 item->di_tv.vval.v_channel = job->jv_channel;
4543 if (job->jv_channel != NULL)
4544 ++job->jv_channel->ch_refcount;
4545 if (dict_add(dict, item) == FAIL)
4546 dictitem_free(item);
4547
4548#ifdef UNIX
4549 nr = job->jv_pid;
4550#else
4551 nr = job->jv_proc_info.dwProcessId;
4552#endif
4553 dict_add_nr_str(dict, "process", nr, NULL);
4554
4555 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004556 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004557 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4558}
4559
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004560 int
4561job_stop(job_T *job, typval_T *argvars)
4562{
4563 char_u *arg;
4564
4565 if (argvars[1].v_type == VAR_UNKNOWN)
4566 arg = (char_u *)"";
4567 else
4568 {
4569 arg = get_tv_string_chk(&argvars[1]);
4570 if (arg == NULL)
4571 {
4572 EMSG(_(e_invarg));
4573 return 0;
4574 }
4575 }
4576 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4577 if (mch_stop_job(job, arg) == FAIL)
4578 return 0;
4579
4580 /* Assume that "hup" does not kill the job. */
4581 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4582 job->jv_channel->ch_job_killed = TRUE;
4583
4584 /* We don't try freeing the job, obviously the caller still has a
4585 * reference to it. */
4586 return 1;
4587}
4588
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004589#endif /* FEAT_JOB_CHANNEL */