blob: 13fb65302b5d1d8c9e4939f0a397499550ae419b [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 Moolenaar42d38a22016-02-20 18:18:59 +01002358 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002359 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002360 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002361 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002362 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002363 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002364 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002365
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002366 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002367 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002368 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002369
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002370 /* Use a message-specific callback, part callback or channel callback */
2371 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2372 if (cbitem->cq_seq_nr == 0)
2373 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002374 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002375 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002376 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002377 partial = cbitem->cq_partial;
2378 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002379 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002380 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002381 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002382 partial = channel->ch_part[part].ch_partial;
2383 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002384 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002385 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002386 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002387 partial = channel->ch_partial;
2388 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002389
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002390 buffer = channel->ch_part[part].ch_bufref.br_buf;
2391 if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002392 {
2393 /* buffer was wiped out */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002394 channel->ch_part[part].ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002395 buffer = NULL;
2396 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002397
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002398 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002399 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002400 listitem_T *item;
2401 int argc = 0;
2402
Bram Moolenaard7ece102016-02-02 23:23:02 +01002403 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002404 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002405 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002406 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002407 channel_parse_json(channel, part);
2408 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002409 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002410 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002411
Bram Moolenaarece61b02016-02-20 21:39:05 +01002412 for (item = listtv->vval.v_list->lv_first;
2413 item != NULL && argc < CH_JSON_MAX_ARGS;
2414 item = item->li_next)
2415 argv[argc++] = item->li_tv;
2416 while (argc < CH_JSON_MAX_ARGS)
2417 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002418
Bram Moolenaarece61b02016-02-20 21:39:05 +01002419 if (argv[0].v_type == VAR_STRING)
2420 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002421 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002422 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002423 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002424 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002425 }
2426
Bram Moolenaarece61b02016-02-20 21:39:05 +01002427 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002428 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002429 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002430 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002431 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002432 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002433 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002434 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002435 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002436 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002437 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002438 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002439 return FALSE;
2440 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002441 else
2442 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002443 /* If there is no callback or buffer drop the message. */
2444 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002445 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002446 /* If there is a close callback it may use ch_read() to get the
2447 * messages. */
2448 if (channel->ch_close_cb == NULL)
2449 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002450 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002451 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002452
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002453 if (ch_mode == MODE_NL)
2454 {
2455 char_u *nl;
2456 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002457 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002458
2459 /* See if we have a message ending in NL in the first buffer. If
2460 * not try to concatenate the first and the second buffer. */
2461 while (TRUE)
2462 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002463 node = channel_peek(channel, part);
2464 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002465 if (nl != NULL)
2466 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002467 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002468 return FALSE; /* incomplete message */
2469 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002470 buf = node->rq_buffer;
2471
2472 /* Convert NUL to NL, the internal representation. */
2473 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2474 if (*p == NUL)
2475 *p = NL;
2476
2477 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002478 {
2479 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002480 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002481 *nl = NUL;
2482 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002483 else
2484 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002485 /* Copy the message into allocated memory (excluding the NL)
2486 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002487 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002488 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002489 }
2490 }
2491 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002492 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002493 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002494 * get everything we have.
2495 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002496 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002497 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002498
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002499 if (msg == NULL)
2500 return FALSE; /* out of memory (and avoids Coverity warning) */
2501
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002502 argv[1].v_type = VAR_STRING;
2503 argv[1].vval.v_string = msg;
2504 }
2505
Bram Moolenaara07fec92016-02-05 21:04:08 +01002506 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002507 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002508 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002509
2510 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002511 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002512 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002513 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002514 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002515 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002516 break;
2517 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002518 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002519 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002520 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002521 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002522 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002523 if (buffer != NULL)
2524 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002525 if (msg == NULL)
2526 /* JSON or JS mode: re-encode the message. */
2527 msg = json_encode(listtv, ch_mode);
2528 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002529 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002530 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002531
Bram Moolenaar187db502016-02-27 14:44:26 +01002532 if (callback != NULL)
2533 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002534 if (cbitem != NULL)
2535 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2536 else
2537 {
2538 /* invoke the channel callback */
2539 ch_logs(channel, "Invoking channel callback %s",
2540 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002541 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002542 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002543 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002544 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002545 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002546 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002547
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002548 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002549 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002550 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002551
2552 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002553}
2554
2555/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002556 * Return TRUE when channel "channel" is open for writing to.
2557 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002558 */
2559 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002560channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002561{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002562 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002563 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002564}
2565
2566/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002567 * Return TRUE when channel "channel" is open for reading or writing.
2568 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002569 */
2570 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002571channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002572{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002573 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002574 || channel->CH_IN_FD != INVALID_FD
2575 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002576 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002577}
2578
2579/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002580 * Return TRUE if "channel" has JSON or other typeahead.
2581 */
2582 static int
2583channel_has_readahead(channel_T *channel, int part)
2584{
2585 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2586
2587 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2588 {
2589 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2590 jsonq_T *item = head->jq_next;
2591
2592 return item != NULL;
2593 }
2594 return channel_peek(channel, part) != NULL;
2595}
2596
2597/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002598 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002599 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002600 */
2601 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002602channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002603{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002604 int part;
2605 int has_readahead = FALSE;
2606
Bram Moolenaar77073442016-02-13 23:23:53 +01002607 if (channel == NULL)
2608 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002609 if (req_part == PART_OUT)
2610 {
2611 if (channel->CH_OUT_FD != INVALID_FD)
2612 return "open";
2613 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002614 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002615 }
2616 else if (req_part == PART_ERR)
2617 {
2618 if (channel->CH_ERR_FD != INVALID_FD)
2619 return "open";
2620 if (channel_has_readahead(channel, PART_ERR))
2621 has_readahead = TRUE;
2622 }
2623 else
2624 {
2625 if (channel_is_open(channel))
2626 return "open";
2627 for (part = PART_SOCK; part <= PART_ERR; ++part)
2628 if (channel_has_readahead(channel, part))
2629 {
2630 has_readahead = TRUE;
2631 break;
2632 }
2633 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002634
2635 if (has_readahead)
2636 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002637 return "closed";
2638}
2639
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002640 static void
2641channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2642{
2643 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002644 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002645 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002646 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002647 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002648
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002649 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002650 STRCAT(namebuf, "_");
2651 tail = STRLEN(namebuf);
2652
2653 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002654 if (chanpart->ch_fd != INVALID_FD)
2655 status = "open";
2656 else if (channel_has_readahead(channel, part))
2657 status = "buffered";
2658 else
2659 status = "closed";
2660 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002661
2662 STRCPY(namebuf + tail, "mode");
2663 switch (chanpart->ch_mode)
2664 {
2665 case MODE_NL: s = "NL"; break;
2666 case MODE_RAW: s = "RAW"; break;
2667 case MODE_JSON: s = "JSON"; break;
2668 case MODE_JS: s = "JS"; break;
2669 }
2670 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2671
2672 STRCPY(namebuf + tail, "io");
2673 if (part == PART_SOCK)
2674 s = "socket";
2675 else switch (chanpart->ch_io)
2676 {
2677 case JIO_NULL: s = "null"; break;
2678 case JIO_PIPE: s = "pipe"; break;
2679 case JIO_FILE: s = "file"; break;
2680 case JIO_BUFFER: s = "buffer"; break;
2681 case JIO_OUT: s = "out"; break;
2682 }
2683 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2684
2685 STRCPY(namebuf + tail, "timeout");
2686 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2687}
2688
2689 void
2690channel_info(channel_T *channel, dict_T *dict)
2691{
2692 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002693 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002694
2695 if (channel->ch_hostname != NULL)
2696 {
2697 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2698 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2699 channel_part_info(channel, dict, "sock", PART_SOCK);
2700 }
2701 else
2702 {
2703 channel_part_info(channel, dict, "out", PART_OUT);
2704 channel_part_info(channel, dict, "err", PART_ERR);
2705 channel_part_info(channel, dict, "in", PART_IN);
2706 }
2707}
2708
Bram Moolenaar77073442016-02-13 23:23:53 +01002709/*
2710 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002711 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002712 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002713 */
2714 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002715channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002716{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002717 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002718
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002719#ifdef FEAT_GUI
2720 channel_gui_unregister(channel);
2721#endif
2722
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002723 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002724 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002725 sock_close(channel->CH_SOCK_FD);
2726 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002727 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002728 may_close_part(&channel->CH_IN_FD);
2729 may_close_part(&channel->CH_OUT_FD);
2730 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002731
Bram Moolenaar8b374212016-02-24 20:43:06 +01002732 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002733 {
2734 typval_T argv[1];
2735 typval_T rettv;
2736 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002737 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002738
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002739 /* Invoke callbacks before the close callback, since it's weird to
2740 * first invoke the close callback. Increment the refcount to avoid
2741 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002742 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002743 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002744 for (part = PART_SOCK; part <= PART_ERR; ++part)
2745 while (may_invoke_callback(channel, part))
2746 ;
2747
2748 /* Invoke the close callback, if still set. */
2749 if (channel->ch_close_cb != NULL)
2750 {
2751 ch_logs(channel, "Invoking close callback %s",
2752 (char *)channel->ch_close_cb);
2753 argv[0].v_type = VAR_CHANNEL;
2754 argv[0].vval.v_channel = channel;
2755 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002756 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002757 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002758 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002759 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002760 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002761
2762 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002763 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002764 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002765 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002766
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002767 --channel->ch_refcount;
2768
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002769 if (channel_need_redraw)
2770 {
2771 channel_need_redraw = FALSE;
2772 redraw_after_callback();
2773 }
2774
Bram Moolenaar437905c2016-04-26 19:01:05 +02002775 /* any remaining messages are useless now */
2776 for (part = PART_SOCK; part <= PART_ERR; ++part)
2777 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002778 }
2779
2780 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002781}
2782
Bram Moolenaard04a0202016-01-26 23:30:18 +01002783/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002784 * Close the "in" part channel "channel".
2785 */
2786 void
2787channel_close_in(channel_T *channel)
2788{
2789 may_close_part(&channel->CH_IN_FD);
2790}
2791
2792/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002793 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002794 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002795 static void
2796channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002797{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002798 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2799 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002800
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002801 while (channel_peek(channel, part) != NULL)
2802 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002803
2804 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002805 {
2806 cbq_T *node = cb_head->cq_next;
2807
2808 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002809 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002810 vim_free(node);
2811 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002812
2813 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002814 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002815 free_tv(json_head->jq_next->jq_value);
2816 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002817 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002818
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002819 free_callback(channel->ch_part[part].ch_callback,
2820 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002821 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002822 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002823}
2824
2825/*
2826 * Clear all the read buffers on "channel".
2827 */
2828 void
2829channel_clear(channel_T *channel)
2830{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002831 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002832 vim_free(channel->ch_hostname);
2833 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002834 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002835 channel_clear_one(channel, PART_OUT);
2836 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002837 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002838 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002839 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002840 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002841 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002842 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002843 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002844}
2845
Bram Moolenaar77073442016-02-13 23:23:53 +01002846#if defined(EXITFREE) || defined(PROTO)
2847 void
2848channel_free_all(void)
2849{
2850 channel_T *channel;
2851
Bram Moolenaard6051b52016-02-28 15:49:03 +01002852 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002853 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2854 channel_clear(channel);
2855}
2856#endif
2857
2858
Bram Moolenaar715d2852016-04-30 17:06:31 +02002859/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002860#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002861
2862/* Buffer size for reading incoming messages. */
2863#define MAXMSGSIZE 4096
2864
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002865#if defined(HAVE_SELECT)
2866/*
2867 * Add write fds where we are waiting for writing to be possible.
2868 */
2869 static int
2870channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2871{
2872 int maxfd = maxfd_arg;
2873 channel_T *ch;
2874
2875 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2876 {
2877 chanpart_T *in_part = &ch->ch_part[PART_IN];
2878
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002879 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002880 {
2881 FD_SET((int)in_part->ch_fd, wfds);
2882 if ((int)in_part->ch_fd >= maxfd)
2883 maxfd = (int)in_part->ch_fd + 1;
2884 }
2885 }
2886 return maxfd;
2887}
2888#else
2889/*
2890 * Add write fds where we are waiting for writing to be possible.
2891 */
2892 static int
2893channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2894{
2895 int nfd = nfd_in;
2896 channel_T *ch;
2897
2898 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2899 {
2900 chanpart_T *in_part = &ch->ch_part[PART_IN];
2901
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002902 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002903 {
2904 in_part->ch_poll_idx = nfd;
2905 fds[nfd].fd = in_part->ch_fd;
2906 fds[nfd].events = POLLOUT;
2907 ++nfd;
2908 }
2909 else
2910 in_part->ch_poll_idx = -1;
2911 }
2912 return nfd;
2913}
2914#endif
2915
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002916typedef enum {
2917 CW_READY,
2918 CW_NOT_READY,
2919 CW_ERROR
2920} channel_wait_result;
2921
Bram Moolenaard04a0202016-01-26 23:30:18 +01002922/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002923 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002924 * Return CW_READY when there is something to read.
2925 * Return CW_NOT_READY when there is nothing to read.
2926 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002927 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002928 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002929channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002930{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002931 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002932 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002933
Bram Moolenaard8070362016-02-15 21:56:54 +01002934# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002935 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002936 {
2937 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002938 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002939 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002940 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002941
2942 /* reading from a pipe, not a socket */
2943 while (TRUE)
2944 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002945 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2946
2947 if (r && nread > 0)
2948 return CW_READY;
2949 if (r == 0)
2950 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002951
2952 /* perhaps write some buffer lines */
2953 channel_write_any_lines();
2954
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002955 sleep_time = deadline - GetTickCount();
2956 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002957 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002958 /* Wait for a little while. Very short at first, up to 10 msec
2959 * after looping a few times. */
2960 if (sleep_time > delay)
2961 sleep_time = delay;
2962 Sleep(sleep_time);
2963 delay = delay * 2;
2964 if (delay > 10)
2965 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002966 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002967 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002968 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002969#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002970 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002971#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002972 struct timeval tval;
2973 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002974 fd_set wfds;
2975 int ret;
2976 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002977
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002978 tval.tv_sec = timeout / 1000;
2979 tval.tv_usec = (timeout % 1000) * 1000;
2980 for (;;)
2981 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002982 FD_ZERO(&rfds);
2983 FD_SET((int)fd, &rfds);
2984
2985 /* Write lines to a pipe when a pipe can be written to. Need to
2986 * set this every time, some buffers may be done. */
2987 maxfd = (int)fd + 1;
2988 FD_ZERO(&wfds);
2989 maxfd = channel_fill_wfds(maxfd, &wfds);
2990
2991 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002992# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002993 SOCK_ERRNO;
2994 if (ret == -1 && errno == EINTR)
2995 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002996# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002997 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002998 {
2999 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003000 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003001 channel_write_any_lines();
3002 continue;
3003 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003004 break;
3005 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003006#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003007 for (;;)
3008 {
3009 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3010 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003011
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003012 fds[0].fd = fd;
3013 fds[0].events = POLLIN;
3014 nfd = channel_fill_poll_write(nfd, fds);
3015 if (poll(fds, nfd, timeout) > 0)
3016 {
3017 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003018 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003019 channel_write_any_lines();
3020 continue;
3021 }
3022 break;
3023 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003024#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003025 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003026 return CW_NOT_READY;
3027}
3028
3029 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02003030channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003031{
3032 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003033 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
3034 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003035
3036 /* Queue a "DETACH" netbeans message in the command queue in order to
3037 * terminate the netbeans session later. Do not end the session here
3038 * directly as we may be running in the context of a call to
3039 * netbeans_parse_messages():
3040 * netbeans_parse_messages
3041 * -> autocmd triggered while processing the netbeans cmd
3042 * -> ui_breakcheck
3043 * -> gui event loop or select loop
3044 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003045 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003046 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003047 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02003048 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003049 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3050
3051 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003052 * died. Don't close the channel right away, it may be the wrong moment
3053 * to invoke callbacks. */
3054 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003055
3056#ifdef FEAT_GUI
3057 /* Stop listening to GUI events right away. */
3058 channel_gui_unregister(channel);
3059#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003060}
3061
3062 static void
3063channel_close_now(channel_T *channel)
3064{
3065 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003066 channel_close(channel, TRUE);
3067 if (channel->ch_nb_close_cb != NULL)
3068 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003069}
3070
3071/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003072 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003073 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003074 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003075 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003076 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003077channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003078{
3079 static char_u *buf = NULL;
3080 int len = 0;
3081 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003082 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003083 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003084
Bram Moolenaar5850a762016-05-27 19:59:48 +02003085 /* If we detected a read error don't try reading again. */
3086 if (channel->ch_to_be_closed)
3087 return;
3088
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003089 fd = channel->ch_part[part].ch_fd;
3090 if (fd == INVALID_FD)
3091 {
3092 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003093 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003094 }
3095 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003096
3097 /* Allocate a buffer to read into. */
3098 if (buf == NULL)
3099 {
3100 buf = alloc(MAXMSGSIZE);
3101 if (buf == NULL)
3102 return; /* out of memory! */
3103 }
3104
3105 /* Keep on reading for as long as there is something to read.
3106 * Use select() or poll() to avoid blocking on a message that is exactly
3107 * MAXMSGSIZE long. */
3108 for (;;)
3109 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003110 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003111 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003112 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003113 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003114 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003115 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003116 if (len <= 0)
3117 break; /* error or nothing more to read */
3118
3119 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003120 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003121 readlen += len;
3122 if (len < MAXMSGSIZE)
3123 break; /* did read everything that's available */
3124 }
3125
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003126 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003127 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003128 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003129
3130#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003131 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003132 if (CH_HAS_GUI && gtk_main_level() > 0)
3133 gtk_main_quit();
3134#endif
3135}
3136
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003137/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003138 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003139 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003140 * Returns what was read in allocated memory.
3141 * Returns NULL in case of error or timeout.
3142 */
3143 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003144channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003145{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003146 char_u *buf;
3147 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003148 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003149 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003150 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003151 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003152
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003153 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003154 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003155
3156 while (TRUE)
3157 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003158 node = channel_peek(channel, part);
3159 if (node != NULL)
3160 {
3161 if (mode == MODE_RAW || (mode == MODE_NL
3162 && channel_first_nl(node) != NULL))
3163 /* got a complete message */
3164 break;
3165 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3166 continue;
3167 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003168
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003169 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003170 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003171 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003172 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003173 {
3174 ch_log(channel, "Timed out");
3175 return NULL;
3176 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003177 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003178 }
3179
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003180 if (mode == MODE_RAW)
3181 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003182 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003183 }
3184 else
3185 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003186 char_u *p;
3187
3188 buf = node->rq_buffer;
3189 nl = channel_first_nl(node);
3190
3191 /* Convert NUL to NL, the internal representation. */
3192 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3193 if (*p == NUL)
3194 *p = NL;
3195
3196 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003197 {
3198 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003199 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003200 *nl = NUL;
3201 }
3202 else
3203 {
3204 /* Copy the message into allocated memory and remove it from the
3205 * buffer. */
3206 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003207 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003208 }
3209 }
3210 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003211 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003212 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003213}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003214
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003215/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003216 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003217 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003218 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003219 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003220 */
3221 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003222channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003223 channel_T *channel,
3224 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003225 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003226 int id,
3227 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003228{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003229 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003230 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003231 int timeout;
3232 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003233
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003234 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003235 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003236 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003237 for (;;)
3238 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003239 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003240
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003241 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003242 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003243 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003244 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003245 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003246 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003247
Bram Moolenaard7ece102016-02-02 23:23:02 +01003248 if (!more)
3249 {
3250 /* Handle any other messages in the queue. If done some more
3251 * messages may have arrived. */
3252 if (channel_parse_messages())
3253 continue;
3254
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003255 /* Wait for up to the timeout. If there was an incomplete message
3256 * use the deadline for that. */
3257 timeout = timeout_arg;
3258 if (chanpart->ch_waiting)
3259 {
3260#ifdef WIN32
3261 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3262#else
3263 {
3264 struct timeval now_tv;
3265
3266 gettimeofday(&now_tv, NULL);
3267 timeout = (chanpart->ch_deadline.tv_sec
3268 - now_tv.tv_sec) * 1000
3269 + (chanpart->ch_deadline.tv_usec
3270 - now_tv.tv_usec) / 1000
3271 + 1;
3272 }
3273#endif
3274 if (timeout < 0)
3275 {
3276 /* Something went wrong, channel_parse_json() didn't
3277 * discard message. Cancel waiting. */
3278 chanpart->ch_waiting = FALSE;
3279 timeout = timeout_arg;
3280 }
3281 else if (timeout > timeout_arg)
3282 timeout = timeout_arg;
3283 }
3284 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003285 if (fd == INVALID_FD
3286 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003287 {
3288 if (timeout == timeout_arg)
3289 {
3290 if (fd != INVALID_FD)
3291 ch_log(channel, "Timed out");
3292 break;
3293 }
3294 }
3295 else
3296 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003297 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003298 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003299 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003300 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003301}
3302
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003303/*
3304 * Common for ch_read() and ch_readraw().
3305 */
3306 void
3307common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3308{
3309 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003310 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003311 jobopt_T opt;
3312 int mode;
3313 int timeout;
3314 int id = -1;
3315 typval_T *listtv = NULL;
3316
3317 /* return an empty string by default */
3318 rettv->v_type = VAR_STRING;
3319 rettv->vval.v_string = NULL;
3320
3321 clear_job_options(&opt);
3322 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3323 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003324 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003325
Bram Moolenaar437905c2016-04-26 19:01:05 +02003326 if (opt.jo_set & JO_PART)
3327 part = opt.jo_part;
3328 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003329 if (channel != NULL)
3330 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003331 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003332 part = channel_part_read(channel);
3333 mode = channel_get_mode(channel, part);
3334 timeout = channel_get_timeout(channel, part);
3335 if (opt.jo_set & JO_TIMEOUT)
3336 timeout = opt.jo_timeout;
3337
3338 if (raw || mode == MODE_RAW || mode == MODE_NL)
3339 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3340 else
3341 {
3342 if (opt.jo_set & JO_ID)
3343 id = opt.jo_id;
3344 channel_read_json_block(channel, part, timeout, id, &listtv);
3345 if (listtv != NULL)
3346 {
3347 *rettv = *listtv;
3348 vim_free(listtv);
3349 }
3350 else
3351 {
3352 rettv->v_type = VAR_SPECIAL;
3353 rettv->vval.v_number = VVAL_NONE;
3354 }
3355 }
3356 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003357
3358theend:
3359 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003360}
3361
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003362# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3363 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003364/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003365 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003366 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003367 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003368 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003369channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003370{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003371 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003372 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003373
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003374 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003375 for (channel = first_channel; channel != NULL;
3376 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003377 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003378 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003379 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003380 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003381 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003382 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003383 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003384 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003385 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003386}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003387# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003388
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003389# if defined(WIN32) || defined(PROTO)
3390/*
3391 * Check the channels for anything that is ready to be read.
3392 * The data is put in the read queue.
3393 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003394 void
3395channel_handle_events(void)
3396{
3397 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003398 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003399 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003400
3401 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3402 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003403 /* If we detected a read error don't try reading again. */
3404 if (channel->ch_to_be_closed)
3405 continue;
3406
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003407 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003408 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003409 {
3410 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003411 if (fd != INVALID_FD)
3412 {
3413 int r = channel_wait(channel, fd, 0);
3414
3415 if (r == CW_READY)
3416 channel_read(channel, part, "channel_handle_events");
3417 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003418 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003419 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003420 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003421 }
3422}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003423# endif
3424
Bram Moolenaard04a0202016-01-26 23:30:18 +01003425/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003426 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003427 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003428 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003429 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003430 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003431channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003432{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003433 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003434 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003435
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003436 fd = channel->ch_part[part].ch_fd;
3437 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003438 {
3439 if (!channel->ch_error && fun != NULL)
3440 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003441 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003442 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003443 }
3444 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003445 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003446 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003447
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003448 if (log_fd != NULL)
3449 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003450 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003451 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003452 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003453 fprintf(log_fd, "'\n");
3454 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003455 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003456 }
3457
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003458 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003459 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003460 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003461 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003462 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003463 {
3464 if (!channel->ch_error && fun != NULL)
3465 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003466 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003467 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003468 }
3469 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003470 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003471 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003472
3473 channel->ch_error = FALSE;
3474 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003475}
3476
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003477/*
3478 * Common for "ch_sendexpr()" and "ch_sendraw()".
3479 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003480 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003481 * Otherwise returns NULL.
3482 */
3483 channel_T *
3484send_common(
3485 typval_T *argvars,
3486 char_u *text,
3487 int id,
3488 int eval,
3489 jobopt_T *opt,
3490 char *fun,
3491 int *part_read)
3492{
3493 channel_T *channel;
3494 int part_send;
3495
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003496 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003497 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003498 if (channel == NULL)
3499 return NULL;
3500 part_send = channel_part_send(channel);
3501 *part_read = channel_part_read(channel);
3502
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003503 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3504 return NULL;
3505
3506 /* Set the callback. An empty callback means no callback and not reading
3507 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3508 * allowed. */
3509 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3510 {
3511 if (eval)
3512 {
3513 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3514 return NULL;
3515 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003516 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003517 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003518 }
3519
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003520 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003521 && opt->jo_callback == NULL)
3522 return channel;
3523 return NULL;
3524}
3525
3526/*
3527 * common for "ch_evalexpr()" and "ch_sendexpr()"
3528 */
3529 void
3530ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3531{
3532 char_u *text;
3533 typval_T *listtv;
3534 channel_T *channel;
3535 int id;
3536 ch_mode_T ch_mode;
3537 int part_send;
3538 int part_read;
3539 jobopt_T opt;
3540 int timeout;
3541
3542 /* return an empty string by default */
3543 rettv->v_type = VAR_STRING;
3544 rettv->vval.v_string = NULL;
3545
Bram Moolenaar437905c2016-04-26 19:01:05 +02003546 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003547 if (channel == NULL)
3548 return;
3549 part_send = channel_part_send(channel);
3550
3551 ch_mode = channel_get_mode(channel, part_send);
3552 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3553 {
3554 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3555 return;
3556 }
3557
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003558 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003559 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003560 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003561 if (text == NULL)
3562 return;
3563
3564 channel = send_common(argvars, text, id, eval, &opt,
3565 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3566 vim_free(text);
3567 if (channel != NULL && eval)
3568 {
3569 if (opt.jo_set & JO_TIMEOUT)
3570 timeout = opt.jo_timeout;
3571 else
3572 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003573 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3574 == OK)
3575 {
3576 list_T *list = listtv->vval.v_list;
3577
3578 /* Move the item from the list and then change the type to
3579 * avoid the value being freed. */
3580 *rettv = list->lv_last->li_tv;
3581 list->lv_last->li_tv.v_type = VAR_NUMBER;
3582 free_tv(listtv);
3583 }
3584 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003585 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003586}
3587
3588/*
3589 * common for "ch_evalraw()" and "ch_sendraw()"
3590 */
3591 void
3592ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3593{
3594 char_u buf[NUMBUFLEN];
3595 char_u *text;
3596 channel_T *channel;
3597 int part_read;
3598 jobopt_T opt;
3599 int timeout;
3600
3601 /* return an empty string by default */
3602 rettv->v_type = VAR_STRING;
3603 rettv->vval.v_string = NULL;
3604
3605 text = get_tv_string_buf(&argvars[1], buf);
3606 channel = send_common(argvars, text, 0, eval, &opt,
3607 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3608 if (channel != NULL && eval)
3609 {
3610 if (opt.jo_set & JO_TIMEOUT)
3611 timeout = opt.jo_timeout;
3612 else
3613 timeout = channel_get_timeout(channel, part_read);
3614 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3615 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003616 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003617}
3618
Bram Moolenaard04a0202016-01-26 23:30:18 +01003619# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003620/*
3621 * Add open channels to the poll struct.
3622 * Return the adjusted struct index.
3623 * The type of "fds" is hidden to avoid problems with the function proto.
3624 */
3625 int
3626channel_poll_setup(int nfd_in, void *fds_in)
3627{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003628 int nfd = nfd_in;
3629 channel_T *channel;
3630 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003631 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003632
Bram Moolenaar77073442016-02-13 23:23:53 +01003633 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003634 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003635 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003636 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003637 chanpart_T *ch_part = &channel->ch_part[part];
3638
3639 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003640 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003641 ch_part->ch_poll_idx = nfd;
3642 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003643 fds[nfd].events = POLLIN;
3644 nfd++;
3645 }
3646 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003647 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003648 }
3649 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003650
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003651 nfd = channel_fill_poll_write(nfd, fds);
3652
Bram Moolenaare0874f82016-01-24 20:36:41 +01003653 return nfd;
3654}
3655
3656/*
3657 * The type of "fds" is hidden to avoid problems with the function proto.
3658 */
3659 int
3660channel_poll_check(int ret_in, void *fds_in)
3661{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003662 int ret = ret_in;
3663 channel_T *channel;
3664 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003665 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003666 int idx;
3667 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003668
Bram Moolenaar77073442016-02-13 23:23:53 +01003669 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003670 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003671 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003672 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003673 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003674
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003675 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003676 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003677 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003678 --ret;
3679 }
3680 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003681
3682 in_part = &channel->ch_part[PART_IN];
3683 idx = in_part->ch_poll_idx;
3684 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3685 {
3686 if (in_part->ch_buf_append)
3687 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003688 if (in_part->ch_bufref.br_buf != NULL)
3689 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003690 }
3691 else
3692 channel_write_in(channel);
3693 --ret;
3694 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003695 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003696
3697 return ret;
3698}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003699# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003700
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003701# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003702/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003703 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003704 */
3705 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003706channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003707{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003708 int maxfd = maxfd_in;
3709 channel_T *channel;
3710 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003711 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003712 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003713
Bram Moolenaar77073442016-02-13 23:23:53 +01003714 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003715 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003716 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003717 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003718 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003719
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003720 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003721 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003722 FD_SET((int)fd, rfds);
3723 if (maxfd < (int)fd)
3724 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003725 }
3726 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003727 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003728
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003729 maxfd = channel_fill_wfds(maxfd, wfds);
3730
Bram Moolenaare0874f82016-01-24 20:36:41 +01003731 return maxfd;
3732}
3733
3734/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003735 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003736 */
3737 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003738channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003739{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003740 int ret = ret_in;
3741 channel_T *channel;
3742 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003743 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003744 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003745 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003746
Bram Moolenaar77073442016-02-13 23:23:53 +01003747 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003748 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003749 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003750 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003751 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003752
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003753 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003754 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003755 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003756 --ret;
3757 }
3758 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003759
3760 in_part = &channel->ch_part[PART_IN];
3761 if (ret > 0 && in_part->ch_fd != INVALID_FD
3762 && FD_ISSET(in_part->ch_fd, wfds))
3763 {
3764 if (in_part->ch_buf_append)
3765 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003766 if (in_part->ch_bufref.br_buf != NULL)
3767 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003768 }
3769 else
3770 channel_write_in(channel);
3771 --ret;
3772 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003773 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003774
3775 return ret;
3776}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003777# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003778
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003779/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003780 * Execute queued up commands.
3781 * Invoked from the main loop when it's safe to execute received commands.
3782 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003783 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003784 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003785channel_parse_messages(void)
3786{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003787 channel_T *channel = first_channel;
3788 int ret = FALSE;
3789 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003790 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003791
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003792 ++safe_to_invoke_callback;
3793
Bram Moolenaard0b65022016-03-06 21:50:33 +01003794 /* Only do this message when another message was given, otherwise we get
3795 * lots of them. */
3796 if (did_log_msg)
3797 {
3798 ch_log(NULL, "looking for messages on channels");
3799 did_log_msg = FALSE;
3800 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003801 while (channel != NULL)
3802 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003803 if (channel->ch_to_be_closed)
3804 {
3805 channel->ch_to_be_closed = FALSE;
3806 channel_close_now(channel);
3807 /* channel may have been freed, start over */
3808 channel = first_channel;
3809 continue;
3810 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003811 if (channel->ch_to_be_freed)
3812 {
3813 channel_free(channel);
3814 /* channel has been freed, start over */
3815 channel = first_channel;
3816 continue;
3817 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003818 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003819 {
3820 /* channel is no longer useful, free it */
3821 channel_free(channel);
3822 channel = first_channel;
3823 part = PART_SOCK;
3824 continue;
3825 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003826 if (channel->ch_part[part].ch_fd != INVALID_FD
3827 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003828 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003829 /* Increase the refcount, in case the handler causes the channel
3830 * to be unreferenced or closed. */
3831 ++channel->ch_refcount;
3832 r = may_invoke_callback(channel, part);
3833 if (r == OK)
3834 ret = TRUE;
3835 if (channel_unref(channel) || r == OK)
3836 {
3837 /* channel was freed or something was done, start over */
3838 channel = first_channel;
3839 part = PART_SOCK;
3840 continue;
3841 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003842 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003843 if (part < PART_ERR)
3844 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003845 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003846 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003847 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003848 part = PART_SOCK;
3849 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003850 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003851
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003852 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003853 {
3854 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003855 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003856 }
3857
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003858 --safe_to_invoke_callback;
3859
Bram Moolenaard7ece102016-02-02 23:23:02 +01003860 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003861}
3862
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003863/*
3864 * Mark references to lists used in channels.
3865 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003866 int
3867set_ref_in_channel(int copyID)
3868{
Bram Moolenaar77073442016-02-13 23:23:53 +01003869 int abort = FALSE;
3870 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003871 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003872
Bram Moolenaar77073442016-02-13 23:23:53 +01003873 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003874 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003875 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003876 tv.v_type = VAR_CHANNEL;
3877 tv.vval.v_channel = channel;
3878 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003879 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003880 return abort;
3881}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003882
3883/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003884 * Return the "part" to write to for "channel".
3885 */
3886 int
3887channel_part_send(channel_T *channel)
3888{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003889 if (channel->CH_SOCK_FD == INVALID_FD)
3890 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003891 return PART_SOCK;
3892}
3893
3894/*
3895 * Return the default "part" to read from for "channel".
3896 */
3897 int
3898channel_part_read(channel_T *channel)
3899{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003900 if (channel->CH_SOCK_FD == INVALID_FD)
3901 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003902 return PART_SOCK;
3903}
3904
3905/*
3906 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003907 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003908 */
3909 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003910channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003911{
Bram Moolenaar77073442016-02-13 23:23:53 +01003912 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003913 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003914 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003915}
3916
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003917/*
3918 * Return the timeout of "channel"/"part"
3919 */
3920 int
3921channel_get_timeout(channel_T *channel, int part)
3922{
3923 return channel->ch_part[part].ch_timeout;
3924}
3925
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003926 static int
3927handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3928{
3929 char_u *val = get_tv_string(item);
3930
3931 opt->jo_set |= jo;
3932 if (STRCMP(val, "nl") == 0)
3933 *modep = MODE_NL;
3934 else if (STRCMP(val, "raw") == 0)
3935 *modep = MODE_RAW;
3936 else if (STRCMP(val, "js") == 0)
3937 *modep = MODE_JS;
3938 else if (STRCMP(val, "json") == 0)
3939 *modep = MODE_JSON;
3940 else
3941 {
3942 EMSG2(_(e_invarg2), val);
3943 return FAIL;
3944 }
3945 return OK;
3946}
3947
3948 static int
3949handle_io(typval_T *item, int part, jobopt_T *opt)
3950{
3951 char_u *val = get_tv_string(item);
3952
3953 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3954 if (STRCMP(val, "null") == 0)
3955 opt->jo_io[part] = JIO_NULL;
3956 else if (STRCMP(val, "pipe") == 0)
3957 opt->jo_io[part] = JIO_PIPE;
3958 else if (STRCMP(val, "file") == 0)
3959 opt->jo_io[part] = JIO_FILE;
3960 else if (STRCMP(val, "buffer") == 0)
3961 opt->jo_io[part] = JIO_BUFFER;
3962 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3963 opt->jo_io[part] = JIO_OUT;
3964 else
3965 {
3966 EMSG2(_(e_invarg2), val);
3967 return FAIL;
3968 }
3969 return OK;
3970}
3971
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003972/*
3973 * Clear a jobopt_T before using it.
3974 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003975 void
3976clear_job_options(jobopt_T *opt)
3977{
3978 vim_memset(opt, 0, sizeof(jobopt_T));
3979}
3980
3981/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003982 * Free any members of a jobopt_T.
3983 */
3984 void
3985free_job_options(jobopt_T *opt)
3986{
3987 if (opt->jo_partial != NULL)
3988 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003989 else if (opt->jo_callback != NULL)
3990 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003991 if (opt->jo_out_partial != NULL)
3992 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003993 else if (opt->jo_out_cb != NULL)
3994 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003995 if (opt->jo_err_partial != NULL)
3996 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003997 else if (opt->jo_err_cb != NULL)
3998 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003999 if (opt->jo_close_partial != NULL)
4000 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004001 else if (opt->jo_close_cb != NULL)
4002 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004003 if (opt->jo_exit_partial != NULL)
4004 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004005 else if (opt->jo_exit_cb != NULL)
4006 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004007}
4008
4009/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004010 * Get the PART_ number from the first character of an option name.
4011 */
4012 static int
4013part_from_char(int c)
4014{
4015 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4016}
4017
4018/*
4019 * Get the option entries from the dict in "tv", parse them and put the result
4020 * in "opt".
4021 * Only accept options in "supported".
4022 * If an option value is invalid return FAIL.
4023 */
4024 int
4025get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4026{
4027 typval_T *item;
4028 char_u *val;
4029 dict_T *dict;
4030 int todo;
4031 hashitem_T *hi;
4032 int part;
4033
4034 opt->jo_set = 0;
4035 if (tv->v_type == VAR_UNKNOWN)
4036 return OK;
4037 if (tv->v_type != VAR_DICT)
4038 {
4039 EMSG(_(e_invarg));
4040 return FAIL;
4041 }
4042 dict = tv->vval.v_dict;
4043 if (dict == NULL)
4044 return OK;
4045
4046 todo = (int)dict->dv_hashtab.ht_used;
4047 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4048 if (!HASHITEM_EMPTY(hi))
4049 {
4050 item = &dict_lookup(hi)->di_tv;
4051
4052 if (STRCMP(hi->hi_key, "mode") == 0)
4053 {
4054 if (!(supported & JO_MODE))
4055 break;
4056 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4057 return FAIL;
4058 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004059 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004060 {
4061 if (!(supported & JO_IN_MODE))
4062 break;
4063 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4064 == FAIL)
4065 return FAIL;
4066 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004067 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004068 {
4069 if (!(supported & JO_OUT_MODE))
4070 break;
4071 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4072 == FAIL)
4073 return FAIL;
4074 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004075 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 {
4077 if (!(supported & JO_ERR_MODE))
4078 break;
4079 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4080 == FAIL)
4081 return FAIL;
4082 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004083 else if (STRCMP(hi->hi_key, "in_io") == 0
4084 || STRCMP(hi->hi_key, "out_io") == 0
4085 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004086 {
4087 if (!(supported & JO_OUT_IO))
4088 break;
4089 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4090 return FAIL;
4091 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004092 else if (STRCMP(hi->hi_key, "in_name") == 0
4093 || STRCMP(hi->hi_key, "out_name") == 0
4094 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004095 {
4096 part = part_from_char(*hi->hi_key);
4097
4098 if (!(supported & JO_OUT_IO))
4099 break;
4100 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4101 opt->jo_io_name[part] =
4102 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4103 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004104 else if (STRCMP(hi->hi_key, "in_buf") == 0
4105 || STRCMP(hi->hi_key, "out_buf") == 0
4106 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004107 {
4108 part = part_from_char(*hi->hi_key);
4109
4110 if (!(supported & JO_OUT_IO))
4111 break;
4112 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4113 opt->jo_io_buf[part] = get_tv_number(item);
4114 if (opt->jo_io_buf[part] <= 0)
4115 {
4116 EMSG2(_(e_invarg2), get_tv_string(item));
4117 return FAIL;
4118 }
4119 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4120 {
4121 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4122 return FAIL;
4123 }
4124 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004125 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4126 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4127 {
4128 part = part_from_char(*hi->hi_key);
4129
4130 if (!(supported & JO_OUT_IO))
4131 break;
4132 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4133 opt->jo_modifiable[part] = get_tv_number(item);
4134 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004135 else if (STRCMP(hi->hi_key, "out_msg") == 0
4136 || STRCMP(hi->hi_key, "err_msg") == 0)
4137 {
4138 part = part_from_char(*hi->hi_key);
4139
4140 if (!(supported & JO_OUT_IO))
4141 break;
4142 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4143 opt->jo_message[part] = get_tv_number(item);
4144 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004145 else if (STRCMP(hi->hi_key, "in_top") == 0
4146 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004147 {
4148 linenr_T *lp;
4149
4150 if (!(supported & JO_OUT_IO))
4151 break;
4152 if (hi->hi_key[3] == 't')
4153 {
4154 lp = &opt->jo_in_top;
4155 opt->jo_set |= JO_IN_TOP;
4156 }
4157 else
4158 {
4159 lp = &opt->jo_in_bot;
4160 opt->jo_set |= JO_IN_BOT;
4161 }
4162 *lp = get_tv_number(item);
4163 if (*lp < 0)
4164 {
4165 EMSG2(_(e_invarg2), get_tv_string(item));
4166 return FAIL;
4167 }
4168 }
4169 else if (STRCMP(hi->hi_key, "channel") == 0)
4170 {
4171 if (!(supported & JO_OUT_IO))
4172 break;
4173 opt->jo_set |= JO_CHANNEL;
4174 if (item->v_type != VAR_CHANNEL)
4175 {
4176 EMSG2(_(e_invarg2), "channel");
4177 return FAIL;
4178 }
4179 opt->jo_channel = item->vval.v_channel;
4180 }
4181 else if (STRCMP(hi->hi_key, "callback") == 0)
4182 {
4183 if (!(supported & JO_CALLBACK))
4184 break;
4185 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004186 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004187 if (opt->jo_callback == NULL)
4188 {
4189 EMSG2(_(e_invarg2), "callback");
4190 return FAIL;
4191 }
4192 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004193 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004194 {
4195 if (!(supported & JO_OUT_CALLBACK))
4196 break;
4197 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004198 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004199 if (opt->jo_out_cb == NULL)
4200 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004201 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004202 return FAIL;
4203 }
4204 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004205 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004206 {
4207 if (!(supported & JO_ERR_CALLBACK))
4208 break;
4209 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004210 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004211 if (opt->jo_err_cb == NULL)
4212 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004213 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004214 return FAIL;
4215 }
4216 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004217 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004218 {
4219 if (!(supported & JO_CLOSE_CALLBACK))
4220 break;
4221 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004222 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004223 if (opt->jo_close_cb == NULL)
4224 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004225 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004226 return FAIL;
4227 }
4228 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004229 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4230 {
4231 if (!(supported & JO_EXIT_CB))
4232 break;
4233 opt->jo_set |= JO_EXIT_CB;
4234 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4235 if (opt->jo_exit_cb == NULL)
4236 {
4237 EMSG2(_(e_invarg2), "exit_cb");
4238 return FAIL;
4239 }
4240 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004241 else if (STRCMP(hi->hi_key, "waittime") == 0)
4242 {
4243 if (!(supported & JO_WAITTIME))
4244 break;
4245 opt->jo_set |= JO_WAITTIME;
4246 opt->jo_waittime = get_tv_number(item);
4247 }
4248 else if (STRCMP(hi->hi_key, "timeout") == 0)
4249 {
4250 if (!(supported & JO_TIMEOUT))
4251 break;
4252 opt->jo_set |= JO_TIMEOUT;
4253 opt->jo_timeout = get_tv_number(item);
4254 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004255 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004256 {
4257 if (!(supported & JO_OUT_TIMEOUT))
4258 break;
4259 opt->jo_set |= JO_OUT_TIMEOUT;
4260 opt->jo_out_timeout = get_tv_number(item);
4261 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004262 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004263 {
4264 if (!(supported & JO_ERR_TIMEOUT))
4265 break;
4266 opt->jo_set |= JO_ERR_TIMEOUT;
4267 opt->jo_err_timeout = get_tv_number(item);
4268 }
4269 else if (STRCMP(hi->hi_key, "part") == 0)
4270 {
4271 if (!(supported & JO_PART))
4272 break;
4273 opt->jo_set |= JO_PART;
4274 val = get_tv_string(item);
4275 if (STRCMP(val, "err") == 0)
4276 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004277 else if (STRCMP(val, "out") == 0)
4278 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004279 else
4280 {
4281 EMSG2(_(e_invarg2), val);
4282 return FAIL;
4283 }
4284 }
4285 else if (STRCMP(hi->hi_key, "id") == 0)
4286 {
4287 if (!(supported & JO_ID))
4288 break;
4289 opt->jo_set |= JO_ID;
4290 opt->jo_id = get_tv_number(item);
4291 }
4292 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4293 {
4294 if (!(supported & JO_STOPONEXIT))
4295 break;
4296 opt->jo_set |= JO_STOPONEXIT;
4297 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4298 opt->jo_soe_buf);
4299 if (opt->jo_stoponexit == NULL)
4300 {
4301 EMSG2(_(e_invarg2), "stoponexit");
4302 return FAIL;
4303 }
4304 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004305 else if (STRCMP(hi->hi_key, "block_write") == 0)
4306 {
4307 if (!(supported & JO_BLOCK_WRITE))
4308 break;
4309 opt->jo_set |= JO_BLOCK_WRITE;
4310 opt->jo_block_write = get_tv_number(item);
4311 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004312 else
4313 break;
4314 --todo;
4315 }
4316 if (todo > 0)
4317 {
4318 EMSG2(_(e_invarg2), hi->hi_key);
4319 return FAIL;
4320 }
4321
4322 return OK;
4323}
4324
4325/*
4326 * Get the channel from the argument.
4327 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004328 * When "check_open" is TRUE check that the channel can be used.
4329 * When "reading" is TRUE "check_open" considers typeahead useful.
4330 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004331 */
4332 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004333get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004334{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004335 channel_T *channel = NULL;
4336 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004337
4338 if (tv->v_type == VAR_JOB)
4339 {
4340 if (tv->vval.v_job != NULL)
4341 channel = tv->vval.v_job->jv_channel;
4342 }
4343 else if (tv->v_type == VAR_CHANNEL)
4344 {
4345 channel = tv->vval.v_channel;
4346 }
4347 else
4348 {
4349 EMSG2(_(e_invarg2), get_tv_string(tv));
4350 return NULL;
4351 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004352 if (channel != NULL && reading)
4353 has_readahead = channel_has_readahead(channel,
4354 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004355
Bram Moolenaar437905c2016-04-26 19:01:05 +02004356 if (check_open && (channel == NULL || (!channel_is_open(channel)
4357 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 {
4359 EMSG(_("E906: not an open channel"));
4360 return NULL;
4361 }
4362 return channel;
4363}
4364
4365static job_T *first_job = NULL;
4366
4367 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004368job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004369{
4370 ch_log(job->jv_channel, "Freeing job");
4371 if (job->jv_channel != NULL)
4372 {
4373 /* The link from the channel to the job doesn't count as a reference,
4374 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004375 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004376 * NULL the reference. We don't set ch_job_killed, unreferencing the
4377 * job doesn't mean it stops running. */
4378 job->jv_channel->ch_job = NULL;
4379 channel_unref(job->jv_channel);
4380 }
4381 mch_clear_job(job);
4382
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004383 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004384 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004385}
4386
4387 static void
4388job_free_job(job_T *job)
4389{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004390 if (job->jv_next != NULL)
4391 job->jv_next->jv_prev = job->jv_prev;
4392 if (job->jv_prev == NULL)
4393 first_job = job->jv_next;
4394 else
4395 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004396 vim_free(job);
4397}
4398
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004399 static void
4400job_free(job_T *job)
4401{
4402 if (!in_free_unref_items)
4403 {
4404 job_free_contents(job);
4405 job_free_job(job);
4406 }
4407}
4408
Bram Moolenaar655da312016-05-28 22:22:34 +02004409#if defined(EXITFREE) || defined(PROTO)
4410 void
4411job_free_all(void)
4412{
4413 while (first_job != NULL)
4414 job_free(first_job);
4415}
4416#endif
4417
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004418/*
4419 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004420 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4421 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004422 */
4423 static int
4424job_still_useful(job_T *job)
4425{
4426 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004427 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4428 || (job->jv_channel != NULL
4429 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004430}
4431
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004432/*
4433 * Mark references in jobs that are still useful.
4434 */
4435 int
4436set_ref_in_job(int copyID)
4437{
4438 int abort = FALSE;
4439 job_T *job;
4440 typval_T tv;
4441
4442 for (job = first_job; job != NULL; job = job->jv_next)
4443 if (job_still_useful(job))
4444 {
4445 tv.v_type = VAR_JOB;
4446 tv.vval.v_job = job;
4447 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4448 }
4449 return abort;
4450}
4451
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004452 void
4453job_unref(job_T *job)
4454{
4455 if (job != NULL && --job->jv_refcount <= 0)
4456 {
4457 /* Do not free the job when it has not ended yet and there is a
4458 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004459 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004460 {
4461 job_free(job);
4462 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004463 else if (job->jv_channel != NULL
4464 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004465 {
4466 /* Do remove the link to the channel, otherwise it hangs
4467 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004468 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004469 job->jv_channel->ch_job = NULL;
4470 channel_unref(job->jv_channel);
4471 job->jv_channel = NULL;
4472 }
4473 }
4474}
4475
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004476 int
4477free_unused_jobs_contents(int copyID, int mask)
4478{
4479 int did_free = FALSE;
4480 job_T *job;
4481
4482 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004483 if ((job->jv_copyID & mask) != (copyID & mask)
4484 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004485 {
4486 /* Free the channel and ordinary items it contains, but don't
4487 * recurse into Lists, Dictionaries etc. */
4488 job_free_contents(job);
4489 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004490 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491 return did_free;
4492}
4493
4494 void
4495free_unused_jobs(int copyID, int mask)
4496{
4497 job_T *job;
4498 job_T *job_next;
4499
4500 for (job = first_job; job != NULL; job = job_next)
4501 {
4502 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004503 if ((job->jv_copyID & mask) != (copyID & mask)
4504 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004505 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004506 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004507 job_free_job(job);
4508 }
4509 }
4510}
4511
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004512/*
4513 * Allocate a job. Sets the refcount to one and sets options default.
4514 */
4515 static job_T *
4516job_alloc(void)
4517{
4518 job_T *job;
4519
4520 job = (job_T *)alloc_clear(sizeof(job_T));
4521 if (job != NULL)
4522 {
4523 job->jv_refcount = 1;
4524 job->jv_stoponexit = vim_strsave((char_u *)"term");
4525
4526 if (first_job != NULL)
4527 {
4528 first_job->jv_prev = job;
4529 job->jv_next = first_job;
4530 }
4531 first_job = job;
4532 }
4533 return job;
4534}
4535
4536 void
4537job_set_options(job_T *job, jobopt_T *opt)
4538{
4539 if (opt->jo_set & JO_STOPONEXIT)
4540 {
4541 vim_free(job->jv_stoponexit);
4542 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4543 job->jv_stoponexit = NULL;
4544 else
4545 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4546 }
4547 if (opt->jo_set & JO_EXIT_CB)
4548 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004549 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004550 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004551 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004552 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004553 job->jv_exit_partial = NULL;
4554 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004555 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004556 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004557 job->jv_exit_partial = opt->jo_exit_partial;
4558 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004559 {
4560 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004561 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004562 }
4563 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004564 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004565 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004566 func_ref(job->jv_exit_cb);
4567 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004568 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004569 }
4570}
4571
4572/*
4573 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4574 */
4575 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004576job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004577{
4578 job_T *job;
4579
4580 for (job = first_job; job != NULL; job = job->jv_next)
4581 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4582 mch_stop_job(job, job->jv_stoponexit);
4583}
4584
4585/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004586 * Return TRUE when there is any job that might exit, which means
4587 * job_check_ended() should be called once in a while.
4588 */
4589 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004590has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004591{
4592 job_T *job;
4593
4594 for (job = first_job; job != NULL; job = job->jv_next)
4595 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4596 return TRUE;
4597 return FALSE;
4598}
4599
4600/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004601 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004602 */
4603 void
4604job_check_ended(void)
4605{
4606 static time_t last_check = 0;
4607 time_t now;
4608 job_T *job;
4609 job_T *next;
4610
4611 /* Only do this once in 10 seconds. */
4612 now = time(NULL);
4613 if (last_check + 10 < now)
4614 {
4615 last_check = now;
4616 for (job = first_job; job != NULL; job = next)
4617 {
4618 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004619 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004620 job_status(job); /* may free "job" */
4621 }
4622 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004623 if (channel_need_redraw)
4624 {
4625 channel_need_redraw = FALSE;
4626 redraw_after_callback();
4627 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004628}
4629
4630/*
4631 * "job_start()" function
4632 */
4633 job_T *
4634job_start(typval_T *argvars)
4635{
4636 job_T *job;
4637 char_u *cmd = NULL;
4638#if defined(UNIX)
4639# define USE_ARGV
4640 char **argv = NULL;
4641 int argc = 0;
4642#else
4643 garray_T ga;
4644#endif
4645 jobopt_T opt;
4646 int part;
4647
4648 job = job_alloc();
4649 if (job == NULL)
4650 return NULL;
4651
4652 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004653#ifndef USE_ARGV
4654 ga_init2(&ga, (int)sizeof(char*), 20);
4655#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004656
4657 /* Default mode is NL. */
4658 clear_job_options(&opt);
4659 opt.jo_mode = MODE_NL;
4660 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004661 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4662 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004663 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004664
4665 /* Check that when io is "file" that there is a file name. */
4666 for (part = PART_OUT; part <= PART_IN; ++part)
4667 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4668 && opt.jo_io[part] == JIO_FILE
4669 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4670 || *opt.jo_io_name[part] == NUL))
4671 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004672 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004673 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004674 }
4675
4676 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4677 {
4678 buf_T *buf = NULL;
4679
4680 /* check that we can find the buffer before starting the job */
4681 if (opt.jo_set & JO_IN_BUF)
4682 {
4683 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4684 if (buf == NULL)
4685 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4686 }
4687 else if (!(opt.jo_set & JO_IN_NAME))
4688 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004689 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004690 }
4691 else
4692 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4693 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004694 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004695 if (buf->b_ml.ml_mfp == NULL)
4696 {
4697 char_u numbuf[NUMBUFLEN];
4698 char_u *s;
4699
4700 if (opt.jo_set & JO_IN_BUF)
4701 {
4702 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4703 s = numbuf;
4704 }
4705 else
4706 s = opt.jo_io_name[PART_IN];
4707 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004708 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004709 }
4710 job->jv_in_buf = buf;
4711 }
4712
4713 job_set_options(job, &opt);
4714
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004715 if (argvars[0].v_type == VAR_STRING)
4716 {
4717 /* Command is a string. */
4718 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004719 if (cmd == NULL || *cmd == NUL)
4720 {
4721 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004722 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004723 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004724#ifdef USE_ARGV
4725 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004726 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727 argv[argc] = NULL;
4728#endif
4729 }
4730 else if (argvars[0].v_type != VAR_LIST
4731 || argvars[0].vval.v_list == NULL
4732 || argvars[0].vval.v_list->lv_len < 1)
4733 {
4734 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004735 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004736 }
4737 else
4738 {
4739 list_T *l = argvars[0].vval.v_list;
4740 listitem_T *li;
4741 char_u *s;
4742
4743#ifdef USE_ARGV
4744 /* Pass argv[] to mch_call_shell(). */
4745 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4746 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004747 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004748#endif
4749 for (li = l->lv_first; li != NULL; li = li->li_next)
4750 {
4751 s = get_tv_string_chk(&li->li_tv);
4752 if (s == NULL)
4753 goto theend;
4754#ifdef USE_ARGV
4755 argv[argc++] = (char *)s;
4756#else
4757 /* Only escape when needed, double quotes are not always allowed. */
4758 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4759 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004760# ifdef WIN32
4761 int old_ssl = p_ssl;
4762
4763 /* This is using CreateProcess, not cmd.exe. Always use
4764 * double quote and backslashes. */
4765 p_ssl = 0;
4766# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004767 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004768# ifdef WIN32
4769 p_ssl = old_ssl;
4770# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004771 if (s == NULL)
4772 goto theend;
4773 ga_concat(&ga, s);
4774 vim_free(s);
4775 }
4776 else
4777 ga_concat(&ga, s);
4778 if (li->li_next != NULL)
4779 ga_append(&ga, ' ');
4780#endif
4781 }
4782#ifdef USE_ARGV
4783 argv[argc] = NULL;
4784#else
4785 cmd = ga.ga_data;
4786#endif
4787 }
4788
4789#ifdef USE_ARGV
4790 if (ch_log_active())
4791 {
4792 garray_T ga;
4793 int i;
4794
4795 ga_init2(&ga, (int)sizeof(char), 200);
4796 for (i = 0; i < argc; ++i)
4797 {
4798 if (i > 0)
4799 ga_concat(&ga, (char_u *)" ");
4800 ga_concat(&ga, (char_u *)argv[i]);
4801 }
4802 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4803 ga_clear(&ga);
4804 }
4805 mch_start_job(argv, job, &opt);
4806#else
4807 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4808 mch_start_job((char *)cmd, job, &opt);
4809#endif
4810
4811 /* If the channel is reading from a buffer, write lines now. */
4812 if (job->jv_channel != NULL)
4813 channel_write_in(job->jv_channel);
4814
4815theend:
4816#ifdef USE_ARGV
4817 vim_free(argv);
4818#else
4819 vim_free(ga.ga_data);
4820#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004821 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004822 return job;
4823}
4824
4825/*
4826 * Get the status of "job" and invoke the exit callback when needed.
4827 * The returned string is not allocated.
4828 */
4829 char *
4830job_status(job_T *job)
4831{
4832 char *result;
4833
4834 if (job->jv_status == JOB_ENDED)
4835 /* No need to check, dead is dead. */
4836 result = "dead";
4837 else if (job->jv_status == JOB_FAILED)
4838 result = "fail";
4839 else
4840 {
4841 result = mch_job_status(job);
4842 if (job->jv_status == JOB_ENDED)
4843 ch_log(job->jv_channel, "Job ended");
4844 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4845 {
4846 typval_T argv[3];
4847 typval_T rettv;
4848 int dummy;
4849
4850 /* invoke the exit callback; make sure the refcount is > 0 */
4851 ++job->jv_refcount;
4852 argv[0].v_type = VAR_JOB;
4853 argv[0].vval.v_job = job;
4854 argv[1].v_type = VAR_NUMBER;
4855 argv[1].vval.v_number = job->jv_exitval;
4856 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004857 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004858 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004859 clear_tv(&rettv);
4860 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004861 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004862 }
4863 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4864 {
4865 /* The job was already unreferenced, now that it ended it can be
4866 * freed. Careful: caller must not use "job" after this! */
4867 job_free(job);
4868 }
4869 }
4870 return result;
4871}
4872
Bram Moolenaar8950a562016-03-12 15:22:55 +01004873/*
4874 * Implementation of job_info().
4875 */
4876 void
4877job_info(job_T *job, dict_T *dict)
4878{
4879 dictitem_T *item;
4880 varnumber_T nr;
4881
4882 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4883
4884 item = dictitem_alloc((char_u *)"channel");
4885 if (item == NULL)
4886 return;
4887 item->di_tv.v_lock = 0;
4888 item->di_tv.v_type = VAR_CHANNEL;
4889 item->di_tv.vval.v_channel = job->jv_channel;
4890 if (job->jv_channel != NULL)
4891 ++job->jv_channel->ch_refcount;
4892 if (dict_add(dict, item) == FAIL)
4893 dictitem_free(item);
4894
4895#ifdef UNIX
4896 nr = job->jv_pid;
4897#else
4898 nr = job->jv_proc_info.dwProcessId;
4899#endif
4900 dict_add_nr_str(dict, "process", nr, NULL);
4901
4902 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004903 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004904 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4905}
4906
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004907 int
4908job_stop(job_T *job, typval_T *argvars)
4909{
4910 char_u *arg;
4911
4912 if (argvars[1].v_type == VAR_UNKNOWN)
4913 arg = (char_u *)"";
4914 else
4915 {
4916 arg = get_tv_string_chk(&argvars[1]);
4917 if (arg == NULL)
4918 {
4919 EMSG(_(e_invarg));
4920 return 0;
4921 }
4922 }
4923 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4924 if (mch_stop_job(job, arg) == FAIL)
4925 return 0;
4926
4927 /* Assume that "hup" does not kill the job. */
4928 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4929 job->jv_channel->ch_job_killed = TRUE;
4930
4931 /* We don't try freeing the job, obviously the caller still has a
4932 * reference to it. */
4933 return 1;
4934}
4935
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004936#endif /* FEAT_JOB_CHANNEL */