blob: 778a30e17c552a6da458999c54a2e83ca07ad453 [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
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaard04a0202016-01-26 23:30:18 +010023#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 Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
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 Moolenaardc0ccae2016-10-09 17:28:01 +0200312 ch_part_T 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 Moolenaardc0ccae2016-10-09 17:28:01 +0200321 for (part = PART_SOCK; part < PART_COUNT; ++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)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200424 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425 else
426 {
427 channel_free_contents(channel);
428 channel_free_channel(channel);
429 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200430 }
431}
432
433/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100435 * possible, there is no callback to be invoked or the associated job was
436 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100437 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100439 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440channel_may_free(channel_T *channel)
441{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100442 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100443 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100444 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100445 return TRUE;
446 }
447 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100448}
449
450/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100451 * Decrement the reference count on "channel" and maybe free it when it goes
452 * down to zero. Don't free it if there is a pending action.
453 * Returns TRUE when the channel is no longer referenced.
454 */
455 int
456channel_unref(channel_T *channel)
457{
458 if (channel != NULL && --channel->ch_refcount <= 0)
459 return channel_may_free(channel);
460 return FALSE;
461}
462
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200463 int
464free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100465{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 int did_free = FALSE;
467 channel_T *ch;
468
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200469 /* This is invoked from the garbage collector, which only runs at a safe
470 * point. */
471 ++safe_to_invoke_callback;
472
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200473 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200474 if (!channel_still_useful(ch)
475 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200476 {
477 /* Free the channel and ordinary items it contains, but don't
478 * recurse into Lists, Dictionaries etc. */
479 channel_free_contents(ch);
480 did_free = TRUE;
481 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200482
483 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200484 return did_free;
485}
486
487 void
488free_unused_channels(int copyID, int mask)
489{
490 channel_T *ch;
491 channel_T *ch_next;
492
493 for (ch = first_channel; ch != NULL; ch = ch_next)
494 {
495 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200496 if (!channel_still_useful(ch)
497 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200498 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200499 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200500 channel_free_channel(ch);
501 }
502 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100503}
504
Bram Moolenaard04a0202016-01-26 23:30:18 +0100505#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100506
507#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
508 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100509channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100510{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100511 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200512 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100513
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100514 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100516 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100517 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200518 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100519}
520#endif
521
Bram Moolenaare0874f82016-01-24 20:36:41 +0100522/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100523 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100524 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525#ifdef FEAT_GUI_X11
526 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527messageFromServer(XtPointer clientData,
528 int *unused1 UNUSED,
529 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100530{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100531 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100532}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100533#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100534
Bram Moolenaard04a0202016-01-26 23:30:18 +0100535#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100536# if GTK_CHECK_VERSION(3,0,0)
537 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200538messageFromServer(GIOChannel *unused1 UNUSED,
539 GIOCondition unused2 UNUSED,
540 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100541{
542 channel_read_fd(GPOINTER_TO_INT(clientData));
543 return TRUE; /* Return FALSE instead in case the event source is to
544 * be removed after this function returns. */
545}
546# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100547 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200548messageFromServer(gpointer clientData,
549 gint unused1 UNUSED,
550 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100551{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100552 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100553}
Bram Moolenaar98921892016-02-23 17:14:37 +0100554# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100555#endif
556
557 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200558channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100559{
Bram Moolenaarde279892016-03-11 22:19:44 +0100560 if (!CH_HAS_GUI)
561 return;
562
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100563# ifdef FEAT_GUI_X11
564 /* Tell notifier we are interested in being called
565 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100566 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
567 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100568 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100569 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100570 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200571 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100572 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100573# else
574# ifdef FEAT_GUI_GTK
575 /* Tell gdk we are interested in being called when there
576 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100577 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100578# if GTK_CHECK_VERSION(3,0,0)
579 {
580 GIOChannel *chnnl = g_io_channel_unix_new(
581 (gint)channel->ch_part[part].ch_fd);
582
583 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
584 chnnl,
585 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200586 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100587 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
588
589 g_io_channel_unref(chnnl);
590 }
591# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100592 channel->ch_part[part].ch_inputHandler = gdk_input_add(
593 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100594 (GdkInputCondition)
595 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200596 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100597 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100598# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100599# endif
600# endif
601}
602
Bram Moolenaarde279892016-03-11 22:19:44 +0100603 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100604channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100605{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100606 if (channel->CH_SOCK_FD != INVALID_FD)
607 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100608 if (channel->CH_OUT_FD != INVALID_FD)
609 channel_gui_register_one(channel, PART_OUT);
610 if (channel->CH_ERR_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100612}
613
614/*
615 * Register any of our file descriptors with the GUI event handling system.
616 * Called when the GUI has started.
617 */
618 void
619channel_gui_register_all(void)
620{
Bram Moolenaar77073442016-02-13 23:23:53 +0100621 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100622
Bram Moolenaar77073442016-02-13 23:23:53 +0100623 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100624 channel_gui_register(channel);
625}
626
627 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200628channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100629{
630# ifdef FEAT_GUI_X11
631 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
632 {
633 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
634 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
635 }
636# else
637# ifdef FEAT_GUI_GTK
638 if (channel->ch_part[part].ch_inputHandler != 0)
639 {
640# if GTK_CHECK_VERSION(3,0,0)
641 g_source_remove(channel->ch_part[part].ch_inputHandler);
642# else
643 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
644# endif
645 channel->ch_part[part].ch_inputHandler = 0;
646 }
647# endif
648# endif
649}
650
651 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100652channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100653{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200654 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100655
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100656 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100657 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658}
659
660#endif
661
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100662static char *e_cannot_connect = N_("E902: Cannot connect to port");
663
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100665 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100666 * "waittime" is the time in msec to wait for the connection.
667 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100668 * Returns the channel for success.
669 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100670 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100671 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100672channel_open(
673 char *hostname,
674 int port_in,
675 int waittime,
676 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100677{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100678 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100680 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100681#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100682 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100683 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100684#else
685 int port = port_in;
686#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100687 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100688 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100689
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100690#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691 channel_init_winsock();
692#endif
693
Bram Moolenaar77073442016-02-13 23:23:53 +0100694 channel = add_channel();
695 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100697 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100698 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100699 }
700
701 /* Get the server internet address and put into addr structure */
702 /* fill in the socket address structure and connect to server */
703 vim_memset((char *)&server, 0, sizeof(server));
704 server.sin_family = AF_INET;
705 server.sin_port = htons(port);
706 if ((host = gethostbyname(hostname)) == NULL)
707 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100708 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200709 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100710 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100711 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100712 }
713 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
714
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100715 /* On Mac and Solaris a zero timeout almost never works. At least wait
716 * one millisecond. Let's do it for all systems, because we don't know why
717 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100718 if (waittime == 0)
719 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720
721 /*
722 * For Unix we need to call connect() again after connect() failed.
723 * On Win32 one time is sufficient.
724 */
725 while (TRUE)
726 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100727 long elapsed_msec = 0;
728 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100729
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100730 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100731 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100732 sd = socket(AF_INET, SOCK_STREAM, 0);
733 if (sd == -1)
734 {
735 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200736 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100737 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100738 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100739 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100740
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100741 if (waittime >= 0)
742 {
743 /* Make connect() non-blocking. */
744 if (
745#ifdef _WIN32
746 ioctlsocket(sd, FIONBIO, &val) < 0
747#else
748 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
749#endif
750 )
751 {
752 SOCK_ERRNO;
753 ch_errorn(channel,
754 "channel_open: Connect failed with errno %d", errno);
755 sock_close(sd);
756 channel_free(channel);
757 return NULL;
758 }
759 }
760
761 /* Try connecting to the server. */
762 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
763 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
764
Bram Moolenaar045a2842016-03-08 22:33:07 +0100765 if (ret == 0)
766 /* The connection could be established. */
767 break;
768
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100769 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100770 if (waittime < 0 || (errno != EWOULDBLOCK
771 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100772#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100773 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100774#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100775 ))
776 {
777 ch_errorn(channel,
778 "channel_open: Connect failed with errno %d", errno);
779 PERROR(_(e_cannot_connect));
780 sock_close(sd);
781 channel_free(channel);
782 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100783 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100784
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100785 /* Limit the waittime to 50 msec. If it doesn't work within this
786 * time we close the socket and try creating it again. */
787 waitnow = waittime > 50 ? 50 : waittime;
788
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100790 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791#ifndef WIN32
792 if (errno != ECONNREFUSED)
793#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100794 {
795 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100796 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100797 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100798#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100799 int so_error = 0;
800 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100801 struct timeval start_tv;
802 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100803#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100804 FD_ZERO(&rfds);
805 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100806 FD_ZERO(&wfds);
807 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100808
Bram Moolenaar562ca712016-03-09 21:50:05 +0100809 tv.tv_sec = waitnow / 1000;
810 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100811#ifndef WIN32
812 gettimeofday(&start_tv, NULL);
813#endif
814 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100815 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100816 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100817
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100818 if (ret < 0)
819 {
820 SOCK_ERRNO;
821 ch_errorn(channel,
822 "channel_open: Connect failed with errno %d", errno);
823 PERROR(_(e_cannot_connect));
824 sock_close(sd);
825 channel_free(channel);
826 return NULL;
827 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100828
Bram Moolenaare081e212016-02-28 22:33:46 +0100829#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100830 /* On Win32: select() is expected to work and wait for up to
831 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100832 if (FD_ISSET(sd, &wfds))
833 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100834 elapsed_msec = waitnow;
835 if (waittime > 1 && elapsed_msec < waittime)
836 {
837 waittime -= elapsed_msec;
838 continue;
839 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100840#else
841 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100842 * After putting the socket in non-blocking mode, connect() will
843 * return EINPROGRESS, select() will not wait (as if writing is
844 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100845 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100846 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100847 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100848 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100849 {
850 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100851 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100852 if (ret < 0 || (so_error != 0
853 && so_error != EWOULDBLOCK
854 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100855# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100856 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100857# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100858 ))
859 {
860 ch_errorn(channel,
861 "channel_open: Connect failed with errno %d",
862 so_error);
863 PERROR(_(e_cannot_connect));
864 sock_close(sd);
865 channel_free(channel);
866 return NULL;
867 }
868 }
869
Bram Moolenaar045a2842016-03-08 22:33:07 +0100870 if (FD_ISSET(sd, &wfds) && so_error == 0)
871 /* Did not detect an error, connection is established. */
872 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100873
Bram Moolenaar045a2842016-03-08 22:33:07 +0100874 gettimeofday(&end_tv, NULL);
875 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
876 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100877#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100878 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100879
880#ifndef WIN32
881 if (waittime > 1 && elapsed_msec < waittime)
882 {
883 /* The port isn't ready but we also didn't get an error.
884 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100885 * yet. Select() may return early, wait until the remaining
886 * "waitnow" and try again. */
887 waitnow -= elapsed_msec;
888 waittime -= elapsed_msec;
889 if (waitnow > 0)
890 {
891 mch_delay((long)waitnow, TRUE);
892 ui_breakcheck();
893 waittime -= waitnow;
894 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100895 if (!got_int)
896 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100897 if (waittime <= 0)
898 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100899 waittime = 1;
900 continue;
901 }
902 /* we were interrupted, behave as if timed out */
903 }
904#endif
905
906 /* We timed out. */
907 ch_error(channel, "Connection timed out");
908 sock_close(sd);
909 channel_free(channel);
910 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100911 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100912
Bram Moolenaar045a2842016-03-08 22:33:07 +0100913 ch_log(channel, "Connection made");
914
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100915 if (waittime >= 0)
916 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100917#ifdef _WIN32
918 val = 0;
919 ioctlsocket(sd, FIONBIO, &val);
920#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100921 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100922#endif
923 }
924
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100925 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100926 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100927 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
928 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200929 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100930
931#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100932 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100933#endif
934
Bram Moolenaar77073442016-02-13 23:23:53 +0100935 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100936}
937
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100938/*
939 * Implements ch_open().
940 */
941 channel_T *
942channel_open_func(typval_T *argvars)
943{
944 char_u *address;
945 char_u *p;
946 char *rest;
947 int port;
948 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200949 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100950
951 address = get_tv_string(&argvars[0]);
952 if (argvars[1].v_type != VAR_UNKNOWN
953 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
954 {
955 EMSG(_(e_invarg));
956 return NULL;
957 }
958
959 /* parse address */
960 p = vim_strchr(address, ':');
961 if (p == NULL)
962 {
963 EMSG2(_(e_invarg2), address);
964 return NULL;
965 }
966 *p++ = NUL;
967 port = strtol((char *)p, &rest, 10);
968 if (*address == NUL || port <= 0 || *rest != NUL)
969 {
970 p[-1] = ':';
971 EMSG2(_(e_invarg2), address);
972 return NULL;
973 }
974
975 /* parse options */
976 clear_job_options(&opt);
977 opt.jo_mode = MODE_JSON;
978 opt.jo_timeout = 2000;
979 if (get_job_options(&argvars[1], &opt,
980 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200981 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100982 if (opt.jo_timeout < 0)
983 {
984 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200985 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100986 }
987
988 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
989 if (channel != NULL)
990 {
991 opt.jo_set = JO_ALL;
992 channel_set_options(channel, &opt);
993 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200994theend:
995 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100996 return channel;
997}
998
Bram Moolenaarde279892016-03-11 22:19:44 +0100999 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001000ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001001{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001002 sock_T *fd = &channel->ch_part[part].ch_fd;
1003
Bram Moolenaarde279892016-03-11 22:19:44 +01001004 if (*fd != INVALID_FD)
1005 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001006 if (part == PART_SOCK)
1007 sock_close(*fd);
1008 else
1009 fd_close(*fd);
Bram Moolenaarde279892016-03-11 22:19:44 +01001010 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001011
1012 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001013 }
1014}
1015
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001016 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001017channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001018{
Bram Moolenaarde279892016-03-11 22:19:44 +01001019 if (in != INVALID_FD)
1020 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001021 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001022 channel->CH_IN_FD = in;
1023 }
1024 if (out != INVALID_FD)
1025 {
1026# if defined(FEAT_GUI)
1027 channel_gui_unregister_one(channel, PART_OUT);
1028# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001029 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001030 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001031 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001032# if defined(FEAT_GUI)
1033 channel_gui_register_one(channel, PART_OUT);
1034# endif
1035 }
1036 if (err != INVALID_FD)
1037 {
1038# if defined(FEAT_GUI)
1039 channel_gui_unregister_one(channel, PART_ERR);
1040# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001041 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001042 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001043 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001044# if defined(FEAT_GUI)
1045 channel_gui_register_one(channel, PART_ERR);
1046# endif
1047 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001048}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001049
Bram Moolenaard6051b52016-02-28 15:49:03 +01001050/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001051 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001052 * This does not keep a refcount, when the job is freed ch_job is cleared.
1053 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001054 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001055channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001056{
Bram Moolenaar77073442016-02-13 23:23:53 +01001057 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001058
1059 channel_set_options(channel, options);
1060
1061 if (job->jv_in_buf != NULL)
1062 {
1063 chanpart_T *in_part = &channel->ch_part[PART_IN];
1064
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001065 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001066 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001067 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001068 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001069 {
1070 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1071 {
1072 /* Special mode: send last-but-one line when appending a line
1073 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001074 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001075 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001076 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001077 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001078 }
1079 else
1080 in_part->ch_buf_top = options->jo_in_top;
1081 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001082 else
1083 in_part->ch_buf_top = 1;
1084 if (options->jo_set & JO_IN_BOT)
1085 in_part->ch_buf_bot = options->jo_in_bot;
1086 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001087 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001088 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001089}
1090
1091/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001092 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001093 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001094 */
1095 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001096find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001097{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001098 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 buf_T *save_curbuf = curbuf;
1100
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001101 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001102 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001103 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001104 if (buf == NULL)
1105 buf = buflist_findname_exp(name);
1106 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001107 if (buf == NULL)
1108 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001109 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001110 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001111 if (buf == NULL)
1112 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001113 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001114 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001115#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001116 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1117 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001118#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001119 if (curbuf->b_ml.ml_mfp == NULL)
1120 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001121 if (msg)
1122 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001123 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001124 changed_bytes(1, 0);
1125 curbuf = save_curbuf;
1126 }
1127
1128 return buf;
1129}
1130
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001131 static void
1132set_callback(
1133 char_u **cbp,
1134 partial_T **pp,
1135 char_u *callback,
1136 partial_T *partial)
1137{
1138 free_callback(*cbp, *pp);
1139 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001140 {
1141 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001142 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001143 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001144 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001145 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001146 func_ref(*cbp);
1147 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001148 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001149 else
1150 *cbp = NULL;
1151 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001152 if (partial != NULL)
1153 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001154}
1155
Bram Moolenaar187db502016-02-27 14:44:26 +01001156/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001157 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001158 */
1159 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001160channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001161{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001162 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001163
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001164 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001165 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001166 channel->ch_part[part].ch_mode = opt->jo_mode;
1167 if (opt->jo_set & JO_IN_MODE)
1168 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1169 if (opt->jo_set & JO_OUT_MODE)
1170 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1171 if (opt->jo_set & JO_ERR_MODE)
1172 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001173
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001174 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001175 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001176 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1177 if (opt->jo_set & JO_OUT_TIMEOUT)
1178 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1179 if (opt->jo_set & JO_ERR_TIMEOUT)
1180 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001181 if (opt->jo_set & JO_BLOCK_WRITE)
1182 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001183
1184 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001185 set_callback(&channel->ch_callback, &channel->ch_partial,
1186 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001187 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001188 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1189 &channel->ch_part[PART_OUT].ch_partial,
1190 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001191 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001192 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1193 &channel->ch_part[PART_ERR].ch_partial,
1194 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001195 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001196 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1197 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar187db502016-02-27 14:44:26 +01001198
1199 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1200 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001201 buf_T *buf;
1202
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001203 /* writing output to a buffer. Default mode is NL. */
1204 if (!(opt->jo_set & JO_OUT_MODE))
1205 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001206 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001207 {
1208 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1209 if (buf == NULL)
1210 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1211 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001212 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001213 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001214 int msg = TRUE;
1215
1216 if (opt->jo_set2 & JO2_OUT_MSG)
1217 msg = opt->jo_message[PART_OUT];
1218 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001219 }
1220 if (buf != NULL)
1221 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001222 if (opt->jo_set & JO_OUT_MODIFIABLE)
1223 channel->ch_part[PART_OUT].ch_nomodifiable =
1224 !opt->jo_modifiable[PART_OUT];
1225
1226 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1227 {
1228 EMSG(_(e_modifiable));
1229 }
1230 else
1231 {
1232 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001233 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001234 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001235 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001236 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001237 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001238
1239 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1240 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1241 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1242 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001243 buf_T *buf;
1244
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001245 /* writing err to a buffer. Default mode is NL. */
1246 if (!(opt->jo_set & JO_ERR_MODE))
1247 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1248 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001249 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001250 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 {
1252 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1253 if (buf == NULL)
1254 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1255 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001256 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001257 {
1258 int msg = TRUE;
1259
1260 if (opt->jo_set2 & JO2_ERR_MSG)
1261 msg = opt->jo_message[PART_ERR];
1262 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1263 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001264 if (buf != NULL)
1265 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001266 if (opt->jo_set & JO_ERR_MODIFIABLE)
1267 channel->ch_part[PART_ERR].ch_nomodifiable =
1268 !opt->jo_modifiable[PART_ERR];
1269 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1270 {
1271 EMSG(_(e_modifiable));
1272 }
1273 else
1274 {
1275 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001276 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001277 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001278 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001279 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001280 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001281
1282 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1283 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1284 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001285}
1286
1287/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001288 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001289 */
1290 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001291channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001292 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001293 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001294 char_u *callback,
1295 partial_T *partial,
1296 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001297{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001298 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001299 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1300
1301 if (item != NULL)
1302 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001303 item->cq_partial = partial;
1304 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001305 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001306 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001307 item->cq_callback = callback;
1308 }
1309 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001310 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001311 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001312 func_ref(item->cq_callback);
1313 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001314 item->cq_seq_nr = id;
1315 item->cq_prev = head->cq_prev;
1316 head->cq_prev = item;
1317 item->cq_next = NULL;
1318 if (item->cq_prev == NULL)
1319 head->cq_next = item;
1320 else
1321 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001322 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001323}
1324
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001325 static void
1326write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1327{
1328 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001329 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001330 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001331 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001332
Bram Moolenaar655da312016-05-28 22:22:34 +02001333 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001334 if ((p = alloc(len + 2)) == NULL)
1335 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001336 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001337
1338 for (i = 0; i < len; ++i)
1339 if (p[i] == NL)
1340 p[i] = NUL;
1341
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001342 p[len] = NL;
1343 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001344 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001345 vim_free(p);
1346}
1347
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001348/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001349 * Return TRUE if "channel" can be written to.
1350 * Returns FALSE if the input is closed or the write would block.
1351 */
1352 static int
1353can_write_buf_line(channel_T *channel)
1354{
1355 chanpart_T *in_part = &channel->ch_part[PART_IN];
1356
1357 if (in_part->ch_fd == INVALID_FD)
1358 return FALSE; /* pipe was closed */
1359
1360 /* for testing: block every other attempt to write */
1361 if (in_part->ch_block_write == 1)
1362 in_part->ch_block_write = -1;
1363 else if (in_part->ch_block_write == -1)
1364 in_part->ch_block_write = 1;
1365
1366 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1367#ifndef WIN32
1368 {
1369# if defined(HAVE_SELECT)
1370 struct timeval tval;
1371 fd_set wfds;
1372 int ret;
1373
1374 FD_ZERO(&wfds);
1375 FD_SET((int)in_part->ch_fd, &wfds);
1376 tval.tv_sec = 0;
1377 tval.tv_usec = 0;
1378 for (;;)
1379 {
1380 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1381# ifdef EINTR
1382 SOCK_ERRNO;
1383 if (ret == -1 && errno == EINTR)
1384 continue;
1385# endif
1386 if (ret <= 0 || in_part->ch_block_write == 1)
1387 {
1388 if (ret > 0)
1389 ch_log(channel, "FAKED Input not ready for writing");
1390 else
1391 ch_log(channel, "Input not ready for writing");
1392 return FALSE;
1393 }
1394 break;
1395 }
1396# else
1397 struct pollfd fds;
1398
1399 fds.fd = in_part->ch_fd;
1400 fds.events = POLLOUT;
1401 if (poll(&fds, 1, 0) <= 0)
1402 {
1403 ch_log(channel, "Input not ready for writing");
1404 return FALSE;
1405 }
1406 if (in_part->ch_block_write == 1)
1407 {
1408 ch_log(channel, "FAKED Input not ready for writing");
1409 return FALSE;
1410 }
1411# endif
1412 }
1413#endif
1414 return TRUE;
1415}
1416
1417/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001418 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001419 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001420 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001421channel_write_in(channel_T *channel)
1422{
1423 chanpart_T *in_part = &channel->ch_part[PART_IN];
1424 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001425 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001426 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001427
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001428 if (buf == NULL || in_part->ch_buf_append)
1429 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001430 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001431 {
1432 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001433 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001434 return;
1435 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001436
1437 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1438 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1439 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001440 if (!can_write_buf_line(channel))
1441 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001442 write_buf_line(buf, lnum, channel);
1443 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001444 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001445
1446 if (written == 1)
1447 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1448 else if (written > 1)
1449 ch_logn(channel, "written %d lines to channel", written);
1450
Bram Moolenaar014069a2016-03-03 22:51:40 +01001451 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001452 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001453 {
1454 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001455 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001456 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001457
1458 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001459 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001460 }
1461 else
1462 ch_logn(channel, "Still %d more lines to write",
1463 buf->b_ml.ml_line_count - lnum + 1);
1464}
1465
1466/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001467 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001468 */
1469 void
1470channel_buffer_free(buf_T *buf)
1471{
1472 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001473 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001474
1475 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001476 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001477 {
1478 chanpart_T *ch_part = &channel->ch_part[part];
1479
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001480 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001481 {
1482 ch_logs(channel, "%s buffer has been wiped out",
1483 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001484 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001485 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001486 }
1487}
1488
1489/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001490 * Write any lines waiting to be written to a channel.
1491 */
1492 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001493channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001494{
1495 channel_T *channel;
1496
1497 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1498 {
1499 chanpart_T *in_part = &channel->ch_part[PART_IN];
1500
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001501 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001502 {
1503 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001504 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001505 else
1506 channel_write_in(channel);
1507 }
1508 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001509}
1510
1511/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001512 * Write appended lines above the last one in "buf" to the channel.
1513 */
1514 void
1515channel_write_new_lines(buf_T *buf)
1516{
1517 channel_T *channel;
1518 int found_one = FALSE;
1519
1520 /* There could be more than one channel for the buffer, loop over all of
1521 * them. */
1522 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1523 {
1524 chanpart_T *in_part = &channel->ch_part[PART_IN];
1525 linenr_T lnum;
1526 int written = 0;
1527
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001528 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001529 {
1530 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001531 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001532 found_one = TRUE;
1533 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1534 ++lnum)
1535 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001536 if (!can_write_buf_line(channel))
1537 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001538 write_buf_line(buf, lnum, channel);
1539 ++written;
1540 }
1541
1542 if (written == 1)
1543 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1544 else if (written > 1)
1545 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001546 if (lnum < buf->b_ml.ml_line_count)
1547 ch_logn(channel, "Still %d more lines to write",
1548 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001549
1550 in_part->ch_buf_bot = lnum;
1551 }
1552 }
1553 if (!found_one)
1554 buf->b_write_to_channel = FALSE;
1555}
1556
1557/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001558 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001559 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001560 */
1561 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001562invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1563 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001564{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001565 typval_T rettv;
1566 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001567
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001568 if (safe_to_invoke_callback == 0)
1569 EMSG("INTERNAL: Invoking callback when it is not safe");
1570
Bram Moolenaar77073442016-02-13 23:23:53 +01001571 argv[0].v_type = VAR_CHANNEL;
1572 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001573
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001574 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1575 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001576 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001577 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001578}
1579
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001580/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001581 * Return the first node from "channel"/"part" without removing it.
1582 * Returns NULL if there is nothing.
1583 */
1584 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001585channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001586{
1587 readq_T *head = &channel->ch_part[part].ch_head;
1588
1589 return head->rq_next;
1590}
1591
1592/*
1593 * Return a pointer to the first NL in "node".
1594 * Skips over NUL characters.
1595 * Returns NULL if there is no NL.
1596 */
1597 char_u *
1598channel_first_nl(readq_T *node)
1599{
1600 char_u *buffer = node->rq_buffer;
1601 long_u i;
1602
1603 for (i = 0; i < node->rq_buflen; ++i)
1604 if (buffer[i] == NL)
1605 return buffer + i;
1606 return NULL;
1607}
1608
1609/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001610 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001611 * The caller must free it.
1612 * Returns NULL if there is nothing.
1613 */
1614 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001615channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001616{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001617 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001618 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001619 char_u *p;
1620
Bram Moolenaar77073442016-02-13 23:23:53 +01001621 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001622 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001623 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001624 p = node->rq_buffer;
1625 head->rq_next = node->rq_next;
1626 if (node->rq_next == NULL)
1627 head->rq_prev = NULL;
1628 else
1629 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001630 vim_free(node);
1631 return p;
1632}
1633
1634/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001635 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001636 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001637 */
1638 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001639channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001640{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001641 readq_T *head = &channel->ch_part[part].ch_head;
1642 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001643 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001644 char_u *res;
1645 char_u *p;
1646
1647 /* If there is only one buffer just get that one. */
1648 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1649 return channel_get(channel, part);
1650
1651 /* Concatenate everything into one buffer. */
1652 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001653 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001654 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001655 if (res == NULL)
1656 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001657 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001658 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001659 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001660 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001661 p += node->rq_buflen;
1662 }
1663 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001664
1665 /* Free all buffers */
1666 do
1667 {
1668 p = channel_get(channel, part);
1669 vim_free(p);
1670 } while (p != NULL);
1671
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001672 /* turn all NUL into NL */
1673 while (len > 0)
1674 {
1675 --len;
1676 if (res[len] == NUL)
1677 res[len] = NL;
1678 }
1679
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001680 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681}
1682
1683/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001684 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001685 * Caller must check these bytes are available.
1686 */
1687 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001688channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001689{
1690 readq_T *head = &channel->ch_part[part].ch_head;
1691 readq_T *node = head->rq_next;
1692 char_u *buf = node->rq_buffer;
1693
1694 mch_memmove(buf, buf + len, node->rq_buflen - len);
1695 node->rq_buflen -= len;
1696}
1697
1698/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001699 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001701 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 */
1703 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001704channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001705{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001706 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001707 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001708 readq_T *last_node;
1709 readq_T *n;
1710 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001711 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001712 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713
Bram Moolenaar77073442016-02-13 23:23:53 +01001714 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001715 return FAIL;
1716
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001717 last_node = node->rq_next;
1718 len = node->rq_buflen + last_node->rq_buflen + 1;
1719 if (want_nl)
1720 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001721 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001722 {
1723 last_node = last_node->rq_next;
1724 len += last_node->rq_buflen;
1725 }
1726
1727 p = newbuf = alloc(len);
1728 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001730 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001731 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001732 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001733 node->rq_buffer = newbuf;
1734 for (n = node; n != last_node; )
1735 {
1736 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001737 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001738 p += n->rq_buflen;
1739 vim_free(n->rq_buffer);
1740 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001741 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001742
1743 /* dispose of the collapsed nodes and their buffers */
1744 for (n = node->rq_next; n != last_node; )
1745 {
1746 n = n->rq_next;
1747 vim_free(n->rq_prev);
1748 }
1749 node->rq_next = last_node->rq_next;
1750 if (last_node->rq_next == NULL)
1751 head->rq_prev = node;
1752 else
1753 last_node->rq_next->rq_prev = node;
1754 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001755 return OK;
1756}
1757
1758/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001759 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001760 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001761 * Returns OK or FAIL.
1762 */
1763 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001764channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001765 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001766{
1767 readq_T *node;
1768 readq_T *head = &channel->ch_part[part].ch_head;
1769 char_u *p;
1770 int i;
1771
1772 node = (readq_T *)alloc(sizeof(readq_T));
1773 if (node == NULL)
1774 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001775 /* A NUL is added at the end, because netbeans code expects that.
1776 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001777 node->rq_buffer = alloc(len + 1);
1778 if (node->rq_buffer == NULL)
1779 {
1780 vim_free(node);
1781 return FAIL; /* out of memory */
1782 }
1783
1784 if (channel->ch_part[part].ch_mode == MODE_NL)
1785 {
1786 /* Drop any CR before a NL. */
1787 p = node->rq_buffer;
1788 for (i = 0; i < len; ++i)
1789 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1790 *p++ = buf[i];
1791 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001792 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793 }
1794 else
1795 {
1796 mch_memmove(node->rq_buffer, buf, len);
1797 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001798 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001799 }
1800
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001801 if (prepend)
1802 {
1803 /* preend node to the head of the queue */
1804 node->rq_next = head->rq_next;
1805 node->rq_prev = NULL;
1806 if (head->rq_next == NULL)
1807 head->rq_prev = node;
1808 else
1809 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001810 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001811 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001812 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001813 {
1814 /* append node to the tail of the queue */
1815 node->rq_next = NULL;
1816 node->rq_prev = head->rq_prev;
1817 if (head->rq_prev == NULL)
1818 head->rq_next = node;
1819 else
1820 head->rq_prev->rq_next = node;
1821 head->rq_prev = node;
1822 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001823
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001824 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001825 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001826 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001827 fprintf(log_fd, "'");
1828 if (fwrite(buf, len, 1, log_fd) != 1)
1829 return FAIL;
1830 fprintf(log_fd, "'\n");
1831 }
1832 return OK;
1833}
1834
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001835 static int
1836channel_fill(js_read_T *reader)
1837{
1838 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001839 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001840 char_u *next = channel_get(channel, part);
1841 int unused;
1842 int len;
1843 char_u *p;
1844
1845 if (next == NULL)
1846 return FALSE;
1847
1848 unused = reader->js_end - reader->js_buf - reader->js_used;
1849 if (unused > 0)
1850 {
1851 /* Prepend unused text. */
1852 len = (int)STRLEN(next);
1853 p = alloc(unused + len + 1);
1854 if (p == NULL)
1855 {
1856 vim_free(next);
1857 return FALSE;
1858 }
1859 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1860 mch_memmove(p + unused, next, len + 1);
1861 vim_free(next);
1862 next = p;
1863 }
1864
1865 vim_free(reader->js_buf);
1866 reader->js_buf = next;
1867 reader->js_used = 0;
1868 return TRUE;
1869}
1870
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001871/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001872 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001873 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001874 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001875 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001876 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001877channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001878{
1879 js_read_T reader;
1880 typval_T listtv;
1881 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001882 chanpart_T *chanpart = &channel->ch_part[part];
1883 jsonq_T *head = &chanpart->ch_json_head;
1884 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001885 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001886
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001887 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001888 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001889
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001890 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001891 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001892 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001893 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001894 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001895
1896 /* When a message is incomplete we wait for a short while for more to
1897 * arrive. After the delay drop the input, otherwise a truncated string
1898 * or list will make us hang. */
1899 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001900 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001901 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001902 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001903 /* Only accept the response when it is a list with at least two
1904 * items. */
1905 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001906 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001907 if (listtv.v_type != VAR_LIST)
1908 ch_error(channel, "Did not receive a list, discarding");
1909 else
1910 ch_errorn(channel, "Expected list with two items, got %d",
1911 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001912 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001913 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001914 else
1915 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001916 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1917 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001918 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001919 else
1920 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001921 item->jq_value = alloc_tv();
1922 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001923 {
1924 vim_free(item);
1925 clear_tv(&listtv);
1926 }
1927 else
1928 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001929 *item->jq_value = listtv;
1930 item->jq_prev = head->jq_prev;
1931 head->jq_prev = item;
1932 item->jq_next = NULL;
1933 if (item->jq_prev == NULL)
1934 head->jq_next = item;
1935 else
1936 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001937 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001938 }
1939 }
1940 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001941
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001942 if (status == OK)
1943 chanpart->ch_waiting = FALSE;
1944 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001945 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001946 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001947 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001948 /* First time encountering incomplete message, set a deadline of
1949 * 100 msec. */
1950 ch_log(channel, "Incomplete message - wait for more");
1951 reader.js_used = 0;
1952 chanpart->ch_waiting = TRUE;
1953#ifdef WIN32
1954 chanpart->ch_deadline = GetTickCount() + 100L;
1955#else
1956 gettimeofday(&chanpart->ch_deadline, NULL);
1957 chanpart->ch_deadline.tv_usec += 100 * 1000;
1958 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1959 {
1960 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1961 ++chanpart->ch_deadline.tv_sec;
1962 }
1963#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001964 }
1965 else
1966 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001967 int timeout;
1968#ifdef WIN32
1969 timeout = GetTickCount() > chanpart->ch_deadline;
1970#else
1971 {
1972 struct timeval now_tv;
1973
1974 gettimeofday(&now_tv, NULL);
1975 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1976 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1977 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1978 }
1979#endif
1980 if (timeout)
1981 {
1982 status = FAIL;
1983 chanpart->ch_waiting = FALSE;
1984 }
1985 else
1986 {
1987 reader.js_used = 0;
1988 ch_log(channel, "still waiting on incomplete message");
1989 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001990 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001991 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001992
1993 if (status == FAIL)
1994 {
1995 ch_error(channel, "Decoding failed - discarding input");
1996 ret = FALSE;
1997 chanpart->ch_waiting = FALSE;
1998 }
1999 else if (reader.js_buf[reader.js_used] != NUL)
2000 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002001 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002002 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002003 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2004 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002005 ret = status == MAYBE ? FALSE: TRUE;
2006 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002007 else
2008 ret = FALSE;
2009
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002010 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002011 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002012}
2013
2014/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002015 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002016 */
2017 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002018remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002019{
Bram Moolenaar77073442016-02-13 23:23:53 +01002020 if (node->cq_prev == NULL)
2021 head->cq_next = node->cq_next;
2022 else
2023 node->cq_prev->cq_next = node->cq_next;
2024 if (node->cq_next == NULL)
2025 head->cq_prev = node->cq_prev;
2026 else
2027 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002028}
2029
2030/*
2031 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002033 */
2034 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002035remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002036{
Bram Moolenaar77073442016-02-13 23:23:53 +01002037 if (node->jq_prev == NULL)
2038 head->jq_next = node->jq_next;
2039 else
2040 node->jq_prev->jq_next = node->jq_next;
2041 if (node->jq_next == NULL)
2042 head->jq_prev = node->jq_prev;
2043 else
2044 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002045 vim_free(node);
2046}
2047
2048/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002049 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002050 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002051 * When "id" is zero or negative jut get the first message. But not the one
2052 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002053 * Return OK when found and return the value in "rettv".
2054 * Return FAIL otherwise.
2055 */
2056 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002057channel_get_json(channel_T *channel, ch_part_T part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002058{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002059 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002060 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002061
Bram Moolenaar77073442016-02-13 23:23:53 +01002062 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002063 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002064 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002065 typval_T *tv = &l->lv_first->li_tv;
2066
2067 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002068 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002069 || tv->vval.v_number == 0
2070 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002071 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002072 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002073 if (tv->v_type == VAR_NUMBER)
2074 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002075 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002076 return OK;
2077 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002078 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002079 }
2080 return FAIL;
2081}
2082
Bram Moolenaarece61b02016-02-20 21:39:05 +01002083#define CH_JSON_MAX_ARGS 4
2084
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002085/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002086 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002087 * "argv[0]" is the command string.
2088 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002089 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002090 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002091channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002092{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002093 char_u *cmd = argv[0].vval.v_string;
2094 char_u *arg;
2095 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002096
Bram Moolenaarece61b02016-02-20 21:39:05 +01002097 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002098 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002099 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002100 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002101 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002102 return;
2103 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002104 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002105 if (arg == NULL)
2106 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002107
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002108 if (STRCMP(cmd, "ex") == 0)
2109 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002110 int save_called_emsg = called_emsg;
2111
2112 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002113 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002114 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002115 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002116 --emsg_silent;
2117 if (called_emsg)
2118 ch_logs(channel, "Ex command error: '%s'",
2119 (char *)get_vim_var_str(VV_ERRMSG));
2120 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002121 }
2122 else if (STRCMP(cmd, "normal") == 0)
2123 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002124 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002125
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002126 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002127 ea.arg = arg;
2128 ea.addr_count = 0;
2129 ea.forceit = TRUE; /* no mapping */
2130 ex_normal(&ea);
2131 }
2132 else if (STRCMP(cmd, "redraw") == 0)
2133 {
2134 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002135
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002136 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002137 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002138 ex_redraw(&ea);
2139 showruler(FALSE);
2140 setcursor();
2141 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002142#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002143 if (gui.in_use)
2144 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002145 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002146 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002147 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002148#endif
2149 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002150 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002151 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002152 int is_call = cmd[0] == 'c';
2153 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002154
Bram Moolenaarece61b02016-02-20 21:39:05 +01002155 if (argv[id_idx].v_type != VAR_UNKNOWN
2156 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002157 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002158 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002159 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002160 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002161 }
2162 else if (is_call && argv[2].v_type != VAR_LIST)
2163 {
2164 ch_error(channel, "third argument for call must be a list");
2165 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002166 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002167 }
2168 else
2169 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002170 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002171 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002172 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002173 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002174
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002175 /* Don't pollute the display with errors. */
2176 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002177 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002178 {
2179 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002180 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002181 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002182 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002183 {
2184 ch_logs(channel, "Calling '%s'", (char *)arg);
2185 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2186 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002187 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002188
2189 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002190 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002191 int id = argv[id_idx].vval.v_number;
2192
Bram Moolenaar55fab432016-02-07 16:53:13 +01002193 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002194 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002195 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002196 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002197 /* If evaluation failed or the result can't be encoded
2198 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002199 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002200 err_tv.v_type = VAR_STRING;
2201 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002202 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002204 if (json != NULL)
2205 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002206 channel_send(channel,
2207 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002208 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002209 vim_free(json);
2210 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002212 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002213 if (tv == &res_tv)
2214 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002215 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002216 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002217 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002218 }
2219 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002220 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002221 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002222 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002223 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002224}
2225
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002226/*
2227 * Invoke the callback at "cbhead".
2228 * Does not redraw but sets channel_need_redraw.
2229 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002230 static void
2231invoke_one_time_callback(
2232 channel_T *channel,
2233 cbq_T *cbhead,
2234 cbq_T *item,
2235 typval_T *argv)
2236{
2237 ch_logs(channel, "Invoking one-time callback %s",
2238 (char *)item->cq_callback);
2239 /* Remove the item from the list first, if the callback
2240 * invokes ch_close() the list will be cleared. */
2241 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002242 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002243 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002244 vim_free(item);
2245}
2246
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002247 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002248append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002249{
2250 buf_T *save_curbuf = curbuf;
2251 linenr_T lnum = buffer->b_ml.ml_line_count;
2252 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002253 chanpart_T *ch_part = &channel->ch_part[part];
2254 int save_p_ma = buffer->b_p_ma;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002255 int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002256
2257 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2258 {
2259 if (!ch_part->ch_nomod_error)
2260 {
2261 ch_error(channel, "Buffer is not modifiable, cannot append");
2262 ch_part->ch_nomod_error = TRUE;
2263 }
2264 return;
2265 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002266
2267 /* If the buffer is also used as input insert above the last
2268 * line. Don't write these lines. */
2269 if (save_write_to)
2270 {
2271 --lnum;
2272 buffer->b_write_to_channel = FALSE;
2273 }
2274
2275 /* Append to the buffer */
2276 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2277
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002278 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002279 curbuf = buffer;
2280 u_sync(TRUE);
2281 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002282 ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002283
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002284 if (empty)
2285 {
2286 /* The buffer is empty, replace the first (dummy) line. */
2287 ml_replace(lnum, msg, TRUE);
2288 lnum = 0;
2289 }
2290 else
2291 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002292 appended_lines_mark(lnum, 1L);
2293 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002294 if (ch_part->ch_nomodifiable)
2295 buffer->b_p_ma = FALSE;
2296 else
2297 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002298
2299 if (buffer->b_nwindows > 0)
2300 {
2301 win_T *wp;
2302 win_T *save_curwin;
2303
2304 FOR_ALL_WINDOWS(wp)
2305 {
2306 if (wp->w_buffer == buffer
2307 && (save_write_to
2308 ? wp->w_cursor.lnum == lnum + 1
2309 : (wp->w_cursor.lnum == lnum
2310 && wp->w_cursor.col == 0)))
2311 {
2312 ++wp->w_cursor.lnum;
2313 save_curwin = curwin;
2314 curwin = wp;
2315 curbuf = curwin->w_buffer;
2316 scroll_cursor_bot(0, FALSE);
2317 curwin = save_curwin;
2318 curbuf = curwin->w_buffer;
2319 }
2320 }
2321 redraw_buf_later(buffer, VALID);
2322 channel_need_redraw = TRUE;
2323 }
2324
2325 if (save_write_to)
2326 {
2327 channel_T *ch;
2328
2329 /* Find channels reading from this buffer and adjust their
2330 * next-to-read line number. */
2331 buffer->b_write_to_channel = TRUE;
2332 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2333 {
2334 chanpart_T *in_part = &ch->ch_part[PART_IN];
2335
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002336 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002337 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2338 }
2339 }
2340}
2341
Bram Moolenaar437905c2016-04-26 19:01:05 +02002342 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002343drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002344{
2345 char_u *msg;
2346
2347 while ((msg = channel_get(channel, part)) != NULL)
2348 {
2349 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2350 vim_free(msg);
2351 }
2352}
2353
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002354/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002355 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002356 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002357 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002358 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002359 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002360may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002361{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002362 char_u *msg = NULL;
2363 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002364 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002365 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002366 chanpart_T *ch_part = &channel->ch_part[part];
2367 ch_mode_T ch_mode = ch_part->ch_mode;
2368 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002369 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002370 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002371 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002372 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002373 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002374
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002375 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002376 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002377 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002378
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002379 /* Use a message-specific callback, part callback or channel callback */
2380 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2381 if (cbitem->cq_seq_nr == 0)
2382 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002383 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002384 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002385 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002386 partial = cbitem->cq_partial;
2387 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002388 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002389 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002390 callback = ch_part->ch_callback;
2391 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002392 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002393 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002394 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002395 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002396 partial = channel->ch_partial;
2397 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002398
Bram Moolenaarec68a992016-10-03 21:37:41 +02002399 buffer = ch_part->ch_bufref.br_buf;
2400 if (buffer != NULL && !bufref_valid(&ch_part->ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002401 {
2402 /* buffer was wiped out */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002403 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002404 buffer = NULL;
2405 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002406
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002407 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002408 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002409 listitem_T *item;
2410 int argc = 0;
2411
Bram Moolenaard7ece102016-02-02 23:23:02 +01002412 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002413 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002414 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002415 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002416 channel_parse_json(channel, part);
2417 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002418 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002419 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002420
Bram Moolenaarece61b02016-02-20 21:39:05 +01002421 for (item = listtv->vval.v_list->lv_first;
2422 item != NULL && argc < CH_JSON_MAX_ARGS;
2423 item = item->li_next)
2424 argv[argc++] = item->li_tv;
2425 while (argc < CH_JSON_MAX_ARGS)
2426 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002427
Bram Moolenaarece61b02016-02-20 21:39:05 +01002428 if (argv[0].v_type == VAR_STRING)
2429 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002430 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002431 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002432 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002433 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002434 }
2435
Bram Moolenaarece61b02016-02-20 21:39:05 +01002436 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002437 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002438 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002439 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002440 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002441 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002442 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002443 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002444 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002445 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002446 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002447 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002448 return FALSE;
2449 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002450 else
2451 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002452 /* If there is no callback or buffer drop the message. */
2453 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002454 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002455 /* If there is a close callback it may use ch_read() to get the
2456 * messages. */
2457 if (channel->ch_close_cb == NULL)
2458 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002459 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002460 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002461
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002462 if (ch_mode == MODE_NL)
2463 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002464 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002465 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002466 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002467
2468 /* See if we have a message ending in NL in the first buffer. If
2469 * not try to concatenate the first and the second buffer. */
2470 while (TRUE)
2471 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002472 node = channel_peek(channel, part);
2473 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002474 if (nl != NULL)
2475 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002476 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002477 {
2478 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2479 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002480 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002481 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002482 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002483 buf = node->rq_buffer;
2484
Bram Moolenaarec68a992016-10-03 21:37:41 +02002485 if (nl == NULL)
2486 {
2487 /* Flush remaining message that is missing a NL. */
2488 buf = vim_realloc(buf, node->rq_buflen + 1);
2489 if (buf == NULL)
2490 return FALSE;
2491 node->rq_buffer = buf;
2492 nl = buf + node->rq_buflen++;
2493 *nl = NUL;
2494 }
2495
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002496 /* Convert NUL to NL, the internal representation. */
2497 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2498 if (*p == NUL)
2499 *p = NL;
2500
2501 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002502 {
2503 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002504 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002505 *nl = NUL;
2506 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002507 else
2508 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002509 /* Copy the message into allocated memory (excluding the NL)
2510 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002511 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002512 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002513 }
2514 }
2515 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002516 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002517 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002518 * get everything we have.
2519 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002520 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002521 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002522
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002523 if (msg == NULL)
2524 return FALSE; /* out of memory (and avoids Coverity warning) */
2525
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002526 argv[1].v_type = VAR_STRING;
2527 argv[1].vval.v_string = msg;
2528 }
2529
Bram Moolenaara07fec92016-02-05 21:04:08 +01002530 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002531 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002532 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002533
2534 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002535 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002536 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002537 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002538 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002539 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002540 break;
2541 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002542 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002543 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002544 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002545 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002546 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002547 if (buffer != NULL)
2548 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002549 if (msg == NULL)
2550 /* JSON or JS mode: re-encode the message. */
2551 msg = json_encode(listtv, ch_mode);
2552 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002553 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002554 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002555
Bram Moolenaar187db502016-02-27 14:44:26 +01002556 if (callback != NULL)
2557 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002558 if (cbitem != NULL)
2559 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2560 else
2561 {
2562 /* invoke the channel callback */
2563 ch_logs(channel, "Invoking channel callback %s",
2564 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002565 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002566 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002567 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002568 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002569 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002570 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002571
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002572 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002573 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002574 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002575
2576 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002577}
2578
2579/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002580 * Return TRUE when channel "channel" is open for writing to.
2581 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002582 */
2583 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002584channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002585{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002586 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002587 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002588}
2589
2590/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002591 * Return TRUE when channel "channel" is open for reading or writing.
2592 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002593 */
2594 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002595channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002596{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002597 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002598 || channel->CH_IN_FD != INVALID_FD
2599 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002600 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002601}
2602
2603/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002604 * Return TRUE if "channel" has JSON or other typeahead.
2605 */
2606 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002607channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002608{
2609 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2610
2611 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2612 {
2613 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2614 jsonq_T *item = head->jq_next;
2615
2616 return item != NULL;
2617 }
2618 return channel_peek(channel, part) != NULL;
2619}
2620
2621/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002622 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002623 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002624 */
2625 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002626channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002627{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002628 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002629 int has_readahead = FALSE;
2630
Bram Moolenaar77073442016-02-13 23:23:53 +01002631 if (channel == NULL)
2632 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002633 if (req_part == PART_OUT)
2634 {
2635 if (channel->CH_OUT_FD != INVALID_FD)
2636 return "open";
2637 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002638 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002639 }
2640 else if (req_part == PART_ERR)
2641 {
2642 if (channel->CH_ERR_FD != INVALID_FD)
2643 return "open";
2644 if (channel_has_readahead(channel, PART_ERR))
2645 has_readahead = TRUE;
2646 }
2647 else
2648 {
2649 if (channel_is_open(channel))
2650 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002651 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002652 if (channel_has_readahead(channel, part))
2653 {
2654 has_readahead = TRUE;
2655 break;
2656 }
2657 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002658
2659 if (has_readahead)
2660 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002661 return "closed";
2662}
2663
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002664 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002665channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002666{
2667 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002668 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002669 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002670 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002671 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002672
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002673 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002674 STRCAT(namebuf, "_");
2675 tail = STRLEN(namebuf);
2676
2677 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002678 if (chanpart->ch_fd != INVALID_FD)
2679 status = "open";
2680 else if (channel_has_readahead(channel, part))
2681 status = "buffered";
2682 else
2683 status = "closed";
2684 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002685
2686 STRCPY(namebuf + tail, "mode");
2687 switch (chanpart->ch_mode)
2688 {
2689 case MODE_NL: s = "NL"; break;
2690 case MODE_RAW: s = "RAW"; break;
2691 case MODE_JSON: s = "JSON"; break;
2692 case MODE_JS: s = "JS"; break;
2693 }
2694 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2695
2696 STRCPY(namebuf + tail, "io");
2697 if (part == PART_SOCK)
2698 s = "socket";
2699 else switch (chanpart->ch_io)
2700 {
2701 case JIO_NULL: s = "null"; break;
2702 case JIO_PIPE: s = "pipe"; break;
2703 case JIO_FILE: s = "file"; break;
2704 case JIO_BUFFER: s = "buffer"; break;
2705 case JIO_OUT: s = "out"; break;
2706 }
2707 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2708
2709 STRCPY(namebuf + tail, "timeout");
2710 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2711}
2712
2713 void
2714channel_info(channel_T *channel, dict_T *dict)
2715{
2716 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002717 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002718
2719 if (channel->ch_hostname != NULL)
2720 {
2721 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2722 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2723 channel_part_info(channel, dict, "sock", PART_SOCK);
2724 }
2725 else
2726 {
2727 channel_part_info(channel, dict, "out", PART_OUT);
2728 channel_part_info(channel, dict, "err", PART_ERR);
2729 channel_part_info(channel, dict, "in", PART_IN);
2730 }
2731}
2732
Bram Moolenaar77073442016-02-13 23:23:53 +01002733/*
2734 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002735 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002736 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002737 */
2738 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002739channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002740{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002741 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002742
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002743#ifdef FEAT_GUI
2744 channel_gui_unregister(channel);
2745#endif
2746
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002747 ch_close_part(channel, PART_SOCK);
2748 ch_close_part(channel, PART_IN);
2749 ch_close_part(channel, PART_OUT);
2750 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002751
Bram Moolenaar8b374212016-02-24 20:43:06 +01002752 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002753 {
2754 typval_T argv[1];
2755 typval_T rettv;
2756 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002757 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002758
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002759 /* Invoke callbacks before the close callback, since it's weird to
2760 * first invoke the close callback. Increment the refcount to avoid
2761 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002762 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002763 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002764 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002765 while (may_invoke_callback(channel, part))
2766 ;
2767
2768 /* Invoke the close callback, if still set. */
2769 if (channel->ch_close_cb != NULL)
2770 {
2771 ch_logs(channel, "Invoking close callback %s",
2772 (char *)channel->ch_close_cb);
2773 argv[0].v_type = VAR_CHANNEL;
2774 argv[0].vval.v_channel = channel;
2775 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002776 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002777 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002778 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002779 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002780 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002781
2782 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002783 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002784 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002785 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002786
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002787 --channel->ch_refcount;
2788
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002789 if (channel_need_redraw)
2790 {
2791 channel_need_redraw = FALSE;
2792 redraw_after_callback();
2793 }
2794
Bram Moolenaar437905c2016-04-26 19:01:05 +02002795 /* any remaining messages are useless now */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002796 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002797 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002798 }
2799
2800 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002801}
2802
Bram Moolenaard04a0202016-01-26 23:30:18 +01002803/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002804 * Close the "in" part channel "channel".
2805 */
2806 void
2807channel_close_in(channel_T *channel)
2808{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002809 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002810}
2811
2812/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002813 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002814 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002815 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002816channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002817{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002818 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2819 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002820
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002821 while (channel_peek(channel, part) != NULL)
2822 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002823
2824 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002825 {
2826 cbq_T *node = cb_head->cq_next;
2827
2828 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002829 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002830 vim_free(node);
2831 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002832
2833 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002834 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002835 free_tv(json_head->jq_next->jq_value);
2836 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002837 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002838
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002839 free_callback(channel->ch_part[part].ch_callback,
2840 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002841 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002842 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002843}
2844
2845/*
2846 * Clear all the read buffers on "channel".
2847 */
2848 void
2849channel_clear(channel_T *channel)
2850{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002851 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002852 vim_free(channel->ch_hostname);
2853 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002854 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002855 channel_clear_one(channel, PART_OUT);
2856 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002857 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002858 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002859 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002860 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002861 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002862 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002863 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002864}
2865
Bram Moolenaar77073442016-02-13 23:23:53 +01002866#if defined(EXITFREE) || defined(PROTO)
2867 void
2868channel_free_all(void)
2869{
2870 channel_T *channel;
2871
Bram Moolenaard6051b52016-02-28 15:49:03 +01002872 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002873 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2874 channel_clear(channel);
2875}
2876#endif
2877
2878
Bram Moolenaar715d2852016-04-30 17:06:31 +02002879/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002880#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002881
2882/* Buffer size for reading incoming messages. */
2883#define MAXMSGSIZE 4096
2884
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002885#if defined(HAVE_SELECT)
2886/*
2887 * Add write fds where we are waiting for writing to be possible.
2888 */
2889 static int
2890channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2891{
2892 int maxfd = maxfd_arg;
2893 channel_T *ch;
2894
2895 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2896 {
2897 chanpart_T *in_part = &ch->ch_part[PART_IN];
2898
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002899 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002900 {
2901 FD_SET((int)in_part->ch_fd, wfds);
2902 if ((int)in_part->ch_fd >= maxfd)
2903 maxfd = (int)in_part->ch_fd + 1;
2904 }
2905 }
2906 return maxfd;
2907}
2908#else
2909/*
2910 * Add write fds where we are waiting for writing to be possible.
2911 */
2912 static int
2913channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2914{
2915 int nfd = nfd_in;
2916 channel_T *ch;
2917
2918 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2919 {
2920 chanpart_T *in_part = &ch->ch_part[PART_IN];
2921
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002922 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002923 {
2924 in_part->ch_poll_idx = nfd;
2925 fds[nfd].fd = in_part->ch_fd;
2926 fds[nfd].events = POLLOUT;
2927 ++nfd;
2928 }
2929 else
2930 in_part->ch_poll_idx = -1;
2931 }
2932 return nfd;
2933}
2934#endif
2935
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002936typedef enum {
2937 CW_READY,
2938 CW_NOT_READY,
2939 CW_ERROR
2940} channel_wait_result;
2941
Bram Moolenaard04a0202016-01-26 23:30:18 +01002942/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002943 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002944 * Return CW_READY when there is something to read.
2945 * Return CW_NOT_READY when there is nothing to read.
2946 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002947 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002948 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002949channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002950{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002951 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002952 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002953
Bram Moolenaard8070362016-02-15 21:56:54 +01002954# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002955 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002956 {
2957 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002958 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002959 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002960 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002961
2962 /* reading from a pipe, not a socket */
2963 while (TRUE)
2964 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002965 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2966
2967 if (r && nread > 0)
2968 return CW_READY;
2969 if (r == 0)
2970 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002971
2972 /* perhaps write some buffer lines */
2973 channel_write_any_lines();
2974
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002975 sleep_time = deadline - GetTickCount();
2976 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002977 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002978 /* Wait for a little while. Very short at first, up to 10 msec
2979 * after looping a few times. */
2980 if (sleep_time > delay)
2981 sleep_time = delay;
2982 Sleep(sleep_time);
2983 delay = delay * 2;
2984 if (delay > 10)
2985 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002986 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002987 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002988 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002989#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002990 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002991#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002992 struct timeval tval;
2993 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002994 fd_set wfds;
2995 int ret;
2996 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002997
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002998 tval.tv_sec = timeout / 1000;
2999 tval.tv_usec = (timeout % 1000) * 1000;
3000 for (;;)
3001 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003002 FD_ZERO(&rfds);
3003 FD_SET((int)fd, &rfds);
3004
3005 /* Write lines to a pipe when a pipe can be written to. Need to
3006 * set this every time, some buffers may be done. */
3007 maxfd = (int)fd + 1;
3008 FD_ZERO(&wfds);
3009 maxfd = channel_fill_wfds(maxfd, &wfds);
3010
3011 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003012# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003013 SOCK_ERRNO;
3014 if (ret == -1 && errno == EINTR)
3015 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003016# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003017 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003018 {
3019 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003020 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003021 channel_write_any_lines();
3022 continue;
3023 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003024 break;
3025 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003026#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003027 for (;;)
3028 {
3029 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3030 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003031
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003032 fds[0].fd = fd;
3033 fds[0].events = POLLIN;
3034 nfd = channel_fill_poll_write(nfd, fds);
3035 if (poll(fds, nfd, timeout) > 0)
3036 {
3037 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003038 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003039 channel_write_any_lines();
3040 continue;
3041 }
3042 break;
3043 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003044#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003045 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003046 return CW_NOT_READY;
3047}
3048
3049 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003050ch_close_part_on_error(
3051 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003052{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003053 char msgbuf[80];
3054
3055 vim_snprintf(msgbuf, sizeof(msgbuf),
3056 "%%s(): Read %s from ch_part[%d], closing",
3057 (is_err ? "error" : "EOF"), part);
3058
3059 if (is_err)
3060 /* Do not call emsg(), most likely the other end just exited. */
3061 ch_errors(channel, msgbuf, func);
3062 else
3063 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003064
3065 /* Queue a "DETACH" netbeans message in the command queue in order to
3066 * terminate the netbeans session later. Do not end the session here
3067 * directly as we may be running in the context of a call to
3068 * netbeans_parse_messages():
3069 * netbeans_parse_messages
3070 * -> autocmd triggered while processing the netbeans cmd
3071 * -> ui_breakcheck
3072 * -> gui event loop or select loop
3073 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003074 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003075 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003076 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003077 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003078 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3079
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003080 /* When reading is not possible close this part of the channel. Don't
3081 * close the channel yet, there may be something to read on another part. */
3082 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003083
3084#ifdef FEAT_GUI
3085 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003086 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003087#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003088}
3089
3090 static void
3091channel_close_now(channel_T *channel)
3092{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003093 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003094 channel_close(channel, TRUE);
3095 if (channel->ch_nb_close_cb != NULL)
3096 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003097}
3098
3099/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003100 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003101 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003102 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003103 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003104 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003105channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003106{
3107 static char_u *buf = NULL;
3108 int len = 0;
3109 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003110 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003111 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003112
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003113 fd = channel->ch_part[part].ch_fd;
3114 if (fd == INVALID_FD)
3115 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003116 ch_errors(channel, "channel_read() called while %s part is closed",
3117 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003118 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003119 }
3120 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003121
3122 /* Allocate a buffer to read into. */
3123 if (buf == NULL)
3124 {
3125 buf = alloc(MAXMSGSIZE);
3126 if (buf == NULL)
3127 return; /* out of memory! */
3128 }
3129
3130 /* Keep on reading for as long as there is something to read.
3131 * Use select() or poll() to avoid blocking on a message that is exactly
3132 * MAXMSGSIZE long. */
3133 for (;;)
3134 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003135 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003136 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003137 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003138 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003139 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003140 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003141 if (len <= 0)
3142 break; /* error or nothing more to read */
3143
3144 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003145 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003146 readlen += len;
3147 if (len < MAXMSGSIZE)
3148 break; /* did read everything that's available */
3149 }
3150
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003151 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003152 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003153 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003154
3155#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003156 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003157 if (CH_HAS_GUI && gtk_main_level() > 0)
3158 gtk_main_quit();
3159#endif
3160}
3161
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003162/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003163 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003164 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003165 * Returns what was read in allocated memory.
3166 * Returns NULL in case of error or timeout.
3167 */
3168 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003169channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003170{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003171 char_u *buf;
3172 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003173 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003174 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003175 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003176 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003177
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003178 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003179 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003180
3181 while (TRUE)
3182 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003183 node = channel_peek(channel, part);
3184 if (node != NULL)
3185 {
3186 if (mode == MODE_RAW || (mode == MODE_NL
3187 && channel_first_nl(node) != NULL))
3188 /* got a complete message */
3189 break;
3190 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3191 continue;
3192 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003193
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003194 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003195 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003196 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003197 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003198 {
3199 ch_log(channel, "Timed out");
3200 return NULL;
3201 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003202 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003203 }
3204
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003205 if (mode == MODE_RAW)
3206 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003207 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003208 }
3209 else
3210 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003211 char_u *p;
3212
3213 buf = node->rq_buffer;
3214 nl = channel_first_nl(node);
3215
3216 /* Convert NUL to NL, the internal representation. */
3217 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3218 if (*p == NUL)
3219 *p = NL;
3220
3221 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003222 {
3223 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003224 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003225 *nl = NUL;
3226 }
3227 else
3228 {
3229 /* Copy the message into allocated memory and remove it from the
3230 * buffer. */
3231 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003232 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003233 }
3234 }
3235 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003236 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003237 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003238}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003239
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003240/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003241 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003242 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003243 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003244 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003245 */
3246 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003247channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003248 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003249 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003250 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003251 int id,
3252 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003253{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003254 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003255 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003256 int timeout;
3257 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003258
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003259 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003260 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003261 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003262 for (;;)
3263 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003264 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003265
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003266 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003267 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003268 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003269 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003270 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003271 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003272
Bram Moolenaard7ece102016-02-02 23:23:02 +01003273 if (!more)
3274 {
3275 /* Handle any other messages in the queue. If done some more
3276 * messages may have arrived. */
3277 if (channel_parse_messages())
3278 continue;
3279
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003280 /* Wait for up to the timeout. If there was an incomplete message
3281 * use the deadline for that. */
3282 timeout = timeout_arg;
3283 if (chanpart->ch_waiting)
3284 {
3285#ifdef WIN32
3286 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3287#else
3288 {
3289 struct timeval now_tv;
3290
3291 gettimeofday(&now_tv, NULL);
3292 timeout = (chanpart->ch_deadline.tv_sec
3293 - now_tv.tv_sec) * 1000
3294 + (chanpart->ch_deadline.tv_usec
3295 - now_tv.tv_usec) / 1000
3296 + 1;
3297 }
3298#endif
3299 if (timeout < 0)
3300 {
3301 /* Something went wrong, channel_parse_json() didn't
3302 * discard message. Cancel waiting. */
3303 chanpart->ch_waiting = FALSE;
3304 timeout = timeout_arg;
3305 }
3306 else if (timeout > timeout_arg)
3307 timeout = timeout_arg;
3308 }
3309 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003310 if (fd == INVALID_FD
3311 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003312 {
3313 if (timeout == timeout_arg)
3314 {
3315 if (fd != INVALID_FD)
3316 ch_log(channel, "Timed out");
3317 break;
3318 }
3319 }
3320 else
3321 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003322 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003323 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003324 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003325 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003326}
3327
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003328/*
3329 * Common for ch_read() and ch_readraw().
3330 */
3331 void
3332common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3333{
3334 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003335 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003336 jobopt_T opt;
3337 int mode;
3338 int timeout;
3339 int id = -1;
3340 typval_T *listtv = NULL;
3341
3342 /* return an empty string by default */
3343 rettv->v_type = VAR_STRING;
3344 rettv->vval.v_string = NULL;
3345
3346 clear_job_options(&opt);
3347 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3348 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003349 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003350
Bram Moolenaar437905c2016-04-26 19:01:05 +02003351 if (opt.jo_set & JO_PART)
3352 part = opt.jo_part;
3353 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003354 if (channel != NULL)
3355 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003356 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003357 part = channel_part_read(channel);
3358 mode = channel_get_mode(channel, part);
3359 timeout = channel_get_timeout(channel, part);
3360 if (opt.jo_set & JO_TIMEOUT)
3361 timeout = opt.jo_timeout;
3362
3363 if (raw || mode == MODE_RAW || mode == MODE_NL)
3364 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3365 else
3366 {
3367 if (opt.jo_set & JO_ID)
3368 id = opt.jo_id;
3369 channel_read_json_block(channel, part, timeout, id, &listtv);
3370 if (listtv != NULL)
3371 {
3372 *rettv = *listtv;
3373 vim_free(listtv);
3374 }
3375 else
3376 {
3377 rettv->v_type = VAR_SPECIAL;
3378 rettv->vval.v_number = VVAL_NONE;
3379 }
3380 }
3381 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003382
3383theend:
3384 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003385}
3386
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003387# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3388 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003389/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003390 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003391 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003392 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003393 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003394channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003395{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003396 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003397 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003398
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003399 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003400 for (channel = first_channel; channel != NULL;
3401 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003402 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003403 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003404 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003405 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003406 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003407 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003408 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003409 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003410 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003411}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003412# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003413
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003414# if defined(WIN32) || defined(PROTO)
3415/*
3416 * Check the channels for anything that is ready to be read.
3417 * The data is put in the read queue.
3418 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003419 void
3420channel_handle_events(void)
3421{
3422 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003423 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003424 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003425
3426 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3427 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003428 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003429 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003430 {
3431 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003432 if (fd != INVALID_FD)
3433 {
3434 int r = channel_wait(channel, fd, 0);
3435
3436 if (r == CW_READY)
3437 channel_read(channel, part, "channel_handle_events");
3438 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003439 ch_close_part_on_error(channel, part, TRUE,
3440 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003441 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003442 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003443 }
3444}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003445# endif
3446
Bram Moolenaard04a0202016-01-26 23:30:18 +01003447/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003448 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003449 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003450 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003451 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003452 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003453channel_send(
3454 channel_T *channel,
3455 ch_part_T part,
3456 char_u *buf,
3457 int len,
3458 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003459{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003460 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003461 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003462
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003463 fd = channel->ch_part[part].ch_fd;
3464 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003465 {
3466 if (!channel->ch_error && fun != NULL)
3467 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003468 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003469 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003470 }
3471 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003472 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003473 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003474
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003475 if (log_fd != NULL)
3476 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003477 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003478 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003479 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003480 fprintf(log_fd, "'\n");
3481 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003482 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003483 }
3484
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003485 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003486 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003487 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003488 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003489 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003490 {
3491 if (!channel->ch_error && fun != NULL)
3492 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003493 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003494 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003495 }
3496 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003497 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003498 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003499
3500 channel->ch_error = FALSE;
3501 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003502}
3503
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003504/*
3505 * Common for "ch_sendexpr()" and "ch_sendraw()".
3506 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003507 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003508 * Otherwise returns NULL.
3509 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003510 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003511send_common(
3512 typval_T *argvars,
3513 char_u *text,
3514 int id,
3515 int eval,
3516 jobopt_T *opt,
3517 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003518 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003519{
3520 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003521 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003522
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003523 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003524 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003525 if (channel == NULL)
3526 return NULL;
3527 part_send = channel_part_send(channel);
3528 *part_read = channel_part_read(channel);
3529
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003530 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3531 return NULL;
3532
3533 /* Set the callback. An empty callback means no callback and not reading
3534 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3535 * allowed. */
3536 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3537 {
3538 if (eval)
3539 {
3540 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3541 return NULL;
3542 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003543 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003544 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003545 }
3546
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003547 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003548 && opt->jo_callback == NULL)
3549 return channel;
3550 return NULL;
3551}
3552
3553/*
3554 * common for "ch_evalexpr()" and "ch_sendexpr()"
3555 */
3556 void
3557ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3558{
3559 char_u *text;
3560 typval_T *listtv;
3561 channel_T *channel;
3562 int id;
3563 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003564 ch_part_T part_send;
3565 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003566 jobopt_T opt;
3567 int timeout;
3568
3569 /* return an empty string by default */
3570 rettv->v_type = VAR_STRING;
3571 rettv->vval.v_string = NULL;
3572
Bram Moolenaar437905c2016-04-26 19:01:05 +02003573 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003574 if (channel == NULL)
3575 return;
3576 part_send = channel_part_send(channel);
3577
3578 ch_mode = channel_get_mode(channel, part_send);
3579 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3580 {
3581 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3582 return;
3583 }
3584
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003585 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003586 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003587 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003588 if (text == NULL)
3589 return;
3590
3591 channel = send_common(argvars, text, id, eval, &opt,
3592 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3593 vim_free(text);
3594 if (channel != NULL && eval)
3595 {
3596 if (opt.jo_set & JO_TIMEOUT)
3597 timeout = opt.jo_timeout;
3598 else
3599 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003600 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3601 == OK)
3602 {
3603 list_T *list = listtv->vval.v_list;
3604
3605 /* Move the item from the list and then change the type to
3606 * avoid the value being freed. */
3607 *rettv = list->lv_last->li_tv;
3608 list->lv_last->li_tv.v_type = VAR_NUMBER;
3609 free_tv(listtv);
3610 }
3611 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003612 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003613}
3614
3615/*
3616 * common for "ch_evalraw()" and "ch_sendraw()"
3617 */
3618 void
3619ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3620{
3621 char_u buf[NUMBUFLEN];
3622 char_u *text;
3623 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003624 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003625 jobopt_T opt;
3626 int timeout;
3627
3628 /* return an empty string by default */
3629 rettv->v_type = VAR_STRING;
3630 rettv->vval.v_string = NULL;
3631
3632 text = get_tv_string_buf(&argvars[1], buf);
3633 channel = send_common(argvars, text, 0, eval, &opt,
3634 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3635 if (channel != NULL && eval)
3636 {
3637 if (opt.jo_set & JO_TIMEOUT)
3638 timeout = opt.jo_timeout;
3639 else
3640 timeout = channel_get_timeout(channel, part_read);
3641 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3642 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003643 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003644}
3645
Bram Moolenaard04a0202016-01-26 23:30:18 +01003646# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003647/*
3648 * Add open channels to the poll struct.
3649 * Return the adjusted struct index.
3650 * The type of "fds" is hidden to avoid problems with the function proto.
3651 */
3652 int
3653channel_poll_setup(int nfd_in, void *fds_in)
3654{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003655 int nfd = nfd_in;
3656 channel_T *channel;
3657 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003658 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003659
Bram Moolenaar77073442016-02-13 23:23:53 +01003660 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003661 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003662 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003663 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003664 chanpart_T *ch_part = &channel->ch_part[part];
3665
3666 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003667 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003668 ch_part->ch_poll_idx = nfd;
3669 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003670 fds[nfd].events = POLLIN;
3671 nfd++;
3672 }
3673 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003674 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003675 }
3676 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003677
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003678 nfd = channel_fill_poll_write(nfd, fds);
3679
Bram Moolenaare0874f82016-01-24 20:36:41 +01003680 return nfd;
3681}
3682
3683/*
3684 * The type of "fds" is hidden to avoid problems with the function proto.
3685 */
3686 int
3687channel_poll_check(int ret_in, void *fds_in)
3688{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003689 int ret = ret_in;
3690 channel_T *channel;
3691 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003692 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003693 int idx;
3694 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003695
Bram Moolenaar77073442016-02-13 23:23:53 +01003696 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003697 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003698 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003699 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003700 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003701
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003702 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003703 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003704 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003705 --ret;
3706 }
3707 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003708
3709 in_part = &channel->ch_part[PART_IN];
3710 idx = in_part->ch_poll_idx;
3711 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3712 {
3713 if (in_part->ch_buf_append)
3714 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003715 if (in_part->ch_bufref.br_buf != NULL)
3716 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003717 }
3718 else
3719 channel_write_in(channel);
3720 --ret;
3721 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003722 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003723
3724 return ret;
3725}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003726# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003727
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003728# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003729/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003730 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003731 */
3732 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003733channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003734{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003735 int maxfd = maxfd_in;
3736 channel_T *channel;
3737 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003738 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003739 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003740
Bram Moolenaar77073442016-02-13 23:23:53 +01003741 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003742 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003743 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003744 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003745 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003746
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003747 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003748 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003749 FD_SET((int)fd, rfds);
3750 if (maxfd < (int)fd)
3751 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003752 }
3753 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003754 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003755
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003756 maxfd = channel_fill_wfds(maxfd, wfds);
3757
Bram Moolenaare0874f82016-01-24 20:36:41 +01003758 return maxfd;
3759}
3760
3761/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003762 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003763 */
3764 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003765channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003766{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003767 int ret = ret_in;
3768 channel_T *channel;
3769 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003770 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003771 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003772 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003773
Bram Moolenaar77073442016-02-13 23:23:53 +01003774 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003775 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003776 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003777 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003778 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003779
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003780 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003781 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003782 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003783 --ret;
3784 }
3785 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003786
3787 in_part = &channel->ch_part[PART_IN];
3788 if (ret > 0 && in_part->ch_fd != INVALID_FD
3789 && FD_ISSET(in_part->ch_fd, wfds))
3790 {
3791 if (in_part->ch_buf_append)
3792 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003793 if (in_part->ch_bufref.br_buf != NULL)
3794 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003795 }
3796 else
3797 channel_write_in(channel);
3798 --ret;
3799 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003800 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003801
3802 return ret;
3803}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003804# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003805
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003806/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003807 * Execute queued up commands.
3808 * Invoked from the main loop when it's safe to execute received commands.
3809 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003810 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003811 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003812channel_parse_messages(void)
3813{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003814 channel_T *channel = first_channel;
3815 int ret = FALSE;
3816 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003817 ch_part_T part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003818
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003819 ++safe_to_invoke_callback;
3820
Bram Moolenaard0b65022016-03-06 21:50:33 +01003821 /* Only do this message when another message was given, otherwise we get
3822 * lots of them. */
3823 if (did_log_msg)
3824 {
3825 ch_log(NULL, "looking for messages on channels");
3826 did_log_msg = FALSE;
3827 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003828 while (channel != NULL)
3829 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003830 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003831 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003832 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003833 channel_close_now(channel);
3834 /* channel may have been freed, start over */
3835 channel = first_channel;
3836 continue;
3837 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003838 if (channel->ch_to_be_freed)
3839 {
3840 channel_free(channel);
3841 /* channel has been freed, start over */
3842 channel = first_channel;
3843 continue;
3844 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003845 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003846 {
3847 /* channel is no longer useful, free it */
3848 channel_free(channel);
3849 channel = first_channel;
3850 part = PART_SOCK;
3851 continue;
3852 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003853 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003854 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003855 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003856 /* Increase the refcount, in case the handler causes the channel
3857 * to be unreferenced or closed. */
3858 ++channel->ch_refcount;
3859 r = may_invoke_callback(channel, part);
3860 if (r == OK)
3861 ret = TRUE;
3862 if (channel_unref(channel) || r == OK)
3863 {
3864 /* channel was freed or something was done, start over */
3865 channel = first_channel;
3866 part = PART_SOCK;
3867 continue;
3868 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003869 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003870 if (part < PART_ERR)
3871 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003872 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003873 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003874 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003875 part = PART_SOCK;
3876 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003877 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003878
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003879 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003880 {
3881 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003882 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003883 }
3884
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003885 --safe_to_invoke_callback;
3886
Bram Moolenaard7ece102016-02-02 23:23:02 +01003887 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003888}
3889
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003890/*
3891 * Mark references to lists used in channels.
3892 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003893 int
3894set_ref_in_channel(int copyID)
3895{
Bram Moolenaar77073442016-02-13 23:23:53 +01003896 int abort = FALSE;
3897 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003898 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003899
Bram Moolenaar77073442016-02-13 23:23:53 +01003900 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003901 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003902 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003903 tv.v_type = VAR_CHANNEL;
3904 tv.vval.v_channel = channel;
3905 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003906 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003907 return abort;
3908}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003909
3910/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003911 * Return the "part" to write to for "channel".
3912 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003913 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003914channel_part_send(channel_T *channel)
3915{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003916 if (channel->CH_SOCK_FD == INVALID_FD)
3917 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003918 return PART_SOCK;
3919}
3920
3921/*
3922 * Return the default "part" to read from for "channel".
3923 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003924 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003925channel_part_read(channel_T *channel)
3926{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003927 if (channel->CH_SOCK_FD == INVALID_FD)
3928 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003929 return PART_SOCK;
3930}
3931
3932/*
3933 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003934 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003935 */
3936 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003937channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003938{
Bram Moolenaar77073442016-02-13 23:23:53 +01003939 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003940 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003941 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003942}
3943
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003944/*
3945 * Return the timeout of "channel"/"part"
3946 */
3947 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003948channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003949{
3950 return channel->ch_part[part].ch_timeout;
3951}
3952
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003953 static int
3954handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3955{
3956 char_u *val = get_tv_string(item);
3957
3958 opt->jo_set |= jo;
3959 if (STRCMP(val, "nl") == 0)
3960 *modep = MODE_NL;
3961 else if (STRCMP(val, "raw") == 0)
3962 *modep = MODE_RAW;
3963 else if (STRCMP(val, "js") == 0)
3964 *modep = MODE_JS;
3965 else if (STRCMP(val, "json") == 0)
3966 *modep = MODE_JSON;
3967 else
3968 {
3969 EMSG2(_(e_invarg2), val);
3970 return FAIL;
3971 }
3972 return OK;
3973}
3974
3975 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003976handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003977{
3978 char_u *val = get_tv_string(item);
3979
3980 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3981 if (STRCMP(val, "null") == 0)
3982 opt->jo_io[part] = JIO_NULL;
3983 else if (STRCMP(val, "pipe") == 0)
3984 opt->jo_io[part] = JIO_PIPE;
3985 else if (STRCMP(val, "file") == 0)
3986 opt->jo_io[part] = JIO_FILE;
3987 else if (STRCMP(val, "buffer") == 0)
3988 opt->jo_io[part] = JIO_BUFFER;
3989 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3990 opt->jo_io[part] = JIO_OUT;
3991 else
3992 {
3993 EMSG2(_(e_invarg2), val);
3994 return FAIL;
3995 }
3996 return OK;
3997}
3998
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003999/*
4000 * Clear a jobopt_T before using it.
4001 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004002 void
4003clear_job_options(jobopt_T *opt)
4004{
4005 vim_memset(opt, 0, sizeof(jobopt_T));
4006}
4007
4008/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004009 * Free any members of a jobopt_T.
4010 */
4011 void
4012free_job_options(jobopt_T *opt)
4013{
4014 if (opt->jo_partial != NULL)
4015 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004016 else if (opt->jo_callback != NULL)
4017 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004018 if (opt->jo_out_partial != NULL)
4019 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004020 else if (opt->jo_out_cb != NULL)
4021 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004022 if (opt->jo_err_partial != NULL)
4023 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004024 else if (opt->jo_err_cb != NULL)
4025 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004026 if (opt->jo_close_partial != NULL)
4027 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004028 else if (opt->jo_close_cb != NULL)
4029 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004030 if (opt->jo_exit_partial != NULL)
4031 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004032 else if (opt->jo_exit_cb != NULL)
4033 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004034}
4035
4036/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004037 * Get the PART_ number from the first character of an option name.
4038 */
4039 static int
4040part_from_char(int c)
4041{
4042 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4043}
4044
4045/*
4046 * Get the option entries from the dict in "tv", parse them and put the result
4047 * in "opt".
4048 * Only accept options in "supported".
4049 * If an option value is invalid return FAIL.
4050 */
4051 int
4052get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4053{
4054 typval_T *item;
4055 char_u *val;
4056 dict_T *dict;
4057 int todo;
4058 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004059 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004060
4061 opt->jo_set = 0;
4062 if (tv->v_type == VAR_UNKNOWN)
4063 return OK;
4064 if (tv->v_type != VAR_DICT)
4065 {
4066 EMSG(_(e_invarg));
4067 return FAIL;
4068 }
4069 dict = tv->vval.v_dict;
4070 if (dict == NULL)
4071 return OK;
4072
4073 todo = (int)dict->dv_hashtab.ht_used;
4074 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4075 if (!HASHITEM_EMPTY(hi))
4076 {
4077 item = &dict_lookup(hi)->di_tv;
4078
4079 if (STRCMP(hi->hi_key, "mode") == 0)
4080 {
4081 if (!(supported & JO_MODE))
4082 break;
4083 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4084 return FAIL;
4085 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004086 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004087 {
4088 if (!(supported & JO_IN_MODE))
4089 break;
4090 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4091 == FAIL)
4092 return FAIL;
4093 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004094 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004095 {
4096 if (!(supported & JO_OUT_MODE))
4097 break;
4098 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4099 == FAIL)
4100 return FAIL;
4101 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004102 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004103 {
4104 if (!(supported & JO_ERR_MODE))
4105 break;
4106 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4107 == FAIL)
4108 return FAIL;
4109 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004110 else if (STRCMP(hi->hi_key, "in_io") == 0
4111 || STRCMP(hi->hi_key, "out_io") == 0
4112 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113 {
4114 if (!(supported & JO_OUT_IO))
4115 break;
4116 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4117 return FAIL;
4118 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004119 else if (STRCMP(hi->hi_key, "in_name") == 0
4120 || STRCMP(hi->hi_key, "out_name") == 0
4121 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004122 {
4123 part = part_from_char(*hi->hi_key);
4124
4125 if (!(supported & JO_OUT_IO))
4126 break;
4127 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4128 opt->jo_io_name[part] =
4129 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4130 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004131 else if (STRCMP(hi->hi_key, "in_buf") == 0
4132 || STRCMP(hi->hi_key, "out_buf") == 0
4133 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004134 {
4135 part = part_from_char(*hi->hi_key);
4136
4137 if (!(supported & JO_OUT_IO))
4138 break;
4139 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4140 opt->jo_io_buf[part] = get_tv_number(item);
4141 if (opt->jo_io_buf[part] <= 0)
4142 {
4143 EMSG2(_(e_invarg2), get_tv_string(item));
4144 return FAIL;
4145 }
4146 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4147 {
4148 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4149 return FAIL;
4150 }
4151 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004152 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4153 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4154 {
4155 part = part_from_char(*hi->hi_key);
4156
4157 if (!(supported & JO_OUT_IO))
4158 break;
4159 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4160 opt->jo_modifiable[part] = get_tv_number(item);
4161 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004162 else if (STRCMP(hi->hi_key, "out_msg") == 0
4163 || STRCMP(hi->hi_key, "err_msg") == 0)
4164 {
4165 part = part_from_char(*hi->hi_key);
4166
4167 if (!(supported & JO_OUT_IO))
4168 break;
4169 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4170 opt->jo_message[part] = get_tv_number(item);
4171 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004172 else if (STRCMP(hi->hi_key, "in_top") == 0
4173 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004174 {
4175 linenr_T *lp;
4176
4177 if (!(supported & JO_OUT_IO))
4178 break;
4179 if (hi->hi_key[3] == 't')
4180 {
4181 lp = &opt->jo_in_top;
4182 opt->jo_set |= JO_IN_TOP;
4183 }
4184 else
4185 {
4186 lp = &opt->jo_in_bot;
4187 opt->jo_set |= JO_IN_BOT;
4188 }
4189 *lp = get_tv_number(item);
4190 if (*lp < 0)
4191 {
4192 EMSG2(_(e_invarg2), get_tv_string(item));
4193 return FAIL;
4194 }
4195 }
4196 else if (STRCMP(hi->hi_key, "channel") == 0)
4197 {
4198 if (!(supported & JO_OUT_IO))
4199 break;
4200 opt->jo_set |= JO_CHANNEL;
4201 if (item->v_type != VAR_CHANNEL)
4202 {
4203 EMSG2(_(e_invarg2), "channel");
4204 return FAIL;
4205 }
4206 opt->jo_channel = item->vval.v_channel;
4207 }
4208 else if (STRCMP(hi->hi_key, "callback") == 0)
4209 {
4210 if (!(supported & JO_CALLBACK))
4211 break;
4212 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004213 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004214 if (opt->jo_callback == NULL)
4215 {
4216 EMSG2(_(e_invarg2), "callback");
4217 return FAIL;
4218 }
4219 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004220 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004221 {
4222 if (!(supported & JO_OUT_CALLBACK))
4223 break;
4224 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004225 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004226 if (opt->jo_out_cb == NULL)
4227 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004228 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004229 return FAIL;
4230 }
4231 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004232 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004233 {
4234 if (!(supported & JO_ERR_CALLBACK))
4235 break;
4236 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004237 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004238 if (opt->jo_err_cb == NULL)
4239 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004240 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004241 return FAIL;
4242 }
4243 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004244 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004245 {
4246 if (!(supported & JO_CLOSE_CALLBACK))
4247 break;
4248 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004249 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004250 if (opt->jo_close_cb == NULL)
4251 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004252 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004253 return FAIL;
4254 }
4255 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004256 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4257 {
4258 if (!(supported & JO_EXIT_CB))
4259 break;
4260 opt->jo_set |= JO_EXIT_CB;
4261 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4262 if (opt->jo_exit_cb == NULL)
4263 {
4264 EMSG2(_(e_invarg2), "exit_cb");
4265 return FAIL;
4266 }
4267 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004268 else if (STRCMP(hi->hi_key, "waittime") == 0)
4269 {
4270 if (!(supported & JO_WAITTIME))
4271 break;
4272 opt->jo_set |= JO_WAITTIME;
4273 opt->jo_waittime = get_tv_number(item);
4274 }
4275 else if (STRCMP(hi->hi_key, "timeout") == 0)
4276 {
4277 if (!(supported & JO_TIMEOUT))
4278 break;
4279 opt->jo_set |= JO_TIMEOUT;
4280 opt->jo_timeout = get_tv_number(item);
4281 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004282 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004283 {
4284 if (!(supported & JO_OUT_TIMEOUT))
4285 break;
4286 opt->jo_set |= JO_OUT_TIMEOUT;
4287 opt->jo_out_timeout = get_tv_number(item);
4288 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004289 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004290 {
4291 if (!(supported & JO_ERR_TIMEOUT))
4292 break;
4293 opt->jo_set |= JO_ERR_TIMEOUT;
4294 opt->jo_err_timeout = get_tv_number(item);
4295 }
4296 else if (STRCMP(hi->hi_key, "part") == 0)
4297 {
4298 if (!(supported & JO_PART))
4299 break;
4300 opt->jo_set |= JO_PART;
4301 val = get_tv_string(item);
4302 if (STRCMP(val, "err") == 0)
4303 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004304 else if (STRCMP(val, "out") == 0)
4305 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004306 else
4307 {
4308 EMSG2(_(e_invarg2), val);
4309 return FAIL;
4310 }
4311 }
4312 else if (STRCMP(hi->hi_key, "id") == 0)
4313 {
4314 if (!(supported & JO_ID))
4315 break;
4316 opt->jo_set |= JO_ID;
4317 opt->jo_id = get_tv_number(item);
4318 }
4319 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4320 {
4321 if (!(supported & JO_STOPONEXIT))
4322 break;
4323 opt->jo_set |= JO_STOPONEXIT;
4324 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4325 opt->jo_soe_buf);
4326 if (opt->jo_stoponexit == NULL)
4327 {
4328 EMSG2(_(e_invarg2), "stoponexit");
4329 return FAIL;
4330 }
4331 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004332 else if (STRCMP(hi->hi_key, "block_write") == 0)
4333 {
4334 if (!(supported & JO_BLOCK_WRITE))
4335 break;
4336 opt->jo_set |= JO_BLOCK_WRITE;
4337 opt->jo_block_write = get_tv_number(item);
4338 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004339 else
4340 break;
4341 --todo;
4342 }
4343 if (todo > 0)
4344 {
4345 EMSG2(_(e_invarg2), hi->hi_key);
4346 return FAIL;
4347 }
4348
4349 return OK;
4350}
4351
4352/*
4353 * Get the channel from the argument.
4354 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004355 * When "check_open" is TRUE check that the channel can be used.
4356 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004357 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 */
4359 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004360get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004361{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004362 channel_T *channel = NULL;
4363 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004364
4365 if (tv->v_type == VAR_JOB)
4366 {
4367 if (tv->vval.v_job != NULL)
4368 channel = tv->vval.v_job->jv_channel;
4369 }
4370 else if (tv->v_type == VAR_CHANNEL)
4371 {
4372 channel = tv->vval.v_channel;
4373 }
4374 else
4375 {
4376 EMSG2(_(e_invarg2), get_tv_string(tv));
4377 return NULL;
4378 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004379 if (channel != NULL && reading)
4380 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004381 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004382
Bram Moolenaar437905c2016-04-26 19:01:05 +02004383 if (check_open && (channel == NULL || (!channel_is_open(channel)
4384 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004385 {
4386 EMSG(_("E906: not an open channel"));
4387 return NULL;
4388 }
4389 return channel;
4390}
4391
4392static job_T *first_job = NULL;
4393
4394 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004395job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004396{
4397 ch_log(job->jv_channel, "Freeing job");
4398 if (job->jv_channel != NULL)
4399 {
4400 /* The link from the channel to the job doesn't count as a reference,
4401 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004402 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004403 * NULL the reference. We don't set ch_job_killed, unreferencing the
4404 * job doesn't mean it stops running. */
4405 job->jv_channel->ch_job = NULL;
4406 channel_unref(job->jv_channel);
4407 }
4408 mch_clear_job(job);
4409
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004410 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004411 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004412}
4413
4414 static void
4415job_free_job(job_T *job)
4416{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004417 if (job->jv_next != NULL)
4418 job->jv_next->jv_prev = job->jv_prev;
4419 if (job->jv_prev == NULL)
4420 first_job = job->jv_next;
4421 else
4422 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004423 vim_free(job);
4424}
4425
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004426 static void
4427job_free(job_T *job)
4428{
4429 if (!in_free_unref_items)
4430 {
4431 job_free_contents(job);
4432 job_free_job(job);
4433 }
4434}
4435
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004436#if defined(EXITFREE) || defined(PROTO)
4437 void
4438job_free_all(void)
4439{
4440 while (first_job != NULL)
4441 job_free(first_job);
4442}
4443#endif
4444
4445/*
4446 * Return TRUE if we need to check if the process of "job" has ended.
4447 */
4448 static int
4449job_need_end_check(job_T *job)
4450{
4451 return job->jv_status == JOB_STARTED
4452 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4453}
4454
4455/*
4456 * Return TRUE if the channel of "job" is still useful.
4457 */
4458 static int
4459job_channel_still_useful(job_T *job)
4460{
4461 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4462}
4463
4464/*
4465 * Return TRUE if the job should not be freed yet. Do not free the job when
4466 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4467 * or when the associated channel will do something with the job output.
4468 */
4469 static int
4470job_still_useful(job_T *job)
4471{
4472 return job_need_end_check(job) || job_channel_still_useful(job);
4473}
4474
4475/*
4476 * NOTE: Must call job_cleanup() only once right after the status of "job"
4477 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4478 * mch_detect_ended_job() returned non-NULL).
4479 */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004480 static void
4481job_cleanup(job_T *job)
4482{
4483 if (job->jv_status != JOB_ENDED)
4484 return;
4485
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004486 /* Ready to cleanup the job. */
4487 job->jv_status = JOB_FINISHED;
4488
Bram Moolenaar97792de2016-10-15 18:36:49 +02004489 if (job->jv_exit_cb != NULL)
4490 {
4491 typval_T argv[3];
4492 typval_T rettv;
4493 int dummy;
4494
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004495 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004496 ++job->jv_refcount;
4497 argv[0].v_type = VAR_JOB;
4498 argv[0].vval.v_job = job;
4499 argv[1].v_type = VAR_NUMBER;
4500 argv[1].vval.v_number = job->jv_exitval;
4501 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4502 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4503 job->jv_exit_partial, NULL);
4504 clear_tv(&rettv);
4505 --job->jv_refcount;
4506 channel_need_redraw = TRUE;
4507 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004508
4509 /* Do not free the job in case the close callback of the associated channel
4510 * isn't invoked yet and may get information by job_info(). */
4511 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004512 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004513 /* The job was already unreferenced and the associated channel was
4514 * detached, now that it ended it can be freed. Careful: caller must
4515 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004516 job_free(job);
4517 }
4518}
4519
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004520/*
4521 * Mark references in jobs that are still useful.
4522 */
4523 int
4524set_ref_in_job(int copyID)
4525{
4526 int abort = FALSE;
4527 job_T *job;
4528 typval_T tv;
4529
4530 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004531 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004532 {
4533 tv.v_type = VAR_JOB;
4534 tv.vval.v_job = job;
4535 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4536 }
4537 return abort;
4538}
4539
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004540/*
4541 * Dereference "job". Note that after this "job" may have been freed.
4542 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004543 void
4544job_unref(job_T *job)
4545{
4546 if (job != NULL && --job->jv_refcount <= 0)
4547 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004548 /* Do not free the job if there is a channel where the close callback
4549 * may get the job info. */
4550 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004552 /* Do not free the job when it has not ended yet and there is a
4553 * "stoponexit" flag or an exit callback. */
4554 if (!job_need_end_check(job))
4555 {
4556 job_free(job);
4557 }
4558 else if (job->jv_channel != NULL)
4559 {
4560 /* Do remove the link to the channel, otherwise it hangs
4561 * around until Vim exits. See job_free() for refcount. */
4562 ch_log(job->jv_channel, "detaching channel from job");
4563 job->jv_channel->ch_job = NULL;
4564 channel_unref(job->jv_channel);
4565 job->jv_channel = NULL;
4566 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004567 }
4568 }
4569}
4570
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571 int
4572free_unused_jobs_contents(int copyID, int mask)
4573{
4574 int did_free = FALSE;
4575 job_T *job;
4576
4577 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004578 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004579 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004580 {
4581 /* Free the channel and ordinary items it contains, but don't
4582 * recurse into Lists, Dictionaries etc. */
4583 job_free_contents(job);
4584 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004585 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004586 return did_free;
4587}
4588
4589 void
4590free_unused_jobs(int copyID, int mask)
4591{
4592 job_T *job;
4593 job_T *job_next;
4594
4595 for (job = first_job; job != NULL; job = job_next)
4596 {
4597 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004598 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004599 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004600 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004601 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004602 job_free_job(job);
4603 }
4604 }
4605}
4606
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004607/*
4608 * Allocate a job. Sets the refcount to one and sets options default.
4609 */
4610 static job_T *
4611job_alloc(void)
4612{
4613 job_T *job;
4614
4615 job = (job_T *)alloc_clear(sizeof(job_T));
4616 if (job != NULL)
4617 {
4618 job->jv_refcount = 1;
4619 job->jv_stoponexit = vim_strsave((char_u *)"term");
4620
4621 if (first_job != NULL)
4622 {
4623 first_job->jv_prev = job;
4624 job->jv_next = first_job;
4625 }
4626 first_job = job;
4627 }
4628 return job;
4629}
4630
4631 void
4632job_set_options(job_T *job, jobopt_T *opt)
4633{
4634 if (opt->jo_set & JO_STOPONEXIT)
4635 {
4636 vim_free(job->jv_stoponexit);
4637 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4638 job->jv_stoponexit = NULL;
4639 else
4640 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4641 }
4642 if (opt->jo_set & JO_EXIT_CB)
4643 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004644 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004645 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004646 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004647 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004648 job->jv_exit_partial = NULL;
4649 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004650 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004651 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004652 job->jv_exit_partial = opt->jo_exit_partial;
4653 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004654 {
4655 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004656 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004657 }
4658 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004659 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004660 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004661 func_ref(job->jv_exit_cb);
4662 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004663 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004664 }
4665}
4666
4667/*
4668 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4669 */
4670 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004671job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004672{
4673 job_T *job;
4674
4675 for (job = first_job; job != NULL; job = job->jv_next)
4676 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4677 mch_stop_job(job, job->jv_stoponexit);
4678}
4679
4680/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004681 * Return TRUE when there is any job that has an exit callback and might exit,
4682 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004683 */
4684 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004685has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004686{
4687 job_T *job;
4688
4689 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004690 /* Only should check if the channel has been closed, if the channel is
4691 * open the job won't exit. */
4692 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004693 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004694 return TRUE;
4695 return FALSE;
4696}
4697
Bram Moolenaar97792de2016-10-15 18:36:49 +02004698#define MAX_CHECK_ENDED 8
4699
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004700/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004701 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004702 */
4703 void
4704job_check_ended(void)
4705{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004706 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004707
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004708 if (first_job == NULL)
4709 return;
4710
Bram Moolenaar97792de2016-10-15 18:36:49 +02004711 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004712 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004713 /* NOTE: mch_detect_ended_job() must only return a job of which the
4714 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004715 job_T *job = mch_detect_ended_job(first_job);
4716
4717 if (job == NULL)
4718 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004719 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004720 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004721
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004722 if (channel_need_redraw)
4723 {
4724 channel_need_redraw = FALSE;
4725 redraw_after_callback();
4726 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004727}
4728
4729/*
4730 * "job_start()" function
4731 */
4732 job_T *
4733job_start(typval_T *argvars)
4734{
4735 job_T *job;
4736 char_u *cmd = NULL;
4737#if defined(UNIX)
4738# define USE_ARGV
4739 char **argv = NULL;
4740 int argc = 0;
4741#else
4742 garray_T ga;
4743#endif
4744 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004745 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004746
4747 job = job_alloc();
4748 if (job == NULL)
4749 return NULL;
4750
4751 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004752#ifndef USE_ARGV
4753 ga_init2(&ga, (int)sizeof(char*), 20);
4754#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004755
4756 /* Default mode is NL. */
4757 clear_job_options(&opt);
4758 opt.jo_mode = MODE_NL;
4759 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004760 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4761 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004762 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763
4764 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004765 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004766 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4767 && opt.jo_io[part] == JIO_FILE
4768 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4769 || *opt.jo_io_name[part] == NUL))
4770 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004771 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004772 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004773 }
4774
4775 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4776 {
4777 buf_T *buf = NULL;
4778
4779 /* check that we can find the buffer before starting the job */
4780 if (opt.jo_set & JO_IN_BUF)
4781 {
4782 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4783 if (buf == NULL)
4784 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4785 }
4786 else if (!(opt.jo_set & JO_IN_NAME))
4787 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004788 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004789 }
4790 else
4791 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4792 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004793 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004794 if (buf->b_ml.ml_mfp == NULL)
4795 {
4796 char_u numbuf[NUMBUFLEN];
4797 char_u *s;
4798
4799 if (opt.jo_set & JO_IN_BUF)
4800 {
4801 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4802 s = numbuf;
4803 }
4804 else
4805 s = opt.jo_io_name[PART_IN];
4806 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004807 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004808 }
4809 job->jv_in_buf = buf;
4810 }
4811
4812 job_set_options(job, &opt);
4813
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004814 if (argvars[0].v_type == VAR_STRING)
4815 {
4816 /* Command is a string. */
4817 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004818 if (cmd == NULL || *cmd == NUL)
4819 {
4820 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004821 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004822 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004823#ifdef USE_ARGV
4824 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004825 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004826 argv[argc] = NULL;
4827#endif
4828 }
4829 else if (argvars[0].v_type != VAR_LIST
4830 || argvars[0].vval.v_list == NULL
4831 || argvars[0].vval.v_list->lv_len < 1)
4832 {
4833 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004834 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004835 }
4836 else
4837 {
4838 list_T *l = argvars[0].vval.v_list;
4839 listitem_T *li;
4840 char_u *s;
4841
4842#ifdef USE_ARGV
4843 /* Pass argv[] to mch_call_shell(). */
4844 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4845 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004846 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004847#endif
4848 for (li = l->lv_first; li != NULL; li = li->li_next)
4849 {
4850 s = get_tv_string_chk(&li->li_tv);
4851 if (s == NULL)
4852 goto theend;
4853#ifdef USE_ARGV
4854 argv[argc++] = (char *)s;
4855#else
4856 /* Only escape when needed, double quotes are not always allowed. */
4857 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4858 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004859# ifdef WIN32
4860 int old_ssl = p_ssl;
4861
4862 /* This is using CreateProcess, not cmd.exe. Always use
4863 * double quote and backslashes. */
4864 p_ssl = 0;
4865# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004866 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004867# ifdef WIN32
4868 p_ssl = old_ssl;
4869# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004870 if (s == NULL)
4871 goto theend;
4872 ga_concat(&ga, s);
4873 vim_free(s);
4874 }
4875 else
4876 ga_concat(&ga, s);
4877 if (li->li_next != NULL)
4878 ga_append(&ga, ' ');
4879#endif
4880 }
4881#ifdef USE_ARGV
4882 argv[argc] = NULL;
4883#else
4884 cmd = ga.ga_data;
4885#endif
4886 }
4887
4888#ifdef USE_ARGV
4889 if (ch_log_active())
4890 {
4891 garray_T ga;
4892 int i;
4893
4894 ga_init2(&ga, (int)sizeof(char), 200);
4895 for (i = 0; i < argc; ++i)
4896 {
4897 if (i > 0)
4898 ga_concat(&ga, (char_u *)" ");
4899 ga_concat(&ga, (char_u *)argv[i]);
4900 }
4901 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4902 ga_clear(&ga);
4903 }
4904 mch_start_job(argv, job, &opt);
4905#else
4906 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4907 mch_start_job((char *)cmd, job, &opt);
4908#endif
4909
4910 /* If the channel is reading from a buffer, write lines now. */
4911 if (job->jv_channel != NULL)
4912 channel_write_in(job->jv_channel);
4913
4914theend:
4915#ifdef USE_ARGV
4916 vim_free(argv);
4917#else
4918 vim_free(ga.ga_data);
4919#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004920 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004921 return job;
4922}
4923
4924/*
4925 * Get the status of "job" and invoke the exit callback when needed.
4926 * The returned string is not allocated.
4927 */
4928 char *
4929job_status(job_T *job)
4930{
4931 char *result;
4932
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004933 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004934 /* No need to check, dead is dead. */
4935 result = "dead";
4936 else if (job->jv_status == JOB_FAILED)
4937 result = "fail";
4938 else
4939 {
4940 result = mch_job_status(job);
4941 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02004942 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004943 }
4944 return result;
4945}
4946
Bram Moolenaar8950a562016-03-12 15:22:55 +01004947/*
4948 * Implementation of job_info().
4949 */
4950 void
4951job_info(job_T *job, dict_T *dict)
4952{
4953 dictitem_T *item;
4954 varnumber_T nr;
4955
4956 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4957
4958 item = dictitem_alloc((char_u *)"channel");
4959 if (item == NULL)
4960 return;
4961 item->di_tv.v_lock = 0;
4962 item->di_tv.v_type = VAR_CHANNEL;
4963 item->di_tv.vval.v_channel = job->jv_channel;
4964 if (job->jv_channel != NULL)
4965 ++job->jv_channel->ch_refcount;
4966 if (dict_add(dict, item) == FAIL)
4967 dictitem_free(item);
4968
4969#ifdef UNIX
4970 nr = job->jv_pid;
4971#else
4972 nr = job->jv_proc_info.dwProcessId;
4973#endif
4974 dict_add_nr_str(dict, "process", nr, NULL);
4975
4976 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004977 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004978 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4979}
4980
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004981 int
4982job_stop(job_T *job, typval_T *argvars)
4983{
4984 char_u *arg;
4985
4986 if (argvars[1].v_type == VAR_UNKNOWN)
4987 arg = (char_u *)"";
4988 else
4989 {
4990 arg = get_tv_string_chk(&argvars[1]);
4991 if (arg == NULL)
4992 {
4993 EMSG(_(e_invarg));
4994 return 0;
4995 }
4996 }
4997 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4998 if (mch_stop_job(job, arg) == FAIL)
4999 return 0;
5000
5001 /* Assume that "hup" does not kill the job. */
5002 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
5003 job->jv_channel->ch_job_killed = TRUE;
5004
5005 /* We don't try freeing the job, obviously the caller still has a
5006 * reference to it. */
5007 return 1;
5008}
5009
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005010#endif /* FEAT_JOB_CHANNEL */