blob: 1be3d27ffef7cd243122d04a492b21265d23c551 [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
355 /* If there is no callback then nobody can get readahead. If the fd is
356 * closed and there is no readahead then the callback won't be called. */
357 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
358 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
359 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100360 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
361 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
362 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
363 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
364 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
365 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100366 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100367 || has_out_msg || has_err_msg))
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100368 || (channel->ch_part[PART_OUT].ch_callback != NULL && has_out_msg)
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100369 || (channel->ch_part[PART_ERR].ch_callback != NULL && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100370}
371
372/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 * Close a channel and free all its resources.
374 */
375 static void
376channel_free_contents(channel_T *channel)
377{
378 channel_close(channel, TRUE);
379 channel_clear(channel);
380 ch_log(channel, "Freeing channel");
381}
382
383 static void
384channel_free_channel(channel_T *channel)
385{
386 if (channel->ch_next != NULL)
387 channel->ch_next->ch_prev = channel->ch_prev;
388 if (channel->ch_prev == NULL)
389 first_channel = channel->ch_next;
390 else
391 channel->ch_prev->ch_next = channel->ch_next;
392 vim_free(channel);
393}
394
395 static void
396channel_free(channel_T *channel)
397{
398 if (!in_free_unref_items)
399 {
400 channel_free_contents(channel);
401 channel_free_channel(channel);
402 }
403}
404
405/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100406 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100407 * possible, there is no callback to be invoked or the associated job was
408 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100409 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100410 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100411 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100412channel_may_free(channel_T *channel)
413{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100414 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100416 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100417 return TRUE;
418 }
419 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100420}
421
422/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100423 * Decrement the reference count on "channel" and maybe free it when it goes
424 * down to zero. Don't free it if there is a pending action.
425 * Returns TRUE when the channel is no longer referenced.
426 */
427 int
428channel_unref(channel_T *channel)
429{
430 if (channel != NULL && --channel->ch_refcount <= 0)
431 return channel_may_free(channel);
432 return FALSE;
433}
434
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200435 int
436free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100437{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200438 int did_free = FALSE;
439 channel_T *ch;
440
441 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200442 if (!channel_still_useful(ch)
443 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200444 {
445 /* Free the channel and ordinary items it contains, but don't
446 * recurse into Lists, Dictionaries etc. */
447 channel_free_contents(ch);
448 did_free = TRUE;
449 }
450 return did_free;
451}
452
453 void
454free_unused_channels(int copyID, int mask)
455{
456 channel_T *ch;
457 channel_T *ch_next;
458
459 for (ch = first_channel; ch != NULL; ch = ch_next)
460 {
461 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200462 if (!channel_still_useful(ch)
463 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200465 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 channel_free_channel(ch);
467 }
468 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100469}
470
Bram Moolenaard04a0202016-01-26 23:30:18 +0100471#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100472
473#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
474 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100475channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100476{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100477 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100478 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100479
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100480 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100481 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100482 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100483 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100484 channel_read(channel, part, "messageFromNetbeans");
Bram Moolenaar77073442016-02-13 23:23:53 +0100485}
486#endif
487
Bram Moolenaare0874f82016-01-24 20:36:41 +0100488/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100489 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100490 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100491#ifdef FEAT_GUI_X11
492 static void
493messageFromNetbeans(XtPointer clientData,
494 int *unused1 UNUSED,
495 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100497 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100500
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100502# if GTK_CHECK_VERSION(3,0,0)
503 static gboolean
504messageFromNetbeans(GIOChannel *unused1 UNUSED,
505 GIOCondition unused2 UNUSED,
506 gpointer clientData)
507{
508 channel_read_fd(GPOINTER_TO_INT(clientData));
509 return TRUE; /* Return FALSE instead in case the event source is to
510 * be removed after this function returns. */
511}
512# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513 static void
514messageFromNetbeans(gpointer clientData,
515 gint unused1 UNUSED,
516 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100518 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100519}
Bram Moolenaar98921892016-02-23 17:14:37 +0100520# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100521#endif
522
523 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100524channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100525{
Bram Moolenaarde279892016-03-11 22:19:44 +0100526 if (!CH_HAS_GUI)
527 return;
528
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100529# ifdef FEAT_GUI_X11
530 /* Tell notifier we are interested in being called
531 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100532 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
533 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100534 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100535 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100536 (XtPointer)(XtInputReadMask + XtInputExceptMask),
537 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100538 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100539# else
540# ifdef FEAT_GUI_GTK
541 /* Tell gdk we are interested in being called when there
542 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100543 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100544# if GTK_CHECK_VERSION(3,0,0)
545 {
546 GIOChannel *chnnl = g_io_channel_unix_new(
547 (gint)channel->ch_part[part].ch_fd);
548
549 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
550 chnnl,
551 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
552 messageFromNetbeans,
553 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
554
555 g_io_channel_unref(chnnl);
556 }
557# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100558 channel->ch_part[part].ch_inputHandler = gdk_input_add(
559 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100560 (GdkInputCondition)
561 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100562 messageFromNetbeans,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100563 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100564# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100565# endif
566# endif
567}
568
Bram Moolenaarde279892016-03-11 22:19:44 +0100569 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100570channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100571{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100572 if (channel->CH_SOCK_FD != INVALID_FD)
573 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100574 if (channel->CH_OUT_FD != INVALID_FD)
575 channel_gui_register_one(channel, PART_OUT);
576 if (channel->CH_ERR_FD != INVALID_FD)
577 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100578}
579
580/*
581 * Register any of our file descriptors with the GUI event handling system.
582 * Called when the GUI has started.
583 */
584 void
585channel_gui_register_all(void)
586{
Bram Moolenaar77073442016-02-13 23:23:53 +0100587 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100588
Bram Moolenaar77073442016-02-13 23:23:53 +0100589 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100590 channel_gui_register(channel);
591}
592
593 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100594channel_gui_unregister_one(channel_T *channel, int part)
595{
596# ifdef FEAT_GUI_X11
597 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
598 {
599 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
600 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
601 }
602# else
603# ifdef FEAT_GUI_GTK
604 if (channel->ch_part[part].ch_inputHandler != 0)
605 {
606# if GTK_CHECK_VERSION(3,0,0)
607 g_source_remove(channel->ch_part[part].ch_inputHandler);
608# else
609 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
610# endif
611 channel->ch_part[part].ch_inputHandler = 0;
612 }
613# endif
614# endif
615}
616
617 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100618channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100619{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100620 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100621
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100622 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100623 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100624}
625
626#endif
627
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100628static char *e_cannot_connect = N_("E902: Cannot connect to port");
629
Bram Moolenaard04a0202016-01-26 23:30:18 +0100630/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100631 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100632 * "waittime" is the time in msec to wait for the connection.
633 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100634 * Returns the channel for success.
635 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100636 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100637 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100638channel_open(
639 char *hostname,
640 int port_in,
641 int waittime,
642 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100643{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100644 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100645 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100646 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100647#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100649 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100650#else
651 int port = port_in;
652#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100653 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100654 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100656#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100657 channel_init_winsock();
658#endif
659
Bram Moolenaar77073442016-02-13 23:23:53 +0100660 channel = add_channel();
661 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100663 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100664 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100665 }
666
667 /* Get the server internet address and put into addr structure */
668 /* fill in the socket address structure and connect to server */
669 vim_memset((char *)&server, 0, sizeof(server));
670 server.sin_family = AF_INET;
671 server.sin_port = htons(port);
672 if ((host = gethostbyname(hostname)) == NULL)
673 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100675 PERROR("E901: gethostbyname() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100676 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678 }
679 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
680
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100681 /* On Mac and Solaris a zero timeout almost never works. At least wait
682 * one millisecond. Let's do it for all systems, because we don't know why
683 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684 if (waittime == 0)
685 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100686
687 /*
688 * For Unix we need to call connect() again after connect() failed.
689 * On Win32 one time is sufficient.
690 */
691 while (TRUE)
692 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100693 long elapsed_msec = 0;
694 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100695
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100696 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 sd = socket(AF_INET, SOCK_STREAM, 0);
699 if (sd == -1)
700 {
701 ch_error(channel, "in socket() in channel_open().");
702 PERROR("E898: socket() in channel_open()");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100703 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100704 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100705 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100706
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100707 if (waittime >= 0)
708 {
709 /* Make connect() non-blocking. */
710 if (
711#ifdef _WIN32
712 ioctlsocket(sd, FIONBIO, &val) < 0
713#else
714 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
715#endif
716 )
717 {
718 SOCK_ERRNO;
719 ch_errorn(channel,
720 "channel_open: Connect failed with errno %d", errno);
721 sock_close(sd);
722 channel_free(channel);
723 return NULL;
724 }
725 }
726
727 /* Try connecting to the server. */
728 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
729 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
730
Bram Moolenaar045a2842016-03-08 22:33:07 +0100731 if (ret == 0)
732 /* The connection could be established. */
733 break;
734
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100735 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736 if (waittime < 0 || (errno != EWOULDBLOCK
737 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100738#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100740#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100741 ))
742 {
743 ch_errorn(channel,
744 "channel_open: Connect failed with errno %d", errno);
745 PERROR(_(e_cannot_connect));
746 sock_close(sd);
747 channel_free(channel);
748 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100749 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100750
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100751 /* Limit the waittime to 50 msec. If it doesn't work within this
752 * time we close the socket and try creating it again. */
753 waitnow = waittime > 50 ? 50 : waittime;
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100756 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100757#ifndef WIN32
758 if (errno != ECONNREFUSED)
759#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100760 {
761 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100762 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100764#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100765 int so_error = 0;
766 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100767 struct timeval start_tv;
768 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100769#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 FD_ZERO(&rfds);
771 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100772 FD_ZERO(&wfds);
773 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100774
Bram Moolenaar562ca712016-03-09 21:50:05 +0100775 tv.tv_sec = waitnow / 1000;
776 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100777#ifndef WIN32
778 gettimeofday(&start_tv, NULL);
779#endif
780 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100781 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100782 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100783
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 if (ret < 0)
785 {
786 SOCK_ERRNO;
787 ch_errorn(channel,
788 "channel_open: Connect failed with errno %d", errno);
789 PERROR(_(e_cannot_connect));
790 sock_close(sd);
791 channel_free(channel);
792 return NULL;
793 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100794
Bram Moolenaare081e212016-02-28 22:33:46 +0100795#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100796 /* On Win32: select() is expected to work and wait for up to
797 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100798 if (FD_ISSET(sd, &wfds))
799 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100800 elapsed_msec = waitnow;
801 if (waittime > 1 && elapsed_msec < waittime)
802 {
803 waittime -= elapsed_msec;
804 continue;
805 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100806#else
807 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 * After putting the socket in non-blocking mode, connect() will
809 * return EINPROGRESS, select() will not wait (as if writing is
810 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100811 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100812 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100814 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 {
816 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100817 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 if (ret < 0 || (so_error != 0
819 && so_error != EWOULDBLOCK
820 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100821# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100822 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100823# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100824 ))
825 {
826 ch_errorn(channel,
827 "channel_open: Connect failed with errno %d",
828 so_error);
829 PERROR(_(e_cannot_connect));
830 sock_close(sd);
831 channel_free(channel);
832 return NULL;
833 }
834 }
835
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 if (FD_ISSET(sd, &wfds) && so_error == 0)
837 /* Did not detect an error, connection is established. */
838 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100839
Bram Moolenaar045a2842016-03-08 22:33:07 +0100840 gettimeofday(&end_tv, NULL);
841 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
842 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100843#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100844 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100845
846#ifndef WIN32
847 if (waittime > 1 && elapsed_msec < waittime)
848 {
849 /* The port isn't ready but we also didn't get an error.
850 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100851 * yet. Select() may return early, wait until the remaining
852 * "waitnow" and try again. */
853 waitnow -= elapsed_msec;
854 waittime -= elapsed_msec;
855 if (waitnow > 0)
856 {
857 mch_delay((long)waitnow, TRUE);
858 ui_breakcheck();
859 waittime -= waitnow;
860 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100861 if (!got_int)
862 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100863 if (waittime <= 0)
864 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100865 waittime = 1;
866 continue;
867 }
868 /* we were interrupted, behave as if timed out */
869 }
870#endif
871
872 /* We timed out. */
873 ch_error(channel, "Connection timed out");
874 sock_close(sd);
875 channel_free(channel);
876 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100877 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100878
Bram Moolenaar045a2842016-03-08 22:33:07 +0100879 ch_log(channel, "Connection made");
880
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100881 if (waittime >= 0)
882 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100883#ifdef _WIN32
884 val = 0;
885 ioctlsocket(sd, FIONBIO, &val);
886#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100887 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100888#endif
889 }
890
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100891 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100892 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100893 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
894 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100895
896#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100897 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100898#endif
899
Bram Moolenaar77073442016-02-13 23:23:53 +0100900 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100901}
902
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100903/*
904 * Implements ch_open().
905 */
906 channel_T *
907channel_open_func(typval_T *argvars)
908{
909 char_u *address;
910 char_u *p;
911 char *rest;
912 int port;
913 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200914 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100915
916 address = get_tv_string(&argvars[0]);
917 if (argvars[1].v_type != VAR_UNKNOWN
918 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
919 {
920 EMSG(_(e_invarg));
921 return NULL;
922 }
923
924 /* parse address */
925 p = vim_strchr(address, ':');
926 if (p == NULL)
927 {
928 EMSG2(_(e_invarg2), address);
929 return NULL;
930 }
931 *p++ = NUL;
932 port = strtol((char *)p, &rest, 10);
933 if (*address == NUL || port <= 0 || *rest != NUL)
934 {
935 p[-1] = ':';
936 EMSG2(_(e_invarg2), address);
937 return NULL;
938 }
939
940 /* parse options */
941 clear_job_options(&opt);
942 opt.jo_mode = MODE_JSON;
943 opt.jo_timeout = 2000;
944 if (get_job_options(&argvars[1], &opt,
945 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200946 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100947 if (opt.jo_timeout < 0)
948 {
949 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200950 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100951 }
952
953 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
954 if (channel != NULL)
955 {
956 opt.jo_set = JO_ALL;
957 channel_set_options(channel, &opt);
958 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200959theend:
960 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100961 return channel;
962}
963
Bram Moolenaarde279892016-03-11 22:19:44 +0100964 static void
965may_close_part(sock_T *fd)
966{
967 if (*fd != INVALID_FD)
968 {
969 fd_close(*fd);
970 *fd = INVALID_FD;
971 }
972}
973
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100974 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100975channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100976{
Bram Moolenaarde279892016-03-11 22:19:44 +0100977 if (in != INVALID_FD)
978 {
979 may_close_part(&channel->CH_IN_FD);
980 channel->CH_IN_FD = in;
981 }
982 if (out != INVALID_FD)
983 {
984# if defined(FEAT_GUI)
985 channel_gui_unregister_one(channel, PART_OUT);
986# endif
987 may_close_part(&channel->CH_OUT_FD);
988 channel->CH_OUT_FD = out;
989# if defined(FEAT_GUI)
990 channel_gui_register_one(channel, PART_OUT);
991# endif
992 }
993 if (err != INVALID_FD)
994 {
995# if defined(FEAT_GUI)
996 channel_gui_unregister_one(channel, PART_ERR);
997# endif
998 may_close_part(&channel->CH_ERR_FD);
999 channel->CH_ERR_FD = err;
1000# if defined(FEAT_GUI)
1001 channel_gui_register_one(channel, PART_ERR);
1002# endif
1003 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001004}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001005
Bram Moolenaard6051b52016-02-28 15:49:03 +01001006/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001007 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001008 * This does not keep a refcount, when the job is freed ch_job is cleared.
1009 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001010 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001011channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001012{
Bram Moolenaar77073442016-02-13 23:23:53 +01001013 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001014
1015 channel_set_options(channel, options);
1016
1017 if (job->jv_in_buf != NULL)
1018 {
1019 chanpart_T *in_part = &channel->ch_part[PART_IN];
1020
1021 in_part->ch_buffer = job->jv_in_buf;
1022 ch_logs(channel, "reading from buffer '%s'",
1023 (char *)in_part->ch_buffer->b_ffname);
1024 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001025 {
1026 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1027 {
1028 /* Special mode: send last-but-one line when appending a line
1029 * to the buffer. */
1030 in_part->ch_buffer->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001031 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001032 in_part->ch_buf_top =
1033 in_part->ch_buffer->b_ml.ml_line_count + 1;
1034 }
1035 else
1036 in_part->ch_buf_top = options->jo_in_top;
1037 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001038 else
1039 in_part->ch_buf_top = 1;
1040 if (options->jo_set & JO_IN_BOT)
1041 in_part->ch_buf_bot = options->jo_in_bot;
1042 else
1043 in_part->ch_buf_bot = in_part->ch_buffer->b_ml.ml_line_count;
1044 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001045}
1046
1047/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001048 * Find a buffer matching "name" or create a new one.
1049 */
1050 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001051find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001052{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001053 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001054 buf_T *save_curbuf = curbuf;
1055
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001056 if (name != NULL && *name != NUL)
1057 buf = buflist_findname(name);
Bram Moolenaar187db502016-02-27 14:44:26 +01001058 if (buf == NULL)
1059 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001060 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001061 NULL, (linenr_T)0, BLN_LISTED);
Bram Moolenaar187db502016-02-27 14:44:26 +01001062 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001063 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001064#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001065 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1066 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001067#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001068 if (curbuf->b_ml.ml_mfp == NULL)
1069 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001070 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1071 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001072 changed_bytes(1, 0);
1073 curbuf = save_curbuf;
1074 }
1075
1076 return buf;
1077}
1078
1079/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001080 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001081 */
1082 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001083channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001084{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001085 int part;
1086 char_u **cbp;
1087 partial_T **pp;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001088
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001089 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001090 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001091 channel->ch_part[part].ch_mode = opt->jo_mode;
1092 if (opt->jo_set & JO_IN_MODE)
1093 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1094 if (opt->jo_set & JO_OUT_MODE)
1095 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1096 if (opt->jo_set & JO_ERR_MODE)
1097 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001098
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001099 if (opt->jo_set & JO_TIMEOUT)
1100 for (part = PART_SOCK; part <= PART_IN; ++part)
1101 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1102 if (opt->jo_set & JO_OUT_TIMEOUT)
1103 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1104 if (opt->jo_set & JO_ERR_TIMEOUT)
1105 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001106 if (opt->jo_set & JO_BLOCK_WRITE)
1107 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001108
1109 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001110 {
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001111 cbp = &channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001112 pp = &channel->ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001113 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001114 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001115 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
1116 *cbp = vim_strsave(opt->jo_callback);
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001117 else
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001118 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001119 *pp = opt->jo_partial;
1120 if (*pp != NULL)
1121 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001122 }
1123 if (opt->jo_set & JO_OUT_CALLBACK)
1124 {
1125 cbp = &channel->ch_part[PART_OUT].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001126 pp = &channel->ch_part[PART_OUT].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001127 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001128 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001129 if (opt->jo_out_cb != NULL && *opt->jo_out_cb != NUL)
1130 *cbp = vim_strsave(opt->jo_out_cb);
1131 else
1132 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001133 *pp = opt->jo_out_partial;
1134 if (*pp != NULL)
1135 ++(*pp)->pt_refcount;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001136 }
1137 if (opt->jo_set & JO_ERR_CALLBACK)
1138 {
1139 cbp = &channel->ch_part[PART_ERR].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001140 pp = &channel->ch_part[PART_ERR].ch_partial;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001141 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001142 partial_unref(*pp);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
1144 *cbp = vim_strsave(opt->jo_err_cb);
1145 else
1146 *cbp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001147 *pp = opt->jo_err_partial;
1148 if (*pp != NULL)
1149 ++(*pp)->pt_refcount;
Bram Moolenaar0ba75a92016-02-19 23:21:26 +01001150 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001151 if (opt->jo_set & JO_CLOSE_CALLBACK)
1152 {
1153 cbp = &channel->ch_close_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 pp = &channel->ch_close_partial;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001155 vim_free(*cbp);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001156 partial_unref(*pp);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001157 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
1158 *cbp = vim_strsave(opt->jo_close_cb);
1159 else
1160 *cbp = NULL;
Bram Moolenaarbdf0bda2016-03-30 21:06:57 +02001161 *pp = opt->jo_close_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001162 if (*pp != NULL)
1163 ++(*pp)->pt_refcount;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001164 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001165
1166 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1167 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001168 /* writing output to a buffer. Default mode is NL. */
1169 if (!(opt->jo_set & JO_OUT_MODE))
1170 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001171 if (opt->jo_set & JO_OUT_BUF)
1172 channel->ch_part[PART_OUT].ch_buffer =
1173 buflist_findnr(opt->jo_io_buf[PART_OUT]);
1174 else
1175 channel->ch_part[PART_OUT].ch_buffer =
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001176 find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1177 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaar187db502016-02-27 14:44:26 +01001178 (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
1179 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001180
1181 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1182 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1183 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1184 {
1185 /* writing err to a buffer. Default mode is NL. */
1186 if (!(opt->jo_set & JO_ERR_MODE))
1187 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1188 if (opt->jo_io[PART_ERR] == JIO_OUT)
1189 channel->ch_part[PART_ERR].ch_buffer =
1190 channel->ch_part[PART_OUT].ch_buffer;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001191 else if (opt->jo_set & JO_ERR_BUF)
1192 channel->ch_part[PART_ERR].ch_buffer =
1193 buflist_findnr(opt->jo_io_buf[PART_ERR]);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001194 else
1195 channel->ch_part[PART_ERR].ch_buffer =
1196 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1197 ch_logs(channel, "writing err to buffer '%s'",
1198 (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
1199 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001200
1201 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1202 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1203 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001204}
1205
1206/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001207 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001208 */
1209 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001210channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001211 channel_T *channel,
1212 int part,
1213 char_u *callback,
1214 partial_T *partial,
1215 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001216{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001217 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001218 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1219
1220 if (item != NULL)
1221 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001222 item->cq_callback = vim_strsave(callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001223 item->cq_partial = partial;
1224 if (partial != NULL)
1225 ++partial->pt_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01001226 item->cq_seq_nr = id;
1227 item->cq_prev = head->cq_prev;
1228 head->cq_prev = item;
1229 item->cq_next = NULL;
1230 if (item->cq_prev == NULL)
1231 head->cq_next = item;
1232 else
1233 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001234 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001235}
1236
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001237 static void
1238write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1239{
1240 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001241 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001242 char_u *p;
1243
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001244 if ((p = alloc(len + 2)) == NULL)
1245 return;
1246 STRCPY(p, line);
1247 p[len] = NL;
1248 p[len + 1] = NUL;
1249 channel_send(channel, PART_IN, p, "write_buf_line()");
1250 vim_free(p);
1251}
1252
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001253/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001254 * Return TRUE if "channel" can be written to.
1255 * Returns FALSE if the input is closed or the write would block.
1256 */
1257 static int
1258can_write_buf_line(channel_T *channel)
1259{
1260 chanpart_T *in_part = &channel->ch_part[PART_IN];
1261
1262 if (in_part->ch_fd == INVALID_FD)
1263 return FALSE; /* pipe was closed */
1264
1265 /* for testing: block every other attempt to write */
1266 if (in_part->ch_block_write == 1)
1267 in_part->ch_block_write = -1;
1268 else if (in_part->ch_block_write == -1)
1269 in_part->ch_block_write = 1;
1270
1271 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1272#ifndef WIN32
1273 {
1274# if defined(HAVE_SELECT)
1275 struct timeval tval;
1276 fd_set wfds;
1277 int ret;
1278
1279 FD_ZERO(&wfds);
1280 FD_SET((int)in_part->ch_fd, &wfds);
1281 tval.tv_sec = 0;
1282 tval.tv_usec = 0;
1283 for (;;)
1284 {
1285 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1286# ifdef EINTR
1287 SOCK_ERRNO;
1288 if (ret == -1 && errno == EINTR)
1289 continue;
1290# endif
1291 if (ret <= 0 || in_part->ch_block_write == 1)
1292 {
1293 if (ret > 0)
1294 ch_log(channel, "FAKED Input not ready for writing");
1295 else
1296 ch_log(channel, "Input not ready for writing");
1297 return FALSE;
1298 }
1299 break;
1300 }
1301# else
1302 struct pollfd fds;
1303
1304 fds.fd = in_part->ch_fd;
1305 fds.events = POLLOUT;
1306 if (poll(&fds, 1, 0) <= 0)
1307 {
1308 ch_log(channel, "Input not ready for writing");
1309 return FALSE;
1310 }
1311 if (in_part->ch_block_write == 1)
1312 {
1313 ch_log(channel, "FAKED Input not ready for writing");
1314 return FALSE;
1315 }
1316# endif
1317 }
1318#endif
1319 return TRUE;
1320}
1321
1322/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001323 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001324 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001325 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001326channel_write_in(channel_T *channel)
1327{
1328 chanpart_T *in_part = &channel->ch_part[PART_IN];
1329 linenr_T lnum;
1330 buf_T *buf = in_part->ch_buffer;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001331 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001332
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001333 if (buf == NULL || in_part->ch_buf_append)
1334 return; /* no buffer or using appending */
Bram Moolenaar014069a2016-03-03 22:51:40 +01001335 if (!buf_valid(buf) || buf->b_ml.ml_mfp == NULL)
1336 {
1337 /* buffer was wiped out or unloaded */
1338 in_part->ch_buffer = NULL;
1339 return;
1340 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001341
1342 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1343 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1344 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001345 if (!can_write_buf_line(channel))
1346 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001347 write_buf_line(buf, lnum, channel);
1348 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001349 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001350
1351 if (written == 1)
1352 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1353 else if (written > 1)
1354 ch_logn(channel, "written %d lines to channel", written);
1355
Bram Moolenaar014069a2016-03-03 22:51:40 +01001356 in_part->ch_buf_top = lnum;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001357 if (lnum > buf->b_ml.ml_line_count)
1358 {
1359 /* Writing is done, no longer need the buffer. */
1360 in_part->ch_buffer = NULL;
1361 ch_log(channel, "Finished writing all lines to channel");
1362 }
1363 else
1364 ch_logn(channel, "Still %d more lines to write",
1365 buf->b_ml.ml_line_count - lnum + 1);
1366}
1367
1368/*
1369 * Write any lines waiting to be written to a channel.
1370 */
1371 void
1372channel_write_any_lines()
1373{
1374 channel_T *channel;
1375
1376 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1377 {
1378 chanpart_T *in_part = &channel->ch_part[PART_IN];
1379
1380 if (in_part->ch_buffer != NULL)
1381 {
1382 if (in_part->ch_buf_append)
1383 channel_write_new_lines(in_part->ch_buffer);
1384 else
1385 channel_write_in(channel);
1386 }
1387 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001388}
1389
1390/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 * Write appended lines above the last one in "buf" to the channel.
1392 */
1393 void
1394channel_write_new_lines(buf_T *buf)
1395{
1396 channel_T *channel;
1397 int found_one = FALSE;
1398
1399 /* There could be more than one channel for the buffer, loop over all of
1400 * them. */
1401 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1402 {
1403 chanpart_T *in_part = &channel->ch_part[PART_IN];
1404 linenr_T lnum;
1405 int written = 0;
1406
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001407 if (in_part->ch_buffer == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001408 {
1409 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001410 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411 found_one = TRUE;
1412 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1413 ++lnum)
1414 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 if (!can_write_buf_line(channel))
1416 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001417 write_buf_line(buf, lnum, channel);
1418 ++written;
1419 }
1420
1421 if (written == 1)
1422 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1423 else if (written > 1)
1424 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001425 if (lnum < buf->b_ml.ml_line_count)
1426 ch_logn(channel, "Still %d more lines to write",
1427 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001428
1429 in_part->ch_buf_bot = lnum;
1430 }
1431 }
1432 if (!found_one)
1433 buf->b_write_to_channel = FALSE;
1434}
1435
1436/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001437 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001438 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001439 */
1440 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001441invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1442 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001443{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001444 typval_T rettv;
1445 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001446
Bram Moolenaar77073442016-02-13 23:23:53 +01001447 argv[0].v_type = VAR_CHANNEL;
1448 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001449
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001450 call_func(callback, (int)STRLEN(callback),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001451 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001452 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001453 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001454}
1455
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001456/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001457 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001458 * The caller must free it.
1459 * Returns NULL if there is nothing.
1460 */
1461 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001462channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001463{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001464 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001465 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001466 char_u *p;
1467
Bram Moolenaar77073442016-02-13 23:23:53 +01001468 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001469 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001470 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001471 p = node->rq_buffer;
1472 head->rq_next = node->rq_next;
1473 if (node->rq_next == NULL)
1474 head->rq_prev = NULL;
1475 else
1476 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001477 vim_free(node);
1478 return p;
1479}
1480
1481/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001482 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001483 */
1484 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001485channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001486{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001487 readq_T *head = &channel->ch_part[part].ch_head;
1488 readq_T *node = head->rq_next;
1489 long_u len = 1;
1490 char_u *res;
1491 char_u *p;
1492
1493 /* If there is only one buffer just get that one. */
1494 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1495 return channel_get(channel, part);
1496
1497 /* Concatenate everything into one buffer. */
1498 for (node = head->rq_next; node != NULL; node = node->rq_next)
1499 len += (long_u)STRLEN(node->rq_buffer);
1500 res = lalloc(len, TRUE);
1501 if (res == NULL)
1502 return NULL;
1503 *res = NUL;
1504 for (node = head->rq_next; node != NULL; node = node->rq_next)
1505 STRCAT(res, node->rq_buffer);
1506
1507 /* Free all buffers */
1508 do
1509 {
1510 p = channel_get(channel, part);
1511 vim_free(p);
1512 } while (p != NULL);
1513
1514 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001515}
1516
1517/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001518 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001519 * Returns FAIL if that is not possible.
1520 */
1521 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001522channel_collapse(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001523{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001524 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001525 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001526 char_u *p;
1527
Bram Moolenaar77073442016-02-13 23:23:53 +01001528 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001529 return FAIL;
1530
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 p = alloc((unsigned)(STRLEN(node->rq_buffer)
1532 + STRLEN(node->rq_next->rq_buffer) + 1));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001533 if (p == NULL)
1534 return FAIL; /* out of memory */
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 STRCPY(p, node->rq_buffer);
1536 STRCAT(p, node->rq_next->rq_buffer);
1537 vim_free(node->rq_next->rq_buffer);
1538 node->rq_next->rq_buffer = p;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001539
Bram Moolenaar77073442016-02-13 23:23:53 +01001540 /* dispose of the node and its buffer */
1541 head->rq_next = node->rq_next;
1542 head->rq_next->rq_prev = NULL;
1543 vim_free(node->rq_buffer);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001544 vim_free(node);
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001549 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001550 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001551 * Returns OK or FAIL.
1552 */
1553 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001554channel_save(channel_T *channel, int part, char_u *buf, int len,
1555 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001556{
1557 readq_T *node;
1558 readq_T *head = &channel->ch_part[part].ch_head;
1559 char_u *p;
1560 int i;
1561
1562 node = (readq_T *)alloc(sizeof(readq_T));
1563 if (node == NULL)
1564 return FAIL; /* out of memory */
1565 node->rq_buffer = alloc(len + 1);
1566 if (node->rq_buffer == NULL)
1567 {
1568 vim_free(node);
1569 return FAIL; /* out of memory */
1570 }
1571
1572 if (channel->ch_part[part].ch_mode == MODE_NL)
1573 {
1574 /* Drop any CR before a NL. */
1575 p = node->rq_buffer;
1576 for (i = 0; i < len; ++i)
1577 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1578 *p++ = buf[i];
1579 *p = NUL;
1580 }
1581 else
1582 {
1583 mch_memmove(node->rq_buffer, buf, len);
1584 node->rq_buffer[len] = NUL;
1585 }
1586
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001587 if (prepend)
1588 {
1589 /* preend node to the head of the queue */
1590 node->rq_next = head->rq_next;
1591 node->rq_prev = NULL;
1592 if (head->rq_next == NULL)
1593 head->rq_prev = node;
1594 else
1595 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001596 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001597 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001598 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001599 {
1600 /* append node to the tail of the queue */
1601 node->rq_next = NULL;
1602 node->rq_prev = head->rq_prev;
1603 if (head->rq_prev == NULL)
1604 head->rq_next = node;
1605 else
1606 head->rq_prev->rq_next = node;
1607 head->rq_prev = node;
1608 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001610 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001612 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001613 fprintf(log_fd, "'");
1614 if (fwrite(buf, len, 1, log_fd) != 1)
1615 return FAIL;
1616 fprintf(log_fd, "'\n");
1617 }
1618 return OK;
1619}
1620
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001621 static int
1622channel_fill(js_read_T *reader)
1623{
1624 channel_T *channel = (channel_T *)reader->js_cookie;
1625 int part = reader->js_cookie_arg;
1626 char_u *next = channel_get(channel, part);
1627 int unused;
1628 int len;
1629 char_u *p;
1630
1631 if (next == NULL)
1632 return FALSE;
1633
1634 unused = reader->js_end - reader->js_buf - reader->js_used;
1635 if (unused > 0)
1636 {
1637 /* Prepend unused text. */
1638 len = (int)STRLEN(next);
1639 p = alloc(unused + len + 1);
1640 if (p == NULL)
1641 {
1642 vim_free(next);
1643 return FALSE;
1644 }
1645 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1646 mch_memmove(p + unused, next, len + 1);
1647 vim_free(next);
1648 next = p;
1649 }
1650
1651 vim_free(reader->js_buf);
1652 reader->js_buf = next;
1653 reader->js_used = 0;
1654 return TRUE;
1655}
1656
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001657/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001658 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001659 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001660 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001661 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001662 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001663channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001664{
1665 js_read_T reader;
1666 typval_T listtv;
1667 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001668 chanpart_T *chanpart = &channel->ch_part[part];
1669 jsonq_T *head = &chanpart->ch_json_head;
1670 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001671 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001672
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001673 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001674 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001676 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001678 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001679 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001680 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001681
1682 /* When a message is incomplete we wait for a short while for more to
1683 * arrive. After the delay drop the input, otherwise a truncated string
1684 * or list will make us hang. */
1685 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001686 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001687 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001688 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001689 /* Only accept the response when it is a list with at least two
1690 * items. */
1691 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001692 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001693 if (listtv.v_type != VAR_LIST)
1694 ch_error(channel, "Did not receive a list, discarding");
1695 else
1696 ch_errorn(channel, "Expected list with two items, got %d",
1697 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001698 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001699 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700 else
1701 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001702 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1703 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001704 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001705 else
1706 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001707 item->jq_value = alloc_tv();
1708 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001709 {
1710 vim_free(item);
1711 clear_tv(&listtv);
1712 }
1713 else
1714 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 *item->jq_value = listtv;
1716 item->jq_prev = head->jq_prev;
1717 head->jq_prev = item;
1718 item->jq_next = NULL;
1719 if (item->jq_prev == NULL)
1720 head->jq_next = item;
1721 else
1722 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001723 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001724 }
1725 }
1726 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001727
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001728 if (status == OK)
1729 chanpart->ch_waiting = FALSE;
1730 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001731 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001732 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001733 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001734 /* First time encountering incomplete message, set a deadline of
1735 * 100 msec. */
1736 ch_log(channel, "Incomplete message - wait for more");
1737 reader.js_used = 0;
1738 chanpart->ch_waiting = TRUE;
1739#ifdef WIN32
1740 chanpart->ch_deadline = GetTickCount() + 100L;
1741#else
1742 gettimeofday(&chanpart->ch_deadline, NULL);
1743 chanpart->ch_deadline.tv_usec += 100 * 1000;
1744 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1745 {
1746 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1747 ++chanpart->ch_deadline.tv_sec;
1748 }
1749#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001750 }
1751 else
1752 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001753 int timeout;
1754#ifdef WIN32
1755 timeout = GetTickCount() > chanpart->ch_deadline;
1756#else
1757 {
1758 struct timeval now_tv;
1759
1760 gettimeofday(&now_tv, NULL);
1761 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1762 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1763 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1764 }
1765#endif
1766 if (timeout)
1767 {
1768 status = FAIL;
1769 chanpart->ch_waiting = FALSE;
1770 }
1771 else
1772 {
1773 reader.js_used = 0;
1774 ch_log(channel, "still waiting on incomplete message");
1775 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001776 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001777 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001778
1779 if (status == FAIL)
1780 {
1781 ch_error(channel, "Decoding failed - discarding input");
1782 ret = FALSE;
1783 chanpart->ch_waiting = FALSE;
1784 }
1785 else if (reader.js_buf[reader.js_used] != NUL)
1786 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001787 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001788 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001789 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1790 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001791 ret = status == MAYBE ? FALSE: TRUE;
1792 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001793 else
1794 ret = FALSE;
1795
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001796 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001797 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001798}
1799
1800/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001801 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001802 */
1803 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001804remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001805{
Bram Moolenaar77073442016-02-13 23:23:53 +01001806 if (node->cq_prev == NULL)
1807 head->cq_next = node->cq_next;
1808 else
1809 node->cq_prev->cq_next = node->cq_next;
1810 if (node->cq_next == NULL)
1811 head->cq_prev = node->cq_prev;
1812 else
1813 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001814}
1815
1816/*
1817 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01001818 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001819 */
1820 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001821remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001822{
Bram Moolenaar77073442016-02-13 23:23:53 +01001823 if (node->jq_prev == NULL)
1824 head->jq_next = node->jq_next;
1825 else
1826 node->jq_prev->jq_next = node->jq_next;
1827 if (node->jq_next == NULL)
1828 head->jq_prev = node->jq_prev;
1829 else
1830 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001831 vim_free(node);
1832}
1833
1834/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001835 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01001837 * When "id" is zero or negative jut get the first message. But not the one
1838 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001839 * Return OK when found and return the value in "rettv".
1840 * Return FAIL otherwise.
1841 */
1842 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001843channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001844{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001845 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001846 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001847
Bram Moolenaar77073442016-02-13 23:23:53 +01001848 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001849 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001850 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001851 typval_T *tv = &l->lv_first->li_tv;
1852
1853 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01001854 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001855 || tv->vval.v_number == 0
1856 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001857 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001858 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001859 if (tv->v_type == VAR_NUMBER)
1860 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01001861 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862 return OK;
1863 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001864 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001865 }
1866 return FAIL;
1867}
1868
Bram Moolenaarece61b02016-02-20 21:39:05 +01001869#define CH_JSON_MAX_ARGS 4
1870
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001871/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001872 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01001873 * "argv[0]" is the command string.
1874 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001875 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001876 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01001877channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001878{
Bram Moolenaarece61b02016-02-20 21:39:05 +01001879 char_u *cmd = argv[0].vval.v_string;
1880 char_u *arg;
1881 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001882
Bram Moolenaarece61b02016-02-20 21:39:05 +01001883 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001884 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001885 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001886 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001887 EMSG("E903: received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001888 return;
1889 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001890 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001891 if (arg == NULL)
1892 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001893
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001894 if (STRCMP(cmd, "ex") == 0)
1895 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001896 int save_called_emsg = called_emsg;
1897
1898 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001899 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001900 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001901 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01001902 --emsg_silent;
1903 if (called_emsg)
1904 ch_logs(channel, "Ex command error: '%s'",
1905 (char *)get_vim_var_str(VV_ERRMSG));
1906 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001907 }
1908 else if (STRCMP(cmd, "normal") == 0)
1909 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001910 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001911
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001912 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001913 ea.arg = arg;
1914 ea.addr_count = 0;
1915 ea.forceit = TRUE; /* no mapping */
1916 ex_normal(&ea);
1917 }
1918 else if (STRCMP(cmd, "redraw") == 0)
1919 {
1920 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001921
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001922 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01001923 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001924 ex_redraw(&ea);
1925 showruler(FALSE);
1926 setcursor();
1927 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001928#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001929 if (gui.in_use)
1930 {
1931 gui_update_cursor(FALSE, FALSE);
1932 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001933 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001934#endif
1935 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001936 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001937 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001938 int is_call = cmd[0] == 'c';
1939 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001940
Bram Moolenaarece61b02016-02-20 21:39:05 +01001941 if (argv[id_idx].v_type != VAR_UNKNOWN
1942 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001943 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001944 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001945 if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001946 EMSG("E904: last argument for expr/call must be a number");
1947 }
1948 else if (is_call && argv[2].v_type != VAR_LIST)
1949 {
1950 ch_error(channel, "third argument for call must be a list");
1951 if (p_verbose > 2)
1952 EMSG("E904: third argument for call must be a list");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001953 }
1954 else
1955 {
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001956 typval_T *tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001957 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001958 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01001959 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001960
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01001961 /* Don't pollute the display with errors. */
1962 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001963 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001964 {
1965 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01001966 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001967 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001968 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001969 {
1970 ch_logs(channel, "Calling '%s'", (char *)arg);
1971 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
1972 tv = &res_tv;
1973 else
1974 tv = NULL;
1975 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01001976
1977 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001978 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001979 int id = argv[id_idx].vval.v_number;
1980
Bram Moolenaar55fab432016-02-07 16:53:13 +01001981 if (tv != NULL)
Bram Moolenaarece61b02016-02-20 21:39:05 +01001982 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001983 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001984 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01001985 /* If evaluation failed or the result can't be encoded
1986 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01001987 vim_free(json);
1988 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001989 err_tv.v_type = VAR_STRING;
1990 err_tv.vval.v_string = (char_u *)"ERROR";
1991 tv = &err_tv;
Bram Moolenaarece61b02016-02-20 21:39:05 +01001992 json = json_encode_nr_expr(id, tv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001993 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01001994 if (json != NULL)
1995 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01001996 channel_send(channel,
1997 part == PART_SOCK ? PART_SOCK : PART_IN,
1998 json, (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01001999 vim_free(json);
2000 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002001 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002002 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002003 if (tv == &res_tv)
2004 clear_tv(tv);
2005 else if (tv != &err_tv)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002006 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002007 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002008 }
2009 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002010 {
2011 ch_errors(channel, "Receved unknown command: %s", (char *)cmd);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002012 EMSG2("E905: received unknown command: %s", cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002013 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002014}
2015
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002016/*
2017 * Invoke the callback at "cbhead".
2018 * Does not redraw but sets channel_need_redraw.
2019 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002020 static void
2021invoke_one_time_callback(
2022 channel_T *channel,
2023 cbq_T *cbhead,
2024 cbq_T *item,
2025 typval_T *argv)
2026{
2027 ch_logs(channel, "Invoking one-time callback %s",
2028 (char *)item->cq_callback);
2029 /* Remove the item from the list first, if the callback
2030 * invokes ch_close() the list will be cleared. */
2031 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002032 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002033 vim_free(item->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002034 partial_unref(item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002035 vim_free(item);
2036}
2037
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002038 static void
2039append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel)
2040{
2041 buf_T *save_curbuf = curbuf;
2042 linenr_T lnum = buffer->b_ml.ml_line_count;
2043 int save_write_to = buffer->b_write_to_channel;
2044
2045 /* If the buffer is also used as input insert above the last
2046 * line. Don't write these lines. */
2047 if (save_write_to)
2048 {
2049 --lnum;
2050 buffer->b_write_to_channel = FALSE;
2051 }
2052
2053 /* Append to the buffer */
2054 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2055
2056 curbuf = buffer;
2057 u_sync(TRUE);
2058 /* ignore undo failure, undo is not very useful here */
2059 ignored = u_save(lnum, lnum + 1);
2060
2061 ml_append(lnum, msg, 0, FALSE);
2062 appended_lines_mark(lnum, 1L);
2063 curbuf = save_curbuf;
2064
2065 if (buffer->b_nwindows > 0)
2066 {
2067 win_T *wp;
2068 win_T *save_curwin;
2069
2070 FOR_ALL_WINDOWS(wp)
2071 {
2072 if (wp->w_buffer == buffer
2073 && (save_write_to
2074 ? wp->w_cursor.lnum == lnum + 1
2075 : (wp->w_cursor.lnum == lnum
2076 && wp->w_cursor.col == 0)))
2077 {
2078 ++wp->w_cursor.lnum;
2079 save_curwin = curwin;
2080 curwin = wp;
2081 curbuf = curwin->w_buffer;
2082 scroll_cursor_bot(0, FALSE);
2083 curwin = save_curwin;
2084 curbuf = curwin->w_buffer;
2085 }
2086 }
2087 redraw_buf_later(buffer, VALID);
2088 channel_need_redraw = TRUE;
2089 }
2090
2091 if (save_write_to)
2092 {
2093 channel_T *ch;
2094
2095 /* Find channels reading from this buffer and adjust their
2096 * next-to-read line number. */
2097 buffer->b_write_to_channel = TRUE;
2098 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2099 {
2100 chanpart_T *in_part = &ch->ch_part[PART_IN];
2101
2102 if (in_part->ch_buffer == buffer)
2103 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2104 }
2105 }
2106}
2107
Bram Moolenaar437905c2016-04-26 19:01:05 +02002108 static void
2109drop_messages(channel_T *channel, int part)
2110{
2111 char_u *msg;
2112
2113 while ((msg = channel_get(channel, part)) != NULL)
2114 {
2115 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2116 vim_free(msg);
2117 }
2118}
2119
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002120/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002121 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002122 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002123 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002124 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002125 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002126may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002127{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128 char_u *msg = NULL;
2129 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002130 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002131 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002132 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002133 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002134 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002135 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002136 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002137 buf_T *buffer = NULL;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002138
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002139 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002140 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002141 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002142
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002143 /* Use a message-specific callback, part callback or channel callback */
2144 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2145 if (cbitem->cq_seq_nr == 0)
2146 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002147 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002148 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002149 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002150 partial = cbitem->cq_partial;
2151 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002152 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002153 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002154 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002155 partial = channel->ch_part[part].ch_partial;
2156 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002157 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002158 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002159 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002160 partial = channel->ch_partial;
2161 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002162
Bram Moolenaar187db502016-02-27 14:44:26 +01002163 buffer = channel->ch_part[part].ch_buffer;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002164 if (buffer != NULL && !buf_valid(buffer))
2165 {
2166 /* buffer was wiped out */
2167 channel->ch_part[part].ch_buffer = NULL;
2168 buffer = NULL;
2169 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002170
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002171 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002172 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002173 listitem_T *item;
2174 int argc = 0;
2175
Bram Moolenaard7ece102016-02-02 23:23:02 +01002176 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002177 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002178 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002179 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002180 channel_parse_json(channel, part);
2181 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002182 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002183 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002184
Bram Moolenaarece61b02016-02-20 21:39:05 +01002185 for (item = listtv->vval.v_list->lv_first;
2186 item != NULL && argc < CH_JSON_MAX_ARGS;
2187 item = item->li_next)
2188 argv[argc++] = item->li_tv;
2189 while (argc < CH_JSON_MAX_ARGS)
2190 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002191
Bram Moolenaarece61b02016-02-20 21:39:05 +01002192 if (argv[0].v_type == VAR_STRING)
2193 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002194 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002195 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002196 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002197 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002198 }
2199
Bram Moolenaarece61b02016-02-20 21:39:05 +01002200 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002201 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002202 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002203 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002204 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002205 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002206 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002207 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002208 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002209 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002210 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002211 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002212 return FALSE;
2213 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002214 else
2215 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002216 /* If there is no callback or buffer drop the message. */
2217 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002218 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002219 /* If there is a close callback it may use ch_read() to get the
2220 * messages. */
2221 if (channel->ch_close_cb == NULL)
2222 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002223 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002224 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002225
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002226 if (ch_mode == MODE_NL)
2227 {
2228 char_u *nl;
2229 char_u *buf;
2230
2231 /* See if we have a message ending in NL in the first buffer. If
2232 * not try to concatenate the first and the second buffer. */
2233 while (TRUE)
2234 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002235 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002236 nl = vim_strchr(buf, NL);
2237 if (nl != NULL)
2238 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002239 if (channel_collapse(channel, part) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002240 return FALSE; /* incomplete message */
2241 }
2242 if (nl[1] == NUL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002243 {
2244 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002245 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002246 *nl = NUL;
2247 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002248 else
2249 {
2250 /* Copy the message into allocated memory and remove it from
2251 * the buffer. */
2252 msg = vim_strnsave(buf, (int)(nl - buf));
2253 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2254 }
2255 }
2256 else
2257 /* For a raw channel we don't know where the message ends, just
2258 * get everything we have. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002259 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002260
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002261 if (msg == NULL)
2262 return FALSE; /* out of memory (and avoids Coverity warning) */
2263
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002264 argv[1].v_type = VAR_STRING;
2265 argv[1].vval.v_string = msg;
2266 }
2267
Bram Moolenaara07fec92016-02-05 21:04:08 +01002268 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002269 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002270 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002271
2272 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002273 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002274 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002275 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002276 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002277 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002278 break;
2279 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002280 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002281 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002282 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002283 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002284 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002285 if (buffer != NULL)
2286 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002287 if (msg == NULL)
2288 /* JSON or JS mode: re-encode the message. */
2289 msg = json_encode(listtv, ch_mode);
2290 if (msg != NULL)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002291 append_to_buffer(buffer, msg, channel);
Bram Moolenaar187db502016-02-27 14:44:26 +01002292 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002293
Bram Moolenaar187db502016-02-27 14:44:26 +01002294 if (callback != NULL)
2295 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002296 if (cbitem != NULL)
2297 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2298 else
2299 {
2300 /* invoke the channel callback */
2301 ch_logs(channel, "Invoking channel callback %s",
2302 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002303 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002304 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002305 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002306 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002307 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002308 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002309
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002310 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002311 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002312 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002313
2314 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002315}
2316
2317/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002318 * Return TRUE when channel "channel" is open for writing to.
2319 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002320 */
2321 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002322channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002323{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002324 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002325 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002326}
2327
2328/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002329 * Return TRUE when channel "channel" is open for reading or writing.
2330 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002331 */
2332 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002333channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002334{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002335 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002336 || channel->CH_IN_FD != INVALID_FD
2337 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002338 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002339}
2340
2341/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002342 * Return TRUE if "channel" has JSON or other typeahead.
2343 */
2344 static int
2345channel_has_readahead(channel_T *channel, int part)
2346{
2347 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2348
2349 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2350 {
2351 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2352 jsonq_T *item = head->jq_next;
2353
2354 return item != NULL;
2355 }
2356 return channel_peek(channel, part) != NULL;
2357}
2358
2359/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002360 * Return a string indicating the status of the channel.
2361 */
2362 char *
2363channel_status(channel_T *channel)
2364{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002365 int part;
2366 int has_readahead = FALSE;
2367
Bram Moolenaar77073442016-02-13 23:23:53 +01002368 if (channel == NULL)
2369 return "fail";
2370 if (channel_is_open(channel))
2371 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002372 for (part = PART_SOCK; part <= PART_ERR; ++part)
2373 if (channel_has_readahead(channel, part))
2374 {
2375 has_readahead = TRUE;
2376 break;
2377 }
2378
2379 if (has_readahead)
2380 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002381 return "closed";
2382}
2383
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002384 static void
2385channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2386{
2387 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002388 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002389 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002390 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002391
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002392 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002393 STRCAT(namebuf, "_");
2394 tail = STRLEN(namebuf);
2395
2396 STRCPY(namebuf + tail, "status");
2397 dict_add_nr_str(dict, namebuf, 0,
2398 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2399
2400 STRCPY(namebuf + tail, "mode");
2401 switch (chanpart->ch_mode)
2402 {
2403 case MODE_NL: s = "NL"; break;
2404 case MODE_RAW: s = "RAW"; break;
2405 case MODE_JSON: s = "JSON"; break;
2406 case MODE_JS: s = "JS"; break;
2407 }
2408 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2409
2410 STRCPY(namebuf + tail, "io");
2411 if (part == PART_SOCK)
2412 s = "socket";
2413 else switch (chanpart->ch_io)
2414 {
2415 case JIO_NULL: s = "null"; break;
2416 case JIO_PIPE: s = "pipe"; break;
2417 case JIO_FILE: s = "file"; break;
2418 case JIO_BUFFER: s = "buffer"; break;
2419 case JIO_OUT: s = "out"; break;
2420 }
2421 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2422
2423 STRCPY(namebuf + tail, "timeout");
2424 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2425}
2426
2427 void
2428channel_info(channel_T *channel, dict_T *dict)
2429{
2430 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2431 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2432
2433 if (channel->ch_hostname != NULL)
2434 {
2435 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2436 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2437 channel_part_info(channel, dict, "sock", PART_SOCK);
2438 }
2439 else
2440 {
2441 channel_part_info(channel, dict, "out", PART_OUT);
2442 channel_part_info(channel, dict, "err", PART_ERR);
2443 channel_part_info(channel, dict, "in", PART_IN);
2444 }
2445}
2446
Bram Moolenaar77073442016-02-13 23:23:53 +01002447/*
2448 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002449 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002450 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002451 */
2452 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002453channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002454{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002455 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002456
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002457#ifdef FEAT_GUI
2458 channel_gui_unregister(channel);
2459#endif
2460
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002461 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002462 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002463 sock_close(channel->CH_SOCK_FD);
2464 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002465 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002466 may_close_part(&channel->CH_IN_FD);
2467 may_close_part(&channel->CH_OUT_FD);
2468 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002469
Bram Moolenaar8b374212016-02-24 20:43:06 +01002470 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002471 {
2472 typval_T argv[1];
2473 typval_T rettv;
2474 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002475 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002476
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002477 /* Invoke callbacks before the close callback, since it's weird to
2478 * first invoke the close callback. Increment the refcount to avoid
2479 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002480 ++channel->ch_refcount;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002481 for (part = PART_SOCK; part <= PART_ERR; ++part)
2482 while (may_invoke_callback(channel, part))
2483 ;
2484
2485 /* Invoke the close callback, if still set. */
2486 if (channel->ch_close_cb != NULL)
2487 {
2488 ch_logs(channel, "Invoking close callback %s",
2489 (char *)channel->ch_close_cb);
2490 argv[0].v_type = VAR_CHANNEL;
2491 argv[0].vval.v_channel = channel;
2492 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002493 &rettv, 1, argv, 0L, 0L, &dummy, TRUE,
2494 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002495 clear_tv(&rettv);
2496 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002497 --channel->ch_refcount;
2498
2499 /* the callback is only called once */
2500 vim_free(channel->ch_close_cb);
2501 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002502 partial_unref(channel->ch_close_partial);
2503 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002504
2505 /* any remaining messages are useless now */
2506 for (part = PART_SOCK; part <= PART_ERR; ++part)
2507 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002508 }
2509
2510 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002511}
2512
Bram Moolenaard04a0202016-01-26 23:30:18 +01002513/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002514 * Return the first buffer from "channel"/"part" without removing it.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002515 * Returns NULL if there is nothing.
2516 */
2517 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002518channel_peek(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002519{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002520 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002521
Bram Moolenaar77073442016-02-13 23:23:53 +01002522 if (head->rq_next == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002523 return NULL;
Bram Moolenaar77073442016-02-13 23:23:53 +01002524 return head->rq_next->rq_buffer;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002525}
2526
2527/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002528 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002529 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002530 static void
2531channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002532{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002533 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2534 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002535
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002536 while (channel_peek(channel, part) != NULL)
2537 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002538
2539 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002540 {
2541 cbq_T *node = cb_head->cq_next;
2542
2543 remove_cb_node(cb_head, node);
2544 vim_free(node->cq_callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002545 partial_unref(node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002546 vim_free(node);
2547 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002548
2549 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002550 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002551 free_tv(json_head->jq_next->jq_value);
2552 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002553 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002554
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002555 vim_free(channel->ch_part[part].ch_callback);
2556 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002557 partial_unref(channel->ch_part[part].ch_partial);
2558 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002559}
2560
2561/*
2562 * Clear all the read buffers on "channel".
2563 */
2564 void
2565channel_clear(channel_T *channel)
2566{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002567 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002568 vim_free(channel->ch_hostname);
2569 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002570 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002571 channel_clear_one(channel, PART_OUT);
2572 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002573 /* there is no callback or queue for PART_IN */
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002574 vim_free(channel->ch_callback);
2575 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002576 partial_unref(channel->ch_partial);
2577 channel->ch_partial = NULL;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002578 vim_free(channel->ch_close_cb);
2579 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002580 partial_unref(channel->ch_close_partial);
2581 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002582}
2583
Bram Moolenaar77073442016-02-13 23:23:53 +01002584#if defined(EXITFREE) || defined(PROTO)
2585 void
2586channel_free_all(void)
2587{
2588 channel_T *channel;
2589
Bram Moolenaard6051b52016-02-28 15:49:03 +01002590 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002591 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2592 channel_clear(channel);
2593}
2594#endif
2595
2596
Bram Moolenaard04a0202016-01-26 23:30:18 +01002597/* Sent when the channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002598#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002599
2600/* Buffer size for reading incoming messages. */
2601#define MAXMSGSIZE 4096
2602
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002603#if defined(HAVE_SELECT)
2604/*
2605 * Add write fds where we are waiting for writing to be possible.
2606 */
2607 static int
2608channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2609{
2610 int maxfd = maxfd_arg;
2611 channel_T *ch;
2612
2613 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2614 {
2615 chanpart_T *in_part = &ch->ch_part[PART_IN];
2616
2617 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2618 {
2619 FD_SET((int)in_part->ch_fd, wfds);
2620 if ((int)in_part->ch_fd >= maxfd)
2621 maxfd = (int)in_part->ch_fd + 1;
2622 }
2623 }
2624 return maxfd;
2625}
2626#else
2627/*
2628 * Add write fds where we are waiting for writing to be possible.
2629 */
2630 static int
2631channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2632{
2633 int nfd = nfd_in;
2634 channel_T *ch;
2635
2636 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2637 {
2638 chanpart_T *in_part = &ch->ch_part[PART_IN];
2639
2640 if (in_part->ch_fd != INVALID_FD && in_part->ch_buffer != NULL)
2641 {
2642 in_part->ch_poll_idx = nfd;
2643 fds[nfd].fd = in_part->ch_fd;
2644 fds[nfd].events = POLLOUT;
2645 ++nfd;
2646 }
2647 else
2648 in_part->ch_poll_idx = -1;
2649 }
2650 return nfd;
2651}
2652#endif
2653
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002654typedef enum {
2655 CW_READY,
2656 CW_NOT_READY,
2657 CW_ERROR
2658} channel_wait_result;
2659
Bram Moolenaard04a0202016-01-26 23:30:18 +01002660/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002661 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002662 * Return CW_READY when there is something to read.
2663 * Return CW_NOT_READY when there is nothing to read.
2664 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002665 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002666 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002667channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002668{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002669 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002670 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002671
Bram Moolenaard8070362016-02-15 21:56:54 +01002672# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002673 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002674 {
2675 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002676 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002677 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002678 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002679
2680 /* reading from a pipe, not a socket */
2681 while (TRUE)
2682 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002683 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2684
2685 if (r && nread > 0)
2686 return CW_READY;
2687 if (r == 0)
2688 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002689
2690 /* perhaps write some buffer lines */
2691 channel_write_any_lines();
2692
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002693 sleep_time = deadline - GetTickCount();
2694 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002695 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002696 /* Wait for a little while. Very short at first, up to 10 msec
2697 * after looping a few times. */
2698 if (sleep_time > delay)
2699 sleep_time = delay;
2700 Sleep(sleep_time);
2701 delay = delay * 2;
2702 if (delay > 10)
2703 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002704 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002705 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002706 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002707#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002708 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002709#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002710 struct timeval tval;
2711 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002712 fd_set wfds;
2713 int ret;
2714 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002715
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002716 tval.tv_sec = timeout / 1000;
2717 tval.tv_usec = (timeout % 1000) * 1000;
2718 for (;;)
2719 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002720 FD_ZERO(&rfds);
2721 FD_SET((int)fd, &rfds);
2722
2723 /* Write lines to a pipe when a pipe can be written to. Need to
2724 * set this every time, some buffers may be done. */
2725 maxfd = (int)fd + 1;
2726 FD_ZERO(&wfds);
2727 maxfd = channel_fill_wfds(maxfd, &wfds);
2728
2729 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002730# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002731 SOCK_ERRNO;
2732 if (ret == -1 && errno == EINTR)
2733 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002734# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002735 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002736 {
2737 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002738 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002739 channel_write_any_lines();
2740 continue;
2741 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002742 break;
2743 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002744#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002745 for (;;)
2746 {
2747 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2748 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002749
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002750 fds[0].fd = fd;
2751 fds[0].events = POLLIN;
2752 nfd = channel_fill_poll_write(nfd, fds);
2753 if (poll(fds, nfd, timeout) > 0)
2754 {
2755 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002756 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002757 channel_write_any_lines();
2758 continue;
2759 }
2760 break;
2761 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002762#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002763 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002764 return CW_NOT_READY;
2765}
2766
2767 static void
2768channel_close_on_error(channel_T *channel, int part, char *func)
2769{
2770 /* Do not call emsg(), most likely the other end just exited. */
2771 ch_errors(channel, "%s(): Cannot read from channel", func);
2772
2773 /* Queue a "DETACH" netbeans message in the command queue in order to
2774 * terminate the netbeans session later. Do not end the session here
2775 * directly as we may be running in the context of a call to
2776 * netbeans_parse_messages():
2777 * netbeans_parse_messages
2778 * -> autocmd triggered while processing the netbeans cmd
2779 * -> ui_breakcheck
2780 * -> gui event loop or select loop
2781 * -> channel_read()
2782 * Don't send "DETACH" for a JS or JSON channel.
2783 */
2784 if (channel->ch_part[part].ch_mode == MODE_RAW
2785 || channel->ch_part[part].ch_mode == MODE_NL)
2786 channel_save(channel, part, (char_u *)DETACH_MSG_RAW,
2787 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
2788
2789 /* When reading from stdout is not possible, assume the other side has
2790 * died. */
2791 channel_close(channel, TRUE);
2792 if (channel->ch_nb_close_cb != NULL)
2793 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002794}
2795
2796/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002797 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002798 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002799 * The data is put in the read queue.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002800 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002801 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002802channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002803{
2804 static char_u *buf = NULL;
2805 int len = 0;
2806 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01002807 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002808 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002809
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002810 fd = channel->ch_part[part].ch_fd;
2811 if (fd == INVALID_FD)
2812 {
2813 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002814 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002815 }
2816 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002817
2818 /* Allocate a buffer to read into. */
2819 if (buf == NULL)
2820 {
2821 buf = alloc(MAXMSGSIZE);
2822 if (buf == NULL)
2823 return; /* out of memory! */
2824 }
2825
2826 /* Keep on reading for as long as there is something to read.
2827 * Use select() or poll() to avoid blocking on a message that is exactly
2828 * MAXMSGSIZE long. */
2829 for (;;)
2830 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002831 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002832 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002833 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002834 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002835 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002836 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002837 if (len <= 0)
2838 break; /* error or nothing more to read */
2839
2840 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002841 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002842 readlen += len;
2843 if (len < MAXMSGSIZE)
2844 break; /* did read everything that's available */
2845 }
2846
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01002847 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01002848 if (readlen <= 0)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002849 channel_close_on_error(channel, part, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002850
2851#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002852 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01002853 if (CH_HAS_GUI && gtk_main_level() > 0)
2854 gtk_main_quit();
2855#endif
2856}
2857
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002858/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002859 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002860 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002861 * Returns what was read in allocated memory.
2862 * Returns NULL in case of error or timeout.
2863 */
2864 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002865channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002866{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002867 char_u *buf;
2868 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002869 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002870 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002871 char_u *nl;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002872
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002873 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002874 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002875
2876 while (TRUE)
2877 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002878 buf = channel_peek(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002879 if (buf != NULL && (mode == MODE_RAW
2880 || (mode == MODE_NL && vim_strchr(buf, NL) != NULL)))
2881 break;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002882 if (buf != NULL && channel_collapse(channel, part) == OK)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002883 continue;
2884
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002885 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002886 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002887 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002888 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002889 {
2890 ch_log(channel, "Timed out");
2891 return NULL;
2892 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002893 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002894 }
2895
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002896 if (mode == MODE_RAW)
2897 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002898 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002899 }
2900 else
2901 {
2902 nl = vim_strchr(buf, NL);
2903 if (nl[1] == NUL)
2904 {
2905 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002906 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002907 *nl = NUL;
2908 }
2909 else
2910 {
2911 /* Copy the message into allocated memory and remove it from the
2912 * buffer. */
2913 msg = vim_strnsave(buf, (int)(nl - buf));
2914 mch_memmove(buf, nl + 1, STRLEN(nl + 1) + 1);
2915 }
2916 }
2917 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002918 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002919 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002920}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002921
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002922/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002923 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002924 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002925 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01002926 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002927 */
2928 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002929channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01002930 channel_T *channel,
2931 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002932 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01002933 int id,
2934 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002935{
Bram Moolenaare56bf152016-02-08 23:23:42 +01002936 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01002937 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002938 int timeout;
2939 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01002940
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002941 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01002942 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002943 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002944 for (;;)
2945 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002946 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002947
2948 /* search for messsage "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002949 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002950 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002951 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002952 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01002953 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002954
Bram Moolenaard7ece102016-02-02 23:23:02 +01002955 if (!more)
2956 {
2957 /* Handle any other messages in the queue. If done some more
2958 * messages may have arrived. */
2959 if (channel_parse_messages())
2960 continue;
2961
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002962 /* Wait for up to the timeout. If there was an incomplete message
2963 * use the deadline for that. */
2964 timeout = timeout_arg;
2965 if (chanpart->ch_waiting)
2966 {
2967#ifdef WIN32
2968 timeout = chanpart->ch_deadline - GetTickCount() + 1;
2969#else
2970 {
2971 struct timeval now_tv;
2972
2973 gettimeofday(&now_tv, NULL);
2974 timeout = (chanpart->ch_deadline.tv_sec
2975 - now_tv.tv_sec) * 1000
2976 + (chanpart->ch_deadline.tv_usec
2977 - now_tv.tv_usec) / 1000
2978 + 1;
2979 }
2980#endif
2981 if (timeout < 0)
2982 {
2983 /* Something went wrong, channel_parse_json() didn't
2984 * discard message. Cancel waiting. */
2985 chanpart->ch_waiting = FALSE;
2986 timeout = timeout_arg;
2987 }
2988 else if (timeout > timeout_arg)
2989 timeout = timeout_arg;
2990 }
2991 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002992 if (fd == INVALID_FD
2993 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002994 {
2995 if (timeout == timeout_arg)
2996 {
2997 if (fd != INVALID_FD)
2998 ch_log(channel, "Timed out");
2999 break;
3000 }
3001 }
3002 else
3003 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003004 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003005 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003006 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003007 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003008}
3009
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003010/*
3011 * Common for ch_read() and ch_readraw().
3012 */
3013 void
3014common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3015{
3016 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003017 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003018 jobopt_T opt;
3019 int mode;
3020 int timeout;
3021 int id = -1;
3022 typval_T *listtv = NULL;
3023
3024 /* return an empty string by default */
3025 rettv->v_type = VAR_STRING;
3026 rettv->vval.v_string = NULL;
3027
3028 clear_job_options(&opt);
3029 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3030 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003031 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003032
Bram Moolenaar437905c2016-04-26 19:01:05 +02003033 if (opt.jo_set & JO_PART)
3034 part = opt.jo_part;
3035 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003036 if (channel != NULL)
3037 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003038 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003039 part = channel_part_read(channel);
3040 mode = channel_get_mode(channel, part);
3041 timeout = channel_get_timeout(channel, part);
3042 if (opt.jo_set & JO_TIMEOUT)
3043 timeout = opt.jo_timeout;
3044
3045 if (raw || mode == MODE_RAW || mode == MODE_NL)
3046 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3047 else
3048 {
3049 if (opt.jo_set & JO_ID)
3050 id = opt.jo_id;
3051 channel_read_json_block(channel, part, timeout, id, &listtv);
3052 if (listtv != NULL)
3053 {
3054 *rettv = *listtv;
3055 vim_free(listtv);
3056 }
3057 else
3058 {
3059 rettv->v_type = VAR_SPECIAL;
3060 rettv->vval.v_number = VVAL_NONE;
3061 }
3062 }
3063 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003064
3065theend:
3066 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003067}
3068
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003069# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3070 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003071/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003072 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003073 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003074 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003075 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003076channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003077{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003078 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003079 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003080
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003081 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003082 for (channel = first_channel; channel != NULL;
3083 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003084 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003085 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003086 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003087 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003088 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003089 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003090 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003091 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003092 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003093}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003094# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003095
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003096# if defined(WIN32) || defined(PROTO)
3097/*
3098 * Check the channels for anything that is ready to be read.
3099 * The data is put in the read queue.
3100 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003101 void
3102channel_handle_events(void)
3103{
3104 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003105 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003106 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003107
3108 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3109 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003110 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003111 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003112 {
3113 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003114 if (fd != INVALID_FD)
3115 {
3116 int r = channel_wait(channel, fd, 0);
3117
3118 if (r == CW_READY)
3119 channel_read(channel, part, "channel_handle_events");
3120 else if (r == CW_ERROR)
3121 channel_close_on_error(channel, part,
3122 "channel_handle_events()");
3123 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003124 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003125 }
3126}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003127# endif
3128
Bram Moolenaard04a0202016-01-26 23:30:18 +01003129/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003130 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003131 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003132 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003133 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003134 int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003135channel_send(channel_T *channel, int part, char_u *buf, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003136{
Bram Moolenaard04a0202016-01-26 23:30:18 +01003137 int len = (int)STRLEN(buf);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003138 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003139 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003140
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003141 fd = channel->ch_part[part].ch_fd;
3142 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003143 {
3144 if (!channel->ch_error && fun != NULL)
3145 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003146 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003147 EMSG2("E630: %s(): write while not connected", fun);
3148 }
3149 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003150 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003151 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003152
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003153 if (log_fd != NULL)
3154 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003155 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003156 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003157 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003158 fprintf(log_fd, "'\n");
3159 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003160 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003161 }
3162
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003163 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003164 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003165 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003166 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003167 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003168 {
3169 if (!channel->ch_error && fun != NULL)
3170 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003171 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003172 EMSG2("E631: %s(): write failed", fun);
3173 }
3174 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003175 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003176 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003177
3178 channel->ch_error = FALSE;
3179 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003180}
3181
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003182/*
3183 * Common for "ch_sendexpr()" and "ch_sendraw()".
3184 * Returns the channel if the caller should read the response.
3185 * Sets "part_read" to the the read fd.
3186 * Otherwise returns NULL.
3187 */
3188 channel_T *
3189send_common(
3190 typval_T *argvars,
3191 char_u *text,
3192 int id,
3193 int eval,
3194 jobopt_T *opt,
3195 char *fun,
3196 int *part_read)
3197{
3198 channel_T *channel;
3199 int part_send;
3200
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003201 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003202 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003203 if (channel == NULL)
3204 return NULL;
3205 part_send = channel_part_send(channel);
3206 *part_read = channel_part_read(channel);
3207
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003208 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3209 return NULL;
3210
3211 /* Set the callback. An empty callback means no callback and not reading
3212 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3213 * allowed. */
3214 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3215 {
3216 if (eval)
3217 {
3218 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3219 return NULL;
3220 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003221 channel_set_req_callback(channel, part_send,
3222 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003223 }
3224
3225 if (channel_send(channel, part_send, text, fun) == OK
3226 && opt->jo_callback == NULL)
3227 return channel;
3228 return NULL;
3229}
3230
3231/*
3232 * common for "ch_evalexpr()" and "ch_sendexpr()"
3233 */
3234 void
3235ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3236{
3237 char_u *text;
3238 typval_T *listtv;
3239 channel_T *channel;
3240 int id;
3241 ch_mode_T ch_mode;
3242 int part_send;
3243 int part_read;
3244 jobopt_T opt;
3245 int timeout;
3246
3247 /* return an empty string by default */
3248 rettv->v_type = VAR_STRING;
3249 rettv->vval.v_string = NULL;
3250
Bram Moolenaar437905c2016-04-26 19:01:05 +02003251 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003252 if (channel == NULL)
3253 return;
3254 part_send = channel_part_send(channel);
3255
3256 ch_mode = channel_get_mode(channel, part_send);
3257 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3258 {
3259 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3260 return;
3261 }
3262
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003263 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003264 text = json_encode_nr_expr(id, &argvars[1],
3265 ch_mode == MODE_JS ? JSON_JS : 0);
3266 if (text == NULL)
3267 return;
3268
3269 channel = send_common(argvars, text, id, eval, &opt,
3270 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3271 vim_free(text);
3272 if (channel != NULL && eval)
3273 {
3274 if (opt.jo_set & JO_TIMEOUT)
3275 timeout = opt.jo_timeout;
3276 else
3277 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003278 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3279 == OK)
3280 {
3281 list_T *list = listtv->vval.v_list;
3282
3283 /* Move the item from the list and then change the type to
3284 * avoid the value being freed. */
3285 *rettv = list->lv_last->li_tv;
3286 list->lv_last->li_tv.v_type = VAR_NUMBER;
3287 free_tv(listtv);
3288 }
3289 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003290 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003291}
3292
3293/*
3294 * common for "ch_evalraw()" and "ch_sendraw()"
3295 */
3296 void
3297ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3298{
3299 char_u buf[NUMBUFLEN];
3300 char_u *text;
3301 channel_T *channel;
3302 int part_read;
3303 jobopt_T opt;
3304 int timeout;
3305
3306 /* return an empty string by default */
3307 rettv->v_type = VAR_STRING;
3308 rettv->vval.v_string = NULL;
3309
3310 text = get_tv_string_buf(&argvars[1], buf);
3311 channel = send_common(argvars, text, 0, eval, &opt,
3312 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3313 if (channel != NULL && eval)
3314 {
3315 if (opt.jo_set & JO_TIMEOUT)
3316 timeout = opt.jo_timeout;
3317 else
3318 timeout = channel_get_timeout(channel, part_read);
3319 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3320 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003321 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003322}
3323
Bram Moolenaard04a0202016-01-26 23:30:18 +01003324# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003325/*
3326 * Add open channels to the poll struct.
3327 * Return the adjusted struct index.
3328 * The type of "fds" is hidden to avoid problems with the function proto.
3329 */
3330 int
3331channel_poll_setup(int nfd_in, void *fds_in)
3332{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003333 int nfd = nfd_in;
3334 channel_T *channel;
3335 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003336 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003337
Bram Moolenaar77073442016-02-13 23:23:53 +01003338 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003339 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003340 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003341 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003342 chanpart_T *ch_part = &channel->ch_part[part];
3343
3344 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003345 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003346 ch_part->ch_poll_idx = nfd;
3347 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003348 fds[nfd].events = POLLIN;
3349 nfd++;
3350 }
3351 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003352 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003353 }
3354 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003355
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003356 nfd = channel_fill_poll_write(nfd, fds);
3357
Bram Moolenaare0874f82016-01-24 20:36:41 +01003358 return nfd;
3359}
3360
3361/*
3362 * The type of "fds" is hidden to avoid problems with the function proto.
3363 */
3364 int
3365channel_poll_check(int ret_in, void *fds_in)
3366{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003367 int ret = ret_in;
3368 channel_T *channel;
3369 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003370 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003371 int idx;
3372 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003373
Bram Moolenaar77073442016-02-13 23:23:53 +01003374 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003375 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003376 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003377 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003378 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003379
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003380 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003381 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003382 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003383 --ret;
3384 }
3385 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003386
3387 in_part = &channel->ch_part[PART_IN];
3388 idx = in_part->ch_poll_idx;
3389 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3390 {
3391 if (in_part->ch_buf_append)
3392 {
3393 if (in_part->ch_buffer != NULL)
3394 channel_write_new_lines(in_part->ch_buffer);
3395 }
3396 else
3397 channel_write_in(channel);
3398 --ret;
3399 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003400 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003401
3402 return ret;
3403}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003404# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003405
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003406# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003407/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003408 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003409 */
3410 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003411channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003412{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003413 int maxfd = maxfd_in;
3414 channel_T *channel;
3415 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003416 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003417 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003418
Bram Moolenaar77073442016-02-13 23:23:53 +01003419 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003420 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003421 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003422 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003423 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003424
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003425 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003426 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003427 FD_SET((int)fd, rfds);
3428 if (maxfd < (int)fd)
3429 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003430 }
3431 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003432 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003433
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003434 maxfd = channel_fill_wfds(maxfd, wfds);
3435
Bram Moolenaare0874f82016-01-24 20:36:41 +01003436 return maxfd;
3437}
3438
3439/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003440 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003441 */
3442 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003443channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003444{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003445 int ret = ret_in;
3446 channel_T *channel;
3447 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003448 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003449 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003450 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003451
Bram Moolenaar77073442016-02-13 23:23:53 +01003452 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003453 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003454 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003455 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003456 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003457
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003458 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003459 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003460 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003461 --ret;
3462 }
3463 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003464
3465 in_part = &channel->ch_part[PART_IN];
3466 if (ret > 0 && in_part->ch_fd != INVALID_FD
3467 && FD_ISSET(in_part->ch_fd, wfds))
3468 {
3469 if (in_part->ch_buf_append)
3470 {
3471 if (in_part->ch_buffer != NULL)
3472 channel_write_new_lines(in_part->ch_buffer);
3473 }
3474 else
3475 channel_write_in(channel);
3476 --ret;
3477 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003478 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003479
3480 return ret;
3481}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003482# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003483
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003484/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003485 * Execute queued up commands.
3486 * Invoked from the main loop when it's safe to execute received commands.
3487 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003488 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003489 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003490channel_parse_messages(void)
3491{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003492 channel_T *channel = first_channel;
3493 int ret = FALSE;
3494 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003495 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003496
Bram Moolenaard0b65022016-03-06 21:50:33 +01003497 /* Only do this message when another message was given, otherwise we get
3498 * lots of them. */
3499 if (did_log_msg)
3500 {
3501 ch_log(NULL, "looking for messages on channels");
3502 did_log_msg = FALSE;
3503 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003504 while (channel != NULL)
3505 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01003506 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003507 {
3508 /* channel is no longer useful, free it */
3509 channel_free(channel);
3510 channel = first_channel;
3511 part = PART_SOCK;
3512 continue;
3513 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003514 if (channel->ch_part[part].ch_fd != INVALID_FD
3515 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003516 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003517 /* Increase the refcount, in case the handler causes the channel
3518 * to be unreferenced or closed. */
3519 ++channel->ch_refcount;
3520 r = may_invoke_callback(channel, part);
3521 if (r == OK)
3522 ret = TRUE;
3523 if (channel_unref(channel) || r == OK)
3524 {
3525 /* channel was freed or something was done, start over */
3526 channel = first_channel;
3527 part = PART_SOCK;
3528 continue;
3529 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003530 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003531 if (part < PART_ERR)
3532 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003533 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003534 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003535 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003536 part = PART_SOCK;
3537 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003538 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003539
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003540 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003541 {
3542 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003543 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003544 }
3545
Bram Moolenaard7ece102016-02-02 23:23:02 +01003546 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003547}
3548
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003549/*
3550 * Mark references to lists used in channels.
3551 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003552 int
3553set_ref_in_channel(int copyID)
3554{
Bram Moolenaar77073442016-02-13 23:23:53 +01003555 int abort = FALSE;
3556 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003557 int part;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003558
Bram Moolenaar77073442016-02-13 23:23:53 +01003559 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003560 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003561 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003562 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003563 jsonq_T *head = &channel->ch_part[part].ch_json_head;
3564 jsonq_T *item = head->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003565
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003566 while (item != NULL)
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003567 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003568 list_T *l = item->jq_value->vval.v_list;
3569
3570 if (l->lv_copyID != copyID)
3571 {
3572 l->lv_copyID = copyID;
3573 abort = abort || set_ref_in_list(l, copyID, NULL);
3574 }
3575 item = item->jq_next;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003576 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003577 }
3578 }
3579 return abort;
3580}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003581
3582/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003583 * Return the "part" to write to for "channel".
3584 */
3585 int
3586channel_part_send(channel_T *channel)
3587{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003588 if (channel->CH_SOCK_FD == INVALID_FD)
3589 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003590 return PART_SOCK;
3591}
3592
3593/*
3594 * Return the default "part" to read from for "channel".
3595 */
3596 int
3597channel_part_read(channel_T *channel)
3598{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003599 if (channel->CH_SOCK_FD == INVALID_FD)
3600 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003601 return PART_SOCK;
3602}
3603
3604/*
3605 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003606 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003607 */
3608 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003609channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003610{
Bram Moolenaar77073442016-02-13 23:23:53 +01003611 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003612 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003613 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003614}
3615
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003616/*
3617 * Return the timeout of "channel"/"part"
3618 */
3619 int
3620channel_get_timeout(channel_T *channel, int part)
3621{
3622 return channel->ch_part[part].ch_timeout;
3623}
3624
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003625 static int
3626handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3627{
3628 char_u *val = get_tv_string(item);
3629
3630 opt->jo_set |= jo;
3631 if (STRCMP(val, "nl") == 0)
3632 *modep = MODE_NL;
3633 else if (STRCMP(val, "raw") == 0)
3634 *modep = MODE_RAW;
3635 else if (STRCMP(val, "js") == 0)
3636 *modep = MODE_JS;
3637 else if (STRCMP(val, "json") == 0)
3638 *modep = MODE_JSON;
3639 else
3640 {
3641 EMSG2(_(e_invarg2), val);
3642 return FAIL;
3643 }
3644 return OK;
3645}
3646
3647 static int
3648handle_io(typval_T *item, int part, jobopt_T *opt)
3649{
3650 char_u *val = get_tv_string(item);
3651
3652 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3653 if (STRCMP(val, "null") == 0)
3654 opt->jo_io[part] = JIO_NULL;
3655 else if (STRCMP(val, "pipe") == 0)
3656 opt->jo_io[part] = JIO_PIPE;
3657 else if (STRCMP(val, "file") == 0)
3658 opt->jo_io[part] = JIO_FILE;
3659 else if (STRCMP(val, "buffer") == 0)
3660 opt->jo_io[part] = JIO_BUFFER;
3661 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3662 opt->jo_io[part] = JIO_OUT;
3663 else
3664 {
3665 EMSG2(_(e_invarg2), val);
3666 return FAIL;
3667 }
3668 return OK;
3669}
3670
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003671/*
3672 * Clear a jobopt_T before using it.
3673 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003674 void
3675clear_job_options(jobopt_T *opt)
3676{
3677 vim_memset(opt, 0, sizeof(jobopt_T));
3678}
3679
3680/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003681 * Free any members of a jobopt_T.
3682 */
3683 void
3684free_job_options(jobopt_T *opt)
3685{
3686 if (opt->jo_partial != NULL)
3687 partial_unref(opt->jo_partial);
3688 if (opt->jo_out_partial != NULL)
3689 partial_unref(opt->jo_out_partial);
3690 if (opt->jo_err_partial != NULL)
3691 partial_unref(opt->jo_err_partial);
3692 if (opt->jo_close_partial != NULL)
3693 partial_unref(opt->jo_close_partial);
3694}
3695
3696/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003697 * Get the PART_ number from the first character of an option name.
3698 */
3699 static int
3700part_from_char(int c)
3701{
3702 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3703}
3704
3705/*
3706 * Get the option entries from the dict in "tv", parse them and put the result
3707 * in "opt".
3708 * Only accept options in "supported".
3709 * If an option value is invalid return FAIL.
3710 */
3711 int
3712get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3713{
3714 typval_T *item;
3715 char_u *val;
3716 dict_T *dict;
3717 int todo;
3718 hashitem_T *hi;
3719 int part;
3720
3721 opt->jo_set = 0;
3722 if (tv->v_type == VAR_UNKNOWN)
3723 return OK;
3724 if (tv->v_type != VAR_DICT)
3725 {
3726 EMSG(_(e_invarg));
3727 return FAIL;
3728 }
3729 dict = tv->vval.v_dict;
3730 if (dict == NULL)
3731 return OK;
3732
3733 todo = (int)dict->dv_hashtab.ht_used;
3734 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3735 if (!HASHITEM_EMPTY(hi))
3736 {
3737 item = &dict_lookup(hi)->di_tv;
3738
3739 if (STRCMP(hi->hi_key, "mode") == 0)
3740 {
3741 if (!(supported & JO_MODE))
3742 break;
3743 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
3744 return FAIL;
3745 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003746 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003747 {
3748 if (!(supported & JO_IN_MODE))
3749 break;
3750 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
3751 == FAIL)
3752 return FAIL;
3753 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003754 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003755 {
3756 if (!(supported & JO_OUT_MODE))
3757 break;
3758 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
3759 == FAIL)
3760 return FAIL;
3761 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003762 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003763 {
3764 if (!(supported & JO_ERR_MODE))
3765 break;
3766 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
3767 == FAIL)
3768 return FAIL;
3769 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003770 else if (STRCMP(hi->hi_key, "in_io") == 0
3771 || STRCMP(hi->hi_key, "out_io") == 0
3772 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003773 {
3774 if (!(supported & JO_OUT_IO))
3775 break;
3776 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
3777 return FAIL;
3778 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003779 else if (STRCMP(hi->hi_key, "in_name") == 0
3780 || STRCMP(hi->hi_key, "out_name") == 0
3781 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003782 {
3783 part = part_from_char(*hi->hi_key);
3784
3785 if (!(supported & JO_OUT_IO))
3786 break;
3787 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
3788 opt->jo_io_name[part] =
3789 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
3790 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003791 else if (STRCMP(hi->hi_key, "in_buf") == 0
3792 || STRCMP(hi->hi_key, "out_buf") == 0
3793 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003794 {
3795 part = part_from_char(*hi->hi_key);
3796
3797 if (!(supported & JO_OUT_IO))
3798 break;
3799 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
3800 opt->jo_io_buf[part] = get_tv_number(item);
3801 if (opt->jo_io_buf[part] <= 0)
3802 {
3803 EMSG2(_(e_invarg2), get_tv_string(item));
3804 return FAIL;
3805 }
3806 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
3807 {
3808 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
3809 return FAIL;
3810 }
3811 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003812 else if (STRCMP(hi->hi_key, "in_top") == 0
3813 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003814 {
3815 linenr_T *lp;
3816
3817 if (!(supported & JO_OUT_IO))
3818 break;
3819 if (hi->hi_key[3] == 't')
3820 {
3821 lp = &opt->jo_in_top;
3822 opt->jo_set |= JO_IN_TOP;
3823 }
3824 else
3825 {
3826 lp = &opt->jo_in_bot;
3827 opt->jo_set |= JO_IN_BOT;
3828 }
3829 *lp = get_tv_number(item);
3830 if (*lp < 0)
3831 {
3832 EMSG2(_(e_invarg2), get_tv_string(item));
3833 return FAIL;
3834 }
3835 }
3836 else if (STRCMP(hi->hi_key, "channel") == 0)
3837 {
3838 if (!(supported & JO_OUT_IO))
3839 break;
3840 opt->jo_set |= JO_CHANNEL;
3841 if (item->v_type != VAR_CHANNEL)
3842 {
3843 EMSG2(_(e_invarg2), "channel");
3844 return FAIL;
3845 }
3846 opt->jo_channel = item->vval.v_channel;
3847 }
3848 else if (STRCMP(hi->hi_key, "callback") == 0)
3849 {
3850 if (!(supported & JO_CALLBACK))
3851 break;
3852 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003853 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003854 if (opt->jo_callback == NULL)
3855 {
3856 EMSG2(_(e_invarg2), "callback");
3857 return FAIL;
3858 }
3859 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003860 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003861 {
3862 if (!(supported & JO_OUT_CALLBACK))
3863 break;
3864 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003865 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003866 if (opt->jo_out_cb == NULL)
3867 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003868 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003869 return FAIL;
3870 }
3871 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003872 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003873 {
3874 if (!(supported & JO_ERR_CALLBACK))
3875 break;
3876 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003877 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003878 if (opt->jo_err_cb == NULL)
3879 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003880 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003881 return FAIL;
3882 }
3883 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003884 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003885 {
3886 if (!(supported & JO_CLOSE_CALLBACK))
3887 break;
3888 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003889 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003890 if (opt->jo_close_cb == NULL)
3891 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003892 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003893 return FAIL;
3894 }
3895 }
3896 else if (STRCMP(hi->hi_key, "waittime") == 0)
3897 {
3898 if (!(supported & JO_WAITTIME))
3899 break;
3900 opt->jo_set |= JO_WAITTIME;
3901 opt->jo_waittime = get_tv_number(item);
3902 }
3903 else if (STRCMP(hi->hi_key, "timeout") == 0)
3904 {
3905 if (!(supported & JO_TIMEOUT))
3906 break;
3907 opt->jo_set |= JO_TIMEOUT;
3908 opt->jo_timeout = get_tv_number(item);
3909 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003910 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003911 {
3912 if (!(supported & JO_OUT_TIMEOUT))
3913 break;
3914 opt->jo_set |= JO_OUT_TIMEOUT;
3915 opt->jo_out_timeout = get_tv_number(item);
3916 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003917 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003918 {
3919 if (!(supported & JO_ERR_TIMEOUT))
3920 break;
3921 opt->jo_set |= JO_ERR_TIMEOUT;
3922 opt->jo_err_timeout = get_tv_number(item);
3923 }
3924 else if (STRCMP(hi->hi_key, "part") == 0)
3925 {
3926 if (!(supported & JO_PART))
3927 break;
3928 opt->jo_set |= JO_PART;
3929 val = get_tv_string(item);
3930 if (STRCMP(val, "err") == 0)
3931 opt->jo_part = PART_ERR;
3932 else
3933 {
3934 EMSG2(_(e_invarg2), val);
3935 return FAIL;
3936 }
3937 }
3938 else if (STRCMP(hi->hi_key, "id") == 0)
3939 {
3940 if (!(supported & JO_ID))
3941 break;
3942 opt->jo_set |= JO_ID;
3943 opt->jo_id = get_tv_number(item);
3944 }
3945 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
3946 {
3947 if (!(supported & JO_STOPONEXIT))
3948 break;
3949 opt->jo_set |= JO_STOPONEXIT;
3950 opt->jo_stoponexit = get_tv_string_buf_chk(item,
3951 opt->jo_soe_buf);
3952 if (opt->jo_stoponexit == NULL)
3953 {
3954 EMSG2(_(e_invarg2), "stoponexit");
3955 return FAIL;
3956 }
3957 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003958 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003959 {
3960 if (!(supported & JO_EXIT_CB))
3961 break;
3962 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003963 if (item->v_type == VAR_PARTIAL && item->vval.v_partial != NULL)
3964 {
3965 opt->jo_exit_partial = item->vval.v_partial;
3966 opt->jo_exit_cb = item->vval.v_partial->pt_name;
3967 }
3968 else
3969 opt->jo_exit_cb = get_tv_string_buf_chk(
3970 item, opt->jo_ecb_buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003971 if (opt->jo_exit_cb == NULL)
3972 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01003973 EMSG2(_(e_invarg2), "exit_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003974 return FAIL;
3975 }
3976 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003977 else if (STRCMP(hi->hi_key, "block_write") == 0)
3978 {
3979 if (!(supported & JO_BLOCK_WRITE))
3980 break;
3981 opt->jo_set |= JO_BLOCK_WRITE;
3982 opt->jo_block_write = get_tv_number(item);
3983 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003984 else
3985 break;
3986 --todo;
3987 }
3988 if (todo > 0)
3989 {
3990 EMSG2(_(e_invarg2), hi->hi_key);
3991 return FAIL;
3992 }
3993
3994 return OK;
3995}
3996
3997/*
3998 * Get the channel from the argument.
3999 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004000 * When "check_open" is TRUE check that the channel can be used.
4001 * When "reading" is TRUE "check_open" considers typeahead useful.
4002 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004003 */
4004 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004005get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004006{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004007 channel_T *channel = NULL;
4008 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004009
4010 if (tv->v_type == VAR_JOB)
4011 {
4012 if (tv->vval.v_job != NULL)
4013 channel = tv->vval.v_job->jv_channel;
4014 }
4015 else if (tv->v_type == VAR_CHANNEL)
4016 {
4017 channel = tv->vval.v_channel;
4018 }
4019 else
4020 {
4021 EMSG2(_(e_invarg2), get_tv_string(tv));
4022 return NULL;
4023 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004024 if (channel != NULL && reading)
4025 has_readahead = channel_has_readahead(channel,
4026 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004027
Bram Moolenaar437905c2016-04-26 19:01:05 +02004028 if (check_open && (channel == NULL || (!channel_is_open(channel)
4029 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004030 {
4031 EMSG(_("E906: not an open channel"));
4032 return NULL;
4033 }
4034 return channel;
4035}
4036
4037static job_T *first_job = NULL;
4038
4039 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004040job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004041{
4042 ch_log(job->jv_channel, "Freeing job");
4043 if (job->jv_channel != NULL)
4044 {
4045 /* The link from the channel to the job doesn't count as a reference,
4046 * thus don't decrement the refcount of the job. The reference from
4047 * the job to the channel does count the refrence, decrement it and
4048 * NULL the reference. We don't set ch_job_killed, unreferencing the
4049 * job doesn't mean it stops running. */
4050 job->jv_channel->ch_job = NULL;
4051 channel_unref(job->jv_channel);
4052 }
4053 mch_clear_job(job);
4054
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004055 vim_free(job->jv_stoponexit);
4056 vim_free(job->jv_exit_cb);
4057 partial_unref(job->jv_exit_partial);
4058}
4059
4060 static void
4061job_free_job(job_T *job)
4062{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004063 if (job->jv_next != NULL)
4064 job->jv_next->jv_prev = job->jv_prev;
4065 if (job->jv_prev == NULL)
4066 first_job = job->jv_next;
4067 else
4068 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004069 vim_free(job);
4070}
4071
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004072 static void
4073job_free(job_T *job)
4074{
4075 if (!in_free_unref_items)
4076 {
4077 job_free_contents(job);
4078 job_free_job(job);
4079 }
4080}
4081
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004082/*
4083 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004084 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4085 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004086 */
4087 static int
4088job_still_useful(job_T *job)
4089{
4090 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004091 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4092 || (job->jv_channel != NULL
4093 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004094}
4095
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004096 void
4097job_unref(job_T *job)
4098{
4099 if (job != NULL && --job->jv_refcount <= 0)
4100 {
4101 /* Do not free the job when it has not ended yet and there is a
4102 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004103 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004104 {
4105 job_free(job);
4106 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004107 else if (job->jv_channel != NULL
4108 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004109 {
4110 /* Do remove the link to the channel, otherwise it hangs
4111 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004112 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113 job->jv_channel->ch_job = NULL;
4114 channel_unref(job->jv_channel);
4115 job->jv_channel = NULL;
4116 }
4117 }
4118}
4119
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004120 int
4121free_unused_jobs_contents(int copyID, int mask)
4122{
4123 int did_free = FALSE;
4124 job_T *job;
4125
4126 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004127 if ((job->jv_copyID & mask) != (copyID & mask)
4128 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004129 {
4130 /* Free the channel and ordinary items it contains, but don't
4131 * recurse into Lists, Dictionaries etc. */
4132 job_free_contents(job);
4133 did_free = TRUE;
4134 }
4135 return did_free;
4136}
4137
4138 void
4139free_unused_jobs(int copyID, int mask)
4140{
4141 job_T *job;
4142 job_T *job_next;
4143
4144 for (job = first_job; job != NULL; job = job_next)
4145 {
4146 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004147 if ((job->jv_copyID & mask) != (copyID & mask)
4148 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004149 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004150 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004151 job_free_job(job);
4152 }
4153 }
4154}
4155
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156/*
4157 * Allocate a job. Sets the refcount to one and sets options default.
4158 */
4159 static job_T *
4160job_alloc(void)
4161{
4162 job_T *job;
4163
4164 job = (job_T *)alloc_clear(sizeof(job_T));
4165 if (job != NULL)
4166 {
4167 job->jv_refcount = 1;
4168 job->jv_stoponexit = vim_strsave((char_u *)"term");
4169
4170 if (first_job != NULL)
4171 {
4172 first_job->jv_prev = job;
4173 job->jv_next = first_job;
4174 }
4175 first_job = job;
4176 }
4177 return job;
4178}
4179
4180 void
4181job_set_options(job_T *job, jobopt_T *opt)
4182{
4183 if (opt->jo_set & JO_STOPONEXIT)
4184 {
4185 vim_free(job->jv_stoponexit);
4186 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4187 job->jv_stoponexit = NULL;
4188 else
4189 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4190 }
4191 if (opt->jo_set & JO_EXIT_CB)
4192 {
4193 vim_free(job->jv_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004194 partial_unref(job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004195 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004196 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004197 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004198 job->jv_exit_partial = NULL;
4199 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004201 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004202 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004203 job->jv_exit_partial = opt->jo_exit_partial;
4204 if (job->jv_exit_partial != NULL)
4205 ++job->jv_exit_partial->pt_refcount;
4206 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004207 }
4208}
4209
4210/*
4211 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4212 */
4213 void
4214job_stop_on_exit()
4215{
4216 job_T *job;
4217
4218 for (job = first_job; job != NULL; job = job->jv_next)
4219 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4220 mch_stop_job(job, job->jv_stoponexit);
4221}
4222
4223/*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004224 * Called once in a while: check if any jobs with an "exit_cb" have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004225 */
4226 void
4227job_check_ended(void)
4228{
4229 static time_t last_check = 0;
4230 time_t now;
4231 job_T *job;
4232 job_T *next;
4233
4234 /* Only do this once in 10 seconds. */
4235 now = time(NULL);
4236 if (last_check + 10 < now)
4237 {
4238 last_check = now;
4239 for (job = first_job; job != NULL; job = next)
4240 {
4241 next = job->jv_next;
4242 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
4243 job_status(job); /* may free "job" */
4244 }
4245 }
4246}
4247
4248/*
4249 * "job_start()" function
4250 */
4251 job_T *
4252job_start(typval_T *argvars)
4253{
4254 job_T *job;
4255 char_u *cmd = NULL;
4256#if defined(UNIX)
4257# define USE_ARGV
4258 char **argv = NULL;
4259 int argc = 0;
4260#else
4261 garray_T ga;
4262#endif
4263 jobopt_T opt;
4264 int part;
4265
4266 job = job_alloc();
4267 if (job == NULL)
4268 return NULL;
4269
4270 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004271#ifndef USE_ARGV
4272 ga_init2(&ga, (int)sizeof(char*), 20);
4273#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004274
4275 /* Default mode is NL. */
4276 clear_job_options(&opt);
4277 opt.jo_mode = MODE_NL;
4278 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004279 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4280 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004281 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004282
4283 /* Check that when io is "file" that there is a file name. */
4284 for (part = PART_OUT; part <= PART_IN; ++part)
4285 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4286 && opt.jo_io[part] == JIO_FILE
4287 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4288 || *opt.jo_io_name[part] == NUL))
4289 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004290 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004291 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004292 }
4293
4294 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4295 {
4296 buf_T *buf = NULL;
4297
4298 /* check that we can find the buffer before starting the job */
4299 if (opt.jo_set & JO_IN_BUF)
4300 {
4301 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4302 if (buf == NULL)
4303 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4304 }
4305 else if (!(opt.jo_set & JO_IN_NAME))
4306 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004307 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004308 }
4309 else
4310 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4311 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004312 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004313 if (buf->b_ml.ml_mfp == NULL)
4314 {
4315 char_u numbuf[NUMBUFLEN];
4316 char_u *s;
4317
4318 if (opt.jo_set & JO_IN_BUF)
4319 {
4320 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4321 s = numbuf;
4322 }
4323 else
4324 s = opt.jo_io_name[PART_IN];
4325 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004326 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004327 }
4328 job->jv_in_buf = buf;
4329 }
4330
4331 job_set_options(job, &opt);
4332
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004333 if (argvars[0].v_type == VAR_STRING)
4334 {
4335 /* Command is a string. */
4336 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004337 if (cmd == NULL || *cmd == NUL)
4338 {
4339 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004340 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004341 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004342#ifdef USE_ARGV
4343 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004344 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004345 argv[argc] = NULL;
4346#endif
4347 }
4348 else if (argvars[0].v_type != VAR_LIST
4349 || argvars[0].vval.v_list == NULL
4350 || argvars[0].vval.v_list->lv_len < 1)
4351 {
4352 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004353 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004354 }
4355 else
4356 {
4357 list_T *l = argvars[0].vval.v_list;
4358 listitem_T *li;
4359 char_u *s;
4360
4361#ifdef USE_ARGV
4362 /* Pass argv[] to mch_call_shell(). */
4363 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4364 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004365 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004366#endif
4367 for (li = l->lv_first; li != NULL; li = li->li_next)
4368 {
4369 s = get_tv_string_chk(&li->li_tv);
4370 if (s == NULL)
4371 goto theend;
4372#ifdef USE_ARGV
4373 argv[argc++] = (char *)s;
4374#else
4375 /* Only escape when needed, double quotes are not always allowed. */
4376 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4377 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004378# ifdef WIN32
4379 int old_ssl = p_ssl;
4380
4381 /* This is using CreateProcess, not cmd.exe. Always use
4382 * double quote and backslashes. */
4383 p_ssl = 0;
4384# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004385 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004386# ifdef WIN32
4387 p_ssl = old_ssl;
4388# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004389 if (s == NULL)
4390 goto theend;
4391 ga_concat(&ga, s);
4392 vim_free(s);
4393 }
4394 else
4395 ga_concat(&ga, s);
4396 if (li->li_next != NULL)
4397 ga_append(&ga, ' ');
4398#endif
4399 }
4400#ifdef USE_ARGV
4401 argv[argc] = NULL;
4402#else
4403 cmd = ga.ga_data;
4404#endif
4405 }
4406
4407#ifdef USE_ARGV
4408 if (ch_log_active())
4409 {
4410 garray_T ga;
4411 int i;
4412
4413 ga_init2(&ga, (int)sizeof(char), 200);
4414 for (i = 0; i < argc; ++i)
4415 {
4416 if (i > 0)
4417 ga_concat(&ga, (char_u *)" ");
4418 ga_concat(&ga, (char_u *)argv[i]);
4419 }
4420 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4421 ga_clear(&ga);
4422 }
4423 mch_start_job(argv, job, &opt);
4424#else
4425 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4426 mch_start_job((char *)cmd, job, &opt);
4427#endif
4428
4429 /* If the channel is reading from a buffer, write lines now. */
4430 if (job->jv_channel != NULL)
4431 channel_write_in(job->jv_channel);
4432
4433theend:
4434#ifdef USE_ARGV
4435 vim_free(argv);
4436#else
4437 vim_free(ga.ga_data);
4438#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004439 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004440 return job;
4441}
4442
4443/*
4444 * Get the status of "job" and invoke the exit callback when needed.
4445 * The returned string is not allocated.
4446 */
4447 char *
4448job_status(job_T *job)
4449{
4450 char *result;
4451
4452 if (job->jv_status == JOB_ENDED)
4453 /* No need to check, dead is dead. */
4454 result = "dead";
4455 else if (job->jv_status == JOB_FAILED)
4456 result = "fail";
4457 else
4458 {
4459 result = mch_job_status(job);
4460 if (job->jv_status == JOB_ENDED)
4461 ch_log(job->jv_channel, "Job ended");
4462 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4463 {
4464 typval_T argv[3];
4465 typval_T rettv;
4466 int dummy;
4467
4468 /* invoke the exit callback; make sure the refcount is > 0 */
4469 ++job->jv_refcount;
4470 argv[0].v_type = VAR_JOB;
4471 argv[0].vval.v_job = job;
4472 argv[1].v_type = VAR_NUMBER;
4473 argv[1].vval.v_number = job->jv_exitval;
4474 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004475 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
4476 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004477 clear_tv(&rettv);
4478 --job->jv_refcount;
4479 }
4480 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4481 {
4482 /* The job was already unreferenced, now that it ended it can be
4483 * freed. Careful: caller must not use "job" after this! */
4484 job_free(job);
4485 }
4486 }
4487 return result;
4488}
4489
Bram Moolenaar8950a562016-03-12 15:22:55 +01004490/*
4491 * Implementation of job_info().
4492 */
4493 void
4494job_info(job_T *job, dict_T *dict)
4495{
4496 dictitem_T *item;
4497 varnumber_T nr;
4498
4499 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4500
4501 item = dictitem_alloc((char_u *)"channel");
4502 if (item == NULL)
4503 return;
4504 item->di_tv.v_lock = 0;
4505 item->di_tv.v_type = VAR_CHANNEL;
4506 item->di_tv.vval.v_channel = job->jv_channel;
4507 if (job->jv_channel != NULL)
4508 ++job->jv_channel->ch_refcount;
4509 if (dict_add(dict, item) == FAIL)
4510 dictitem_free(item);
4511
4512#ifdef UNIX
4513 nr = job->jv_pid;
4514#else
4515 nr = job->jv_proc_info.dwProcessId;
4516#endif
4517 dict_add_nr_str(dict, "process", nr, NULL);
4518
4519 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004520 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004521 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4522}
4523
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004524 int
4525job_stop(job_T *job, typval_T *argvars)
4526{
4527 char_u *arg;
4528
4529 if (argvars[1].v_type == VAR_UNKNOWN)
4530 arg = (char_u *)"";
4531 else
4532 {
4533 arg = get_tv_string_chk(&argvars[1]);
4534 if (arg == NULL)
4535 {
4536 EMSG(_(e_invarg));
4537 return 0;
4538 }
4539 }
4540 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4541 if (mch_stop_job(job, arg) == FAIL)
4542 return 0;
4543
4544 /* Assume that "hup" does not kill the job. */
4545 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4546 job->jv_channel->ch_job_killed = TRUE;
4547
4548 /* We don't try freeing the job, obviously the caller still has a
4549 * reference to it. */
4550 return 1;
4551}
4552
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004553#endif /* FEAT_JOB_CHANNEL */