blob: f522e80c4ae323fb2ddd74a9aa922c969fde8acb [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 Moolenaar958dc692016-12-01 15:34:12 +01001198 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001199
1200 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1201 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001202 buf_T *buf;
1203
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001204 /* writing output to a buffer. Default mode is NL. */
1205 if (!(opt->jo_set & JO_OUT_MODE))
1206 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001207 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001208 {
1209 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1210 if (buf == NULL)
1211 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1212 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001213 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001214 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001215 int msg = TRUE;
1216
1217 if (opt->jo_set2 & JO2_OUT_MSG)
1218 msg = opt->jo_message[PART_OUT];
1219 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001220 }
1221 if (buf != NULL)
1222 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001223 if (opt->jo_set & JO_OUT_MODIFIABLE)
1224 channel->ch_part[PART_OUT].ch_nomodifiable =
1225 !opt->jo_modifiable[PART_OUT];
1226
1227 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1228 {
1229 EMSG(_(e_modifiable));
1230 }
1231 else
1232 {
1233 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001234 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001235 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001236 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001237 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001238 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001239
1240 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1241 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1242 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1243 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 buf_T *buf;
1245
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001246 /* writing err to a buffer. Default mode is NL. */
1247 if (!(opt->jo_set & JO_ERR_MODE))
1248 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1249 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001250 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001251 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001252 {
1253 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1254 if (buf == NULL)
1255 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1256 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001257 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001258 {
1259 int msg = TRUE;
1260
1261 if (opt->jo_set2 & JO2_ERR_MSG)
1262 msg = opt->jo_message[PART_ERR];
1263 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1264 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001265 if (buf != NULL)
1266 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001267 if (opt->jo_set & JO_ERR_MODIFIABLE)
1268 channel->ch_part[PART_ERR].ch_nomodifiable =
1269 !opt->jo_modifiable[PART_ERR];
1270 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1271 {
1272 EMSG(_(e_modifiable));
1273 }
1274 else
1275 {
1276 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001277 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001278 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001279 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001280 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001281 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001282
1283 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1284 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1285 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001286}
1287
1288/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001289 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001290 */
1291 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001292channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001293 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001294 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001295 char_u *callback,
1296 partial_T *partial,
1297 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001298{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001299 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001300 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1301
1302 if (item != NULL)
1303 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001304 item->cq_partial = partial;
1305 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001306 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001307 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001308 item->cq_callback = callback;
1309 }
1310 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001311 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001312 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001313 func_ref(item->cq_callback);
1314 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001315 item->cq_seq_nr = id;
1316 item->cq_prev = head->cq_prev;
1317 head->cq_prev = item;
1318 item->cq_next = NULL;
1319 if (item->cq_prev == NULL)
1320 head->cq_next = item;
1321 else
1322 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001323 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001324}
1325
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001326 static void
1327write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1328{
1329 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001330 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001331 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001332 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001333
Bram Moolenaar655da312016-05-28 22:22:34 +02001334 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001335 if ((p = alloc(len + 2)) == NULL)
1336 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001337 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001338
1339 for (i = 0; i < len; ++i)
1340 if (p[i] == NL)
1341 p[i] = NUL;
1342
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001343 p[len] = NL;
1344 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001345 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001346 vim_free(p);
1347}
1348
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001349/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001350 * Return TRUE if "channel" can be written to.
1351 * Returns FALSE if the input is closed or the write would block.
1352 */
1353 static int
1354can_write_buf_line(channel_T *channel)
1355{
1356 chanpart_T *in_part = &channel->ch_part[PART_IN];
1357
1358 if (in_part->ch_fd == INVALID_FD)
1359 return FALSE; /* pipe was closed */
1360
1361 /* for testing: block every other attempt to write */
1362 if (in_part->ch_block_write == 1)
1363 in_part->ch_block_write = -1;
1364 else if (in_part->ch_block_write == -1)
1365 in_part->ch_block_write = 1;
1366
1367 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1368#ifndef WIN32
1369 {
1370# if defined(HAVE_SELECT)
1371 struct timeval tval;
1372 fd_set wfds;
1373 int ret;
1374
1375 FD_ZERO(&wfds);
1376 FD_SET((int)in_part->ch_fd, &wfds);
1377 tval.tv_sec = 0;
1378 tval.tv_usec = 0;
1379 for (;;)
1380 {
1381 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1382# ifdef EINTR
1383 SOCK_ERRNO;
1384 if (ret == -1 && errno == EINTR)
1385 continue;
1386# endif
1387 if (ret <= 0 || in_part->ch_block_write == 1)
1388 {
1389 if (ret > 0)
1390 ch_log(channel, "FAKED Input not ready for writing");
1391 else
1392 ch_log(channel, "Input not ready for writing");
1393 return FALSE;
1394 }
1395 break;
1396 }
1397# else
1398 struct pollfd fds;
1399
1400 fds.fd = in_part->ch_fd;
1401 fds.events = POLLOUT;
1402 if (poll(&fds, 1, 0) <= 0)
1403 {
1404 ch_log(channel, "Input not ready for writing");
1405 return FALSE;
1406 }
1407 if (in_part->ch_block_write == 1)
1408 {
1409 ch_log(channel, "FAKED Input not ready for writing");
1410 return FALSE;
1411 }
1412# endif
1413 }
1414#endif
1415 return TRUE;
1416}
1417
1418/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001419 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001420 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001421 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001422channel_write_in(channel_T *channel)
1423{
1424 chanpart_T *in_part = &channel->ch_part[PART_IN];
1425 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001426 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001427 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001428
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001429 if (buf == NULL || in_part->ch_buf_append)
1430 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001431 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001432 {
1433 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001434 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001435 return;
1436 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001437
1438 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1439 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1440 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001441 if (!can_write_buf_line(channel))
1442 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001443 write_buf_line(buf, lnum, channel);
1444 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001445 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001446
1447 if (written == 1)
1448 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1449 else if (written > 1)
1450 ch_logn(channel, "written %d lines to channel", written);
1451
Bram Moolenaar014069a2016-03-03 22:51:40 +01001452 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001453 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001454 {
1455 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001456 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001457 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001458
1459 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001460 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001461 }
1462 else
1463 ch_logn(channel, "Still %d more lines to write",
1464 buf->b_ml.ml_line_count - lnum + 1);
1465}
1466
1467/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001468 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001469 */
1470 void
1471channel_buffer_free(buf_T *buf)
1472{
1473 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001474 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001475
1476 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001477 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001478 {
1479 chanpart_T *ch_part = &channel->ch_part[part];
1480
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001481 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001482 {
1483 ch_logs(channel, "%s buffer has been wiped out",
1484 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001485 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001486 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001487 }
1488}
1489
1490/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001491 * Write any lines waiting to be written to a channel.
1492 */
1493 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001494channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001495{
1496 channel_T *channel;
1497
1498 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1499 {
1500 chanpart_T *in_part = &channel->ch_part[PART_IN];
1501
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001502 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001503 {
1504 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001505 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001506 else
1507 channel_write_in(channel);
1508 }
1509 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001510}
1511
1512/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001513 * Write appended lines above the last one in "buf" to the channel.
1514 */
1515 void
1516channel_write_new_lines(buf_T *buf)
1517{
1518 channel_T *channel;
1519 int found_one = FALSE;
1520
1521 /* There could be more than one channel for the buffer, loop over all of
1522 * them. */
1523 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1524 {
1525 chanpart_T *in_part = &channel->ch_part[PART_IN];
1526 linenr_T lnum;
1527 int written = 0;
1528
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001529 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530 {
1531 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001532 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001533 found_one = TRUE;
1534 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1535 ++lnum)
1536 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001537 if (!can_write_buf_line(channel))
1538 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001539 write_buf_line(buf, lnum, channel);
1540 ++written;
1541 }
1542
1543 if (written == 1)
1544 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1545 else if (written > 1)
1546 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001547 if (lnum < buf->b_ml.ml_line_count)
1548 ch_logn(channel, "Still %d more lines to write",
1549 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001550
1551 in_part->ch_buf_bot = lnum;
1552 }
1553 }
1554 if (!found_one)
1555 buf->b_write_to_channel = FALSE;
1556}
1557
1558/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001559 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001560 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001561 */
1562 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001563invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1564 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001565{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001566 typval_T rettv;
1567 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001568
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001569 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001570 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001571
Bram Moolenaar77073442016-02-13 23:23:53 +01001572 argv[0].v_type = VAR_CHANNEL;
1573 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001574
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001575 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1576 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001577 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001578 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001579}
1580
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001581/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001582 * Return the first node from "channel"/"part" without removing it.
1583 * Returns NULL if there is nothing.
1584 */
1585 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001586channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001587{
1588 readq_T *head = &channel->ch_part[part].ch_head;
1589
1590 return head->rq_next;
1591}
1592
1593/*
1594 * Return a pointer to the first NL in "node".
1595 * Skips over NUL characters.
1596 * Returns NULL if there is no NL.
1597 */
1598 char_u *
1599channel_first_nl(readq_T *node)
1600{
1601 char_u *buffer = node->rq_buffer;
1602 long_u i;
1603
1604 for (i = 0; i < node->rq_buflen; ++i)
1605 if (buffer[i] == NL)
1606 return buffer + i;
1607 return NULL;
1608}
1609
1610/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001612 * The caller must free it.
1613 * Returns NULL if there is nothing.
1614 */
1615 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001616channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001617{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001619 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001620 char_u *p;
1621
Bram Moolenaar77073442016-02-13 23:23:53 +01001622 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001623 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001625 p = node->rq_buffer;
1626 head->rq_next = node->rq_next;
1627 if (node->rq_next == NULL)
1628 head->rq_prev = NULL;
1629 else
1630 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001631 vim_free(node);
1632 return p;
1633}
1634
1635/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001636 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001637 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001638 */
1639 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001640channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001641{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001642 readq_T *head = &channel->ch_part[part].ch_head;
1643 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001644 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001645 char_u *res;
1646 char_u *p;
1647
1648 /* If there is only one buffer just get that one. */
1649 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1650 return channel_get(channel, part);
1651
1652 /* Concatenate everything into one buffer. */
1653 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001654 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001655 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001656 if (res == NULL)
1657 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001658 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001659 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001660 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001661 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001662 p += node->rq_buflen;
1663 }
1664 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001665
1666 /* Free all buffers */
1667 do
1668 {
1669 p = channel_get(channel, part);
1670 vim_free(p);
1671 } while (p != NULL);
1672
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001673 /* turn all NUL into NL */
1674 while (len > 0)
1675 {
1676 --len;
1677 if (res[len] == NUL)
1678 res[len] = NL;
1679 }
1680
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001681 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001682}
1683
1684/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001685 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001686 * Caller must check these bytes are available.
1687 */
1688 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001689channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001690{
1691 readq_T *head = &channel->ch_part[part].ch_head;
1692 readq_T *node = head->rq_next;
1693 char_u *buf = node->rq_buffer;
1694
1695 mch_memmove(buf, buf + len, node->rq_buflen - len);
1696 node->rq_buflen -= len;
1697}
1698
1699/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001700 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001702 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 */
1704 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001705channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001708 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 readq_T *last_node;
1710 readq_T *n;
1711 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001712 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001713 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001714
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001716 return FAIL;
1717
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001718 last_node = node->rq_next;
1719 len = node->rq_buflen + last_node->rq_buflen + 1;
1720 if (want_nl)
1721 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001722 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001723 {
1724 last_node = last_node->rq_next;
1725 len += last_node->rq_buflen;
1726 }
1727
1728 p = newbuf = alloc(len);
1729 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001730 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001731 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001732 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001733 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734 node->rq_buffer = newbuf;
1735 for (n = node; n != last_node; )
1736 {
1737 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001738 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001739 p += n->rq_buflen;
1740 vim_free(n->rq_buffer);
1741 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001742 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001743
1744 /* dispose of the collapsed nodes and their buffers */
1745 for (n = node->rq_next; n != last_node; )
1746 {
1747 n = n->rq_next;
1748 vim_free(n->rq_prev);
1749 }
1750 node->rq_next = last_node->rq_next;
1751 if (last_node->rq_next == NULL)
1752 head->rq_prev = node;
1753 else
1754 last_node->rq_next->rq_prev = node;
1755 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001756 return OK;
1757}
1758
1759/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001760 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001761 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001762 * Returns OK or FAIL.
1763 */
1764 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001765channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001766 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767{
1768 readq_T *node;
1769 readq_T *head = &channel->ch_part[part].ch_head;
1770 char_u *p;
1771 int i;
1772
1773 node = (readq_T *)alloc(sizeof(readq_T));
1774 if (node == NULL)
1775 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001776 /* A NUL is added at the end, because netbeans code expects that.
1777 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 node->rq_buffer = alloc(len + 1);
1779 if (node->rq_buffer == NULL)
1780 {
1781 vim_free(node);
1782 return FAIL; /* out of memory */
1783 }
1784
1785 if (channel->ch_part[part].ch_mode == MODE_NL)
1786 {
1787 /* Drop any CR before a NL. */
1788 p = node->rq_buffer;
1789 for (i = 0; i < len; ++i)
1790 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1791 *p++ = buf[i];
1792 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001793 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001794 }
1795 else
1796 {
1797 mch_memmove(node->rq_buffer, buf, len);
1798 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001799 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001800 }
1801
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001802 if (prepend)
1803 {
1804 /* preend node to the head of the queue */
1805 node->rq_next = head->rq_next;
1806 node->rq_prev = NULL;
1807 if (head->rq_next == NULL)
1808 head->rq_prev = node;
1809 else
1810 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001811 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001812 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001813 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001814 {
1815 /* append node to the tail of the queue */
1816 node->rq_next = NULL;
1817 node->rq_prev = head->rq_prev;
1818 if (head->rq_prev == NULL)
1819 head->rq_next = node;
1820 else
1821 head->rq_prev->rq_next = node;
1822 head->rq_prev = node;
1823 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001824
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001825 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001826 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001827 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001828 fprintf(log_fd, "'");
1829 if (fwrite(buf, len, 1, log_fd) != 1)
1830 return FAIL;
1831 fprintf(log_fd, "'\n");
1832 }
1833 return OK;
1834}
1835
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001836 static int
1837channel_fill(js_read_T *reader)
1838{
1839 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001840 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001841 char_u *next = channel_get(channel, part);
1842 int unused;
1843 int len;
1844 char_u *p;
1845
1846 if (next == NULL)
1847 return FALSE;
1848
1849 unused = reader->js_end - reader->js_buf - reader->js_used;
1850 if (unused > 0)
1851 {
1852 /* Prepend unused text. */
1853 len = (int)STRLEN(next);
1854 p = alloc(unused + len + 1);
1855 if (p == NULL)
1856 {
1857 vim_free(next);
1858 return FALSE;
1859 }
1860 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1861 mch_memmove(p + unused, next, len + 1);
1862 vim_free(next);
1863 next = p;
1864 }
1865
1866 vim_free(reader->js_buf);
1867 reader->js_buf = next;
1868 reader->js_used = 0;
1869 return TRUE;
1870}
1871
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001872/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001873 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001874 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001875 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001877 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001878channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001879{
1880 js_read_T reader;
1881 typval_T listtv;
1882 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001883 chanpart_T *chanpart = &channel->ch_part[part];
1884 jsonq_T *head = &chanpart->ch_json_head;
1885 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001886 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001887
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001888 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001889 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001890
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001891 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001893 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001894 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001895 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001896
1897 /* When a message is incomplete we wait for a short while for more to
1898 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001899 * or list will make us hang.
1900 * Do not generate error messages, they will be written in a channel log. */
1901 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001902 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001903 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001904 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001905 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001906 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001907 /* Only accept the response when it is a list with at least two
1908 * items. */
1909 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001910 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001911 if (listtv.v_type != VAR_LIST)
1912 ch_error(channel, "Did not receive a list, discarding");
1913 else
1914 ch_errorn(channel, "Expected list with two items, got %d",
1915 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001916 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001917 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001918 else
1919 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001920 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1921 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001922 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001923 else
1924 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001925 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001926 item->jq_value = alloc_tv();
1927 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001928 {
1929 vim_free(item);
1930 clear_tv(&listtv);
1931 }
1932 else
1933 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001934 *item->jq_value = listtv;
1935 item->jq_prev = head->jq_prev;
1936 head->jq_prev = item;
1937 item->jq_next = NULL;
1938 if (item->jq_prev == NULL)
1939 head->jq_next = item;
1940 else
1941 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001942 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001943 }
1944 }
1945 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001946
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001947 if (status == OK)
1948 chanpart->ch_waiting = FALSE;
1949 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001950 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001951 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001952 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001953 /* First time encountering incomplete message, set a deadline of
1954 * 100 msec. */
1955 ch_log(channel, "Incomplete message - wait for more");
1956 reader.js_used = 0;
1957 chanpart->ch_waiting = TRUE;
1958#ifdef WIN32
1959 chanpart->ch_deadline = GetTickCount() + 100L;
1960#else
1961 gettimeofday(&chanpart->ch_deadline, NULL);
1962 chanpart->ch_deadline.tv_usec += 100 * 1000;
1963 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1964 {
1965 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1966 ++chanpart->ch_deadline.tv_sec;
1967 }
1968#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001969 }
1970 else
1971 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001972 int timeout;
1973#ifdef WIN32
1974 timeout = GetTickCount() > chanpart->ch_deadline;
1975#else
1976 {
1977 struct timeval now_tv;
1978
1979 gettimeofday(&now_tv, NULL);
1980 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1981 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1982 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1983 }
1984#endif
1985 if (timeout)
1986 {
1987 status = FAIL;
1988 chanpart->ch_waiting = FALSE;
1989 }
1990 else
1991 {
1992 reader.js_used = 0;
1993 ch_log(channel, "still waiting on incomplete message");
1994 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001995 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001996 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001997
1998 if (status == FAIL)
1999 {
2000 ch_error(channel, "Decoding failed - discarding input");
2001 ret = FALSE;
2002 chanpart->ch_waiting = FALSE;
2003 }
2004 else if (reader.js_buf[reader.js_used] != NUL)
2005 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002006 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002007 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002008 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2009 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002010 ret = status == MAYBE ? FALSE: TRUE;
2011 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002012 else
2013 ret = FALSE;
2014
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002015 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002016 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002017}
2018
2019/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002020 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002021 */
2022 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002023remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002024{
Bram Moolenaar77073442016-02-13 23:23:53 +01002025 if (node->cq_prev == NULL)
2026 head->cq_next = node->cq_next;
2027 else
2028 node->cq_prev->cq_next = node->cq_next;
2029 if (node->cq_next == NULL)
2030 head->cq_prev = node->cq_prev;
2031 else
2032 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002033}
2034
2035/*
2036 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002037 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002038 */
2039 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002040remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041{
Bram Moolenaar77073442016-02-13 23:23:53 +01002042 if (node->jq_prev == NULL)
2043 head->jq_next = node->jq_next;
2044 else
2045 node->jq_prev->jq_next = node->jq_next;
2046 if (node->jq_next == NULL)
2047 head->jq_prev = node->jq_prev;
2048 else
2049 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002050 vim_free(node);
2051}
2052
2053/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002054 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002055 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002056 * When "id" is zero or negative jut get the first message. But not the one
2057 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002058 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002059 * Return OK when found and return the value in "rettv".
2060 * Return FAIL otherwise.
2061 */
2062 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002063channel_get_json(
2064 channel_T *channel,
2065 ch_part_T part,
2066 int id,
2067 int without_callback,
2068 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002069{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002070 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002071 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002072
Bram Moolenaar77073442016-02-13 23:23:53 +01002073 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002074 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002075 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002076 typval_T *tv = &l->lv_first->li_tv;
2077
Bram Moolenaar958dc692016-12-01 15:34:12 +01002078 if ((without_callback || !item->jq_no_callback)
2079 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002080 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002081 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002082 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002083 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002084 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002085 if (tv->v_type == VAR_NUMBER)
2086 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002087 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002088 return OK;
2089 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002090 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002091 }
2092 return FAIL;
2093}
2094
Bram Moolenaar958dc692016-12-01 15:34:12 +01002095/*
2096 * Put back "rettv" into the JSON queue, there was no callback for it.
2097 * Takes over the values in "rettv".
2098 */
2099 static void
2100channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2101{
2102 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2103 jsonq_T *item = head->jq_next;
2104 jsonq_T *newitem;
2105
2106 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2107 /* last item was pushed back, append to the end */
2108 item = NULL;
2109 else while (item != NULL && item->jq_no_callback)
2110 /* append after the last item that was pushed back */
2111 item = item->jq_next;
2112
2113 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2114 if (newitem == NULL)
2115 clear_tv(rettv);
2116 else
2117 {
2118 newitem->jq_value = alloc_tv();
2119 if (newitem->jq_value == NULL)
2120 {
2121 vim_free(newitem);
2122 clear_tv(rettv);
2123 }
2124 else
2125 {
2126 newitem->jq_no_callback = FALSE;
2127 *newitem->jq_value = *rettv;
2128 if (item == NULL)
2129 {
2130 /* append to the end */
2131 newitem->jq_prev = head->jq_prev;
2132 head->jq_prev = newitem;
2133 newitem->jq_next = NULL;
2134 if (newitem->jq_prev == NULL)
2135 head->jq_next = newitem;
2136 else
2137 newitem->jq_prev->jq_next = newitem;
2138 }
2139 else
2140 {
2141 /* append after "item" */
2142 newitem->jq_prev = item;
2143 newitem->jq_next = item->jq_next;
2144 item->jq_next = newitem;
2145 if (newitem->jq_next == NULL)
2146 head->jq_prev = newitem;
2147 else
2148 newitem->jq_next->jq_prev = newitem;
2149 }
2150 }
2151 }
2152}
2153
Bram Moolenaarece61b02016-02-20 21:39:05 +01002154#define CH_JSON_MAX_ARGS 4
2155
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002156/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002157 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002158 * "argv[0]" is the command string.
2159 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002160 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002161 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002162channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002163{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002164 char_u *cmd = argv[0].vval.v_string;
2165 char_u *arg;
2166 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002167
Bram Moolenaarece61b02016-02-20 21:39:05 +01002168 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002169 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002170 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002171 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002172 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002173 return;
2174 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002175 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002176 if (arg == NULL)
2177 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002178
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002179 if (STRCMP(cmd, "ex") == 0)
2180 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002181 int save_called_emsg = called_emsg;
2182
2183 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002184 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002185 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002186 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002187 --emsg_silent;
2188 if (called_emsg)
2189 ch_logs(channel, "Ex command error: '%s'",
2190 (char *)get_vim_var_str(VV_ERRMSG));
2191 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002192 }
2193 else if (STRCMP(cmd, "normal") == 0)
2194 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002195 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002196
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002197 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002198 ea.arg = arg;
2199 ea.addr_count = 0;
2200 ea.forceit = TRUE; /* no mapping */
2201 ex_normal(&ea);
2202 }
2203 else if (STRCMP(cmd, "redraw") == 0)
2204 {
2205 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002206
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002207 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002208 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002209 ex_redraw(&ea);
2210 showruler(FALSE);
2211 setcursor();
2212 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002213#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002214 if (gui.in_use)
2215 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002216 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002217 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002218 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002219#endif
2220 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002221 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002222 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002223 int is_call = cmd[0] == 'c';
2224 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002225
Bram Moolenaarece61b02016-02-20 21:39:05 +01002226 if (argv[id_idx].v_type != VAR_UNKNOWN
2227 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002228 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002229 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002230 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002231 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002232 }
2233 else if (is_call && argv[2].v_type != VAR_LIST)
2234 {
2235 ch_error(channel, "third argument for call must be a list");
2236 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002237 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002238 }
2239 else
2240 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002241 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002242 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002243 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002244 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002245
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002246 /* Don't pollute the display with errors. */
2247 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002248 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002249 {
2250 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002251 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002252 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002253 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002254 {
2255 ch_logs(channel, "Calling '%s'", (char *)arg);
2256 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2257 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002258 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002259
2260 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002261 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002262 int id = argv[id_idx].vval.v_number;
2263
Bram Moolenaar55fab432016-02-07 16:53:13 +01002264 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002265 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002266 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002267 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002268 /* If evaluation failed or the result can't be encoded
2269 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002270 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002271 err_tv.v_type = VAR_STRING;
2272 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002273 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002274 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002275 if (json != NULL)
2276 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 channel_send(channel,
2278 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002279 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002280 vim_free(json);
2281 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002282 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002283 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002284 if (tv == &res_tv)
2285 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002286 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002287 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002288 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002289 }
2290 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002291 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002292 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002293 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002294 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002295}
2296
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002297/*
2298 * Invoke the callback at "cbhead".
2299 * Does not redraw but sets channel_need_redraw.
2300 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002301 static void
2302invoke_one_time_callback(
2303 channel_T *channel,
2304 cbq_T *cbhead,
2305 cbq_T *item,
2306 typval_T *argv)
2307{
2308 ch_logs(channel, "Invoking one-time callback %s",
2309 (char *)item->cq_callback);
2310 /* Remove the item from the list first, if the callback
2311 * invokes ch_close() the list will be cleared. */
2312 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002313 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002314 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002315 vim_free(item);
2316}
2317
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002318 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002319append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002320{
2321 buf_T *save_curbuf = curbuf;
2322 linenr_T lnum = buffer->b_ml.ml_line_count;
2323 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002324 chanpart_T *ch_part = &channel->ch_part[part];
2325 int save_p_ma = buffer->b_p_ma;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002326 int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002327
2328 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2329 {
2330 if (!ch_part->ch_nomod_error)
2331 {
2332 ch_error(channel, "Buffer is not modifiable, cannot append");
2333 ch_part->ch_nomod_error = TRUE;
2334 }
2335 return;
2336 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002337
2338 /* If the buffer is also used as input insert above the last
2339 * line. Don't write these lines. */
2340 if (save_write_to)
2341 {
2342 --lnum;
2343 buffer->b_write_to_channel = FALSE;
2344 }
2345
2346 /* Append to the buffer */
2347 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2348
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002349 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002350 curbuf = buffer;
2351 u_sync(TRUE);
2352 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002353 ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002354
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002355 if (empty)
2356 {
2357 /* The buffer is empty, replace the first (dummy) line. */
2358 ml_replace(lnum, msg, TRUE);
2359 lnum = 0;
2360 }
2361 else
2362 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002363 appended_lines_mark(lnum, 1L);
2364 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002365 if (ch_part->ch_nomodifiable)
2366 buffer->b_p_ma = FALSE;
2367 else
2368 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002369
2370 if (buffer->b_nwindows > 0)
2371 {
2372 win_T *wp;
2373 win_T *save_curwin;
2374
2375 FOR_ALL_WINDOWS(wp)
2376 {
2377 if (wp->w_buffer == buffer
2378 && (save_write_to
2379 ? wp->w_cursor.lnum == lnum + 1
2380 : (wp->w_cursor.lnum == lnum
2381 && wp->w_cursor.col == 0)))
2382 {
2383 ++wp->w_cursor.lnum;
2384 save_curwin = curwin;
2385 curwin = wp;
2386 curbuf = curwin->w_buffer;
2387 scroll_cursor_bot(0, FALSE);
2388 curwin = save_curwin;
2389 curbuf = curwin->w_buffer;
2390 }
2391 }
2392 redraw_buf_later(buffer, VALID);
2393 channel_need_redraw = TRUE;
2394 }
2395
2396 if (save_write_to)
2397 {
2398 channel_T *ch;
2399
2400 /* Find channels reading from this buffer and adjust their
2401 * next-to-read line number. */
2402 buffer->b_write_to_channel = TRUE;
2403 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2404 {
2405 chanpart_T *in_part = &ch->ch_part[PART_IN];
2406
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002407 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002408 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2409 }
2410 }
2411}
2412
Bram Moolenaar437905c2016-04-26 19:01:05 +02002413 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002414drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002415{
2416 char_u *msg;
2417
2418 while ((msg = channel_get(channel, part)) != NULL)
2419 {
2420 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2421 vim_free(msg);
2422 }
2423}
2424
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002425/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002426 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002427 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002428 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002429 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002430 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002431may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002432{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002433 char_u *msg = NULL;
2434 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002435 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002436 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002437 chanpart_T *ch_part = &channel->ch_part[part];
2438 ch_mode_T ch_mode = ch_part->ch_mode;
2439 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002440 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002441 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002442 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002443 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002444 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002445
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002446 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002447 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002448 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002449
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002450 /* Use a message-specific callback, part callback or channel callback */
2451 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2452 if (cbitem->cq_seq_nr == 0)
2453 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002454 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002455 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002456 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002457 partial = cbitem->cq_partial;
2458 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002459 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002460 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002461 callback = ch_part->ch_callback;
2462 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002463 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002464 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002465 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002466 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002467 partial = channel->ch_partial;
2468 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002469
Bram Moolenaarec68a992016-10-03 21:37:41 +02002470 buffer = ch_part->ch_bufref.br_buf;
2471 if (buffer != NULL && !bufref_valid(&ch_part->ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002472 {
2473 /* buffer was wiped out */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002474 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002475 buffer = NULL;
2476 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002477
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002478 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002479 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002480 listitem_T *item;
2481 int argc = 0;
2482
Bram Moolenaard7ece102016-02-02 23:23:02 +01002483 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002484 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002485 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002486 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002487 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002488 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002489 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002490 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002491
Bram Moolenaarece61b02016-02-20 21:39:05 +01002492 for (item = listtv->vval.v_list->lv_first;
2493 item != NULL && argc < CH_JSON_MAX_ARGS;
2494 item = item->li_next)
2495 argv[argc++] = item->li_tv;
2496 while (argc < CH_JSON_MAX_ARGS)
2497 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002498
Bram Moolenaarece61b02016-02-20 21:39:05 +01002499 if (argv[0].v_type == VAR_STRING)
2500 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002501 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002502 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002503 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002504 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002505 }
2506
Bram Moolenaarece61b02016-02-20 21:39:05 +01002507 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002508 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002509 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002510 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002511 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002512 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002513 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002514 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002515 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002516 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002517 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002518 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002519 return FALSE;
2520 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002521 else
2522 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002523 /* If there is no callback or buffer drop the message. */
2524 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002525 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002526 /* If there is a close callback it may use ch_read() to get the
2527 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002528 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002529 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002530 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002531 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002532
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002533 if (ch_mode == MODE_NL)
2534 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002535 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002536 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002537 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002538
2539 /* See if we have a message ending in NL in the first buffer. If
2540 * not try to concatenate the first and the second buffer. */
2541 while (TRUE)
2542 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002543 node = channel_peek(channel, part);
2544 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002545 if (nl != NULL)
2546 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002547 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002548 {
2549 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2550 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002551 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002552 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002553 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002554 buf = node->rq_buffer;
2555
Bram Moolenaarec68a992016-10-03 21:37:41 +02002556 if (nl == NULL)
2557 {
2558 /* Flush remaining message that is missing a NL. */
2559 buf = vim_realloc(buf, node->rq_buflen + 1);
2560 if (buf == NULL)
2561 return FALSE;
2562 node->rq_buffer = buf;
2563 nl = buf + node->rq_buflen++;
2564 *nl = NUL;
2565 }
2566
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002567 /* Convert NUL to NL, the internal representation. */
2568 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2569 if (*p == NUL)
2570 *p = NL;
2571
2572 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002573 {
2574 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002575 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002576 *nl = NUL;
2577 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002578 else
2579 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002580 /* Copy the message into allocated memory (excluding the NL)
2581 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002582 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002583 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002584 }
2585 }
2586 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002587 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002588 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002589 * get everything we have.
2590 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002591 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002592 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002593
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002594 if (msg == NULL)
2595 return FALSE; /* out of memory (and avoids Coverity warning) */
2596
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002597 argv[1].v_type = VAR_STRING;
2598 argv[1].vval.v_string = msg;
2599 }
2600
Bram Moolenaara07fec92016-02-05 21:04:08 +01002601 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002602 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002603 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002604
Bram Moolenaar958dc692016-12-01 15:34:12 +01002605 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002606 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002607 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002608 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002609 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002610 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002611 break;
2612 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002613 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002614 {
2615 if (channel->ch_drop_never)
2616 {
2617 /* message must be read with ch_read() */
2618 channel_push_json(channel, part, listtv);
2619 listtv = NULL;
2620 }
2621 else
2622 ch_logn(channel, "Dropping message %d without callback",
2623 seq_nr);
2624 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002625 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002626 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002627 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002628 if (buffer != NULL)
2629 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002630 if (msg == NULL)
2631 /* JSON or JS mode: re-encode the message. */
2632 msg = json_encode(listtv, ch_mode);
2633 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002634 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002635 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002636
Bram Moolenaar187db502016-02-27 14:44:26 +01002637 if (callback != NULL)
2638 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002639 if (cbitem != NULL)
2640 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2641 else
2642 {
2643 /* invoke the channel callback */
2644 ch_logs(channel, "Invoking channel callback %s",
2645 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002646 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002647 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002648 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002649 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002650 else
Bram Moolenaar958dc692016-12-01 15:34:12 +01002651 ch_logn(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002652
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002653 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002654 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002655 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002656
2657 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002658}
2659
2660/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002661 * Return TRUE when channel "channel" is open for writing to.
2662 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002663 */
2664 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002665channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002666{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002667 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002668 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002669}
2670
2671/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002672 * Return TRUE when channel "channel" is open for reading or writing.
2673 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002674 */
2675 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002676channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002677{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002678 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002679 || channel->CH_IN_FD != INVALID_FD
2680 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002681 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002682}
2683
2684/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002685 * Return TRUE if "channel" has JSON or other typeahead.
2686 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002687 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002688channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002689{
2690 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2691
2692 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2693 {
2694 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2695 jsonq_T *item = head->jq_next;
2696
2697 return item != NULL;
2698 }
2699 return channel_peek(channel, part) != NULL;
2700}
2701
2702/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002703 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002704 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002705 */
2706 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002707channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002708{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002709 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002710 int has_readahead = FALSE;
2711
Bram Moolenaar77073442016-02-13 23:23:53 +01002712 if (channel == NULL)
2713 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002714 if (req_part == PART_OUT)
2715 {
2716 if (channel->CH_OUT_FD != INVALID_FD)
2717 return "open";
2718 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002719 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002720 }
2721 else if (req_part == PART_ERR)
2722 {
2723 if (channel->CH_ERR_FD != INVALID_FD)
2724 return "open";
2725 if (channel_has_readahead(channel, PART_ERR))
2726 has_readahead = TRUE;
2727 }
2728 else
2729 {
2730 if (channel_is_open(channel))
2731 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002732 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002733 if (channel_has_readahead(channel, part))
2734 {
2735 has_readahead = TRUE;
2736 break;
2737 }
2738 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002739
2740 if (has_readahead)
2741 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002742 return "closed";
2743}
2744
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002745 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002746channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002747{
2748 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002749 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002750 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002751 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002752 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002753
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002754 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002755 STRCAT(namebuf, "_");
2756 tail = STRLEN(namebuf);
2757
2758 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002759 if (chanpart->ch_fd != INVALID_FD)
2760 status = "open";
2761 else if (channel_has_readahead(channel, part))
2762 status = "buffered";
2763 else
2764 status = "closed";
2765 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002766
2767 STRCPY(namebuf + tail, "mode");
2768 switch (chanpart->ch_mode)
2769 {
2770 case MODE_NL: s = "NL"; break;
2771 case MODE_RAW: s = "RAW"; break;
2772 case MODE_JSON: s = "JSON"; break;
2773 case MODE_JS: s = "JS"; break;
2774 }
2775 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2776
2777 STRCPY(namebuf + tail, "io");
2778 if (part == PART_SOCK)
2779 s = "socket";
2780 else switch (chanpart->ch_io)
2781 {
2782 case JIO_NULL: s = "null"; break;
2783 case JIO_PIPE: s = "pipe"; break;
2784 case JIO_FILE: s = "file"; break;
2785 case JIO_BUFFER: s = "buffer"; break;
2786 case JIO_OUT: s = "out"; break;
2787 }
2788 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2789
2790 STRCPY(namebuf + tail, "timeout");
2791 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2792}
2793
2794 void
2795channel_info(channel_T *channel, dict_T *dict)
2796{
2797 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002798 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002799
2800 if (channel->ch_hostname != NULL)
2801 {
2802 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2803 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2804 channel_part_info(channel, dict, "sock", PART_SOCK);
2805 }
2806 else
2807 {
2808 channel_part_info(channel, dict, "out", PART_OUT);
2809 channel_part_info(channel, dict, "err", PART_ERR);
2810 channel_part_info(channel, dict, "in", PART_IN);
2811 }
2812}
2813
Bram Moolenaar77073442016-02-13 23:23:53 +01002814/*
2815 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002816 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002817 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002818 */
2819 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002820channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002821{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002822 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002823
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002824#ifdef FEAT_GUI
2825 channel_gui_unregister(channel);
2826#endif
2827
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002828 ch_close_part(channel, PART_SOCK);
2829 ch_close_part(channel, PART_IN);
2830 ch_close_part(channel, PART_OUT);
2831 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002832
Bram Moolenaar8b374212016-02-24 20:43:06 +01002833 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002834 {
2835 typval_T argv[1];
2836 typval_T rettv;
2837 int dummy;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002838 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002839
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002840 /* Invoke callbacks before the close callback, since it's weird to
2841 * first invoke the close callback. Increment the refcount to avoid
2842 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002843 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002844 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002845 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002846 while (may_invoke_callback(channel, part))
2847 ;
2848
2849 /* Invoke the close callback, if still set. */
2850 if (channel->ch_close_cb != NULL)
2851 {
2852 ch_logs(channel, "Invoking close callback %s",
2853 (char *)channel->ch_close_cb);
2854 argv[0].v_type = VAR_CHANNEL;
2855 argv[0].vval.v_channel = channel;
2856 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002857 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002858 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002859 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002860 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002861 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002862
2863 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002864 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002865 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002866 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002867
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002868 --channel->ch_refcount;
2869
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002870 if (channel_need_redraw)
2871 {
2872 channel_need_redraw = FALSE;
2873 redraw_after_callback();
2874 }
2875
Bram Moolenaar958dc692016-12-01 15:34:12 +01002876 if (!channel->ch_drop_never)
2877 /* any remaining messages are useless now */
2878 for (part = PART_SOCK; part < PART_IN; ++part)
2879 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002880 }
2881
2882 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002883}
2884
Bram Moolenaard04a0202016-01-26 23:30:18 +01002885/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002886 * Close the "in" part channel "channel".
2887 */
2888 void
2889channel_close_in(channel_T *channel)
2890{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002891 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002892}
2893
2894/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002895 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002896 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002897 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002898channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002899{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002900 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2901 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002902
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002903 while (channel_peek(channel, part) != NULL)
2904 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002905
2906 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002907 {
2908 cbq_T *node = cb_head->cq_next;
2909
2910 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002911 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002912 vim_free(node);
2913 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002914
2915 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002916 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002917 free_tv(json_head->jq_next->jq_value);
2918 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002919 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002920
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002921 free_callback(channel->ch_part[part].ch_callback,
2922 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002923 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002924 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002925}
2926
2927/*
2928 * Clear all the read buffers on "channel".
2929 */
2930 void
2931channel_clear(channel_T *channel)
2932{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002933 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002934 vim_free(channel->ch_hostname);
2935 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002936 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002937 channel_clear_one(channel, PART_OUT);
2938 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002939 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002940 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002941 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002942 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002943 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002944 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002945 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002946}
2947
Bram Moolenaar77073442016-02-13 23:23:53 +01002948#if defined(EXITFREE) || defined(PROTO)
2949 void
2950channel_free_all(void)
2951{
2952 channel_T *channel;
2953
Bram Moolenaard6051b52016-02-28 15:49:03 +01002954 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002955 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2956 channel_clear(channel);
2957}
2958#endif
2959
2960
Bram Moolenaar715d2852016-04-30 17:06:31 +02002961/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002962#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002963
2964/* Buffer size for reading incoming messages. */
2965#define MAXMSGSIZE 4096
2966
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002967#if defined(HAVE_SELECT)
2968/*
2969 * Add write fds where we are waiting for writing to be possible.
2970 */
2971 static int
2972channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2973{
2974 int maxfd = maxfd_arg;
2975 channel_T *ch;
2976
2977 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2978 {
2979 chanpart_T *in_part = &ch->ch_part[PART_IN];
2980
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002981 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002982 {
2983 FD_SET((int)in_part->ch_fd, wfds);
2984 if ((int)in_part->ch_fd >= maxfd)
2985 maxfd = (int)in_part->ch_fd + 1;
2986 }
2987 }
2988 return maxfd;
2989}
2990#else
2991/*
2992 * Add write fds where we are waiting for writing to be possible.
2993 */
2994 static int
2995channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2996{
2997 int nfd = nfd_in;
2998 channel_T *ch;
2999
3000 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3001 {
3002 chanpart_T *in_part = &ch->ch_part[PART_IN];
3003
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003004 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003005 {
3006 in_part->ch_poll_idx = nfd;
3007 fds[nfd].fd = in_part->ch_fd;
3008 fds[nfd].events = POLLOUT;
3009 ++nfd;
3010 }
3011 else
3012 in_part->ch_poll_idx = -1;
3013 }
3014 return nfd;
3015}
3016#endif
3017
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003018typedef enum {
3019 CW_READY,
3020 CW_NOT_READY,
3021 CW_ERROR
3022} channel_wait_result;
3023
Bram Moolenaard04a0202016-01-26 23:30:18 +01003024/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003025 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003026 * Return CW_READY when there is something to read.
3027 * Return CW_NOT_READY when there is nothing to read.
3028 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003029 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003030 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003031channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003032{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003033 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003034 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003035
Bram Moolenaard8070362016-02-15 21:56:54 +01003036# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003037 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003038 {
3039 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003040 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003041 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003042 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003043
3044 /* reading from a pipe, not a socket */
3045 while (TRUE)
3046 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003047 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3048
3049 if (r && nread > 0)
3050 return CW_READY;
3051 if (r == 0)
3052 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003053
3054 /* perhaps write some buffer lines */
3055 channel_write_any_lines();
3056
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003057 sleep_time = deadline - GetTickCount();
3058 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003059 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003060 /* Wait for a little while. Very short at first, up to 10 msec
3061 * after looping a few times. */
3062 if (sleep_time > delay)
3063 sleep_time = delay;
3064 Sleep(sleep_time);
3065 delay = delay * 2;
3066 if (delay > 10)
3067 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003068 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003069 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003070 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003071#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003072 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003073#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003074 struct timeval tval;
3075 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003076 fd_set wfds;
3077 int ret;
3078 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003079
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003080 tval.tv_sec = timeout / 1000;
3081 tval.tv_usec = (timeout % 1000) * 1000;
3082 for (;;)
3083 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003084 FD_ZERO(&rfds);
3085 FD_SET((int)fd, &rfds);
3086
3087 /* Write lines to a pipe when a pipe can be written to. Need to
3088 * set this every time, some buffers may be done. */
3089 maxfd = (int)fd + 1;
3090 FD_ZERO(&wfds);
3091 maxfd = channel_fill_wfds(maxfd, &wfds);
3092
3093 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003094# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003095 SOCK_ERRNO;
3096 if (ret == -1 && errno == EINTR)
3097 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003098# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003099 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003100 {
3101 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003102 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003103 channel_write_any_lines();
3104 continue;
3105 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003106 break;
3107 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003108#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003109 for (;;)
3110 {
3111 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3112 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003113
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003114 fds[0].fd = fd;
3115 fds[0].events = POLLIN;
3116 nfd = channel_fill_poll_write(nfd, fds);
3117 if (poll(fds, nfd, timeout) > 0)
3118 {
3119 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003120 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003121 channel_write_any_lines();
3122 continue;
3123 }
3124 break;
3125 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003126#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003127 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003128 return CW_NOT_READY;
3129}
3130
3131 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003132ch_close_part_on_error(
3133 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003134{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003135 char msgbuf[80];
3136
3137 vim_snprintf(msgbuf, sizeof(msgbuf),
3138 "%%s(): Read %s from ch_part[%d], closing",
3139 (is_err ? "error" : "EOF"), part);
3140
3141 if (is_err)
3142 /* Do not call emsg(), most likely the other end just exited. */
3143 ch_errors(channel, msgbuf, func);
3144 else
3145 ch_logs(channel, msgbuf, func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003146
3147 /* Queue a "DETACH" netbeans message in the command queue in order to
3148 * terminate the netbeans session later. Do not end the session here
3149 * directly as we may be running in the context of a call to
3150 * netbeans_parse_messages():
3151 * netbeans_parse_messages
3152 * -> autocmd triggered while processing the netbeans cmd
3153 * -> ui_breakcheck
3154 * -> gui event loop or select loop
3155 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003156 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003157 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003158 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003159 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003160 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3161
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003162 /* When reading is not possible close this part of the channel. Don't
3163 * close the channel yet, there may be something to read on another part. */
3164 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003165
3166#ifdef FEAT_GUI
3167 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003168 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003169#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003170}
3171
3172 static void
3173channel_close_now(channel_T *channel)
3174{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003175 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003176 if (channel->ch_nb_close_cb != NULL)
3177 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003178 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003179}
3180
3181/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003182 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003183 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003184 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003185 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003186 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003187channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003188{
3189 static char_u *buf = NULL;
3190 int len = 0;
3191 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003192 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003193 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003194
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003195 fd = channel->ch_part[part].ch_fd;
3196 if (fd == INVALID_FD)
3197 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003198 ch_errors(channel, "channel_read() called while %s part is closed",
3199 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003200 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003201 }
3202 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003203
3204 /* Allocate a buffer to read into. */
3205 if (buf == NULL)
3206 {
3207 buf = alloc(MAXMSGSIZE);
3208 if (buf == NULL)
3209 return; /* out of memory! */
3210 }
3211
3212 /* Keep on reading for as long as there is something to read.
3213 * Use select() or poll() to avoid blocking on a message that is exactly
3214 * MAXMSGSIZE long. */
3215 for (;;)
3216 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003217 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003218 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003219 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003220 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003221 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003222 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003223 if (len <= 0)
3224 break; /* error or nothing more to read */
3225
3226 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003227 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003228 readlen += len;
3229 if (len < MAXMSGSIZE)
3230 break; /* did read everything that's available */
3231 }
3232
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003233 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003234 if (readlen <= 0)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003235 ch_close_part_on_error(channel, part, (len < 0), func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236
3237#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003238 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003239 if (CH_HAS_GUI && gtk_main_level() > 0)
3240 gtk_main_quit();
3241#endif
3242}
3243
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003244/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003245 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003246 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003247 * Returns what was read in allocated memory.
3248 * Returns NULL in case of error or timeout.
3249 */
3250 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003251channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003252{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003253 char_u *buf;
3254 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003255 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003256 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003257 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003258 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003259
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003260 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003261 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003262
3263 while (TRUE)
3264 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003265 node = channel_peek(channel, part);
3266 if (node != NULL)
3267 {
3268 if (mode == MODE_RAW || (mode == MODE_NL
3269 && channel_first_nl(node) != NULL))
3270 /* got a complete message */
3271 break;
3272 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3273 continue;
3274 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003275
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003276 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003277 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003278 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003279 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003280 {
3281 ch_log(channel, "Timed out");
3282 return NULL;
3283 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003284 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003285 }
3286
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003287 if (mode == MODE_RAW)
3288 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003289 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003290 }
3291 else
3292 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003293 char_u *p;
3294
3295 buf = node->rq_buffer;
3296 nl = channel_first_nl(node);
3297
3298 /* Convert NUL to NL, the internal representation. */
3299 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3300 if (*p == NUL)
3301 *p = NL;
3302
3303 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003304 {
3305 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003306 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003307 *nl = NUL;
3308 }
3309 else
3310 {
3311 /* Copy the message into allocated memory and remove it from the
3312 * buffer. */
3313 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003314 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003315 }
3316 }
3317 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003318 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003319 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003320}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003321
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003322/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003323 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003324 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003325 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003326 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003327 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003328 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003329channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003330 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003331 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003332 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003333 int id,
3334 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003335{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003336 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003337 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003338 int timeout;
3339 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003340
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003341 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003342 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003343 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003344 for (;;)
3345 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003346 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003347
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003348 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003349 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003350 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003351 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003352 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003353 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003354
Bram Moolenaard7ece102016-02-02 23:23:02 +01003355 if (!more)
3356 {
3357 /* Handle any other messages in the queue. If done some more
3358 * messages may have arrived. */
3359 if (channel_parse_messages())
3360 continue;
3361
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003362 /* Wait for up to the timeout. If there was an incomplete message
3363 * use the deadline for that. */
3364 timeout = timeout_arg;
3365 if (chanpart->ch_waiting)
3366 {
3367#ifdef WIN32
3368 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3369#else
3370 {
3371 struct timeval now_tv;
3372
3373 gettimeofday(&now_tv, NULL);
3374 timeout = (chanpart->ch_deadline.tv_sec
3375 - now_tv.tv_sec) * 1000
3376 + (chanpart->ch_deadline.tv_usec
3377 - now_tv.tv_usec) / 1000
3378 + 1;
3379 }
3380#endif
3381 if (timeout < 0)
3382 {
3383 /* Something went wrong, channel_parse_json() didn't
3384 * discard message. Cancel waiting. */
3385 chanpart->ch_waiting = FALSE;
3386 timeout = timeout_arg;
3387 }
3388 else if (timeout > timeout_arg)
3389 timeout = timeout_arg;
3390 }
3391 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003392 if (fd == INVALID_FD
3393 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003394 {
3395 if (timeout == timeout_arg)
3396 {
3397 if (fd != INVALID_FD)
3398 ch_log(channel, "Timed out");
3399 break;
3400 }
3401 }
3402 else
3403 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003404 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003405 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003406 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003407 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003408}
3409
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003410/*
3411 * Common for ch_read() and ch_readraw().
3412 */
3413 void
3414common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3415{
3416 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003417 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003418 jobopt_T opt;
3419 int mode;
3420 int timeout;
3421 int id = -1;
3422 typval_T *listtv = NULL;
3423
3424 /* return an empty string by default */
3425 rettv->v_type = VAR_STRING;
3426 rettv->vval.v_string = NULL;
3427
3428 clear_job_options(&opt);
3429 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3430 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003431 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003432
Bram Moolenaar437905c2016-04-26 19:01:05 +02003433 if (opt.jo_set & JO_PART)
3434 part = opt.jo_part;
3435 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003436 if (channel != NULL)
3437 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003438 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003439 part = channel_part_read(channel);
3440 mode = channel_get_mode(channel, part);
3441 timeout = channel_get_timeout(channel, part);
3442 if (opt.jo_set & JO_TIMEOUT)
3443 timeout = opt.jo_timeout;
3444
3445 if (raw || mode == MODE_RAW || mode == MODE_NL)
3446 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3447 else
3448 {
3449 if (opt.jo_set & JO_ID)
3450 id = opt.jo_id;
3451 channel_read_json_block(channel, part, timeout, id, &listtv);
3452 if (listtv != NULL)
3453 {
3454 *rettv = *listtv;
3455 vim_free(listtv);
3456 }
3457 else
3458 {
3459 rettv->v_type = VAR_SPECIAL;
3460 rettv->vval.v_number = VVAL_NONE;
3461 }
3462 }
3463 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003464
3465theend:
3466 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003467}
3468
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003469# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3470 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003471/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003472 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003473 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003474 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003475 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003476channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003477{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003478 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003479 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003480
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003481 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003482 for (channel = first_channel; channel != NULL;
3483 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003484 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003485 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003486 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003487 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003488 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003489 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003490 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003491 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003492 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003493}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003494# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003495
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003496# if defined(WIN32) || defined(PROTO)
3497/*
3498 * Check the channels for anything that is ready to be read.
3499 * The data is put in the read queue.
3500 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003501 void
3502channel_handle_events(void)
3503{
3504 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003505 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003506 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003507
3508 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3509 {
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003510 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003511 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003512 {
3513 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003514 if (fd != INVALID_FD)
3515 {
3516 int r = channel_wait(channel, fd, 0);
3517
3518 if (r == CW_READY)
3519 channel_read(channel, part, "channel_handle_events");
3520 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003521 ch_close_part_on_error(channel, part, TRUE,
3522 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003523 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003524 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003525 }
3526}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003527# endif
3528
Bram Moolenaard04a0202016-01-26 23:30:18 +01003529/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003530 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003531 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003532 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003533 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003534 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003535channel_send(
3536 channel_T *channel,
3537 ch_part_T part,
3538 char_u *buf,
3539 int len,
3540 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003541{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003542 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003543 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003544
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003545 fd = channel->ch_part[part].ch_fd;
3546 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003547 {
3548 if (!channel->ch_error && fun != NULL)
3549 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003550 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003551 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003552 }
3553 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003554 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003555 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003556
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003557 if (log_fd != NULL)
3558 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003559 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003560 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003561 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003562 fprintf(log_fd, "'\n");
3563 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003564 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003565 }
3566
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003567 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003568 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003569 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003570 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003571 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003572 {
3573 if (!channel->ch_error && fun != NULL)
3574 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003575 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003576 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003577 }
3578 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003579 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003580 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003581
3582 channel->ch_error = FALSE;
3583 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003584}
3585
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003586/*
3587 * Common for "ch_sendexpr()" and "ch_sendraw()".
3588 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003589 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003590 * Otherwise returns NULL.
3591 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003592 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003593send_common(
3594 typval_T *argvars,
3595 char_u *text,
3596 int id,
3597 int eval,
3598 jobopt_T *opt,
3599 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003600 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003601{
3602 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003603 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003604
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003605 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003606 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003607 if (channel == NULL)
3608 return NULL;
3609 part_send = channel_part_send(channel);
3610 *part_read = channel_part_read(channel);
3611
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003612 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3613 return NULL;
3614
3615 /* Set the callback. An empty callback means no callback and not reading
3616 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3617 * allowed. */
3618 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3619 {
3620 if (eval)
3621 {
3622 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3623 return NULL;
3624 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003625 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003626 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003627 }
3628
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003629 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003630 && opt->jo_callback == NULL)
3631 return channel;
3632 return NULL;
3633}
3634
3635/*
3636 * common for "ch_evalexpr()" and "ch_sendexpr()"
3637 */
3638 void
3639ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3640{
3641 char_u *text;
3642 typval_T *listtv;
3643 channel_T *channel;
3644 int id;
3645 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003646 ch_part_T part_send;
3647 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003648 jobopt_T opt;
3649 int timeout;
3650
3651 /* return an empty string by default */
3652 rettv->v_type = VAR_STRING;
3653 rettv->vval.v_string = NULL;
3654
Bram Moolenaar437905c2016-04-26 19:01:05 +02003655 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003656 if (channel == NULL)
3657 return;
3658 part_send = channel_part_send(channel);
3659
3660 ch_mode = channel_get_mode(channel, part_send);
3661 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3662 {
3663 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3664 return;
3665 }
3666
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003667 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003668 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003669 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003670 if (text == NULL)
3671 return;
3672
3673 channel = send_common(argvars, text, id, eval, &opt,
3674 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3675 vim_free(text);
3676 if (channel != NULL && eval)
3677 {
3678 if (opt.jo_set & JO_TIMEOUT)
3679 timeout = opt.jo_timeout;
3680 else
3681 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003682 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3683 == OK)
3684 {
3685 list_T *list = listtv->vval.v_list;
3686
3687 /* Move the item from the list and then change the type to
3688 * avoid the value being freed. */
3689 *rettv = list->lv_last->li_tv;
3690 list->lv_last->li_tv.v_type = VAR_NUMBER;
3691 free_tv(listtv);
3692 }
3693 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003694 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003695}
3696
3697/*
3698 * common for "ch_evalraw()" and "ch_sendraw()"
3699 */
3700 void
3701ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3702{
3703 char_u buf[NUMBUFLEN];
3704 char_u *text;
3705 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003706 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003707 jobopt_T opt;
3708 int timeout;
3709
3710 /* return an empty string by default */
3711 rettv->v_type = VAR_STRING;
3712 rettv->vval.v_string = NULL;
3713
3714 text = get_tv_string_buf(&argvars[1], buf);
3715 channel = send_common(argvars, text, 0, eval, &opt,
3716 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3717 if (channel != NULL && eval)
3718 {
3719 if (opt.jo_set & JO_TIMEOUT)
3720 timeout = opt.jo_timeout;
3721 else
3722 timeout = channel_get_timeout(channel, part_read);
3723 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3724 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003725 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003726}
3727
Bram Moolenaard04a0202016-01-26 23:30:18 +01003728# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003729/*
3730 * Add open channels to the poll struct.
3731 * Return the adjusted struct index.
3732 * The type of "fds" is hidden to avoid problems with the function proto.
3733 */
3734 int
3735channel_poll_setup(int nfd_in, void *fds_in)
3736{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003737 int nfd = nfd_in;
3738 channel_T *channel;
3739 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003740 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003741
Bram Moolenaar77073442016-02-13 23:23:53 +01003742 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003743 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003744 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003745 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003746 chanpart_T *ch_part = &channel->ch_part[part];
3747
3748 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003749 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003750 ch_part->ch_poll_idx = nfd;
3751 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003752 fds[nfd].events = POLLIN;
3753 nfd++;
3754 }
3755 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003756 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003757 }
3758 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003759
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003760 nfd = channel_fill_poll_write(nfd, fds);
3761
Bram Moolenaare0874f82016-01-24 20:36:41 +01003762 return nfd;
3763}
3764
3765/*
3766 * The type of "fds" is hidden to avoid problems with the function proto.
3767 */
3768 int
3769channel_poll_check(int ret_in, void *fds_in)
3770{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003771 int ret = ret_in;
3772 channel_T *channel;
3773 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003774 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003775 int idx;
3776 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003777
Bram Moolenaar77073442016-02-13 23:23:53 +01003778 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003779 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003780 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003781 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003782 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003783
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003784 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003785 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003786 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003787 --ret;
3788 }
3789 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003790
3791 in_part = &channel->ch_part[PART_IN];
3792 idx = in_part->ch_poll_idx;
3793 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3794 {
3795 if (in_part->ch_buf_append)
3796 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003797 if (in_part->ch_bufref.br_buf != NULL)
3798 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003799 }
3800 else
3801 channel_write_in(channel);
3802 --ret;
3803 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003804 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003805
3806 return ret;
3807}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003808# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003809
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003810# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003811/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003812 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003813 */
3814 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003815channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003816{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003817 int maxfd = maxfd_in;
3818 channel_T *channel;
3819 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003820 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003821 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003822
Bram Moolenaar77073442016-02-13 23:23:53 +01003823 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003824 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003825 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003826 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003827 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003828
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003829 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003830 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003831 FD_SET((int)fd, rfds);
3832 if (maxfd < (int)fd)
3833 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003834 }
3835 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003836 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003837
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003838 maxfd = channel_fill_wfds(maxfd, wfds);
3839
Bram Moolenaare0874f82016-01-24 20:36:41 +01003840 return maxfd;
3841}
3842
3843/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003844 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003845 */
3846 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003847channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003848{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003849 int ret = ret_in;
3850 channel_T *channel;
3851 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003852 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003853 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003854 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003855
Bram Moolenaar77073442016-02-13 23:23:53 +01003856 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003857 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003858 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003859 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003860 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003861
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003862 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003863 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003864 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003865 --ret;
3866 }
3867 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003868
3869 in_part = &channel->ch_part[PART_IN];
3870 if (ret > 0 && in_part->ch_fd != INVALID_FD
3871 && FD_ISSET(in_part->ch_fd, wfds))
3872 {
3873 if (in_part->ch_buf_append)
3874 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003875 if (in_part->ch_bufref.br_buf != NULL)
3876 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003877 }
3878 else
3879 channel_write_in(channel);
3880 --ret;
3881 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003882 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003883
3884 return ret;
3885}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003886# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003887
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003888/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003889 * Execute queued up commands.
3890 * Invoked from the main loop when it's safe to execute received commands.
3891 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003892 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003893 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003894channel_parse_messages(void)
3895{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003896 channel_T *channel = first_channel;
3897 int ret = FALSE;
3898 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003899 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003900#ifdef ELAPSED_FUNC
3901 ELAPSED_TYPE start_tv;
3902
3903 ELAPSED_INIT(start_tv);
3904#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003905
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003906 ++safe_to_invoke_callback;
3907
Bram Moolenaard0b65022016-03-06 21:50:33 +01003908 /* Only do this message when another message was given, otherwise we get
3909 * lots of them. */
3910 if (did_log_msg)
3911 {
3912 ch_log(NULL, "looking for messages on channels");
3913 did_log_msg = FALSE;
3914 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003915 while (channel != NULL)
3916 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003917 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003918 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003919 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003920 channel_close_now(channel);
3921 /* channel may have been freed, start over */
3922 channel = first_channel;
3923 continue;
3924 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003925 if (channel->ch_to_be_freed)
3926 {
3927 channel_free(channel);
3928 /* channel has been freed, start over */
3929 channel = first_channel;
3930 continue;
3931 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003932 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003933 {
3934 /* channel is no longer useful, free it */
3935 channel_free(channel);
3936 channel = first_channel;
3937 part = PART_SOCK;
3938 continue;
3939 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003940 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003941 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003942 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003943 /* Increase the refcount, in case the handler causes the channel
3944 * to be unreferenced or closed. */
3945 ++channel->ch_refcount;
3946 r = may_invoke_callback(channel, part);
3947 if (r == OK)
3948 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01003949 if (channel_unref(channel) || (r == OK
3950#ifdef ELAPSED_FUNC
3951 /* Limit the time we loop here to 100 msec, otherwise
3952 * Vim becomes unresponsive when the callback takes
3953 * more than a bit of time. */
3954 && ELAPSED_FUNC(start_tv) < 100L
3955#endif
3956 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003957 {
3958 /* channel was freed or something was done, start over */
3959 channel = first_channel;
3960 part = PART_SOCK;
3961 continue;
3962 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003963 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003964 if (part < PART_ERR)
3965 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003966 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003967 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003968 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003969 part = PART_SOCK;
3970 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003971 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003972
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003973 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003974 {
3975 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003976 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003977 }
3978
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003979 --safe_to_invoke_callback;
3980
Bram Moolenaard7ece102016-02-02 23:23:02 +01003981 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003982}
3983
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003984/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01003985 * Return TRUE if any channel has readahead. That means we should not block on
3986 * waiting for input.
3987 */
3988 int
3989channel_any_readahead(void)
3990{
3991 channel_T *channel = first_channel;
3992 ch_part_T part = PART_SOCK;
3993
3994 while (channel != NULL)
3995 {
3996 if (channel_has_readahead(channel, part))
3997 return TRUE;
3998 if (part < PART_ERR)
3999 ++part;
4000 else
4001 {
4002 channel = channel->ch_next;
4003 part = PART_SOCK;
4004 }
4005 }
4006 return FALSE;
4007}
4008
4009/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004010 * Mark references to lists used in channels.
4011 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004012 int
4013set_ref_in_channel(int copyID)
4014{
Bram Moolenaar77073442016-02-13 23:23:53 +01004015 int abort = FALSE;
4016 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004017 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004018
Bram Moolenaar77073442016-02-13 23:23:53 +01004019 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004020 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004021 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004022 tv.v_type = VAR_CHANNEL;
4023 tv.vval.v_channel = channel;
4024 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004025 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004026 return abort;
4027}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004028
4029/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004030 * Return the "part" to write to for "channel".
4031 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004032 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004033channel_part_send(channel_T *channel)
4034{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004035 if (channel->CH_SOCK_FD == INVALID_FD)
4036 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004037 return PART_SOCK;
4038}
4039
4040/*
4041 * Return the default "part" to read from for "channel".
4042 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004043 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004044channel_part_read(channel_T *channel)
4045{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004046 if (channel->CH_SOCK_FD == INVALID_FD)
4047 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004048 return PART_SOCK;
4049}
4050
4051/*
4052 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004053 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004054 */
4055 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004056channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004057{
Bram Moolenaar77073442016-02-13 23:23:53 +01004058 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004059 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004060 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004061}
4062
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004063/*
4064 * Return the timeout of "channel"/"part"
4065 */
4066 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004067channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004068{
4069 return channel->ch_part[part].ch_timeout;
4070}
4071
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004072 static int
4073handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4074{
4075 char_u *val = get_tv_string(item);
4076
4077 opt->jo_set |= jo;
4078 if (STRCMP(val, "nl") == 0)
4079 *modep = MODE_NL;
4080 else if (STRCMP(val, "raw") == 0)
4081 *modep = MODE_RAW;
4082 else if (STRCMP(val, "js") == 0)
4083 *modep = MODE_JS;
4084 else if (STRCMP(val, "json") == 0)
4085 *modep = MODE_JSON;
4086 else
4087 {
4088 EMSG2(_(e_invarg2), val);
4089 return FAIL;
4090 }
4091 return OK;
4092}
4093
4094 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004095handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004096{
4097 char_u *val = get_tv_string(item);
4098
4099 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4100 if (STRCMP(val, "null") == 0)
4101 opt->jo_io[part] = JIO_NULL;
4102 else if (STRCMP(val, "pipe") == 0)
4103 opt->jo_io[part] = JIO_PIPE;
4104 else if (STRCMP(val, "file") == 0)
4105 opt->jo_io[part] = JIO_FILE;
4106 else if (STRCMP(val, "buffer") == 0)
4107 opt->jo_io[part] = JIO_BUFFER;
4108 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4109 opt->jo_io[part] = JIO_OUT;
4110 else
4111 {
4112 EMSG2(_(e_invarg2), val);
4113 return FAIL;
4114 }
4115 return OK;
4116}
4117
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004118/*
4119 * Clear a jobopt_T before using it.
4120 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004121 void
4122clear_job_options(jobopt_T *opt)
4123{
4124 vim_memset(opt, 0, sizeof(jobopt_T));
4125}
4126
4127/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004128 * Free any members of a jobopt_T.
4129 */
4130 void
4131free_job_options(jobopt_T *opt)
4132{
4133 if (opt->jo_partial != NULL)
4134 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004135 else if (opt->jo_callback != NULL)
4136 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004137 if (opt->jo_out_partial != NULL)
4138 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004139 else if (opt->jo_out_cb != NULL)
4140 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004141 if (opt->jo_err_partial != NULL)
4142 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004143 else if (opt->jo_err_cb != NULL)
4144 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004145 if (opt->jo_close_partial != NULL)
4146 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004147 else if (opt->jo_close_cb != NULL)
4148 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004149 if (opt->jo_exit_partial != NULL)
4150 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004151 else if (opt->jo_exit_cb != NULL)
4152 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004153}
4154
4155/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004156 * Get the PART_ number from the first character of an option name.
4157 */
4158 static int
4159part_from_char(int c)
4160{
4161 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4162}
4163
4164/*
4165 * Get the option entries from the dict in "tv", parse them and put the result
4166 * in "opt".
4167 * Only accept options in "supported".
4168 * If an option value is invalid return FAIL.
4169 */
4170 int
4171get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4172{
4173 typval_T *item;
4174 char_u *val;
4175 dict_T *dict;
4176 int todo;
4177 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004178 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004179
4180 opt->jo_set = 0;
4181 if (tv->v_type == VAR_UNKNOWN)
4182 return OK;
4183 if (tv->v_type != VAR_DICT)
4184 {
4185 EMSG(_(e_invarg));
4186 return FAIL;
4187 }
4188 dict = tv->vval.v_dict;
4189 if (dict == NULL)
4190 return OK;
4191
4192 todo = (int)dict->dv_hashtab.ht_used;
4193 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4194 if (!HASHITEM_EMPTY(hi))
4195 {
4196 item = &dict_lookup(hi)->di_tv;
4197
4198 if (STRCMP(hi->hi_key, "mode") == 0)
4199 {
4200 if (!(supported & JO_MODE))
4201 break;
4202 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4203 return FAIL;
4204 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004205 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004206 {
4207 if (!(supported & JO_IN_MODE))
4208 break;
4209 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4210 == FAIL)
4211 return FAIL;
4212 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004213 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004214 {
4215 if (!(supported & JO_OUT_MODE))
4216 break;
4217 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4218 == FAIL)
4219 return FAIL;
4220 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004221 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004222 {
4223 if (!(supported & JO_ERR_MODE))
4224 break;
4225 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4226 == FAIL)
4227 return FAIL;
4228 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004229 else if (STRCMP(hi->hi_key, "in_io") == 0
4230 || STRCMP(hi->hi_key, "out_io") == 0
4231 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004232 {
4233 if (!(supported & JO_OUT_IO))
4234 break;
4235 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4236 return FAIL;
4237 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004238 else if (STRCMP(hi->hi_key, "in_name") == 0
4239 || STRCMP(hi->hi_key, "out_name") == 0
4240 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004241 {
4242 part = part_from_char(*hi->hi_key);
4243
4244 if (!(supported & JO_OUT_IO))
4245 break;
4246 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4247 opt->jo_io_name[part] =
4248 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4249 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004250 else if (STRCMP(hi->hi_key, "in_buf") == 0
4251 || STRCMP(hi->hi_key, "out_buf") == 0
4252 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004253 {
4254 part = part_from_char(*hi->hi_key);
4255
4256 if (!(supported & JO_OUT_IO))
4257 break;
4258 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4259 opt->jo_io_buf[part] = get_tv_number(item);
4260 if (opt->jo_io_buf[part] <= 0)
4261 {
4262 EMSG2(_(e_invarg2), get_tv_string(item));
4263 return FAIL;
4264 }
4265 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4266 {
4267 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4268 return FAIL;
4269 }
4270 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004271 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4272 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4273 {
4274 part = part_from_char(*hi->hi_key);
4275
4276 if (!(supported & JO_OUT_IO))
4277 break;
4278 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4279 opt->jo_modifiable[part] = get_tv_number(item);
4280 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004281 else if (STRCMP(hi->hi_key, "out_msg") == 0
4282 || STRCMP(hi->hi_key, "err_msg") == 0)
4283 {
4284 part = part_from_char(*hi->hi_key);
4285
4286 if (!(supported & JO_OUT_IO))
4287 break;
4288 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4289 opt->jo_message[part] = get_tv_number(item);
4290 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004291 else if (STRCMP(hi->hi_key, "in_top") == 0
4292 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004293 {
4294 linenr_T *lp;
4295
4296 if (!(supported & JO_OUT_IO))
4297 break;
4298 if (hi->hi_key[3] == 't')
4299 {
4300 lp = &opt->jo_in_top;
4301 opt->jo_set |= JO_IN_TOP;
4302 }
4303 else
4304 {
4305 lp = &opt->jo_in_bot;
4306 opt->jo_set |= JO_IN_BOT;
4307 }
4308 *lp = get_tv_number(item);
4309 if (*lp < 0)
4310 {
4311 EMSG2(_(e_invarg2), get_tv_string(item));
4312 return FAIL;
4313 }
4314 }
4315 else if (STRCMP(hi->hi_key, "channel") == 0)
4316 {
4317 if (!(supported & JO_OUT_IO))
4318 break;
4319 opt->jo_set |= JO_CHANNEL;
4320 if (item->v_type != VAR_CHANNEL)
4321 {
4322 EMSG2(_(e_invarg2), "channel");
4323 return FAIL;
4324 }
4325 opt->jo_channel = item->vval.v_channel;
4326 }
4327 else if (STRCMP(hi->hi_key, "callback") == 0)
4328 {
4329 if (!(supported & JO_CALLBACK))
4330 break;
4331 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004332 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004333 if (opt->jo_callback == NULL)
4334 {
4335 EMSG2(_(e_invarg2), "callback");
4336 return FAIL;
4337 }
4338 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004339 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004340 {
4341 if (!(supported & JO_OUT_CALLBACK))
4342 break;
4343 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004344 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004345 if (opt->jo_out_cb == NULL)
4346 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004347 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004348 return FAIL;
4349 }
4350 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004351 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004352 {
4353 if (!(supported & JO_ERR_CALLBACK))
4354 break;
4355 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004356 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004357 if (opt->jo_err_cb == NULL)
4358 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004359 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004360 return FAIL;
4361 }
4362 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004363 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004364 {
4365 if (!(supported & JO_CLOSE_CALLBACK))
4366 break;
4367 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004368 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004369 if (opt->jo_close_cb == NULL)
4370 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004371 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004372 return FAIL;
4373 }
4374 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004375 else if (STRCMP(hi->hi_key, "drop") == 0)
4376 {
4377 int never = FALSE;
4378 val = get_tv_string(item);
4379
4380 if (STRCMP(val, "never") == 0)
4381 never = TRUE;
4382 else if (STRCMP(val, "auto") != 0)
4383 {
4384 EMSG2(_(e_invarg2), "drop");
4385 return FAIL;
4386 }
4387 opt->jo_drop_never = never;
4388 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004389 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4390 {
4391 if (!(supported & JO_EXIT_CB))
4392 break;
4393 opt->jo_set |= JO_EXIT_CB;
4394 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4395 if (opt->jo_exit_cb == NULL)
4396 {
4397 EMSG2(_(e_invarg2), "exit_cb");
4398 return FAIL;
4399 }
4400 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004401 else if (STRCMP(hi->hi_key, "waittime") == 0)
4402 {
4403 if (!(supported & JO_WAITTIME))
4404 break;
4405 opt->jo_set |= JO_WAITTIME;
4406 opt->jo_waittime = get_tv_number(item);
4407 }
4408 else if (STRCMP(hi->hi_key, "timeout") == 0)
4409 {
4410 if (!(supported & JO_TIMEOUT))
4411 break;
4412 opt->jo_set |= JO_TIMEOUT;
4413 opt->jo_timeout = get_tv_number(item);
4414 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004415 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004416 {
4417 if (!(supported & JO_OUT_TIMEOUT))
4418 break;
4419 opt->jo_set |= JO_OUT_TIMEOUT;
4420 opt->jo_out_timeout = get_tv_number(item);
4421 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004422 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004423 {
4424 if (!(supported & JO_ERR_TIMEOUT))
4425 break;
4426 opt->jo_set |= JO_ERR_TIMEOUT;
4427 opt->jo_err_timeout = get_tv_number(item);
4428 }
4429 else if (STRCMP(hi->hi_key, "part") == 0)
4430 {
4431 if (!(supported & JO_PART))
4432 break;
4433 opt->jo_set |= JO_PART;
4434 val = get_tv_string(item);
4435 if (STRCMP(val, "err") == 0)
4436 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004437 else if (STRCMP(val, "out") == 0)
4438 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004439 else
4440 {
4441 EMSG2(_(e_invarg2), val);
4442 return FAIL;
4443 }
4444 }
4445 else if (STRCMP(hi->hi_key, "id") == 0)
4446 {
4447 if (!(supported & JO_ID))
4448 break;
4449 opt->jo_set |= JO_ID;
4450 opt->jo_id = get_tv_number(item);
4451 }
4452 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4453 {
4454 if (!(supported & JO_STOPONEXIT))
4455 break;
4456 opt->jo_set |= JO_STOPONEXIT;
4457 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4458 opt->jo_soe_buf);
4459 if (opt->jo_stoponexit == NULL)
4460 {
4461 EMSG2(_(e_invarg2), "stoponexit");
4462 return FAIL;
4463 }
4464 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004465 else if (STRCMP(hi->hi_key, "block_write") == 0)
4466 {
4467 if (!(supported & JO_BLOCK_WRITE))
4468 break;
4469 opt->jo_set |= JO_BLOCK_WRITE;
4470 opt->jo_block_write = get_tv_number(item);
4471 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004472 else
4473 break;
4474 --todo;
4475 }
4476 if (todo > 0)
4477 {
4478 EMSG2(_(e_invarg2), hi->hi_key);
4479 return FAIL;
4480 }
4481
4482 return OK;
4483}
4484
4485/*
4486 * Get the channel from the argument.
4487 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004488 * When "check_open" is TRUE check that the channel can be used.
4489 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004490 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491 */
4492 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004493get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004495 channel_T *channel = NULL;
4496 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004497
4498 if (tv->v_type == VAR_JOB)
4499 {
4500 if (tv->vval.v_job != NULL)
4501 channel = tv->vval.v_job->jv_channel;
4502 }
4503 else if (tv->v_type == VAR_CHANNEL)
4504 {
4505 channel = tv->vval.v_channel;
4506 }
4507 else
4508 {
4509 EMSG2(_(e_invarg2), get_tv_string(tv));
4510 return NULL;
4511 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004512 if (channel != NULL && reading)
4513 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004514 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004515
Bram Moolenaar437905c2016-04-26 19:01:05 +02004516 if (check_open && (channel == NULL || (!channel_is_open(channel)
4517 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004518 {
4519 EMSG(_("E906: not an open channel"));
4520 return NULL;
4521 }
4522 return channel;
4523}
4524
4525static job_T *first_job = NULL;
4526
4527 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004528job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004529{
4530 ch_log(job->jv_channel, "Freeing job");
4531 if (job->jv_channel != NULL)
4532 {
4533 /* The link from the channel to the job doesn't count as a reference,
4534 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004535 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004536 * NULL the reference. We don't set ch_job_killed, unreferencing the
4537 * job doesn't mean it stops running. */
4538 job->jv_channel->ch_job = NULL;
4539 channel_unref(job->jv_channel);
4540 }
4541 mch_clear_job(job);
4542
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004543 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004544 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004545}
4546
4547 static void
4548job_free_job(job_T *job)
4549{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004550 if (job->jv_next != NULL)
4551 job->jv_next->jv_prev = job->jv_prev;
4552 if (job->jv_prev == NULL)
4553 first_job = job->jv_next;
4554 else
4555 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004556 vim_free(job);
4557}
4558
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004559 static void
4560job_free(job_T *job)
4561{
4562 if (!in_free_unref_items)
4563 {
4564 job_free_contents(job);
4565 job_free_job(job);
4566 }
4567}
4568
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004569#if defined(EXITFREE) || defined(PROTO)
4570 void
4571job_free_all(void)
4572{
4573 while (first_job != NULL)
4574 job_free(first_job);
4575}
4576#endif
4577
4578/*
4579 * Return TRUE if we need to check if the process of "job" has ended.
4580 */
4581 static int
4582job_need_end_check(job_T *job)
4583{
4584 return job->jv_status == JOB_STARTED
4585 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4586}
4587
4588/*
4589 * Return TRUE if the channel of "job" is still useful.
4590 */
4591 static int
4592job_channel_still_useful(job_T *job)
4593{
4594 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4595}
4596
4597/*
4598 * Return TRUE if the job should not be freed yet. Do not free the job when
4599 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4600 * or when the associated channel will do something with the job output.
4601 */
4602 static int
4603job_still_useful(job_T *job)
4604{
4605 return job_need_end_check(job) || job_channel_still_useful(job);
4606}
4607
4608/*
4609 * NOTE: Must call job_cleanup() only once right after the status of "job"
4610 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
4611 * mch_detect_ended_job() returned non-NULL).
4612 */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004613 static void
4614job_cleanup(job_T *job)
4615{
4616 if (job->jv_status != JOB_ENDED)
4617 return;
4618
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004619 /* Ready to cleanup the job. */
4620 job->jv_status = JOB_FINISHED;
4621
Bram Moolenaar97792de2016-10-15 18:36:49 +02004622 if (job->jv_exit_cb != NULL)
4623 {
4624 typval_T argv[3];
4625 typval_T rettv;
4626 int dummy;
4627
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004628 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004629 ++job->jv_refcount;
4630 argv[0].v_type = VAR_JOB;
4631 argv[0].vval.v_job = job;
4632 argv[1].v_type = VAR_NUMBER;
4633 argv[1].vval.v_number = job->jv_exitval;
4634 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
4635 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
4636 job->jv_exit_partial, NULL);
4637 clear_tv(&rettv);
4638 --job->jv_refcount;
4639 channel_need_redraw = TRUE;
4640 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004641
4642 /* Do not free the job in case the close callback of the associated channel
4643 * isn't invoked yet and may get information by job_info(). */
4644 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02004645 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004646 /* The job was already unreferenced and the associated channel was
4647 * detached, now that it ended it can be freed. Careful: caller must
4648 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004649 job_free(job);
4650 }
4651}
4652
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004653/*
4654 * Mark references in jobs that are still useful.
4655 */
4656 int
4657set_ref_in_job(int copyID)
4658{
4659 int abort = FALSE;
4660 job_T *job;
4661 typval_T tv;
4662
4663 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004664 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004665 {
4666 tv.v_type = VAR_JOB;
4667 tv.vval.v_job = job;
4668 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4669 }
4670 return abort;
4671}
4672
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004673/*
4674 * Dereference "job". Note that after this "job" may have been freed.
4675 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004676 void
4677job_unref(job_T *job)
4678{
4679 if (job != NULL && --job->jv_refcount <= 0)
4680 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004681 /* Do not free the job if there is a channel where the close callback
4682 * may get the job info. */
4683 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004684 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004685 /* Do not free the job when it has not ended yet and there is a
4686 * "stoponexit" flag or an exit callback. */
4687 if (!job_need_end_check(job))
4688 {
4689 job_free(job);
4690 }
4691 else if (job->jv_channel != NULL)
4692 {
4693 /* Do remove the link to the channel, otherwise it hangs
4694 * around until Vim exits. See job_free() for refcount. */
4695 ch_log(job->jv_channel, "detaching channel from job");
4696 job->jv_channel->ch_job = NULL;
4697 channel_unref(job->jv_channel);
4698 job->jv_channel = NULL;
4699 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004700 }
4701 }
4702}
4703
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004704 int
4705free_unused_jobs_contents(int copyID, int mask)
4706{
4707 int did_free = FALSE;
4708 job_T *job;
4709
4710 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004711 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004712 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004713 {
4714 /* Free the channel and ordinary items it contains, but don't
4715 * recurse into Lists, Dictionaries etc. */
4716 job_free_contents(job);
4717 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004718 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004719 return did_free;
4720}
4721
4722 void
4723free_unused_jobs(int copyID, int mask)
4724{
4725 job_T *job;
4726 job_T *job_next;
4727
4728 for (job = first_job; job != NULL; job = job_next)
4729 {
4730 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004731 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004732 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004733 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004734 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004735 job_free_job(job);
4736 }
4737 }
4738}
4739
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004740/*
4741 * Allocate a job. Sets the refcount to one and sets options default.
4742 */
4743 static job_T *
4744job_alloc(void)
4745{
4746 job_T *job;
4747
4748 job = (job_T *)alloc_clear(sizeof(job_T));
4749 if (job != NULL)
4750 {
4751 job->jv_refcount = 1;
4752 job->jv_stoponexit = vim_strsave((char_u *)"term");
4753
4754 if (first_job != NULL)
4755 {
4756 first_job->jv_prev = job;
4757 job->jv_next = first_job;
4758 }
4759 first_job = job;
4760 }
4761 return job;
4762}
4763
4764 void
4765job_set_options(job_T *job, jobopt_T *opt)
4766{
4767 if (opt->jo_set & JO_STOPONEXIT)
4768 {
4769 vim_free(job->jv_stoponexit);
4770 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4771 job->jv_stoponexit = NULL;
4772 else
4773 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4774 }
4775 if (opt->jo_set & JO_EXIT_CB)
4776 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004777 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004778 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004779 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004780 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004781 job->jv_exit_partial = NULL;
4782 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004783 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004784 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004785 job->jv_exit_partial = opt->jo_exit_partial;
4786 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004787 {
4788 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004789 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004790 }
4791 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004792 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004793 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004794 func_ref(job->jv_exit_cb);
4795 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004796 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004797 }
4798}
4799
4800/*
4801 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4802 */
4803 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004804job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004805{
4806 job_T *job;
4807
4808 for (job = first_job; job != NULL; job = job->jv_next)
4809 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4810 mch_stop_job(job, job->jv_stoponexit);
4811}
4812
4813/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004814 * Return TRUE when there is any job that has an exit callback and might exit,
4815 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004816 */
4817 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004818has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004819{
4820 job_T *job;
4821
4822 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02004823 /* Only should check if the channel has been closed, if the channel is
4824 * open the job won't exit. */
4825 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004826 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004827 return TRUE;
4828 return FALSE;
4829}
4830
Bram Moolenaar97792de2016-10-15 18:36:49 +02004831#define MAX_CHECK_ENDED 8
4832
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004833/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004834 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004835 */
4836 void
4837job_check_ended(void)
4838{
Bram Moolenaar97792de2016-10-15 18:36:49 +02004839 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004840
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004841 if (first_job == NULL)
4842 return;
4843
Bram Moolenaar97792de2016-10-15 18:36:49 +02004844 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004845 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004846 /* NOTE: mch_detect_ended_job() must only return a job of which the
4847 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02004848 job_T *job = mch_detect_ended_job(first_job);
4849
4850 if (job == NULL)
4851 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004852 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004853 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02004854
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004855 if (channel_need_redraw)
4856 {
4857 channel_need_redraw = FALSE;
4858 redraw_after_callback();
4859 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004860}
4861
4862/*
4863 * "job_start()" function
4864 */
4865 job_T *
4866job_start(typval_T *argvars)
4867{
4868 job_T *job;
4869 char_u *cmd = NULL;
4870#if defined(UNIX)
4871# define USE_ARGV
4872 char **argv = NULL;
4873 int argc = 0;
4874#else
4875 garray_T ga;
4876#endif
4877 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004878 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004879
4880 job = job_alloc();
4881 if (job == NULL)
4882 return NULL;
4883
4884 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004885#ifndef USE_ARGV
4886 ga_init2(&ga, (int)sizeof(char*), 20);
4887#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004888
4889 /* Default mode is NL. */
4890 clear_job_options(&opt);
4891 opt.jo_mode = MODE_NL;
4892 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004893 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4894 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004895 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004896
4897 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004898 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004899 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4900 && opt.jo_io[part] == JIO_FILE
4901 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4902 || *opt.jo_io_name[part] == NUL))
4903 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004904 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004905 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004906 }
4907
4908 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4909 {
4910 buf_T *buf = NULL;
4911
4912 /* check that we can find the buffer before starting the job */
4913 if (opt.jo_set & JO_IN_BUF)
4914 {
4915 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4916 if (buf == NULL)
4917 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4918 }
4919 else if (!(opt.jo_set & JO_IN_NAME))
4920 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004921 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004922 }
4923 else
4924 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4925 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004926 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004927 if (buf->b_ml.ml_mfp == NULL)
4928 {
4929 char_u numbuf[NUMBUFLEN];
4930 char_u *s;
4931
4932 if (opt.jo_set & JO_IN_BUF)
4933 {
4934 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4935 s = numbuf;
4936 }
4937 else
4938 s = opt.jo_io_name[PART_IN];
4939 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004940 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004941 }
4942 job->jv_in_buf = buf;
4943 }
4944
4945 job_set_options(job, &opt);
4946
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004947 if (argvars[0].v_type == VAR_STRING)
4948 {
4949 /* Command is a string. */
4950 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004951 if (cmd == NULL || *cmd == NUL)
4952 {
4953 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004954 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004955 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004956#ifdef USE_ARGV
4957 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004958 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004959 argv[argc] = NULL;
4960#endif
4961 }
4962 else if (argvars[0].v_type != VAR_LIST
4963 || argvars[0].vval.v_list == NULL
4964 || argvars[0].vval.v_list->lv_len < 1)
4965 {
4966 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004967 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004968 }
4969 else
4970 {
4971 list_T *l = argvars[0].vval.v_list;
4972 listitem_T *li;
4973 char_u *s;
4974
4975#ifdef USE_ARGV
4976 /* Pass argv[] to mch_call_shell(). */
4977 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4978 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004979 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004980#endif
4981 for (li = l->lv_first; li != NULL; li = li->li_next)
4982 {
4983 s = get_tv_string_chk(&li->li_tv);
4984 if (s == NULL)
4985 goto theend;
4986#ifdef USE_ARGV
4987 argv[argc++] = (char *)s;
4988#else
4989 /* Only escape when needed, double quotes are not always allowed. */
4990 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4991 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004992# ifdef WIN32
4993 int old_ssl = p_ssl;
4994
4995 /* This is using CreateProcess, not cmd.exe. Always use
4996 * double quote and backslashes. */
4997 p_ssl = 0;
4998# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004999 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01005000# ifdef WIN32
5001 p_ssl = old_ssl;
5002# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005003 if (s == NULL)
5004 goto theend;
5005 ga_concat(&ga, s);
5006 vim_free(s);
5007 }
5008 else
5009 ga_concat(&ga, s);
5010 if (li->li_next != NULL)
5011 ga_append(&ga, ' ');
5012#endif
5013 }
5014#ifdef USE_ARGV
5015 argv[argc] = NULL;
5016#else
5017 cmd = ga.ga_data;
5018#endif
5019 }
5020
5021#ifdef USE_ARGV
5022 if (ch_log_active())
5023 {
5024 garray_T ga;
5025 int i;
5026
5027 ga_init2(&ga, (int)sizeof(char), 200);
5028 for (i = 0; i < argc; ++i)
5029 {
5030 if (i > 0)
5031 ga_concat(&ga, (char_u *)" ");
5032 ga_concat(&ga, (char_u *)argv[i]);
5033 }
5034 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
5035 ga_clear(&ga);
5036 }
5037 mch_start_job(argv, job, &opt);
5038#else
5039 ch_logs(NULL, "Starting job: %s", (char *)cmd);
5040 mch_start_job((char *)cmd, job, &opt);
5041#endif
5042
5043 /* If the channel is reading from a buffer, write lines now. */
5044 if (job->jv_channel != NULL)
5045 channel_write_in(job->jv_channel);
5046
5047theend:
5048#ifdef USE_ARGV
5049 vim_free(argv);
5050#else
5051 vim_free(ga.ga_data);
5052#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005053 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005054 return job;
5055}
5056
5057/*
5058 * Get the status of "job" and invoke the exit callback when needed.
5059 * The returned string is not allocated.
5060 */
5061 char *
5062job_status(job_T *job)
5063{
5064 char *result;
5065
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005066 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005067 /* No need to check, dead is dead. */
5068 result = "dead";
5069 else if (job->jv_status == JOB_FAILED)
5070 result = "fail";
5071 else
5072 {
5073 result = mch_job_status(job);
5074 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005075 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005076 }
5077 return result;
5078}
5079
Bram Moolenaar8950a562016-03-12 15:22:55 +01005080/*
5081 * Implementation of job_info().
5082 */
5083 void
5084job_info(job_T *job, dict_T *dict)
5085{
5086 dictitem_T *item;
5087 varnumber_T nr;
5088
5089 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5090
5091 item = dictitem_alloc((char_u *)"channel");
5092 if (item == NULL)
5093 return;
5094 item->di_tv.v_lock = 0;
5095 item->di_tv.v_type = VAR_CHANNEL;
5096 item->di_tv.vval.v_channel = job->jv_channel;
5097 if (job->jv_channel != NULL)
5098 ++job->jv_channel->ch_refcount;
5099 if (dict_add(dict, item) == FAIL)
5100 dictitem_free(item);
5101
5102#ifdef UNIX
5103 nr = job->jv_pid;
5104#else
5105 nr = job->jv_proc_info.dwProcessId;
5106#endif
5107 dict_add_nr_str(dict, "process", nr, NULL);
5108
5109 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005110 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005111 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5112}
5113
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005114 int
5115job_stop(job_T *job, typval_T *argvars)
5116{
5117 char_u *arg;
5118
5119 if (argvars[1].v_type == VAR_UNKNOWN)
5120 arg = (char_u *)"";
5121 else
5122 {
5123 arg = get_tv_string_chk(&argvars[1]);
5124 if (arg == NULL)
5125 {
5126 EMSG(_(e_invarg));
5127 return 0;
5128 }
5129 }
5130 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
5131 if (mch_stop_job(job, arg) == FAIL)
5132 return 0;
5133
5134 /* Assume that "hup" does not kill the job. */
5135 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
5136 job->jv_channel->ch_job_killed = TRUE;
5137
5138 /* We don't try freeing the job, obviously the caller still has a
5139 * reference to it. */
5140 return 1;
5141}
5142
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005143#endif /* FEAT_JOB_CHANNEL */