blob: ba6e7ec955abda956ad83c2deea37838a3d35b1a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
22/* Note: when making changes here also adjust configure.in. */
23#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
119 EMSG2(_(e_notopen), fname);
120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200135ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100162ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100170 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171 }
172}
173
174 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100175ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176{
177 if (log_fd != NULL)
178 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100181 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100183 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184 }
185}
186
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100188ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100192 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100196 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197 }
198}
199
200 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100201ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100209 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 }
211}
212
213 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100214ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215{
216 if (log_fd != NULL)
217 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100222 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223 }
224}
225
226 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100227ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100228{
229 if (log_fd != NULL)
230 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100235 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236 }
237}
238
239 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100240ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100241{
242 if (log_fd != NULL)
243 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100248 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100249 }
250}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100251
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252#ifdef _WIN32
253# undef PERROR
254# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
255 (char_u *)msg, (char_u *)strerror_win32(errno))
256
257 static char *
258strerror_win32(int eno)
259{
260 static LPVOID msgbuf = NULL;
261 char_u *ptr;
262
263 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200264 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100265 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200266 msgbuf = NULL;
267 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100268 FormatMessage(
269 FORMAT_MESSAGE_ALLOCATE_BUFFER |
270 FORMAT_MESSAGE_FROM_SYSTEM |
271 FORMAT_MESSAGE_IGNORE_INSERTS,
272 NULL,
273 eno,
274 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
275 (LPTSTR) &msgbuf,
276 0,
277 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200278 if (msgbuf != NULL)
279 /* chomp \r or \n */
280 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
281 switch (*ptr)
282 {
283 case '\r':
284 STRMOVE(ptr, ptr + 1);
285 ptr--;
286 break;
287 case '\n':
288 if (*(ptr + 1) == '\0')
289 *ptr = '\0';
290 else
291 *ptr = ' ';
292 break;
293 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100294 return msgbuf;
295}
296#endif
297
Bram Moolenaar77073442016-02-13 23:23:53 +0100298/*
299 * The list of all allocated channels.
300 */
301static channel_T *first_channel = NULL;
302static int next_ch_id = 0;
303
304/*
305 * Allocate a new channel. The refcount is set to 1.
306 * The channel isn't actually used until it is opened.
307 * Returns NULL if out of memory.
308 */
309 channel_T *
310add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100311{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100314
Bram Moolenaar77073442016-02-13 23:23:53 +0100315 if (channel == NULL)
316 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317
Bram Moolenaar77073442016-02-13 23:23:53 +0100318 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100319 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100320
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100323 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100324#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100325 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100326#endif
327#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100328 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100329#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100330 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100331 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100332
Bram Moolenaar77073442016-02-13 23:23:53 +0100333 if (first_channel != NULL)
334 {
335 first_channel->ch_prev = channel;
336 channel->ch_next = first_channel;
337 }
338 first_channel = channel;
339
340 channel->ch_refcount = 1;
341 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100342}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100343
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200344 int
345has_any_channel(void)
346{
347 return first_channel != NULL;
348}
349
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100350/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100351 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100352 * Return TRUE if "channel" has a callback and the associated job wasn't
353 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100354 */
355 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100356channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100357{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100359 int has_out_msg;
360 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100361
362 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100363 if (channel->ch_job_killed && channel->ch_job == NULL)
364 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100365
366 /* If there is a close callback it may still need to be invoked. */
367 if (channel->ch_close_cb != NULL)
368 return TRUE;
369
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200370 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200371 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200372 return TRUE;
373
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100374 /* If there is no callback then nobody can get readahead. If the fd is
375 * closed and there is no readahead then the callback won't be called. */
376 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
377 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
378 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100379 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
380 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
381 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
382 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
383 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
384 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100385 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100386 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200387 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200388 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
389 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200390 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200391 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
392 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100393}
394
395/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200396 * Close a channel and free all its resources.
397 */
398 static void
399channel_free_contents(channel_T *channel)
400{
401 channel_close(channel, TRUE);
402 channel_clear(channel);
403 ch_log(channel, "Freeing channel");
404}
405
406 static void
407channel_free_channel(channel_T *channel)
408{
409 if (channel->ch_next != NULL)
410 channel->ch_next->ch_prev = channel->ch_prev;
411 if (channel->ch_prev == NULL)
412 first_channel = channel->ch_next;
413 else
414 channel->ch_prev->ch_next = channel->ch_next;
415 vim_free(channel);
416}
417
418 static void
419channel_free(channel_T *channel)
420{
421 if (!in_free_unref_items)
422 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200423 if (safe_to_invoke_callback == 0)
424 {
425 channel->ch_to_be_freed = TRUE;
426 }
427 else
428 {
429 channel_free_contents(channel);
430 channel_free_channel(channel);
431 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200432 }
433}
434
435/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100437 * possible, there is no callback to be invoked or the associated job was
438 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100439 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100441 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100442channel_may_free(channel_T *channel)
443{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100444 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100445 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100446 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100447 return TRUE;
448 }
449 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100450}
451
452/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100453 * Decrement the reference count on "channel" and maybe free it when it goes
454 * down to zero. Don't free it if there is a pending action.
455 * Returns TRUE when the channel is no longer referenced.
456 */
457 int
458channel_unref(channel_T *channel)
459{
460 if (channel != NULL && --channel->ch_refcount <= 0)
461 return channel_may_free(channel);
462 return FALSE;
463}
464
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200465 int
466free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100467{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200468 int did_free = FALSE;
469 channel_T *ch;
470
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200471 /* This is invoked from the garbage collector, which only runs at a safe
472 * point. */
473 ++safe_to_invoke_callback;
474
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200475 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200476 if (!channel_still_useful(ch)
477 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200478 {
479 /* Free the channel and ordinary items it contains, but don't
480 * recurse into Lists, Dictionaries etc. */
481 channel_free_contents(ch);
482 did_free = TRUE;
483 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200484
485 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200486 return did_free;
487}
488
489 void
490free_unused_channels(int copyID, int mask)
491{
492 channel_T *ch;
493 channel_T *ch_next;
494
495 for (ch = first_channel; ch != NULL; ch = ch_next)
496 {
497 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200498 if (!channel_still_useful(ch)
499 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200500 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200501 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200502 channel_free_channel(ch);
503 }
504 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100505}
506
Bram Moolenaard04a0202016-01-26 23:30:18 +0100507#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100508
509#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
510 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100511channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100512{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100513 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100514 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100515
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100516 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100517 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100518 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100519 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200520 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100521}
522#endif
523
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100526 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100527#ifdef FEAT_GUI_X11
528 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200529messageFromServer(XtPointer clientData,
530 int *unused1 UNUSED,
531 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100532{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100533 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100534}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100536
Bram Moolenaard04a0202016-01-26 23:30:18 +0100537#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100538# if GTK_CHECK_VERSION(3,0,0)
539 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200540messageFromServer(GIOChannel *unused1 UNUSED,
541 GIOCondition unused2 UNUSED,
542 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100543{
544 channel_read_fd(GPOINTER_TO_INT(clientData));
545 return TRUE; /* Return FALSE instead in case the event source is to
546 * be removed after this function returns. */
547}
548# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100549 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200550messageFromServer(gpointer clientData,
551 gint unused1 UNUSED,
552 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100553{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100554 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100555}
Bram Moolenaar98921892016-02-23 17:14:37 +0100556# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100557#endif
558
559 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100560channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100561{
Bram Moolenaarde279892016-03-11 22:19:44 +0100562 if (!CH_HAS_GUI)
563 return;
564
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100565# ifdef FEAT_GUI_X11
566 /* Tell notifier we are interested in being called
567 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
569 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100570 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100571 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100572 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200573 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100574 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100575# else
576# ifdef FEAT_GUI_GTK
577 /* Tell gdk we are interested in being called when there
578 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100579 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100580# if GTK_CHECK_VERSION(3,0,0)
581 {
582 GIOChannel *chnnl = g_io_channel_unix_new(
583 (gint)channel->ch_part[part].ch_fd);
584
585 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
586 chnnl,
587 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200588 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100589 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
590
591 g_io_channel_unref(chnnl);
592 }
593# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100594 channel->ch_part[part].ch_inputHandler = gdk_input_add(
595 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100596 (GdkInputCondition)
597 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200598 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100599 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100600# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100601# endif
602# endif
603}
604
Bram Moolenaarde279892016-03-11 22:19:44 +0100605 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100606channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100607{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100608 if (channel->CH_SOCK_FD != INVALID_FD)
609 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100610 if (channel->CH_OUT_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_OUT);
612 if (channel->CH_ERR_FD != INVALID_FD)
613 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100614}
615
616/*
617 * Register any of our file descriptors with the GUI event handling system.
618 * Called when the GUI has started.
619 */
620 void
621channel_gui_register_all(void)
622{
Bram Moolenaar77073442016-02-13 23:23:53 +0100623 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100624
Bram Moolenaar77073442016-02-13 23:23:53 +0100625 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100626 channel_gui_register(channel);
627}
628
629 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100630channel_gui_unregister_one(channel_T *channel, int part)
631{
632# ifdef FEAT_GUI_X11
633 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
634 {
635 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
636 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
637 }
638# else
639# ifdef FEAT_GUI_GTK
640 if (channel->ch_part[part].ch_inputHandler != 0)
641 {
642# if GTK_CHECK_VERSION(3,0,0)
643 g_source_remove(channel->ch_part[part].ch_inputHandler);
644# else
645 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
646# endif
647 channel->ch_part[part].ch_inputHandler = 0;
648 }
649# endif
650# endif
651}
652
653 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100654channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100655{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100656 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100657
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100658 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100659 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660}
661
662#endif
663
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100664static char *e_cannot_connect = N_("E902: Cannot connect to port");
665
Bram Moolenaard04a0202016-01-26 23:30:18 +0100666/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100667 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100668 * "waittime" is the time in msec to wait for the connection.
669 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100670 * Returns the channel for success.
671 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100672 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100673 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100674channel_open(
675 char *hostname,
676 int port_in,
677 int waittime,
678 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100681 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100682 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100683#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100684 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100685 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686#else
687 int port = port_in;
688#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100689 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100690 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100692#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693 channel_init_winsock();
694#endif
695
Bram Moolenaar77073442016-02-13 23:23:53 +0100696 channel = add_channel();
697 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100698 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100699 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100700 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100701 }
702
703 /* Get the server internet address and put into addr structure */
704 /* fill in the socket address structure and connect to server */
705 vim_memset((char *)&server, 0, sizeof(server));
706 server.sin_family = AF_INET;
707 server.sin_port = htons(port);
708 if ((host = gethostbyname(hostname)) == NULL)
709 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100710 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200711 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100712 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100713 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100714 }
715 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
716
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100717 /* On Mac and Solaris a zero timeout almost never works. At least wait
718 * one millisecond. Let's do it for all systems, because we don't know why
719 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 if (waittime == 0)
721 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100722
723 /*
724 * For Unix we need to call connect() again after connect() failed.
725 * On Win32 one time is sufficient.
726 */
727 while (TRUE)
728 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100729 long elapsed_msec = 0;
730 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100731
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100732 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100733 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100734 sd = socket(AF_INET, SOCK_STREAM, 0);
735 if (sd == -1)
736 {
737 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200738 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100739 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100740 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100741 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100742
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100743 if (waittime >= 0)
744 {
745 /* Make connect() non-blocking. */
746 if (
747#ifdef _WIN32
748 ioctlsocket(sd, FIONBIO, &val) < 0
749#else
750 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
751#endif
752 )
753 {
754 SOCK_ERRNO;
755 ch_errorn(channel,
756 "channel_open: Connect failed with errno %d", errno);
757 sock_close(sd);
758 channel_free(channel);
759 return NULL;
760 }
761 }
762
763 /* Try connecting to the server. */
764 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
765 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
766
Bram Moolenaar045a2842016-03-08 22:33:07 +0100767 if (ret == 0)
768 /* The connection could be established. */
769 break;
770
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100771 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100772 if (waittime < 0 || (errno != EWOULDBLOCK
773 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100774#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100775 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100776#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100777 ))
778 {
779 ch_errorn(channel,
780 "channel_open: Connect failed with errno %d", errno);
781 PERROR(_(e_cannot_connect));
782 sock_close(sd);
783 channel_free(channel);
784 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100785 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100786
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100787 /* Limit the waittime to 50 msec. If it doesn't work within this
788 * time we close the socket and try creating it again. */
789 waitnow = waittime > 50 ? 50 : waittime;
790
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100792 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100793#ifndef WIN32
794 if (errno != ECONNREFUSED)
795#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100796 {
797 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100798 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100799 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100800#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100801 int so_error = 0;
802 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100803 struct timeval start_tv;
804 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100805#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 FD_ZERO(&rfds);
807 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 FD_ZERO(&wfds);
809 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100810
Bram Moolenaar562ca712016-03-09 21:50:05 +0100811 tv.tv_sec = waitnow / 1000;
812 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100813#ifndef WIN32
814 gettimeofday(&start_tv, NULL);
815#endif
816 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100817 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100819
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100820 if (ret < 0)
821 {
822 SOCK_ERRNO;
823 ch_errorn(channel,
824 "channel_open: Connect failed with errno %d", errno);
825 PERROR(_(e_cannot_connect));
826 sock_close(sd);
827 channel_free(channel);
828 return NULL;
829 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100830
Bram Moolenaare081e212016-02-28 22:33:46 +0100831#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100832 /* On Win32: select() is expected to work and wait for up to
833 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100834 if (FD_ISSET(sd, &wfds))
835 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100836 elapsed_msec = waitnow;
837 if (waittime > 1 && elapsed_msec < waittime)
838 {
839 waittime -= elapsed_msec;
840 continue;
841 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100842#else
843 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100844 * After putting the socket in non-blocking mode, connect() will
845 * return EINPROGRESS, select() will not wait (as if writing is
846 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100847 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100848 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100849 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100850 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100851 {
852 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100853 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100854 if (ret < 0 || (so_error != 0
855 && so_error != EWOULDBLOCK
856 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100857# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100858 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100859# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100860 ))
861 {
862 ch_errorn(channel,
863 "channel_open: Connect failed with errno %d",
864 so_error);
865 PERROR(_(e_cannot_connect));
866 sock_close(sd);
867 channel_free(channel);
868 return NULL;
869 }
870 }
871
Bram Moolenaar045a2842016-03-08 22:33:07 +0100872 if (FD_ISSET(sd, &wfds) && so_error == 0)
873 /* Did not detect an error, connection is established. */
874 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100875
Bram Moolenaar045a2842016-03-08 22:33:07 +0100876 gettimeofday(&end_tv, NULL);
877 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
878 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100879#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100880 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100881
882#ifndef WIN32
883 if (waittime > 1 && elapsed_msec < waittime)
884 {
885 /* The port isn't ready but we also didn't get an error.
886 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100887 * yet. Select() may return early, wait until the remaining
888 * "waitnow" and try again. */
889 waitnow -= elapsed_msec;
890 waittime -= elapsed_msec;
891 if (waitnow > 0)
892 {
893 mch_delay((long)waitnow, TRUE);
894 ui_breakcheck();
895 waittime -= waitnow;
896 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100897 if (!got_int)
898 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100899 if (waittime <= 0)
900 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100901 waittime = 1;
902 continue;
903 }
904 /* we were interrupted, behave as if timed out */
905 }
906#endif
907
908 /* We timed out. */
909 ch_error(channel, "Connection timed out");
910 sock_close(sd);
911 channel_free(channel);
912 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100913 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100914
Bram Moolenaar045a2842016-03-08 22:33:07 +0100915 ch_log(channel, "Connection made");
916
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100917 if (waittime >= 0)
918 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100919#ifdef _WIN32
920 val = 0;
921 ioctlsocket(sd, FIONBIO, &val);
922#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100923 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100924#endif
925 }
926
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100927 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100928 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100929 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
930 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100931
932#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100933 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100934#endif
935
Bram Moolenaar77073442016-02-13 23:23:53 +0100936 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100937}
938
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939/*
940 * Implements ch_open().
941 */
942 channel_T *
943channel_open_func(typval_T *argvars)
944{
945 char_u *address;
946 char_u *p;
947 char *rest;
948 int port;
949 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200950 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100951
952 address = get_tv_string(&argvars[0]);
953 if (argvars[1].v_type != VAR_UNKNOWN
954 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
955 {
956 EMSG(_(e_invarg));
957 return NULL;
958 }
959
960 /* parse address */
961 p = vim_strchr(address, ':');
962 if (p == NULL)
963 {
964 EMSG2(_(e_invarg2), address);
965 return NULL;
966 }
967 *p++ = NUL;
968 port = strtol((char *)p, &rest, 10);
969 if (*address == NUL || port <= 0 || *rest != NUL)
970 {
971 p[-1] = ':';
972 EMSG2(_(e_invarg2), address);
973 return NULL;
974 }
975
976 /* parse options */
977 clear_job_options(&opt);
978 opt.jo_mode = MODE_JSON;
979 opt.jo_timeout = 2000;
980 if (get_job_options(&argvars[1], &opt,
981 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200982 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100983 if (opt.jo_timeout < 0)
984 {
985 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200986 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100987 }
988
989 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
990 if (channel != NULL)
991 {
992 opt.jo_set = JO_ALL;
993 channel_set_options(channel, &opt);
994 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200995theend:
996 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100997 return channel;
998}
999
Bram Moolenaarde279892016-03-11 22:19:44 +01001000 static void
1001may_close_part(sock_T *fd)
1002{
1003 if (*fd != INVALID_FD)
1004 {
1005 fd_close(*fd);
1006 *fd = INVALID_FD;
1007 }
1008}
1009
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001010 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001011channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001012{
Bram Moolenaarde279892016-03-11 22:19:44 +01001013 if (in != INVALID_FD)
1014 {
1015 may_close_part(&channel->CH_IN_FD);
1016 channel->CH_IN_FD = in;
1017 }
1018 if (out != INVALID_FD)
1019 {
1020# if defined(FEAT_GUI)
1021 channel_gui_unregister_one(channel, PART_OUT);
1022# endif
1023 may_close_part(&channel->CH_OUT_FD);
1024 channel->CH_OUT_FD = out;
1025# if defined(FEAT_GUI)
1026 channel_gui_register_one(channel, PART_OUT);
1027# endif
1028 }
1029 if (err != INVALID_FD)
1030 {
1031# if defined(FEAT_GUI)
1032 channel_gui_unregister_one(channel, PART_ERR);
1033# endif
1034 may_close_part(&channel->CH_ERR_FD);
1035 channel->CH_ERR_FD = err;
1036# if defined(FEAT_GUI)
1037 channel_gui_register_one(channel, PART_ERR);
1038# endif
1039 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001040}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001041
Bram Moolenaard6051b52016-02-28 15:49:03 +01001042/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001043 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001044 * This does not keep a refcount, when the job is freed ch_job is cleared.
1045 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001046 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001047channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001048{
Bram Moolenaar77073442016-02-13 23:23:53 +01001049 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001050
1051 channel_set_options(channel, options);
1052
1053 if (job->jv_in_buf != NULL)
1054 {
1055 chanpart_T *in_part = &channel->ch_part[PART_IN];
1056
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001057 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001058 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001059 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001060 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001061 {
1062 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1063 {
1064 /* Special mode: send last-but-one line when appending a line
1065 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001066 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001067 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001068 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001069 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001070 }
1071 else
1072 in_part->ch_buf_top = options->jo_in_top;
1073 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001074 else
1075 in_part->ch_buf_top = 1;
1076 if (options->jo_set & JO_IN_BOT)
1077 in_part->ch_buf_bot = options->jo_in_bot;
1078 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001079 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001080 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001081}
1082
1083/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001084 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001085 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001086 */
1087 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001088find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001089{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001090 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001091 buf_T *save_curbuf = curbuf;
1092
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001093 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001094 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001095 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001096 if (buf == NULL)
1097 buf = buflist_findname_exp(name);
1098 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 if (buf == NULL)
1100 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001101 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001102 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001103 if (buf == NULL)
1104 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001105 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001106 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001107#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001108 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1109 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001110#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001111 if (curbuf->b_ml.ml_mfp == NULL)
1112 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001113 if (msg)
1114 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001115 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001116 changed_bytes(1, 0);
1117 curbuf = save_curbuf;
1118 }
1119
1120 return buf;
1121}
1122
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001123 static void
1124set_callback(
1125 char_u **cbp,
1126 partial_T **pp,
1127 char_u *callback,
1128 partial_T *partial)
1129{
1130 free_callback(*cbp, *pp);
1131 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001132 {
1133 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001134 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001135 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001136 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001137 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001138 func_ref(*cbp);
1139 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001140 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001141 else
1142 *cbp = NULL;
1143 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001144 if (partial != NULL)
1145 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001146}
1147
Bram Moolenaar187db502016-02-27 14:44:26 +01001148/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001150 */
1151 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001152channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001153{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001154 int part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001155
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001156 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001157 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001158 channel->ch_part[part].ch_mode = opt->jo_mode;
1159 if (opt->jo_set & JO_IN_MODE)
1160 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1161 if (opt->jo_set & JO_OUT_MODE)
1162 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1163 if (opt->jo_set & JO_ERR_MODE)
1164 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001165
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001166 if (opt->jo_set & JO_TIMEOUT)
1167 for (part = PART_SOCK; part <= PART_IN; ++part)
1168 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1169 if (opt->jo_set & JO_OUT_TIMEOUT)
1170 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1171 if (opt->jo_set & JO_ERR_TIMEOUT)
1172 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001173 if (opt->jo_set & JO_BLOCK_WRITE)
1174 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001175
1176 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001177 set_callback(&channel->ch_callback, &channel->ch_partial,
1178 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001179 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001180 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1181 &channel->ch_part[PART_OUT].ch_partial,
1182 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001183 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001184 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1185 &channel->ch_part[PART_ERR].ch_partial,
1186 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001187 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001188 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1189 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar187db502016-02-27 14:44:26 +01001190
1191 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1192 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001193 buf_T *buf;
1194
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001195 /* writing output to a buffer. Default mode is NL. */
1196 if (!(opt->jo_set & JO_OUT_MODE))
1197 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001198 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001199 {
1200 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1201 if (buf == NULL)
1202 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1203 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001204 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001205 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001206 int msg = TRUE;
1207
1208 if (opt->jo_set2 & JO2_OUT_MSG)
1209 msg = opt->jo_message[PART_OUT];
1210 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001211 }
1212 if (buf != NULL)
1213 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001214 if (opt->jo_set & JO_OUT_MODIFIABLE)
1215 channel->ch_part[PART_OUT].ch_nomodifiable =
1216 !opt->jo_modifiable[PART_OUT];
1217
1218 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1219 {
1220 EMSG(_(e_modifiable));
1221 }
1222 else
1223 {
1224 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001225 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001226 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001227 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001228 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001229 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001230
1231 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1232 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1233 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1234 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001235 buf_T *buf;
1236
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001237 /* writing err to a buffer. Default mode is NL. */
1238 if (!(opt->jo_set & JO_ERR_MODE))
1239 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1240 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001241 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001242 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001243 {
1244 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1245 if (buf == NULL)
1246 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1247 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001248 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001249 {
1250 int msg = TRUE;
1251
1252 if (opt->jo_set2 & JO2_ERR_MSG)
1253 msg = opt->jo_message[PART_ERR];
1254 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1255 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001256 if (buf != NULL)
1257 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001258 if (opt->jo_set & JO_ERR_MODIFIABLE)
1259 channel->ch_part[PART_ERR].ch_nomodifiable =
1260 !opt->jo_modifiable[PART_ERR];
1261 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1262 {
1263 EMSG(_(e_modifiable));
1264 }
1265 else
1266 {
1267 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001268 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001269 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001270 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001271 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001272 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001273
1274 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1275 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1276 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001277}
1278
1279/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001280 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001281 */
1282 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001283channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001284 channel_T *channel,
1285 int part,
1286 char_u *callback,
1287 partial_T *partial,
1288 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001289{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001290 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001291 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1292
1293 if (item != NULL)
1294 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001295 item->cq_partial = partial;
1296 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001297 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001298 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001299 item->cq_callback = callback;
1300 }
1301 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001302 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001303 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001304 func_ref(item->cq_callback);
1305 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001306 item->cq_seq_nr = id;
1307 item->cq_prev = head->cq_prev;
1308 head->cq_prev = item;
1309 item->cq_next = NULL;
1310 if (item->cq_prev == NULL)
1311 head->cq_next = item;
1312 else
1313 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001314 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001315}
1316
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001317 static void
1318write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1319{
1320 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001321 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001322 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001323 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001324
Bram Moolenaar655da312016-05-28 22:22:34 +02001325 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001326 if ((p = alloc(len + 2)) == NULL)
1327 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001328 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001329
1330 for (i = 0; i < len; ++i)
1331 if (p[i] == NL)
1332 p[i] = NUL;
1333
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001334 p[len] = NL;
1335 p[len + 1] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001336 channel_send(channel, PART_IN, p, len + 1, "write_buf_line()");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001337 vim_free(p);
1338}
1339
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001340/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001341 * Return TRUE if "channel" can be written to.
1342 * Returns FALSE if the input is closed or the write would block.
1343 */
1344 static int
1345can_write_buf_line(channel_T *channel)
1346{
1347 chanpart_T *in_part = &channel->ch_part[PART_IN];
1348
1349 if (in_part->ch_fd == INVALID_FD)
1350 return FALSE; /* pipe was closed */
1351
1352 /* for testing: block every other attempt to write */
1353 if (in_part->ch_block_write == 1)
1354 in_part->ch_block_write = -1;
1355 else if (in_part->ch_block_write == -1)
1356 in_part->ch_block_write = 1;
1357
1358 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1359#ifndef WIN32
1360 {
1361# if defined(HAVE_SELECT)
1362 struct timeval tval;
1363 fd_set wfds;
1364 int ret;
1365
1366 FD_ZERO(&wfds);
1367 FD_SET((int)in_part->ch_fd, &wfds);
1368 tval.tv_sec = 0;
1369 tval.tv_usec = 0;
1370 for (;;)
1371 {
1372 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1373# ifdef EINTR
1374 SOCK_ERRNO;
1375 if (ret == -1 && errno == EINTR)
1376 continue;
1377# endif
1378 if (ret <= 0 || in_part->ch_block_write == 1)
1379 {
1380 if (ret > 0)
1381 ch_log(channel, "FAKED Input not ready for writing");
1382 else
1383 ch_log(channel, "Input not ready for writing");
1384 return FALSE;
1385 }
1386 break;
1387 }
1388# else
1389 struct pollfd fds;
1390
1391 fds.fd = in_part->ch_fd;
1392 fds.events = POLLOUT;
1393 if (poll(&fds, 1, 0) <= 0)
1394 {
1395 ch_log(channel, "Input not ready for writing");
1396 return FALSE;
1397 }
1398 if (in_part->ch_block_write == 1)
1399 {
1400 ch_log(channel, "FAKED Input not ready for writing");
1401 return FALSE;
1402 }
1403# endif
1404 }
1405#endif
1406 return TRUE;
1407}
1408
1409/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001410 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001411 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001412 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001413channel_write_in(channel_T *channel)
1414{
1415 chanpart_T *in_part = &channel->ch_part[PART_IN];
1416 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001417 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001418 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001419
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001420 if (buf == NULL || in_part->ch_buf_append)
1421 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001422 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001423 {
1424 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001425 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001426 return;
1427 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001428
1429 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1430 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1431 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001432 if (!can_write_buf_line(channel))
1433 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001434 write_buf_line(buf, lnum, channel);
1435 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001436 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001437
1438 if (written == 1)
1439 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1440 else if (written > 1)
1441 ch_logn(channel, "written %d lines to channel", written);
1442
Bram Moolenaar014069a2016-03-03 22:51:40 +01001443 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001444 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001445 {
1446 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001447 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001448 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001449
1450 /* Close the pipe/socket, so that the other side gets EOF. */
1451 may_close_part(&channel->CH_IN_FD);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001452 }
1453 else
1454 ch_logn(channel, "Still %d more lines to write",
1455 buf->b_ml.ml_line_count - lnum + 1);
1456}
1457
1458/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001459 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001460 */
1461 void
1462channel_buffer_free(buf_T *buf)
1463{
1464 channel_T *channel;
1465 int part;
1466
1467 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1468 for (part = PART_SOCK; part <= PART_IN; ++part)
1469 {
1470 chanpart_T *ch_part = &channel->ch_part[part];
1471
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001472 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001473 {
1474 ch_logs(channel, "%s buffer has been wiped out",
1475 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001476 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001477 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001478 }
1479}
1480
1481/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001482 * Write any lines waiting to be written to a channel.
1483 */
1484 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001485channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001486{
1487 channel_T *channel;
1488
1489 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1490 {
1491 chanpart_T *in_part = &channel->ch_part[PART_IN];
1492
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001493 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001494 {
1495 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001496 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001497 else
1498 channel_write_in(channel);
1499 }
1500 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001501}
1502
1503/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 * Write appended lines above the last one in "buf" to the channel.
1505 */
1506 void
1507channel_write_new_lines(buf_T *buf)
1508{
1509 channel_T *channel;
1510 int found_one = FALSE;
1511
1512 /* There could be more than one channel for the buffer, loop over all of
1513 * them. */
1514 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1515 {
1516 chanpart_T *in_part = &channel->ch_part[PART_IN];
1517 linenr_T lnum;
1518 int written = 0;
1519
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001520 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001521 {
1522 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001523 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001524 found_one = TRUE;
1525 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1526 ++lnum)
1527 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001528 if (!can_write_buf_line(channel))
1529 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530 write_buf_line(buf, lnum, channel);
1531 ++written;
1532 }
1533
1534 if (written == 1)
1535 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1536 else if (written > 1)
1537 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001538 if (lnum < buf->b_ml.ml_line_count)
1539 ch_logn(channel, "Still %d more lines to write",
1540 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001541
1542 in_part->ch_buf_bot = lnum;
1543 }
1544 }
1545 if (!found_one)
1546 buf->b_write_to_channel = FALSE;
1547}
1548
1549/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001550 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001551 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001552 */
1553 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001554invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1555 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001556{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001557 typval_T rettv;
1558 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001559
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001560 if (safe_to_invoke_callback == 0)
1561 EMSG("INTERNAL: Invoking callback when it is not safe");
1562
Bram Moolenaar77073442016-02-13 23:23:53 +01001563 argv[0].v_type = VAR_CHANNEL;
1564 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001565
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001566 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1567 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001568 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001569 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001570}
1571
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001572/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001573 * Return the first node from "channel"/"part" without removing it.
1574 * Returns NULL if there is nothing.
1575 */
1576 readq_T *
1577channel_peek(channel_T *channel, int part)
1578{
1579 readq_T *head = &channel->ch_part[part].ch_head;
1580
1581 return head->rq_next;
1582}
1583
1584/*
1585 * Return a pointer to the first NL in "node".
1586 * Skips over NUL characters.
1587 * Returns NULL if there is no NL.
1588 */
1589 char_u *
1590channel_first_nl(readq_T *node)
1591{
1592 char_u *buffer = node->rq_buffer;
1593 long_u i;
1594
1595 for (i = 0; i < node->rq_buflen; ++i)
1596 if (buffer[i] == NL)
1597 return buffer + i;
1598 return NULL;
1599}
1600
1601/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001602 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001603 * The caller must free it.
1604 * Returns NULL if there is nothing.
1605 */
1606 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001607channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001608{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001611 char_u *p;
1612
Bram Moolenaar77073442016-02-13 23:23:53 +01001613 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001614 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001616 p = node->rq_buffer;
1617 head->rq_next = node->rq_next;
1618 if (node->rq_next == NULL)
1619 head->rq_prev = NULL;
1620 else
1621 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001622 vim_free(node);
1623 return p;
1624}
1625
1626/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001627 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001628 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001629 */
1630 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001631channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001632{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001633 readq_T *head = &channel->ch_part[part].ch_head;
1634 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001635 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001636 char_u *res;
1637 char_u *p;
1638
1639 /* If there is only one buffer just get that one. */
1640 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1641 return channel_get(channel, part);
1642
1643 /* Concatenate everything into one buffer. */
1644 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001645 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001646 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001647 if (res == NULL)
1648 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001649 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001650 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001651 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001652 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001653 p += node->rq_buflen;
1654 }
1655 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001656
1657 /* Free all buffers */
1658 do
1659 {
1660 p = channel_get(channel, part);
1661 vim_free(p);
1662 } while (p != NULL);
1663
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001664 /* turn all NUL into NL */
1665 while (len > 0)
1666 {
1667 --len;
1668 if (res[len] == NUL)
1669 res[len] = NL;
1670 }
1671
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001672 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673}
1674
1675/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001676 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001677 * Caller must check these bytes are available.
1678 */
1679 void
1680channel_consume(channel_T *channel, int part, int len)
1681{
1682 readq_T *head = &channel->ch_part[part].ch_head;
1683 readq_T *node = head->rq_next;
1684 char_u *buf = node->rq_buffer;
1685
1686 mch_memmove(buf, buf + len, node->rq_buflen - len);
1687 node->rq_buflen -= len;
1688}
1689
1690/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001691 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001692 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001693 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001694 */
1695 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001696channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001698 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001699 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001700 readq_T *last_node;
1701 readq_T *n;
1702 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001704 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001705
Bram Moolenaar77073442016-02-13 23:23:53 +01001706 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001707 return FAIL;
1708
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 last_node = node->rq_next;
1710 len = node->rq_buflen + last_node->rq_buflen + 1;
1711 if (want_nl)
1712 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001713 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001714 {
1715 last_node = last_node->rq_next;
1716 len += last_node->rq_buflen;
1717 }
1718
1719 p = newbuf = alloc(len);
1720 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001722 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001723 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001724 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 node->rq_buffer = newbuf;
1726 for (n = node; n != last_node; )
1727 {
1728 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001729 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001730 p += n->rq_buflen;
1731 vim_free(n->rq_buffer);
1732 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001733 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734
1735 /* dispose of the collapsed nodes and their buffers */
1736 for (n = node->rq_next; n != last_node; )
1737 {
1738 n = n->rq_next;
1739 vim_free(n->rq_prev);
1740 }
1741 node->rq_next = last_node->rq_next;
1742 if (last_node->rq_next == NULL)
1743 head->rq_prev = node;
1744 else
1745 last_node->rq_next->rq_prev = node;
1746 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001747 return OK;
1748}
1749
1750/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001751 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001752 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001753 * Returns OK or FAIL.
1754 */
1755 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001756channel_save(channel_T *channel, int part, char_u *buf, int len,
1757 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001758{
1759 readq_T *node;
1760 readq_T *head = &channel->ch_part[part].ch_head;
1761 char_u *p;
1762 int i;
1763
1764 node = (readq_T *)alloc(sizeof(readq_T));
1765 if (node == NULL)
1766 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001767 /* A NUL is added at the end, because netbeans code expects that.
1768 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 node->rq_buffer = alloc(len + 1);
1770 if (node->rq_buffer == NULL)
1771 {
1772 vim_free(node);
1773 return FAIL; /* out of memory */
1774 }
1775
1776 if (channel->ch_part[part].ch_mode == MODE_NL)
1777 {
1778 /* Drop any CR before a NL. */
1779 p = node->rq_buffer;
1780 for (i = 0; i < len; ++i)
1781 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1782 *p++ = buf[i];
1783 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001784 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785 }
1786 else
1787 {
1788 mch_memmove(node->rq_buffer, buf, len);
1789 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001790 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001791 }
1792
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001793 if (prepend)
1794 {
1795 /* preend node to the head of the queue */
1796 node->rq_next = head->rq_next;
1797 node->rq_prev = NULL;
1798 if (head->rq_next == NULL)
1799 head->rq_prev = node;
1800 else
1801 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001803 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001804 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001805 {
1806 /* append node to the tail of the queue */
1807 node->rq_next = NULL;
1808 node->rq_prev = head->rq_prev;
1809 if (head->rq_prev == NULL)
1810 head->rq_next = node;
1811 else
1812 head->rq_prev->rq_next = node;
1813 head->rq_prev = node;
1814 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001815
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001816 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001817 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001818 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001819 fprintf(log_fd, "'");
1820 if (fwrite(buf, len, 1, log_fd) != 1)
1821 return FAIL;
1822 fprintf(log_fd, "'\n");
1823 }
1824 return OK;
1825}
1826
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001827 static int
1828channel_fill(js_read_T *reader)
1829{
1830 channel_T *channel = (channel_T *)reader->js_cookie;
1831 int part = reader->js_cookie_arg;
1832 char_u *next = channel_get(channel, part);
1833 int unused;
1834 int len;
1835 char_u *p;
1836
1837 if (next == NULL)
1838 return FALSE;
1839
1840 unused = reader->js_end - reader->js_buf - reader->js_used;
1841 if (unused > 0)
1842 {
1843 /* Prepend unused text. */
1844 len = (int)STRLEN(next);
1845 p = alloc(unused + len + 1);
1846 if (p == NULL)
1847 {
1848 vim_free(next);
1849 return FALSE;
1850 }
1851 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1852 mch_memmove(p + unused, next, len + 1);
1853 vim_free(next);
1854 next = p;
1855 }
1856
1857 vim_free(reader->js_buf);
1858 reader->js_buf = next;
1859 reader->js_used = 0;
1860 return TRUE;
1861}
1862
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001864 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001865 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001866 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001867 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001868 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001869channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001870{
1871 js_read_T reader;
1872 typval_T listtv;
1873 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001874 chanpart_T *chanpart = &channel->ch_part[part];
1875 jsonq_T *head = &chanpart->ch_json_head;
1876 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001877 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001878
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001879 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001880 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001881
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001882 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001883 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001884 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001885 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001886 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001887
1888 /* When a message is incomplete we wait for a short while for more to
1889 * arrive. After the delay drop the input, otherwise a truncated string
1890 * or list will make us hang. */
1891 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001892 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001893 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001895 /* Only accept the response when it is a list with at least two
1896 * items. */
1897 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001898 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001899 if (listtv.v_type != VAR_LIST)
1900 ch_error(channel, "Did not receive a list, discarding");
1901 else
1902 ch_errorn(channel, "Expected list with two items, got %d",
1903 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001904 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001905 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001906 else
1907 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001908 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1909 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001910 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001911 else
1912 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001913 item->jq_value = alloc_tv();
1914 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001915 {
1916 vim_free(item);
1917 clear_tv(&listtv);
1918 }
1919 else
1920 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001921 *item->jq_value = listtv;
1922 item->jq_prev = head->jq_prev;
1923 head->jq_prev = item;
1924 item->jq_next = NULL;
1925 if (item->jq_prev == NULL)
1926 head->jq_next = item;
1927 else
1928 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001929 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001930 }
1931 }
1932 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001933
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001934 if (status == OK)
1935 chanpart->ch_waiting = FALSE;
1936 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001937 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001938 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001939 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001940 /* First time encountering incomplete message, set a deadline of
1941 * 100 msec. */
1942 ch_log(channel, "Incomplete message - wait for more");
1943 reader.js_used = 0;
1944 chanpart->ch_waiting = TRUE;
1945#ifdef WIN32
1946 chanpart->ch_deadline = GetTickCount() + 100L;
1947#else
1948 gettimeofday(&chanpart->ch_deadline, NULL);
1949 chanpart->ch_deadline.tv_usec += 100 * 1000;
1950 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1951 {
1952 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1953 ++chanpart->ch_deadline.tv_sec;
1954 }
1955#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001956 }
1957 else
1958 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001959 int timeout;
1960#ifdef WIN32
1961 timeout = GetTickCount() > chanpart->ch_deadline;
1962#else
1963 {
1964 struct timeval now_tv;
1965
1966 gettimeofday(&now_tv, NULL);
1967 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1968 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1969 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1970 }
1971#endif
1972 if (timeout)
1973 {
1974 status = FAIL;
1975 chanpart->ch_waiting = FALSE;
1976 }
1977 else
1978 {
1979 reader.js_used = 0;
1980 ch_log(channel, "still waiting on incomplete message");
1981 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001982 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001983 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001984
1985 if (status == FAIL)
1986 {
1987 ch_error(channel, "Decoding failed - discarding input");
1988 ret = FALSE;
1989 chanpart->ch_waiting = FALSE;
1990 }
1991 else if (reader.js_buf[reader.js_used] != NUL)
1992 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001993 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001995 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1996 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001997 ret = status == MAYBE ? FALSE: TRUE;
1998 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001999 else
2000 ret = FALSE;
2001
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002002 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002003 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002004}
2005
2006/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002007 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002008 */
2009 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002010remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002011{
Bram Moolenaar77073442016-02-13 23:23:53 +01002012 if (node->cq_prev == NULL)
2013 head->cq_next = node->cq_next;
2014 else
2015 node->cq_prev->cq_next = node->cq_next;
2016 if (node->cq_next == NULL)
2017 head->cq_prev = node->cq_prev;
2018 else
2019 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002020}
2021
2022/*
2023 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002024 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002025 */
2026 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002027remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002028{
Bram Moolenaar77073442016-02-13 23:23:53 +01002029 if (node->jq_prev == NULL)
2030 head->jq_next = node->jq_next;
2031 else
2032 node->jq_prev->jq_next = node->jq_next;
2033 if (node->jq_next == NULL)
2034 head->jq_prev = node->jq_prev;
2035 else
2036 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002037 vim_free(node);
2038}
2039
2040/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002041 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002042 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002043 * When "id" is zero or negative jut get the first message. But not the one
2044 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002045 * Return OK when found and return the value in "rettv".
2046 * Return FAIL otherwise.
2047 */
2048 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002049channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002050{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002051 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002052 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002053
Bram Moolenaar77073442016-02-13 23:23:53 +01002054 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002055 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002056 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057 typval_T *tv = &l->lv_first->li_tv;
2058
2059 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002060 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002061 || tv->vval.v_number == 0
2062 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002063 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002064 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002065 if (tv->v_type == VAR_NUMBER)
2066 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002067 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002068 return OK;
2069 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002070 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002071 }
2072 return FAIL;
2073}
2074
Bram Moolenaarece61b02016-02-20 21:39:05 +01002075#define CH_JSON_MAX_ARGS 4
2076
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002077/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002078 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002079 * "argv[0]" is the command string.
2080 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002081 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002082 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002083channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002084{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002085 char_u *cmd = argv[0].vval.v_string;
2086 char_u *arg;
2087 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002088
Bram Moolenaarece61b02016-02-20 21:39:05 +01002089 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002090 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002091 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002092 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002093 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002094 return;
2095 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002096 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002097 if (arg == NULL)
2098 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002099
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002100 if (STRCMP(cmd, "ex") == 0)
2101 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002102 int save_called_emsg = called_emsg;
2103
2104 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002105 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002106 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002107 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002108 --emsg_silent;
2109 if (called_emsg)
2110 ch_logs(channel, "Ex command error: '%s'",
2111 (char *)get_vim_var_str(VV_ERRMSG));
2112 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002113 }
2114 else if (STRCMP(cmd, "normal") == 0)
2115 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002116 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002117
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002118 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002119 ea.arg = arg;
2120 ea.addr_count = 0;
2121 ea.forceit = TRUE; /* no mapping */
2122 ex_normal(&ea);
2123 }
2124 else if (STRCMP(cmd, "redraw") == 0)
2125 {
2126 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002127
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002128 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002129 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002130 ex_redraw(&ea);
2131 showruler(FALSE);
2132 setcursor();
2133 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002134#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002135 if (gui.in_use)
2136 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002137 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002138 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002139 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002140#endif
2141 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002142 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002143 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002144 int is_call = cmd[0] == 'c';
2145 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002146
Bram Moolenaarece61b02016-02-20 21:39:05 +01002147 if (argv[id_idx].v_type != VAR_UNKNOWN
2148 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002149 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002150 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002151 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002152 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002153 }
2154 else if (is_call && argv[2].v_type != VAR_LIST)
2155 {
2156 ch_error(channel, "third argument for call must be a list");
2157 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002158 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002159 }
2160 else
2161 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002162 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002163 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002164 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002165 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002166
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002167 /* Don't pollute the display with errors. */
2168 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002169 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002170 {
2171 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002173 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002174 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002175 {
2176 ch_logs(channel, "Calling '%s'", (char *)arg);
2177 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2178 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002179 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002180
2181 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002182 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002183 int id = argv[id_idx].vval.v_number;
2184
Bram Moolenaar55fab432016-02-07 16:53:13 +01002185 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002186 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002187 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002188 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002189 /* If evaluation failed or the result can't be encoded
2190 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002191 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192 err_tv.v_type = VAR_STRING;
2193 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002194 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002195 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002196 if (json != NULL)
2197 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 channel_send(channel,
2199 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002200 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002201 vim_free(json);
2202 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002204 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002205 if (tv == &res_tv)
2206 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002207 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002208 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002209 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002210 }
2211 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002212 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002213 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002214 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002215 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002216}
2217
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002218/*
2219 * Invoke the callback at "cbhead".
2220 * Does not redraw but sets channel_need_redraw.
2221 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002222 static void
2223invoke_one_time_callback(
2224 channel_T *channel,
2225 cbq_T *cbhead,
2226 cbq_T *item,
2227 typval_T *argv)
2228{
2229 ch_logs(channel, "Invoking one-time callback %s",
2230 (char *)item->cq_callback);
2231 /* Remove the item from the list first, if the callback
2232 * invokes ch_close() the list will be cleared. */
2233 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002234 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002235 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002236 vim_free(item);
2237}
2238
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002239 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002240append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002241{
2242 buf_T *save_curbuf = curbuf;
2243 linenr_T lnum = buffer->b_ml.ml_line_count;
2244 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002245 chanpart_T *ch_part = &channel->ch_part[part];
2246 int save_p_ma = buffer->b_p_ma;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002247 int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002248
2249 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2250 {
2251 if (!ch_part->ch_nomod_error)
2252 {
2253 ch_error(channel, "Buffer is not modifiable, cannot append");
2254 ch_part->ch_nomod_error = TRUE;
2255 }
2256 return;
2257 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002258
2259 /* If the buffer is also used as input insert above the last
2260 * line. Don't write these lines. */
2261 if (save_write_to)
2262 {
2263 --lnum;
2264 buffer->b_write_to_channel = FALSE;
2265 }
2266
2267 /* Append to the buffer */
2268 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2269
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002270 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002271 curbuf = buffer;
2272 u_sync(TRUE);
2273 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002274 ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002275
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002276 if (empty)
2277 {
2278 /* The buffer is empty, replace the first (dummy) line. */
2279 ml_replace(lnum, msg, TRUE);
2280 lnum = 0;
2281 }
2282 else
2283 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002284 appended_lines_mark(lnum, 1L);
2285 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002286 if (ch_part->ch_nomodifiable)
2287 buffer->b_p_ma = FALSE;
2288 else
2289 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002290
2291 if (buffer->b_nwindows > 0)
2292 {
2293 win_T *wp;
2294 win_T *save_curwin;
2295
2296 FOR_ALL_WINDOWS(wp)
2297 {
2298 if (wp->w_buffer == buffer
2299 && (save_write_to
2300 ? wp->w_cursor.lnum == lnum + 1
2301 : (wp->w_cursor.lnum == lnum
2302 && wp->w_cursor.col == 0)))
2303 {
2304 ++wp->w_cursor.lnum;
2305 save_curwin = curwin;
2306 curwin = wp;
2307 curbuf = curwin->w_buffer;
2308 scroll_cursor_bot(0, FALSE);
2309 curwin = save_curwin;
2310 curbuf = curwin->w_buffer;
2311 }
2312 }
2313 redraw_buf_later(buffer, VALID);
2314 channel_need_redraw = TRUE;
2315 }
2316
2317 if (save_write_to)
2318 {
2319 channel_T *ch;
2320
2321 /* Find channels reading from this buffer and adjust their
2322 * next-to-read line number. */
2323 buffer->b_write_to_channel = TRUE;
2324 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2325 {
2326 chanpart_T *in_part = &ch->ch_part[PART_IN];
2327
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002328 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002329 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2330 }
2331 }
2332}
2333
Bram Moolenaar437905c2016-04-26 19:01:05 +02002334 static void
2335drop_messages(channel_T *channel, int part)
2336{
2337 char_u *msg;
2338
2339 while ((msg = channel_get(channel, part)) != NULL)
2340 {
2341 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2342 vim_free(msg);
2343 }
2344}
2345
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002346/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002347 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002348 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002349 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002350 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002351 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002352may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002353{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002354 char_u *msg = NULL;
2355 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002356 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002357 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002358 chanpart_T *ch_part = &channel->ch_part[part];
2359 ch_mode_T ch_mode = ch_part->ch_mode;
2360 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002361 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002362 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002363 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002364 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002365 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002366
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002367 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002368 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002369 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002370
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002371 /* Use a message-specific callback, part callback or channel callback */
2372 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2373 if (cbitem->cq_seq_nr == 0)
2374 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002375 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002376 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002377 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002378 partial = cbitem->cq_partial;
2379 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002380 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002381 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002382 callback = ch_part->ch_callback;
2383 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002384 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002385 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002386 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002387 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002388 partial = channel->ch_partial;
2389 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002390
Bram Moolenaarec68a992016-10-03 21:37:41 +02002391 buffer = ch_part->ch_bufref.br_buf;
2392 if (buffer != NULL && !bufref_valid(&ch_part->ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002393 {
2394 /* buffer was wiped out */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002395 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002396 buffer = NULL;
2397 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002398
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002399 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002400 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002401 listitem_T *item;
2402 int argc = 0;
2403
Bram Moolenaard7ece102016-02-02 23:23:02 +01002404 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002405 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002406 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002407 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002408 channel_parse_json(channel, part);
2409 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002410 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002411 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002412
Bram Moolenaarece61b02016-02-20 21:39:05 +01002413 for (item = listtv->vval.v_list->lv_first;
2414 item != NULL && argc < CH_JSON_MAX_ARGS;
2415 item = item->li_next)
2416 argv[argc++] = item->li_tv;
2417 while (argc < CH_JSON_MAX_ARGS)
2418 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002419
Bram Moolenaarece61b02016-02-20 21:39:05 +01002420 if (argv[0].v_type == VAR_STRING)
2421 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002422 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002423 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002424 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002425 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002426 }
2427
Bram Moolenaarece61b02016-02-20 21:39:05 +01002428 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002429 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002430 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002431 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002432 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002433 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002434 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002435 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002436 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002437 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002438 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002439 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002440 return FALSE;
2441 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002442 else
2443 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002444 /* If there is no callback or buffer drop the message. */
2445 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002446 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002447 /* If there is a close callback it may use ch_read() to get the
2448 * messages. */
2449 if (channel->ch_close_cb == NULL)
2450 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002451 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002452 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002453
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002454 if (ch_mode == MODE_NL)
2455 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002456 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002457 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002458 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002459
2460 /* See if we have a message ending in NL in the first buffer. If
2461 * not try to concatenate the first and the second buffer. */
2462 while (TRUE)
2463 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002464 node = channel_peek(channel, part);
2465 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002466 if (nl != NULL)
2467 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002468 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002469 {
2470 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2471 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002472 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002473 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002474 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002475 buf = node->rq_buffer;
2476
Bram Moolenaarec68a992016-10-03 21:37:41 +02002477 if (nl == NULL)
2478 {
2479 /* Flush remaining message that is missing a NL. */
2480 buf = vim_realloc(buf, node->rq_buflen + 1);
2481 if (buf == NULL)
2482 return FALSE;
2483 node->rq_buffer = buf;
2484 nl = buf + node->rq_buflen++;
2485 *nl = NUL;
2486 }
2487
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002488 /* Convert NUL to NL, the internal representation. */
2489 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2490 if (*p == NUL)
2491 *p = NL;
2492
2493 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002494 {
2495 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002496 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002497 *nl = NUL;
2498 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002499 else
2500 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002501 /* Copy the message into allocated memory (excluding the NL)
2502 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002503 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002504 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002505 }
2506 }
2507 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002508 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002509 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002510 * get everything we have.
2511 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002512 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002513 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002514
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002515 if (msg == NULL)
2516 return FALSE; /* out of memory (and avoids Coverity warning) */
2517
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002518 argv[1].v_type = VAR_STRING;
2519 argv[1].vval.v_string = msg;
2520 }
2521
Bram Moolenaara07fec92016-02-05 21:04:08 +01002522 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002523 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002524 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002525
2526 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002527 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002528 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002529 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002530 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002531 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002532 break;
2533 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002534 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002535 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002536 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002537 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002538 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002539 if (buffer != NULL)
2540 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002541 if (msg == NULL)
2542 /* JSON or JS mode: re-encode the message. */
2543 msg = json_encode(listtv, ch_mode);
2544 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002545 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002546 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002547
Bram Moolenaar187db502016-02-27 14:44:26 +01002548 if (callback != NULL)
2549 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002550 if (cbitem != NULL)
2551 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2552 else
2553 {
2554 /* invoke the channel callback */
2555 ch_logs(channel, "Invoking channel callback %s",
2556 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002557 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002558 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002559 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002560 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002561 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002562 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002563
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002564 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002565 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002566 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002567
2568 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002569}
2570
2571/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002572 * Return TRUE when channel "channel" is open for writing to.
2573 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002574 */
2575 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002576channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002577{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002578 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002579 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002580}
2581
2582/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002583 * Return TRUE when channel "channel" is open for reading or writing.
2584 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002585 */
2586 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002587channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002588{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002589 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002590 || channel->CH_IN_FD != INVALID_FD
2591 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002592 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002593}
2594
2595/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002596 * Return TRUE if "channel" has JSON or other typeahead.
2597 */
2598 static int
2599channel_has_readahead(channel_T *channel, int part)
2600{
2601 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2602
2603 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2604 {
2605 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2606 jsonq_T *item = head->jq_next;
2607
2608 return item != NULL;
2609 }
2610 return channel_peek(channel, part) != NULL;
2611}
2612
2613/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002614 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002615 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002616 */
2617 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002618channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002619{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002620 int part;
2621 int has_readahead = FALSE;
2622
Bram Moolenaar77073442016-02-13 23:23:53 +01002623 if (channel == NULL)
2624 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002625 if (req_part == PART_OUT)
2626 {
2627 if (channel->CH_OUT_FD != INVALID_FD)
2628 return "open";
2629 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002630 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002631 }
2632 else if (req_part == PART_ERR)
2633 {
2634 if (channel->CH_ERR_FD != INVALID_FD)
2635 return "open";
2636 if (channel_has_readahead(channel, PART_ERR))
2637 has_readahead = TRUE;
2638 }
2639 else
2640 {
2641 if (channel_is_open(channel))
2642 return "open";
2643 for (part = PART_SOCK; part <= PART_ERR; ++part)
2644 if (channel_has_readahead(channel, part))
2645 {
2646 has_readahead = TRUE;
2647 break;
2648 }
2649 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002650
2651 if (has_readahead)
2652 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002653 return "closed";
2654}
2655
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002656 static void
2657channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2658{
2659 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002660 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002661 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002662 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002663 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002664
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002665 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002666 STRCAT(namebuf, "_");
2667 tail = STRLEN(namebuf);
2668
2669 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002670 if (chanpart->ch_fd != INVALID_FD)
2671 status = "open";
2672 else if (channel_has_readahead(channel, part))
2673 status = "buffered";
2674 else
2675 status = "closed";
2676 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002677
2678 STRCPY(namebuf + tail, "mode");
2679 switch (chanpart->ch_mode)
2680 {
2681 case MODE_NL: s = "NL"; break;
2682 case MODE_RAW: s = "RAW"; break;
2683 case MODE_JSON: s = "JSON"; break;
2684 case MODE_JS: s = "JS"; break;
2685 }
2686 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2687
2688 STRCPY(namebuf + tail, "io");
2689 if (part == PART_SOCK)
2690 s = "socket";
2691 else switch (chanpart->ch_io)
2692 {
2693 case JIO_NULL: s = "null"; break;
2694 case JIO_PIPE: s = "pipe"; break;
2695 case JIO_FILE: s = "file"; break;
2696 case JIO_BUFFER: s = "buffer"; break;
2697 case JIO_OUT: s = "out"; break;
2698 }
2699 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2700
2701 STRCPY(namebuf + tail, "timeout");
2702 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2703}
2704
2705 void
2706channel_info(channel_T *channel, dict_T *dict)
2707{
2708 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002709 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002710
2711 if (channel->ch_hostname != NULL)
2712 {
2713 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2714 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2715 channel_part_info(channel, dict, "sock", PART_SOCK);
2716 }
2717 else
2718 {
2719 channel_part_info(channel, dict, "out", PART_OUT);
2720 channel_part_info(channel, dict, "err", PART_ERR);
2721 channel_part_info(channel, dict, "in", PART_IN);
2722 }
2723}
2724
Bram Moolenaar77073442016-02-13 23:23:53 +01002725/*
2726 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002727 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002728 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002729 */
2730 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002731channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002732{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002733 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002734
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002735#ifdef FEAT_GUI
2736 channel_gui_unregister(channel);
2737#endif
2738
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002739 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002740 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002741 sock_close(channel->CH_SOCK_FD);
2742 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002743 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002744 may_close_part(&channel->CH_IN_FD);
2745 may_close_part(&channel->CH_OUT_FD);
2746 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002747
Bram Moolenaar8b374212016-02-24 20:43:06 +01002748 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002749 {
2750 typval_T argv[1];
2751 typval_T rettv;
2752 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002753 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002754
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002755 /* Invoke callbacks before the close callback, since it's weird to
2756 * first invoke the close callback. Increment the refcount to avoid
2757 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002758 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002759 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002760 for (part = PART_SOCK; part <= PART_ERR; ++part)
2761 while (may_invoke_callback(channel, part))
2762 ;
2763
2764 /* Invoke the close callback, if still set. */
2765 if (channel->ch_close_cb != NULL)
2766 {
2767 ch_logs(channel, "Invoking close callback %s",
2768 (char *)channel->ch_close_cb);
2769 argv[0].v_type = VAR_CHANNEL;
2770 argv[0].vval.v_channel = channel;
2771 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002772 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002773 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002774 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002775 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002776 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002777
2778 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002779 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002780 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002781 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002782
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002783 --channel->ch_refcount;
2784
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002785 if (channel_need_redraw)
2786 {
2787 channel_need_redraw = FALSE;
2788 redraw_after_callback();
2789 }
2790
Bram Moolenaar437905c2016-04-26 19:01:05 +02002791 /* any remaining messages are useless now */
2792 for (part = PART_SOCK; part <= PART_ERR; ++part)
2793 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002794 }
2795
2796 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002797}
2798
Bram Moolenaard04a0202016-01-26 23:30:18 +01002799/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002800 * Close the "in" part channel "channel".
2801 */
2802 void
2803channel_close_in(channel_T *channel)
2804{
2805 may_close_part(&channel->CH_IN_FD);
2806}
2807
2808/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002809 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002810 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002811 static void
2812channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002813{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002814 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2815 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002816
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002817 while (channel_peek(channel, part) != NULL)
2818 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002819
2820 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002821 {
2822 cbq_T *node = cb_head->cq_next;
2823
2824 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002825 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002826 vim_free(node);
2827 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002828
2829 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002830 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002831 free_tv(json_head->jq_next->jq_value);
2832 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002833 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002834
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002835 free_callback(channel->ch_part[part].ch_callback,
2836 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002837 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002838 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002839}
2840
2841/*
2842 * Clear all the read buffers on "channel".
2843 */
2844 void
2845channel_clear(channel_T *channel)
2846{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002847 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002848 vim_free(channel->ch_hostname);
2849 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002850 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002851 channel_clear_one(channel, PART_OUT);
2852 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002853 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002854 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002855 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002856 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002857 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002858 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002859 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002860}
2861
Bram Moolenaar77073442016-02-13 23:23:53 +01002862#if defined(EXITFREE) || defined(PROTO)
2863 void
2864channel_free_all(void)
2865{
2866 channel_T *channel;
2867
Bram Moolenaard6051b52016-02-28 15:49:03 +01002868 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002869 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2870 channel_clear(channel);
2871}
2872#endif
2873
2874
Bram Moolenaar715d2852016-04-30 17:06:31 +02002875/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002876#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002877
2878/* Buffer size for reading incoming messages. */
2879#define MAXMSGSIZE 4096
2880
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002881#if defined(HAVE_SELECT)
2882/*
2883 * Add write fds where we are waiting for writing to be possible.
2884 */
2885 static int
2886channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2887{
2888 int maxfd = maxfd_arg;
2889 channel_T *ch;
2890
2891 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2892 {
2893 chanpart_T *in_part = &ch->ch_part[PART_IN];
2894
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002895 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002896 {
2897 FD_SET((int)in_part->ch_fd, wfds);
2898 if ((int)in_part->ch_fd >= maxfd)
2899 maxfd = (int)in_part->ch_fd + 1;
2900 }
2901 }
2902 return maxfd;
2903}
2904#else
2905/*
2906 * Add write fds where we are waiting for writing to be possible.
2907 */
2908 static int
2909channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2910{
2911 int nfd = nfd_in;
2912 channel_T *ch;
2913
2914 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2915 {
2916 chanpart_T *in_part = &ch->ch_part[PART_IN];
2917
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002918 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002919 {
2920 in_part->ch_poll_idx = nfd;
2921 fds[nfd].fd = in_part->ch_fd;
2922 fds[nfd].events = POLLOUT;
2923 ++nfd;
2924 }
2925 else
2926 in_part->ch_poll_idx = -1;
2927 }
2928 return nfd;
2929}
2930#endif
2931
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002932typedef enum {
2933 CW_READY,
2934 CW_NOT_READY,
2935 CW_ERROR
2936} channel_wait_result;
2937
Bram Moolenaard04a0202016-01-26 23:30:18 +01002938/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002939 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002940 * Return CW_READY when there is something to read.
2941 * Return CW_NOT_READY when there is nothing to read.
2942 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002943 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002944 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002945channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002946{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002947 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002948 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002949
Bram Moolenaard8070362016-02-15 21:56:54 +01002950# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002951 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002952 {
2953 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002954 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002955 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002956 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002957
2958 /* reading from a pipe, not a socket */
2959 while (TRUE)
2960 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002961 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2962
2963 if (r && nread > 0)
2964 return CW_READY;
2965 if (r == 0)
2966 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002967
2968 /* perhaps write some buffer lines */
2969 channel_write_any_lines();
2970
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002971 sleep_time = deadline - GetTickCount();
2972 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002973 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002974 /* Wait for a little while. Very short at first, up to 10 msec
2975 * after looping a few times. */
2976 if (sleep_time > delay)
2977 sleep_time = delay;
2978 Sleep(sleep_time);
2979 delay = delay * 2;
2980 if (delay > 10)
2981 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002982 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002983 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002984 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002985#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002986 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002987#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002988 struct timeval tval;
2989 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002990 fd_set wfds;
2991 int ret;
2992 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002993
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002994 tval.tv_sec = timeout / 1000;
2995 tval.tv_usec = (timeout % 1000) * 1000;
2996 for (;;)
2997 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002998 FD_ZERO(&rfds);
2999 FD_SET((int)fd, &rfds);
3000
3001 /* Write lines to a pipe when a pipe can be written to. Need to
3002 * set this every time, some buffers may be done. */
3003 maxfd = (int)fd + 1;
3004 FD_ZERO(&wfds);
3005 maxfd = channel_fill_wfds(maxfd, &wfds);
3006
3007 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003008# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003009 SOCK_ERRNO;
3010 if (ret == -1 && errno == EINTR)
3011 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003012# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003013 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003014 {
3015 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003016 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003017 channel_write_any_lines();
3018 continue;
3019 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003020 break;
3021 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003022#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003023 for (;;)
3024 {
3025 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3026 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003027
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003028 fds[0].fd = fd;
3029 fds[0].events = POLLIN;
3030 nfd = channel_fill_poll_write(nfd, fds);
3031 if (poll(fds, nfd, timeout) > 0)
3032 {
3033 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003034 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003035 channel_write_any_lines();
3036 continue;
3037 }
3038 break;
3039 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003040#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003041 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003042 return CW_NOT_READY;
3043}
3044
3045 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02003046channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003047{
3048 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003049 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
3050 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003051
3052 /* Queue a "DETACH" netbeans message in the command queue in order to
3053 * terminate the netbeans session later. Do not end the session here
3054 * directly as we may be running in the context of a call to
3055 * netbeans_parse_messages():
3056 * netbeans_parse_messages
3057 * -> autocmd triggered while processing the netbeans cmd
3058 * -> ui_breakcheck
3059 * -> gui event loop or select loop
3060 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003061 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003062 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003063 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003064 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003065 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3066
3067 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003068 * died. Don't close the channel right away, it may be the wrong moment
3069 * to invoke callbacks. */
3070 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003071
3072#ifdef FEAT_GUI
3073 /* Stop listening to GUI events right away. */
3074 channel_gui_unregister(channel);
3075#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003076}
3077
3078 static void
3079channel_close_now(channel_T *channel)
3080{
3081 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003082 channel_close(channel, TRUE);
3083 if (channel->ch_nb_close_cb != NULL)
3084 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003085}
3086
3087/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003088 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003089 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003090 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003091 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003092 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003093channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003094{
3095 static char_u *buf = NULL;
3096 int len = 0;
3097 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003098 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003099 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003100
Bram Moolenaar5850a762016-05-27 19:59:48 +02003101 /* If we detected a read error don't try reading again. */
3102 if (channel->ch_to_be_closed)
3103 return;
3104
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003105 fd = channel->ch_part[part].ch_fd;
3106 if (fd == INVALID_FD)
3107 {
3108 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003109 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003110 }
3111 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003112
3113 /* Allocate a buffer to read into. */
3114 if (buf == NULL)
3115 {
3116 buf = alloc(MAXMSGSIZE);
3117 if (buf == NULL)
3118 return; /* out of memory! */
3119 }
3120
3121 /* Keep on reading for as long as there is something to read.
3122 * Use select() or poll() to avoid blocking on a message that is exactly
3123 * MAXMSGSIZE long. */
3124 for (;;)
3125 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003126 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003127 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003128 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003129 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003130 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003131 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003132 if (len <= 0)
3133 break; /* error or nothing more to read */
3134
3135 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003136 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003137 readlen += len;
3138 if (len < MAXMSGSIZE)
3139 break; /* did read everything that's available */
3140 }
3141
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003142 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003143 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003144 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003145
3146#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003147 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003148 if (CH_HAS_GUI && gtk_main_level() > 0)
3149 gtk_main_quit();
3150#endif
3151}
3152
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003153/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003154 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003155 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003156 * Returns what was read in allocated memory.
3157 * Returns NULL in case of error or timeout.
3158 */
3159 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003160channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003161{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003162 char_u *buf;
3163 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003164 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003165 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003166 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003167 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003168
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003169 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003170 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003171
3172 while (TRUE)
3173 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003174 node = channel_peek(channel, part);
3175 if (node != NULL)
3176 {
3177 if (mode == MODE_RAW || (mode == MODE_NL
3178 && channel_first_nl(node) != NULL))
3179 /* got a complete message */
3180 break;
3181 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3182 continue;
3183 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003184
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003185 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003186 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003187 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003188 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003189 {
3190 ch_log(channel, "Timed out");
3191 return NULL;
3192 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003193 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003194 }
3195
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003196 if (mode == MODE_RAW)
3197 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003198 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003199 }
3200 else
3201 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003202 char_u *p;
3203
3204 buf = node->rq_buffer;
3205 nl = channel_first_nl(node);
3206
3207 /* Convert NUL to NL, the internal representation. */
3208 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3209 if (*p == NUL)
3210 *p = NL;
3211
3212 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003213 {
3214 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003215 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003216 *nl = NUL;
3217 }
3218 else
3219 {
3220 /* Copy the message into allocated memory and remove it from the
3221 * buffer. */
3222 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003223 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003224 }
3225 }
3226 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003227 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003228 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003229}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003230
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003231/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003232 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003233 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003234 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003235 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003236 */
3237 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003238channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003239 channel_T *channel,
3240 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003241 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003242 int id,
3243 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003244{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003245 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003246 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003247 int timeout;
3248 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003249
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003250 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003251 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003252 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003253 for (;;)
3254 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003255 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003256
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003257 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003258 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003259 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003260 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003261 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003262 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003263
Bram Moolenaard7ece102016-02-02 23:23:02 +01003264 if (!more)
3265 {
3266 /* Handle any other messages in the queue. If done some more
3267 * messages may have arrived. */
3268 if (channel_parse_messages())
3269 continue;
3270
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003271 /* Wait for up to the timeout. If there was an incomplete message
3272 * use the deadline for that. */
3273 timeout = timeout_arg;
3274 if (chanpart->ch_waiting)
3275 {
3276#ifdef WIN32
3277 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3278#else
3279 {
3280 struct timeval now_tv;
3281
3282 gettimeofday(&now_tv, NULL);
3283 timeout = (chanpart->ch_deadline.tv_sec
3284 - now_tv.tv_sec) * 1000
3285 + (chanpart->ch_deadline.tv_usec
3286 - now_tv.tv_usec) / 1000
3287 + 1;
3288 }
3289#endif
3290 if (timeout < 0)
3291 {
3292 /* Something went wrong, channel_parse_json() didn't
3293 * discard message. Cancel waiting. */
3294 chanpart->ch_waiting = FALSE;
3295 timeout = timeout_arg;
3296 }
3297 else if (timeout > timeout_arg)
3298 timeout = timeout_arg;
3299 }
3300 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003301 if (fd == INVALID_FD
3302 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003303 {
3304 if (timeout == timeout_arg)
3305 {
3306 if (fd != INVALID_FD)
3307 ch_log(channel, "Timed out");
3308 break;
3309 }
3310 }
3311 else
3312 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003313 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003314 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003315 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003316 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003317}
3318
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003319/*
3320 * Common for ch_read() and ch_readraw().
3321 */
3322 void
3323common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3324{
3325 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003326 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003327 jobopt_T opt;
3328 int mode;
3329 int timeout;
3330 int id = -1;
3331 typval_T *listtv = NULL;
3332
3333 /* return an empty string by default */
3334 rettv->v_type = VAR_STRING;
3335 rettv->vval.v_string = NULL;
3336
3337 clear_job_options(&opt);
3338 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3339 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003340 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003341
Bram Moolenaar437905c2016-04-26 19:01:05 +02003342 if (opt.jo_set & JO_PART)
3343 part = opt.jo_part;
3344 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003345 if (channel != NULL)
3346 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003347 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003348 part = channel_part_read(channel);
3349 mode = channel_get_mode(channel, part);
3350 timeout = channel_get_timeout(channel, part);
3351 if (opt.jo_set & JO_TIMEOUT)
3352 timeout = opt.jo_timeout;
3353
3354 if (raw || mode == MODE_RAW || mode == MODE_NL)
3355 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3356 else
3357 {
3358 if (opt.jo_set & JO_ID)
3359 id = opt.jo_id;
3360 channel_read_json_block(channel, part, timeout, id, &listtv);
3361 if (listtv != NULL)
3362 {
3363 *rettv = *listtv;
3364 vim_free(listtv);
3365 }
3366 else
3367 {
3368 rettv->v_type = VAR_SPECIAL;
3369 rettv->vval.v_number = VVAL_NONE;
3370 }
3371 }
3372 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003373
3374theend:
3375 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003376}
3377
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003378# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3379 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003380/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003381 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003382 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003383 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003384 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003385channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003386{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003387 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003388 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003389
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003390 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003391 for (channel = first_channel; channel != NULL;
3392 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003393 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003394 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003395 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003396 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003397 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003398 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003399 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003400 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003401 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003402}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003403# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003404
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003405# if defined(WIN32) || defined(PROTO)
3406/*
3407 * Check the channels for anything that is ready to be read.
3408 * The data is put in the read queue.
3409 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003410 void
3411channel_handle_events(void)
3412{
3413 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003414 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003415 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003416
3417 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3418 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003419 /* If we detected a read error don't try reading again. */
3420 if (channel->ch_to_be_closed)
3421 continue;
3422
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003423 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003424 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003425 {
3426 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003427 if (fd != INVALID_FD)
3428 {
3429 int r = channel_wait(channel, fd, 0);
3430
3431 if (r == CW_READY)
3432 channel_read(channel, part, "channel_handle_events");
3433 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003434 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003435 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003436 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003437 }
3438}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003439# endif
3440
Bram Moolenaard04a0202016-01-26 23:30:18 +01003441/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003442 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003443 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003444 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003445 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003446 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003447channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003448{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003449 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003450 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003451
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003452 fd = channel->ch_part[part].ch_fd;
3453 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003454 {
3455 if (!channel->ch_error && fun != NULL)
3456 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003457 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003458 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003459 }
3460 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003461 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003462 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003463
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003464 if (log_fd != NULL)
3465 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003466 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003467 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003468 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003469 fprintf(log_fd, "'\n");
3470 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003471 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003472 }
3473
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003474 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003475 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003476 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003477 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003478 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003479 {
3480 if (!channel->ch_error && fun != NULL)
3481 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003482 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003483 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003484 }
3485 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003486 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003487 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003488
3489 channel->ch_error = FALSE;
3490 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003491}
3492
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003493/*
3494 * Common for "ch_sendexpr()" and "ch_sendraw()".
3495 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003496 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003497 * Otherwise returns NULL.
3498 */
3499 channel_T *
3500send_common(
3501 typval_T *argvars,
3502 char_u *text,
3503 int id,
3504 int eval,
3505 jobopt_T *opt,
3506 char *fun,
3507 int *part_read)
3508{
3509 channel_T *channel;
3510 int part_send;
3511
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003512 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003513 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003514 if (channel == NULL)
3515 return NULL;
3516 part_send = channel_part_send(channel);
3517 *part_read = channel_part_read(channel);
3518
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003519 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3520 return NULL;
3521
3522 /* Set the callback. An empty callback means no callback and not reading
3523 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3524 * allowed. */
3525 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3526 {
3527 if (eval)
3528 {
3529 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3530 return NULL;
3531 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003532 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003533 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003534 }
3535
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003536 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003537 && opt->jo_callback == NULL)
3538 return channel;
3539 return NULL;
3540}
3541
3542/*
3543 * common for "ch_evalexpr()" and "ch_sendexpr()"
3544 */
3545 void
3546ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3547{
3548 char_u *text;
3549 typval_T *listtv;
3550 channel_T *channel;
3551 int id;
3552 ch_mode_T ch_mode;
3553 int part_send;
3554 int part_read;
3555 jobopt_T opt;
3556 int timeout;
3557
3558 /* return an empty string by default */
3559 rettv->v_type = VAR_STRING;
3560 rettv->vval.v_string = NULL;
3561
Bram Moolenaar437905c2016-04-26 19:01:05 +02003562 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003563 if (channel == NULL)
3564 return;
3565 part_send = channel_part_send(channel);
3566
3567 ch_mode = channel_get_mode(channel, part_send);
3568 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3569 {
3570 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3571 return;
3572 }
3573
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003574 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003575 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003576 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003577 if (text == NULL)
3578 return;
3579
3580 channel = send_common(argvars, text, id, eval, &opt,
3581 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3582 vim_free(text);
3583 if (channel != NULL && eval)
3584 {
3585 if (opt.jo_set & JO_TIMEOUT)
3586 timeout = opt.jo_timeout;
3587 else
3588 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003589 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3590 == OK)
3591 {
3592 list_T *list = listtv->vval.v_list;
3593
3594 /* Move the item from the list and then change the type to
3595 * avoid the value being freed. */
3596 *rettv = list->lv_last->li_tv;
3597 list->lv_last->li_tv.v_type = VAR_NUMBER;
3598 free_tv(listtv);
3599 }
3600 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003601 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003602}
3603
3604/*
3605 * common for "ch_evalraw()" and "ch_sendraw()"
3606 */
3607 void
3608ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3609{
3610 char_u buf[NUMBUFLEN];
3611 char_u *text;
3612 channel_T *channel;
3613 int part_read;
3614 jobopt_T opt;
3615 int timeout;
3616
3617 /* return an empty string by default */
3618 rettv->v_type = VAR_STRING;
3619 rettv->vval.v_string = NULL;
3620
3621 text = get_tv_string_buf(&argvars[1], buf);
3622 channel = send_common(argvars, text, 0, eval, &opt,
3623 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3624 if (channel != NULL && eval)
3625 {
3626 if (opt.jo_set & JO_TIMEOUT)
3627 timeout = opt.jo_timeout;
3628 else
3629 timeout = channel_get_timeout(channel, part_read);
3630 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3631 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003632 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003633}
3634
Bram Moolenaard04a0202016-01-26 23:30:18 +01003635# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003636/*
3637 * Add open channels to the poll struct.
3638 * Return the adjusted struct index.
3639 * The type of "fds" is hidden to avoid problems with the function proto.
3640 */
3641 int
3642channel_poll_setup(int nfd_in, void *fds_in)
3643{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003644 int nfd = nfd_in;
3645 channel_T *channel;
3646 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003647 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003648
Bram Moolenaar77073442016-02-13 23:23:53 +01003649 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003650 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003651 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003652 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003653 chanpart_T *ch_part = &channel->ch_part[part];
3654
3655 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003656 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003657 ch_part->ch_poll_idx = nfd;
3658 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003659 fds[nfd].events = POLLIN;
3660 nfd++;
3661 }
3662 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003663 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003664 }
3665 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003666
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003667 nfd = channel_fill_poll_write(nfd, fds);
3668
Bram Moolenaare0874f82016-01-24 20:36:41 +01003669 return nfd;
3670}
3671
3672/*
3673 * The type of "fds" is hidden to avoid problems with the function proto.
3674 */
3675 int
3676channel_poll_check(int ret_in, void *fds_in)
3677{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003678 int ret = ret_in;
3679 channel_T *channel;
3680 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003681 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003682 int idx;
3683 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003684
Bram Moolenaar77073442016-02-13 23:23:53 +01003685 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003686 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003687 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003688 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003689 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003690
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003691 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003692 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003693 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003694 --ret;
3695 }
3696 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003697
3698 in_part = &channel->ch_part[PART_IN];
3699 idx = in_part->ch_poll_idx;
3700 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3701 {
3702 if (in_part->ch_buf_append)
3703 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003704 if (in_part->ch_bufref.br_buf != NULL)
3705 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003706 }
3707 else
3708 channel_write_in(channel);
3709 --ret;
3710 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003711 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003712
3713 return ret;
3714}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003715# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003716
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003717# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003718/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003719 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003720 */
3721 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003722channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003723{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003724 int maxfd = maxfd_in;
3725 channel_T *channel;
3726 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003727 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003728 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003729
Bram Moolenaar77073442016-02-13 23:23:53 +01003730 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003731 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003732 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003733 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003734 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003735
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003736 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003737 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003738 FD_SET((int)fd, rfds);
3739 if (maxfd < (int)fd)
3740 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003741 }
3742 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003743 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003744
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003745 maxfd = channel_fill_wfds(maxfd, wfds);
3746
Bram Moolenaare0874f82016-01-24 20:36:41 +01003747 return maxfd;
3748}
3749
3750/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003751 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003752 */
3753 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003754channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003755{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003756 int ret = ret_in;
3757 channel_T *channel;
3758 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003759 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003760 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003761 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003762
Bram Moolenaar77073442016-02-13 23:23:53 +01003763 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003764 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003765 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003766 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003767 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003768
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003769 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003770 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003771 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003772 --ret;
3773 }
3774 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003775
3776 in_part = &channel->ch_part[PART_IN];
3777 if (ret > 0 && in_part->ch_fd != INVALID_FD
3778 && FD_ISSET(in_part->ch_fd, wfds))
3779 {
3780 if (in_part->ch_buf_append)
3781 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003782 if (in_part->ch_bufref.br_buf != NULL)
3783 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003784 }
3785 else
3786 channel_write_in(channel);
3787 --ret;
3788 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003789 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003790
3791 return ret;
3792}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003793# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003794
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003795/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003796 * Execute queued up commands.
3797 * Invoked from the main loop when it's safe to execute received commands.
3798 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003799 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003800 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003801channel_parse_messages(void)
3802{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003803 channel_T *channel = first_channel;
3804 int ret = FALSE;
3805 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003806 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003807
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003808 ++safe_to_invoke_callback;
3809
Bram Moolenaard0b65022016-03-06 21:50:33 +01003810 /* Only do this message when another message was given, otherwise we get
3811 * lots of them. */
3812 if (did_log_msg)
3813 {
3814 ch_log(NULL, "looking for messages on channels");
3815 did_log_msg = FALSE;
3816 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003817 while (channel != NULL)
3818 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003819 if (channel->ch_to_be_closed)
3820 {
3821 channel->ch_to_be_closed = FALSE;
3822 channel_close_now(channel);
3823 /* channel may have been freed, start over */
3824 channel = first_channel;
3825 continue;
3826 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003827 if (channel->ch_to_be_freed)
3828 {
3829 channel_free(channel);
3830 /* channel has been freed, start over */
3831 channel = first_channel;
3832 continue;
3833 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003834 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003835 {
3836 /* channel is no longer useful, free it */
3837 channel_free(channel);
3838 channel = first_channel;
3839 part = PART_SOCK;
3840 continue;
3841 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003842 if (channel->ch_part[part].ch_fd != INVALID_FD
3843 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003844 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003845 /* Increase the refcount, in case the handler causes the channel
3846 * to be unreferenced or closed. */
3847 ++channel->ch_refcount;
3848 r = may_invoke_callback(channel, part);
3849 if (r == OK)
3850 ret = TRUE;
3851 if (channel_unref(channel) || r == OK)
3852 {
3853 /* channel was freed or something was done, start over */
3854 channel = first_channel;
3855 part = PART_SOCK;
3856 continue;
3857 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003858 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003859 if (part < PART_ERR)
3860 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003861 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003862 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003863 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003864 part = PART_SOCK;
3865 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003866 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003867
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003868 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003869 {
3870 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003871 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003872 }
3873
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003874 --safe_to_invoke_callback;
3875
Bram Moolenaard7ece102016-02-02 23:23:02 +01003876 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003877}
3878
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003879/*
3880 * Mark references to lists used in channels.
3881 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003882 int
3883set_ref_in_channel(int copyID)
3884{
Bram Moolenaar77073442016-02-13 23:23:53 +01003885 int abort = FALSE;
3886 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003887 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003888
Bram Moolenaar77073442016-02-13 23:23:53 +01003889 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003890 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003891 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003892 tv.v_type = VAR_CHANNEL;
3893 tv.vval.v_channel = channel;
3894 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003895 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003896 return abort;
3897}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003898
3899/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003900 * Return the "part" to write to for "channel".
3901 */
3902 int
3903channel_part_send(channel_T *channel)
3904{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003905 if (channel->CH_SOCK_FD == INVALID_FD)
3906 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003907 return PART_SOCK;
3908}
3909
3910/*
3911 * Return the default "part" to read from for "channel".
3912 */
3913 int
3914channel_part_read(channel_T *channel)
3915{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003916 if (channel->CH_SOCK_FD == INVALID_FD)
3917 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003918 return PART_SOCK;
3919}
3920
3921/*
3922 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003923 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003924 */
3925 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003926channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003927{
Bram Moolenaar77073442016-02-13 23:23:53 +01003928 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003929 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003930 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003931}
3932
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003933/*
3934 * Return the timeout of "channel"/"part"
3935 */
3936 int
3937channel_get_timeout(channel_T *channel, int part)
3938{
3939 return channel->ch_part[part].ch_timeout;
3940}
3941
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003942 static int
3943handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3944{
3945 char_u *val = get_tv_string(item);
3946
3947 opt->jo_set |= jo;
3948 if (STRCMP(val, "nl") == 0)
3949 *modep = MODE_NL;
3950 else if (STRCMP(val, "raw") == 0)
3951 *modep = MODE_RAW;
3952 else if (STRCMP(val, "js") == 0)
3953 *modep = MODE_JS;
3954 else if (STRCMP(val, "json") == 0)
3955 *modep = MODE_JSON;
3956 else
3957 {
3958 EMSG2(_(e_invarg2), val);
3959 return FAIL;
3960 }
3961 return OK;
3962}
3963
3964 static int
3965handle_io(typval_T *item, int part, jobopt_T *opt)
3966{
3967 char_u *val = get_tv_string(item);
3968
3969 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3970 if (STRCMP(val, "null") == 0)
3971 opt->jo_io[part] = JIO_NULL;
3972 else if (STRCMP(val, "pipe") == 0)
3973 opt->jo_io[part] = JIO_PIPE;
3974 else if (STRCMP(val, "file") == 0)
3975 opt->jo_io[part] = JIO_FILE;
3976 else if (STRCMP(val, "buffer") == 0)
3977 opt->jo_io[part] = JIO_BUFFER;
3978 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3979 opt->jo_io[part] = JIO_OUT;
3980 else
3981 {
3982 EMSG2(_(e_invarg2), val);
3983 return FAIL;
3984 }
3985 return OK;
3986}
3987
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003988/*
3989 * Clear a jobopt_T before using it.
3990 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003991 void
3992clear_job_options(jobopt_T *opt)
3993{
3994 vim_memset(opt, 0, sizeof(jobopt_T));
3995}
3996
3997/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003998 * Free any members of a jobopt_T.
3999 */
4000 void
4001free_job_options(jobopt_T *opt)
4002{
4003 if (opt->jo_partial != NULL)
4004 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004005 else if (opt->jo_callback != NULL)
4006 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004007 if (opt->jo_out_partial != NULL)
4008 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004009 else if (opt->jo_out_cb != NULL)
4010 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004011 if (opt->jo_err_partial != NULL)
4012 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013 else if (opt->jo_err_cb != NULL)
4014 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004015 if (opt->jo_close_partial != NULL)
4016 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004017 else if (opt->jo_close_cb != NULL)
4018 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004019 if (opt->jo_exit_partial != NULL)
4020 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004021 else if (opt->jo_exit_cb != NULL)
4022 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004023}
4024
4025/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004026 * Get the PART_ number from the first character of an option name.
4027 */
4028 static int
4029part_from_char(int c)
4030{
4031 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4032}
4033
4034/*
4035 * Get the option entries from the dict in "tv", parse them and put the result
4036 * in "opt".
4037 * Only accept options in "supported".
4038 * If an option value is invalid return FAIL.
4039 */
4040 int
4041get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4042{
4043 typval_T *item;
4044 char_u *val;
4045 dict_T *dict;
4046 int todo;
4047 hashitem_T *hi;
4048 int part;
4049
4050 opt->jo_set = 0;
4051 if (tv->v_type == VAR_UNKNOWN)
4052 return OK;
4053 if (tv->v_type != VAR_DICT)
4054 {
4055 EMSG(_(e_invarg));
4056 return FAIL;
4057 }
4058 dict = tv->vval.v_dict;
4059 if (dict == NULL)
4060 return OK;
4061
4062 todo = (int)dict->dv_hashtab.ht_used;
4063 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4064 if (!HASHITEM_EMPTY(hi))
4065 {
4066 item = &dict_lookup(hi)->di_tv;
4067
4068 if (STRCMP(hi->hi_key, "mode") == 0)
4069 {
4070 if (!(supported & JO_MODE))
4071 break;
4072 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4073 return FAIL;
4074 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004075 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 {
4077 if (!(supported & JO_IN_MODE))
4078 break;
4079 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4080 == FAIL)
4081 return FAIL;
4082 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004083 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004084 {
4085 if (!(supported & JO_OUT_MODE))
4086 break;
4087 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4088 == FAIL)
4089 return FAIL;
4090 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004091 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004092 {
4093 if (!(supported & JO_ERR_MODE))
4094 break;
4095 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4096 == FAIL)
4097 return FAIL;
4098 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004099 else if (STRCMP(hi->hi_key, "in_io") == 0
4100 || STRCMP(hi->hi_key, "out_io") == 0
4101 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004102 {
4103 if (!(supported & JO_OUT_IO))
4104 break;
4105 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4106 return FAIL;
4107 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004108 else if (STRCMP(hi->hi_key, "in_name") == 0
4109 || STRCMP(hi->hi_key, "out_name") == 0
4110 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004111 {
4112 part = part_from_char(*hi->hi_key);
4113
4114 if (!(supported & JO_OUT_IO))
4115 break;
4116 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4117 opt->jo_io_name[part] =
4118 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4119 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004120 else if (STRCMP(hi->hi_key, "in_buf") == 0
4121 || STRCMP(hi->hi_key, "out_buf") == 0
4122 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004123 {
4124 part = part_from_char(*hi->hi_key);
4125
4126 if (!(supported & JO_OUT_IO))
4127 break;
4128 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4129 opt->jo_io_buf[part] = get_tv_number(item);
4130 if (opt->jo_io_buf[part] <= 0)
4131 {
4132 EMSG2(_(e_invarg2), get_tv_string(item));
4133 return FAIL;
4134 }
4135 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4136 {
4137 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4138 return FAIL;
4139 }
4140 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004141 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4142 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4143 {
4144 part = part_from_char(*hi->hi_key);
4145
4146 if (!(supported & JO_OUT_IO))
4147 break;
4148 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4149 opt->jo_modifiable[part] = get_tv_number(item);
4150 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004151 else if (STRCMP(hi->hi_key, "out_msg") == 0
4152 || STRCMP(hi->hi_key, "err_msg") == 0)
4153 {
4154 part = part_from_char(*hi->hi_key);
4155
4156 if (!(supported & JO_OUT_IO))
4157 break;
4158 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4159 opt->jo_message[part] = get_tv_number(item);
4160 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004161 else if (STRCMP(hi->hi_key, "in_top") == 0
4162 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004163 {
4164 linenr_T *lp;
4165
4166 if (!(supported & JO_OUT_IO))
4167 break;
4168 if (hi->hi_key[3] == 't')
4169 {
4170 lp = &opt->jo_in_top;
4171 opt->jo_set |= JO_IN_TOP;
4172 }
4173 else
4174 {
4175 lp = &opt->jo_in_bot;
4176 opt->jo_set |= JO_IN_BOT;
4177 }
4178 *lp = get_tv_number(item);
4179 if (*lp < 0)
4180 {
4181 EMSG2(_(e_invarg2), get_tv_string(item));
4182 return FAIL;
4183 }
4184 }
4185 else if (STRCMP(hi->hi_key, "channel") == 0)
4186 {
4187 if (!(supported & JO_OUT_IO))
4188 break;
4189 opt->jo_set |= JO_CHANNEL;
4190 if (item->v_type != VAR_CHANNEL)
4191 {
4192 EMSG2(_(e_invarg2), "channel");
4193 return FAIL;
4194 }
4195 opt->jo_channel = item->vval.v_channel;
4196 }
4197 else if (STRCMP(hi->hi_key, "callback") == 0)
4198 {
4199 if (!(supported & JO_CALLBACK))
4200 break;
4201 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004202 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004203 if (opt->jo_callback == NULL)
4204 {
4205 EMSG2(_(e_invarg2), "callback");
4206 return FAIL;
4207 }
4208 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004209 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004210 {
4211 if (!(supported & JO_OUT_CALLBACK))
4212 break;
4213 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004214 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004215 if (opt->jo_out_cb == NULL)
4216 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004217 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004218 return FAIL;
4219 }
4220 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004221 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004222 {
4223 if (!(supported & JO_ERR_CALLBACK))
4224 break;
4225 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004226 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004227 if (opt->jo_err_cb == NULL)
4228 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004229 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004230 return FAIL;
4231 }
4232 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004233 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004234 {
4235 if (!(supported & JO_CLOSE_CALLBACK))
4236 break;
4237 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004238 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004239 if (opt->jo_close_cb == NULL)
4240 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004241 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004242 return FAIL;
4243 }
4244 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004245 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4246 {
4247 if (!(supported & JO_EXIT_CB))
4248 break;
4249 opt->jo_set |= JO_EXIT_CB;
4250 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4251 if (opt->jo_exit_cb == NULL)
4252 {
4253 EMSG2(_(e_invarg2), "exit_cb");
4254 return FAIL;
4255 }
4256 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004257 else if (STRCMP(hi->hi_key, "waittime") == 0)
4258 {
4259 if (!(supported & JO_WAITTIME))
4260 break;
4261 opt->jo_set |= JO_WAITTIME;
4262 opt->jo_waittime = get_tv_number(item);
4263 }
4264 else if (STRCMP(hi->hi_key, "timeout") == 0)
4265 {
4266 if (!(supported & JO_TIMEOUT))
4267 break;
4268 opt->jo_set |= JO_TIMEOUT;
4269 opt->jo_timeout = get_tv_number(item);
4270 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004271 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004272 {
4273 if (!(supported & JO_OUT_TIMEOUT))
4274 break;
4275 opt->jo_set |= JO_OUT_TIMEOUT;
4276 opt->jo_out_timeout = get_tv_number(item);
4277 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004278 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004279 {
4280 if (!(supported & JO_ERR_TIMEOUT))
4281 break;
4282 opt->jo_set |= JO_ERR_TIMEOUT;
4283 opt->jo_err_timeout = get_tv_number(item);
4284 }
4285 else if (STRCMP(hi->hi_key, "part") == 0)
4286 {
4287 if (!(supported & JO_PART))
4288 break;
4289 opt->jo_set |= JO_PART;
4290 val = get_tv_string(item);
4291 if (STRCMP(val, "err") == 0)
4292 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004293 else if (STRCMP(val, "out") == 0)
4294 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004295 else
4296 {
4297 EMSG2(_(e_invarg2), val);
4298 return FAIL;
4299 }
4300 }
4301 else if (STRCMP(hi->hi_key, "id") == 0)
4302 {
4303 if (!(supported & JO_ID))
4304 break;
4305 opt->jo_set |= JO_ID;
4306 opt->jo_id = get_tv_number(item);
4307 }
4308 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4309 {
4310 if (!(supported & JO_STOPONEXIT))
4311 break;
4312 opt->jo_set |= JO_STOPONEXIT;
4313 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4314 opt->jo_soe_buf);
4315 if (opt->jo_stoponexit == NULL)
4316 {
4317 EMSG2(_(e_invarg2), "stoponexit");
4318 return FAIL;
4319 }
4320 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004321 else if (STRCMP(hi->hi_key, "block_write") == 0)
4322 {
4323 if (!(supported & JO_BLOCK_WRITE))
4324 break;
4325 opt->jo_set |= JO_BLOCK_WRITE;
4326 opt->jo_block_write = get_tv_number(item);
4327 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004328 else
4329 break;
4330 --todo;
4331 }
4332 if (todo > 0)
4333 {
4334 EMSG2(_(e_invarg2), hi->hi_key);
4335 return FAIL;
4336 }
4337
4338 return OK;
4339}
4340
4341/*
4342 * Get the channel from the argument.
4343 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004344 * When "check_open" is TRUE check that the channel can be used.
4345 * When "reading" is TRUE "check_open" considers typeahead useful.
4346 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004347 */
4348 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004349get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004350{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004351 channel_T *channel = NULL;
4352 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004353
4354 if (tv->v_type == VAR_JOB)
4355 {
4356 if (tv->vval.v_job != NULL)
4357 channel = tv->vval.v_job->jv_channel;
4358 }
4359 else if (tv->v_type == VAR_CHANNEL)
4360 {
4361 channel = tv->vval.v_channel;
4362 }
4363 else
4364 {
4365 EMSG2(_(e_invarg2), get_tv_string(tv));
4366 return NULL;
4367 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004368 if (channel != NULL && reading)
4369 has_readahead = channel_has_readahead(channel,
4370 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004371
Bram Moolenaar437905c2016-04-26 19:01:05 +02004372 if (check_open && (channel == NULL || (!channel_is_open(channel)
4373 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004374 {
4375 EMSG(_("E906: not an open channel"));
4376 return NULL;
4377 }
4378 return channel;
4379}
4380
4381static job_T *first_job = NULL;
4382
4383 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004384job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004385{
4386 ch_log(job->jv_channel, "Freeing job");
4387 if (job->jv_channel != NULL)
4388 {
4389 /* The link from the channel to the job doesn't count as a reference,
4390 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004391 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004392 * NULL the reference. We don't set ch_job_killed, unreferencing the
4393 * job doesn't mean it stops running. */
4394 job->jv_channel->ch_job = NULL;
4395 channel_unref(job->jv_channel);
4396 }
4397 mch_clear_job(job);
4398
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004399 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004400 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004401}
4402
4403 static void
4404job_free_job(job_T *job)
4405{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004406 if (job->jv_next != NULL)
4407 job->jv_next->jv_prev = job->jv_prev;
4408 if (job->jv_prev == NULL)
4409 first_job = job->jv_next;
4410 else
4411 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004412 vim_free(job);
4413}
4414
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004415 static void
4416job_free(job_T *job)
4417{
4418 if (!in_free_unref_items)
4419 {
4420 job_free_contents(job);
4421 job_free_job(job);
4422 }
4423}
4424
Bram Moolenaar655da312016-05-28 22:22:34 +02004425#if defined(EXITFREE) || defined(PROTO)
4426 void
4427job_free_all(void)
4428{
4429 while (first_job != NULL)
4430 job_free(first_job);
4431}
4432#endif
4433
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004434/*
4435 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004436 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4437 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004438 */
4439 static int
4440job_still_useful(job_T *job)
4441{
4442 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004443 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4444 || (job->jv_channel != NULL
4445 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004446}
4447
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004448/*
4449 * Mark references in jobs that are still useful.
4450 */
4451 int
4452set_ref_in_job(int copyID)
4453{
4454 int abort = FALSE;
4455 job_T *job;
4456 typval_T tv;
4457
4458 for (job = first_job; job != NULL; job = job->jv_next)
4459 if (job_still_useful(job))
4460 {
4461 tv.v_type = VAR_JOB;
4462 tv.vval.v_job = job;
4463 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4464 }
4465 return abort;
4466}
4467
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004468 void
4469job_unref(job_T *job)
4470{
4471 if (job != NULL && --job->jv_refcount <= 0)
4472 {
4473 /* Do not free the job when it has not ended yet and there is a
4474 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004475 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004476 {
4477 job_free(job);
4478 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004479 else if (job->jv_channel != NULL
4480 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004481 {
4482 /* Do remove the link to the channel, otherwise it hangs
4483 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004484 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004485 job->jv_channel->ch_job = NULL;
4486 channel_unref(job->jv_channel);
4487 job->jv_channel = NULL;
4488 }
4489 }
4490}
4491
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004492 int
4493free_unused_jobs_contents(int copyID, int mask)
4494{
4495 int did_free = FALSE;
4496 job_T *job;
4497
4498 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004499 if ((job->jv_copyID & mask) != (copyID & mask)
4500 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004501 {
4502 /* Free the channel and ordinary items it contains, but don't
4503 * recurse into Lists, Dictionaries etc. */
4504 job_free_contents(job);
4505 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004506 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004507 return did_free;
4508}
4509
4510 void
4511free_unused_jobs(int copyID, int mask)
4512{
4513 job_T *job;
4514 job_T *job_next;
4515
4516 for (job = first_job; job != NULL; job = job_next)
4517 {
4518 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004519 if ((job->jv_copyID & mask) != (copyID & mask)
4520 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004521 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004522 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004523 job_free_job(job);
4524 }
4525 }
4526}
4527
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004528/*
4529 * Allocate a job. Sets the refcount to one and sets options default.
4530 */
4531 static job_T *
4532job_alloc(void)
4533{
4534 job_T *job;
4535
4536 job = (job_T *)alloc_clear(sizeof(job_T));
4537 if (job != NULL)
4538 {
4539 job->jv_refcount = 1;
4540 job->jv_stoponexit = vim_strsave((char_u *)"term");
4541
4542 if (first_job != NULL)
4543 {
4544 first_job->jv_prev = job;
4545 job->jv_next = first_job;
4546 }
4547 first_job = job;
4548 }
4549 return job;
4550}
4551
4552 void
4553job_set_options(job_T *job, jobopt_T *opt)
4554{
4555 if (opt->jo_set & JO_STOPONEXIT)
4556 {
4557 vim_free(job->jv_stoponexit);
4558 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4559 job->jv_stoponexit = NULL;
4560 else
4561 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4562 }
4563 if (opt->jo_set & JO_EXIT_CB)
4564 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004565 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004566 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004567 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004569 job->jv_exit_partial = NULL;
4570 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004571 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004572 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004573 job->jv_exit_partial = opt->jo_exit_partial;
4574 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004575 {
4576 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004577 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004578 }
4579 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004580 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004581 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004582 func_ref(job->jv_exit_cb);
4583 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004584 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004585 }
4586}
4587
4588/*
4589 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4590 */
4591 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004592job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004593{
4594 job_T *job;
4595
4596 for (job = first_job; job != NULL; job = job->jv_next)
4597 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4598 mch_stop_job(job, job->jv_stoponexit);
4599}
4600
4601/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004602 * Return TRUE when there is any job that might exit, which means
4603 * job_check_ended() should be called once in a while.
4604 */
4605 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004606has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004607{
4608 job_T *job;
4609
4610 for (job = first_job; job != NULL; job = job->jv_next)
4611 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4612 return TRUE;
4613 return FALSE;
4614}
4615
4616/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004617 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004618 */
4619 void
4620job_check_ended(void)
4621{
4622 static time_t last_check = 0;
4623 time_t now;
4624 job_T *job;
4625 job_T *next;
4626
4627 /* Only do this once in 10 seconds. */
4628 now = time(NULL);
4629 if (last_check + 10 < now)
4630 {
4631 last_check = now;
4632 for (job = first_job; job != NULL; job = next)
4633 {
4634 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004635 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004636 job_status(job); /* may free "job" */
4637 }
4638 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004639 if (channel_need_redraw)
4640 {
4641 channel_need_redraw = FALSE;
4642 redraw_after_callback();
4643 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004644}
4645
4646/*
4647 * "job_start()" function
4648 */
4649 job_T *
4650job_start(typval_T *argvars)
4651{
4652 job_T *job;
4653 char_u *cmd = NULL;
4654#if defined(UNIX)
4655# define USE_ARGV
4656 char **argv = NULL;
4657 int argc = 0;
4658#else
4659 garray_T ga;
4660#endif
4661 jobopt_T opt;
4662 int part;
4663
4664 job = job_alloc();
4665 if (job == NULL)
4666 return NULL;
4667
4668 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004669#ifndef USE_ARGV
4670 ga_init2(&ga, (int)sizeof(char*), 20);
4671#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004672
4673 /* Default mode is NL. */
4674 clear_job_options(&opt);
4675 opt.jo_mode = MODE_NL;
4676 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004677 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4678 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004679 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004680
4681 /* Check that when io is "file" that there is a file name. */
4682 for (part = PART_OUT; part <= PART_IN; ++part)
4683 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4684 && opt.jo_io[part] == JIO_FILE
4685 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4686 || *opt.jo_io_name[part] == NUL))
4687 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004688 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004689 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004690 }
4691
4692 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4693 {
4694 buf_T *buf = NULL;
4695
4696 /* check that we can find the buffer before starting the job */
4697 if (opt.jo_set & JO_IN_BUF)
4698 {
4699 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4700 if (buf == NULL)
4701 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4702 }
4703 else if (!(opt.jo_set & JO_IN_NAME))
4704 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004705 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004706 }
4707 else
4708 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4709 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004710 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004711 if (buf->b_ml.ml_mfp == NULL)
4712 {
4713 char_u numbuf[NUMBUFLEN];
4714 char_u *s;
4715
4716 if (opt.jo_set & JO_IN_BUF)
4717 {
4718 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4719 s = numbuf;
4720 }
4721 else
4722 s = opt.jo_io_name[PART_IN];
4723 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004724 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004725 }
4726 job->jv_in_buf = buf;
4727 }
4728
4729 job_set_options(job, &opt);
4730
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004731 if (argvars[0].v_type == VAR_STRING)
4732 {
4733 /* Command is a string. */
4734 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004735 if (cmd == NULL || *cmd == NUL)
4736 {
4737 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004738 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004739 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004740#ifdef USE_ARGV
4741 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004742 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004743 argv[argc] = NULL;
4744#endif
4745 }
4746 else if (argvars[0].v_type != VAR_LIST
4747 || argvars[0].vval.v_list == NULL
4748 || argvars[0].vval.v_list->lv_len < 1)
4749 {
4750 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004751 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004752 }
4753 else
4754 {
4755 list_T *l = argvars[0].vval.v_list;
4756 listitem_T *li;
4757 char_u *s;
4758
4759#ifdef USE_ARGV
4760 /* Pass argv[] to mch_call_shell(). */
4761 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4762 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004763 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004764#endif
4765 for (li = l->lv_first; li != NULL; li = li->li_next)
4766 {
4767 s = get_tv_string_chk(&li->li_tv);
4768 if (s == NULL)
4769 goto theend;
4770#ifdef USE_ARGV
4771 argv[argc++] = (char *)s;
4772#else
4773 /* Only escape when needed, double quotes are not always allowed. */
4774 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4775 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004776# ifdef WIN32
4777 int old_ssl = p_ssl;
4778
4779 /* This is using CreateProcess, not cmd.exe. Always use
4780 * double quote and backslashes. */
4781 p_ssl = 0;
4782# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004783 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004784# ifdef WIN32
4785 p_ssl = old_ssl;
4786# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004787 if (s == NULL)
4788 goto theend;
4789 ga_concat(&ga, s);
4790 vim_free(s);
4791 }
4792 else
4793 ga_concat(&ga, s);
4794 if (li->li_next != NULL)
4795 ga_append(&ga, ' ');
4796#endif
4797 }
4798#ifdef USE_ARGV
4799 argv[argc] = NULL;
4800#else
4801 cmd = ga.ga_data;
4802#endif
4803 }
4804
4805#ifdef USE_ARGV
4806 if (ch_log_active())
4807 {
4808 garray_T ga;
4809 int i;
4810
4811 ga_init2(&ga, (int)sizeof(char), 200);
4812 for (i = 0; i < argc; ++i)
4813 {
4814 if (i > 0)
4815 ga_concat(&ga, (char_u *)" ");
4816 ga_concat(&ga, (char_u *)argv[i]);
4817 }
4818 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4819 ga_clear(&ga);
4820 }
4821 mch_start_job(argv, job, &opt);
4822#else
4823 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4824 mch_start_job((char *)cmd, job, &opt);
4825#endif
4826
4827 /* If the channel is reading from a buffer, write lines now. */
4828 if (job->jv_channel != NULL)
4829 channel_write_in(job->jv_channel);
4830
4831theend:
4832#ifdef USE_ARGV
4833 vim_free(argv);
4834#else
4835 vim_free(ga.ga_data);
4836#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004837 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004838 return job;
4839}
4840
4841/*
4842 * Get the status of "job" and invoke the exit callback when needed.
4843 * The returned string is not allocated.
4844 */
4845 char *
4846job_status(job_T *job)
4847{
4848 char *result;
4849
4850 if (job->jv_status == JOB_ENDED)
4851 /* No need to check, dead is dead. */
4852 result = "dead";
4853 else if (job->jv_status == JOB_FAILED)
4854 result = "fail";
4855 else
4856 {
4857 result = mch_job_status(job);
4858 if (job->jv_status == JOB_ENDED)
4859 ch_log(job->jv_channel, "Job ended");
4860 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4861 {
4862 typval_T argv[3];
4863 typval_T rettv;
4864 int dummy;
4865
4866 /* invoke the exit callback; make sure the refcount is > 0 */
4867 ++job->jv_refcount;
4868 argv[0].v_type = VAR_JOB;
4869 argv[0].vval.v_job = job;
4870 argv[1].v_type = VAR_NUMBER;
4871 argv[1].vval.v_number = job->jv_exitval;
4872 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004873 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004874 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004875 clear_tv(&rettv);
4876 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004877 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004878 }
4879 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4880 {
4881 /* The job was already unreferenced, now that it ended it can be
4882 * freed. Careful: caller must not use "job" after this! */
4883 job_free(job);
4884 }
4885 }
4886 return result;
4887}
4888
Bram Moolenaar8950a562016-03-12 15:22:55 +01004889/*
4890 * Implementation of job_info().
4891 */
4892 void
4893job_info(job_T *job, dict_T *dict)
4894{
4895 dictitem_T *item;
4896 varnumber_T nr;
4897
4898 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4899
4900 item = dictitem_alloc((char_u *)"channel");
4901 if (item == NULL)
4902 return;
4903 item->di_tv.v_lock = 0;
4904 item->di_tv.v_type = VAR_CHANNEL;
4905 item->di_tv.vval.v_channel = job->jv_channel;
4906 if (job->jv_channel != NULL)
4907 ++job->jv_channel->ch_refcount;
4908 if (dict_add(dict, item) == FAIL)
4909 dictitem_free(item);
4910
4911#ifdef UNIX
4912 nr = job->jv_pid;
4913#else
4914 nr = job->jv_proc_info.dwProcessId;
4915#endif
4916 dict_add_nr_str(dict, "process", nr, NULL);
4917
4918 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004919 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004920 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4921}
4922
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004923 int
4924job_stop(job_T *job, typval_T *argvars)
4925{
4926 char_u *arg;
4927
4928 if (argvars[1].v_type == VAR_UNKNOWN)
4929 arg = (char_u *)"";
4930 else
4931 {
4932 arg = get_tv_string_chk(&argvars[1]);
4933 if (arg == NULL)
4934 {
4935 EMSG(_(e_invarg));
4936 return 0;
4937 }
4938 }
4939 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4940 if (mch_stop_job(job, arg) == FAIL)
4941 return 0;
4942
4943 /* Assume that "hup" does not kill the job. */
4944 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4945 job->jv_channel->ch_job_killed = TRUE;
4946
4947 /* We don't try freeing the job, obviously the caller still has a
4948 * reference to it. */
4949 return 1;
4950}
4951
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004952#endif /* FEAT_JOB_CHANNEL */