blob: 0bab47a6263cd58f42a7d2bdf376dc67d542716c [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 Moolenaar169ebb02016-09-07 23:32:23 +02001082find_buffer(char_u *name, int err, int msg)
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 Moolenaar169ebb02016-09-07 23:32:23 +02001107 if (msg)
1108 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001109 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001110 changed_bytes(1, 0);
1111 curbuf = save_curbuf;
1112 }
1113
1114 return buf;
1115}
1116
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001117 static void
1118set_callback(
1119 char_u **cbp,
1120 partial_T **pp,
1121 char_u *callback,
1122 partial_T *partial)
1123{
1124 free_callback(*cbp, *pp);
1125 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001126 {
1127 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001128 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001129 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001130 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001131 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001132 func_ref(*cbp);
1133 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001134 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001135 else
1136 *cbp = NULL;
1137 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001138 if (partial != NULL)
1139 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001140}
1141
Bram Moolenaar187db502016-02-27 14:44:26 +01001142/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001144 */
1145 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001146channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001147{
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001148 int part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001149
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001150 if (opt->jo_set & JO_MODE)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001151 for (part = PART_SOCK; part <= PART_IN; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001152 channel->ch_part[part].ch_mode = opt->jo_mode;
1153 if (opt->jo_set & JO_IN_MODE)
1154 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1155 if (opt->jo_set & JO_OUT_MODE)
1156 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1157 if (opt->jo_set & JO_ERR_MODE)
1158 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001159
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001160 if (opt->jo_set & JO_TIMEOUT)
1161 for (part = PART_SOCK; part <= PART_IN; ++part)
1162 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1163 if (opt->jo_set & JO_OUT_TIMEOUT)
1164 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1165 if (opt->jo_set & JO_ERR_TIMEOUT)
1166 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001167 if (opt->jo_set & JO_BLOCK_WRITE)
1168 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001169
1170 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001171 set_callback(&channel->ch_callback, &channel->ch_partial,
1172 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001173 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001174 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1175 &channel->ch_part[PART_OUT].ch_partial,
1176 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001177 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001178 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1179 &channel->ch_part[PART_ERR].ch_partial,
1180 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001181 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001182 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1183 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar187db502016-02-27 14:44:26 +01001184
1185 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1186 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001187 buf_T *buf;
1188
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001189 /* writing output to a buffer. Default mode is NL. */
1190 if (!(opt->jo_set & JO_OUT_MODE))
1191 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001192 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001193 {
1194 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1195 if (buf == NULL)
1196 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1197 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001198 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001199 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001200 int msg = TRUE;
1201
1202 if (opt->jo_set2 & JO2_OUT_MSG)
1203 msg = opt->jo_message[PART_OUT];
1204 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001205 }
1206 if (buf != NULL)
1207 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001208 if (opt->jo_set & JO_OUT_MODIFIABLE)
1209 channel->ch_part[PART_OUT].ch_nomodifiable =
1210 !opt->jo_modifiable[PART_OUT];
1211
1212 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1213 {
1214 EMSG(_(e_modifiable));
1215 }
1216 else
1217 {
1218 ch_logs(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001219 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001220 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001221 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001223 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001224
1225 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1226 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1227 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1228 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001229 buf_T *buf;
1230
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001231 /* writing err to a buffer. Default mode is NL. */
1232 if (!(opt->jo_set & JO_ERR_MODE))
1233 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1234 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001235 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001236 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001237 {
1238 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1239 if (buf == NULL)
1240 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1241 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001242 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001243 {
1244 int msg = TRUE;
1245
1246 if (opt->jo_set2 & JO2_ERR_MSG)
1247 msg = opt->jo_message[PART_ERR];
1248 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1249 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001250 if (buf != NULL)
1251 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001252 if (opt->jo_set & JO_ERR_MODIFIABLE)
1253 channel->ch_part[PART_ERR].ch_nomodifiable =
1254 !opt->jo_modifiable[PART_ERR];
1255 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1256 {
1257 EMSG(_(e_modifiable));
1258 }
1259 else
1260 {
1261 ch_logs(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001262 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001263 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001264 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001265 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001266 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001267
1268 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1269 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1270 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001271}
1272
1273/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001274 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001275 */
1276 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001277channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001278 channel_T *channel,
1279 int part,
1280 char_u *callback,
1281 partial_T *partial,
1282 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001283{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001284 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001285 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1286
1287 if (item != NULL)
1288 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001289 item->cq_partial = partial;
1290 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001291 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001292 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001293 item->cq_callback = callback;
1294 }
1295 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001296 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001297 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001298 func_ref(item->cq_callback);
1299 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001300 item->cq_seq_nr = id;
1301 item->cq_prev = head->cq_prev;
1302 head->cq_prev = item;
1303 item->cq_next = NULL;
1304 if (item->cq_prev == NULL)
1305 head->cq_next = item;
1306 else
1307 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001308 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001309}
1310
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001311 static void
1312write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1313{
1314 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001315 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001316 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001317 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001318
Bram Moolenaar655da312016-05-28 22:22:34 +02001319 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001320 if ((p = alloc(len + 2)) == NULL)
1321 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001322 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001323
1324 for (i = 0; i < len; ++i)
1325 if (p[i] == NL)
1326 p[i] = NUL;
1327
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001328 p[len] = NL;
1329 p[len + 1] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001330 channel_send(channel, PART_IN, p, len + 1, "write_buf_line()");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001331 vim_free(p);
1332}
1333
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001334/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001335 * Return TRUE if "channel" can be written to.
1336 * Returns FALSE if the input is closed or the write would block.
1337 */
1338 static int
1339can_write_buf_line(channel_T *channel)
1340{
1341 chanpart_T *in_part = &channel->ch_part[PART_IN];
1342
1343 if (in_part->ch_fd == INVALID_FD)
1344 return FALSE; /* pipe was closed */
1345
1346 /* for testing: block every other attempt to write */
1347 if (in_part->ch_block_write == 1)
1348 in_part->ch_block_write = -1;
1349 else if (in_part->ch_block_write == -1)
1350 in_part->ch_block_write = 1;
1351
1352 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1353#ifndef WIN32
1354 {
1355# if defined(HAVE_SELECT)
1356 struct timeval tval;
1357 fd_set wfds;
1358 int ret;
1359
1360 FD_ZERO(&wfds);
1361 FD_SET((int)in_part->ch_fd, &wfds);
1362 tval.tv_sec = 0;
1363 tval.tv_usec = 0;
1364 for (;;)
1365 {
1366 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1367# ifdef EINTR
1368 SOCK_ERRNO;
1369 if (ret == -1 && errno == EINTR)
1370 continue;
1371# endif
1372 if (ret <= 0 || in_part->ch_block_write == 1)
1373 {
1374 if (ret > 0)
1375 ch_log(channel, "FAKED Input not ready for writing");
1376 else
1377 ch_log(channel, "Input not ready for writing");
1378 return FALSE;
1379 }
1380 break;
1381 }
1382# else
1383 struct pollfd fds;
1384
1385 fds.fd = in_part->ch_fd;
1386 fds.events = POLLOUT;
1387 if (poll(&fds, 1, 0) <= 0)
1388 {
1389 ch_log(channel, "Input not ready for writing");
1390 return FALSE;
1391 }
1392 if (in_part->ch_block_write == 1)
1393 {
1394 ch_log(channel, "FAKED Input not ready for writing");
1395 return FALSE;
1396 }
1397# endif
1398 }
1399#endif
1400 return TRUE;
1401}
1402
1403/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001404 * Write any lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001405 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001406 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001407channel_write_in(channel_T *channel)
1408{
1409 chanpart_T *in_part = &channel->ch_part[PART_IN];
1410 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001411 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001412 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001413
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001414 if (buf == NULL || in_part->ch_buf_append)
1415 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001416 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001417 {
1418 /* buffer was wiped out or unloaded */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001419 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001420 return;
1421 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001422
1423 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1424 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1425 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001426 if (!can_write_buf_line(channel))
1427 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001428 write_buf_line(buf, lnum, channel);
1429 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001430 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001431
1432 if (written == 1)
1433 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1434 else if (written > 1)
1435 ch_logn(channel, "written %d lines to channel", written);
1436
Bram Moolenaar014069a2016-03-03 22:51:40 +01001437 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001438 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001439 {
1440 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001441 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001442 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001443
1444 /* Close the pipe/socket, so that the other side gets EOF. */
1445 may_close_part(&channel->CH_IN_FD);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001446 }
1447 else
1448 ch_logn(channel, "Still %d more lines to write",
1449 buf->b_ml.ml_line_count - lnum + 1);
1450}
1451
1452/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001453 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001454 */
1455 void
1456channel_buffer_free(buf_T *buf)
1457{
1458 channel_T *channel;
1459 int part;
1460
1461 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1462 for (part = PART_SOCK; part <= PART_IN; ++part)
1463 {
1464 chanpart_T *ch_part = &channel->ch_part[part];
1465
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001466 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001467 {
1468 ch_logs(channel, "%s buffer has been wiped out",
1469 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001470 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001471 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001472 }
1473}
1474
1475/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001476 * Write any lines waiting to be written to a channel.
1477 */
1478 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001479channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001480{
1481 channel_T *channel;
1482
1483 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1484 {
1485 chanpart_T *in_part = &channel->ch_part[PART_IN];
1486
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001487 if (in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001488 {
1489 if (in_part->ch_buf_append)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001490 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001491 else
1492 channel_write_in(channel);
1493 }
1494 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001495}
1496
1497/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001498 * Write appended lines above the last one in "buf" to the channel.
1499 */
1500 void
1501channel_write_new_lines(buf_T *buf)
1502{
1503 channel_T *channel;
1504 int found_one = FALSE;
1505
1506 /* There could be more than one channel for the buffer, loop over all of
1507 * them. */
1508 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1509 {
1510 chanpart_T *in_part = &channel->ch_part[PART_IN];
1511 linenr_T lnum;
1512 int written = 0;
1513
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001514 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001515 {
1516 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001517 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001518 found_one = TRUE;
1519 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1520 ++lnum)
1521 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001522 if (!can_write_buf_line(channel))
1523 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001524 write_buf_line(buf, lnum, channel);
1525 ++written;
1526 }
1527
1528 if (written == 1)
1529 ch_logn(channel, "written line %d to channel", (int)lnum - 1);
1530 else if (written > 1)
1531 ch_logn(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001532 if (lnum < buf->b_ml.ml_line_count)
1533 ch_logn(channel, "Still %d more lines to write",
1534 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001535
1536 in_part->ch_buf_bot = lnum;
1537 }
1538 }
1539 if (!found_one)
1540 buf->b_write_to_channel = FALSE;
1541}
1542
1543/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001544 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001545 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001546 */
1547 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001548invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1549 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001550{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001551 typval_T rettv;
1552 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001553
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001554 if (safe_to_invoke_callback == 0)
1555 EMSG("INTERNAL: Invoking callback when it is not safe");
1556
Bram Moolenaar77073442016-02-13 23:23:53 +01001557 argv[0].v_type = VAR_CHANNEL;
1558 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001559
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001560 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1561 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001562 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001563 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001564}
1565
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001566/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001567 * Return the first node from "channel"/"part" without removing it.
1568 * Returns NULL if there is nothing.
1569 */
1570 readq_T *
1571channel_peek(channel_T *channel, int part)
1572{
1573 readq_T *head = &channel->ch_part[part].ch_head;
1574
1575 return head->rq_next;
1576}
1577
1578/*
1579 * Return a pointer to the first NL in "node".
1580 * Skips over NUL characters.
1581 * Returns NULL if there is no NL.
1582 */
1583 char_u *
1584channel_first_nl(readq_T *node)
1585{
1586 char_u *buffer = node->rq_buffer;
1587 long_u i;
1588
1589 for (i = 0; i < node->rq_buflen; ++i)
1590 if (buffer[i] == NL)
1591 return buffer + i;
1592 return NULL;
1593}
1594
1595/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001596 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001597 * The caller must free it.
1598 * Returns NULL if there is nothing.
1599 */
1600 char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001601channel_get(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001602{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001603 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001604 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001605 char_u *p;
1606
Bram Moolenaar77073442016-02-13 23:23:53 +01001607 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001608 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001609 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 p = node->rq_buffer;
1611 head->rq_next = node->rq_next;
1612 if (node->rq_next == NULL)
1613 head->rq_prev = NULL;
1614 else
1615 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001616 vim_free(node);
1617 return p;
1618}
1619
1620/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001621 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001622 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001623 */
1624 static char_u *
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001625channel_get_all(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001626{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001627 readq_T *head = &channel->ch_part[part].ch_head;
1628 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001629 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001630 char_u *res;
1631 char_u *p;
1632
1633 /* If there is only one buffer just get that one. */
1634 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1635 return channel_get(channel, part);
1636
1637 /* Concatenate everything into one buffer. */
1638 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001639 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001640 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001641 if (res == NULL)
1642 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001643 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001644 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001645 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001646 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001647 p += node->rq_buflen;
1648 }
1649 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001650
1651 /* Free all buffers */
1652 do
1653 {
1654 p = channel_get(channel, part);
1655 vim_free(p);
1656 } while (p != NULL);
1657
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001658 /* turn all NUL into NL */
1659 while (len > 0)
1660 {
1661 --len;
1662 if (res[len] == NUL)
1663 res[len] = NL;
1664 }
1665
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001666 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001667}
1668
1669/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001670 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001671 * Caller must check these bytes are available.
1672 */
1673 void
1674channel_consume(channel_T *channel, int part, int len)
1675{
1676 readq_T *head = &channel->ch_part[part].ch_head;
1677 readq_T *node = head->rq_next;
1678 char_u *buf = node->rq_buffer;
1679
1680 mch_memmove(buf, buf + len, node->rq_buflen - len);
1681 node->rq_buflen -= len;
1682}
1683
1684/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001685 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001687 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001688 */
1689 int
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001690channel_collapse(channel_T *channel, int part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001691{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001692 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001693 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001694 readq_T *last_node;
1695 readq_T *n;
1696 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001698 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001699
Bram Moolenaar77073442016-02-13 23:23:53 +01001700 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 return FAIL;
1702
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001703 last_node = node->rq_next;
1704 len = node->rq_buflen + last_node->rq_buflen + 1;
1705 if (want_nl)
1706 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001707 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001708 {
1709 last_node = last_node->rq_next;
1710 len += last_node->rq_buflen;
1711 }
1712
1713 p = newbuf = alloc(len);
1714 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001715 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001716 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001717 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001718 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001719 node->rq_buffer = newbuf;
1720 for (n = node; n != last_node; )
1721 {
1722 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001723 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001724 p += n->rq_buflen;
1725 vim_free(n->rq_buffer);
1726 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001727 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001728
1729 /* dispose of the collapsed nodes and their buffers */
1730 for (n = node->rq_next; n != last_node; )
1731 {
1732 n = n->rq_next;
1733 vim_free(n->rq_prev);
1734 }
1735 node->rq_next = last_node->rq_next;
1736 if (last_node->rq_next == NULL)
1737 head->rq_prev = node;
1738 else
1739 last_node->rq_next->rq_prev = node;
1740 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001741 return OK;
1742}
1743
1744/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001745 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001746 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001747 * Returns OK or FAIL.
1748 */
1749 static int
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001750channel_save(channel_T *channel, int part, char_u *buf, int len,
1751 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001752{
1753 readq_T *node;
1754 readq_T *head = &channel->ch_part[part].ch_head;
1755 char_u *p;
1756 int i;
1757
1758 node = (readq_T *)alloc(sizeof(readq_T));
1759 if (node == NULL)
1760 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001761 /* A NUL is added at the end, because netbeans code expects that.
1762 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001763 node->rq_buffer = alloc(len + 1);
1764 if (node->rq_buffer == NULL)
1765 {
1766 vim_free(node);
1767 return FAIL; /* out of memory */
1768 }
1769
1770 if (channel->ch_part[part].ch_mode == MODE_NL)
1771 {
1772 /* Drop any CR before a NL. */
1773 p = node->rq_buffer;
1774 for (i = 0; i < len; ++i)
1775 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1776 *p++ = buf[i];
1777 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001778 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001779 }
1780 else
1781 {
1782 mch_memmove(node->rq_buffer, buf, len);
1783 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001784 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785 }
1786
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001787 if (prepend)
1788 {
1789 /* preend node to the head of the queue */
1790 node->rq_next = head->rq_next;
1791 node->rq_prev = NULL;
1792 if (head->rq_next == NULL)
1793 head->rq_prev = node;
1794 else
1795 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001796 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001797 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001798 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001799 {
1800 /* append node to the tail of the queue */
1801 node->rq_next = NULL;
1802 node->rq_prev = head->rq_prev;
1803 if (head->rq_prev == NULL)
1804 head->rq_next = node;
1805 else
1806 head->rq_prev->rq_next = node;
1807 head->rq_prev = node;
1808 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001809
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001810 if (log_fd != NULL && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001811 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001812 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001813 fprintf(log_fd, "'");
1814 if (fwrite(buf, len, 1, log_fd) != 1)
1815 return FAIL;
1816 fprintf(log_fd, "'\n");
1817 }
1818 return OK;
1819}
1820
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001821 static int
1822channel_fill(js_read_T *reader)
1823{
1824 channel_T *channel = (channel_T *)reader->js_cookie;
1825 int part = reader->js_cookie_arg;
1826 char_u *next = channel_get(channel, part);
1827 int unused;
1828 int len;
1829 char_u *p;
1830
1831 if (next == NULL)
1832 return FALSE;
1833
1834 unused = reader->js_end - reader->js_buf - reader->js_used;
1835 if (unused > 0)
1836 {
1837 /* Prepend unused text. */
1838 len = (int)STRLEN(next);
1839 p = alloc(unused + len + 1);
1840 if (p == NULL)
1841 {
1842 vim_free(next);
1843 return FALSE;
1844 }
1845 mch_memmove(p, reader->js_buf + reader->js_used, unused);
1846 mch_memmove(p + unused, next, len + 1);
1847 vim_free(next);
1848 next = p;
1849 }
1850
1851 vim_free(reader->js_buf);
1852 reader->js_buf = next;
1853 reader->js_used = 0;
1854 return TRUE;
1855}
1856
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001857/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001858 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001859 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001860 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001861 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001862 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863channel_parse_json(channel_T *channel, int part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001864{
1865 js_read_T reader;
1866 typval_T listtv;
1867 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001868 chanpart_T *chanpart = &channel->ch_part[part];
1869 jsonq_T *head = &chanpart->ch_json_head;
1870 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001871 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001872
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001873 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001874 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001875
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001876 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001877 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001878 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001879 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001880 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001881
1882 /* When a message is incomplete we wait for a short while for more to
1883 * arrive. After the delay drop the input, otherwise a truncated string
1884 * or list will make us hang. */
1885 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001886 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001887 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001888 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001889 /* Only accept the response when it is a list with at least two
1890 * items. */
1891 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001892 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001893 if (listtv.v_type != VAR_LIST)
1894 ch_error(channel, "Did not receive a list, discarding");
1895 else
1896 ch_errorn(channel, "Expected list with two items, got %d",
1897 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001898 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001899 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001900 else
1901 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001902 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1903 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001904 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001905 else
1906 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001907 item->jq_value = alloc_tv();
1908 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001909 {
1910 vim_free(item);
1911 clear_tv(&listtv);
1912 }
1913 else
1914 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001915 *item->jq_value = listtv;
1916 item->jq_prev = head->jq_prev;
1917 head->jq_prev = item;
1918 item->jq_next = NULL;
1919 if (item->jq_prev == NULL)
1920 head->jq_next = item;
1921 else
1922 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001923 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001924 }
1925 }
1926 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001927
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001928 if (status == OK)
1929 chanpart->ch_waiting = FALSE;
1930 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001931 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001932 if (!chanpart->ch_waiting)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001933 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001934 /* First time encountering incomplete message, set a deadline of
1935 * 100 msec. */
1936 ch_log(channel, "Incomplete message - wait for more");
1937 reader.js_used = 0;
1938 chanpart->ch_waiting = TRUE;
1939#ifdef WIN32
1940 chanpart->ch_deadline = GetTickCount() + 100L;
1941#else
1942 gettimeofday(&chanpart->ch_deadline, NULL);
1943 chanpart->ch_deadline.tv_usec += 100 * 1000;
1944 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1945 {
1946 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1947 ++chanpart->ch_deadline.tv_sec;
1948 }
1949#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001950 }
1951 else
1952 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001953 int timeout;
1954#ifdef WIN32
1955 timeout = GetTickCount() > chanpart->ch_deadline;
1956#else
1957 {
1958 struct timeval now_tv;
1959
1960 gettimeofday(&now_tv, NULL);
1961 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1962 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1963 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1964 }
1965#endif
1966 if (timeout)
1967 {
1968 status = FAIL;
1969 chanpart->ch_waiting = FALSE;
1970 }
1971 else
1972 {
1973 reader.js_used = 0;
1974 ch_log(channel, "still waiting on incomplete message");
1975 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001976 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001977 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978
1979 if (status == FAIL)
1980 {
1981 ch_error(channel, "Decoding failed - discarding input");
1982 ret = FALSE;
1983 chanpart->ch_waiting = FALSE;
1984 }
1985 else if (reader.js_buf[reader.js_used] != NUL)
1986 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001987 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001988 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001989 (int)(reader.js_end - reader.js_buf) - reader.js_used,
1990 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001991 ret = status == MAYBE ? FALSE: TRUE;
1992 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001993 else
1994 ret = FALSE;
1995
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001996 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001997 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001998}
1999
2000/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002001 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002002 */
2003 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002004remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002005{
Bram Moolenaar77073442016-02-13 23:23:53 +01002006 if (node->cq_prev == NULL)
2007 head->cq_next = node->cq_next;
2008 else
2009 node->cq_prev->cq_next = node->cq_next;
2010 if (node->cq_next == NULL)
2011 head->cq_prev = node->cq_prev;
2012 else
2013 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002014}
2015
2016/*
2017 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002018 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002019 */
2020 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002021remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002022{
Bram Moolenaar77073442016-02-13 23:23:53 +01002023 if (node->jq_prev == NULL)
2024 head->jq_next = node->jq_next;
2025 else
2026 node->jq_prev->jq_next = node->jq_next;
2027 if (node->jq_next == NULL)
2028 head->jq_prev = node->jq_prev;
2029 else
2030 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002031 vim_free(node);
2032}
2033
2034/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002035 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002036 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002037 * When "id" is zero or negative jut get the first message. But not the one
2038 * with id ch_block_id.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002039 * Return OK when found and return the value in "rettv".
2040 * Return FAIL otherwise.
2041 */
2042 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002043channel_get_json(channel_T *channel, int part, int id, typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002044{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002045 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002046 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047
Bram Moolenaar77073442016-02-13 23:23:53 +01002048 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002049 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002050 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002051 typval_T *tv = &l->lv_first->li_tv;
2052
2053 if ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002054 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002055 || tv->vval.v_number == 0
2056 || tv->vval.v_number != channel->ch_part[part].ch_block_id)))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002058 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002059 if (tv->v_type == VAR_NUMBER)
2060 ch_logn(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002061 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002062 return OK;
2063 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002064 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002065 }
2066 return FAIL;
2067}
2068
Bram Moolenaarece61b02016-02-20 21:39:05 +01002069#define CH_JSON_MAX_ARGS 4
2070
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002071/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002072 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002073 * "argv[0]" is the command string.
2074 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002075 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002076 static void
Bram Moolenaarece61b02016-02-20 21:39:05 +01002077channel_exe_cmd(channel_T *channel, int part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002078{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002079 char_u *cmd = argv[0].vval.v_string;
2080 char_u *arg;
2081 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002082
Bram Moolenaarece61b02016-02-20 21:39:05 +01002083 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002084 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002085 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002086 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002087 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002088 return;
2089 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002090 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002091 if (arg == NULL)
2092 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002093
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002094 if (STRCMP(cmd, "ex") == 0)
2095 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002096 int save_called_emsg = called_emsg;
2097
2098 called_emsg = FALSE;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002099 ch_logs(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002100 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002101 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002102 --emsg_silent;
2103 if (called_emsg)
2104 ch_logs(channel, "Ex command error: '%s'",
2105 (char *)get_vim_var_str(VV_ERRMSG));
2106 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002107 }
2108 else if (STRCMP(cmd, "normal") == 0)
2109 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002110 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002111
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002112 ch_logs(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002113 ea.arg = arg;
2114 ea.addr_count = 0;
2115 ea.forceit = TRUE; /* no mapping */
2116 ex_normal(&ea);
2117 }
2118 else if (STRCMP(cmd, "redraw") == 0)
2119 {
2120 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002121
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002122 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002123 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002124 ex_redraw(&ea);
2125 showruler(FALSE);
2126 setcursor();
2127 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002128#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002129 if (gui.in_use)
2130 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002131 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002132 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002133 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002134#endif
2135 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002136 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002137 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002138 int is_call = cmd[0] == 'c';
2139 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002140
Bram Moolenaarece61b02016-02-20 21:39:05 +01002141 if (argv[id_idx].v_type != VAR_UNKNOWN
2142 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002143 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002144 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002145 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002146 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002147 }
2148 else if (is_call && argv[2].v_type != VAR_LIST)
2149 {
2150 ch_error(channel, "third argument for call must be a list");
2151 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002152 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002153 }
2154 else
2155 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002156 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002157 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002158 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002159 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002160
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002161 /* Don't pollute the display with errors. */
2162 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002163 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002164 {
2165 ch_logs(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002166 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002167 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002168 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002169 {
2170 ch_logs(channel, "Calling '%s'", (char *)arg);
2171 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2172 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002173 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002174
2175 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002176 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002177 int id = argv[id_idx].vval.v_number;
2178
Bram Moolenaar55fab432016-02-07 16:53:13 +01002179 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002180 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002181 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002182 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002183 /* If evaluation failed or the result can't be encoded
2184 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002185 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002186 err_tv.v_type = VAR_STRING;
2187 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002188 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002189 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002190 if (json != NULL)
2191 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002192 channel_send(channel,
2193 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002194 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002195 vim_free(json);
2196 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002197 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002198 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002199 if (tv == &res_tv)
2200 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002201 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002202 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002204 }
2205 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002206 {
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02002207 ch_errors(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002208 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002209 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002210}
2211
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002212/*
2213 * Invoke the callback at "cbhead".
2214 * Does not redraw but sets channel_need_redraw.
2215 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002216 static void
2217invoke_one_time_callback(
2218 channel_T *channel,
2219 cbq_T *cbhead,
2220 cbq_T *item,
2221 typval_T *argv)
2222{
2223 ch_logs(channel, "Invoking one-time callback %s",
2224 (char *)item->cq_callback);
2225 /* Remove the item from the list first, if the callback
2226 * invokes ch_close() the list will be cleared. */
2227 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002228 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002229 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002230 vim_free(item);
2231}
2232
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002233 static void
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002234append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002235{
2236 buf_T *save_curbuf = curbuf;
2237 linenr_T lnum = buffer->b_ml.ml_line_count;
2238 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002239 chanpart_T *ch_part = &channel->ch_part[part];
2240 int save_p_ma = buffer->b_p_ma;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002241 int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002242
2243 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2244 {
2245 if (!ch_part->ch_nomod_error)
2246 {
2247 ch_error(channel, "Buffer is not modifiable, cannot append");
2248 ch_part->ch_nomod_error = TRUE;
2249 }
2250 return;
2251 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002252
2253 /* If the buffer is also used as input insert above the last
2254 * line. Don't write these lines. */
2255 if (save_write_to)
2256 {
2257 --lnum;
2258 buffer->b_write_to_channel = FALSE;
2259 }
2260
2261 /* Append to the buffer */
2262 ch_logn(channel, "appending line %d to buffer", (int)lnum + 1);
2263
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002264 buffer->b_p_ma = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002265 curbuf = buffer;
2266 u_sync(TRUE);
2267 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002268 ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002269
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002270 if (empty)
2271 {
2272 /* The buffer is empty, replace the first (dummy) line. */
2273 ml_replace(lnum, msg, TRUE);
2274 lnum = 0;
2275 }
2276 else
2277 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002278 appended_lines_mark(lnum, 1L);
2279 curbuf = save_curbuf;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002280 if (ch_part->ch_nomodifiable)
2281 buffer->b_p_ma = FALSE;
2282 else
2283 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002284
2285 if (buffer->b_nwindows > 0)
2286 {
2287 win_T *wp;
2288 win_T *save_curwin;
2289
2290 FOR_ALL_WINDOWS(wp)
2291 {
2292 if (wp->w_buffer == buffer
2293 && (save_write_to
2294 ? wp->w_cursor.lnum == lnum + 1
2295 : (wp->w_cursor.lnum == lnum
2296 && wp->w_cursor.col == 0)))
2297 {
2298 ++wp->w_cursor.lnum;
2299 save_curwin = curwin;
2300 curwin = wp;
2301 curbuf = curwin->w_buffer;
2302 scroll_cursor_bot(0, FALSE);
2303 curwin = save_curwin;
2304 curbuf = curwin->w_buffer;
2305 }
2306 }
2307 redraw_buf_later(buffer, VALID);
2308 channel_need_redraw = TRUE;
2309 }
2310
2311 if (save_write_to)
2312 {
2313 channel_T *ch;
2314
2315 /* Find channels reading from this buffer and adjust their
2316 * next-to-read line number. */
2317 buffer->b_write_to_channel = TRUE;
2318 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2319 {
2320 chanpart_T *in_part = &ch->ch_part[PART_IN];
2321
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002322 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002323 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2324 }
2325 }
2326}
2327
Bram Moolenaar437905c2016-04-26 19:01:05 +02002328 static void
2329drop_messages(channel_T *channel, int part)
2330{
2331 char_u *msg;
2332
2333 while ((msg = channel_get(channel, part)) != NULL)
2334 {
2335 ch_logs(channel, "Dropping message '%s'", (char *)msg);
2336 vim_free(msg);
2337 }
2338}
2339
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002340/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002341 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002342 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002343 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002344 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002345 static int
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002346may_invoke_callback(channel_T *channel, int part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002347{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002348 char_u *msg = NULL;
2349 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002350 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002351 int seq_nr = -1;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002352 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002353 cbq_T *cbhead = &channel->ch_part[part].ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002354 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002355 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002356 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002357 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002358 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002359
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002360 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002361 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002362 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002363
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002364 /* Use a message-specific callback, part callback or channel callback */
2365 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2366 if (cbitem->cq_seq_nr == 0)
2367 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002368 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002369 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002370 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002371 partial = cbitem->cq_partial;
2372 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002373 else if (channel->ch_part[part].ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002374 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002375 callback = channel->ch_part[part].ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002376 partial = channel->ch_part[part].ch_partial;
2377 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002378 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002379 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002380 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002381 partial = channel->ch_partial;
2382 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002383
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002384 buffer = channel->ch_part[part].ch_bufref.br_buf;
2385 if (buffer != NULL && !bufref_valid(&channel->ch_part[part].ch_bufref))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002386 {
2387 /* buffer was wiped out */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002388 channel->ch_part[part].ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002389 buffer = NULL;
2390 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002391
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002392 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002393 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002394 listitem_T *item;
2395 int argc = 0;
2396
Bram Moolenaard7ece102016-02-02 23:23:02 +01002397 /* Get any json message in the queue. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002398 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002399 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002400 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002401 channel_parse_json(channel, part);
2402 if (channel_get_json(channel, part, -1, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002403 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002404 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002405
Bram Moolenaarece61b02016-02-20 21:39:05 +01002406 for (item = listtv->vval.v_list->lv_first;
2407 item != NULL && argc < CH_JSON_MAX_ARGS;
2408 item = item->li_next)
2409 argv[argc++] = item->li_tv;
2410 while (argc < CH_JSON_MAX_ARGS)
2411 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002412
Bram Moolenaarece61b02016-02-20 21:39:05 +01002413 if (argv[0].v_type == VAR_STRING)
2414 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002415 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002416 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002417 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002418 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002419 }
2420
Bram Moolenaarece61b02016-02-20 21:39:05 +01002421 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002422 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002423 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002424 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002425 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002426 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002427 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002428 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002429 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002430 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002431 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002432 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002433 return FALSE;
2434 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002435 else
2436 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002437 /* If there is no callback or buffer drop the message. */
2438 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002439 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002440 /* If there is a close callback it may use ch_read() to get the
2441 * messages. */
2442 if (channel->ch_close_cb == NULL)
2443 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002444 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002445 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002446
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002447 if (ch_mode == MODE_NL)
2448 {
2449 char_u *nl;
2450 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002451 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002452
2453 /* See if we have a message ending in NL in the first buffer. If
2454 * not try to concatenate the first and the second buffer. */
2455 while (TRUE)
2456 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002457 node = channel_peek(channel, part);
2458 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002459 if (nl != NULL)
2460 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002461 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002462 return FALSE; /* incomplete message */
2463 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002464 buf = node->rq_buffer;
2465
2466 /* Convert NUL to NL, the internal representation. */
2467 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2468 if (*p == NUL)
2469 *p = NL;
2470
2471 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002472 {
2473 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002474 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002475 *nl = NUL;
2476 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002477 else
2478 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002479 /* Copy the message into allocated memory (excluding the NL)
2480 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002481 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002482 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002483 }
2484 }
2485 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002486 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002487 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002488 * get everything we have.
2489 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002490 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002491 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002492
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002493 if (msg == NULL)
2494 return FALSE; /* out of memory (and avoids Coverity warning) */
2495
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002496 argv[1].v_type = VAR_STRING;
2497 argv[1].vval.v_string = msg;
2498 }
2499
Bram Moolenaara07fec92016-02-05 21:04:08 +01002500 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002501 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002502 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002503
2504 /* invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002505 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002506 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002507 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002508 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002509 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002510 break;
2511 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002512 if (!done)
Bram Moolenaard6051b52016-02-28 15:49:03 +01002513 ch_logn(channel, "Dropping message %d without callback", seq_nr);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002514 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002515 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002516 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002517 if (buffer != NULL)
2518 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002519 if (msg == NULL)
2520 /* JSON or JS mode: re-encode the message. */
2521 msg = json_encode(listtv, ch_mode);
2522 if (msg != NULL)
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002523 append_to_buffer(buffer, msg, channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002524 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002525
Bram Moolenaar187db502016-02-27 14:44:26 +01002526 if (callback != NULL)
2527 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002528 if (cbitem != NULL)
2529 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2530 else
2531 {
2532 /* invoke the channel callback */
2533 ch_logs(channel, "Invoking channel callback %s",
2534 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002535 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002536 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002537 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002538 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002539 else
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002540 ch_log(channel, "Dropping message");
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002541
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002542 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002543 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002544 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002545
2546 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002547}
2548
2549/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002550 * Return TRUE when channel "channel" is open for writing to.
2551 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002552 */
2553 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002554channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002555{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002556 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002557 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002558}
2559
2560/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002561 * Return TRUE when channel "channel" is open for reading or writing.
2562 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002563 */
2564 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002565channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002566{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002567 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002568 || channel->CH_IN_FD != INVALID_FD
2569 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002570 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002571}
2572
2573/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002574 * Return TRUE if "channel" has JSON or other typeahead.
2575 */
2576 static int
2577channel_has_readahead(channel_T *channel, int part)
2578{
2579 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2580
2581 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2582 {
2583 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2584 jsonq_T *item = head->jq_next;
2585
2586 return item != NULL;
2587 }
2588 return channel_peek(channel, part) != NULL;
2589}
2590
2591/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002592 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002593 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002594 */
2595 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002596channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002597{
Bram Moolenaar437905c2016-04-26 19:01:05 +02002598 int part;
2599 int has_readahead = FALSE;
2600
Bram Moolenaar77073442016-02-13 23:23:53 +01002601 if (channel == NULL)
2602 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002603 if (req_part == PART_OUT)
2604 {
2605 if (channel->CH_OUT_FD != INVALID_FD)
2606 return "open";
2607 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002608 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002609 }
2610 else if (req_part == PART_ERR)
2611 {
2612 if (channel->CH_ERR_FD != INVALID_FD)
2613 return "open";
2614 if (channel_has_readahead(channel, PART_ERR))
2615 has_readahead = TRUE;
2616 }
2617 else
2618 {
2619 if (channel_is_open(channel))
2620 return "open";
2621 for (part = PART_SOCK; part <= PART_ERR; ++part)
2622 if (channel_has_readahead(channel, part))
2623 {
2624 has_readahead = TRUE;
2625 break;
2626 }
2627 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002628
2629 if (has_readahead)
2630 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002631 return "closed";
2632}
2633
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002634 static void
2635channel_part_info(channel_T *channel, dict_T *dict, char *name, int part)
2636{
2637 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002638 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002639 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002640 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002641 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002642
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002643 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002644 STRCAT(namebuf, "_");
2645 tail = STRLEN(namebuf);
2646
2647 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002648 if (chanpart->ch_fd != INVALID_FD)
2649 status = "open";
2650 else if (channel_has_readahead(channel, part))
2651 status = "buffered";
2652 else
2653 status = "closed";
2654 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002655
2656 STRCPY(namebuf + tail, "mode");
2657 switch (chanpart->ch_mode)
2658 {
2659 case MODE_NL: s = "NL"; break;
2660 case MODE_RAW: s = "RAW"; break;
2661 case MODE_JSON: s = "JSON"; break;
2662 case MODE_JS: s = "JS"; break;
2663 }
2664 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2665
2666 STRCPY(namebuf + tail, "io");
2667 if (part == PART_SOCK)
2668 s = "socket";
2669 else switch (chanpart->ch_io)
2670 {
2671 case JIO_NULL: s = "null"; break;
2672 case JIO_PIPE: s = "pipe"; break;
2673 case JIO_FILE: s = "file"; break;
2674 case JIO_BUFFER: s = "buffer"; break;
2675 case JIO_OUT: s = "out"; break;
2676 }
2677 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2678
2679 STRCPY(namebuf + tail, "timeout");
2680 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2681}
2682
2683 void
2684channel_info(channel_T *channel, dict_T *dict)
2685{
2686 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002687 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002688
2689 if (channel->ch_hostname != NULL)
2690 {
2691 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2692 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2693 channel_part_info(channel, dict, "sock", PART_SOCK);
2694 }
2695 else
2696 {
2697 channel_part_info(channel, dict, "out", PART_OUT);
2698 channel_part_info(channel, dict, "err", PART_ERR);
2699 channel_part_info(channel, dict, "in", PART_IN);
2700 }
2701}
2702
Bram Moolenaar77073442016-02-13 23:23:53 +01002703/*
2704 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002705 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002706 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002707 */
2708 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002709channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002710{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002711 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002712
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002713#ifdef FEAT_GUI
2714 channel_gui_unregister(channel);
2715#endif
2716
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002717 if (channel->CH_SOCK_FD != INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002718 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002719 sock_close(channel->CH_SOCK_FD);
2720 channel->CH_SOCK_FD = INVALID_FD;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002721 }
Bram Moolenaarde279892016-03-11 22:19:44 +01002722 may_close_part(&channel->CH_IN_FD);
2723 may_close_part(&channel->CH_OUT_FD);
2724 may_close_part(&channel->CH_ERR_FD);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002725
Bram Moolenaar8b374212016-02-24 20:43:06 +01002726 if (invoke_close_cb && channel->ch_close_cb != NULL)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002727 {
2728 typval_T argv[1];
2729 typval_T rettv;
2730 int dummy;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002731 int part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002732
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002733 /* Invoke callbacks before the close callback, since it's weird to
2734 * first invoke the close callback. Increment the refcount to avoid
2735 * the channel being freed halfway. */
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002736 ++channel->ch_refcount;
Bram Moolenaard75263c2016-04-30 16:07:23 +02002737 ch_log(channel, "Invoking callbacks before closing");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002738 for (part = PART_SOCK; part <= PART_ERR; ++part)
2739 while (may_invoke_callback(channel, part))
2740 ;
2741
2742 /* Invoke the close callback, if still set. */
2743 if (channel->ch_close_cb != NULL)
2744 {
2745 ch_logs(channel, "Invoking close callback %s",
2746 (char *)channel->ch_close_cb);
2747 argv[0].v_type = VAR_CHANNEL;
2748 argv[0].vval.v_channel = channel;
2749 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002750 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002751 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002752 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002753 channel_need_redraw = TRUE;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002754 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002755
2756 /* the callback is only called once */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002757 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002758 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002759 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002760
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002761 --channel->ch_refcount;
2762
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002763 if (channel_need_redraw)
2764 {
2765 channel_need_redraw = FALSE;
2766 redraw_after_callback();
2767 }
2768
Bram Moolenaar437905c2016-04-26 19:01:05 +02002769 /* any remaining messages are useless now */
2770 for (part = PART_SOCK; part <= PART_ERR; ++part)
2771 drop_messages(channel, part);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002772 }
2773
2774 channel->ch_nb_close_cb = NULL;
Bram Moolenaare0874f82016-01-24 20:36:41 +01002775}
2776
Bram Moolenaard04a0202016-01-26 23:30:18 +01002777/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002778 * Close the "in" part channel "channel".
2779 */
2780 void
2781channel_close_in(channel_T *channel)
2782{
2783 may_close_part(&channel->CH_IN_FD);
2784}
2785
2786/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002787 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002788 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002789 static void
2790channel_clear_one(channel_T *channel, int part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002791{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002792 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2793 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002794
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002795 while (channel_peek(channel, part) != NULL)
2796 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002797
2798 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002799 {
2800 cbq_T *node = cb_head->cq_next;
2801
2802 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002803 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002804 vim_free(node);
2805 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002806
2807 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002808 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002809 free_tv(json_head->jq_next->jq_value);
2810 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002811 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002812
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002813 free_callback(channel->ch_part[part].ch_callback,
2814 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002815 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002816 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002817}
2818
2819/*
2820 * Clear all the read buffers on "channel".
2821 */
2822 void
2823channel_clear(channel_T *channel)
2824{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002825 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002826 vim_free(channel->ch_hostname);
2827 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002828 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002829 channel_clear_one(channel, PART_OUT);
2830 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002831 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002832 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002833 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002834 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002835 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002836 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002837 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002838}
2839
Bram Moolenaar77073442016-02-13 23:23:53 +01002840#if defined(EXITFREE) || defined(PROTO)
2841 void
2842channel_free_all(void)
2843{
2844 channel_T *channel;
2845
Bram Moolenaard6051b52016-02-28 15:49:03 +01002846 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002847 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2848 channel_clear(channel);
2849}
2850#endif
2851
2852
Bram Moolenaar715d2852016-04-30 17:06:31 +02002853/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002854#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002855
2856/* Buffer size for reading incoming messages. */
2857#define MAXMSGSIZE 4096
2858
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002859#if defined(HAVE_SELECT)
2860/*
2861 * Add write fds where we are waiting for writing to be possible.
2862 */
2863 static int
2864channel_fill_wfds(int maxfd_arg, fd_set *wfds)
2865{
2866 int maxfd = maxfd_arg;
2867 channel_T *ch;
2868
2869 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2870 {
2871 chanpart_T *in_part = &ch->ch_part[PART_IN];
2872
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002873 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002874 {
2875 FD_SET((int)in_part->ch_fd, wfds);
2876 if ((int)in_part->ch_fd >= maxfd)
2877 maxfd = (int)in_part->ch_fd + 1;
2878 }
2879 }
2880 return maxfd;
2881}
2882#else
2883/*
2884 * Add write fds where we are waiting for writing to be possible.
2885 */
2886 static int
2887channel_fill_poll_write(int nfd_in, struct pollfd *fds)
2888{
2889 int nfd = nfd_in;
2890 channel_T *ch;
2891
2892 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2893 {
2894 chanpart_T *in_part = &ch->ch_part[PART_IN];
2895
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002896 if (in_part->ch_fd != INVALID_FD && in_part->ch_bufref.br_buf != NULL)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002897 {
2898 in_part->ch_poll_idx = nfd;
2899 fds[nfd].fd = in_part->ch_fd;
2900 fds[nfd].events = POLLOUT;
2901 ++nfd;
2902 }
2903 else
2904 in_part->ch_poll_idx = -1;
2905 }
2906 return nfd;
2907}
2908#endif
2909
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002910typedef enum {
2911 CW_READY,
2912 CW_NOT_READY,
2913 CW_ERROR
2914} channel_wait_result;
2915
Bram Moolenaard04a0202016-01-26 23:30:18 +01002916/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002917 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002918 * Return CW_READY when there is something to read.
2919 * Return CW_NOT_READY when there is nothing to read.
2920 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002921 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002922 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01002923channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002924{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002925 if (timeout > 0)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002926 ch_logn(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01002927
Bram Moolenaard8070362016-02-15 21:56:54 +01002928# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002929 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01002930 {
2931 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002932 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01002933 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002934 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01002935
2936 /* reading from a pipe, not a socket */
2937 while (TRUE)
2938 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002939 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
2940
2941 if (r && nread > 0)
2942 return CW_READY;
2943 if (r == 0)
2944 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002945
2946 /* perhaps write some buffer lines */
2947 channel_write_any_lines();
2948
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002949 sleep_time = deadline - GetTickCount();
2950 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01002951 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02002952 /* Wait for a little while. Very short at first, up to 10 msec
2953 * after looping a few times. */
2954 if (sleep_time > delay)
2955 sleep_time = delay;
2956 Sleep(sleep_time);
2957 delay = delay * 2;
2958 if (delay > 10)
2959 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01002960 }
Bram Moolenaard8070362016-02-15 21:56:54 +01002961 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002962 else
Bram Moolenaard8070362016-02-15 21:56:54 +01002963#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002964 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01002965#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002966 struct timeval tval;
2967 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002968 fd_set wfds;
2969 int ret;
2970 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002971
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002972 tval.tv_sec = timeout / 1000;
2973 tval.tv_usec = (timeout % 1000) * 1000;
2974 for (;;)
2975 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002976 FD_ZERO(&rfds);
2977 FD_SET((int)fd, &rfds);
2978
2979 /* Write lines to a pipe when a pipe can be written to. Need to
2980 * set this every time, some buffers may be done. */
2981 maxfd = (int)fd + 1;
2982 FD_ZERO(&wfds);
2983 maxfd = channel_fill_wfds(maxfd, &wfds);
2984
2985 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01002986# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002987 SOCK_ERRNO;
2988 if (ret == -1 && errno == EINTR)
2989 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01002990# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002991 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002992 {
2993 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002994 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02002995 channel_write_any_lines();
2996 continue;
2997 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01002998 break;
2999 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003000#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003001 for (;;)
3002 {
3003 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3004 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003005
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003006 fds[0].fd = fd;
3007 fds[0].events = POLLIN;
3008 nfd = channel_fill_poll_write(nfd, fds);
3009 if (poll(fds, nfd, timeout) > 0)
3010 {
3011 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003012 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003013 channel_write_any_lines();
3014 continue;
3015 }
3016 break;
3017 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003018#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003019 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003020 return CW_NOT_READY;
3021}
3022
3023 static void
Bram Moolenaar715d2852016-04-30 17:06:31 +02003024channel_close_on_error(channel_T *channel, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003025{
3026 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003027 ch_errors(channel, "%s(): Cannot read from channel, will close it soon",
3028 func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003029
3030 /* Queue a "DETACH" netbeans message in the command queue in order to
3031 * terminate the netbeans session later. Do not end the session here
3032 * directly as we may be running in the context of a call to
3033 * netbeans_parse_messages():
3034 * netbeans_parse_messages
3035 * -> autocmd triggered while processing the netbeans cmd
3036 * -> ui_breakcheck
3037 * -> gui event loop or select loop
3038 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003039 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003040 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003041 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaard75263c2016-04-30 16:07:23 +02003042 channel_save(channel, PART_OUT, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003043 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3044
3045 /* When reading from stdout is not possible, assume the other side has
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003046 * died. Don't close the channel right away, it may be the wrong moment
3047 * to invoke callbacks. */
3048 channel->ch_to_be_closed = TRUE;
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003049
3050#ifdef FEAT_GUI
3051 /* Stop listening to GUI events right away. */
3052 channel_gui_unregister(channel);
3053#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003054}
3055
3056 static void
3057channel_close_now(channel_T *channel)
3058{
3059 ch_log(channel, "Closing channel because of previous read error");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003060 channel_close(channel, TRUE);
3061 if (channel->ch_nb_close_cb != NULL)
3062 (*channel->ch_nb_close_cb)();
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003063}
3064
3065/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003066 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003067 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003068 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003069 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003070 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003071channel_read(channel_T *channel, int part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003072{
3073 static char_u *buf = NULL;
3074 int len = 0;
3075 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003076 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003077 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003078
Bram Moolenaar5850a762016-05-27 19:59:48 +02003079 /* If we detected a read error don't try reading again. */
3080 if (channel->ch_to_be_closed)
3081 return;
3082
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003083 fd = channel->ch_part[part].ch_fd;
3084 if (fd == INVALID_FD)
3085 {
3086 ch_error(channel, "channel_read() called while socket is closed");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003087 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003088 }
3089 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090
3091 /* Allocate a buffer to read into. */
3092 if (buf == NULL)
3093 {
3094 buf = alloc(MAXMSGSIZE);
3095 if (buf == NULL)
3096 return; /* out of memory! */
3097 }
3098
3099 /* Keep on reading for as long as there is something to read.
3100 * Use select() or poll() to avoid blocking on a message that is exactly
3101 * MAXMSGSIZE long. */
3102 for (;;)
3103 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003104 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003105 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003106 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003107 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003108 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003109 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003110 if (len <= 0)
3111 break; /* error or nothing more to read */
3112
3113 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003114 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003115 readlen += len;
3116 if (len < MAXMSGSIZE)
3117 break; /* did read everything that's available */
3118 }
3119
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003120 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003121 if (readlen <= 0)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003122 channel_close_on_error(channel, func);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003123
3124#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003125 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003126 if (CH_HAS_GUI && gtk_main_level() > 0)
3127 gtk_main_quit();
3128#endif
3129}
3130
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003131/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003132 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003133 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003134 * Returns what was read in allocated memory.
3135 * Returns NULL in case of error or timeout.
3136 */
3137 char_u *
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003138channel_read_block(channel_T *channel, int part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003139{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003140 char_u *buf;
3141 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003142 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003143 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003144 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003145 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003146
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003147 ch_logsn(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003148 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003149
3150 while (TRUE)
3151 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003152 node = channel_peek(channel, part);
3153 if (node != NULL)
3154 {
3155 if (mode == MODE_RAW || (mode == MODE_NL
3156 && channel_first_nl(node) != NULL))
3157 /* got a complete message */
3158 break;
3159 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3160 continue;
3161 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003162
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003163 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003164 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003165 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003166 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003167 {
3168 ch_log(channel, "Timed out");
3169 return NULL;
3170 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003171 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003172 }
3173
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003174 if (mode == MODE_RAW)
3175 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003176 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003177 }
3178 else
3179 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003180 char_u *p;
3181
3182 buf = node->rq_buffer;
3183 nl = channel_first_nl(node);
3184
3185 /* Convert NUL to NL, the internal representation. */
3186 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3187 if (*p == NUL)
3188 *p = NL;
3189
3190 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003191 {
3192 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003193 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003194 *nl = NUL;
3195 }
3196 else
3197 {
3198 /* Copy the message into allocated memory and remove it from the
3199 * buffer. */
3200 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003201 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003202 }
3203 }
3204 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003205 ch_logn(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003206 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003207}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003208
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003209/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003210 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003211 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003212 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003213 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003214 */
3215 int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003216channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003217 channel_T *channel,
3218 int part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003219 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003220 int id,
3221 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003222{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003223 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003224 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003225 int timeout;
3226 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003227
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003228 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003229 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003230 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003231 for (;;)
3232 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003233 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003234
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003235 /* search for message "id" */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003236 if (channel_get_json(channel, part, id, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003237 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003238 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003239 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003240 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003241
Bram Moolenaard7ece102016-02-02 23:23:02 +01003242 if (!more)
3243 {
3244 /* Handle any other messages in the queue. If done some more
3245 * messages may have arrived. */
3246 if (channel_parse_messages())
3247 continue;
3248
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003249 /* Wait for up to the timeout. If there was an incomplete message
3250 * use the deadline for that. */
3251 timeout = timeout_arg;
3252 if (chanpart->ch_waiting)
3253 {
3254#ifdef WIN32
3255 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3256#else
3257 {
3258 struct timeval now_tv;
3259
3260 gettimeofday(&now_tv, NULL);
3261 timeout = (chanpart->ch_deadline.tv_sec
3262 - now_tv.tv_sec) * 1000
3263 + (chanpart->ch_deadline.tv_usec
3264 - now_tv.tv_usec) / 1000
3265 + 1;
3266 }
3267#endif
3268 if (timeout < 0)
3269 {
3270 /* Something went wrong, channel_parse_json() didn't
3271 * discard message. Cancel waiting. */
3272 chanpart->ch_waiting = FALSE;
3273 timeout = timeout_arg;
3274 }
3275 else if (timeout > timeout_arg)
3276 timeout = timeout_arg;
3277 }
3278 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003279 if (fd == INVALID_FD
3280 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003281 {
3282 if (timeout == timeout_arg)
3283 {
3284 if (fd != INVALID_FD)
3285 ch_log(channel, "Timed out");
3286 break;
3287 }
3288 }
3289 else
3290 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003291 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003292 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003293 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003294 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003295}
3296
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003297/*
3298 * Common for ch_read() and ch_readraw().
3299 */
3300 void
3301common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3302{
3303 channel_T *channel;
Bram Moolenaar437905c2016-04-26 19:01:05 +02003304 int part = -1;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003305 jobopt_T opt;
3306 int mode;
3307 int timeout;
3308 int id = -1;
3309 typval_T *listtv = NULL;
3310
3311 /* return an empty string by default */
3312 rettv->v_type = VAR_STRING;
3313 rettv->vval.v_string = NULL;
3314
3315 clear_job_options(&opt);
3316 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
3317 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003318 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003319
Bram Moolenaar437905c2016-04-26 19:01:05 +02003320 if (opt.jo_set & JO_PART)
3321 part = opt.jo_part;
3322 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003323 if (channel != NULL)
3324 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02003325 if (part < 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003326 part = channel_part_read(channel);
3327 mode = channel_get_mode(channel, part);
3328 timeout = channel_get_timeout(channel, part);
3329 if (opt.jo_set & JO_TIMEOUT)
3330 timeout = opt.jo_timeout;
3331
3332 if (raw || mode == MODE_RAW || mode == MODE_NL)
3333 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3334 else
3335 {
3336 if (opt.jo_set & JO_ID)
3337 id = opt.jo_id;
3338 channel_read_json_block(channel, part, timeout, id, &listtv);
3339 if (listtv != NULL)
3340 {
3341 *rettv = *listtv;
3342 vim_free(listtv);
3343 }
3344 else
3345 {
3346 rettv->v_type = VAR_SPECIAL;
3347 rettv->vval.v_number = VVAL_NONE;
3348 }
3349 }
3350 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003351
3352theend:
3353 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003354}
3355
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003356# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3357 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003358/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003359 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003360 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003361 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003362 channel_T *
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003363channel_fd2channel(sock_T fd, int *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003364{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003365 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003366 int part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003367
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003368 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003369 for (channel = first_channel; channel != NULL;
3370 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003371 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003372 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003373 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003374 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003375 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003376 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003377 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003378 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003379 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003380}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003381# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003382
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003383# if defined(WIN32) || defined(PROTO)
3384/*
3385 * Check the channels for anything that is ready to be read.
3386 * The data is put in the read queue.
3387 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003388 void
3389channel_handle_events(void)
3390{
3391 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003392 int part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003393 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003394
3395 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3396 {
Bram Moolenaar5850a762016-05-27 19:59:48 +02003397 /* If we detected a read error don't try reading again. */
3398 if (channel->ch_to_be_closed)
3399 continue;
3400
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003401 /* check the socket and pipes */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003402 for (part = PART_SOCK; part <= PART_ERR; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003403 {
3404 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003405 if (fd != INVALID_FD)
3406 {
3407 int r = channel_wait(channel, fd, 0);
3408
3409 if (r == CW_READY)
3410 channel_read(channel, part, "channel_handle_events");
3411 else if (r == CW_ERROR)
Bram Moolenaar715d2852016-04-30 17:06:31 +02003412 channel_close_on_error(channel, "channel_handle_events()");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003413 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003414 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003415 }
3416}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003417# endif
3418
Bram Moolenaard04a0202016-01-26 23:30:18 +01003419/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003420 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003421 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003422 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003423 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003424 int
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003425channel_send(channel_T *channel, int part, char_u *buf, int len, char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003426{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003427 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003428 sock_T fd;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003429
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003430 fd = channel->ch_part[part].ch_fd;
3431 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003432 {
3433 if (!channel->ch_error && fun != NULL)
3434 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003435 ch_errors(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003436 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003437 }
3438 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003439 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003440 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003441
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003442 if (log_fd != NULL)
3443 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003444 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003445 fprintf(log_fd, "'");
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003446 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003447 fprintf(log_fd, "'\n");
3448 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003449 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003450 }
3451
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003452 if (part == PART_SOCK)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003453 res = sock_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003454 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003455 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003456 if (res != len)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003457 {
3458 if (!channel->ch_error && fun != NULL)
3459 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003460 ch_errors(channel, "%s(): write failed", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003461 EMSG2(_("E631: %s(): write failed"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003462 }
3463 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003464 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003465 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003466
3467 channel->ch_error = FALSE;
3468 return OK;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003469}
3470
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003471/*
3472 * Common for "ch_sendexpr()" and "ch_sendraw()".
3473 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003474 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003475 * Otherwise returns NULL.
3476 */
3477 channel_T *
3478send_common(
3479 typval_T *argvars,
3480 char_u *text,
3481 int id,
3482 int eval,
3483 jobopt_T *opt,
3484 char *fun,
3485 int *part_read)
3486{
3487 channel_T *channel;
3488 int part_send;
3489
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003490 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003491 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003492 if (channel == NULL)
3493 return NULL;
3494 part_send = channel_part_send(channel);
3495 *part_read = channel_part_read(channel);
3496
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003497 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
3498 return NULL;
3499
3500 /* Set the callback. An empty callback means no callback and not reading
3501 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3502 * allowed. */
3503 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3504 {
3505 if (eval)
3506 {
3507 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3508 return NULL;
3509 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003510 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003511 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003512 }
3513
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003514 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003515 && opt->jo_callback == NULL)
3516 return channel;
3517 return NULL;
3518}
3519
3520/*
3521 * common for "ch_evalexpr()" and "ch_sendexpr()"
3522 */
3523 void
3524ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3525{
3526 char_u *text;
3527 typval_T *listtv;
3528 channel_T *channel;
3529 int id;
3530 ch_mode_T ch_mode;
3531 int part_send;
3532 int part_read;
3533 jobopt_T opt;
3534 int timeout;
3535
3536 /* return an empty string by default */
3537 rettv->v_type = VAR_STRING;
3538 rettv->vval.v_string = NULL;
3539
Bram Moolenaar437905c2016-04-26 19:01:05 +02003540 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003541 if (channel == NULL)
3542 return;
3543 part_send = channel_part_send(channel);
3544
3545 ch_mode = channel_get_mode(channel, part_send);
3546 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3547 {
3548 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3549 return;
3550 }
3551
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003552 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003553 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003554 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003555 if (text == NULL)
3556 return;
3557
3558 channel = send_common(argvars, text, id, eval, &opt,
3559 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3560 vim_free(text);
3561 if (channel != NULL && eval)
3562 {
3563 if (opt.jo_set & JO_TIMEOUT)
3564 timeout = opt.jo_timeout;
3565 else
3566 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003567 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3568 == OK)
3569 {
3570 list_T *list = listtv->vval.v_list;
3571
3572 /* Move the item from the list and then change the type to
3573 * avoid the value being freed. */
3574 *rettv = list->lv_last->li_tv;
3575 list->lv_last->li_tv.v_type = VAR_NUMBER;
3576 free_tv(listtv);
3577 }
3578 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003579 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003580}
3581
3582/*
3583 * common for "ch_evalraw()" and "ch_sendraw()"
3584 */
3585 void
3586ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3587{
3588 char_u buf[NUMBUFLEN];
3589 char_u *text;
3590 channel_T *channel;
3591 int part_read;
3592 jobopt_T opt;
3593 int timeout;
3594
3595 /* return an empty string by default */
3596 rettv->v_type = VAR_STRING;
3597 rettv->vval.v_string = NULL;
3598
3599 text = get_tv_string_buf(&argvars[1], buf);
3600 channel = send_common(argvars, text, 0, eval, &opt,
3601 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3602 if (channel != NULL && eval)
3603 {
3604 if (opt.jo_set & JO_TIMEOUT)
3605 timeout = opt.jo_timeout;
3606 else
3607 timeout = channel_get_timeout(channel, part_read);
3608 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3609 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003610 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003611}
3612
Bram Moolenaard04a0202016-01-26 23:30:18 +01003613# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003614/*
3615 * Add open channels to the poll struct.
3616 * Return the adjusted struct index.
3617 * The type of "fds" is hidden to avoid problems with the function proto.
3618 */
3619 int
3620channel_poll_setup(int nfd_in, void *fds_in)
3621{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003622 int nfd = nfd_in;
3623 channel_T *channel;
3624 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003625 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003626
Bram Moolenaar77073442016-02-13 23:23:53 +01003627 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003628 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003629 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003630 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003631 chanpart_T *ch_part = &channel->ch_part[part];
3632
3633 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003634 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003635 ch_part->ch_poll_idx = nfd;
3636 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003637 fds[nfd].events = POLLIN;
3638 nfd++;
3639 }
3640 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003641 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003642 }
3643 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003644
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003645 nfd = channel_fill_poll_write(nfd, fds);
3646
Bram Moolenaare0874f82016-01-24 20:36:41 +01003647 return nfd;
3648}
3649
3650/*
3651 * The type of "fds" is hidden to avoid problems with the function proto.
3652 */
3653 int
3654channel_poll_check(int ret_in, void *fds_in)
3655{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003656 int ret = ret_in;
3657 channel_T *channel;
3658 struct pollfd *fds = fds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003659 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003660 int idx;
3661 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003662
Bram Moolenaar77073442016-02-13 23:23:53 +01003663 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003664 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003665 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003666 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003667 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003668
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003669 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003670 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003671 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003672 --ret;
3673 }
3674 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003675
3676 in_part = &channel->ch_part[PART_IN];
3677 idx = in_part->ch_poll_idx;
3678 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3679 {
3680 if (in_part->ch_buf_append)
3681 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003682 if (in_part->ch_bufref.br_buf != NULL)
3683 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003684 }
3685 else
3686 channel_write_in(channel);
3687 --ret;
3688 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003689 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003690
3691 return ret;
3692}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003693# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003694
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003695# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003696/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003697 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003698 */
3699 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003700channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003701{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003702 int maxfd = maxfd_in;
3703 channel_T *channel;
3704 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003705 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003706 int part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003707
Bram Moolenaar77073442016-02-13 23:23:53 +01003708 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003709 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003710 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003711 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003712 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003713
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003714 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003715 {
Bram Moolenaard8070362016-02-15 21:56:54 +01003716 FD_SET((int)fd, rfds);
3717 if (maxfd < (int)fd)
3718 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003719 }
3720 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003721 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003722
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003723 maxfd = channel_fill_wfds(maxfd, wfds);
3724
Bram Moolenaare0874f82016-01-24 20:36:41 +01003725 return maxfd;
3726}
3727
3728/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003729 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01003730 */
3731 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003732channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003733{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003734 int ret = ret_in;
3735 channel_T *channel;
3736 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003737 fd_set *wfds = wfds_in;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003738 int part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003739 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003740
Bram Moolenaar77073442016-02-13 23:23:53 +01003741 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003742 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003743 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003744 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003745 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003746
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003747 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003748 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003749 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003750 --ret;
3751 }
3752 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003753
3754 in_part = &channel->ch_part[PART_IN];
3755 if (ret > 0 && in_part->ch_fd != INVALID_FD
3756 && FD_ISSET(in_part->ch_fd, wfds))
3757 {
3758 if (in_part->ch_buf_append)
3759 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02003760 if (in_part->ch_bufref.br_buf != NULL)
3761 channel_write_new_lines(in_part->ch_bufref.br_buf);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003762 }
3763 else
3764 channel_write_in(channel);
3765 --ret;
3766 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003767 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003768
3769 return ret;
3770}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003771# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003772
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003773/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01003774 * Execute queued up commands.
3775 * Invoked from the main loop when it's safe to execute received commands.
3776 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003777 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01003778 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003779channel_parse_messages(void)
3780{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003781 channel_T *channel = first_channel;
3782 int ret = FALSE;
3783 int r;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003784 int part = PART_SOCK;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003785
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003786 ++safe_to_invoke_callback;
3787
Bram Moolenaard0b65022016-03-06 21:50:33 +01003788 /* Only do this message when another message was given, otherwise we get
3789 * lots of them. */
3790 if (did_log_msg)
3791 {
3792 ch_log(NULL, "looking for messages on channels");
3793 did_log_msg = FALSE;
3794 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003795 while (channel != NULL)
3796 {
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003797 if (channel->ch_to_be_closed)
3798 {
3799 channel->ch_to_be_closed = FALSE;
3800 channel_close_now(channel);
3801 /* channel may have been freed, start over */
3802 channel = first_channel;
3803 continue;
3804 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003805 if (channel->ch_to_be_freed)
3806 {
3807 channel_free(channel);
3808 /* channel has been freed, start over */
3809 channel = first_channel;
3810 continue;
3811 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01003812 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003813 {
3814 /* channel is no longer useful, free it */
3815 channel_free(channel);
3816 channel = first_channel;
3817 part = PART_SOCK;
3818 continue;
3819 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003820 if (channel->ch_part[part].ch_fd != INVALID_FD
3821 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01003822 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003823 /* Increase the refcount, in case the handler causes the channel
3824 * to be unreferenced or closed. */
3825 ++channel->ch_refcount;
3826 r = may_invoke_callback(channel, part);
3827 if (r == OK)
3828 ret = TRUE;
3829 if (channel_unref(channel) || r == OK)
3830 {
3831 /* channel was freed or something was done, start over */
3832 channel = first_channel;
3833 part = PART_SOCK;
3834 continue;
3835 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01003836 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003837 if (part < PART_ERR)
3838 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003839 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003840 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003841 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003842 part = PART_SOCK;
3843 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003844 }
Bram Moolenaar187db502016-02-27 14:44:26 +01003845
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003846 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01003847 {
3848 channel_need_redraw = FALSE;
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02003849 redraw_after_callback();
Bram Moolenaar187db502016-02-27 14:44:26 +01003850 }
3851
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02003852 --safe_to_invoke_callback;
3853
Bram Moolenaard7ece102016-02-02 23:23:02 +01003854 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01003855}
3856
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01003857/*
3858 * Mark references to lists used in channels.
3859 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003860 int
3861set_ref_in_channel(int copyID)
3862{
Bram Moolenaar77073442016-02-13 23:23:53 +01003863 int abort = FALSE;
3864 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003865 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003866
Bram Moolenaar77073442016-02-13 23:23:53 +01003867 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003868 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003869 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003870 tv.v_type = VAR_CHANNEL;
3871 tv.vval.v_channel = channel;
3872 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003873 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003874 return abort;
3875}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003876
3877/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003878 * Return the "part" to write to for "channel".
3879 */
3880 int
3881channel_part_send(channel_T *channel)
3882{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003883 if (channel->CH_SOCK_FD == INVALID_FD)
3884 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003885 return PART_SOCK;
3886}
3887
3888/*
3889 * Return the default "part" to read from for "channel".
3890 */
3891 int
3892channel_part_read(channel_T *channel)
3893{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003894 if (channel->CH_SOCK_FD == INVALID_FD)
3895 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003896 return PART_SOCK;
3897}
3898
3899/*
3900 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01003901 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003902 */
3903 ch_mode_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003904channel_get_mode(channel_T *channel, int part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003905{
Bram Moolenaar77073442016-02-13 23:23:53 +01003906 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003907 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003908 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01003909}
3910
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003911/*
3912 * Return the timeout of "channel"/"part"
3913 */
3914 int
3915channel_get_timeout(channel_T *channel, int part)
3916{
3917 return channel->ch_part[part].ch_timeout;
3918}
3919
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003920 static int
3921handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
3922{
3923 char_u *val = get_tv_string(item);
3924
3925 opt->jo_set |= jo;
3926 if (STRCMP(val, "nl") == 0)
3927 *modep = MODE_NL;
3928 else if (STRCMP(val, "raw") == 0)
3929 *modep = MODE_RAW;
3930 else if (STRCMP(val, "js") == 0)
3931 *modep = MODE_JS;
3932 else if (STRCMP(val, "json") == 0)
3933 *modep = MODE_JSON;
3934 else
3935 {
3936 EMSG2(_(e_invarg2), val);
3937 return FAIL;
3938 }
3939 return OK;
3940}
3941
3942 static int
3943handle_io(typval_T *item, int part, jobopt_T *opt)
3944{
3945 char_u *val = get_tv_string(item);
3946
3947 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
3948 if (STRCMP(val, "null") == 0)
3949 opt->jo_io[part] = JIO_NULL;
3950 else if (STRCMP(val, "pipe") == 0)
3951 opt->jo_io[part] = JIO_PIPE;
3952 else if (STRCMP(val, "file") == 0)
3953 opt->jo_io[part] = JIO_FILE;
3954 else if (STRCMP(val, "buffer") == 0)
3955 opt->jo_io[part] = JIO_BUFFER;
3956 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
3957 opt->jo_io[part] = JIO_OUT;
3958 else
3959 {
3960 EMSG2(_(e_invarg2), val);
3961 return FAIL;
3962 }
3963 return OK;
3964}
3965
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003966/*
3967 * Clear a jobopt_T before using it.
3968 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003969 void
3970clear_job_options(jobopt_T *opt)
3971{
3972 vim_memset(opt, 0, sizeof(jobopt_T));
3973}
3974
3975/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003976 * Free any members of a jobopt_T.
3977 */
3978 void
3979free_job_options(jobopt_T *opt)
3980{
3981 if (opt->jo_partial != NULL)
3982 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003983 else if (opt->jo_callback != NULL)
3984 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003985 if (opt->jo_out_partial != NULL)
3986 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003987 else if (opt->jo_out_cb != NULL)
3988 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003989 if (opt->jo_err_partial != NULL)
3990 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003991 else if (opt->jo_err_cb != NULL)
3992 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003993 if (opt->jo_close_partial != NULL)
3994 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003995 else if (opt->jo_close_cb != NULL)
3996 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02003997 if (opt->jo_exit_partial != NULL)
3998 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003999 else if (opt->jo_exit_cb != NULL)
4000 func_unref(opt->jo_exit_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004001}
4002
4003/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004004 * Get the PART_ number from the first character of an option name.
4005 */
4006 static int
4007part_from_char(int c)
4008{
4009 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4010}
4011
4012/*
4013 * Get the option entries from the dict in "tv", parse them and put the result
4014 * in "opt".
4015 * Only accept options in "supported".
4016 * If an option value is invalid return FAIL.
4017 */
4018 int
4019get_job_options(typval_T *tv, jobopt_T *opt, int supported)
4020{
4021 typval_T *item;
4022 char_u *val;
4023 dict_T *dict;
4024 int todo;
4025 hashitem_T *hi;
4026 int part;
4027
4028 opt->jo_set = 0;
4029 if (tv->v_type == VAR_UNKNOWN)
4030 return OK;
4031 if (tv->v_type != VAR_DICT)
4032 {
4033 EMSG(_(e_invarg));
4034 return FAIL;
4035 }
4036 dict = tv->vval.v_dict;
4037 if (dict == NULL)
4038 return OK;
4039
4040 todo = (int)dict->dv_hashtab.ht_used;
4041 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4042 if (!HASHITEM_EMPTY(hi))
4043 {
4044 item = &dict_lookup(hi)->di_tv;
4045
4046 if (STRCMP(hi->hi_key, "mode") == 0)
4047 {
4048 if (!(supported & JO_MODE))
4049 break;
4050 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4051 return FAIL;
4052 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004053 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004054 {
4055 if (!(supported & JO_IN_MODE))
4056 break;
4057 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4058 == FAIL)
4059 return FAIL;
4060 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004061 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062 {
4063 if (!(supported & JO_OUT_MODE))
4064 break;
4065 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4066 == FAIL)
4067 return FAIL;
4068 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004069 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004070 {
4071 if (!(supported & JO_ERR_MODE))
4072 break;
4073 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4074 == FAIL)
4075 return FAIL;
4076 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004077 else if (STRCMP(hi->hi_key, "in_io") == 0
4078 || STRCMP(hi->hi_key, "out_io") == 0
4079 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004080 {
4081 if (!(supported & JO_OUT_IO))
4082 break;
4083 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4084 return FAIL;
4085 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004086 else if (STRCMP(hi->hi_key, "in_name") == 0
4087 || STRCMP(hi->hi_key, "out_name") == 0
4088 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004089 {
4090 part = part_from_char(*hi->hi_key);
4091
4092 if (!(supported & JO_OUT_IO))
4093 break;
4094 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4095 opt->jo_io_name[part] =
4096 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4097 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004098 else if (STRCMP(hi->hi_key, "in_buf") == 0
4099 || STRCMP(hi->hi_key, "out_buf") == 0
4100 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004101 {
4102 part = part_from_char(*hi->hi_key);
4103
4104 if (!(supported & JO_OUT_IO))
4105 break;
4106 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4107 opt->jo_io_buf[part] = get_tv_number(item);
4108 if (opt->jo_io_buf[part] <= 0)
4109 {
4110 EMSG2(_(e_invarg2), get_tv_string(item));
4111 return FAIL;
4112 }
4113 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4114 {
4115 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4116 return FAIL;
4117 }
4118 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004119 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4120 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4121 {
4122 part = part_from_char(*hi->hi_key);
4123
4124 if (!(supported & JO_OUT_IO))
4125 break;
4126 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4127 opt->jo_modifiable[part] = get_tv_number(item);
4128 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004129 else if (STRCMP(hi->hi_key, "out_msg") == 0
4130 || STRCMP(hi->hi_key, "err_msg") == 0)
4131 {
4132 part = part_from_char(*hi->hi_key);
4133
4134 if (!(supported & JO_OUT_IO))
4135 break;
4136 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4137 opt->jo_message[part] = get_tv_number(item);
4138 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004139 else if (STRCMP(hi->hi_key, "in_top") == 0
4140 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004141 {
4142 linenr_T *lp;
4143
4144 if (!(supported & JO_OUT_IO))
4145 break;
4146 if (hi->hi_key[3] == 't')
4147 {
4148 lp = &opt->jo_in_top;
4149 opt->jo_set |= JO_IN_TOP;
4150 }
4151 else
4152 {
4153 lp = &opt->jo_in_bot;
4154 opt->jo_set |= JO_IN_BOT;
4155 }
4156 *lp = get_tv_number(item);
4157 if (*lp < 0)
4158 {
4159 EMSG2(_(e_invarg2), get_tv_string(item));
4160 return FAIL;
4161 }
4162 }
4163 else if (STRCMP(hi->hi_key, "channel") == 0)
4164 {
4165 if (!(supported & JO_OUT_IO))
4166 break;
4167 opt->jo_set |= JO_CHANNEL;
4168 if (item->v_type != VAR_CHANNEL)
4169 {
4170 EMSG2(_(e_invarg2), "channel");
4171 return FAIL;
4172 }
4173 opt->jo_channel = item->vval.v_channel;
4174 }
4175 else if (STRCMP(hi->hi_key, "callback") == 0)
4176 {
4177 if (!(supported & JO_CALLBACK))
4178 break;
4179 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004180 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004181 if (opt->jo_callback == NULL)
4182 {
4183 EMSG2(_(e_invarg2), "callback");
4184 return FAIL;
4185 }
4186 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004187 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004188 {
4189 if (!(supported & JO_OUT_CALLBACK))
4190 break;
4191 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004192 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004193 if (opt->jo_out_cb == NULL)
4194 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004195 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004196 return FAIL;
4197 }
4198 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004199 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004200 {
4201 if (!(supported & JO_ERR_CALLBACK))
4202 break;
4203 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004204 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004205 if (opt->jo_err_cb == NULL)
4206 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004207 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004208 return FAIL;
4209 }
4210 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004211 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004212 {
4213 if (!(supported & JO_CLOSE_CALLBACK))
4214 break;
4215 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004216 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004217 if (opt->jo_close_cb == NULL)
4218 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004219 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004220 return FAIL;
4221 }
4222 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004223 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4224 {
4225 if (!(supported & JO_EXIT_CB))
4226 break;
4227 opt->jo_set |= JO_EXIT_CB;
4228 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4229 if (opt->jo_exit_cb == NULL)
4230 {
4231 EMSG2(_(e_invarg2), "exit_cb");
4232 return FAIL;
4233 }
4234 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004235 else if (STRCMP(hi->hi_key, "waittime") == 0)
4236 {
4237 if (!(supported & JO_WAITTIME))
4238 break;
4239 opt->jo_set |= JO_WAITTIME;
4240 opt->jo_waittime = get_tv_number(item);
4241 }
4242 else if (STRCMP(hi->hi_key, "timeout") == 0)
4243 {
4244 if (!(supported & JO_TIMEOUT))
4245 break;
4246 opt->jo_set |= JO_TIMEOUT;
4247 opt->jo_timeout = get_tv_number(item);
4248 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004249 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004250 {
4251 if (!(supported & JO_OUT_TIMEOUT))
4252 break;
4253 opt->jo_set |= JO_OUT_TIMEOUT;
4254 opt->jo_out_timeout = get_tv_number(item);
4255 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004256 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004257 {
4258 if (!(supported & JO_ERR_TIMEOUT))
4259 break;
4260 opt->jo_set |= JO_ERR_TIMEOUT;
4261 opt->jo_err_timeout = get_tv_number(item);
4262 }
4263 else if (STRCMP(hi->hi_key, "part") == 0)
4264 {
4265 if (!(supported & JO_PART))
4266 break;
4267 opt->jo_set |= JO_PART;
4268 val = get_tv_string(item);
4269 if (STRCMP(val, "err") == 0)
4270 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004271 else if (STRCMP(val, "out") == 0)
4272 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004273 else
4274 {
4275 EMSG2(_(e_invarg2), val);
4276 return FAIL;
4277 }
4278 }
4279 else if (STRCMP(hi->hi_key, "id") == 0)
4280 {
4281 if (!(supported & JO_ID))
4282 break;
4283 opt->jo_set |= JO_ID;
4284 opt->jo_id = get_tv_number(item);
4285 }
4286 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4287 {
4288 if (!(supported & JO_STOPONEXIT))
4289 break;
4290 opt->jo_set |= JO_STOPONEXIT;
4291 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4292 opt->jo_soe_buf);
4293 if (opt->jo_stoponexit == NULL)
4294 {
4295 EMSG2(_(e_invarg2), "stoponexit");
4296 return FAIL;
4297 }
4298 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004299 else if (STRCMP(hi->hi_key, "block_write") == 0)
4300 {
4301 if (!(supported & JO_BLOCK_WRITE))
4302 break;
4303 opt->jo_set |= JO_BLOCK_WRITE;
4304 opt->jo_block_write = get_tv_number(item);
4305 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004306 else
4307 break;
4308 --todo;
4309 }
4310 if (todo > 0)
4311 {
4312 EMSG2(_(e_invarg2), hi->hi_key);
4313 return FAIL;
4314 }
4315
4316 return OK;
4317}
4318
4319/*
4320 * Get the channel from the argument.
4321 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004322 * When "check_open" is TRUE check that the channel can be used.
4323 * When "reading" is TRUE "check_open" considers typeahead useful.
4324 * "part" is used to check typeahead, when -1 use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004325 */
4326 channel_T *
Bram Moolenaar437905c2016-04-26 19:01:05 +02004327get_channel_arg(typval_T *tv, int check_open, int reading, int part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004328{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004329 channel_T *channel = NULL;
4330 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004331
4332 if (tv->v_type == VAR_JOB)
4333 {
4334 if (tv->vval.v_job != NULL)
4335 channel = tv->vval.v_job->jv_channel;
4336 }
4337 else if (tv->v_type == VAR_CHANNEL)
4338 {
4339 channel = tv->vval.v_channel;
4340 }
4341 else
4342 {
4343 EMSG2(_(e_invarg2), get_tv_string(tv));
4344 return NULL;
4345 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004346 if (channel != NULL && reading)
4347 has_readahead = channel_has_readahead(channel,
4348 part >= 0 ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004349
Bram Moolenaar437905c2016-04-26 19:01:05 +02004350 if (check_open && (channel == NULL || (!channel_is_open(channel)
4351 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004352 {
4353 EMSG(_("E906: not an open channel"));
4354 return NULL;
4355 }
4356 return channel;
4357}
4358
4359static job_T *first_job = NULL;
4360
4361 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004362job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004363{
4364 ch_log(job->jv_channel, "Freeing job");
4365 if (job->jv_channel != NULL)
4366 {
4367 /* The link from the channel to the job doesn't count as a reference,
4368 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004369 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004370 * NULL the reference. We don't set ch_job_killed, unreferencing the
4371 * job doesn't mean it stops running. */
4372 job->jv_channel->ch_job = NULL;
4373 channel_unref(job->jv_channel);
4374 }
4375 mch_clear_job(job);
4376
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004377 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004378 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004379}
4380
4381 static void
4382job_free_job(job_T *job)
4383{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004384 if (job->jv_next != NULL)
4385 job->jv_next->jv_prev = job->jv_prev;
4386 if (job->jv_prev == NULL)
4387 first_job = job->jv_next;
4388 else
4389 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004390 vim_free(job);
4391}
4392
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004393 static void
4394job_free(job_T *job)
4395{
4396 if (!in_free_unref_items)
4397 {
4398 job_free_contents(job);
4399 job_free_job(job);
4400 }
4401}
4402
Bram Moolenaar655da312016-05-28 22:22:34 +02004403#if defined(EXITFREE) || defined(PROTO)
4404 void
4405job_free_all(void)
4406{
4407 while (first_job != NULL)
4408 job_free(first_job);
4409}
4410#endif
4411
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004412/*
4413 * Return TRUE if the job should not be freed yet. Do not free the job when
Bram Moolenaar674127e2016-04-26 20:30:07 +02004414 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4415 * or when the associated channel will do something with the job output.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004416 */
4417 static int
4418job_still_useful(job_T *job)
4419{
4420 return job->jv_status == JOB_STARTED
Bram Moolenaar674127e2016-04-26 20:30:07 +02004421 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL
4422 || (job->jv_channel != NULL
4423 && channel_still_useful(job->jv_channel)));
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004424}
4425
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004426/*
4427 * Mark references in jobs that are still useful.
4428 */
4429 int
4430set_ref_in_job(int copyID)
4431{
4432 int abort = FALSE;
4433 job_T *job;
4434 typval_T tv;
4435
4436 for (job = first_job; job != NULL; job = job->jv_next)
4437 if (job_still_useful(job))
4438 {
4439 tv.v_type = VAR_JOB;
4440 tv.vval.v_job = job;
4441 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4442 }
4443 return abort;
4444}
4445
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004446 void
4447job_unref(job_T *job)
4448{
4449 if (job != NULL && --job->jv_refcount <= 0)
4450 {
4451 /* Do not free the job when it has not ended yet and there is a
4452 * "stoponexit" flag or an exit callback. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004453 if (!job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004454 {
4455 job_free(job);
4456 }
Bram Moolenaar674127e2016-04-26 20:30:07 +02004457 else if (job->jv_channel != NULL
4458 && !channel_still_useful(job->jv_channel))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004459 {
4460 /* Do remove the link to the channel, otherwise it hangs
4461 * around until Vim exits. See job_free() for refcount. */
Bram Moolenaar674127e2016-04-26 20:30:07 +02004462 ch_log(job->jv_channel, "detaching channel from job");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004463 job->jv_channel->ch_job = NULL;
4464 channel_unref(job->jv_channel);
4465 job->jv_channel = NULL;
4466 }
4467 }
4468}
4469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004470 int
4471free_unused_jobs_contents(int copyID, int mask)
4472{
4473 int did_free = FALSE;
4474 job_T *job;
4475
4476 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004477 if ((job->jv_copyID & mask) != (copyID & mask)
4478 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004479 {
4480 /* Free the channel and ordinary items it contains, but don't
4481 * recurse into Lists, Dictionaries etc. */
4482 job_free_contents(job);
4483 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004484 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004485 return did_free;
4486}
4487
4488 void
4489free_unused_jobs(int copyID, int mask)
4490{
4491 job_T *job;
4492 job_T *job_next;
4493
4494 for (job = first_job; job != NULL; job = job_next)
4495 {
4496 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004497 if ((job->jv_copyID & mask) != (copyID & mask)
4498 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004499 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004500 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004501 job_free_job(job);
4502 }
4503 }
4504}
4505
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004506/*
4507 * Allocate a job. Sets the refcount to one and sets options default.
4508 */
4509 static job_T *
4510job_alloc(void)
4511{
4512 job_T *job;
4513
4514 job = (job_T *)alloc_clear(sizeof(job_T));
4515 if (job != NULL)
4516 {
4517 job->jv_refcount = 1;
4518 job->jv_stoponexit = vim_strsave((char_u *)"term");
4519
4520 if (first_job != NULL)
4521 {
4522 first_job->jv_prev = job;
4523 job->jv_next = first_job;
4524 }
4525 first_job = job;
4526 }
4527 return job;
4528}
4529
4530 void
4531job_set_options(job_T *job, jobopt_T *opt)
4532{
4533 if (opt->jo_set & JO_STOPONEXIT)
4534 {
4535 vim_free(job->jv_stoponexit);
4536 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
4537 job->jv_stoponexit = NULL;
4538 else
4539 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
4540 }
4541 if (opt->jo_set & JO_EXIT_CB)
4542 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004543 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004544 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004545 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004546 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004547 job->jv_exit_partial = NULL;
4548 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004549 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004550 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004551 job->jv_exit_partial = opt->jo_exit_partial;
4552 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004553 {
4554 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004555 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004556 }
4557 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004558 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02004559 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004560 func_ref(job->jv_exit_cb);
4561 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004562 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004563 }
4564}
4565
4566/*
4567 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
4568 */
4569 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02004570job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004571{
4572 job_T *job;
4573
4574 for (job = first_job; job != NULL; job = job->jv_next)
4575 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
4576 mch_stop_job(job, job->jv_stoponexit);
4577}
4578
4579/*
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004580 * Return TRUE when there is any job that might exit, which means
4581 * job_check_ended() should be called once in a while.
4582 */
4583 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02004584has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004585{
4586 job_T *job;
4587
4588 for (job = first_job; job != NULL; job = job->jv_next)
4589 if (job->jv_status == JOB_STARTED && job_still_useful(job))
4590 return TRUE;
4591 return FALSE;
4592}
4593
4594/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004595 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004596 */
4597 void
4598job_check_ended(void)
4599{
4600 static time_t last_check = 0;
4601 time_t now;
4602 job_T *job;
4603 job_T *next;
4604
4605 /* Only do this once in 10 seconds. */
4606 now = time(NULL);
4607 if (last_check + 10 < now)
4608 {
4609 last_check = now;
4610 for (job = first_job; job != NULL; job = next)
4611 {
4612 next = job->jv_next;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02004613 if (job->jv_status == JOB_STARTED && job_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004614 job_status(job); /* may free "job" */
4615 }
4616 }
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004617 if (channel_need_redraw)
4618 {
4619 channel_need_redraw = FALSE;
4620 redraw_after_callback();
4621 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004622}
4623
4624/*
4625 * "job_start()" function
4626 */
4627 job_T *
4628job_start(typval_T *argvars)
4629{
4630 job_T *job;
4631 char_u *cmd = NULL;
4632#if defined(UNIX)
4633# define USE_ARGV
4634 char **argv = NULL;
4635 int argc = 0;
4636#else
4637 garray_T ga;
4638#endif
4639 jobopt_T opt;
4640 int part;
4641
4642 job = job_alloc();
4643 if (job == NULL)
4644 return NULL;
4645
4646 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004647#ifndef USE_ARGV
4648 ga_init2(&ga, (int)sizeof(char*), 20);
4649#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004650
4651 /* Default mode is NL. */
4652 clear_job_options(&opt);
4653 opt.jo_mode = MODE_NL;
4654 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004655 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
4656 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004657 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004658
4659 /* Check that when io is "file" that there is a file name. */
4660 for (part = PART_OUT; part <= PART_IN; ++part)
4661 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
4662 && opt.jo_io[part] == JIO_FILE
4663 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
4664 || *opt.jo_io_name[part] == NUL))
4665 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004666 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004667 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004668 }
4669
4670 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
4671 {
4672 buf_T *buf = NULL;
4673
4674 /* check that we can find the buffer before starting the job */
4675 if (opt.jo_set & JO_IN_BUF)
4676 {
4677 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
4678 if (buf == NULL)
4679 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
4680 }
4681 else if (!(opt.jo_set & JO_IN_NAME))
4682 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004683 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004684 }
4685 else
4686 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
4687 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004688 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004689 if (buf->b_ml.ml_mfp == NULL)
4690 {
4691 char_u numbuf[NUMBUFLEN];
4692 char_u *s;
4693
4694 if (opt.jo_set & JO_IN_BUF)
4695 {
4696 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
4697 s = numbuf;
4698 }
4699 else
4700 s = opt.jo_io_name[PART_IN];
4701 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004702 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004703 }
4704 job->jv_in_buf = buf;
4705 }
4706
4707 job_set_options(job, &opt);
4708
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004709 if (argvars[0].v_type == VAR_STRING)
4710 {
4711 /* Command is a string. */
4712 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02004713 if (cmd == NULL || *cmd == NUL)
4714 {
4715 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004716 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02004717 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004718#ifdef USE_ARGV
4719 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004720 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004721 argv[argc] = NULL;
4722#endif
4723 }
4724 else if (argvars[0].v_type != VAR_LIST
4725 || argvars[0].vval.v_list == NULL
4726 || argvars[0].vval.v_list->lv_len < 1)
4727 {
4728 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004729 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004730 }
4731 else
4732 {
4733 list_T *l = argvars[0].vval.v_list;
4734 listitem_T *li;
4735 char_u *s;
4736
4737#ifdef USE_ARGV
4738 /* Pass argv[] to mch_call_shell(). */
4739 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
4740 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004741 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004742#endif
4743 for (li = l->lv_first; li != NULL; li = li->li_next)
4744 {
4745 s = get_tv_string_chk(&li->li_tv);
4746 if (s == NULL)
4747 goto theend;
4748#ifdef USE_ARGV
4749 argv[argc++] = (char *)s;
4750#else
4751 /* Only escape when needed, double quotes are not always allowed. */
4752 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
4753 {
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004754# ifdef WIN32
4755 int old_ssl = p_ssl;
4756
4757 /* This is using CreateProcess, not cmd.exe. Always use
4758 * double quote and backslashes. */
4759 p_ssl = 0;
4760# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004761 s = vim_strsave_shellescape(s, FALSE, TRUE);
Bram Moolenaar583c1f12016-03-12 15:58:34 +01004762# ifdef WIN32
4763 p_ssl = old_ssl;
4764# endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004765 if (s == NULL)
4766 goto theend;
4767 ga_concat(&ga, s);
4768 vim_free(s);
4769 }
4770 else
4771 ga_concat(&ga, s);
4772 if (li->li_next != NULL)
4773 ga_append(&ga, ' ');
4774#endif
4775 }
4776#ifdef USE_ARGV
4777 argv[argc] = NULL;
4778#else
4779 cmd = ga.ga_data;
4780#endif
4781 }
4782
4783#ifdef USE_ARGV
4784 if (ch_log_active())
4785 {
4786 garray_T ga;
4787 int i;
4788
4789 ga_init2(&ga, (int)sizeof(char), 200);
4790 for (i = 0; i < argc; ++i)
4791 {
4792 if (i > 0)
4793 ga_concat(&ga, (char_u *)" ");
4794 ga_concat(&ga, (char_u *)argv[i]);
4795 }
4796 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
4797 ga_clear(&ga);
4798 }
4799 mch_start_job(argv, job, &opt);
4800#else
4801 ch_logs(NULL, "Starting job: %s", (char *)cmd);
4802 mch_start_job((char *)cmd, job, &opt);
4803#endif
4804
4805 /* If the channel is reading from a buffer, write lines now. */
4806 if (job->jv_channel != NULL)
4807 channel_write_in(job->jv_channel);
4808
4809theend:
4810#ifdef USE_ARGV
4811 vim_free(argv);
4812#else
4813 vim_free(ga.ga_data);
4814#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004815 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004816 return job;
4817}
4818
4819/*
4820 * Get the status of "job" and invoke the exit callback when needed.
4821 * The returned string is not allocated.
4822 */
4823 char *
4824job_status(job_T *job)
4825{
4826 char *result;
4827
4828 if (job->jv_status == JOB_ENDED)
4829 /* No need to check, dead is dead. */
4830 result = "dead";
4831 else if (job->jv_status == JOB_FAILED)
4832 result = "fail";
4833 else
4834 {
4835 result = mch_job_status(job);
4836 if (job->jv_status == JOB_ENDED)
4837 ch_log(job->jv_channel, "Job ended");
4838 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
4839 {
4840 typval_T argv[3];
4841 typval_T rettv;
4842 int dummy;
4843
4844 /* invoke the exit callback; make sure the refcount is > 0 */
4845 ++job->jv_refcount;
4846 argv[0].v_type = VAR_JOB;
4847 argv[0].vval.v_job = job;
4848 argv[1].v_type = VAR_NUMBER;
4849 argv[1].vval.v_number = job->jv_exitval;
4850 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004851 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004852 job->jv_exit_partial, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004853 clear_tv(&rettv);
4854 --job->jv_refcount;
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02004855 channel_need_redraw = TRUE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004856 }
4857 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
4858 {
4859 /* The job was already unreferenced, now that it ended it can be
4860 * freed. Careful: caller must not use "job" after this! */
4861 job_free(job);
4862 }
4863 }
4864 return result;
4865}
4866
Bram Moolenaar8950a562016-03-12 15:22:55 +01004867/*
4868 * Implementation of job_info().
4869 */
4870 void
4871job_info(job_T *job, dict_T *dict)
4872{
4873 dictitem_T *item;
4874 varnumber_T nr;
4875
4876 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
4877
4878 item = dictitem_alloc((char_u *)"channel");
4879 if (item == NULL)
4880 return;
4881 item->di_tv.v_lock = 0;
4882 item->di_tv.v_type = VAR_CHANNEL;
4883 item->di_tv.vval.v_channel = job->jv_channel;
4884 if (job->jv_channel != NULL)
4885 ++job->jv_channel->ch_refcount;
4886 if (dict_add(dict, item) == FAIL)
4887 dictitem_free(item);
4888
4889#ifdef UNIX
4890 nr = job->jv_pid;
4891#else
4892 nr = job->jv_proc_info.dwProcessId;
4893#endif
4894 dict_add_nr_str(dict, "process", nr, NULL);
4895
4896 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004897 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01004898 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
4899}
4900
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004901 int
4902job_stop(job_T *job, typval_T *argvars)
4903{
4904 char_u *arg;
4905
4906 if (argvars[1].v_type == VAR_UNKNOWN)
4907 arg = (char_u *)"";
4908 else
4909 {
4910 arg = get_tv_string_chk(&argvars[1]);
4911 if (arg == NULL)
4912 {
4913 EMSG(_(e_invarg));
4914 return 0;
4915 }
4916 }
4917 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
4918 if (mch_stop_job(job, arg) == FAIL)
4919 return 0;
4920
4921 /* Assume that "hup" does not kill the job. */
4922 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
4923 job->jv_channel->ch_job_killed = TRUE;
4924
4925 /* We don't try freeing the job, obviously the caller still has a
4926 * reference to it. */
4927 return 1;
4928}
4929
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004930#endif /* FEAT_JOB_CHANNEL */