blob: 10ed42e6b7e014f28fa1cbd23c97a08f6c4b6073 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
22/* Note: when making changes here also adjust configure.in. */
23#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaarb2658a12016-04-26 17:16:24 +020057static void channel_read(channel_T *channel, int part, char *func);
58
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
119 EMSG2(_(e_notopen), fname);
120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200135ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100162ch_log(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100166 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100170 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100171 }
172}
173
174 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100175ch_logn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176{
177 if (log_fd != NULL)
178 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100181 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100183 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184 }
185}
186
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100187 void
Bram Moolenaar77073442016-02-13 23:23:53 +0100188ch_logs(channel_T *ch, char *msg, char *name)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100192 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193 fprintf(log_fd, msg, name);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100194 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100195 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100196 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197 }
198}
199
200 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100201ch_logsn(channel_T *ch, char *msg, char *name, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100205 ch_log_lead("", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206 fprintf(log_fd, msg, name, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100207 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100208 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100209 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100210 }
211}
212
213 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100214ch_error(channel_T *ch, char *msg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100215{
216 if (log_fd != NULL)
217 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100218 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 fputs(msg, log_fd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100220 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100221 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100222 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100223 }
224}
225
226 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100227ch_errorn(channel_T *ch, char *msg, int nr)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100228{
229 if (log_fd != NULL)
230 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fprintf(log_fd, msg, nr);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100233 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100235 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100236 }
237}
238
239 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100240ch_errors(channel_T *ch, char *msg, char *arg)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100241{
242 if (log_fd != NULL)
243 {
Bram Moolenaar77073442016-02-13 23:23:53 +0100244 ch_log_lead("ERR ", ch);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100245 fprintf(log_fd, msg, arg);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100246 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100247 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100248 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100249 }
250}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100251
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252#ifdef _WIN32
253# undef PERROR
254# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
255 (char_u *)msg, (char_u *)strerror_win32(errno))
256
257 static char *
258strerror_win32(int eno)
259{
260 static LPVOID msgbuf = NULL;
261 char_u *ptr;
262
263 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200264 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100265 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200266 msgbuf = NULL;
267 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100268 FormatMessage(
269 FORMAT_MESSAGE_ALLOCATE_BUFFER |
270 FORMAT_MESSAGE_FROM_SYSTEM |
271 FORMAT_MESSAGE_IGNORE_INSERTS,
272 NULL,
273 eno,
274 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
275 (LPTSTR) &msgbuf,
276 0,
277 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200278 if (msgbuf != NULL)
279 /* chomp \r or \n */
280 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
281 switch (*ptr)
282 {
283 case '\r':
284 STRMOVE(ptr, ptr + 1);
285 ptr--;
286 break;
287 case '\n':
288 if (*(ptr + 1) == '\0')
289 *ptr = '\0';
290 else
291 *ptr = ' ';
292 break;
293 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100294 return msgbuf;
295}
296#endif
297
Bram Moolenaar77073442016-02-13 23:23:53 +0100298/*
299 * The list of all allocated channels.
300 */
301static channel_T *first_channel = NULL;
302static int next_ch_id = 0;
303
304/*
305 * Allocate a new channel. The refcount is set to 1.
306 * The channel isn't actually used until it is opened.
307 * Returns NULL if out of memory.
308 */
309 channel_T *
310add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100311{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 int part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100313 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100314
Bram Moolenaar77073442016-02-13 23:23:53 +0100315 if (channel == NULL)
316 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100317
Bram Moolenaar77073442016-02-13 23:23:53 +0100318 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100319 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100320
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100321 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100322 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100323 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100324#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100325 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100326#endif
327#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100328 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100329#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100330 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100331 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100332
Bram Moolenaar77073442016-02-13 23:23:53 +0100333 if (first_channel != NULL)
334 {
335 first_channel->ch_prev = channel;
336 channel->ch_next = first_channel;
337 }
338 first_channel = channel;
339
340 channel->ch_refcount = 1;
341 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100342}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100343
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100344/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100345 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100346 * Return TRUE if "channel" has a callback and the associated job wasn't
347 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100348 */
349 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100350channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100351{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100352 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100353 int has_out_msg;
354 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100355
356 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100357 if (channel->ch_job_killed && channel->ch_job == NULL)
358 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100359
360 /* If there is a close callback it may still need to be invoked. */
361 if (channel->ch_close_cb != NULL)
362 return TRUE;
363
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200364 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200365 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200366 return TRUE;
367
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100368 /* If there is no callback then nobody can get readahead. If the fd is
369 * closed and there is no readahead then the callback won't be called. */
370 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
371 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
372 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100373 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
374 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
375 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
376 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
377 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
378 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100379 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100380 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200381 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200382 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
383 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200384 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200385 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
386 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100387}
388
389/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200390 * Close a channel and free all its resources.
391 */
392 static void
393channel_free_contents(channel_T *channel)
394{
395 channel_close(channel, TRUE);
396 channel_clear(channel);
397 ch_log(channel, "Freeing channel");
398}
399
400 static void
401channel_free_channel(channel_T *channel)
402{
403 if (channel->ch_next != NULL)
404 channel->ch_next->ch_prev = channel->ch_prev;
405 if (channel->ch_prev == NULL)
406 first_channel = channel->ch_next;
407 else
408 channel->ch_prev->ch_next = channel->ch_next;
409 vim_free(channel);
410}
411
412 static void
413channel_free(channel_T *channel)
414{
415 if (!in_free_unref_items)
416 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200417 if (safe_to_invoke_callback == 0)
418 {
419 channel->ch_to_be_freed = TRUE;
420 }
421 else
422 {
423 channel_free_contents(channel);
424 channel_free_channel(channel);
425 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200426 }
427}
428
429/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100430 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100431 * possible, there is no callback to be invoked or the associated job was
432 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100433 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100434 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100435 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436channel_may_free(channel_T *channel)
437{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100438 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100439 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100440 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100441 return TRUE;
442 }
443 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100444}
445
446/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100447 * Decrement the reference count on "channel" and maybe free it when it goes
448 * down to zero. Don't free it if there is a pending action.
449 * Returns TRUE when the channel is no longer referenced.
450 */
451 int
452channel_unref(channel_T *channel)
453{
454 if (channel != NULL && --channel->ch_refcount <= 0)
455 return channel_may_free(channel);
456 return FALSE;
457}
458
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200459 int
460free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100461{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200462 int did_free = FALSE;
463 channel_T *ch;
464
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200465 /* This is invoked from the garbage collector, which only runs at a safe
466 * point. */
467 ++safe_to_invoke_callback;
468
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200469 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200470 if (!channel_still_useful(ch)
471 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200472 {
473 /* Free the channel and ordinary items it contains, but don't
474 * recurse into Lists, Dictionaries etc. */
475 channel_free_contents(ch);
476 did_free = TRUE;
477 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200478
479 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200480 return did_free;
481}
482
483 void
484free_unused_channels(int copyID, int mask)
485{
486 channel_T *ch;
487 channel_T *ch_next;
488
489 for (ch = first_channel; ch != NULL; ch = ch_next)
490 {
491 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200492 if (!channel_still_useful(ch)
493 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200494 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200495 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200496 channel_free_channel(ch);
497 }
498 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100499}
500
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100502
503#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
504 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100505channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100506{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100507 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100508 int part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100509
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100510 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100511 if (channel == NULL)
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100512 ch_errorn(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100513 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200514 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100515}
516#endif
517
Bram Moolenaare0874f82016-01-24 20:36:41 +0100518/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100519 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100520 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100521#ifdef FEAT_GUI_X11
522 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200523messageFromServer(XtPointer clientData,
524 int *unused1 UNUSED,
525 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100526{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100527 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100528}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100529#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100530
Bram Moolenaard04a0202016-01-26 23:30:18 +0100531#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100532# if GTK_CHECK_VERSION(3,0,0)
533 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200534messageFromServer(GIOChannel *unused1 UNUSED,
535 GIOCondition unused2 UNUSED,
536 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100537{
538 channel_read_fd(GPOINTER_TO_INT(clientData));
539 return TRUE; /* Return FALSE instead in case the event source is to
540 * be removed after this function returns. */
541}
542# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100543 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200544messageFromServer(gpointer clientData,
545 gint unused1 UNUSED,
546 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100547{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100548 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100549}
Bram Moolenaar98921892016-02-23 17:14:37 +0100550# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100551#endif
552
553 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100554channel_gui_register_one(channel_T *channel, int part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100555{
Bram Moolenaarde279892016-03-11 22:19:44 +0100556 if (!CH_HAS_GUI)
557 return;
558
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100559# ifdef FEAT_GUI_X11
560 /* Tell notifier we are interested in being called
561 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100562 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
563 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100564 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100565 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100566 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200567 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100568 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100569# else
570# ifdef FEAT_GUI_GTK
571 /* Tell gdk we are interested in being called when there
572 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100573 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100574# if GTK_CHECK_VERSION(3,0,0)
575 {
576 GIOChannel *chnnl = g_io_channel_unix_new(
577 (gint)channel->ch_part[part].ch_fd);
578
579 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
580 chnnl,
581 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200582 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100583 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
584
585 g_io_channel_unref(chnnl);
586 }
587# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100588 channel->ch_part[part].ch_inputHandler = gdk_input_add(
589 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100590 (GdkInputCondition)
591 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200592 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100593 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100594# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100595# endif
596# endif
597}
598
Bram Moolenaarde279892016-03-11 22:19:44 +0100599 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100600channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100601{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100602 if (channel->CH_SOCK_FD != INVALID_FD)
603 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100604 if (channel->CH_OUT_FD != INVALID_FD)
605 channel_gui_register_one(channel, PART_OUT);
606 if (channel->CH_ERR_FD != INVALID_FD)
607 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100608}
609
610/*
611 * Register any of our file descriptors with the GUI event handling system.
612 * Called when the GUI has started.
613 */
614 void
615channel_gui_register_all(void)
616{
Bram Moolenaar77073442016-02-13 23:23:53 +0100617 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100618
Bram Moolenaar77073442016-02-13 23:23:53 +0100619 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100620 channel_gui_register(channel);
621}
622
623 static void
Bram Moolenaarde279892016-03-11 22:19:44 +0100624channel_gui_unregister_one(channel_T *channel, int part)
625{
626# ifdef FEAT_GUI_X11
627 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
628 {
629 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
630 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
631 }
632# else
633# ifdef FEAT_GUI_GTK
634 if (channel->ch_part[part].ch_inputHandler != 0)
635 {
636# if GTK_CHECK_VERSION(3,0,0)
637 g_source_remove(channel->ch_part[part].ch_inputHandler);
638# else
639 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
640# endif
641 channel->ch_part[part].ch_inputHandler = 0;
642 }
643# endif
644# endif
645}
646
647 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100648channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100649{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100650 int part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100651
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100652 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100653 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100654}
655
656#endif
657
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100658static char *e_cannot_connect = N_("E902: Cannot connect to port");
659
Bram Moolenaard04a0202016-01-26 23:30:18 +0100660/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100661 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100662 * "waittime" is the time in msec to wait for the connection.
663 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100664 * Returns the channel for success.
665 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100666 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100667 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100668channel_open(
669 char *hostname,
670 int port_in,
671 int waittime,
672 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100675 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100676 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100677#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100679 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100680#else
681 int port = port_in;
682#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100683 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100684 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100685
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100686#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100687 channel_init_winsock();
688#endif
689
Bram Moolenaar77073442016-02-13 23:23:53 +0100690 channel = add_channel();
691 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100692 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100693 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100694 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100695 }
696
697 /* Get the server internet address and put into addr structure */
698 /* fill in the socket address structure and connect to server */
699 vim_memset((char *)&server, 0, sizeof(server));
700 server.sin_family = AF_INET;
701 server.sin_port = htons(port);
702 if ((host = gethostbyname(hostname)) == NULL)
703 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100704 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200705 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100706 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100707 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100708 }
709 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
710
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100711 /* On Mac and Solaris a zero timeout almost never works. At least wait
712 * one millisecond. Let's do it for all systems, because we don't know why
713 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100714 if (waittime == 0)
715 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100716
717 /*
718 * For Unix we need to call connect() again after connect() failed.
719 * On Win32 one time is sufficient.
720 */
721 while (TRUE)
722 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100723 long elapsed_msec = 0;
724 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100725
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100726 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100727 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100728 sd = socket(AF_INET, SOCK_STREAM, 0);
729 if (sd == -1)
730 {
731 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200732 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100733 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100734 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100735 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100736
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100737 if (waittime >= 0)
738 {
739 /* Make connect() non-blocking. */
740 if (
741#ifdef _WIN32
742 ioctlsocket(sd, FIONBIO, &val) < 0
743#else
744 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
745#endif
746 )
747 {
748 SOCK_ERRNO;
749 ch_errorn(channel,
750 "channel_open: Connect failed with errno %d", errno);
751 sock_close(sd);
752 channel_free(channel);
753 return NULL;
754 }
755 }
756
757 /* Try connecting to the server. */
758 ch_logsn(channel, "Connecting to %s port %d", hostname, port);
759 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
760
Bram Moolenaar045a2842016-03-08 22:33:07 +0100761 if (ret == 0)
762 /* The connection could be established. */
763 break;
764
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100765 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100766 if (waittime < 0 || (errno != EWOULDBLOCK
767 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100768#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100769 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100770#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100771 ))
772 {
773 ch_errorn(channel,
774 "channel_open: Connect failed with errno %d", errno);
775 PERROR(_(e_cannot_connect));
776 sock_close(sd);
777 channel_free(channel);
778 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100779 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100780
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100781 /* Limit the waittime to 50 msec. If it doesn't work within this
782 * time we close the socket and try creating it again. */
783 waitnow = waittime > 50 ? 50 : waittime;
784
Bram Moolenaar045a2842016-03-08 22:33:07 +0100785 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100786 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100787#ifndef WIN32
788 if (errno != ECONNREFUSED)
789#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100790 {
791 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100792 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100793 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100794#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100795 int so_error = 0;
796 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100797 struct timeval start_tv;
798 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100799#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100800 FD_ZERO(&rfds);
801 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100802 FD_ZERO(&wfds);
803 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100804
Bram Moolenaar562ca712016-03-09 21:50:05 +0100805 tv.tv_sec = waitnow / 1000;
806 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100807#ifndef WIN32
808 gettimeofday(&start_tv, NULL);
809#endif
810 ch_logn(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100811 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100812 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100813
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100814 if (ret < 0)
815 {
816 SOCK_ERRNO;
817 ch_errorn(channel,
818 "channel_open: Connect failed with errno %d", errno);
819 PERROR(_(e_cannot_connect));
820 sock_close(sd);
821 channel_free(channel);
822 return NULL;
823 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100824
Bram Moolenaare081e212016-02-28 22:33:46 +0100825#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100826 /* On Win32: select() is expected to work and wait for up to
827 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100828 if (FD_ISSET(sd, &wfds))
829 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100830 elapsed_msec = waitnow;
831 if (waittime > 1 && elapsed_msec < waittime)
832 {
833 waittime -= elapsed_msec;
834 continue;
835 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100836#else
837 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100838 * After putting the socket in non-blocking mode, connect() will
839 * return EINPROGRESS, select() will not wait (as if writing is
840 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100841 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100842 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100843 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100844 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100845 {
846 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100847 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 if (ret < 0 || (so_error != 0
849 && so_error != EWOULDBLOCK
850 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100851# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100852 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100853# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100854 ))
855 {
856 ch_errorn(channel,
857 "channel_open: Connect failed with errno %d",
858 so_error);
859 PERROR(_(e_cannot_connect));
860 sock_close(sd);
861 channel_free(channel);
862 return NULL;
863 }
864 }
865
Bram Moolenaar045a2842016-03-08 22:33:07 +0100866 if (FD_ISSET(sd, &wfds) && so_error == 0)
867 /* Did not detect an error, connection is established. */
868 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100869
Bram Moolenaar045a2842016-03-08 22:33:07 +0100870 gettimeofday(&end_tv, NULL);
871 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
872 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100873#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100874 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875
876#ifndef WIN32
877 if (waittime > 1 && elapsed_msec < waittime)
878 {
879 /* The port isn't ready but we also didn't get an error.
880 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100881 * yet. Select() may return early, wait until the remaining
882 * "waitnow" and try again. */
883 waitnow -= elapsed_msec;
884 waittime -= elapsed_msec;
885 if (waitnow > 0)
886 {
887 mch_delay((long)waitnow, TRUE);
888 ui_breakcheck();
889 waittime -= waitnow;
890 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100891 if (!got_int)
892 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100893 if (waittime <= 0)
894 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100895 waittime = 1;
896 continue;
897 }
898 /* we were interrupted, behave as if timed out */
899 }
900#endif
901
902 /* We timed out. */
903 ch_error(channel, "Connection timed out");
904 sock_close(sd);
905 channel_free(channel);
906 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100907 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100908
Bram Moolenaar045a2842016-03-08 22:33:07 +0100909 ch_log(channel, "Connection made");
910
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100911 if (waittime >= 0)
912 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100913#ifdef _WIN32
914 val = 0;
915 ioctlsocket(sd, FIONBIO, &val);
916#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100917 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100918#endif
919 }
920
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100921 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100922 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100923 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
924 channel->ch_port = port_in;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100925
926#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100927 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100928#endif
929
Bram Moolenaar77073442016-02-13 23:23:53 +0100930 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100931}
932
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100933/*
934 * Implements ch_open().
935 */
936 channel_T *
937channel_open_func(typval_T *argvars)
938{
939 char_u *address;
940 char_u *p;
941 char *rest;
942 int port;
943 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200944 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100945
946 address = get_tv_string(&argvars[0]);
947 if (argvars[1].v_type != VAR_UNKNOWN
948 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
949 {
950 EMSG(_(e_invarg));
951 return NULL;
952 }
953
954 /* parse address */
955 p = vim_strchr(address, ':');
956 if (p == NULL)
957 {
958 EMSG2(_(e_invarg2), address);
959 return NULL;
960 }
961 *p++ = NUL;
962 port = strtol((char *)p, &rest, 10);
963 if (*address == NUL || port <= 0 || *rest != NUL)
964 {
965 p[-1] = ':';
966 EMSG2(_(e_invarg2), address);
967 return NULL;
968 }
969
970 /* parse options */
971 clear_job_options(&opt);
972 opt.jo_mode = MODE_JSON;
973 opt.jo_timeout = 2000;
974 if (get_job_options(&argvars[1], &opt,
975 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200976 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100977 if (opt.jo_timeout < 0)
978 {
979 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200980 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100981 }
982
983 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
984 if (channel != NULL)
985 {
986 opt.jo_set = JO_ALL;
987 channel_set_options(channel, &opt);
988 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200989theend:
990 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100991 return channel;
992}
993
Bram Moolenaarde279892016-03-11 22:19:44 +0100994 static void
995may_close_part(sock_T *fd)
996{
997 if (*fd != INVALID_FD)
998 {
999 fd_close(*fd);
1000 *fd = INVALID_FD;
1001 }
1002}
1003
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001004 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001005channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001006{
Bram Moolenaarde279892016-03-11 22:19:44 +01001007 if (in != INVALID_FD)
1008 {
1009 may_close_part(&channel->CH_IN_FD);
1010 channel->CH_IN_FD = in;
1011 }
1012 if (out != INVALID_FD)
1013 {
1014# if defined(FEAT_GUI)
1015 channel_gui_unregister_one(channel, PART_OUT);
1016# endif
1017 may_close_part(&channel->CH_OUT_FD);
1018 channel->CH_OUT_FD = out;
1019# if defined(FEAT_GUI)
1020 channel_gui_register_one(channel, PART_OUT);
1021# endif
1022 }
1023 if (err != INVALID_FD)
1024 {
1025# if defined(FEAT_GUI)
1026 channel_gui_unregister_one(channel, PART_ERR);
1027# endif
1028 may_close_part(&channel->CH_ERR_FD);
1029 channel->CH_ERR_FD = err;
1030# if defined(FEAT_GUI)
1031 channel_gui_register_one(channel, PART_ERR);
1032# endif
1033 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001034}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001035
Bram Moolenaard6051b52016-02-28 15:49:03 +01001036/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001037 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001038 * This does not keep a refcount, when the job is freed ch_job is cleared.
1039 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001040 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001041channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001042{
Bram Moolenaar77073442016-02-13 23:23:53 +01001043 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001044
1045 channel_set_options(channel, options);
1046
1047 if (job->jv_in_buf != NULL)
1048 {
1049 chanpart_T *in_part = &channel->ch_part[PART_IN];
1050
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001051 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001052 ch_logs(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001053 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001054 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001055 {
1056 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1057 {
1058 /* Special mode: send last-but-one line when appending a line
1059 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001060 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001061 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001062 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001063 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001064 }
1065 else
1066 in_part->ch_buf_top = options->jo_in_top;
1067 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001068 else
1069 in_part->ch_buf_top = 1;
1070 if (options->jo_set & JO_IN_BOT)
1071 in_part->ch_buf_bot = options->jo_in_bot;
1072 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001073 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001074 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001075}
1076
1077/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001078 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001079 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001080 */
1081 static buf_T *
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001082find_buffer(char_u *name, int err)
Bram Moolenaar187db502016-02-27 14:44:26 +01001083{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001084 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001085 buf_T *save_curbuf = curbuf;
1086
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001087 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001088 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001089 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001090 if (buf == NULL)
1091 buf = buflist_findname_exp(name);
1092 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001093 if (buf == NULL)
1094 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001095 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001096 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001097 if (buf == NULL)
1098 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001099 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001100 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001101#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001102 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1103 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001104#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001105 if (curbuf->b_ml.ml_mfp == NULL)
1106 ml_open(curbuf);
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001107 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1108 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001109 changed_bytes(1, 0);
1110 curbuf = save_curbuf;
1111 }
1112
1113 return buf;
1114}
1115
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001116 static void
1117set_callback(
1118 char_u **cbp,
1119 partial_T **pp,
1120 char_u *callback,
1121 partial_T *partial)
1122{
1123 free_callback(*cbp, *pp);
1124 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001125 {
1126 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001127 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001128 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001129 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001130 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001131 func_ref(*cbp);
1132 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001133 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001134 else
1135 *cbp = NULL;
1136 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001137 if (partial != NULL)
1138 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001139}
1140
Bram Moolenaar187db502016-02-27 14:44:26 +01001141/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001142 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001143 */
1144 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001145channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001146{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001147 int part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001148
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001149 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001150 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001151 channel->ch_part[part].ch_mode = opt->jo_mode;
1152 if (opt->jo_set & JO_IN_MODE)
1153 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1154 if (opt->jo_set & JO_OUT_MODE)
1155 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1156 if (opt->jo_set & JO_ERR_MODE)
1157 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001158
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001159 if (opt->jo_set & JO_TIMEOUT)
1160 for (part = PART_SOCK; part <= PART_IN; ++part)
1161 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1162 if (opt->jo_set & JO_OUT_TIMEOUT)
1163 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1164 if (opt->jo_set & JO_ERR_TIMEOUT)
1165 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001166 if (opt->jo_set & JO_BLOCK_WRITE)
1167 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001168
1169 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001170 set_callback(&channel->ch_callback, &channel->ch_partial,
1171 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001172 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001173 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1174 &channel->ch_part[PART_OUT].ch_partial,
1175 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001176 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001177 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1178 &channel->ch_part[PART_ERR].ch_partial,
1179 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001180 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001181 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1182 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar187db502016-02-27 14:44:26 +01001183
1184 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1185 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001186 buf_T *buf;
1187
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001188 /* writing output to a buffer. Default mode is NL. */
1189 if (!(opt->jo_set & JO_OUT_MODE))
1190 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001191 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001192 {
1193 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1194 if (buf == NULL)
1195 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1196 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001197 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001198 {
1199 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1200 }
1201 if (buf != NULL)
1202 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001203 if (opt->jo_set & JO_OUT_MODIFIABLE)
1204 channel->ch_part[PART_OUT].ch_nomodifiable =
1205 !opt->jo_modifiable[PART_OUT];
1206
1207 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1208 {
1209 EMSG(_(e_modifiable));
1210 }
1211 else
1212 {
1213 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001214 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001215 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001216 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001217 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001218 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001219
1220 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1221 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1222 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1223 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001224 buf_T *buf;
1225
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001226 /* writing err to a buffer. Default mode is NL. */
1227 if (!(opt->jo_set & JO_ERR_MODE))
1228 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1229 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001230 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001231 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001232 {
1233 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1234 if (buf == NULL)
1235 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1236 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001237 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001238 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1239 if (buf != NULL)
1240 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001241 if (opt->jo_set & JO_ERR_MODIFIABLE)
1242 channel->ch_part[PART_ERR].ch_nomodifiable =
1243 !opt->jo_modifiable[PART_ERR];
1244 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1245 {
1246 EMSG(_(e_modifiable));
1247 }
1248 else
1249 {
1250 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001251 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001252 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001253 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001254 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001255 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001256
1257 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1258 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1259 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001260}
1261
1262/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001263 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001264 */
1265 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001266channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001267 channel_T *channel,
1268 int part,
1269 char_u *callback,
1270 partial_T *partial,
1271 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001272{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001273 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001274 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1275
1276 if (item != NULL)
1277 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001278 item->cq_partial = partial;
1279 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001280 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001281 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001282 item->cq_callback = callback;
1283 }
1284 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001285 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001286 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001287 func_ref(item->cq_callback);
1288 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001289 item->cq_seq_nr = id;
1290 item->cq_prev = head->cq_prev;
1291 head->cq_prev = item;
1292 item->cq_next = NULL;
1293 if (item->cq_prev == NULL)
1294 head->cq_next = item;
1295 else
1296 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001297 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001298}
1299
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001300 static void
1301write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1302{
1303 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001304 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001305 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001306 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001307
Bram Moolenaar655da312016-05-28 22:22:34 +02001308 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001309 if ((p = alloc(len + 2)) == NULL)
1310 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001311 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001312
1313 for (i = 0; i < len; ++i)
1314 if (p[i] == NL)
1315 p[i] = NUL;
1316
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001317 p[len] = NL;
1318 p[len + 1] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001319 channel_send(channel, PART_IN, p, len + 1, "write_buf_line()");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001320 vim_free(p);
1321}
1322
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001323/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001324 * Return TRUE if "channel" can be written to.
1325 * Returns FALSE if the input is closed or the write would block.
1326 */
1327 static int
1328can_write_buf_line(channel_T *channel)
1329{
1330 chanpart_T *in_part = &channel->ch_part[PART_IN];
1331
1332 if (in_part->ch_fd == INVALID_FD)
1333 return FALSE; /* pipe was closed */
1334
1335 /* for testing: block every other attempt to write */
1336 if (in_part->ch_block_write == 1)
1337 in_part->ch_block_write = -1;
1338 else if (in_part->ch_block_write == -1)
1339 in_part->ch_block_write = 1;
1340
1341 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1342#ifndef WIN32
1343 {
1344# if defined(HAVE_SELECT)
1345 struct timeval tval;
1346 fd_set wfds;
1347 int ret;
1348
1349 FD_ZERO(&wfds);
1350 FD_SET((int)in_part->ch_fd, &wfds);
1351 tval.tv_sec = 0;
1352 tval.tv_usec = 0;
1353 for (;;)
1354 {
1355 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1356# ifdef EINTR
1357 SOCK_ERRNO;
1358 if (ret == -1 && errno == EINTR)
1359 continue;
1360# endif
1361 if (ret <= 0 || in_part->ch_block_write == 1)
1362 {
1363 if (ret > 0)
1364 ch_log(channel, "FAKED Input not ready for writing");
1365 else
1366 ch_log(channel, "Input not ready for writing");
1367 return FALSE;
1368 }
1369 break;
1370 }
1371# else
1372 struct pollfd fds;
1373
1374 fds.fd = in_part->ch_fd;
1375 fds.events = POLLOUT;
1376 if (poll(&fds, 1, 0) <= 0)
1377 {
1378 ch_log(channel, "Input not ready for writing");
1379 return FALSE;
1380 }
1381 if (in_part->ch_block_write == 1)
1382 {
1383 ch_log(channel, "FAKED Input not ready for writing");
1384 return FALSE;
1385 }
1386# endif
1387 }
1388#endif
1389 return TRUE;
1390}
1391
1392/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001393 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001394 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001395 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001396channel_write_in(channel_T *channel)
1397{
1398 chanpart_T *in_part = &channel->ch_part[PART_IN];
1399 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001400 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001401 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001402
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001403 if (buf == NULL || in_part->ch_buf_append)
1404 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001405 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001406 {
1407 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001408 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001409 return;
1410 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001411
1412 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1413 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1414 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001415 if (!can_write_buf_line(channel))
1416 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001417 write_buf_line(buf, lnum, channel);
1418 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001419 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001420
1421 if (written == 1)
1422 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1423 else if (written > 1)
1424 ch_logn(channel, "written %d lines to channel", written);
1425
Bram Moolenaar014069a2016-03-03 22:51:40 +01001426 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001427 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001428 {
1429 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001430 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001431 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001432
1433 /* Close the pipe/socket, so that the other side gets EOF. */
1434 may_close_part(&channel->CH_IN_FD);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001435 }
1436 else
1437 ch_logn(channel, "Still %d more lines to write",
1438 buf->b_ml.ml_line_count - lnum + 1);
1439}
1440
1441/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001442 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001443 */
1444 void
1445channel_buffer_free(buf_T *buf)
1446{
1447 channel_T *channel;
1448 int part;
1449
1450 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1451 for (part = PART_SOCK; part <= PART_IN; ++part)
1452 {
1453 chanpart_T *ch_part = &channel->ch_part[part];
1454
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001455 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001456 {
1457 ch_logs(channel, "%s buffer has been wiped out",
1458 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001459 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001460 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001461 }
1462}
1463
1464/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001465 * Write any lines waiting to be written to a channel.
1466 */
1467 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001468channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001469{
1470 channel_T *channel;
1471
1472 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1473 {
1474 chanpart_T *in_part = &channel->ch_part[PART_IN];
1475
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001476 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001477 {
1478 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001479 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001480 else
1481 channel_write_in(channel);
1482 }
1483 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001484}
1485
1486/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001487 * Write appended lines above the last one in "buf" to the channel.
1488 */
1489 void
1490channel_write_new_lines(buf_T *buf)
1491{
1492 channel_T *channel;
1493 int found_one = FALSE;
1494
1495 /* There could be more than one channel for the buffer, loop over all of
1496 * them. */
1497 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1498 {
1499 chanpart_T *in_part = &channel->ch_part[PART_IN];
1500 linenr_T lnum;
1501 int written = 0;
1502
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001503 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 {
1505 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001506 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001507 found_one = TRUE;
1508 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1509 ++lnum)
1510 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001511 if (!can_write_buf_line(channel))
1512 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001513 write_buf_line(buf, lnum, channel);
1514 ++written;
1515 }
1516
1517 if (written == 1)
1518 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1519 else if (written > 1)
1520 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001521 if (lnum < buf->b_ml.ml_line_count)
1522 ch_logn(channel, "Still %d more lines to write",
1523 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001524
1525 in_part->ch_buf_bot = lnum;
1526 }
1527 }
1528 if (!found_one)
1529 buf->b_write_to_channel = FALSE;
1530}
1531
1532/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001533 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001534 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001535 */
1536 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001537invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1538 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001539{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001540 typval_T rettv;
1541 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001542
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001543 if (safe_to_invoke_callback == 0)
1544 EMSG("INTERNAL: Invoking callback when it is not safe");
1545
Bram Moolenaar77073442016-02-13 23:23:53 +01001546 argv[0].v_type = VAR_CHANNEL;
1547 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001548
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001549 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1550 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001551 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001552 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001553}
1554
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001555/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001556 * Return the first node from "channel"/"part" without removing it.
1557 * Returns NULL if there is nothing.
1558 */
1559 readq_T *
1560channel_peek(channel_T *channel, int part)
1561{
1562 readq_T *head = &channel->ch_part[part].ch_head;
1563
1564 return head->rq_next;
1565}
1566
1567/*
1568 * Return a pointer to the first NL in "node".
1569 * Skips over NUL characters.
1570 * Returns NULL if there is no NL.
1571 */
1572 char_u *
1573channel_first_nl(readq_T *node)
1574{
1575 char_u *buffer = node->rq_buffer;
1576 long_u i;
1577
1578 for (i = 0; i < node->rq_buflen; ++i)
1579 if (buffer[i] == NL)
1580 return buffer + i;
1581 return NULL;
1582}
1583
1584/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001585 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001586 * The caller must free it.
1587 * Returns NULL if there is nothing.
1588 */
1589 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001590channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001591{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001592 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001593 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001594 char_u *p;
1595
Bram Moolenaar77073442016-02-13 23:23:53 +01001596 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001597 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001598 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001599 p = node->rq_buffer;
1600 head->rq_next = node->rq_next;
1601 if (node->rq_next == NULL)
1602 head->rq_prev = NULL;
1603 else
1604 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001605 vim_free(node);
1606 return p;
1607}
1608
1609/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001610 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001611 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001612 */
1613 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001614channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001616 readq_T *head = &channel->ch_part[part].ch_head;
1617 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001618 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001619 char_u *res;
1620 char_u *p;
1621
1622 /* If there is only one buffer just get that one. */
1623 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1624 return channel_get(channel, part);
1625
1626 /* Concatenate everything into one buffer. */
1627 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001628 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001629 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001630 if (res == NULL)
1631 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001632 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001633 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001634 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001635 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001636 p += node->rq_buflen;
1637 }
1638 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001639
1640 /* Free all buffers */
1641 do
1642 {
1643 p = channel_get(channel, part);
1644 vim_free(p);
1645 } while (p != NULL);
1646
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001647 /* turn all NUL into NL */
1648 while (len > 0)
1649 {
1650 --len;
1651 if (res[len] == NUL)
1652 res[len] = NL;
1653 }
1654
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001655 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001656}
1657
1658/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001659 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001660 * Caller must check these bytes are available.
1661 */
1662 void
1663channel_consume(channel_T *channel, int part, int len)
1664{
1665 readq_T *head = &channel->ch_part[part].ch_head;
1666 readq_T *node = head->rq_next;
1667 char_u *buf = node->rq_buffer;
1668
1669 mch_memmove(buf, buf + len, node->rq_buflen - len);
1670 node->rq_buflen -= len;
1671}
1672
1673/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001674 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001676 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001677 */
1678 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001679channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001680{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001681 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001682 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001683 readq_T *last_node;
1684 readq_T *n;
1685 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001687 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001688
Bram Moolenaar77073442016-02-13 23:23:53 +01001689 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001690 return FAIL;
1691
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001692 last_node = node->rq_next;
1693 len = node->rq_buflen + last_node->rq_buflen + 1;
1694 if (want_nl)
1695 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001696 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001697 {
1698 last_node = last_node->rq_next;
1699 len += last_node->rq_buflen;
1700 }
1701
1702 p = newbuf = alloc(len);
1703 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001704 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001705 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001706 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001707 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001708 node->rq_buffer = newbuf;
1709 for (n = node; n != last_node; )
1710 {
1711 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001712 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001713 p += n->rq_buflen;
1714 vim_free(n->rq_buffer);
1715 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001716 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001717
1718 /* dispose of the collapsed nodes and their buffers */
1719 for (n = node->rq_next; n != last_node; )
1720 {
1721 n = n->rq_next;
1722 vim_free(n->rq_prev);
1723 }
1724 node->rq_next = last_node->rq_next;
1725 if (last_node->rq_next == NULL)
1726 head->rq_prev = node;
1727 else
1728 last_node->rq_next->rq_prev = node;
1729 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001730 return OK;
1731}
1732
1733/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001734 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001735 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001736 * Returns OK or FAIL.
1737 */
1738 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001739channel_save(channel_T *channel, int part, char_u *buf, int len,
1740 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001741{
1742 readq_T *node;
1743 readq_T *head = &channel->ch_part[part].ch_head;
1744 char_u *p;
1745 int i;
1746
1747 node = (readq_T *)alloc(sizeof(readq_T));
1748 if (node == NULL)
1749 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001750 /* A NUL is added at the end, because netbeans code expects that.
1751 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001752 node->rq_buffer = alloc(len + 1);
1753 if (node->rq_buffer == NULL)
1754 {
1755 vim_free(node);
1756 return FAIL; /* out of memory */
1757 }
1758
1759 if (channel->ch_part[part].ch_mode == MODE_NL)
1760 {
1761 /* Drop any CR before a NL. */
1762 p = node->rq_buffer;
1763 for (i = 0; i < len; ++i)
1764 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1765 *p++ = buf[i];
1766 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001767 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001768 }
1769 else
1770 {
1771 mch_memmove(node->rq_buffer, buf, len);
1772 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001773 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001774 }
1775
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001776 if (prepend)
1777 {
1778 /* preend node to the head of the queue */
1779 node->rq_next = head->rq_next;
1780 node->rq_prev = NULL;
1781 if (head->rq_next == NULL)
1782 head->rq_prev = node;
1783 else
1784 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001786 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001787 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001788 {
1789 /* append node to the tail of the queue */
1790 node->rq_next = NULL;
1791 node->rq_prev = head->rq_prev;
1792 if (head->rq_prev == NULL)
1793 head->rq_next = node;
1794 else
1795 head->rq_prev->rq_next = node;
1796 head->rq_prev = node;
1797 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001798
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001799 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001800 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001801 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 fprintf(log_fd, "'");
1803 if (fwrite(buf, len, 1, log_fd) != 1)
1804 return FAIL;
1805 fprintf(log_fd, "'\n");
1806 }
1807 return OK;
1808}
1809
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001810 static int
1811channel_fill(js_read_T *reader)
1812{
1813 channel_T *channel = (channel_T *)reader->js_cookie;
1814 int part = reader->js_cookie_arg;
1815 char_u *next = channel_get(channel, part);
1816 int unused;
1817 int len;
1818 char_u *p;
1819
1820 if (next == NULL)
1821 return FALSE;
1822
1823 unused = reader->js_end - reader->js_buf - reader->js_used;
1824 if (unused > 0)
1825 {
1826 /* Prepend unused text. */
1827 len = (int)STRLEN(next);
1828 p = alloc(unused + len + 1);
1829 if (p == NULL)
1830 {
1831 vim_free(next);
1832 return FALSE;
1833 }
1834 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1835 mch_memmove(p + unused, next, len + 1);
1836 vim_free(next);
1837 next = p;
1838 }
1839
1840 vim_free(reader->js_buf);
1841 reader->js_buf = next;
1842 reader->js_used = 0;
1843 return TRUE;
1844}
1845
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001846/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001847 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001848 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001849 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001850 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001851 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001852channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001853{
1854 js_read_T reader;
1855 typval_T listtv;
1856 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001857 chanpart_T *chanpart = &channel->ch_part[part];
1858 jsonq_T *head = &chanpart->ch_json_head;
1859 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001860 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001861
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001862 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001863 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001864
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001865 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001866 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001867 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001868 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001869 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001870
1871 /* When a message is incomplete we wait for a short while for more to
1872 * arrive. After the delay drop the input, otherwise a truncated string
1873 * or list will make us hang. */
1874 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001875 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001876 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001877 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001878 /* Only accept the response when it is a list with at least two
1879 * items. */
1880 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001881 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001882 if (listtv.v_type != VAR_LIST)
1883 ch_error(channel, "Did not receive a list, discarding");
1884 else
1885 ch_errorn(channel, "Expected list with two items, got %d",
1886 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001887 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001888 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001889 else
1890 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001891 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1892 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001893 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 else
1895 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001896 item->jq_value = alloc_tv();
1897 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001898 {
1899 vim_free(item);
1900 clear_tv(&listtv);
1901 }
1902 else
1903 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001904 *item->jq_value = listtv;
1905 item->jq_prev = head->jq_prev;
1906 head->jq_prev = item;
1907 item->jq_next = NULL;
1908 if (item->jq_prev == NULL)
1909 head->jq_next = item;
1910 else
1911 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001912 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001913 }
1914 }
1915 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001916
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001917 if (status == OK)
1918 chanpart->ch_waiting = FALSE;
1919 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001920 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001921 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001922 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001923 /* First time encountering incomplete message, set a deadline of
1924 * 100 msec. */
1925 ch_log(channel, "Incomplete message - wait for more");
1926 reader.js_used = 0;
1927 chanpart->ch_waiting = TRUE;
1928#ifdef WIN32
1929 chanpart->ch_deadline = GetTickCount() + 100L;
1930#else
1931 gettimeofday(&chanpart->ch_deadline, NULL);
1932 chanpart->ch_deadline.tv_usec += 100 * 1000;
1933 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1934 {
1935 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1936 ++chanpart->ch_deadline.tv_sec;
1937 }
1938#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001939 }
1940 else
1941 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001942 int timeout;
1943#ifdef WIN32
1944 timeout = GetTickCount() > chanpart->ch_deadline;
1945#else
1946 {
1947 struct timeval now_tv;
1948
1949 gettimeofday(&now_tv, NULL);
1950 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1951 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1952 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1953 }
1954#endif
1955 if (timeout)
1956 {
1957 status = FAIL;
1958 chanpart->ch_waiting = FALSE;
1959 }
1960 else
1961 {
1962 reader.js_used = 0;
1963 ch_log(channel, "still waiting on incomplete message");
1964 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001965 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001966 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001967
1968 if (status == FAIL)
1969 {
1970 ch_error(channel, "Decoding failed - discarding input");
1971 ret = FALSE;
1972 chanpart->ch_waiting = FALSE;
1973 }
1974 else if (reader.js_buf[reader.js_used] != NUL)
1975 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001976 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001977 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001978 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1979 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001980 ret = status == MAYBE ? FALSE: TRUE;
1981 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001982 else
1983 ret = FALSE;
1984
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001985 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001986 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001987}
1988
1989/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01001990 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01001991 */
1992 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01001993remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01001994{
Bram Moolenaar77073442016-02-13 23:23:53 +01001995 if (node->cq_prev == NULL)
1996 head->cq_next = node->cq_next;
1997 else
1998 node->cq_prev->cq_next = node->cq_next;
1999 if (node->cq_next == NULL)
2000 head->cq_prev = node->cq_prev;
2001 else
2002 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002003}
2004
2005/*
2006 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002007 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002008 */
2009 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002010remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002011{
Bram Moolenaar77073442016-02-13 23:23:53 +01002012 if (node->jq_prev == NULL)
2013 head->jq_next = node->jq_next;
2014 else
2015 node->jq_prev->jq_next = node->jq_next;
2016 if (node->jq_next == NULL)
2017 head->jq_prev = node->jq_prev;
2018 else
2019 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002020 vim_free(node);
2021}
2022
2023/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002024 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002025 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002026 * When "id" is zero or negative jut get the first message. But not the one
2027 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002028 * Return OK when found and return the value in "rettv".
2029 * Return FAIL otherwise.
2030 */
2031 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002032channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002033{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002034 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002035 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002036
Bram Moolenaar77073442016-02-13 23:23:53 +01002037 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002038 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002039 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002040 typval_T *tv = &l->lv_first->li_tv;
2041
2042 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002043 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002044 || tv->vval.v_number == 0
2045 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002046 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002047 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002048 if (tv->v_type == VAR_NUMBER)
2049 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002051 return OK;
2052 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002053 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002054 }
2055 return FAIL;
2056}
2057
Bram Moolenaarece61b02016-02-20 21:39:05 +01002058#define CH_JSON_MAX_ARGS 4
2059
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002060/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002061 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002062 * "argv[0]" is the command string.
2063 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002064 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002065 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002066channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002067{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002068 char_u *cmd = argv[0].vval.v_string;
2069 char_u *arg;
2070 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002071
Bram Moolenaarece61b02016-02-20 21:39:05 +01002072 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002073 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002074 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002075 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002076 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002077 return;
2078 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002079 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002080 if (arg == NULL)
2081 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002082
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002083 if (STRCMP(cmd, "ex") == 0)
2084 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002085 int save_called_emsg = called_emsg;
2086
2087 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002088 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002089 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002090 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002091 --emsg_silent;
2092 if (called_emsg)
2093 ch_logs(channel, "Ex command error: '%s'",
2094 (char *)get_vim_var_str(VV_ERRMSG));
2095 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002096 }
2097 else if (STRCMP(cmd, "normal") == 0)
2098 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002099 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002100
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002101 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002102 ea.arg = arg;
2103 ea.addr_count = 0;
2104 ea.forceit = TRUE; /* no mapping */
2105 ex_normal(&ea);
2106 }
2107 else if (STRCMP(cmd, "redraw") == 0)
2108 {
2109 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002110
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002111 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002112 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002113 ex_redraw(&ea);
2114 showruler(FALSE);
2115 setcursor();
2116 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002117#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002118 if (gui.in_use)
2119 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002120 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002121 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002122 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002123#endif
2124 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002125 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002126 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002127 int is_call = cmd[0] == 'c';
2128 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002129
Bram Moolenaarece61b02016-02-20 21:39:05 +01002130 if (argv[id_idx].v_type != VAR_UNKNOWN
2131 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002132 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002133 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002134 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002135 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002136 }
2137 else if (is_call && argv[2].v_type != VAR_LIST)
2138 {
2139 ch_error(channel, "third argument for call must be a list");
2140 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002141 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002142 }
2143 else
2144 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002145 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002146 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002147 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002148 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002149
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002150 /* Don't pollute the display with errors. */
2151 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002152 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002153 {
2154 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002155 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002156 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002157 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002158 {
2159 ch_logs(channel, "Calling '%s'", (char *)arg);
2160 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2161 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002162 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002163
2164 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002165 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002166 int id = argv[id_idx].vval.v_number;
2167
Bram Moolenaar55fab432016-02-07 16:53:13 +01002168 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002169 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002170 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002171 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002172 /* If evaluation failed or the result can't be encoded
2173 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002174 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002175 err_tv.v_type = VAR_STRING;
2176 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002177 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002178 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002179 if (json != NULL)
2180 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002181 channel_send(channel,
2182 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002183 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002184 vim_free(json);
2185 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002186 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002187 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002188 if (tv == &res_tv)
2189 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002190 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002191 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002192 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002193 }
2194 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002195 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002196 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002197 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002198 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002199}
2200
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002201/*
2202 * Invoke the callback at "cbhead".
2203 * Does not redraw but sets channel_need_redraw.
2204 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002205 static void
2206invoke_one_time_callback(
2207 channel_T *channel,
2208 cbq_T *cbhead,
2209 cbq_T *item,
2210 typval_T *argv)
2211{
2212 ch_logs(channel, "Invoking one-time callback %s",
2213 (char *)item->cq_callback);
2214 /* Remove the item from the list first, if the callback
2215 * invokes ch_close() the list will be cleared. */
2216 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002217 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002218 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002219 vim_free(item);
2220}
2221
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002222 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002223append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002224{
2225 buf_T *save_curbuf = curbuf;
2226 linenr_T lnum = buffer->b_ml.ml_line_count;
2227 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002228 chanpart_T *ch_part = &channel->ch_part[part];
2229 int save_p_ma = buffer->b_p_ma;
2230
2231 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2232 {
2233 if (!ch_part->ch_nomod_error)
2234 {
2235 ch_error(channel, "Buffer is not modifiable, cannot append");
2236 ch_part->ch_nomod_error = TRUE;
2237 }
2238 return;
2239 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002240
2241 /* If the buffer is also used as input insert above the last
2242 * line. Don't write these lines. */
2243 if (save_write_to)
2244 {
2245 --lnum;
2246 buffer->b_write_to_channel = FALSE;
2247 }
2248
2249 /* Append to the buffer */
2250 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2251
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002252 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002253 curbuf = buffer;
2254 u_sync(TRUE);
2255 /* ignore undo failure, undo is not very useful here */
2256 ignored = u_save(lnum, lnum + 1);
2257
2258 ml_append(lnum, msg, 0, FALSE);
2259 appended_lines_mark(lnum, 1L);
2260 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002261 if (ch_part->ch_nomodifiable)
2262 buffer->b_p_ma = FALSE;
2263 else
2264 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002265
2266 if (buffer->b_nwindows > 0)
2267 {
2268 win_T *wp;
2269 win_T *save_curwin;
2270
2271 FOR_ALL_WINDOWS(wp)
2272 {
2273 if (wp->w_buffer == buffer
2274 && (save_write_to
2275 ? wp->w_cursor.lnum == lnum + 1
2276 : (wp->w_cursor.lnum == lnum
2277 && wp->w_cursor.col == 0)))
2278 {
2279 ++wp->w_cursor.lnum;
2280 save_curwin = curwin;
2281 curwin = wp;
2282 curbuf = curwin->w_buffer;
2283 scroll_cursor_bot(0, FALSE);
2284 curwin = save_curwin;
2285 curbuf = curwin->w_buffer;
2286 }
2287 }
2288 redraw_buf_later(buffer, VALID);
2289 channel_need_redraw = TRUE;
2290 }
2291
2292 if (save_write_to)
2293 {
2294 channel_T *ch;
2295
2296 /* Find channels reading from this buffer and adjust their
2297 * next-to-read line number. */
2298 buffer->b_write_to_channel = TRUE;
2299 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2300 {
2301 chanpart_T *in_part = &ch->ch_part[PART_IN];
2302
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002303 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002304 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2305 }
2306 }
2307}
2308
Bram Moolenaar437905c2016-04-26 19:01:05 +02002309 static void
2310drop_messages(channel_T *channel, int part)
2311{
2312 char_u *msg;
2313
2314 while ((msg = channel_get(channel, part)) != NULL)
2315 {
2316 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2317 vim_free(msg);
2318 }
2319}
2320
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002321/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002322 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002323 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002324 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002325 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002326 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002327may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002328{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002329 char_u *msg = NULL;
2330 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002332 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002333 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002334 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002335 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002336 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002337 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002338 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002339 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002340
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002341 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002342 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002343 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002344
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002345 /* Use a message-specific callback, part callback or channel callback */
2346 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2347 if (cbitem->cq_seq_nr == 0)
2348 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002349 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002350 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002351 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002352 partial = cbitem->cq_partial;
2353 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002354 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002355 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002356 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002357 partial = channel->ch_part[part].ch_partial;
2358 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002359 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002360 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002361 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002362 partial = channel->ch_partial;
2363 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002364
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002365 buffer = channel->ch_part[part].ch_bufref.br_buf;
2366 if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002367 {
2368 /* buffer was wiped out */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002369 channel->ch_part[part].ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002370 buffer = NULL;
2371 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002372
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002373 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002374 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002375 listitem_T *item;
2376 int argc = 0;
2377
Bram Moolenaard7ece102016-02-02 23:23:02 +01002378 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002379 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002380 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002381 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002382 channel_parse_json(channel, part);
2383 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002384 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002385 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002386
Bram Moolenaarece61b02016-02-20 21:39:05 +01002387 for (item = listtv->vval.v_list->lv_first;
2388 item != NULL && argc < CH_JSON_MAX_ARGS;
2389 item = item->li_next)
2390 argv[argc++] = item->li_tv;
2391 while (argc < CH_JSON_MAX_ARGS)
2392 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002393
Bram Moolenaarece61b02016-02-20 21:39:05 +01002394 if (argv[0].v_type == VAR_STRING)
2395 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002396 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002397 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002398 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002399 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002400 }
2401
Bram Moolenaarece61b02016-02-20 21:39:05 +01002402 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002403 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002404 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002405 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002406 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002407 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002408 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002409 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002410 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002411 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002412 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002413 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002414 return FALSE;
2415 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002416 else
2417 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002418 /* If there is no callback or buffer drop the message. */
2419 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002420 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002421 /* If there is a close callback it may use ch_read() to get the
2422 * messages. */
2423 if (channel->ch_close_cb == NULL)
2424 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002425 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002426 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002427
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002428 if (ch_mode == MODE_NL)
2429 {
2430 char_u *nl;
2431 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002432 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002433
2434 /* See if we have a message ending in NL in the first buffer. If
2435 * not try to concatenate the first and the second buffer. */
2436 while (TRUE)
2437 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002438 node = channel_peek(channel, part);
2439 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002440 if (nl != NULL)
2441 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002442 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002443 return FALSE; /* incomplete message */
2444 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002445 buf = node->rq_buffer;
2446
2447 /* Convert NUL to NL, the internal representation. */
2448 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2449 if (*p == NUL)
2450 *p = NL;
2451
2452 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002453 {
2454 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002455 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002456 *nl = NUL;
2457 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002458 else
2459 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002460 /* Copy the message into allocated memory (excluding the NL)
2461 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002462 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002463 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002464 }
2465 }
2466 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002467 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002468 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002469 * get everything we have.
2470 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002471 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002472 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002473
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002474 if (msg == NULL)
2475 return FALSE; /* out of memory (and avoids Coverity warning) */
2476
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002477 argv[1].v_type = VAR_STRING;
2478 argv[1].vval.v_string = msg;
2479 }
2480
Bram Moolenaara07fec92016-02-05 21:04:08 +01002481 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002482 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002483 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002484
2485 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002486 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002487 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002488 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002489 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002490 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002491 break;
2492 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002493 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002494 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002495 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002496 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002497 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002498 if (buffer != NULL)
2499 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002500 if (msg == NULL)
2501 /* JSON or JS mode: re-encode the message. */
2502 msg = json_encode(listtv, ch_mode);
2503 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002504 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002505 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002506
Bram Moolenaar187db502016-02-27 14:44:26 +01002507 if (callback != NULL)
2508 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002509 if (cbitem != NULL)
2510 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2511 else
2512 {
2513 /* invoke the channel callback */
2514 ch_logs(channel, "Invoking channel callback %s",
2515 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002516 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002517 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002518 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002519 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002520 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002521 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002522
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002523 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002524 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002525 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002526
2527 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002528}
2529
2530/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002531 * Return TRUE when channel "channel" is open for writing to.
2532 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002533 */
2534 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002535channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002536{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002537 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002538 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002539}
2540
2541/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002542 * Return TRUE when channel "channel" is open for reading or writing.
2543 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002544 */
2545 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002546channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002547{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002548 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002549 || channel->CH_IN_FD != INVALID_FD
2550 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002551 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002552}
2553
2554/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002555 * Return TRUE if "channel" has JSON or other typeahead.
2556 */
2557 static int
2558channel_has_readahead(channel_T *channel, int part)
2559{
2560 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2561
2562 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2563 {
2564 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2565 jsonq_T *item = head->jq_next;
2566
2567 return item != NULL;
2568 }
2569 return channel_peek(channel, part) != NULL;
2570}
2571
2572/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002573 * Return a string indicating the status of the channel.
2574 */
2575 char *
2576channel_status(channel_T *channel)
2577{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002578 int part;
2579 int has_readahead = FALSE;
2580
Bram Moolenaar77073442016-02-13 23:23:53 +01002581 if (channel == NULL)
2582 return "fail";
2583 if (channel_is_open(channel))
2584 return "open";
Bram Moolenaar437905c2016-04-26 19:01:05 +02002585 for (part = PART_SOCK; part <= PART_ERR; ++part)
2586 if (channel_has_readahead(channel, part))
2587 {
2588 has_readahead = TRUE;
2589 break;
2590 }
2591
2592 if (has_readahead)
2593 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002594 return "closed";
2595}
2596
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002597 static void
2598channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2599{
2600 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002601 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002602 size_t tail;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002603 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002604
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002605 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002606 STRCAT(namebuf, "_");
2607 tail = STRLEN(namebuf);
2608
2609 STRCPY(namebuf + tail, "status");
2610 dict_add_nr_str(dict, namebuf, 0,
2611 (char_u *)(chanpart->ch_fd == INVALID_FD ? "closed" : "open"));
2612
2613 STRCPY(namebuf + tail, "mode");
2614 switch (chanpart->ch_mode)
2615 {
2616 case MODE_NL: s = "NL"; break;
2617 case MODE_RAW: s = "RAW"; break;
2618 case MODE_JSON: s = "JSON"; break;
2619 case MODE_JS: s = "JS"; break;
2620 }
2621 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2622
2623 STRCPY(namebuf + tail, "io");
2624 if (part == PART_SOCK)
2625 s = "socket";
2626 else switch (chanpart->ch_io)
2627 {
2628 case JIO_NULL: s = "null"; break;
2629 case JIO_PIPE: s = "pipe"; break;
2630 case JIO_FILE: s = "file"; break;
2631 case JIO_BUFFER: s = "buffer"; break;
2632 case JIO_OUT: s = "out"; break;
2633 }
2634 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2635
2636 STRCPY(namebuf + tail, "timeout");
2637 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2638}
2639
2640 void
2641channel_info(channel_T *channel, dict_T *dict)
2642{
2643 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
2644 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel));
2645
2646 if (channel->ch_hostname != NULL)
2647 {
2648 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2649 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2650 channel_part_info(channel, dict, "sock", PART_SOCK);
2651 }
2652 else
2653 {
2654 channel_part_info(channel, dict, "out", PART_OUT);
2655 channel_part_info(channel, dict, "err", PART_ERR);
2656 channel_part_info(channel, dict, "in", PART_IN);
2657 }
2658}
2659
Bram Moolenaar77073442016-02-13 23:23:53 +01002660/*
2661 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002662 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002663 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002664 */
2665 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002666channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002667{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002668 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002669
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002670#ifdef FEAT_GUI
2671 channel_gui_unregister(channel);
2672#endif
2673
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002674 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002675 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002676 sock_close(channel->CH_SOCK_FD);
2677 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002678 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002679 may_close_part(&channel->CH_IN_FD);
2680 may_close_part(&channel->CH_OUT_FD);
2681 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002682
Bram Moolenaar8b374212016-02-24 20:43:06 +01002683 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002684 {
2685 typval_T argv[1];
2686 typval_T rettv;
2687 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002688 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002689
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002690 /* Invoke callbacks before the close callback, since it's weird to
2691 * first invoke the close callback. Increment the refcount to avoid
2692 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002693 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002694 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002695 for (part = PART_SOCK; part <= PART_ERR; ++part)
2696 while (may_invoke_callback(channel, part))
2697 ;
2698
2699 /* Invoke the close callback, if still set. */
2700 if (channel->ch_close_cb != NULL)
2701 {
2702 ch_logs(channel, "Invoking close callback %s",
2703 (char *)channel->ch_close_cb);
2704 argv[0].v_type = VAR_CHANNEL;
2705 argv[0].vval.v_channel = channel;
2706 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002707 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002708 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002709 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002710 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002711 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002712
2713 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002714 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002715 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002716 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002717
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002718 --channel->ch_refcount;
2719
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002720 if (channel_need_redraw)
2721 {
2722 channel_need_redraw = FALSE;
2723 redraw_after_callback();
2724 }
2725
Bram Moolenaar437905c2016-04-26 19:01:05 +02002726 /* any remaining messages are useless now */
2727 for (part = PART_SOCK; part <= PART_ERR; ++part)
2728 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002729 }
2730
2731 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002732}
2733
Bram Moolenaard04a0202016-01-26 23:30:18 +01002734/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002735 * Close the "in" part channel "channel".
2736 */
2737 void
2738channel_close_in(channel_T *channel)
2739{
2740 may_close_part(&channel->CH_IN_FD);
2741}
2742
2743/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002744 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002745 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002746 static void
2747channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002748{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002749 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2750 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002751
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002752 while (channel_peek(channel, part) != NULL)
2753 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002754
2755 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002756 {
2757 cbq_T *node = cb_head->cq_next;
2758
2759 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002760 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002761 vim_free(node);
2762 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002763
2764 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002765 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002766 free_tv(json_head->jq_next->jq_value);
2767 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002768 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002769
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002770 free_callback(channel->ch_part[part].ch_callback,
2771 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002772 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002773 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002774}
2775
2776/*
2777 * Clear all the read buffers on "channel".
2778 */
2779 void
2780channel_clear(channel_T *channel)
2781{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002782 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002783 vim_free(channel->ch_hostname);
2784 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002785 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002786 channel_clear_one(channel, PART_OUT);
2787 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002788 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002789 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002790 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002791 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002792 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002793 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002794 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002795}
2796
Bram Moolenaar77073442016-02-13 23:23:53 +01002797#if defined(EXITFREE) || defined(PROTO)
2798 void
2799channel_free_all(void)
2800{
2801 channel_T *channel;
2802
Bram Moolenaard6051b52016-02-28 15:49:03 +01002803 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002804 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2805 channel_clear(channel);
2806}
2807#endif
2808
2809
Bram Moolenaar715d2852016-04-30 17:06:31 +02002810/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002811#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002812
2813/* Buffer size for reading incoming messages. */
2814#define MAXMSGSIZE 4096
2815
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002816#if defined(HAVE_SELECT)
2817/*
2818 * Add write fds where we are waiting for writing to be possible.
2819 */
2820 static int
2821channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2822{
2823 int maxfd = maxfd_arg;
2824 channel_T *ch;
2825
2826 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2827 {
2828 chanpart_T *in_part = &ch->ch_part[PART_IN];
2829
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002830 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002831 {
2832 FD_SET((int)in_part->ch_fd, wfds);
2833 if ((int)in_part->ch_fd >= maxfd)
2834 maxfd = (int)in_part->ch_fd + 1;
2835 }
2836 }
2837 return maxfd;
2838}
2839#else
2840/*
2841 * Add write fds where we are waiting for writing to be possible.
2842 */
2843 static int
2844channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2845{
2846 int nfd = nfd_in;
2847 channel_T *ch;
2848
2849 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2850 {
2851 chanpart_T *in_part = &ch->ch_part[PART_IN];
2852
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002853 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002854 {
2855 in_part->ch_poll_idx = nfd;
2856 fds[nfd].fd = in_part->ch_fd;
2857 fds[nfd].events = POLLOUT;
2858 ++nfd;
2859 }
2860 else
2861 in_part->ch_poll_idx = -1;
2862 }
2863 return nfd;
2864}
2865#endif
2866
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002867typedef enum {
2868 CW_READY,
2869 CW_NOT_READY,
2870 CW_ERROR
2871} channel_wait_result;
2872
Bram Moolenaard04a0202016-01-26 23:30:18 +01002873/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002874 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002875 * Return CW_READY when there is something to read.
2876 * Return CW_NOT_READY when there is nothing to read.
2877 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002878 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002879 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002880channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002881{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002882 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002883 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002884
Bram Moolenaard8070362016-02-15 21:56:54 +01002885# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002886 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002887 {
2888 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002889 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002890 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002891 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002892
2893 /* reading from a pipe, not a socket */
2894 while (TRUE)
2895 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002896 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2897
2898 if (r && nread > 0)
2899 return CW_READY;
2900 if (r == 0)
2901 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002902
2903 /* perhaps write some buffer lines */
2904 channel_write_any_lines();
2905
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002906 sleep_time = deadline - GetTickCount();
2907 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002908 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002909 /* Wait for a little while. Very short at first, up to 10 msec
2910 * after looping a few times. */
2911 if (sleep_time > delay)
2912 sleep_time = delay;
2913 Sleep(sleep_time);
2914 delay = delay * 2;
2915 if (delay > 10)
2916 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002917 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002918 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002919 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002920#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002921 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002922#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002923 struct timeval tval;
2924 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002925 fd_set wfds;
2926 int ret;
2927 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002928
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002929 tval.tv_sec = timeout / 1000;
2930 tval.tv_usec = (timeout % 1000) * 1000;
2931 for (;;)
2932 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002933 FD_ZERO(&rfds);
2934 FD_SET((int)fd, &rfds);
2935
2936 /* Write lines to a pipe when a pipe can be written to. Need to
2937 * set this every time, some buffers may be done. */
2938 maxfd = (int)fd + 1;
2939 FD_ZERO(&wfds);
2940 maxfd = channel_fill_wfds(maxfd, &wfds);
2941
2942 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002943# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002944 SOCK_ERRNO;
2945 if (ret == -1 && errno == EINTR)
2946 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002947# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002948 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002949 {
2950 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002951 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002952 channel_write_any_lines();
2953 continue;
2954 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002955 break;
2956 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01002957#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002958 for (;;)
2959 {
2960 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
2961 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002962
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002963 fds[0].fd = fd;
2964 fds[0].events = POLLIN;
2965 nfd = channel_fill_poll_write(nfd, fds);
2966 if (poll(fds, nfd, timeout) > 0)
2967 {
2968 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002969 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002970 channel_write_any_lines();
2971 continue;
2972 }
2973 break;
2974 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002975#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002976 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002977 return CW_NOT_READY;
2978}
2979
2980 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02002981channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002982{
2983 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02002984 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
2985 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002986
2987 /* Queue a "DETACH" netbeans message in the command queue in order to
2988 * terminate the netbeans session later. Do not end the session here
2989 * directly as we may be running in the context of a call to
2990 * netbeans_parse_messages():
2991 * netbeans_parse_messages
2992 * -> autocmd triggered while processing the netbeans cmd
2993 * -> ui_breakcheck
2994 * -> gui event loop or select loop
2995 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02002996 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002997 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02002998 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02002999 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003000 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3001
3002 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003003 * died. Don't close the channel right away, it may be the wrong moment
3004 * to invoke callbacks. */
3005 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003006
3007#ifdef FEAT_GUI
3008 /* Stop listening to GUI events right away. */
3009 channel_gui_unregister(channel);
3010#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003011}
3012
3013 static void
3014channel_close_now(channel_T *channel)
3015{
3016 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003017 channel_close(channel, TRUE);
3018 if (channel->ch_nb_close_cb != NULL)
3019 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003020}
3021
3022/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003023 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003024 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003025 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003026 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003027 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003028channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003029{
3030 static char_u *buf = NULL;
3031 int len = 0;
3032 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003033 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003034 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003035
Bram Moolenaar5850a762016-05-27 19:59:48 +02003036 /* If we detected a read error don't try reading again. */
3037 if (channel->ch_to_be_closed)
3038 return;
3039
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003040 fd = channel->ch_part[part].ch_fd;
3041 if (fd == INVALID_FD)
3042 {
3043 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003044 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003045 }
3046 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003047
3048 /* Allocate a buffer to read into. */
3049 if (buf == NULL)
3050 {
3051 buf = alloc(MAXMSGSIZE);
3052 if (buf == NULL)
3053 return; /* out of memory! */
3054 }
3055
3056 /* Keep on reading for as long as there is something to read.
3057 * Use select() or poll() to avoid blocking on a message that is exactly
3058 * MAXMSGSIZE long. */
3059 for (;;)
3060 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003061 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003062 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003063 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003064 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003065 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003066 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003067 if (len <= 0)
3068 break; /* error or nothing more to read */
3069
3070 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003071 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003072 readlen += len;
3073 if (len < MAXMSGSIZE)
3074 break; /* did read everything that's available */
3075 }
3076
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003077 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003078 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003079 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003080
3081#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003082 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003083 if (CH_HAS_GUI && gtk_main_level() > 0)
3084 gtk_main_quit();
3085#endif
3086}
3087
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003088/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003089 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003090 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003091 * Returns what was read in allocated memory.
3092 * Returns NULL in case of error or timeout.
3093 */
3094 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003095channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003096{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003097 char_u *buf;
3098 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003099 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003100 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003101 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003102 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003103
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003104 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003105 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003106
3107 while (TRUE)
3108 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003109 node = channel_peek(channel, part);
3110 if (node != NULL)
3111 {
3112 if (mode == MODE_RAW || (mode == MODE_NL
3113 && channel_first_nl(node) != NULL))
3114 /* got a complete message */
3115 break;
3116 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3117 continue;
3118 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003119
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003120 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003121 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003122 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003123 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003124 {
3125 ch_log(channel, "Timed out");
3126 return NULL;
3127 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003128 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003129 }
3130
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003131 if (mode == MODE_RAW)
3132 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003133 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003134 }
3135 else
3136 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003137 char_u *p;
3138
3139 buf = node->rq_buffer;
3140 nl = channel_first_nl(node);
3141
3142 /* Convert NUL to NL, the internal representation. */
3143 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3144 if (*p == NUL)
3145 *p = NL;
3146
3147 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003148 {
3149 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003150 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003151 *nl = NUL;
3152 }
3153 else
3154 {
3155 /* Copy the message into allocated memory and remove it from the
3156 * buffer. */
3157 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003158 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003159 }
3160 }
3161 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003162 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003163 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003164}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003165
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003166/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003167 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003168 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003169 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003170 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003171 */
3172 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003173channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003174 channel_T *channel,
3175 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003176 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003177 int id,
3178 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003179{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003180 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003181 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003182 int timeout;
3183 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003184
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003185 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003186 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003187 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003188 for (;;)
3189 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003190 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003191
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003192 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003193 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003194 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003195 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003196 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003197 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003198
Bram Moolenaard7ece102016-02-02 23:23:02 +01003199 if (!more)
3200 {
3201 /* Handle any other messages in the queue. If done some more
3202 * messages may have arrived. */
3203 if (channel_parse_messages())
3204 continue;
3205
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003206 /* Wait for up to the timeout. If there was an incomplete message
3207 * use the deadline for that. */
3208 timeout = timeout_arg;
3209 if (chanpart->ch_waiting)
3210 {
3211#ifdef WIN32
3212 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3213#else
3214 {
3215 struct timeval now_tv;
3216
3217 gettimeofday(&now_tv, NULL);
3218 timeout = (chanpart->ch_deadline.tv_sec
3219 - now_tv.tv_sec) * 1000
3220 + (chanpart->ch_deadline.tv_usec
3221 - now_tv.tv_usec) / 1000
3222 + 1;
3223 }
3224#endif
3225 if (timeout < 0)
3226 {
3227 /* Something went wrong, channel_parse_json() didn't
3228 * discard message. Cancel waiting. */
3229 chanpart->ch_waiting = FALSE;
3230 timeout = timeout_arg;
3231 }
3232 else if (timeout > timeout_arg)
3233 timeout = timeout_arg;
3234 }
3235 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003236 if (fd == INVALID_FD
3237 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003238 {
3239 if (timeout == timeout_arg)
3240 {
3241 if (fd != INVALID_FD)
3242 ch_log(channel, "Timed out");
3243 break;
3244 }
3245 }
3246 else
3247 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003248 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003249 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003250 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003251 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003252}
3253
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003254/*
3255 * Common for ch_read() and ch_readraw().
3256 */
3257 void
3258common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3259{
3260 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003261 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003262 jobopt_T opt;
3263 int mode;
3264 int timeout;
3265 int id = -1;
3266 typval_T *listtv = NULL;
3267
3268 /* return an empty string by default */
3269 rettv->v_type = VAR_STRING;
3270 rettv->vval.v_string = NULL;
3271
3272 clear_job_options(&opt);
3273 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3274 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003275 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003276
Bram Moolenaar437905c2016-04-26 19:01:05 +02003277 if (opt.jo_set & JO_PART)
3278 part = opt.jo_part;
3279 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003280 if (channel != NULL)
3281 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003282 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003283 part = channel_part_read(channel);
3284 mode = channel_get_mode(channel, part);
3285 timeout = channel_get_timeout(channel, part);
3286 if (opt.jo_set & JO_TIMEOUT)
3287 timeout = opt.jo_timeout;
3288
3289 if (raw || mode == MODE_RAW || mode == MODE_NL)
3290 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3291 else
3292 {
3293 if (opt.jo_set & JO_ID)
3294 id = opt.jo_id;
3295 channel_read_json_block(channel, part, timeout, id, &listtv);
3296 if (listtv != NULL)
3297 {
3298 *rettv = *listtv;
3299 vim_free(listtv);
3300 }
3301 else
3302 {
3303 rettv->v_type = VAR_SPECIAL;
3304 rettv->vval.v_number = VVAL_NONE;
3305 }
3306 }
3307 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003308
3309theend:
3310 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003311}
3312
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003313# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3314 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003315/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003316 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003317 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003318 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003319 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003320channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003321{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003322 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003323 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003324
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003325 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003326 for (channel = first_channel; channel != NULL;
3327 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003328 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003329 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003330 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003331 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003332 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003333 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003334 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003335 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003336 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003337}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003338# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003339
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003340# if defined(WIN32) || defined(PROTO)
3341/*
3342 * Check the channels for anything that is ready to be read.
3343 * The data is put in the read queue.
3344 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003345 void
3346channel_handle_events(void)
3347{
3348 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003349 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003350 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003351
3352 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3353 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003354 /* If we detected a read error don't try reading again. */
3355 if (channel->ch_to_be_closed)
3356 continue;
3357
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003358 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003359 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003360 {
3361 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003362 if (fd != INVALID_FD)
3363 {
3364 int r = channel_wait(channel, fd, 0);
3365
3366 if (r == CW_READY)
3367 channel_read(channel, part, "channel_handle_events");
3368 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003369 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003370 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003371 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003372 }
3373}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003374# endif
3375
Bram Moolenaard04a0202016-01-26 23:30:18 +01003376/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003377 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003378 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003379 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003380 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003381 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003382channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003383{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003384 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003385 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003386
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003387 fd = channel->ch_part[part].ch_fd;
3388 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003389 {
3390 if (!channel->ch_error && fun != NULL)
3391 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003392 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003393 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003394 }
3395 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003396 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003397 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003398
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003399 if (log_fd != NULL)
3400 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003401 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003402 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003403 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003404 fprintf(log_fd, "'\n");
3405 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003406 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003407 }
3408
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003409 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003410 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003411 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003412 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003413 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003414 {
3415 if (!channel->ch_error && fun != NULL)
3416 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003417 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003418 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003419 }
3420 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003421 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003422 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003423
3424 channel->ch_error = FALSE;
3425 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003426}
3427
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003428/*
3429 * Common for "ch_sendexpr()" and "ch_sendraw()".
3430 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003431 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003432 * Otherwise returns NULL.
3433 */
3434 channel_T *
3435send_common(
3436 typval_T *argvars,
3437 char_u *text,
3438 int id,
3439 int eval,
3440 jobopt_T *opt,
3441 char *fun,
3442 int *part_read)
3443{
3444 channel_T *channel;
3445 int part_send;
3446
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003447 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003448 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003449 if (channel == NULL)
3450 return NULL;
3451 part_send = channel_part_send(channel);
3452 *part_read = channel_part_read(channel);
3453
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003454 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3455 return NULL;
3456
3457 /* Set the callback. An empty callback means no callback and not reading
3458 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3459 * allowed. */
3460 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3461 {
3462 if (eval)
3463 {
3464 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3465 return NULL;
3466 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003467 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003468 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003469 }
3470
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003471 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003472 && opt->jo_callback == NULL)
3473 return channel;
3474 return NULL;
3475}
3476
3477/*
3478 * common for "ch_evalexpr()" and "ch_sendexpr()"
3479 */
3480 void
3481ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3482{
3483 char_u *text;
3484 typval_T *listtv;
3485 channel_T *channel;
3486 int id;
3487 ch_mode_T ch_mode;
3488 int part_send;
3489 int part_read;
3490 jobopt_T opt;
3491 int timeout;
3492
3493 /* return an empty string by default */
3494 rettv->v_type = VAR_STRING;
3495 rettv->vval.v_string = NULL;
3496
Bram Moolenaar437905c2016-04-26 19:01:05 +02003497 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003498 if (channel == NULL)
3499 return;
3500 part_send = channel_part_send(channel);
3501
3502 ch_mode = channel_get_mode(channel, part_send);
3503 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3504 {
3505 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3506 return;
3507 }
3508
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003509 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003510 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003511 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003512 if (text == NULL)
3513 return;
3514
3515 channel = send_common(argvars, text, id, eval, &opt,
3516 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3517 vim_free(text);
3518 if (channel != NULL && eval)
3519 {
3520 if (opt.jo_set & JO_TIMEOUT)
3521 timeout = opt.jo_timeout;
3522 else
3523 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003524 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3525 == OK)
3526 {
3527 list_T *list = listtv->vval.v_list;
3528
3529 /* Move the item from the list and then change the type to
3530 * avoid the value being freed. */
3531 *rettv = list->lv_last->li_tv;
3532 list->lv_last->li_tv.v_type = VAR_NUMBER;
3533 free_tv(listtv);
3534 }
3535 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003536 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003537}
3538
3539/*
3540 * common for "ch_evalraw()" and "ch_sendraw()"
3541 */
3542 void
3543ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3544{
3545 char_u buf[NUMBUFLEN];
3546 char_u *text;
3547 channel_T *channel;
3548 int part_read;
3549 jobopt_T opt;
3550 int timeout;
3551
3552 /* return an empty string by default */
3553 rettv->v_type = VAR_STRING;
3554 rettv->vval.v_string = NULL;
3555
3556 text = get_tv_string_buf(&argvars[1], buf);
3557 channel = send_common(argvars, text, 0, eval, &opt,
3558 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3559 if (channel != NULL && eval)
3560 {
3561 if (opt.jo_set & JO_TIMEOUT)
3562 timeout = opt.jo_timeout;
3563 else
3564 timeout = channel_get_timeout(channel, part_read);
3565 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3566 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003567 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003568}
3569
Bram Moolenaard04a0202016-01-26 23:30:18 +01003570# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003571/*
3572 * Add open channels to the poll struct.
3573 * Return the adjusted struct index.
3574 * The type of "fds" is hidden to avoid problems with the function proto.
3575 */
3576 int
3577channel_poll_setup(int nfd_in, void *fds_in)
3578{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003579 int nfd = nfd_in;
3580 channel_T *channel;
3581 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003582 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003583
Bram Moolenaar77073442016-02-13 23:23:53 +01003584 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003585 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003586 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003587 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003588 chanpart_T *ch_part = &channel->ch_part[part];
3589
3590 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003591 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003592 ch_part->ch_poll_idx = nfd;
3593 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003594 fds[nfd].events = POLLIN;
3595 nfd++;
3596 }
3597 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003598 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003599 }
3600 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003601
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003602 nfd = channel_fill_poll_write(nfd, fds);
3603
Bram Moolenaare0874f82016-01-24 20:36:41 +01003604 return nfd;
3605}
3606
3607/*
3608 * The type of "fds" is hidden to avoid problems with the function proto.
3609 */
3610 int
3611channel_poll_check(int ret_in, void *fds_in)
3612{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003613 int ret = ret_in;
3614 channel_T *channel;
3615 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003616 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003617 int idx;
3618 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003619
Bram Moolenaar77073442016-02-13 23:23:53 +01003620 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003621 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003622 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003623 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003624 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003625
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003626 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003627 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003628 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003629 --ret;
3630 }
3631 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003632
3633 in_part = &channel->ch_part[PART_IN];
3634 idx = in_part->ch_poll_idx;
3635 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3636 {
3637 if (in_part->ch_buf_append)
3638 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003639 if (in_part->ch_bufref.br_buf != NULL)
3640 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003641 }
3642 else
3643 channel_write_in(channel);
3644 --ret;
3645 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003646 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003647
3648 return ret;
3649}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003650# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003651
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003652# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003653/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003654 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003655 */
3656 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003657channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003658{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003659 int maxfd = maxfd_in;
3660 channel_T *channel;
3661 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003662 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003663 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003664
Bram Moolenaar77073442016-02-13 23:23:53 +01003665 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003666 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003667 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003668 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003669 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003670
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003671 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003672 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003673 FD_SET((int)fd, rfds);
3674 if (maxfd < (int)fd)
3675 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003676 }
3677 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003678 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003679
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003680 maxfd = channel_fill_wfds(maxfd, wfds);
3681
Bram Moolenaare0874f82016-01-24 20:36:41 +01003682 return maxfd;
3683}
3684
3685/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003686 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003687 */
3688 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003689channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003690{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003691 int ret = ret_in;
3692 channel_T *channel;
3693 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003694 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003695 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003696 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003697
Bram Moolenaar77073442016-02-13 23:23:53 +01003698 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003699 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003700 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003701 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003702 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003703
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003704 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003705 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003706 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003707 --ret;
3708 }
3709 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003710
3711 in_part = &channel->ch_part[PART_IN];
3712 if (ret > 0 && in_part->ch_fd != INVALID_FD
3713 && FD_ISSET(in_part->ch_fd, wfds))
3714 {
3715 if (in_part->ch_buf_append)
3716 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003717 if (in_part->ch_bufref.br_buf != NULL)
3718 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003719 }
3720 else
3721 channel_write_in(channel);
3722 --ret;
3723 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003724 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003725
3726 return ret;
3727}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003728# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003729
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003730/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003731 * Execute queued up commands.
3732 * Invoked from the main loop when it's safe to execute received commands.
3733 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003734 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003735 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003736channel_parse_messages(void)
3737{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003738 channel_T *channel = first_channel;
3739 int ret = FALSE;
3740 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003741 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003742
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003743 ++safe_to_invoke_callback;
3744
Bram Moolenaard0b65022016-03-06 21:50:33 +01003745 /* Only do this message when another message was given, otherwise we get
3746 * lots of them. */
3747 if (did_log_msg)
3748 {
3749 ch_log(NULL, "looking for messages on channels");
3750 did_log_msg = FALSE;
3751 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003752 while (channel != NULL)
3753 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003754 if (channel->ch_to_be_closed)
3755 {
3756 channel->ch_to_be_closed = FALSE;
3757 channel_close_now(channel);
3758 /* channel may have been freed, start over */
3759 channel = first_channel;
3760 continue;
3761 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003762 if (channel->ch_to_be_freed)
3763 {
3764 channel_free(channel);
3765 /* channel has been freed, start over */
3766 channel = first_channel;
3767 continue;
3768 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003769 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003770 {
3771 /* channel is no longer useful, free it */
3772 channel_free(channel);
3773 channel = first_channel;
3774 part = PART_SOCK;
3775 continue;
3776 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003777 if (channel->ch_part[part].ch_fd != INVALID_FD
3778 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003779 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003780 /* Increase the refcount, in case the handler causes the channel
3781 * to be unreferenced or closed. */
3782 ++channel->ch_refcount;
3783 r = may_invoke_callback(channel, part);
3784 if (r == OK)
3785 ret = TRUE;
3786 if (channel_unref(channel) || r == OK)
3787 {
3788 /* channel was freed or something was done, start over */
3789 channel = first_channel;
3790 part = PART_SOCK;
3791 continue;
3792 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003793 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003794 if (part < PART_ERR)
3795 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003796 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003797 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003798 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003799 part = PART_SOCK;
3800 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003801 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003802
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003803 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003804 {
3805 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003806 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003807 }
3808
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003809 --safe_to_invoke_callback;
3810
Bram Moolenaard7ece102016-02-02 23:23:02 +01003811 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003812}
3813
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003814/*
3815 * Mark references to lists used in channels.
3816 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003817 int
3818set_ref_in_channel(int copyID)
3819{
Bram Moolenaar77073442016-02-13 23:23:53 +01003820 int abort = FALSE;
3821 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003822 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003823
Bram Moolenaar77073442016-02-13 23:23:53 +01003824 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003825 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003826 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003827 tv.v_type = VAR_CHANNEL;
3828 tv.vval.v_channel = channel;
3829 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003830 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003831 return abort;
3832}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003833
3834/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003835 * Return the "part" to write to for "channel".
3836 */
3837 int
3838channel_part_send(channel_T *channel)
3839{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003840 if (channel->CH_SOCK_FD == INVALID_FD)
3841 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003842 return PART_SOCK;
3843}
3844
3845/*
3846 * Return the default "part" to read from for "channel".
3847 */
3848 int
3849channel_part_read(channel_T *channel)
3850{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003851 if (channel->CH_SOCK_FD == INVALID_FD)
3852 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003853 return PART_SOCK;
3854}
3855
3856/*
3857 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003858 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003859 */
3860 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003861channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003862{
Bram Moolenaar77073442016-02-13 23:23:53 +01003863 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003864 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003865 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003866}
3867
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003868/*
3869 * Return the timeout of "channel"/"part"
3870 */
3871 int
3872channel_get_timeout(channel_T *channel, int part)
3873{
3874 return channel->ch_part[part].ch_timeout;
3875}
3876
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003877 static int
3878handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3879{
3880 char_u *val = get_tv_string(item);
3881
3882 opt->jo_set |= jo;
3883 if (STRCMP(val, "nl") == 0)
3884 *modep = MODE_NL;
3885 else if (STRCMP(val, "raw") == 0)
3886 *modep = MODE_RAW;
3887 else if (STRCMP(val, "js") == 0)
3888 *modep = MODE_JS;
3889 else if (STRCMP(val, "json") == 0)
3890 *modep = MODE_JSON;
3891 else
3892 {
3893 EMSG2(_(e_invarg2), val);
3894 return FAIL;
3895 }
3896 return OK;
3897}
3898
3899 static int
3900handle_io(typval_T *item, int part, jobopt_T *opt)
3901{
3902 char_u *val = get_tv_string(item);
3903
3904 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3905 if (STRCMP(val, "null") == 0)
3906 opt->jo_io[part] = JIO_NULL;
3907 else if (STRCMP(val, "pipe") == 0)
3908 opt->jo_io[part] = JIO_PIPE;
3909 else if (STRCMP(val, "file") == 0)
3910 opt->jo_io[part] = JIO_FILE;
3911 else if (STRCMP(val, "buffer") == 0)
3912 opt->jo_io[part] = JIO_BUFFER;
3913 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3914 opt->jo_io[part] = JIO_OUT;
3915 else
3916 {
3917 EMSG2(_(e_invarg2), val);
3918 return FAIL;
3919 }
3920 return OK;
3921}
3922
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003923/*
3924 * Clear a jobopt_T before using it.
3925 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003926 void
3927clear_job_options(jobopt_T *opt)
3928{
3929 vim_memset(opt, 0, sizeof(jobopt_T));
3930}
3931
3932/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003933 * Free any members of a jobopt_T.
3934 */
3935 void
3936free_job_options(jobopt_T *opt)
3937{
3938 if (opt->jo_partial != NULL)
3939 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003940 else if (opt->jo_callback != NULL)
3941 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003942 if (opt->jo_out_partial != NULL)
3943 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003944 else if (opt->jo_out_cb != NULL)
3945 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003946 if (opt->jo_err_partial != NULL)
3947 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003948 else if (opt->jo_err_cb != NULL)
3949 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003950 if (opt->jo_close_partial != NULL)
3951 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003952 else if (opt->jo_close_cb != NULL)
3953 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003954 if (opt->jo_exit_partial != NULL)
3955 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003956 else if (opt->jo_exit_cb != NULL)
3957 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003958}
3959
3960/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003961 * Get the PART_ number from the first character of an option name.
3962 */
3963 static int
3964part_from_char(int c)
3965{
3966 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
3967}
3968
3969/*
3970 * Get the option entries from the dict in "tv", parse them and put the result
3971 * in "opt".
3972 * Only accept options in "supported".
3973 * If an option value is invalid return FAIL.
3974 */
3975 int
3976get_job_options(typval_T *tv, jobopt_T *opt, int supported)
3977{
3978 typval_T *item;
3979 char_u *val;
3980 dict_T *dict;
3981 int todo;
3982 hashitem_T *hi;
3983 int part;
3984
3985 opt->jo_set = 0;
3986 if (tv->v_type == VAR_UNKNOWN)
3987 return OK;
3988 if (tv->v_type != VAR_DICT)
3989 {
3990 EMSG(_(e_invarg));
3991 return FAIL;
3992 }
3993 dict = tv->vval.v_dict;
3994 if (dict == NULL)
3995 return OK;
3996
3997 todo = (int)dict->dv_hashtab.ht_used;
3998 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
3999 if (!HASHITEM_EMPTY(hi))
4000 {
4001 item = &dict_lookup(hi)->di_tv;
4002
4003 if (STRCMP(hi->hi_key, "mode") == 0)
4004 {
4005 if (!(supported & JO_MODE))
4006 break;
4007 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4008 return FAIL;
4009 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004010 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004011 {
4012 if (!(supported & JO_IN_MODE))
4013 break;
4014 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4015 == FAIL)
4016 return FAIL;
4017 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004018 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004019 {
4020 if (!(supported & JO_OUT_MODE))
4021 break;
4022 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4023 == FAIL)
4024 return FAIL;
4025 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004026 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004027 {
4028 if (!(supported & JO_ERR_MODE))
4029 break;
4030 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4031 == FAIL)
4032 return FAIL;
4033 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004034 else if (STRCMP(hi->hi_key, "in_io") == 0
4035 || STRCMP(hi->hi_key, "out_io") == 0
4036 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004037 {
4038 if (!(supported & JO_OUT_IO))
4039 break;
4040 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4041 return FAIL;
4042 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004043 else if (STRCMP(hi->hi_key, "in_name") == 0
4044 || STRCMP(hi->hi_key, "out_name") == 0
4045 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004046 {
4047 part = part_from_char(*hi->hi_key);
4048
4049 if (!(supported & JO_OUT_IO))
4050 break;
4051 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4052 opt->jo_io_name[part] =
4053 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4054 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004055 else if (STRCMP(hi->hi_key, "in_buf") == 0
4056 || STRCMP(hi->hi_key, "out_buf") == 0
4057 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004058 {
4059 part = part_from_char(*hi->hi_key);
4060
4061 if (!(supported & JO_OUT_IO))
4062 break;
4063 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4064 opt->jo_io_buf[part] = get_tv_number(item);
4065 if (opt->jo_io_buf[part] <= 0)
4066 {
4067 EMSG2(_(e_invarg2), get_tv_string(item));
4068 return FAIL;
4069 }
4070 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4071 {
4072 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4073 return FAIL;
4074 }
4075 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004076 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4077 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4078 {
4079 part = part_from_char(*hi->hi_key);
4080
4081 if (!(supported & JO_OUT_IO))
4082 break;
4083 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4084 opt->jo_modifiable[part] = get_tv_number(item);
4085 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004086 else if (STRCMP(hi->hi_key, "in_top") == 0
4087 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 {
4089 linenr_T *lp;
4090
4091 if (!(supported & JO_OUT_IO))
4092 break;
4093 if (hi->hi_key[3] == 't')
4094 {
4095 lp = &opt->jo_in_top;
4096 opt->jo_set |= JO_IN_TOP;
4097 }
4098 else
4099 {
4100 lp = &opt->jo_in_bot;
4101 opt->jo_set |= JO_IN_BOT;
4102 }
4103 *lp = get_tv_number(item);
4104 if (*lp < 0)
4105 {
4106 EMSG2(_(e_invarg2), get_tv_string(item));
4107 return FAIL;
4108 }
4109 }
4110 else if (STRCMP(hi->hi_key, "channel") == 0)
4111 {
4112 if (!(supported & JO_OUT_IO))
4113 break;
4114 opt->jo_set |= JO_CHANNEL;
4115 if (item->v_type != VAR_CHANNEL)
4116 {
4117 EMSG2(_(e_invarg2), "channel");
4118 return FAIL;
4119 }
4120 opt->jo_channel = item->vval.v_channel;
4121 }
4122 else if (STRCMP(hi->hi_key, "callback") == 0)
4123 {
4124 if (!(supported & JO_CALLBACK))
4125 break;
4126 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004127 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 if (opt->jo_callback == NULL)
4129 {
4130 EMSG2(_(e_invarg2), "callback");
4131 return FAIL;
4132 }
4133 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004134 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004135 {
4136 if (!(supported & JO_OUT_CALLBACK))
4137 break;
4138 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004139 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140 if (opt->jo_out_cb == NULL)
4141 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004142 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004143 return FAIL;
4144 }
4145 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004146 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004147 {
4148 if (!(supported & JO_ERR_CALLBACK))
4149 break;
4150 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004151 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004152 if (opt->jo_err_cb == NULL)
4153 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004154 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004155 return FAIL;
4156 }
4157 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004158 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004159 {
4160 if (!(supported & JO_CLOSE_CALLBACK))
4161 break;
4162 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004163 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004164 if (opt->jo_close_cb == NULL)
4165 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004166 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004167 return FAIL;
4168 }
4169 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004170 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4171 {
4172 if (!(supported & JO_EXIT_CB))
4173 break;
4174 opt->jo_set |= JO_EXIT_CB;
4175 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4176 if (opt->jo_exit_cb == NULL)
4177 {
4178 EMSG2(_(e_invarg2), "exit_cb");
4179 return FAIL;
4180 }
4181 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004182 else if (STRCMP(hi->hi_key, "waittime") == 0)
4183 {
4184 if (!(supported & JO_WAITTIME))
4185 break;
4186 opt->jo_set |= JO_WAITTIME;
4187 opt->jo_waittime = get_tv_number(item);
4188 }
4189 else if (STRCMP(hi->hi_key, "timeout") == 0)
4190 {
4191 if (!(supported & JO_TIMEOUT))
4192 break;
4193 opt->jo_set |= JO_TIMEOUT;
4194 opt->jo_timeout = get_tv_number(item);
4195 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004196 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004197 {
4198 if (!(supported & JO_OUT_TIMEOUT))
4199 break;
4200 opt->jo_set |= JO_OUT_TIMEOUT;
4201 opt->jo_out_timeout = get_tv_number(item);
4202 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004203 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004204 {
4205 if (!(supported & JO_ERR_TIMEOUT))
4206 break;
4207 opt->jo_set |= JO_ERR_TIMEOUT;
4208 opt->jo_err_timeout = get_tv_number(item);
4209 }
4210 else if (STRCMP(hi->hi_key, "part") == 0)
4211 {
4212 if (!(supported & JO_PART))
4213 break;
4214 opt->jo_set |= JO_PART;
4215 val = get_tv_string(item);
4216 if (STRCMP(val, "err") == 0)
4217 opt->jo_part = PART_ERR;
4218 else
4219 {
4220 EMSG2(_(e_invarg2), val);
4221 return FAIL;
4222 }
4223 }
4224 else if (STRCMP(hi->hi_key, "id") == 0)
4225 {
4226 if (!(supported & JO_ID))
4227 break;
4228 opt->jo_set |= JO_ID;
4229 opt->jo_id = get_tv_number(item);
4230 }
4231 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4232 {
4233 if (!(supported & JO_STOPONEXIT))
4234 break;
4235 opt->jo_set |= JO_STOPONEXIT;
4236 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4237 opt->jo_soe_buf);
4238 if (opt->jo_stoponexit == NULL)
4239 {
4240 EMSG2(_(e_invarg2), "stoponexit");
4241 return FAIL;
4242 }
4243 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004244 else if (STRCMP(hi->hi_key, "block_write") == 0)
4245 {
4246 if (!(supported & JO_BLOCK_WRITE))
4247 break;
4248 opt->jo_set |= JO_BLOCK_WRITE;
4249 opt->jo_block_write = get_tv_number(item);
4250 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004251 else
4252 break;
4253 --todo;
4254 }
4255 if (todo > 0)
4256 {
4257 EMSG2(_(e_invarg2), hi->hi_key);
4258 return FAIL;
4259 }
4260
4261 return OK;
4262}
4263
4264/*
4265 * Get the channel from the argument.
4266 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004267 * When "check_open" is TRUE check that the channel can be used.
4268 * When "reading" is TRUE "check_open" considers typeahead useful.
4269 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004270 */
4271 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004272get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004273{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004274 channel_T *channel = NULL;
4275 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004276
4277 if (tv->v_type == VAR_JOB)
4278 {
4279 if (tv->vval.v_job != NULL)
4280 channel = tv->vval.v_job->jv_channel;
4281 }
4282 else if (tv->v_type == VAR_CHANNEL)
4283 {
4284 channel = tv->vval.v_channel;
4285 }
4286 else
4287 {
4288 EMSG2(_(e_invarg2), get_tv_string(tv));
4289 return NULL;
4290 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004291 if (channel != NULL && reading)
4292 has_readahead = channel_has_readahead(channel,
4293 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004294
Bram Moolenaar437905c2016-04-26 19:01:05 +02004295 if (check_open && (channel == NULL || (!channel_is_open(channel)
4296 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004297 {
4298 EMSG(_("E906: not an open channel"));
4299 return NULL;
4300 }
4301 return channel;
4302}
4303
4304static job_T *first_job = NULL;
4305
4306 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004307job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004308{
4309 ch_log(job->jv_channel, "Freeing job");
4310 if (job->jv_channel != NULL)
4311 {
4312 /* The link from the channel to the job doesn't count as a reference,
4313 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004314 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004315 * NULL the reference. We don't set ch_job_killed, unreferencing the
4316 * job doesn't mean it stops running. */
4317 job->jv_channel->ch_job = NULL;
4318 channel_unref(job->jv_channel);
4319 }
4320 mch_clear_job(job);
4321
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004323 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004324}
4325
4326 static void
4327job_free_job(job_T *job)
4328{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004329 if (job->jv_next != NULL)
4330 job->jv_next->jv_prev = job->jv_prev;
4331 if (job->jv_prev == NULL)
4332 first_job = job->jv_next;
4333 else
4334 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004335 vim_free(job);
4336}
4337
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004338 static void
4339job_free(job_T *job)
4340{
4341 if (!in_free_unref_items)
4342 {
4343 job_free_contents(job);
4344 job_free_job(job);
4345 }
4346}
4347
Bram Moolenaar655da312016-05-28 22:22:34 +02004348#if defined(EXITFREE) || defined(PROTO)
4349 void
4350job_free_all(void)
4351{
4352 while (first_job != NULL)
4353 job_free(first_job);
4354}
4355#endif
4356
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004357/*
4358 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004359 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4360 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004361 */
4362 static int
4363job_still_useful(job_T *job)
4364{
4365 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004366 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4367 || (job->jv_channel != NULL
4368 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004369}
4370
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004371/*
4372 * Mark references in jobs that are still useful.
4373 */
4374 int
4375set_ref_in_job(int copyID)
4376{
4377 int abort = FALSE;
4378 job_T *job;
4379 typval_T tv;
4380
4381 for (job = first_job; job != NULL; job = job->jv_next)
4382 if (job_still_useful(job))
4383 {
4384 tv.v_type = VAR_JOB;
4385 tv.vval.v_job = job;
4386 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4387 }
4388 return abort;
4389}
4390
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004391 void
4392job_unref(job_T *job)
4393{
4394 if (job != NULL && --job->jv_refcount <= 0)
4395 {
4396 /* Do not free the job when it has not ended yet and there is a
4397 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004398 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004399 {
4400 job_free(job);
4401 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004402 else if (job->jv_channel != NULL
4403 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004404 {
4405 /* Do remove the link to the channel, otherwise it hangs
4406 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004407 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004408 job->jv_channel->ch_job = NULL;
4409 channel_unref(job->jv_channel);
4410 job->jv_channel = NULL;
4411 }
4412 }
4413}
4414
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004415 int
4416free_unused_jobs_contents(int copyID, int mask)
4417{
4418 int did_free = FALSE;
4419 job_T *job;
4420
4421 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004422 if ((job->jv_copyID & mask) != (copyID & mask)
4423 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004424 {
4425 /* Free the channel and ordinary items it contains, but don't
4426 * recurse into Lists, Dictionaries etc. */
4427 job_free_contents(job);
4428 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004429 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004430 return did_free;
4431}
4432
4433 void
4434free_unused_jobs(int copyID, int mask)
4435{
4436 job_T *job;
4437 job_T *job_next;
4438
4439 for (job = first_job; job != NULL; job = job_next)
4440 {
4441 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004442 if ((job->jv_copyID & mask) != (copyID & mask)
4443 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004444 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004445 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004446 job_free_job(job);
4447 }
4448 }
4449}
4450
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004451/*
4452 * Allocate a job. Sets the refcount to one and sets options default.
4453 */
4454 static job_T *
4455job_alloc(void)
4456{
4457 job_T *job;
4458
4459 job = (job_T *)alloc_clear(sizeof(job_T));
4460 if (job != NULL)
4461 {
4462 job->jv_refcount = 1;
4463 job->jv_stoponexit = vim_strsave((char_u *)"term");
4464
4465 if (first_job != NULL)
4466 {
4467 first_job->jv_prev = job;
4468 job->jv_next = first_job;
4469 }
4470 first_job = job;
4471 }
4472 return job;
4473}
4474
4475 void
4476job_set_options(job_T *job, jobopt_T *opt)
4477{
4478 if (opt->jo_set & JO_STOPONEXIT)
4479 {
4480 vim_free(job->jv_stoponexit);
4481 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4482 job->jv_stoponexit = NULL;
4483 else
4484 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4485 }
4486 if (opt->jo_set & JO_EXIT_CB)
4487 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004488 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004489 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004490 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004492 job->jv_exit_partial = NULL;
4493 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004495 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004496 job->jv_exit_partial = opt->jo_exit_partial;
4497 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004498 {
4499 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004500 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004501 }
4502 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004503 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004504 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004505 func_ref(job->jv_exit_cb);
4506 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004507 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004508 }
4509}
4510
4511/*
4512 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4513 */
4514 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004515job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004516{
4517 job_T *job;
4518
4519 for (job = first_job; job != NULL; job = job->jv_next)
4520 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4521 mch_stop_job(job, job->jv_stoponexit);
4522}
4523
4524/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004525 * Return TRUE when there is any job that might exit, which means
4526 * job_check_ended() should be called once in a while.
4527 */
4528 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004529has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004530{
4531 job_T *job;
4532
4533 for (job = first_job; job != NULL; job = job->jv_next)
4534 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4535 return TRUE;
4536 return FALSE;
4537}
4538
4539/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004540 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004541 */
4542 void
4543job_check_ended(void)
4544{
4545 static time_t last_check = 0;
4546 time_t now;
4547 job_T *job;
4548 job_T *next;
4549
4550 /* Only do this once in 10 seconds. */
4551 now = time(NULL);
4552 if (last_check + 10 < now)
4553 {
4554 last_check = now;
4555 for (job = first_job; job != NULL; job = next)
4556 {
4557 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004558 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004559 job_status(job); /* may free "job" */
4560 }
4561 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004562 if (channel_need_redraw)
4563 {
4564 channel_need_redraw = FALSE;
4565 redraw_after_callback();
4566 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004567}
4568
4569/*
4570 * "job_start()" function
4571 */
4572 job_T *
4573job_start(typval_T *argvars)
4574{
4575 job_T *job;
4576 char_u *cmd = NULL;
4577#if defined(UNIX)
4578# define USE_ARGV
4579 char **argv = NULL;
4580 int argc = 0;
4581#else
4582 garray_T ga;
4583#endif
4584 jobopt_T opt;
4585 int part;
4586
4587 job = job_alloc();
4588 if (job == NULL)
4589 return NULL;
4590
4591 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004592#ifndef USE_ARGV
4593 ga_init2(&ga, (int)sizeof(char*), 20);
4594#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004595
4596 /* Default mode is NL. */
4597 clear_job_options(&opt);
4598 opt.jo_mode = MODE_NL;
4599 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004600 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4601 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004602 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004603
4604 /* Check that when io is "file" that there is a file name. */
4605 for (part = PART_OUT; part <= PART_IN; ++part)
4606 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4607 && opt.jo_io[part] == JIO_FILE
4608 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4609 || *opt.jo_io_name[part] == NUL))
4610 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004611 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004612 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004613 }
4614
4615 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4616 {
4617 buf_T *buf = NULL;
4618
4619 /* check that we can find the buffer before starting the job */
4620 if (opt.jo_set & JO_IN_BUF)
4621 {
4622 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4623 if (buf == NULL)
4624 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4625 }
4626 else if (!(opt.jo_set & JO_IN_NAME))
4627 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004628 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004629 }
4630 else
4631 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4632 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004633 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004634 if (buf->b_ml.ml_mfp == NULL)
4635 {
4636 char_u numbuf[NUMBUFLEN];
4637 char_u *s;
4638
4639 if (opt.jo_set & JO_IN_BUF)
4640 {
4641 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4642 s = numbuf;
4643 }
4644 else
4645 s = opt.jo_io_name[PART_IN];
4646 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004647 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004648 }
4649 job->jv_in_buf = buf;
4650 }
4651
4652 job_set_options(job, &opt);
4653
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004654 if (argvars[0].v_type == VAR_STRING)
4655 {
4656 /* Command is a string. */
4657 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004658 if (cmd == NULL || *cmd == NUL)
4659 {
4660 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004661 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004662 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004663#ifdef USE_ARGV
4664 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004665 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004666 argv[argc] = NULL;
4667#endif
4668 }
4669 else if (argvars[0].v_type != VAR_LIST
4670 || argvars[0].vval.v_list == NULL
4671 || argvars[0].vval.v_list->lv_len < 1)
4672 {
4673 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004674 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004675 }
4676 else
4677 {
4678 list_T *l = argvars[0].vval.v_list;
4679 listitem_T *li;
4680 char_u *s;
4681
4682#ifdef USE_ARGV
4683 /* Pass argv[] to mch_call_shell(). */
4684 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4685 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004686 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004687#endif
4688 for (li = l->lv_first; li != NULL; li = li->li_next)
4689 {
4690 s = get_tv_string_chk(&li->li_tv);
4691 if (s == NULL)
4692 goto theend;
4693#ifdef USE_ARGV
4694 argv[argc++] = (char *)s;
4695#else
4696 /* Only escape when needed, double quotes are not always allowed. */
4697 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4698 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004699# ifdef WIN32
4700 int old_ssl = p_ssl;
4701
4702 /* This is using CreateProcess, not cmd.exe. Always use
4703 * double quote and backslashes. */
4704 p_ssl = 0;
4705# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004706 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004707# ifdef WIN32
4708 p_ssl = old_ssl;
4709# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004710 if (s == NULL)
4711 goto theend;
4712 ga_concat(&ga, s);
4713 vim_free(s);
4714 }
4715 else
4716 ga_concat(&ga, s);
4717 if (li->li_next != NULL)
4718 ga_append(&ga, ' ');
4719#endif
4720 }
4721#ifdef USE_ARGV
4722 argv[argc] = NULL;
4723#else
4724 cmd = ga.ga_data;
4725#endif
4726 }
4727
4728#ifdef USE_ARGV
4729 if (ch_log_active())
4730 {
4731 garray_T ga;
4732 int i;
4733
4734 ga_init2(&ga, (int)sizeof(char), 200);
4735 for (i = 0; i < argc; ++i)
4736 {
4737 if (i > 0)
4738 ga_concat(&ga, (char_u *)" ");
4739 ga_concat(&ga, (char_u *)argv[i]);
4740 }
4741 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4742 ga_clear(&ga);
4743 }
4744 mch_start_job(argv, job, &opt);
4745#else
4746 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4747 mch_start_job((char *)cmd, job, &opt);
4748#endif
4749
4750 /* If the channel is reading from a buffer, write lines now. */
4751 if (job->jv_channel != NULL)
4752 channel_write_in(job->jv_channel);
4753
4754theend:
4755#ifdef USE_ARGV
4756 vim_free(argv);
4757#else
4758 vim_free(ga.ga_data);
4759#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004760 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004761 return job;
4762}
4763
4764/*
4765 * Get the status of "job" and invoke the exit callback when needed.
4766 * The returned string is not allocated.
4767 */
4768 char *
4769job_status(job_T *job)
4770{
4771 char *result;
4772
4773 if (job->jv_status == JOB_ENDED)
4774 /* No need to check, dead is dead. */
4775 result = "dead";
4776 else if (job->jv_status == JOB_FAILED)
4777 result = "fail";
4778 else
4779 {
4780 result = mch_job_status(job);
4781 if (job->jv_status == JOB_ENDED)
4782 ch_log(job->jv_channel, "Job ended");
4783 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4784 {
4785 typval_T argv[3];
4786 typval_T rettv;
4787 int dummy;
4788
4789 /* invoke the exit callback; make sure the refcount is > 0 */
4790 ++job->jv_refcount;
4791 argv[0].v_type = VAR_JOB;
4792 argv[0].vval.v_job = job;
4793 argv[1].v_type = VAR_NUMBER;
4794 argv[1].vval.v_number = job->jv_exitval;
4795 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004796 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004797 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004798 clear_tv(&rettv);
4799 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004800 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004801 }
4802 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4803 {
4804 /* The job was already unreferenced, now that it ended it can be
4805 * freed. Careful: caller must not use "job" after this! */
4806 job_free(job);
4807 }
4808 }
4809 return result;
4810}
4811
Bram Moolenaar8950a562016-03-12 15:22:55 +01004812/*
4813 * Implementation of job_info().
4814 */
4815 void
4816job_info(job_T *job, dict_T *dict)
4817{
4818 dictitem_T *item;
4819 varnumber_T nr;
4820
4821 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4822
4823 item = dictitem_alloc((char_u *)"channel");
4824 if (item == NULL)
4825 return;
4826 item->di_tv.v_lock = 0;
4827 item->di_tv.v_type = VAR_CHANNEL;
4828 item->di_tv.vval.v_channel = job->jv_channel;
4829 if (job->jv_channel != NULL)
4830 ++job->jv_channel->ch_refcount;
4831 if (dict_add(dict, item) == FAIL)
4832 dictitem_free(item);
4833
4834#ifdef UNIX
4835 nr = job->jv_pid;
4836#else
4837 nr = job->jv_proc_info.dwProcessId;
4838#endif
4839 dict_add_nr_str(dict, "process", nr, NULL);
4840
4841 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004842 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004843 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4844}
4845
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004846 int
4847job_stop(job_T *job, typval_T *argvars)
4848{
4849 char_u *arg;
4850
4851 if (argvars[1].v_type == VAR_UNKNOWN)
4852 arg = (char_u *)"";
4853 else
4854 {
4855 arg = get_tv_string_chk(&argvars[1]);
4856 if (arg == NULL)
4857 {
4858 EMSG(_(e_invarg));
4859 return 0;
4860 }
4861 }
4862 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4863 if (mch_stop_job(job, arg) == FAIL)
4864 return 0;
4865
4866 /* Assume that "hup" does not kill the job. */
4867 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4868 job->jv_channel->ch_job_killed = TRUE;
4869
4870 /* We don't try freeing the job, obviously the caller still has a
4871 * reference to it. */
4872 return 1;
4873}
4874
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004875#endif /* FEAT_JOB_CHANNEL */