blob: 30a4304d8a6ae09762e0f97aad8f22b4551d8099 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare0874f82016-01-24 20:36:41 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 */
8
9/*
10 * Implements communication through a socket or any file handle.
11 */
12
13#include "vim.h"
14
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +010016
Bram Moolenaard04a0202016-01-26 23:30:18 +010017/* TRUE when netbeans is running with a GUI. */
18#ifdef FEAT_GUI
19# define CH_HAS_GUI (gui.in_use || gui.starting)
20#endif
21
Bram Moolenaar3f7d0902016-11-12 21:13:42 +010022/* Note: when making changes here also adjust configure.ac. */
Bram Moolenaard04a0202016-01-26 23:30:18 +010023#ifdef WIN32
24/* WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */
26# define SOCK_ERRNO errno = WSAGetLastError()
27# undef ECONNREFUSED
28# define ECONNREFUSED WSAECONNREFUSED
Bram Moolenaar4d919d72016-02-05 22:36:41 +010029# undef EWOULDBLOCK
30# define EWOULDBLOCK WSAEWOULDBLOCK
Bram Moolenaard42119f2016-02-28 20:51:49 +010031# undef EINPROGRESS
32# define EINPROGRESS WSAEINPROGRESS
Bram Moolenaard04a0202016-01-26 23:30:18 +010033# ifdef EINTR
34# undef EINTR
35# endif
36# define EINTR WSAEINTR
Bram Moolenaard8070362016-02-15 21:56:54 +010037# define sock_write(sd, buf, len) send((SOCKET)sd, buf, len, 0)
38# define sock_read(sd, buf, len) recv((SOCKET)sd, buf, len, 0)
39# define sock_close(sd) closesocket((SOCKET)sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010040#else
41# include <netdb.h>
42# include <netinet/in.h>
43
44# include <sys/socket.h>
45# ifdef HAVE_LIBGEN_H
46# include <libgen.h>
47# endif
48# define SOCK_ERRNO
49# define sock_write(sd, buf, len) write(sd, buf, len)
50# define sock_read(sd, buf, len) read(sd, buf, len)
51# define sock_close(sd) close(sd)
Bram Moolenaar0943a092016-02-16 13:11:17 +010052# define fd_read(fd, buf, len) read(fd, buf, len)
Bram Moolenaard8070362016-02-15 21:56:54 +010053# define fd_write(sd, buf, len) write(sd, buf, len)
54# define fd_close(sd) close(sd)
Bram Moolenaard04a0202016-01-26 23:30:18 +010055#endif
56
Bram Moolenaardc0ccae2016-10-09 17:28:01 +020057static void channel_read(channel_T *channel, ch_part_T part, char *func);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020058
Bram Moolenaar187db502016-02-27 14:44:26 +010059/* Whether a redraw is needed for appending a line to a buffer. */
60static int channel_need_redraw = FALSE;
61
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020062/* Whether we are inside channel_parse_messages() or another situation where it
63 * is safe to invoke callbacks. */
64static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010065
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020066static char *part_names[] = {"sock", "out", "err", "in"};
67
Bram Moolenaard8070362016-02-15 21:56:54 +010068#ifdef WIN32
69 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010070fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010071{
72 HANDLE h = (HANDLE)fd;
73 DWORD nread;
74
75 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
76 return -1;
77 return (int)nread;
78}
79
80 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010081fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010082{
83 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85
86 if (!WriteFile(h, buf, (DWORD)len, &nwrite, NULL))
87 return -1;
88 return (int)nwrite;
89}
90
91 static void
92fd_close(sock_T fd)
93{
94 HANDLE h = (HANDLE)fd;
95
96 CloseHandle(h);
97}
98#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010099
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100100/* Log file opened with ch_logfile(). */
101static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100102#ifdef FEAT_RELTIME
103static proftime_T log_start;
104#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100105
106 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100107ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100108{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100109 FILE *file = NULL;
110
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100111 if (log_fd != NULL)
112 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100113
114 if (*fname != NUL)
115 {
116 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
117 if (file == NULL)
118 {
119 EMSG2(_(e_notopen), fname);
120 return;
121 }
122 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100124
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100125 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100126 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100128#ifdef FEAT_RELTIME
129 profile_start(&log_start);
130#endif
131 }
132}
133
134 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200135ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100136{
137 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138}
139
140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100141ch_log_lead(char *what, channel_T *ch)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100142{
143 if (log_fd != NULL)
144 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100145#ifdef FEAT_RELTIME
146 proftime_T log_now;
147
148 profile_start(&log_now);
149 profile_sub(&log_now, &log_start);
150 fprintf(log_fd, "%s ", profile_msg(&log_now));
151#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100152 if (ch != NULL)
153 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 else
155 fprintf(log_fd, "%s: ", what);
156 }
157}
158
Bram Moolenaard0b65022016-03-06 21:50:33 +0100159static int did_log_msg = TRUE;
160
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200162ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200166 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167
Bram Moolenaar77073442016-02-13 23:23:53 +0100168 ch_log_lead("", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200169 va_start(ap, fmt);
170 vfprintf(log_fd, fmt, ap);
171 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100174 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 }
176}
177
178 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200179ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180{
181 if (log_fd != NULL)
182 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200183 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184
Bram Moolenaar77073442016-02-13 23:23:53 +0100185 ch_log_lead("ERR ", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200186 va_start(ap, fmt);
187 vfprintf(log_fd, fmt, ap);
188 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100194
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100195#ifdef _WIN32
196# undef PERROR
197# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
198 (char_u *)msg, (char_u *)strerror_win32(errno))
199
200 static char *
201strerror_win32(int eno)
202{
203 static LPVOID msgbuf = NULL;
204 char_u *ptr;
205
206 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200207 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100208 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200209 msgbuf = NULL;
210 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100211 FormatMessage(
212 FORMAT_MESSAGE_ALLOCATE_BUFFER |
213 FORMAT_MESSAGE_FROM_SYSTEM |
214 FORMAT_MESSAGE_IGNORE_INSERTS,
215 NULL,
216 eno,
217 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
218 (LPTSTR) &msgbuf,
219 0,
220 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200221 if (msgbuf != NULL)
222 /* chomp \r or \n */
223 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
224 switch (*ptr)
225 {
226 case '\r':
227 STRMOVE(ptr, ptr + 1);
228 ptr--;
229 break;
230 case '\n':
231 if (*(ptr + 1) == '\0')
232 *ptr = '\0';
233 else
234 *ptr = ' ';
235 break;
236 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100237 return msgbuf;
238}
239#endif
240
Bram Moolenaar77073442016-02-13 23:23:53 +0100241/*
242 * The list of all allocated channels.
243 */
244static channel_T *first_channel = NULL;
245static int next_ch_id = 0;
246
247/*
248 * Allocate a new channel. The refcount is set to 1.
249 * The channel isn't actually used until it is opened.
250 * Returns NULL if out of memory.
251 */
252 channel_T *
253add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100254{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200255 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100256 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100257
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if (channel == NULL)
259 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100260
Bram Moolenaar77073442016-02-13 23:23:53 +0100261 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100262 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100263
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200264 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100265 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100266 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100267#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100268 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100269#endif
270#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100271 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100272#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100273 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100274 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100275
Bram Moolenaar77073442016-02-13 23:23:53 +0100276 if (first_channel != NULL)
277 {
278 first_channel->ch_prev = channel;
279 channel->ch_next = first_channel;
280 }
281 first_channel = channel;
282
283 channel->ch_refcount = 1;
284 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100285}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100286
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200287 int
288has_any_channel(void)
289{
290 return first_channel != NULL;
291}
292
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100293/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100294 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100295 * Return TRUE if "channel" has a callback and the associated job wasn't
296 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100297 */
298 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100299channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100300{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100301 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100302 int has_out_msg;
303 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100304
305 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100306 if (channel->ch_job_killed && channel->ch_job == NULL)
307 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100308
309 /* If there is a close callback it may still need to be invoked. */
310 if (channel->ch_close_cb != NULL)
311 return TRUE;
312
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200313 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200314 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200315 return TRUE;
316
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100317 /* If there is no callback then nobody can get readahead. If the fd is
318 * closed and there is no readahead then the callback won't be called. */
319 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
320 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
321 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100322 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
323 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
324 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
325 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
326 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
327 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100328 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100329 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200330 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
332 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200333 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200334 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
335 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100336}
337
338/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200339 * Close a channel and free all its resources.
340 */
341 static void
342channel_free_contents(channel_T *channel)
343{
344 channel_close(channel, TRUE);
345 channel_clear(channel);
346 ch_log(channel, "Freeing channel");
347}
348
349 static void
350channel_free_channel(channel_T *channel)
351{
352 if (channel->ch_next != NULL)
353 channel->ch_next->ch_prev = channel->ch_prev;
354 if (channel->ch_prev == NULL)
355 first_channel = channel->ch_next;
356 else
357 channel->ch_prev->ch_next = channel->ch_next;
358 vim_free(channel);
359}
360
361 static void
362channel_free(channel_T *channel)
363{
364 if (!in_free_unref_items)
365 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200366 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200367 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200368 else
369 {
370 channel_free_contents(channel);
371 channel_free_channel(channel);
372 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 }
374}
375
376/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100378 * possible, there is no callback to be invoked or the associated job was
379 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100380 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100382 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100383channel_may_free(channel_T *channel)
384{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100385 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100386 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100387 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100388 return TRUE;
389 }
390 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100391}
392
393/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100394 * Decrement the reference count on "channel" and maybe free it when it goes
395 * down to zero. Don't free it if there is a pending action.
396 * Returns TRUE when the channel is no longer referenced.
397 */
398 int
399channel_unref(channel_T *channel)
400{
401 if (channel != NULL && --channel->ch_refcount <= 0)
402 return channel_may_free(channel);
403 return FALSE;
404}
405
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200406 int
407free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100408{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200409 int did_free = FALSE;
410 channel_T *ch;
411
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200412 /* This is invoked from the garbage collector, which only runs at a safe
413 * point. */
414 ++safe_to_invoke_callback;
415
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200416 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200417 if (!channel_still_useful(ch)
418 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200419 {
420 /* Free the channel and ordinary items it contains, but don't
421 * recurse into Lists, Dictionaries etc. */
422 channel_free_contents(ch);
423 did_free = TRUE;
424 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425
426 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200427 return did_free;
428}
429
430 void
431free_unused_channels(int copyID, int mask)
432{
433 channel_T *ch;
434 channel_T *ch_next;
435
436 for (ch = first_channel; ch != NULL; ch = ch_next)
437 {
438 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200439 if (!channel_still_useful(ch)
440 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200441 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200442 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200443 channel_free_channel(ch);
444 }
445 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100446}
447
Bram Moolenaard04a0202016-01-26 23:30:18 +0100448#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100449
450#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
451 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100452channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100453{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100454 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200455 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100456
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100457 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100458 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200459 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100460 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200461 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100462}
463#endif
464
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100466 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100468#ifdef FEAT_GUI_X11
469 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200470messageFromServer(XtPointer clientData,
471 int *unused1 UNUSED,
472 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100473{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100474 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100475}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100476#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100477
Bram Moolenaard04a0202016-01-26 23:30:18 +0100478#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100479# if GTK_CHECK_VERSION(3,0,0)
480 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200481messageFromServer(GIOChannel *unused1 UNUSED,
482 GIOCondition unused2 UNUSED,
483 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100484{
485 channel_read_fd(GPOINTER_TO_INT(clientData));
486 return TRUE; /* Return FALSE instead in case the event source is to
487 * be removed after this function returns. */
488}
489# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100490 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200491messageFromServer(gpointer clientData,
492 gint unused1 UNUSED,
493 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100495 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
Bram Moolenaar98921892016-02-23 17:14:37 +0100497# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498#endif
499
500 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200501channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100502{
Bram Moolenaarde279892016-03-11 22:19:44 +0100503 if (!CH_HAS_GUI)
504 return;
505
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200506 /* gets stuck in handling events for a not connected channel */
507 if (channel->ch_keep_open)
508 return;
509
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100510# ifdef FEAT_GUI_X11
511 /* Tell notifier we are interested in being called
512 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100513 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
514 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100515 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100516 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100517 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200518 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100519 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100520# else
521# ifdef FEAT_GUI_GTK
522 /* Tell gdk we are interested in being called when there
523 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100524 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100525# if GTK_CHECK_VERSION(3,0,0)
526 {
527 GIOChannel *chnnl = g_io_channel_unix_new(
528 (gint)channel->ch_part[part].ch_fd);
529
530 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
531 chnnl,
532 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200533 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100534 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
535
536 g_io_channel_unref(chnnl);
537 }
538# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100539 channel->ch_part[part].ch_inputHandler = gdk_input_add(
540 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100541 (GdkInputCondition)
542 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200543 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100544 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100545# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100546# endif
547# endif
548}
549
Bram Moolenaarde279892016-03-11 22:19:44 +0100550 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100551channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100552{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100553 if (channel->CH_SOCK_FD != INVALID_FD)
554 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200555 if (channel->CH_OUT_FD != INVALID_FD
556 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100557 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200558 if (channel->CH_ERR_FD != INVALID_FD
559 && channel->CH_ERR_FD != channel->CH_SOCK_FD
560 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100561 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100562}
563
564/*
565 * Register any of our file descriptors with the GUI event handling system.
566 * Called when the GUI has started.
567 */
568 void
569channel_gui_register_all(void)
570{
Bram Moolenaar77073442016-02-13 23:23:53 +0100571 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100572
Bram Moolenaar77073442016-02-13 23:23:53 +0100573 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100574 channel_gui_register(channel);
575}
576
577 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200578channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100579{
580# ifdef FEAT_GUI_X11
581 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
582 {
583 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
584 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
585 }
586# else
587# ifdef FEAT_GUI_GTK
588 if (channel->ch_part[part].ch_inputHandler != 0)
589 {
590# if GTK_CHECK_VERSION(3,0,0)
591 g_source_remove(channel->ch_part[part].ch_inputHandler);
592# else
593 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
594# endif
595 channel->ch_part[part].ch_inputHandler = 0;
596 }
597# endif
598# endif
599}
600
601 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100602channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100603{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200604 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100605
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100606 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100607 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100608}
609
610#endif
611
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100612static char *e_cannot_connect = N_("E902: Cannot connect to port");
613
Bram Moolenaard04a0202016-01-26 23:30:18 +0100614/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100615 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100616 * "waittime" is the time in msec to wait for the connection.
617 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100618 * Returns the channel for success.
619 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100620 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100621 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100622channel_open(
623 char *hostname,
624 int port_in,
625 int waittime,
626 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100627{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100628 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100629 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100630 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100631#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100632 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100633 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100634#else
635 int port = port_in;
636#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100637 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100638 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100639
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100640#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641 channel_init_winsock();
642#endif
643
Bram Moolenaar77073442016-02-13 23:23:53 +0100644 channel = add_channel();
645 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100647 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100648 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100649 }
650
651 /* Get the server internet address and put into addr structure */
652 /* fill in the socket address structure and connect to server */
653 vim_memset((char *)&server, 0, sizeof(server));
654 server.sin_family = AF_INET;
655 server.sin_port = htons(port);
656 if ((host = gethostbyname(hostname)) == NULL)
657 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100658 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200659 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100660 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100661 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100663 {
664 char *p;
665
666 /* When using host->h_addr directly ubsan warns for it to not be
667 * aligned. First copy the pointer to aviod that. */
668 memcpy(&p, &host->h_addr, sizeof(p));
669 memcpy((char *)&server.sin_addr, p, host->h_length);
670 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100672 /* On Mac and Solaris a zero timeout almost never works. At least wait
673 * one millisecond. Let's do it for all systems, because we don't know why
674 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100675 if (waittime == 0)
676 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100677
678 /*
679 * For Unix we need to call connect() again after connect() failed.
680 * On Win32 one time is sufficient.
681 */
682 while (TRUE)
683 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100684 long elapsed_msec = 0;
685 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100686
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100688 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100689 sd = socket(AF_INET, SOCK_STREAM, 0);
690 if (sd == -1)
691 {
692 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200693 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100694 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100695 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100697
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 if (waittime >= 0)
699 {
700 /* Make connect() non-blocking. */
701 if (
702#ifdef _WIN32
703 ioctlsocket(sd, FIONBIO, &val) < 0
704#else
705 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
706#endif
707 )
708 {
709 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200710 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100711 "channel_open: Connect failed with errno %d", errno);
712 sock_close(sd);
713 channel_free(channel);
714 return NULL;
715 }
716 }
717
718 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200719 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
721
Bram Moolenaar045a2842016-03-08 22:33:07 +0100722 if (ret == 0)
723 /* The connection could be established. */
724 break;
725
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100726 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100727 if (waittime < 0 || (errno != EWOULDBLOCK
728 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100729#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100730 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100731#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100732 ))
733 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200734 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100735 "channel_open: Connect failed with errno %d", errno);
736 PERROR(_(e_cannot_connect));
737 sock_close(sd);
738 channel_free(channel);
739 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100740 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100741
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100742 /* Limit the waittime to 50 msec. If it doesn't work within this
743 * time we close the socket and try creating it again. */
744 waitnow = waittime > 50 ? 50 : waittime;
745
Bram Moolenaar045a2842016-03-08 22:33:07 +0100746 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100747 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100748#ifndef WIN32
749 if (errno != ECONNREFUSED)
750#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100751 {
752 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100753 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100754 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100755#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100756 int so_error = 0;
757 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100758 struct timeval start_tv;
759 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100760#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100761 FD_ZERO(&rfds);
762 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 FD_ZERO(&wfds);
764 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100765
Bram Moolenaar562ca712016-03-09 21:50:05 +0100766 tv.tv_sec = waitnow / 1000;
767 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768#ifndef WIN32
769 gettimeofday(&start_tv, NULL);
770#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200771 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100772 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100773 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100774
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100775 if (ret < 0)
776 {
777 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200778 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100779 "channel_open: Connect failed with errno %d", errno);
780 PERROR(_(e_cannot_connect));
781 sock_close(sd);
782 channel_free(channel);
783 return NULL;
784 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100785
Bram Moolenaare081e212016-02-28 22:33:46 +0100786#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100787 /* On Win32: select() is expected to work and wait for up to
788 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 if (FD_ISSET(sd, &wfds))
790 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100791 elapsed_msec = waitnow;
792 if (waittime > 1 && elapsed_msec < waittime)
793 {
794 waittime -= elapsed_msec;
795 continue;
796 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100797#else
798 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100799 * After putting the socket in non-blocking mode, connect() will
800 * return EINPROGRESS, select() will not wait (as if writing is
801 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100802 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100803 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100804 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100805 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 {
807 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100809 if (ret < 0 || (so_error != 0
810 && so_error != EWOULDBLOCK
811 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100812# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100814# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 ))
816 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200817 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 "channel_open: Connect failed with errno %d",
819 so_error);
820 PERROR(_(e_cannot_connect));
821 sock_close(sd);
822 channel_free(channel);
823 return NULL;
824 }
825 }
826
Bram Moolenaar045a2842016-03-08 22:33:07 +0100827 if (FD_ISSET(sd, &wfds) && so_error == 0)
828 /* Did not detect an error, connection is established. */
829 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100830
Bram Moolenaar045a2842016-03-08 22:33:07 +0100831 gettimeofday(&end_tv, NULL);
832 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
833 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100834#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100835 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836
837#ifndef WIN32
838 if (waittime > 1 && elapsed_msec < waittime)
839 {
840 /* The port isn't ready but we also didn't get an error.
841 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100842 * yet. Select() may return early, wait until the remaining
843 * "waitnow" and try again. */
844 waitnow -= elapsed_msec;
845 waittime -= elapsed_msec;
846 if (waitnow > 0)
847 {
848 mch_delay((long)waitnow, TRUE);
849 ui_breakcheck();
850 waittime -= waitnow;
851 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100852 if (!got_int)
853 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100854 if (waittime <= 0)
855 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100856 waittime = 1;
857 continue;
858 }
859 /* we were interrupted, behave as if timed out */
860 }
861#endif
862
863 /* We timed out. */
864 ch_error(channel, "Connection timed out");
865 sock_close(sd);
866 channel_free(channel);
867 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100868 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100869
Bram Moolenaar045a2842016-03-08 22:33:07 +0100870 ch_log(channel, "Connection made");
871
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100872 if (waittime >= 0)
873 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100874#ifdef _WIN32
875 val = 0;
876 ioctlsocket(sd, FIONBIO, &val);
877#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100878 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100879#endif
880 }
881
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100882 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100883 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100884 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
885 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200886 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100887
888#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100889 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100890#endif
891
Bram Moolenaar77073442016-02-13 23:23:53 +0100892 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100893}
894
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100895/*
896 * Implements ch_open().
897 */
898 channel_T *
899channel_open_func(typval_T *argvars)
900{
901 char_u *address;
902 char_u *p;
903 char *rest;
904 int port;
905 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200906 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100907
908 address = get_tv_string(&argvars[0]);
909 if (argvars[1].v_type != VAR_UNKNOWN
910 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
911 {
912 EMSG(_(e_invarg));
913 return NULL;
914 }
915
916 /* parse address */
917 p = vim_strchr(address, ':');
918 if (p == NULL)
919 {
920 EMSG2(_(e_invarg2), address);
921 return NULL;
922 }
923 *p++ = NUL;
924 port = strtol((char *)p, &rest, 10);
925 if (*address == NUL || port <= 0 || *rest != NUL)
926 {
927 p[-1] = ':';
928 EMSG2(_(e_invarg2), address);
929 return NULL;
930 }
931
932 /* parse options */
933 clear_job_options(&opt);
934 opt.jo_mode = MODE_JSON;
935 opt.jo_timeout = 2000;
936 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200937 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200938 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939 if (opt.jo_timeout < 0)
940 {
941 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200942 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100943 }
944
945 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
946 if (channel != NULL)
947 {
948 opt.jo_set = JO_ALL;
949 channel_set_options(channel, &opt);
950 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200951theend:
952 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100953 return channel;
954}
955
Bram Moolenaarde279892016-03-11 22:19:44 +0100956 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200957ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100958{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200959 sock_T *fd = &channel->ch_part[part].ch_fd;
960
Bram Moolenaarde279892016-03-11 22:19:44 +0100961 if (*fd != INVALID_FD)
962 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200963 if (part == PART_SOCK)
964 sock_close(*fd);
965 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200966 {
967 /* When using a pty the same FD is set on multiple parts, only
968 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200969 if ((part == PART_IN || channel->CH_IN_FD != *fd)
970 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
971 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200972 fd_close(*fd);
973 }
Bram Moolenaarde279892016-03-11 22:19:44 +0100974 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200975
976 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +0100977 }
978}
979
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100980 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100981channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100982{
Bram Moolenaarde279892016-03-11 22:19:44 +0100983 if (in != INVALID_FD)
984 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200985 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +0100986 channel->CH_IN_FD = in;
987 }
988 if (out != INVALID_FD)
989 {
990# if defined(FEAT_GUI)
991 channel_gui_unregister_one(channel, PART_OUT);
992# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200993 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +0100994 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200995 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +0100996# if defined(FEAT_GUI)
997 channel_gui_register_one(channel, PART_OUT);
998# endif
999 }
1000 if (err != INVALID_FD)
1001 {
1002# if defined(FEAT_GUI)
1003 channel_gui_unregister_one(channel, PART_ERR);
1004# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001005 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001006 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001007 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001008# if defined(FEAT_GUI)
1009 channel_gui_register_one(channel, PART_ERR);
1010# endif
1011 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001012}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001013
Bram Moolenaard6051b52016-02-28 15:49:03 +01001014/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001015 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001016 * This does not keep a refcount, when the job is freed ch_job is cleared.
1017 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001018 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001019channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001020{
Bram Moolenaar77073442016-02-13 23:23:53 +01001021 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001022
1023 channel_set_options(channel, options);
1024
1025 if (job->jv_in_buf != NULL)
1026 {
1027 chanpart_T *in_part = &channel->ch_part[PART_IN];
1028
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001029 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001030 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001031 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001032 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001033 {
1034 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1035 {
1036 /* Special mode: send last-but-one line when appending a line
1037 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001038 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001039 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001040 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001041 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001042 }
1043 else
1044 in_part->ch_buf_top = options->jo_in_top;
1045 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001046 else
1047 in_part->ch_buf_top = 1;
1048 if (options->jo_set & JO_IN_BOT)
1049 in_part->ch_buf_bot = options->jo_in_bot;
1050 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001051 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001052 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001053}
1054
1055/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001056 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001057 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001058 */
1059 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001060find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001061{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001062 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001063 buf_T *save_curbuf = curbuf;
1064
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001065 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001066 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001067 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001068 if (buf == NULL)
1069 buf = buflist_findname_exp(name);
1070 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001071 if (buf == NULL)
1072 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001073 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001074 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001075 if (buf == NULL)
1076 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001077 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001078 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001079#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001080 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1081 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001082#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001083 if (curbuf->b_ml.ml_mfp == NULL)
1084 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001085 if (msg)
1086 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001087 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001088 changed_bytes(1, 0);
1089 curbuf = save_curbuf;
1090 }
1091
1092 return buf;
1093}
1094
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001095 static void
1096set_callback(
1097 char_u **cbp,
1098 partial_T **pp,
1099 char_u *callback,
1100 partial_T *partial)
1101{
1102 free_callback(*cbp, *pp);
1103 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001104 {
1105 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001106 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001107 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001108 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001109 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001110 func_ref(*cbp);
1111 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001112 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001113 else
1114 *cbp = NULL;
1115 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001116 if (partial != NULL)
1117 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001118}
1119
Bram Moolenaar187db502016-02-27 14:44:26 +01001120/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001121 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001122 */
1123 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001124channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001125{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001126 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001127
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001128 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001129 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001130 channel->ch_part[part].ch_mode = opt->jo_mode;
1131 if (opt->jo_set & JO_IN_MODE)
1132 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1133 if (opt->jo_set & JO_OUT_MODE)
1134 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1135 if (opt->jo_set & JO_ERR_MODE)
1136 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001137
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001138 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001139 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001140 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1141 if (opt->jo_set & JO_OUT_TIMEOUT)
1142 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1143 if (opt->jo_set & JO_ERR_TIMEOUT)
1144 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001145 if (opt->jo_set & JO_BLOCK_WRITE)
1146 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001147
1148 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001149 set_callback(&channel->ch_callback, &channel->ch_partial,
1150 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001151 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001152 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1153 &channel->ch_part[PART_OUT].ch_partial,
1154 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001155 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001156 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1157 &channel->ch_part[PART_ERR].ch_partial,
1158 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001159 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001160 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1161 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001162 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001163
1164 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1165 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001166 buf_T *buf;
1167
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001168 /* writing output to a buffer. Default mode is NL. */
1169 if (!(opt->jo_set & JO_OUT_MODE))
1170 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001171 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001172 {
1173 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1174 if (buf == NULL)
1175 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1176 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001177 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001178 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001179 int msg = TRUE;
1180
1181 if (opt->jo_set2 & JO2_OUT_MSG)
1182 msg = opt->jo_message[PART_OUT];
1183 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001184 }
1185 if (buf != NULL)
1186 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001187 if (opt->jo_set & JO_OUT_MODIFIABLE)
1188 channel->ch_part[PART_OUT].ch_nomodifiable =
1189 !opt->jo_modifiable[PART_OUT];
1190
1191 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1192 {
1193 EMSG(_(e_modifiable));
1194 }
1195 else
1196 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001197 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001198 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001199 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001200 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001201 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001202 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001203
1204 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1205 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1206 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1207 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001208 buf_T *buf;
1209
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001210 /* writing err to a buffer. Default mode is NL. */
1211 if (!(opt->jo_set & JO_ERR_MODE))
1212 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1213 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001214 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001215 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001216 {
1217 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1218 if (buf == NULL)
1219 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1220 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001221 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001222 {
1223 int msg = TRUE;
1224
1225 if (opt->jo_set2 & JO2_ERR_MSG)
1226 msg = opt->jo_message[PART_ERR];
1227 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1228 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001229 if (buf != NULL)
1230 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001231 if (opt->jo_set & JO_ERR_MODIFIABLE)
1232 channel->ch_part[PART_ERR].ch_nomodifiable =
1233 !opt->jo_modifiable[PART_ERR];
1234 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1235 {
1236 EMSG(_(e_modifiable));
1237 }
1238 else
1239 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001240 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001241 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001242 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001243 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001245 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001246
1247 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1248 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1249 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001250}
1251
1252/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001253 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001254 */
1255 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001256channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001257 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001258 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001259 char_u *callback,
1260 partial_T *partial,
1261 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001262{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001263 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001264 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1265
1266 if (item != NULL)
1267 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001268 item->cq_partial = partial;
1269 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001270 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001271 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001272 item->cq_callback = callback;
1273 }
1274 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001275 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001276 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001277 func_ref(item->cq_callback);
1278 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001279 item->cq_seq_nr = id;
1280 item->cq_prev = head->cq_prev;
1281 head->cq_prev = item;
1282 item->cq_next = NULL;
1283 if (item->cq_prev == NULL)
1284 head->cq_next = item;
1285 else
1286 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001287 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001288}
1289
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001290 static void
1291write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1292{
1293 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001294 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001295 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001296 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001297
Bram Moolenaar655da312016-05-28 22:22:34 +02001298 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001299 if ((p = alloc(len + 2)) == NULL)
1300 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001301 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001302
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001303 if (channel->ch_write_text_mode)
1304 p[len] = CAR;
1305 else
1306 {
1307 for (i = 0; i < len; ++i)
1308 if (p[i] == NL)
1309 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001310
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001311 p[len] = NL;
1312 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001313 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001314 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001315 vim_free(p);
1316}
1317
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001318/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001319 * Return TRUE if "channel" can be written to.
1320 * Returns FALSE if the input is closed or the write would block.
1321 */
1322 static int
1323can_write_buf_line(channel_T *channel)
1324{
1325 chanpart_T *in_part = &channel->ch_part[PART_IN];
1326
1327 if (in_part->ch_fd == INVALID_FD)
1328 return FALSE; /* pipe was closed */
1329
1330 /* for testing: block every other attempt to write */
1331 if (in_part->ch_block_write == 1)
1332 in_part->ch_block_write = -1;
1333 else if (in_part->ch_block_write == -1)
1334 in_part->ch_block_write = 1;
1335
1336 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1337#ifndef WIN32
1338 {
1339# if defined(HAVE_SELECT)
1340 struct timeval tval;
1341 fd_set wfds;
1342 int ret;
1343
1344 FD_ZERO(&wfds);
1345 FD_SET((int)in_part->ch_fd, &wfds);
1346 tval.tv_sec = 0;
1347 tval.tv_usec = 0;
1348 for (;;)
1349 {
1350 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1351# ifdef EINTR
1352 SOCK_ERRNO;
1353 if (ret == -1 && errno == EINTR)
1354 continue;
1355# endif
1356 if (ret <= 0 || in_part->ch_block_write == 1)
1357 {
1358 if (ret > 0)
1359 ch_log(channel, "FAKED Input not ready for writing");
1360 else
1361 ch_log(channel, "Input not ready for writing");
1362 return FALSE;
1363 }
1364 break;
1365 }
1366# else
1367 struct pollfd fds;
1368
1369 fds.fd = in_part->ch_fd;
1370 fds.events = POLLOUT;
1371 if (poll(&fds, 1, 0) <= 0)
1372 {
1373 ch_log(channel, "Input not ready for writing");
1374 return FALSE;
1375 }
1376 if (in_part->ch_block_write == 1)
1377 {
1378 ch_log(channel, "FAKED Input not ready for writing");
1379 return FALSE;
1380 }
1381# endif
1382 }
1383#endif
1384 return TRUE;
1385}
1386
1387/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001388 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001389 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001390 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001391channel_write_in(channel_T *channel)
1392{
1393 chanpart_T *in_part = &channel->ch_part[PART_IN];
1394 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001395 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001396 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001397
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001398 if (buf == NULL || in_part->ch_buf_append)
1399 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001400 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001401 {
1402 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001403 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001404 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001405 return;
1406 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001407
1408 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1409 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1410 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001411 if (!can_write_buf_line(channel))
1412 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001413 write_buf_line(buf, lnum, channel);
1414 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001415 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001416
1417 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001418 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001419 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001420 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001421
Bram Moolenaar014069a2016-03-03 22:51:40 +01001422 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001423 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001424 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001425#if defined(FEAT_TERMINAL)
1426 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001427 if (channel->ch_job != NULL)
1428 term_send_eof(channel);
1429#endif
1430
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001431 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001432 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001433 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001434
1435 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001436 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001437 }
1438 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001439 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001440 buf->b_ml.ml_line_count - lnum + 1);
1441}
1442
1443/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001444 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001445 */
1446 void
1447channel_buffer_free(buf_T *buf)
1448{
1449 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001450 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001451
1452 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001453 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001454 {
1455 chanpart_T *ch_part = &channel->ch_part[part];
1456
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001457 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001458 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001459 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001460 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001461 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001462 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001463 }
1464}
1465
1466/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001467 * Write any lines waiting to be written to "channel".
1468 */
1469 static void
1470channel_write_input(channel_T *channel)
1471{
1472 chanpart_T *in_part = &channel->ch_part[PART_IN];
1473
1474 if (in_part->ch_writeque.wq_next != NULL)
1475 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1476 else if (in_part->ch_bufref.br_buf != NULL)
1477 {
1478 if (in_part->ch_buf_append)
1479 channel_write_new_lines(in_part->ch_bufref.br_buf);
1480 else
1481 channel_write_in(channel);
1482 }
1483}
1484
1485/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001486 * Write any lines waiting to be written to a channel.
1487 */
1488 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001489channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001490{
1491 channel_T *channel;
1492
1493 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001494 channel_write_input(channel);
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)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001529 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001531 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001532 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001533 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001534 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)
Bram Moolenaarde330112017-01-08 20:50:52 +01001555 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001556
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 *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001571channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001572{
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 Moolenaardc0ccae2016-10-09 17:28:01 +02001601channel_get(channel_T *channel, ch_part_T 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 Moolenaardc0ccae2016-10-09 17:28:01 +02001625channel_get_all(channel_T *channel, ch_part_T 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
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001674channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001675{
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 Moolenaardc0ccae2016-10-09 17:28:01 +02001690channel_collapse(channel_T *channel, ch_part_T 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 Moolenaardc0ccae2016-10-09 17:28:01 +02001750channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001751 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 Moolenaar88989cc2017-02-06 21:56:09 +01001821/*
1822 * Try to fill the buffer of "reader".
1823 * Returns FALSE when nothing was added.
1824 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001825 static int
1826channel_fill(js_read_T *reader)
1827{
1828 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001829 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001830 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001831 int keeplen;
1832 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001833 char_u *p;
1834
1835 if (next == NULL)
1836 return FALSE;
1837
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001838 keeplen = reader->js_end - reader->js_buf;
1839 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001840 {
1841 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001842 addlen = (int)STRLEN(next);
1843 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001844 if (p == NULL)
1845 {
1846 vim_free(next);
1847 return FALSE;
1848 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001849 mch_memmove(p, reader->js_buf, keeplen);
1850 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001851 vim_free(next);
1852 next = p;
1853 }
1854
1855 vim_free(reader->js_buf);
1856 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 return TRUE;
1858}
1859
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001860/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001861 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001862 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001863 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001864 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001865 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001866channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001867{
1868 js_read_T reader;
1869 typval_T listtv;
1870 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001871 chanpart_T *chanpart = &channel->ch_part[part];
1872 jsonq_T *head = &chanpart->ch_json_head;
1873 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001874 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001875
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001876 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001877 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001878
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001879 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001880 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001881 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001882 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001883 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001884
1885 /* When a message is incomplete we wait for a short while for more to
1886 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001887 * or list will make us hang.
1888 * Do not generate error messages, they will be written in a channel log. */
1889 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001890 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001891 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001892 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001893 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001895 /* Only accept the response when it is a list with at least two
1896 * items. */
1897 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001898 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001899 if (listtv.v_type != VAR_LIST)
1900 ch_error(channel, "Did not receive a list, discarding");
1901 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001902 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001903 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001904 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001905 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001906 else
1907 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001908 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1909 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001910 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001911 else
1912 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001913 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001914 item->jq_value = alloc_tv();
1915 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001916 {
1917 vim_free(item);
1918 clear_tv(&listtv);
1919 }
1920 else
1921 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001922 *item->jq_value = listtv;
1923 item->jq_prev = head->jq_prev;
1924 head->jq_prev = item;
1925 item->jq_next = NULL;
1926 if (item->jq_prev == NULL)
1927 head->jq_next = item;
1928 else
1929 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001930 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001931 }
1932 }
1933 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001934
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001935 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001936 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001937 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001938 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001939 size_t buflen = STRLEN(reader.js_buf);
1940
1941 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001942 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001943 /* First time encountering incomplete message or after receiving
1944 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001945 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001946 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001947 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001948 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001949 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001950#ifdef WIN32
1951 chanpart->ch_deadline = GetTickCount() + 100L;
1952#else
1953 gettimeofday(&chanpart->ch_deadline, NULL);
1954 chanpart->ch_deadline.tv_usec += 100 * 1000;
1955 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1956 {
1957 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1958 ++chanpart->ch_deadline.tv_sec;
1959 }
1960#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001961 }
1962 else
1963 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001964 int timeout;
1965#ifdef WIN32
1966 timeout = GetTickCount() > chanpart->ch_deadline;
1967#else
1968 {
1969 struct timeval now_tv;
1970
1971 gettimeofday(&now_tv, NULL);
1972 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1973 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1974 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1975 }
1976#endif
1977 if (timeout)
1978 {
1979 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001980 chanpart->ch_wait_len = 0;
1981 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001982 }
1983 else
1984 {
1985 reader.js_used = 0;
1986 ch_log(channel, "still waiting on incomplete message");
1987 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001988 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001989 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001990
1991 if (status == FAIL)
1992 {
1993 ch_error(channel, "Decoding failed - discarding input");
1994 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001995 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001996 }
1997 else if (reader.js_buf[reader.js_used] != NUL)
1998 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001999 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002000 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002001 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2002 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002003 ret = status == MAYBE ? FALSE: TRUE;
2004 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002005 else
2006 ret = FALSE;
2007
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002008 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002009 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002010}
2011
2012/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002013 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002014 */
2015 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002016remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002017{
Bram Moolenaar77073442016-02-13 23:23:53 +01002018 if (node->cq_prev == NULL)
2019 head->cq_next = node->cq_next;
2020 else
2021 node->cq_prev->cq_next = node->cq_next;
2022 if (node->cq_next == NULL)
2023 head->cq_prev = node->cq_prev;
2024 else
2025 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002026}
2027
2028/*
2029 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002030 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002031 */
2032 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002033remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002034{
Bram Moolenaar77073442016-02-13 23:23:53 +01002035 if (node->jq_prev == NULL)
2036 head->jq_next = node->jq_next;
2037 else
2038 node->jq_prev->jq_next = node->jq_next;
2039 if (node->jq_next == NULL)
2040 head->jq_prev = node->jq_prev;
2041 else
2042 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002043 vim_free(node);
2044}
2045
2046/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002047 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002048 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002049 * When "id" is zero or negative jut get the first message. But not the one
2050 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002051 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002052 * Return OK when found and return the value in "rettv".
2053 * Return FAIL otherwise.
2054 */
2055 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002056channel_get_json(
2057 channel_T *channel,
2058 ch_part_T part,
2059 int id,
2060 int without_callback,
2061 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002062{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002063 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002064 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002065
Bram Moolenaar77073442016-02-13 23:23:53 +01002066 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002067 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002068 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002069 typval_T *tv = &l->lv_first->li_tv;
2070
Bram Moolenaar958dc692016-12-01 15:34:12 +01002071 if ((without_callback || !item->jq_no_callback)
2072 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002073 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002074 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002075 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002076 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002077 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002078 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002079 ch_log(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002080 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002081 return OK;
2082 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002083 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002084 }
2085 return FAIL;
2086}
2087
Bram Moolenaar958dc692016-12-01 15:34:12 +01002088/*
2089 * Put back "rettv" into the JSON queue, there was no callback for it.
2090 * Takes over the values in "rettv".
2091 */
2092 static void
2093channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2094{
2095 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2096 jsonq_T *item = head->jq_next;
2097 jsonq_T *newitem;
2098
2099 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2100 /* last item was pushed back, append to the end */
2101 item = NULL;
2102 else while (item != NULL && item->jq_no_callback)
2103 /* append after the last item that was pushed back */
2104 item = item->jq_next;
2105
2106 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2107 if (newitem == NULL)
2108 clear_tv(rettv);
2109 else
2110 {
2111 newitem->jq_value = alloc_tv();
2112 if (newitem->jq_value == NULL)
2113 {
2114 vim_free(newitem);
2115 clear_tv(rettv);
2116 }
2117 else
2118 {
2119 newitem->jq_no_callback = FALSE;
2120 *newitem->jq_value = *rettv;
2121 if (item == NULL)
2122 {
2123 /* append to the end */
2124 newitem->jq_prev = head->jq_prev;
2125 head->jq_prev = newitem;
2126 newitem->jq_next = NULL;
2127 if (newitem->jq_prev == NULL)
2128 head->jq_next = newitem;
2129 else
2130 newitem->jq_prev->jq_next = newitem;
2131 }
2132 else
2133 {
2134 /* append after "item" */
2135 newitem->jq_prev = item;
2136 newitem->jq_next = item->jq_next;
2137 item->jq_next = newitem;
2138 if (newitem->jq_next == NULL)
2139 head->jq_prev = newitem;
2140 else
2141 newitem->jq_next->jq_prev = newitem;
2142 }
2143 }
2144 }
2145}
2146
Bram Moolenaarece61b02016-02-20 21:39:05 +01002147#define CH_JSON_MAX_ARGS 4
2148
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002149/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002150 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002151 * "argv[0]" is the command string.
2152 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002153 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002154 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002155channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002156{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002157 char_u *cmd = argv[0].vval.v_string;
2158 char_u *arg;
2159 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002160
Bram Moolenaarece61b02016-02-20 21:39:05 +01002161 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002162 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002163 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002164 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002165 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002166 return;
2167 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002168 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002169 if (arg == NULL)
2170 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002171
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002172 if (STRCMP(cmd, "ex") == 0)
2173 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002174 int save_called_emsg = called_emsg;
2175
2176 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002177 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002178 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002179 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002180 --emsg_silent;
2181 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002182 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002183 (char *)get_vim_var_str(VV_ERRMSG));
2184 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002185 }
2186 else if (STRCMP(cmd, "normal") == 0)
2187 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002188 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002189
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002190 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002191 ea.arg = arg;
2192 ea.addr_count = 0;
2193 ea.forceit = TRUE; /* no mapping */
2194 ex_normal(&ea);
2195 }
2196 else if (STRCMP(cmd, "redraw") == 0)
2197 {
2198 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002199
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002200 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002201 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002202 ex_redraw(&ea);
2203 showruler(FALSE);
2204 setcursor();
2205 out_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002206#ifdef FEAT_GUI
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002207 if (gui.in_use)
2208 {
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002209 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002210 gui_mch_flush();
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002211 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002212#endif
2213 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002214 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002215 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002216 int is_call = cmd[0] == 'c';
2217 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002218
Bram Moolenaarece61b02016-02-20 21:39:05 +01002219 if (argv[id_idx].v_type != VAR_UNKNOWN
2220 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002221 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002222 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002223 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002224 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002225 }
2226 else if (is_call && argv[2].v_type != VAR_LIST)
2227 {
2228 ch_error(channel, "third argument for call must be a list");
2229 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002230 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002231 }
2232 else
2233 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002234 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002235 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002236 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002237 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002238
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002239 /* Don't pollute the display with errors. */
2240 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002241 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002242 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002243 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002244 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002245 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002246 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002247 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002248 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002249 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2250 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002251 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002252
2253 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002254 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002255 int id = argv[id_idx].vval.v_number;
2256
Bram Moolenaar55fab432016-02-07 16:53:13 +01002257 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002258 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002259 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002260 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002261 /* If evaluation failed or the result can't be encoded
2262 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002263 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002264 err_tv.v_type = VAR_STRING;
2265 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002266 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002267 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002268 if (json != NULL)
2269 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002270 channel_send(channel,
2271 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002272 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002273 vim_free(json);
2274 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002275 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002276 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002277 if (tv == &res_tv)
2278 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002279 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002280 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002281 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002282 }
2283 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002284 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002285 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002286 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002287 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002288}
2289
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002290/*
2291 * Invoke the callback at "cbhead".
2292 * Does not redraw but sets channel_need_redraw.
2293 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002294 static void
2295invoke_one_time_callback(
2296 channel_T *channel,
2297 cbq_T *cbhead,
2298 cbq_T *item,
2299 typval_T *argv)
2300{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002301 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002302 (char *)item->cq_callback);
2303 /* Remove the item from the list first, if the callback
2304 * invokes ch_close() the list will be cleared. */
2305 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002306 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002307 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002308 vim_free(item);
2309}
2310
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002311 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002312append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002313{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002314 bufref_T save_curbuf = {NULL, 0, 0};
2315 win_T *save_curwin = NULL;
2316 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002317 linenr_T lnum = buffer->b_ml.ml_line_count;
2318 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002319 chanpart_T *ch_part = &channel->ch_part[part];
2320 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002321 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002322
2323 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2324 {
2325 if (!ch_part->ch_nomod_error)
2326 {
2327 ch_error(channel, "Buffer is not modifiable, cannot append");
2328 ch_part->ch_nomod_error = TRUE;
2329 }
2330 return;
2331 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002332
2333 /* If the buffer is also used as input insert above the last
2334 * line. Don't write these lines. */
2335 if (save_write_to)
2336 {
2337 --lnum;
2338 buffer->b_write_to_channel = FALSE;
2339 }
2340
2341 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002342 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002343
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002344 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002345
2346 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2347 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2348
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002349 u_sync(TRUE);
2350 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002351 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002352
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002353 if (empty)
2354 {
2355 /* The buffer is empty, replace the first (dummy) line. */
2356 ml_replace(lnum, msg, TRUE);
2357 lnum = 0;
2358 }
2359 else
2360 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002361 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002362
2363 /* Restore curbuf/curwin/curtab */
2364 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2365
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002366 if (ch_part->ch_nomodifiable)
2367 buffer->b_p_ma = FALSE;
2368 else
2369 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002370
2371 if (buffer->b_nwindows > 0)
2372 {
2373 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002374
2375 FOR_ALL_WINDOWS(wp)
2376 {
2377 if (wp->w_buffer == buffer
2378 && (save_write_to
2379 ? wp->w_cursor.lnum == lnum + 1
2380 : (wp->w_cursor.lnum == lnum
2381 && wp->w_cursor.col == 0)))
2382 {
2383 ++wp->w_cursor.lnum;
2384 save_curwin = curwin;
2385 curwin = wp;
2386 curbuf = curwin->w_buffer;
2387 scroll_cursor_bot(0, FALSE);
2388 curwin = save_curwin;
2389 curbuf = curwin->w_buffer;
2390 }
2391 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002392 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002393 channel_need_redraw = TRUE;
2394 }
2395
2396 if (save_write_to)
2397 {
2398 channel_T *ch;
2399
2400 /* Find channels reading from this buffer and adjust their
2401 * next-to-read line number. */
2402 buffer->b_write_to_channel = TRUE;
2403 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2404 {
2405 chanpart_T *in_part = &ch->ch_part[PART_IN];
2406
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002407 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002408 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2409 }
2410 }
2411}
2412
Bram Moolenaar437905c2016-04-26 19:01:05 +02002413 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002414drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002415{
2416 char_u *msg;
2417
2418 while ((msg = channel_get(channel, part)) != NULL)
2419 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002420 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002421 vim_free(msg);
2422 }
2423}
2424
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002425/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002426 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002427 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002428 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002429 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002430 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002431may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002432{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002433 char_u *msg = NULL;
2434 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002435 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002436 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002437 chanpart_T *ch_part = &channel->ch_part[part];
2438 ch_mode_T ch_mode = ch_part->ch_mode;
2439 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002440 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002441 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002442 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002443 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002444 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002445
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002446 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002447 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002448 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002449
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002450 /* Use a message-specific callback, part callback or channel callback */
2451 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2452 if (cbitem->cq_seq_nr == 0)
2453 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002454 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002455 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002456 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002457 partial = cbitem->cq_partial;
2458 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002459 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002460 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002461 callback = ch_part->ch_callback;
2462 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002463 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002464 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002465 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002466 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002467 partial = channel->ch_partial;
2468 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002469
Bram Moolenaarec68a992016-10-03 21:37:41 +02002470 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002471 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2472 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002473 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002474 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002475 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002476 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002477 buffer = NULL;
2478 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002479
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002480 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002481 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002482 listitem_T *item;
2483 int argc = 0;
2484
Bram Moolenaard7ece102016-02-02 23:23:02 +01002485 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002486 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002487 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002488 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002489 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002490 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002491 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002492 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002493
Bram Moolenaarece61b02016-02-20 21:39:05 +01002494 for (item = listtv->vval.v_list->lv_first;
2495 item != NULL && argc < CH_JSON_MAX_ARGS;
2496 item = item->li_next)
2497 argv[argc++] = item->li_tv;
2498 while (argc < CH_JSON_MAX_ARGS)
2499 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002500
Bram Moolenaarece61b02016-02-20 21:39:05 +01002501 if (argv[0].v_type == VAR_STRING)
2502 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002503 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002504 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002505 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002506 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002507 }
2508
Bram Moolenaarece61b02016-02-20 21:39:05 +01002509 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002510 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002511 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002512 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002513 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002514 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002515 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002516 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002517 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002518 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002519 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002520 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002521 return FALSE;
2522 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002523 else
2524 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002525 /* If there is no callback or buffer drop the message. */
2526 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002527 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002528 /* If there is a close callback it may use ch_read() to get the
2529 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002530 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002531 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002532 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002533 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002534
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002535 if (ch_mode == MODE_NL)
2536 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002537 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002538 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002539 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002540
2541 /* See if we have a message ending in NL in the first buffer. If
2542 * not try to concatenate the first and the second buffer. */
2543 while (TRUE)
2544 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002545 node = channel_peek(channel, part);
2546 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002547 if (nl != NULL)
2548 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002549 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002550 {
2551 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2552 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002553 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002554 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002555 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002556 buf = node->rq_buffer;
2557
Bram Moolenaarec68a992016-10-03 21:37:41 +02002558 if (nl == NULL)
2559 {
2560 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002561 char_u *new_buf;
2562
2563 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2564 if (new_buf == NULL)
2565 /* This might fail over and over again, should the message
2566 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002567 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002568 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002569 node->rq_buffer = buf;
2570 nl = buf + node->rq_buflen++;
2571 *nl = NUL;
2572 }
2573
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002574 /* Convert NUL to NL, the internal representation. */
2575 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2576 if (*p == NUL)
2577 *p = NL;
2578
2579 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002580 {
2581 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002582 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002583 *nl = NUL;
2584 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002585 else
2586 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002587 /* Copy the message into allocated memory (excluding the NL)
2588 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002589 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002590 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002591 }
2592 }
2593 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002594 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002595 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002596 * get everything we have.
2597 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002598 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002599 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002600
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002601 if (msg == NULL)
2602 return FALSE; /* out of memory (and avoids Coverity warning) */
2603
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002604 argv[1].v_type = VAR_STRING;
2605 argv[1].vval.v_string = msg;
2606 }
2607
Bram Moolenaara07fec92016-02-05 21:04:08 +01002608 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002609 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002610 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002611
Bram Moolenaar958dc692016-12-01 15:34:12 +01002612 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002613 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002614 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002615 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002616 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002617 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002618 break;
2619 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002620 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002621 {
2622 if (channel->ch_drop_never)
2623 {
2624 /* message must be read with ch_read() */
2625 channel_push_json(channel, part, listtv);
2626 listtv = NULL;
2627 }
2628 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002629 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002630 seq_nr);
2631 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002632 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002633 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002634 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002635 if (buffer != NULL)
2636 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002637 if (msg == NULL)
2638 /* JSON or JS mode: re-encode the message. */
2639 msg = json_encode(listtv, ch_mode);
2640 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002641 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002642#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002643 if (buffer->b_term != NULL)
2644 write_to_term(buffer, msg, channel);
2645 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002646#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002647 append_to_buffer(buffer, msg, channel, part);
2648 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002649 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002650
Bram Moolenaar187db502016-02-27 14:44:26 +01002651 if (callback != NULL)
2652 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002653 if (cbitem != NULL)
2654 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2655 else
2656 {
2657 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002658 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002659 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002660 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002661 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002662 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002663 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002664 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002665 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002666
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002667 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002668 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002669 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002670
2671 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002672}
2673
2674/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002675 * Return TRUE when channel "channel" is open for writing to.
2676 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002677 */
2678 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002679channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002680{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002681 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002682 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002683}
2684
2685/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002686 * Return TRUE when channel "channel" is open for reading or writing.
2687 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002688 */
2689 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002690channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002691{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002692 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002693 || channel->CH_IN_FD != INVALID_FD
2694 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002695 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002696}
2697
2698/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002699 * Return TRUE if "channel" has JSON or other typeahead.
2700 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002701 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002702channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002703{
2704 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2705
2706 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2707 {
2708 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2709 jsonq_T *item = head->jq_next;
2710
2711 return item != NULL;
2712 }
2713 return channel_peek(channel, part) != NULL;
2714}
2715
2716/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002717 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002718 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002719 */
2720 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002721channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002722{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002723 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002724 int has_readahead = FALSE;
2725
Bram Moolenaar77073442016-02-13 23:23:53 +01002726 if (channel == NULL)
2727 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002728 if (req_part == PART_OUT)
2729 {
2730 if (channel->CH_OUT_FD != INVALID_FD)
2731 return "open";
2732 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002733 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002734 }
2735 else if (req_part == PART_ERR)
2736 {
2737 if (channel->CH_ERR_FD != INVALID_FD)
2738 return "open";
2739 if (channel_has_readahead(channel, PART_ERR))
2740 has_readahead = TRUE;
2741 }
2742 else
2743 {
2744 if (channel_is_open(channel))
2745 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002746 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002747 if (channel_has_readahead(channel, part))
2748 {
2749 has_readahead = TRUE;
2750 break;
2751 }
2752 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002753
2754 if (has_readahead)
2755 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002756 return "closed";
2757}
2758
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002759 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002760channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002761{
2762 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002763 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002764 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002765 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002766 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002767
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002768 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002769 STRCAT(namebuf, "_");
2770 tail = STRLEN(namebuf);
2771
2772 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002773 if (chanpart->ch_fd != INVALID_FD)
2774 status = "open";
2775 else if (channel_has_readahead(channel, part))
2776 status = "buffered";
2777 else
2778 status = "closed";
2779 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002780
2781 STRCPY(namebuf + tail, "mode");
2782 switch (chanpart->ch_mode)
2783 {
2784 case MODE_NL: s = "NL"; break;
2785 case MODE_RAW: s = "RAW"; break;
2786 case MODE_JSON: s = "JSON"; break;
2787 case MODE_JS: s = "JS"; break;
2788 }
2789 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2790
2791 STRCPY(namebuf + tail, "io");
2792 if (part == PART_SOCK)
2793 s = "socket";
2794 else switch (chanpart->ch_io)
2795 {
2796 case JIO_NULL: s = "null"; break;
2797 case JIO_PIPE: s = "pipe"; break;
2798 case JIO_FILE: s = "file"; break;
2799 case JIO_BUFFER: s = "buffer"; break;
2800 case JIO_OUT: s = "out"; break;
2801 }
2802 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2803
2804 STRCPY(namebuf + tail, "timeout");
2805 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2806}
2807
2808 void
2809channel_info(channel_T *channel, dict_T *dict)
2810{
2811 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002812 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002813
2814 if (channel->ch_hostname != NULL)
2815 {
2816 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2817 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2818 channel_part_info(channel, dict, "sock", PART_SOCK);
2819 }
2820 else
2821 {
2822 channel_part_info(channel, dict, "out", PART_OUT);
2823 channel_part_info(channel, dict, "err", PART_ERR);
2824 channel_part_info(channel, dict, "in", PART_IN);
2825 }
2826}
2827
Bram Moolenaar77073442016-02-13 23:23:53 +01002828/*
2829 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002830 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002831 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002832 */
2833 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002834channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002835{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002836 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002837
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002838#ifdef FEAT_GUI
2839 channel_gui_unregister(channel);
2840#endif
2841
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002842 ch_close_part(channel, PART_SOCK);
2843 ch_close_part(channel, PART_IN);
2844 ch_close_part(channel, PART_OUT);
2845 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002846
Bram Moolenaara9f02812017-08-05 17:40:38 +02002847 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002848 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002849 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002850
Bram Moolenaara9f02812017-08-05 17:40:38 +02002851 /* Invoke callbacks and flush buffers before the close callback. */
2852 if (channel->ch_close_cb != NULL)
2853 ch_log(channel,
2854 "Invoking callbacks and flushing buffers before closing");
2855 for (part = PART_SOCK; part < PART_IN; ++part)
2856 {
2857 if (channel->ch_close_cb != NULL
2858 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2859 {
2860 /* Increment the refcount to avoid the channel being freed
2861 * halfway. */
2862 ++channel->ch_refcount;
2863 if (channel->ch_close_cb == NULL)
2864 ch_log(channel, "flushing %s buffers before closing",
2865 part_names[part]);
2866 while (may_invoke_callback(channel, part))
2867 ;
2868 --channel->ch_refcount;
2869 }
2870 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002871
Bram Moolenaara9f02812017-08-05 17:40:38 +02002872 if (channel->ch_close_cb != NULL)
2873 {
2874 typval_T argv[1];
2875 typval_T rettv;
2876 int dummy;
2877
2878 /* Increment the refcount to avoid the channel being freed
2879 * halfway. */
2880 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002881 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002882 (char *)channel->ch_close_cb);
2883 argv[0].v_type = VAR_CHANNEL;
2884 argv[0].vval.v_channel = channel;
2885 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002886 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002887 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002888 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002889 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002890
Bram Moolenaara9f02812017-08-05 17:40:38 +02002891 /* the callback is only called once */
2892 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2893 channel->ch_close_cb = NULL;
2894 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002895
Bram Moolenaara9f02812017-08-05 17:40:38 +02002896 --channel->ch_refcount;
Bram Moolenaar28ae5772016-05-28 14:16:10 +02002897
Bram Moolenaara9f02812017-08-05 17:40:38 +02002898 if (channel_need_redraw)
2899 {
2900 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002901 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002902 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002903
Bram Moolenaara9f02812017-08-05 17:40:38 +02002904 if (!channel->ch_drop_never)
2905 /* any remaining messages are useless now */
2906 for (part = PART_SOCK; part < PART_IN; ++part)
2907 drop_messages(channel, part);
2908 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002909 }
2910
2911 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002912
2913#ifdef FEAT_TERMINAL
2914 term_channel_closed(channel);
2915#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002916}
2917
Bram Moolenaard04a0202016-01-26 23:30:18 +01002918/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002919 * Close the "in" part channel "channel".
2920 */
2921 void
2922channel_close_in(channel_T *channel)
2923{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002924 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002925}
2926
2927/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002928 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002929 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002930 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002931channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002932{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002933 jsonq_T *json_head = &channel->ch_part[part].ch_json_head;
2934 cbq_T *cb_head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002935
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002936 while (channel_peek(channel, part) != NULL)
2937 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002938
2939 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002940 {
2941 cbq_T *node = cb_head->cq_next;
2942
2943 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002944 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002945 vim_free(node);
2946 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002947
2948 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002949 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002950 free_tv(json_head->jq_next->jq_value);
2951 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002952 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002953
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002954 free_callback(channel->ch_part[part].ch_callback,
2955 channel->ch_part[part].ch_partial);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002956 channel->ch_part[part].ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002957 channel->ch_part[part].ch_partial = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002958}
2959
2960/*
2961 * Clear all the read buffers on "channel".
2962 */
2963 void
2964channel_clear(channel_T *channel)
2965{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002966 ch_log(channel, "Clearing channel");
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002967 vim_free(channel->ch_hostname);
2968 channel->ch_hostname = NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002969 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002970 channel_clear_one(channel, PART_OUT);
2971 channel_clear_one(channel, PART_ERR);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02002972 /* there is no callback or queue for PART_IN */
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002973 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002974 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002975 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002976 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002977 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002978 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002979}
2980
Bram Moolenaar77073442016-02-13 23:23:53 +01002981#if defined(EXITFREE) || defined(PROTO)
2982 void
2983channel_free_all(void)
2984{
2985 channel_T *channel;
2986
Bram Moolenaard6051b52016-02-28 15:49:03 +01002987 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01002988 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
2989 channel_clear(channel);
2990}
2991#endif
2992
2993
Bram Moolenaar715d2852016-04-30 17:06:31 +02002994/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01002995#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01002996
2997/* Buffer size for reading incoming messages. */
2998#define MAXMSGSIZE 4096
2999
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003000#if defined(HAVE_SELECT)
3001/*
3002 * Add write fds where we are waiting for writing to be possible.
3003 */
3004 static int
3005channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3006{
3007 int maxfd = maxfd_arg;
3008 channel_T *ch;
3009
3010 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3011 {
3012 chanpart_T *in_part = &ch->ch_part[PART_IN];
3013
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003014 if (in_part->ch_fd != INVALID_FD
3015 && (in_part->ch_bufref.br_buf != NULL
3016 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003017 {
3018 FD_SET((int)in_part->ch_fd, wfds);
3019 if ((int)in_part->ch_fd >= maxfd)
3020 maxfd = (int)in_part->ch_fd + 1;
3021 }
3022 }
3023 return maxfd;
3024}
3025#else
3026/*
3027 * Add write fds where we are waiting for writing to be possible.
3028 */
3029 static int
3030channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3031{
3032 int nfd = nfd_in;
3033 channel_T *ch;
3034
3035 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3036 {
3037 chanpart_T *in_part = &ch->ch_part[PART_IN];
3038
Bram Moolenaar683b7962017-08-19 15:51:59 +02003039 if (in_part->ch_fd != INVALID_FD
3040 && (in_part->ch_bufref.br_buf != NULL
3041 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003042 {
3043 in_part->ch_poll_idx = nfd;
3044 fds[nfd].fd = in_part->ch_fd;
3045 fds[nfd].events = POLLOUT;
3046 ++nfd;
3047 }
3048 else
3049 in_part->ch_poll_idx = -1;
3050 }
3051 return nfd;
3052}
3053#endif
3054
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003055typedef enum {
3056 CW_READY,
3057 CW_NOT_READY,
3058 CW_ERROR
3059} channel_wait_result;
3060
Bram Moolenaard04a0202016-01-26 23:30:18 +01003061/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003062 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003063 * Return CW_READY when there is something to read.
3064 * Return CW_NOT_READY when there is nothing to read.
3065 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003066 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003067 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003068channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003069{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003070 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003071 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003072
Bram Moolenaard8070362016-02-15 21:56:54 +01003073# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003074 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003075 {
3076 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003077 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003078 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003079 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003080
3081 /* reading from a pipe, not a socket */
3082 while (TRUE)
3083 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003084 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3085
3086 if (r && nread > 0)
3087 return CW_READY;
3088 if (r == 0)
3089 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003090
3091 /* perhaps write some buffer lines */
3092 channel_write_any_lines();
3093
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003094 sleep_time = deadline - GetTickCount();
3095 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003096 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003097 /* Wait for a little while. Very short at first, up to 10 msec
3098 * after looping a few times. */
3099 if (sleep_time > delay)
3100 sleep_time = delay;
3101 Sleep(sleep_time);
3102 delay = delay * 2;
3103 if (delay > 10)
3104 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003105 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003106 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003107 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003108#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003109 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003110#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003111 struct timeval tval;
3112 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003113 fd_set wfds;
3114 int ret;
3115 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003116
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003117 tval.tv_sec = timeout / 1000;
3118 tval.tv_usec = (timeout % 1000) * 1000;
3119 for (;;)
3120 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003121 FD_ZERO(&rfds);
3122 FD_SET((int)fd, &rfds);
3123
3124 /* Write lines to a pipe when a pipe can be written to. Need to
3125 * set this every time, some buffers may be done. */
3126 maxfd = (int)fd + 1;
3127 FD_ZERO(&wfds);
3128 maxfd = channel_fill_wfds(maxfd, &wfds);
3129
3130 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003131# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003132 SOCK_ERRNO;
3133 if (ret == -1 && errno == EINTR)
3134 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003135# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003136 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003137 {
3138 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003139 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003140 channel_write_any_lines();
3141 continue;
3142 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003143 break;
3144 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003145#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003146 for (;;)
3147 {
3148 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3149 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003150
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003151 fds[0].fd = fd;
3152 fds[0].events = POLLIN;
3153 nfd = channel_fill_poll_write(nfd, fds);
3154 if (poll(fds, nfd, timeout) > 0)
3155 {
3156 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003157 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003158 channel_write_any_lines();
3159 continue;
3160 }
3161 break;
3162 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003163#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003164 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003165 return CW_NOT_READY;
3166}
3167
3168 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003169ch_close_part_on_error(
3170 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003171{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003172 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003173
3174 if (is_err)
3175 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003176 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003177 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003178 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003179
3180 /* Queue a "DETACH" netbeans message in the command queue in order to
3181 * terminate the netbeans session later. Do not end the session here
3182 * directly as we may be running in the context of a call to
3183 * netbeans_parse_messages():
3184 * netbeans_parse_messages
3185 * -> autocmd triggered while processing the netbeans cmd
3186 * -> ui_breakcheck
3187 * -> gui event loop or select loop
3188 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003189 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003190 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003191 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003192 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003193 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3194
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003195 /* When reading is not possible close this part of the channel. Don't
3196 * close the channel yet, there may be something to read on another part. */
3197 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003198
3199#ifdef FEAT_GUI
3200 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003201 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003202#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003203}
3204
3205 static void
3206channel_close_now(channel_T *channel)
3207{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003208 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003209 if (channel->ch_nb_close_cb != NULL)
3210 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003211 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003212}
3213
3214/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003215 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003216 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003217 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003218 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003219 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003220channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003221{
3222 static char_u *buf = NULL;
3223 int len = 0;
3224 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003225 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003226 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003227
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003228 fd = channel->ch_part[part].ch_fd;
3229 if (fd == INVALID_FD)
3230 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003231 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003232 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003233 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003234 }
3235 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003236
3237 /* Allocate a buffer to read into. */
3238 if (buf == NULL)
3239 {
3240 buf = alloc(MAXMSGSIZE);
3241 if (buf == NULL)
3242 return; /* out of memory! */
3243 }
3244
3245 /* Keep on reading for as long as there is something to read.
3246 * Use select() or poll() to avoid blocking on a message that is exactly
3247 * MAXMSGSIZE long. */
3248 for (;;)
3249 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003250 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003251 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003252 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003253 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003254 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003255 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003256 if (len <= 0)
3257 break; /* error or nothing more to read */
3258
3259 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003260 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003261 readlen += len;
3262 if (len < MAXMSGSIZE)
3263 break; /* did read everything that's available */
3264 }
3265
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003266 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003267 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003268 {
3269 if (!channel->ch_keep_open)
3270 ch_close_part_on_error(channel, part, (len < 0), func);
3271 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003272#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003273 else if (CH_HAS_GUI && gtk_main_level() > 0)
3274 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003275 gtk_main_quit();
3276#endif
3277}
3278
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003279/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003280 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003281 * read or the timeout expires.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003282 * Returns what was read in allocated memory.
3283 * Returns NULL in case of error or timeout.
3284 */
3285 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003286channel_read_block(channel_T *channel, ch_part_T part, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003287{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003288 char_u *buf;
3289 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003290 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003291 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003292 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003293 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003294
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003295 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003296 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003297
3298 while (TRUE)
3299 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003300 node = channel_peek(channel, part);
3301 if (node != NULL)
3302 {
3303 if (mode == MODE_RAW || (mode == MODE_NL
3304 && channel_first_nl(node) != NULL))
3305 /* got a complete message */
3306 break;
3307 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3308 continue;
3309 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003310
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003311 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003312 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003313 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003314 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003315 {
3316 ch_log(channel, "Timed out");
3317 return NULL;
3318 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003319 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003320 }
3321
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003322 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003323 if (mode == MODE_RAW)
3324 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003325 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003326 }
3327 else
3328 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003329 char_u *p;
3330
3331 buf = node->rq_buffer;
3332 nl = channel_first_nl(node);
3333
3334 /* Convert NUL to NL, the internal representation. */
3335 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
3336 if (*p == NUL)
3337 *p = NL;
3338
3339 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003340 {
3341 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003342 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003343 *nl = NUL;
3344 }
3345 else
3346 {
3347 /* Copy the message into allocated memory and remove it from the
3348 * buffer. */
3349 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003350 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003351 }
3352 }
3353 if (log_fd != NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003354 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003355 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003356}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003357
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003358/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003359 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003360 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003361 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003362 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003363 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003364 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003365channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003366 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003367 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003368 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003369 int id,
3370 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003371{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003372 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003373 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003374 int timeout;
3375 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003376
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003377 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003378 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003379 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003380 for (;;)
3381 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003382 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003383
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003384 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003385 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003386 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003387 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003388 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003389 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003390
Bram Moolenaard7ece102016-02-02 23:23:02 +01003391 if (!more)
3392 {
3393 /* Handle any other messages in the queue. If done some more
3394 * messages may have arrived. */
3395 if (channel_parse_messages())
3396 continue;
3397
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003398 /* Wait for up to the timeout. If there was an incomplete message
3399 * use the deadline for that. */
3400 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003401 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003402 {
3403#ifdef WIN32
3404 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3405#else
3406 {
3407 struct timeval now_tv;
3408
3409 gettimeofday(&now_tv, NULL);
3410 timeout = (chanpart->ch_deadline.tv_sec
3411 - now_tv.tv_sec) * 1000
3412 + (chanpart->ch_deadline.tv_usec
3413 - now_tv.tv_usec) / 1000
3414 + 1;
3415 }
3416#endif
3417 if (timeout < 0)
3418 {
3419 /* Something went wrong, channel_parse_json() didn't
3420 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003421 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003422 timeout = timeout_arg;
3423 }
3424 else if (timeout > timeout_arg)
3425 timeout = timeout_arg;
3426 }
3427 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003428 if (fd == INVALID_FD
3429 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003430 {
3431 if (timeout == timeout_arg)
3432 {
3433 if (fd != INVALID_FD)
3434 ch_log(channel, "Timed out");
3435 break;
3436 }
3437 }
3438 else
3439 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003440 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003441 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003442 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003443 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003444}
3445
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003446/*
3447 * Common for ch_read() and ch_readraw().
3448 */
3449 void
3450common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3451{
3452 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003453 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003454 jobopt_T opt;
3455 int mode;
3456 int timeout;
3457 int id = -1;
3458 typval_T *listtv = NULL;
3459
3460 /* return an empty string by default */
3461 rettv->v_type = VAR_STRING;
3462 rettv->vval.v_string = NULL;
3463
3464 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003465 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003466 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003467 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003468
Bram Moolenaar437905c2016-04-26 19:01:05 +02003469 if (opt.jo_set & JO_PART)
3470 part = opt.jo_part;
3471 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003472 if (channel != NULL)
3473 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003474 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003475 part = channel_part_read(channel);
3476 mode = channel_get_mode(channel, part);
3477 timeout = channel_get_timeout(channel, part);
3478 if (opt.jo_set & JO_TIMEOUT)
3479 timeout = opt.jo_timeout;
3480
3481 if (raw || mode == MODE_RAW || mode == MODE_NL)
3482 rettv->vval.v_string = channel_read_block(channel, part, timeout);
3483 else
3484 {
3485 if (opt.jo_set & JO_ID)
3486 id = opt.jo_id;
3487 channel_read_json_block(channel, part, timeout, id, &listtv);
3488 if (listtv != NULL)
3489 {
3490 *rettv = *listtv;
3491 vim_free(listtv);
3492 }
3493 else
3494 {
3495 rettv->v_type = VAR_SPECIAL;
3496 rettv->vval.v_number = VVAL_NONE;
3497 }
3498 }
3499 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003500
3501theend:
3502 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003503}
3504
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003505# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3506 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003507/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003508 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003509 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003510 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003511 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003512channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003513{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003514 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003515 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003516
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003517 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003518 for (channel = first_channel; channel != NULL;
3519 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003520 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003521 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003522 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003523 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003524 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003525 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003526 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003527 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003528 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003529}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003530# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003531
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003532# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003533/*
3534 * Check the channels for anything that is ready to be read.
3535 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003536 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003537 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003538 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003539channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003540{
3541 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003542 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003543 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003544
3545 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3546 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003547 if (only_keep_open && !channel->ch_keep_open)
3548 continue;
3549
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003550 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003551 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003552 {
3553 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003554 if (fd != INVALID_FD)
3555 {
3556 int r = channel_wait(channel, fd, 0);
3557
3558 if (r == CW_READY)
3559 channel_read(channel, part, "channel_handle_events");
3560 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003561 ch_close_part_on_error(channel, part, TRUE,
3562 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003563 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003564 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003565 }
3566}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003567# endif
3568
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003569# if defined(FEAT_GUI) || defined(PROTO)
3570/*
3571 * Return TRUE when there is any channel with a keep_open flag.
3572 */
3573 int
3574channel_any_keep_open()
3575{
3576 channel_T *channel;
3577
3578 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3579 if (channel->ch_keep_open)
3580 return TRUE;
3581 return FALSE;
3582}
3583# endif
3584
Bram Moolenaard04a0202016-01-26 23:30:18 +01003585/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003586 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003587 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003588 */
3589 void
3590channel_set_nonblock(channel_T *channel, ch_part_T part)
3591{
3592 chanpart_T *ch_part = &channel->ch_part[part];
3593 int fd = ch_part->ch_fd;
3594
3595 if (fd != INVALID_FD)
3596 {
3597#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003598 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003599
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003600 ioctlsocket(fd, FIONBIO, &val);
3601#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003602 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003603#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003604 ch_part->ch_nonblocking = TRUE;
3605 }
3606}
3607
3608/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003609 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003610 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003611 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003612 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003613 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003614channel_send(
3615 channel_T *channel,
3616 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003617 char_u *buf_arg,
3618 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003619 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003620{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003621 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003622 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003623 chanpart_T *ch_part = &channel->ch_part[part];
3624 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003625
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003626 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003627 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003628 {
3629 if (!channel->ch_error && fun != NULL)
3630 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003631 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003632 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003633 }
3634 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003635 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003636 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003637
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003638 if (log_fd != NULL)
3639 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003640 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003641 fprintf(log_fd, "'");
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003642 ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003643 fprintf(log_fd, "'\n");
3644 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003645 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003646 }
3647
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003648 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003649 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003650 writeq_T *wq = &ch_part->ch_writeque;
3651 char_u *buf;
3652 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003653
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003654 if (wq->wq_next != NULL)
3655 {
3656 /* first write what was queued */
3657 buf = wq->wq_next->wq_ga.ga_data;
3658 len = wq->wq_next->wq_ga.ga_len;
3659 did_use_queue = TRUE;
3660 }
3661 else
3662 {
3663 if (len_arg == 0)
3664 /* nothing to write, called from channel_select_check() */
3665 return OK;
3666 buf = buf_arg;
3667 len = len_arg;
3668 }
3669
3670 if (part == PART_SOCK)
3671 res = sock_write(fd, (char *)buf, len);
3672 else
3673 res = fd_write(fd, (char *)buf, len);
3674 if (res < 0 && (errno == EWOULDBLOCK
3675#ifdef EAGAIN
3676 || errno == EAGAIN
3677#endif
3678 ))
3679 res = 0; /* nothing got written */
3680
3681 if (res >= 0 && ch_part->ch_nonblocking)
3682 {
3683 writeq_T *entry = wq->wq_next;
3684
3685 if (did_use_queue)
3686 ch_log(channel, "Sent %d bytes now", res);
3687 if (res == len)
3688 {
3689 /* Wrote all the buf[len] bytes. */
3690 if (entry != NULL)
3691 {
3692 /* Remove the entry from the write queue. */
3693 ga_clear(&entry->wq_ga);
3694 wq->wq_next = entry->wq_next;
3695 if (wq->wq_next == NULL)
3696 wq->wq_prev = NULL;
3697 else
3698 wq->wq_next->wq_prev = NULL;
3699 continue;
3700 }
3701 if (did_use_queue)
3702 ch_log(channel, "Write queue empty");
3703 }
3704 else
3705 {
3706 /* Wrote only buf[res] bytes, can't write more now. */
3707 if (entry != NULL)
3708 {
3709 if (res > 0)
3710 {
3711 /* Remove the bytes that were written. */
3712 mch_memmove(entry->wq_ga.ga_data,
3713 (char *)entry->wq_ga.ga_data + res,
3714 len - res);
3715 entry->wq_ga.ga_len -= res;
3716 }
3717 buf = buf_arg;
3718 len = len_arg;
3719 }
3720 else
3721 {
3722 buf += res;
3723 len -= res;
3724 }
3725 ch_log(channel, "Adding %d bytes to the write queue", len);
3726
3727 /* Append the not written bytes of the argument to the write
3728 * buffer. Limit entries to 4000 bytes. */
3729 if (wq->wq_prev != NULL
3730 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3731 {
3732 writeq_T *last = wq->wq_prev;
3733
3734 /* append to the last entry */
3735 if (ga_grow(&last->wq_ga, len) == OK)
3736 {
3737 mch_memmove((char *)last->wq_ga.ga_data
3738 + last->wq_ga.ga_len,
3739 buf, len);
3740 last->wq_ga.ga_len += len;
3741 }
3742 }
3743 else
3744 {
3745 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3746
3747 if (last != NULL)
3748 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003749 last->wq_prev = wq->wq_prev;
3750 last->wq_next = NULL;
3751 if (wq->wq_prev == NULL)
3752 wq->wq_next = last;
3753 else
3754 wq->wq_prev->wq_next = last;
3755 wq->wq_prev = last;
3756 ga_init2(&last->wq_ga, 1, 1000);
3757 if (ga_grow(&last->wq_ga, len) == OK)
3758 {
3759 mch_memmove(last->wq_ga.ga_data, buf, len);
3760 last->wq_ga.ga_len = len;
3761 }
3762 }
3763 }
3764 }
3765 }
3766 else if (res != len)
3767 {
3768 if (!channel->ch_error && fun != NULL)
3769 {
3770 ch_error(channel, "%s(): write failed", fun);
3771 EMSG2(_("E631: %s(): write failed"), fun);
3772 }
3773 channel->ch_error = TRUE;
3774 return FAIL;
3775 }
3776
3777 channel->ch_error = FALSE;
3778 return OK;
3779 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003780}
3781
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003782/*
3783 * Common for "ch_sendexpr()" and "ch_sendraw()".
3784 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003785 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003786 * Otherwise returns NULL.
3787 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003788 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003789send_common(
3790 typval_T *argvars,
3791 char_u *text,
3792 int id,
3793 int eval,
3794 jobopt_T *opt,
3795 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003796 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003797{
3798 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003799 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003800
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003801 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003802 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003803 if (channel == NULL)
3804 return NULL;
3805 part_send = channel_part_send(channel);
3806 *part_read = channel_part_read(channel);
3807
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003808 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003809 return NULL;
3810
3811 /* Set the callback. An empty callback means no callback and not reading
3812 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3813 * allowed. */
3814 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3815 {
3816 if (eval)
3817 {
3818 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3819 return NULL;
3820 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003821 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003822 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003823 }
3824
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003825 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003826 && opt->jo_callback == NULL)
3827 return channel;
3828 return NULL;
3829}
3830
3831/*
3832 * common for "ch_evalexpr()" and "ch_sendexpr()"
3833 */
3834 void
3835ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3836{
3837 char_u *text;
3838 typval_T *listtv;
3839 channel_T *channel;
3840 int id;
3841 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003842 ch_part_T part_send;
3843 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003844 jobopt_T opt;
3845 int timeout;
3846
3847 /* return an empty string by default */
3848 rettv->v_type = VAR_STRING;
3849 rettv->vval.v_string = NULL;
3850
Bram Moolenaar437905c2016-04-26 19:01:05 +02003851 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852 if (channel == NULL)
3853 return;
3854 part_send = channel_part_send(channel);
3855
3856 ch_mode = channel_get_mode(channel, part_send);
3857 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3858 {
3859 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3860 return;
3861 }
3862
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003863 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003864 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003865 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003866 if (text == NULL)
3867 return;
3868
3869 channel = send_common(argvars, text, id, eval, &opt,
3870 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3871 vim_free(text);
3872 if (channel != NULL && eval)
3873 {
3874 if (opt.jo_set & JO_TIMEOUT)
3875 timeout = opt.jo_timeout;
3876 else
3877 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003878 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3879 == OK)
3880 {
3881 list_T *list = listtv->vval.v_list;
3882
3883 /* Move the item from the list and then change the type to
3884 * avoid the value being freed. */
3885 *rettv = list->lv_last->li_tv;
3886 list->lv_last->li_tv.v_type = VAR_NUMBER;
3887 free_tv(listtv);
3888 }
3889 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003890 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003891}
3892
3893/*
3894 * common for "ch_evalraw()" and "ch_sendraw()"
3895 */
3896 void
3897ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3898{
3899 char_u buf[NUMBUFLEN];
3900 char_u *text;
3901 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003902 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003903 jobopt_T opt;
3904 int timeout;
3905
3906 /* return an empty string by default */
3907 rettv->v_type = VAR_STRING;
3908 rettv->vval.v_string = NULL;
3909
3910 text = get_tv_string_buf(&argvars[1], buf);
3911 channel = send_common(argvars, text, 0, eval, &opt,
3912 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3913 if (channel != NULL && eval)
3914 {
3915 if (opt.jo_set & JO_TIMEOUT)
3916 timeout = opt.jo_timeout;
3917 else
3918 timeout = channel_get_timeout(channel, part_read);
3919 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
3920 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003921 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003922}
3923
Bram Moolenaard04a0202016-01-26 23:30:18 +01003924# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003925/*
3926 * Add open channels to the poll struct.
3927 * Return the adjusted struct index.
3928 * The type of "fds" is hidden to avoid problems with the function proto.
3929 */
3930 int
3931channel_poll_setup(int nfd_in, void *fds_in)
3932{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003933 int nfd = nfd_in;
3934 channel_T *channel;
3935 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003936 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003937
Bram Moolenaar77073442016-02-13 23:23:53 +01003938 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003939 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003940 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003941 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003942 chanpart_T *ch_part = &channel->ch_part[part];
3943
3944 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003945 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003946 ch_part->ch_poll_idx = nfd;
3947 fds[nfd].fd = ch_part->ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003948 fds[nfd].events = POLLIN;
3949 nfd++;
3950 }
3951 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003952 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003953 }
3954 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003955
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003956 nfd = channel_fill_poll_write(nfd, fds);
3957
Bram Moolenaare0874f82016-01-24 20:36:41 +01003958 return nfd;
3959}
3960
3961/*
3962 * The type of "fds" is hidden to avoid problems with the function proto.
3963 */
3964 int
3965channel_poll_check(int ret_in, void *fds_in)
3966{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003967 int ret = ret_in;
3968 channel_T *channel;
3969 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003970 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003971 int idx;
3972 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003973
Bram Moolenaar77073442016-02-13 23:23:53 +01003974 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003975 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003976 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003977 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003978 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003979
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003980 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003981 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003982 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003983 --ret;
3984 }
3985 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003986
3987 in_part = &channel->ch_part[PART_IN];
3988 idx = in_part->ch_poll_idx;
3989 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
3990 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02003991 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003992 --ret;
3993 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003994 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01003995
3996 return ret;
3997}
Bram Moolenaard04a0202016-01-26 23:30:18 +01003998# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01003999
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004000# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004001/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004002 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004003 */
4004 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004005channel_select_setup(int maxfd_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004006{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004007 int maxfd = maxfd_in;
4008 channel_T *channel;
4009 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004010 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004011 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004012
Bram Moolenaar77073442016-02-13 23:23:53 +01004013 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004014 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004015 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004016 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004017 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004018
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004019 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004020 {
Bram Moolenaard8070362016-02-15 21:56:54 +01004021 FD_SET((int)fd, rfds);
4022 if (maxfd < (int)fd)
4023 maxfd = (int)fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004024 }
4025 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004026 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004027
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004028 maxfd = channel_fill_wfds(maxfd, wfds);
4029
Bram Moolenaare0874f82016-01-24 20:36:41 +01004030 return maxfd;
4031}
4032
4033/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004034 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004035 */
4036 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004037channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004038{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004039 int ret = ret_in;
4040 channel_T *channel;
4041 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004042 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004043 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004044 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004045
Bram Moolenaar77073442016-02-13 23:23:53 +01004046 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004047 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004048 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004049 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004050 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004051
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004052 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004053 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004054 channel_read(channel, part, "channel_select_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004055 --ret;
4056 }
4057 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004058
4059 in_part = &channel->ch_part[PART_IN];
4060 if (ret > 0 && in_part->ch_fd != INVALID_FD
4061 && FD_ISSET(in_part->ch_fd, wfds))
4062 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004063 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004064 --ret;
4065 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004066 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004067
4068 return ret;
4069}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004070# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004071
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004072/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004073 * Execute queued up commands.
4074 * Invoked from the main loop when it's safe to execute received commands.
4075 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004076 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004077 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004078channel_parse_messages(void)
4079{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004080 channel_T *channel = first_channel;
4081 int ret = FALSE;
4082 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004083 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004084#ifdef ELAPSED_FUNC
4085 ELAPSED_TYPE start_tv;
4086
4087 ELAPSED_INIT(start_tv);
4088#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004089
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004090 ++safe_to_invoke_callback;
4091
Bram Moolenaard0b65022016-03-06 21:50:33 +01004092 /* Only do this message when another message was given, otherwise we get
4093 * lots of them. */
4094 if (did_log_msg)
4095 {
4096 ch_log(NULL, "looking for messages on channels");
4097 did_log_msg = FALSE;
4098 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004099 while (channel != NULL)
4100 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004101 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004102 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004103 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004104 channel_close_now(channel);
4105 /* channel may have been freed, start over */
4106 channel = first_channel;
4107 continue;
4108 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004109 if (channel->ch_to_be_freed)
4110 {
4111 channel_free(channel);
4112 /* channel has been freed, start over */
4113 channel = first_channel;
4114 continue;
4115 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004116 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004117 {
4118 /* channel is no longer useful, free it */
4119 channel_free(channel);
4120 channel = first_channel;
4121 part = PART_SOCK;
4122 continue;
4123 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004124 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004125 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004126 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004127 /* Increase the refcount, in case the handler causes the channel
4128 * to be unreferenced or closed. */
4129 ++channel->ch_refcount;
4130 r = may_invoke_callback(channel, part);
4131 if (r == OK)
4132 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004133 if (channel_unref(channel) || (r == OK
4134#ifdef ELAPSED_FUNC
4135 /* Limit the time we loop here to 100 msec, otherwise
4136 * Vim becomes unresponsive when the callback takes
4137 * more than a bit of time. */
4138 && ELAPSED_FUNC(start_tv) < 100L
4139#endif
4140 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004141 {
4142 /* channel was freed or something was done, start over */
4143 channel = first_channel;
4144 part = PART_SOCK;
4145 continue;
4146 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004147 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004148 if (part < PART_ERR)
4149 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004150 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004151 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004152 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004153 part = PART_SOCK;
4154 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004155 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004156
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004157 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004158 {
4159 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004160 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004161 }
4162
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004163 --safe_to_invoke_callback;
4164
Bram Moolenaard7ece102016-02-02 23:23:02 +01004165 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004166}
4167
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004168/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004169 * Return TRUE if any channel has readahead. That means we should not block on
4170 * waiting for input.
4171 */
4172 int
4173channel_any_readahead(void)
4174{
4175 channel_T *channel = first_channel;
4176 ch_part_T part = PART_SOCK;
4177
4178 while (channel != NULL)
4179 {
4180 if (channel_has_readahead(channel, part))
4181 return TRUE;
4182 if (part < PART_ERR)
4183 ++part;
4184 else
4185 {
4186 channel = channel->ch_next;
4187 part = PART_SOCK;
4188 }
4189 }
4190 return FALSE;
4191}
4192
4193/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004194 * Mark references to lists used in channels.
4195 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004196 int
4197set_ref_in_channel(int copyID)
4198{
Bram Moolenaar77073442016-02-13 23:23:53 +01004199 int abort = FALSE;
4200 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004201 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004202
Bram Moolenaar77073442016-02-13 23:23:53 +01004203 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004204 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004205 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004206 tv.v_type = VAR_CHANNEL;
4207 tv.vval.v_channel = channel;
4208 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004209 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004210 return abort;
4211}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004212
4213/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004214 * Return the "part" to write to for "channel".
4215 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004216 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004217channel_part_send(channel_T *channel)
4218{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004219 if (channel->CH_SOCK_FD == INVALID_FD)
4220 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004221 return PART_SOCK;
4222}
4223
4224/*
4225 * Return the default "part" to read from for "channel".
4226 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004227 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004228channel_part_read(channel_T *channel)
4229{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004230 if (channel->CH_SOCK_FD == INVALID_FD)
4231 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004232 return PART_SOCK;
4233}
4234
4235/*
4236 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004237 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004238 */
4239 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004240channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004241{
Bram Moolenaar77073442016-02-13 23:23:53 +01004242 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004243 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004244 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004245}
4246
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004247/*
4248 * Return the timeout of "channel"/"part"
4249 */
4250 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004251channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004252{
4253 return channel->ch_part[part].ch_timeout;
4254}
4255
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004256 static int
4257handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4258{
4259 char_u *val = get_tv_string(item);
4260
4261 opt->jo_set |= jo;
4262 if (STRCMP(val, "nl") == 0)
4263 *modep = MODE_NL;
4264 else if (STRCMP(val, "raw") == 0)
4265 *modep = MODE_RAW;
4266 else if (STRCMP(val, "js") == 0)
4267 *modep = MODE_JS;
4268 else if (STRCMP(val, "json") == 0)
4269 *modep = MODE_JSON;
4270 else
4271 {
4272 EMSG2(_(e_invarg2), val);
4273 return FAIL;
4274 }
4275 return OK;
4276}
4277
4278 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004279handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004280{
4281 char_u *val = get_tv_string(item);
4282
4283 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4284 if (STRCMP(val, "null") == 0)
4285 opt->jo_io[part] = JIO_NULL;
4286 else if (STRCMP(val, "pipe") == 0)
4287 opt->jo_io[part] = JIO_PIPE;
4288 else if (STRCMP(val, "file") == 0)
4289 opt->jo_io[part] = JIO_FILE;
4290 else if (STRCMP(val, "buffer") == 0)
4291 opt->jo_io[part] = JIO_BUFFER;
4292 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4293 opt->jo_io[part] = JIO_OUT;
4294 else
4295 {
4296 EMSG2(_(e_invarg2), val);
4297 return FAIL;
4298 }
4299 return OK;
4300}
4301
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004302/*
4303 * Clear a jobopt_T before using it.
4304 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004305 void
4306clear_job_options(jobopt_T *opt)
4307{
4308 vim_memset(opt, 0, sizeof(jobopt_T));
4309}
4310
4311/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004312 * Free any members of a jobopt_T.
4313 */
4314 void
4315free_job_options(jobopt_T *opt)
4316{
4317 if (opt->jo_partial != NULL)
4318 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004319 else if (opt->jo_callback != NULL)
4320 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004321 if (opt->jo_out_partial != NULL)
4322 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004323 else if (opt->jo_out_cb != NULL)
4324 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004325 if (opt->jo_err_partial != NULL)
4326 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004327 else if (opt->jo_err_cb != NULL)
4328 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004329 if (opt->jo_close_partial != NULL)
4330 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004331 else if (opt->jo_close_cb != NULL)
4332 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004333 if (opt->jo_exit_partial != NULL)
4334 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004335 else if (opt->jo_exit_cb != NULL)
4336 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004337 if (opt->jo_env != NULL)
4338 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004339}
4340
4341/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004342 * Get the PART_ number from the first character of an option name.
4343 */
4344 static int
4345part_from_char(int c)
4346{
4347 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4348}
4349
4350/*
4351 * Get the option entries from the dict in "tv", parse them and put the result
4352 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004353 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004354 * If an option value is invalid return FAIL.
4355 */
4356 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004357get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358{
4359 typval_T *item;
4360 char_u *val;
4361 dict_T *dict;
4362 int todo;
4363 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004364 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004365
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004366 if (tv->v_type == VAR_UNKNOWN)
4367 return OK;
4368 if (tv->v_type != VAR_DICT)
4369 {
4370 EMSG(_(e_invarg));
4371 return FAIL;
4372 }
4373 dict = tv->vval.v_dict;
4374 if (dict == NULL)
4375 return OK;
4376
4377 todo = (int)dict->dv_hashtab.ht_used;
4378 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4379 if (!HASHITEM_EMPTY(hi))
4380 {
4381 item = &dict_lookup(hi)->di_tv;
4382
4383 if (STRCMP(hi->hi_key, "mode") == 0)
4384 {
4385 if (!(supported & JO_MODE))
4386 break;
4387 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4388 return FAIL;
4389 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004390 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004391 {
4392 if (!(supported & JO_IN_MODE))
4393 break;
4394 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4395 == FAIL)
4396 return FAIL;
4397 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004398 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004399 {
4400 if (!(supported & JO_OUT_MODE))
4401 break;
4402 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4403 == FAIL)
4404 return FAIL;
4405 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004406 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004407 {
4408 if (!(supported & JO_ERR_MODE))
4409 break;
4410 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4411 == FAIL)
4412 return FAIL;
4413 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004414 else if (STRCMP(hi->hi_key, "in_io") == 0
4415 || STRCMP(hi->hi_key, "out_io") == 0
4416 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004417 {
4418 if (!(supported & JO_OUT_IO))
4419 break;
4420 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4421 return FAIL;
4422 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004423 else if (STRCMP(hi->hi_key, "in_name") == 0
4424 || STRCMP(hi->hi_key, "out_name") == 0
4425 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004426 {
4427 part = part_from_char(*hi->hi_key);
4428
4429 if (!(supported & JO_OUT_IO))
4430 break;
4431 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4432 opt->jo_io_name[part] =
4433 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4434 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004435 else if (STRCMP(hi->hi_key, "pty") == 0)
4436 {
4437 if (!(supported & JO_MODE))
4438 break;
4439 opt->jo_pty = get_tv_number(item);
4440 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004441 else if (STRCMP(hi->hi_key, "in_buf") == 0
4442 || STRCMP(hi->hi_key, "out_buf") == 0
4443 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004444 {
4445 part = part_from_char(*hi->hi_key);
4446
4447 if (!(supported & JO_OUT_IO))
4448 break;
4449 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4450 opt->jo_io_buf[part] = get_tv_number(item);
4451 if (opt->jo_io_buf[part] <= 0)
4452 {
4453 EMSG2(_(e_invarg2), get_tv_string(item));
4454 return FAIL;
4455 }
4456 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4457 {
4458 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4459 return FAIL;
4460 }
4461 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004462 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4463 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4464 {
4465 part = part_from_char(*hi->hi_key);
4466
4467 if (!(supported & JO_OUT_IO))
4468 break;
4469 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4470 opt->jo_modifiable[part] = get_tv_number(item);
4471 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004472 else if (STRCMP(hi->hi_key, "out_msg") == 0
4473 || STRCMP(hi->hi_key, "err_msg") == 0)
4474 {
4475 part = part_from_char(*hi->hi_key);
4476
4477 if (!(supported & JO_OUT_IO))
4478 break;
4479 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4480 opt->jo_message[part] = get_tv_number(item);
4481 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004482 else if (STRCMP(hi->hi_key, "in_top") == 0
4483 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004484 {
4485 linenr_T *lp;
4486
4487 if (!(supported & JO_OUT_IO))
4488 break;
4489 if (hi->hi_key[3] == 't')
4490 {
4491 lp = &opt->jo_in_top;
4492 opt->jo_set |= JO_IN_TOP;
4493 }
4494 else
4495 {
4496 lp = &opt->jo_in_bot;
4497 opt->jo_set |= JO_IN_BOT;
4498 }
4499 *lp = get_tv_number(item);
4500 if (*lp < 0)
4501 {
4502 EMSG2(_(e_invarg2), get_tv_string(item));
4503 return FAIL;
4504 }
4505 }
4506 else if (STRCMP(hi->hi_key, "channel") == 0)
4507 {
4508 if (!(supported & JO_OUT_IO))
4509 break;
4510 opt->jo_set |= JO_CHANNEL;
4511 if (item->v_type != VAR_CHANNEL)
4512 {
4513 EMSG2(_(e_invarg2), "channel");
4514 return FAIL;
4515 }
4516 opt->jo_channel = item->vval.v_channel;
4517 }
4518 else if (STRCMP(hi->hi_key, "callback") == 0)
4519 {
4520 if (!(supported & JO_CALLBACK))
4521 break;
4522 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004523 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004524 if (opt->jo_callback == NULL)
4525 {
4526 EMSG2(_(e_invarg2), "callback");
4527 return FAIL;
4528 }
4529 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004530 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004531 {
4532 if (!(supported & JO_OUT_CALLBACK))
4533 break;
4534 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004535 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004536 if (opt->jo_out_cb == NULL)
4537 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004538 EMSG2(_(e_invarg2), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004539 return FAIL;
4540 }
4541 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004542 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004543 {
4544 if (!(supported & JO_ERR_CALLBACK))
4545 break;
4546 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004547 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004548 if (opt->jo_err_cb == NULL)
4549 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004550 EMSG2(_(e_invarg2), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004551 return FAIL;
4552 }
4553 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004554 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004555 {
4556 if (!(supported & JO_CLOSE_CALLBACK))
4557 break;
4558 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004559 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004560 if (opt->jo_close_cb == NULL)
4561 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004562 EMSG2(_(e_invarg2), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004563 return FAIL;
4564 }
4565 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004566 else if (STRCMP(hi->hi_key, "drop") == 0)
4567 {
4568 int never = FALSE;
4569 val = get_tv_string(item);
4570
4571 if (STRCMP(val, "never") == 0)
4572 never = TRUE;
4573 else if (STRCMP(val, "auto") != 0)
4574 {
4575 EMSG2(_(e_invarg2), "drop");
4576 return FAIL;
4577 }
4578 opt->jo_drop_never = never;
4579 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004580 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4581 {
4582 if (!(supported & JO_EXIT_CB))
4583 break;
4584 opt->jo_set |= JO_EXIT_CB;
4585 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4586 if (opt->jo_exit_cb == NULL)
4587 {
4588 EMSG2(_(e_invarg2), "exit_cb");
4589 return FAIL;
4590 }
4591 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004592#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004593 else if (STRCMP(hi->hi_key, "term_name") == 0)
4594 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004595 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004596 break;
4597 opt->jo_set2 |= JO2_TERM_NAME;
4598 opt->jo_term_name = get_tv_string_chk(item);
4599 if (opt->jo_term_name == NULL)
4600 {
4601 EMSG2(_(e_invarg2), "term_name");
4602 return FAIL;
4603 }
4604 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004605 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4606 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004607 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004608 break;
4609 val = get_tv_string(item);
4610 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4611 {
Bram Moolenaarf1237f12017-08-11 15:45:28 +02004612 EMSG2(_(e_invarg2), val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004613 return FAIL;
4614 }
4615 opt->jo_set2 |= JO2_TERM_FINISH;
4616 opt->jo_term_finish = *val;
4617 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004618 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4619 {
4620 char_u *p;
4621
4622 if (!(supported2 & JO2_TERM_OPENCMD))
4623 break;
4624 opt->jo_set2 |= JO2_TERM_OPENCMD;
4625 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4626 if (p != NULL)
4627 {
4628 /* Must have %d and no other %. */
4629 p = vim_strchr(p, '%');
4630 if (p != NULL && (p[1] != 'd'
4631 || vim_strchr(p + 2, '%') != NULL))
4632 p = NULL;
4633 }
4634 if (p == NULL)
4635 {
4636 EMSG2(_(e_invarg2), "term_opencmd");
4637 return FAIL;
4638 }
4639 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004640 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4641 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004642 char_u *p;
4643
4644 if (!(supported2 & JO2_EOF_CHARS))
4645 break;
4646 opt->jo_set2 |= JO2_EOF_CHARS;
4647 p = opt->jo_eof_chars = get_tv_string_chk(item);
4648 if (p == NULL)
4649 {
4650 EMSG2(_(e_invarg2), "term_opencmd");
4651 return FAIL;
4652 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004653 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004654 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4655 {
4656 if (!(supported2 & JO2_TERM_ROWS))
4657 break;
4658 opt->jo_set |= JO2_TERM_ROWS;
4659 opt->jo_term_rows = get_tv_number(item);
4660 }
4661 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4662 {
4663 if (!(supported2 & JO2_TERM_COLS))
4664 break;
4665 opt->jo_set |= JO2_TERM_COLS;
4666 opt->jo_term_cols = get_tv_number(item);
4667 }
4668 else if (STRCMP(hi->hi_key, "vertical") == 0)
4669 {
4670 if (!(supported2 & JO2_VERTICAL))
4671 break;
4672 opt->jo_set |= JO2_VERTICAL;
4673 opt->jo_vertical = get_tv_number(item);
4674 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004675 else if (STRCMP(hi->hi_key, "curwin") == 0)
4676 {
4677 if (!(supported2 & JO2_CURWIN))
4678 break;
4679 opt->jo_set |= JO2_CURWIN;
4680 opt->jo_curwin = get_tv_number(item);
4681 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004682 else if (STRCMP(hi->hi_key, "hidden") == 0)
4683 {
4684 if (!(supported2 & JO2_HIDDEN))
4685 break;
4686 opt->jo_set |= JO2_HIDDEN;
4687 opt->jo_hidden = get_tv_number(item);
4688 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004689#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004690 else if (STRCMP(hi->hi_key, "env") == 0)
4691 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004692 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004693 break;
4694 opt->jo_set |= JO2_ENV;
4695 opt->jo_env = item->vval.v_dict;
4696 ++item->vval.v_dict->dv_refcount;
4697 }
4698 else if (STRCMP(hi->hi_key, "cwd") == 0)
4699 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004700 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004701 break;
4702 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4703 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4704 {
4705 EMSG2(_(e_invarg2), "cwd");
4706 return FAIL;
4707 }
4708 opt->jo_set |= JO2_CWD;
4709 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004710 else if (STRCMP(hi->hi_key, "waittime") == 0)
4711 {
4712 if (!(supported & JO_WAITTIME))
4713 break;
4714 opt->jo_set |= JO_WAITTIME;
4715 opt->jo_waittime = get_tv_number(item);
4716 }
4717 else if (STRCMP(hi->hi_key, "timeout") == 0)
4718 {
4719 if (!(supported & JO_TIMEOUT))
4720 break;
4721 opt->jo_set |= JO_TIMEOUT;
4722 opt->jo_timeout = get_tv_number(item);
4723 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004724 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004725 {
4726 if (!(supported & JO_OUT_TIMEOUT))
4727 break;
4728 opt->jo_set |= JO_OUT_TIMEOUT;
4729 opt->jo_out_timeout = get_tv_number(item);
4730 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004731 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004732 {
4733 if (!(supported & JO_ERR_TIMEOUT))
4734 break;
4735 opt->jo_set |= JO_ERR_TIMEOUT;
4736 opt->jo_err_timeout = get_tv_number(item);
4737 }
4738 else if (STRCMP(hi->hi_key, "part") == 0)
4739 {
4740 if (!(supported & JO_PART))
4741 break;
4742 opt->jo_set |= JO_PART;
4743 val = get_tv_string(item);
4744 if (STRCMP(val, "err") == 0)
4745 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004746 else if (STRCMP(val, "out") == 0)
4747 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004748 else
4749 {
4750 EMSG2(_(e_invarg2), val);
4751 return FAIL;
4752 }
4753 }
4754 else if (STRCMP(hi->hi_key, "id") == 0)
4755 {
4756 if (!(supported & JO_ID))
4757 break;
4758 opt->jo_set |= JO_ID;
4759 opt->jo_id = get_tv_number(item);
4760 }
4761 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4762 {
4763 if (!(supported & JO_STOPONEXIT))
4764 break;
4765 opt->jo_set |= JO_STOPONEXIT;
4766 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4767 opt->jo_soe_buf);
4768 if (opt->jo_stoponexit == NULL)
4769 {
4770 EMSG2(_(e_invarg2), "stoponexit");
4771 return FAIL;
4772 }
4773 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004774 else if (STRCMP(hi->hi_key, "block_write") == 0)
4775 {
4776 if (!(supported & JO_BLOCK_WRITE))
4777 break;
4778 opt->jo_set |= JO_BLOCK_WRITE;
4779 opt->jo_block_write = get_tv_number(item);
4780 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004781 else
4782 break;
4783 --todo;
4784 }
4785 if (todo > 0)
4786 {
4787 EMSG2(_(e_invarg2), hi->hi_key);
4788 return FAIL;
4789 }
4790
4791 return OK;
4792}
4793
4794/*
4795 * Get the channel from the argument.
4796 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004797 * When "check_open" is TRUE check that the channel can be used.
4798 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004799 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004800 */
4801 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004802get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004803{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004804 channel_T *channel = NULL;
4805 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004806
4807 if (tv->v_type == VAR_JOB)
4808 {
4809 if (tv->vval.v_job != NULL)
4810 channel = tv->vval.v_job->jv_channel;
4811 }
4812 else if (tv->v_type == VAR_CHANNEL)
4813 {
4814 channel = tv->vval.v_channel;
4815 }
4816 else
4817 {
4818 EMSG2(_(e_invarg2), get_tv_string(tv));
4819 return NULL;
4820 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004821 if (channel != NULL && reading)
4822 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004823 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004824
Bram Moolenaar437905c2016-04-26 19:01:05 +02004825 if (check_open && (channel == NULL || (!channel_is_open(channel)
4826 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004827 {
4828 EMSG(_("E906: not an open channel"));
4829 return NULL;
4830 }
4831 return channel;
4832}
4833
4834static job_T *first_job = NULL;
4835
4836 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004837job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004838{
4839 ch_log(job->jv_channel, "Freeing job");
4840 if (job->jv_channel != NULL)
4841 {
4842 /* The link from the channel to the job doesn't count as a reference,
4843 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004844 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004845 * NULL the reference. We don't set ch_job_killed, unreferencing the
4846 * job doesn't mean it stops running. */
4847 job->jv_channel->ch_job = NULL;
4848 channel_unref(job->jv_channel);
4849 }
4850 mch_clear_job(job);
4851
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02004852 vim_free(job->jv_tty_name);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004853 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004854 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004855}
4856
4857 static void
4858job_free_job(job_T *job)
4859{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004860 if (job->jv_next != NULL)
4861 job->jv_next->jv_prev = job->jv_prev;
4862 if (job->jv_prev == NULL)
4863 first_job = job->jv_next;
4864 else
4865 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004866 vim_free(job);
4867}
4868
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004869 static void
4870job_free(job_T *job)
4871{
4872 if (!in_free_unref_items)
4873 {
4874 job_free_contents(job);
4875 job_free_job(job);
4876 }
4877}
4878
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004879#if defined(EXITFREE) || defined(PROTO)
4880 void
4881job_free_all(void)
4882{
4883 while (first_job != NULL)
4884 job_free(first_job);
4885}
4886#endif
4887
4888/*
4889 * Return TRUE if we need to check if the process of "job" has ended.
4890 */
4891 static int
4892job_need_end_check(job_T *job)
4893{
4894 return job->jv_status == JOB_STARTED
4895 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
4896}
4897
4898/*
4899 * Return TRUE if the channel of "job" is still useful.
4900 */
4901 static int
4902job_channel_still_useful(job_T *job)
4903{
4904 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
4905}
4906
4907/*
4908 * Return TRUE if the job should not be freed yet. Do not free the job when
4909 * it has not ended yet and there is a "stoponexit" flag, an exit callback
4910 * or when the associated channel will do something with the job output.
4911 */
4912 static int
4913job_still_useful(job_T *job)
4914{
4915 return job_need_end_check(job) || job_channel_still_useful(job);
4916}
4917
Bram Moolenaardcaa6132017-08-13 17:13:09 +02004918#if !defined(USE_ARGV) || defined(PROTO)
4919/*
4920 * Escape one argument for an external command.
4921 * Returns the escaped string in allocated memory. NULL when out of memory.
4922 */
4923 static char_u *
4924win32_escape_arg(char_u *arg)
4925{
4926 int slen, dlen;
4927 int escaping = 0;
4928 int i;
4929 char_u *s, *d;
4930 char_u *escaped_arg;
4931 int has_spaces = FALSE;
4932
4933 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02004934 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02004935 dlen = slen;
4936 for (s = arg; *s != NUL; MB_PTR_ADV(s))
4937 {
4938 if (*s == '"' || *s == '\\')
4939 ++dlen;
4940 if (*s == ' ' || *s == '\t')
4941 has_spaces = TRUE;
4942 }
4943
4944 if (has_spaces)
4945 dlen += 2;
4946
4947 if (dlen == slen)
4948 return vim_strsave(arg);
4949
4950 /* Allocate memory for the result and fill it. */
4951 escaped_arg = alloc(dlen + 1);
4952 if (escaped_arg == NULL)
4953 return NULL;
4954 memset(escaped_arg, 0, dlen+1);
4955
4956 d = escaped_arg;
4957
4958 if (has_spaces)
4959 *d++ = '"';
4960
4961 for (s = arg; *s != NUL;)
4962 {
4963 switch (*s)
4964 {
4965 case '"':
4966 for (i = 0; i < escaping; i++)
4967 *d++ = '\\';
4968 escaping = 0;
4969 *d++ = '\\';
4970 *d++ = *s++;
4971 break;
4972 case '\\':
4973 escaping++;
4974 *d++ = *s++;
4975 break;
4976 default:
4977 escaping = 0;
4978 MB_COPY_CHAR(s, d);
4979 break;
4980 }
4981 }
4982
4983 /* add terminating quote and finish with a NUL */
4984 if (has_spaces)
4985 {
4986 for (i = 0; i < escaping; i++)
4987 *d++ = '\\';
4988 *d++ = '"';
4989 }
4990 *d = NUL;
4991
4992 return escaped_arg;
4993}
4994
4995/*
4996 * Build a command line from a list, taking care of escaping.
4997 * The result is put in gap->ga_data.
4998 * Returns FAIL when out of memory.
4999 */
5000 int
5001win32_build_cmd(list_T *l, garray_T *gap)
5002{
5003 listitem_T *li;
5004 char_u *s;
5005
5006 for (li = l->lv_first; li != NULL; li = li->li_next)
5007 {
5008 s = get_tv_string_chk(&li->li_tv);
5009 if (s == NULL)
5010 return FAIL;
5011 s = win32_escape_arg(s);
5012 if (s == NULL)
5013 return FAIL;
5014 ga_concat(gap, s);
5015 vim_free(s);
5016 if (li->li_next != NULL)
5017 ga_append(gap, ' ');
5018 }
5019 return OK;
5020}
5021#endif
5022
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005023/*
5024 * NOTE: Must call job_cleanup() only once right after the status of "job"
5025 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5026 * mch_detect_ended_job() returned non-NULL).
5027 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005028 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005029job_cleanup(job_T *job)
5030{
5031 if (job->jv_status != JOB_ENDED)
5032 return;
5033
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005034 /* Ready to cleanup the job. */
5035 job->jv_status = JOB_FINISHED;
5036
Bram Moolenaar97792de2016-10-15 18:36:49 +02005037 if (job->jv_exit_cb != NULL)
5038 {
5039 typval_T argv[3];
5040 typval_T rettv;
5041 int dummy;
5042
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005043 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005044 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005045 ++job->jv_refcount;
5046 argv[0].v_type = VAR_JOB;
5047 argv[0].vval.v_job = job;
5048 argv[1].v_type = VAR_NUMBER;
5049 argv[1].vval.v_number = job->jv_exitval;
5050 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5051 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5052 job->jv_exit_partial, NULL);
5053 clear_tv(&rettv);
5054 --job->jv_refcount;
5055 channel_need_redraw = TRUE;
5056 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005057
5058 /* Do not free the job in case the close callback of the associated channel
5059 * isn't invoked yet and may get information by job_info(). */
5060 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005061 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005062 /* The job was already unreferenced and the associated channel was
5063 * detached, now that it ended it can be freed. Careful: caller must
5064 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005065 job_free(job);
5066 }
5067}
5068
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005069/*
5070 * Mark references in jobs that are still useful.
5071 */
5072 int
5073set_ref_in_job(int copyID)
5074{
5075 int abort = FALSE;
5076 job_T *job;
5077 typval_T tv;
5078
5079 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005080 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005081 {
5082 tv.v_type = VAR_JOB;
5083 tv.vval.v_job = job;
5084 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5085 }
5086 return abort;
5087}
5088
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005089/*
5090 * Dereference "job". Note that after this "job" may have been freed.
5091 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005092 void
5093job_unref(job_T *job)
5094{
5095 if (job != NULL && --job->jv_refcount <= 0)
5096 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005097 /* Do not free the job if there is a channel where the close callback
5098 * may get the job info. */
5099 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005100 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005101 /* Do not free the job when it has not ended yet and there is a
5102 * "stoponexit" flag or an exit callback. */
5103 if (!job_need_end_check(job))
5104 {
5105 job_free(job);
5106 }
5107 else if (job->jv_channel != NULL)
5108 {
5109 /* Do remove the link to the channel, otherwise it hangs
5110 * around until Vim exits. See job_free() for refcount. */
5111 ch_log(job->jv_channel, "detaching channel from job");
5112 job->jv_channel->ch_job = NULL;
5113 channel_unref(job->jv_channel);
5114 job->jv_channel = NULL;
5115 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005116 }
5117 }
5118}
5119
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005120 int
5121free_unused_jobs_contents(int copyID, int mask)
5122{
5123 int did_free = FALSE;
5124 job_T *job;
5125
5126 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005127 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005128 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005129 {
5130 /* Free the channel and ordinary items it contains, but don't
5131 * recurse into Lists, Dictionaries etc. */
5132 job_free_contents(job);
5133 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005134 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005135 return did_free;
5136}
5137
5138 void
5139free_unused_jobs(int copyID, int mask)
5140{
5141 job_T *job;
5142 job_T *job_next;
5143
5144 for (job = first_job; job != NULL; job = job_next)
5145 {
5146 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005147 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005148 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005149 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005150 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005151 job_free_job(job);
5152 }
5153 }
5154}
5155
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005156/*
5157 * Allocate a job. Sets the refcount to one and sets options default.
5158 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005159 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005160job_alloc(void)
5161{
5162 job_T *job;
5163
5164 job = (job_T *)alloc_clear(sizeof(job_T));
5165 if (job != NULL)
5166 {
5167 job->jv_refcount = 1;
5168 job->jv_stoponexit = vim_strsave((char_u *)"term");
5169
5170 if (first_job != NULL)
5171 {
5172 first_job->jv_prev = job;
5173 job->jv_next = first_job;
5174 }
5175 first_job = job;
5176 }
5177 return job;
5178}
5179
5180 void
5181job_set_options(job_T *job, jobopt_T *opt)
5182{
5183 if (opt->jo_set & JO_STOPONEXIT)
5184 {
5185 vim_free(job->jv_stoponexit);
5186 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5187 job->jv_stoponexit = NULL;
5188 else
5189 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5190 }
5191 if (opt->jo_set & JO_EXIT_CB)
5192 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005193 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005194 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005195 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005196 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005197 job->jv_exit_partial = NULL;
5198 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005199 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005200 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005201 job->jv_exit_partial = opt->jo_exit_partial;
5202 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005203 {
5204 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005205 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005206 }
5207 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005208 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005209 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005210 func_ref(job->jv_exit_cb);
5211 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005212 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 }
5214}
5215
5216/*
5217 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5218 */
5219 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005220job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005221{
5222 job_T *job;
5223
5224 for (job = first_job; job != NULL; job = job->jv_next)
5225 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005226 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005227}
5228
5229/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005230 * Return TRUE when there is any job that has an exit callback and might exit,
5231 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005232 */
5233 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005234has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005235{
5236 job_T *job;
5237
5238 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005239 /* Only should check if the channel has been closed, if the channel is
5240 * open the job won't exit. */
5241 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005242 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005243 return TRUE;
5244 return FALSE;
5245}
5246
Bram Moolenaar97792de2016-10-15 18:36:49 +02005247#define MAX_CHECK_ENDED 8
5248
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005249/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005250 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005251 */
5252 void
5253job_check_ended(void)
5254{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005255 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005256
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005257 if (first_job == NULL)
5258 return;
5259
Bram Moolenaar97792de2016-10-15 18:36:49 +02005260 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005261 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005262 /* NOTE: mch_detect_ended_job() must only return a job of which the
5263 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005264 job_T *job = mch_detect_ended_job(first_job);
5265
5266 if (job == NULL)
5267 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005268 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005269 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005270
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005271 if (channel_need_redraw)
5272 {
5273 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005274 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005275 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005276}
5277
5278/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005279 * Create a job and return it. Implements job_start().
5280 * The returned job has a refcount of one.
5281 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005282 */
5283 job_T *
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005284job_start(typval_T *argvars, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005285{
5286 job_T *job;
5287 char_u *cmd = NULL;
5288#if defined(UNIX)
5289# define USE_ARGV
5290 char **argv = NULL;
5291 int argc = 0;
5292#else
5293 garray_T ga;
5294#endif
5295 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005296 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005297
5298 job = job_alloc();
5299 if (job == NULL)
5300 return NULL;
5301
5302 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005303#ifndef USE_ARGV
5304 ga_init2(&ga, (int)sizeof(char*), 20);
5305#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005306
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005307 if (opt_arg != NULL)
5308 opt = *opt_arg;
5309 else
5310 {
5311 /* Default mode is NL. */
5312 clear_job_options(&opt);
5313 opt.jo_mode = MODE_NL;
5314 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005315 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5316 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5317 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005318 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005319 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005320
5321 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005322 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005323 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5324 && opt.jo_io[part] == JIO_FILE
5325 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5326 || *opt.jo_io_name[part] == NUL))
5327 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005328 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005329 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005330 }
5331
5332 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5333 {
5334 buf_T *buf = NULL;
5335
5336 /* check that we can find the buffer before starting the job */
5337 if (opt.jo_set & JO_IN_BUF)
5338 {
5339 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5340 if (buf == NULL)
5341 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5342 }
5343 else if (!(opt.jo_set & JO_IN_NAME))
5344 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005345 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005346 }
5347 else
5348 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5349 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005350 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005351 if (buf->b_ml.ml_mfp == NULL)
5352 {
5353 char_u numbuf[NUMBUFLEN];
5354 char_u *s;
5355
5356 if (opt.jo_set & JO_IN_BUF)
5357 {
5358 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5359 s = numbuf;
5360 }
5361 else
5362 s = opt.jo_io_name[PART_IN];
5363 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005364 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005365 }
5366 job->jv_in_buf = buf;
5367 }
5368
5369 job_set_options(job, &opt);
5370
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005371 if (argvars[0].v_type == VAR_STRING)
5372 {
5373 /* Command is a string. */
5374 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005375 if (cmd == NULL || *cmd == NUL)
5376 {
5377 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005378 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005379 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005380#ifdef USE_ARGV
5381 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005382 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005383 argv[argc] = NULL;
5384#endif
5385 }
5386 else if (argvars[0].v_type != VAR_LIST
5387 || argvars[0].vval.v_list == NULL
5388 || argvars[0].vval.v_list->lv_len < 1)
5389 {
5390 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005391 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005392 }
5393 else
5394 {
5395 list_T *l = argvars[0].vval.v_list;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005396#ifdef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005397 listitem_T *li;
5398 char_u *s;
5399
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005400 /* Pass argv[] to mch_call_shell(). */
5401 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5402 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005403 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005404 for (li = l->lv_first; li != NULL; li = li->li_next)
5405 {
5406 s = get_tv_string_chk(&li->li_tv);
5407 if (s == NULL)
5408 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005409 argv[argc++] = (char *)s;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005410 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005411 argv[argc] = NULL;
5412#else
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005413 if (win32_build_cmd(l, &ga) == FAIL)
5414 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005415 cmd = ga.ga_data;
5416#endif
5417 }
5418
5419#ifdef USE_ARGV
5420 if (ch_log_active())
5421 {
5422 garray_T ga;
5423 int i;
5424
5425 ga_init2(&ga, (int)sizeof(char), 200);
5426 for (i = 0; i < argc; ++i)
5427 {
5428 if (i > 0)
5429 ga_concat(&ga, (char_u *)" ");
5430 ga_concat(&ga, (char_u *)argv[i]);
5431 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005432 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005433 ga_clear(&ga);
5434 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005435 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005436#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005437 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005438 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005439#endif
5440
5441 /* If the channel is reading from a buffer, write lines now. */
5442 if (job->jv_channel != NULL)
5443 channel_write_in(job->jv_channel);
5444
5445theend:
5446#ifdef USE_ARGV
5447 vim_free(argv);
5448#else
5449 vim_free(ga.ga_data);
5450#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005451 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005452 return job;
5453}
5454
5455/*
5456 * Get the status of "job" and invoke the exit callback when needed.
5457 * The returned string is not allocated.
5458 */
5459 char *
5460job_status(job_T *job)
5461{
5462 char *result;
5463
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005464 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005465 /* No need to check, dead is dead. */
5466 result = "dead";
5467 else if (job->jv_status == JOB_FAILED)
5468 result = "fail";
5469 else
5470 {
5471 result = mch_job_status(job);
5472 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005473 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005474 }
5475 return result;
5476}
5477
Bram Moolenaar8950a562016-03-12 15:22:55 +01005478/*
5479 * Implementation of job_info().
5480 */
5481 void
5482job_info(job_T *job, dict_T *dict)
5483{
5484 dictitem_T *item;
5485 varnumber_T nr;
5486
5487 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5488
5489 item = dictitem_alloc((char_u *)"channel");
5490 if (item == NULL)
5491 return;
5492 item->di_tv.v_lock = 0;
5493 item->di_tv.v_type = VAR_CHANNEL;
5494 item->di_tv.vval.v_channel = job->jv_channel;
5495 if (job->jv_channel != NULL)
5496 ++job->jv_channel->ch_refcount;
5497 if (dict_add(dict, item) == FAIL)
5498 dictitem_free(item);
5499
5500#ifdef UNIX
5501 nr = job->jv_pid;
5502#else
5503 nr = job->jv_proc_info.dwProcessId;
5504#endif
5505 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02005506 dict_add_nr_str(dict, "tty", 0L,
5507 job->jv_tty_name != NULL ? job->jv_tty_name : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005508
5509 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005510 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005511 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5512}
5513
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005514/*
5515 * Send a signal to "job". Implements job_stop().
5516 * When "type" is not NULL use this for the type.
5517 * Otherwise use argvars[1] for the type.
5518 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005519 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005520job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005521{
5522 char_u *arg;
5523
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005524 if (type != NULL)
5525 arg = (char_u *)type;
5526 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005527 arg = (char_u *)"";
5528 else
5529 {
5530 arg = get_tv_string_chk(&argvars[1]);
5531 if (arg == NULL)
5532 {
5533 EMSG(_(e_invarg));
5534 return 0;
5535 }
5536 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005537 if (job->jv_status == JOB_FAILED)
5538 {
5539 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5540 return 0;
5541 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005542 if (job->jv_status == JOB_ENDED)
5543 {
5544 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5545 return 0;
5546 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005547 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005548 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005549 return 0;
5550
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005551 /* Assume that only "kill" will kill the job. */
5552 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005553 job->jv_channel->ch_job_killed = TRUE;
5554
5555 /* We don't try freeing the job, obviously the caller still has a
5556 * reference to it. */
5557 return 1;
5558}
5559
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005560#endif /* FEAT_JOB_CHANNEL */