blob: 88c53b7e0d4d2d483450a02430f4458a284ef5b6 [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 Moolenaar71eeb742017-09-13 22:18:01 +0200141ch_log_lead(const 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 Moolenaar4ac2e8d2018-04-08 12:38:26 +0200161#ifndef PROTO /* prototype is in vim.h */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100162 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200163ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100164{
165 if (log_fd != NULL)
166 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200167 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100168
Bram Moolenaar77073442016-02-13 23:23:53 +0100169 ch_log_lead("", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200170 va_start(ap, fmt);
171 vfprintf(log_fd, fmt, ap);
172 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100173 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100174 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100175 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100176 }
177}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200178#endif
179
180 static void
181ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200182#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
183 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200184#endif
185 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100186
187 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200188ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100189{
190 if (log_fd != NULL)
191 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200192 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100193
Bram Moolenaar77073442016-02-13 23:23:53 +0100194 ch_log_lead("ERR ", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200195 va_start(ap, fmt);
196 vfprintf(log_fd, fmt, ap);
197 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100198 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100199 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100200 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201 }
202}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100203
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100204#ifdef _WIN32
205# undef PERROR
206# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
207 (char_u *)msg, (char_u *)strerror_win32(errno))
208
209 static char *
210strerror_win32(int eno)
211{
212 static LPVOID msgbuf = NULL;
213 char_u *ptr;
214
215 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200216 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100217 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200218 msgbuf = NULL;
219 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100220 FormatMessage(
221 FORMAT_MESSAGE_ALLOCATE_BUFFER |
222 FORMAT_MESSAGE_FROM_SYSTEM |
223 FORMAT_MESSAGE_IGNORE_INSERTS,
224 NULL,
225 eno,
226 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
227 (LPTSTR) &msgbuf,
228 0,
229 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200230 if (msgbuf != NULL)
231 /* chomp \r or \n */
232 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
233 switch (*ptr)
234 {
235 case '\r':
236 STRMOVE(ptr, ptr + 1);
237 ptr--;
238 break;
239 case '\n':
240 if (*(ptr + 1) == '\0')
241 *ptr = '\0';
242 else
243 *ptr = ' ';
244 break;
245 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100246 return msgbuf;
247}
248#endif
249
Bram Moolenaar77073442016-02-13 23:23:53 +0100250/*
251 * The list of all allocated channels.
252 */
253static channel_T *first_channel = NULL;
254static int next_ch_id = 0;
255
256/*
257 * Allocate a new channel. The refcount is set to 1.
258 * The channel isn't actually used until it is opened.
259 * Returns NULL if out of memory.
260 */
261 channel_T *
262add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100263{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200264 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100265 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100266
Bram Moolenaar77073442016-02-13 23:23:53 +0100267 if (channel == NULL)
268 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100269
Bram Moolenaar77073442016-02-13 23:23:53 +0100270 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100271 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100272
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200273 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100274 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100275 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100276#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100277 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100278#endif
279#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100280 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100281#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100282 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100283 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100284
Bram Moolenaar77073442016-02-13 23:23:53 +0100285 if (first_channel != NULL)
286 {
287 first_channel->ch_prev = channel;
288 channel->ch_next = first_channel;
289 }
290 first_channel = channel;
291
292 channel->ch_refcount = 1;
293 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100294}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100295
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200296 int
297has_any_channel(void)
298{
299 return first_channel != NULL;
300}
301
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100302/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100303 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100304 * Return TRUE if "channel" has a callback and the associated job wasn't
305 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100306 */
307 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100308channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100309{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100310 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100311 int has_out_msg;
312 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100313
314 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100315 if (channel->ch_job_killed && channel->ch_job == NULL)
316 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100317
318 /* If there is a close callback it may still need to be invoked. */
319 if (channel->ch_close_cb != NULL)
320 return TRUE;
321
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200322 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200323 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200324 return TRUE;
325
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100326 /* If there is no callback then nobody can get readahead. If the fd is
327 * closed and there is no readahead then the callback won't be called. */
328 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100329 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
330 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100331 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
332 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
333 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
334 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
335 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
336 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100337 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100338 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200339 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200340 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
341 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200342 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200343 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
344 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100345}
346
347/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200348 * Close a channel and free all its resources.
349 */
350 static void
351channel_free_contents(channel_T *channel)
352{
353 channel_close(channel, TRUE);
354 channel_clear(channel);
355 ch_log(channel, "Freeing channel");
356}
357
358 static void
359channel_free_channel(channel_T *channel)
360{
361 if (channel->ch_next != NULL)
362 channel->ch_next->ch_prev = channel->ch_prev;
363 if (channel->ch_prev == NULL)
364 first_channel = channel->ch_next;
365 else
366 channel->ch_prev->ch_next = channel->ch_next;
367 vim_free(channel);
368}
369
370 static void
371channel_free(channel_T *channel)
372{
373 if (!in_free_unref_items)
374 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200375 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200376 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200377 else
378 {
379 channel_free_contents(channel);
380 channel_free_channel(channel);
381 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200382 }
383}
384
385/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100386 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100387 * possible, there is no callback to be invoked or the associated job was
388 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100389 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100390 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100391 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100392channel_may_free(channel_T *channel)
393{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100394 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100395 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100396 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100397 return TRUE;
398 }
399 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100400}
401
402/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100403 * Decrement the reference count on "channel" and maybe free it when it goes
404 * down to zero. Don't free it if there is a pending action.
405 * Returns TRUE when the channel is no longer referenced.
406 */
407 int
408channel_unref(channel_T *channel)
409{
410 if (channel != NULL && --channel->ch_refcount <= 0)
411 return channel_may_free(channel);
412 return FALSE;
413}
414
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200415 int
416free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100417{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200418 int did_free = FALSE;
419 channel_T *ch;
420
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200421 /* This is invoked from the garbage collector, which only runs at a safe
422 * point. */
423 ++safe_to_invoke_callback;
424
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200425 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200426 if (!channel_still_useful(ch)
427 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200428 {
429 /* Free the channel and ordinary items it contains, but don't
430 * recurse into Lists, Dictionaries etc. */
431 channel_free_contents(ch);
432 did_free = TRUE;
433 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200434
435 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200436 return did_free;
437}
438
439 void
440free_unused_channels(int copyID, int mask)
441{
442 channel_T *ch;
443 channel_T *ch_next;
444
445 for (ch = first_channel; ch != NULL; ch = ch_next)
446 {
447 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200448 if (!channel_still_useful(ch)
449 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200450 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200451 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200452 channel_free_channel(ch);
453 }
454 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100455}
456
Bram Moolenaard04a0202016-01-26 23:30:18 +0100457#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100458
459#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
460 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100461channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100462{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100463 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200464 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100465
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100466 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100467 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200468 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100469 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200470 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100471}
472#endif
473
Bram Moolenaare0874f82016-01-24 20:36:41 +0100474/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100475 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100476 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100477#ifdef FEAT_GUI_X11
478 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200479messageFromServer(XtPointer clientData,
480 int *unused1 UNUSED,
481 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100482{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100483 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100484}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100485#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100486
Bram Moolenaard04a0202016-01-26 23:30:18 +0100487#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100488# if GTK_CHECK_VERSION(3,0,0)
489 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200490messageFromServer(GIOChannel *unused1 UNUSED,
491 GIOCondition unused2 UNUSED,
492 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100493{
494 channel_read_fd(GPOINTER_TO_INT(clientData));
495 return TRUE; /* Return FALSE instead in case the event source is to
496 * be removed after this function returns. */
497}
498# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100499 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200500messageFromServer(gpointer clientData,
501 gint unused1 UNUSED,
502 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100503{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100505}
Bram Moolenaar98921892016-02-23 17:14:37 +0100506# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100507#endif
508
509 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200510channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100511{
Bram Moolenaarde279892016-03-11 22:19:44 +0100512 if (!CH_HAS_GUI)
513 return;
514
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200515 /* gets stuck in handling events for a not connected channel */
516 if (channel->ch_keep_open)
517 return;
518
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100519# ifdef FEAT_GUI_X11
520 /* Tell notifier we are interested in being called
521 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100522 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
523 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100524 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100525 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100526 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100528 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100529# else
530# ifdef FEAT_GUI_GTK
531 /* Tell gdk we are interested in being called when there
532 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100533 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100534# if GTK_CHECK_VERSION(3,0,0)
535 {
536 GIOChannel *chnnl = g_io_channel_unix_new(
537 (gint)channel->ch_part[part].ch_fd);
538
539 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
540 chnnl,
541 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200542 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100543 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
544
545 g_io_channel_unref(chnnl);
546 }
547# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100548 channel->ch_part[part].ch_inputHandler = gdk_input_add(
549 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100550 (GdkInputCondition)
551 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200552 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100553 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100554# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100555# endif
556# endif
557}
558
Bram Moolenaarde279892016-03-11 22:19:44 +0100559 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100560channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100561{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100562 if (channel->CH_SOCK_FD != INVALID_FD)
563 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200564 if (channel->CH_OUT_FD != INVALID_FD
565 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100566 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200567 if (channel->CH_ERR_FD != INVALID_FD
568 && channel->CH_ERR_FD != channel->CH_SOCK_FD
569 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100571}
572
573/*
574 * Register any of our file descriptors with the GUI event handling system.
575 * Called when the GUI has started.
576 */
577 void
578channel_gui_register_all(void)
579{
Bram Moolenaar77073442016-02-13 23:23:53 +0100580 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100581
Bram Moolenaar77073442016-02-13 23:23:53 +0100582 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100583 channel_gui_register(channel);
584}
585
586 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200587channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100588{
589# ifdef FEAT_GUI_X11
590 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
591 {
592 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
593 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
594 }
595# else
596# ifdef FEAT_GUI_GTK
597 if (channel->ch_part[part].ch_inputHandler != 0)
598 {
599# if GTK_CHECK_VERSION(3,0,0)
600 g_source_remove(channel->ch_part[part].ch_inputHandler);
601# else
602 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
603# endif
604 channel->ch_part[part].ch_inputHandler = 0;
605 }
606# endif
607# endif
608}
609
610 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100611channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100612{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200613 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100614
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100615 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100616 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100617}
618
619#endif
620
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100621static char *e_cannot_connect = N_("E902: Cannot connect to port");
622
Bram Moolenaard04a0202016-01-26 23:30:18 +0100623/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100624 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100625 * "waittime" is the time in msec to wait for the connection.
626 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100627 * Returns the channel for success.
628 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100629 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100631channel_open(
632 char *hostname,
633 int port_in,
634 int waittime,
635 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100636{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100637 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100638 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100639 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100640#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100642 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100643#else
644 int port = port_in;
645#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100646 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100647 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100649#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100650 channel_init_winsock();
651#endif
652
Bram Moolenaar77073442016-02-13 23:23:53 +0100653 channel = add_channel();
654 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100655 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100656 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100657 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658 }
659
660 /* Get the server internet address and put into addr structure */
661 /* fill in the socket address structure and connect to server */
662 vim_memset((char *)&server, 0, sizeof(server));
663 server.sin_family = AF_INET;
664 server.sin_port = htons(port);
665 if ((host = gethostbyname(hostname)) == NULL)
666 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100667 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200668 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100669 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100670 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100672 {
673 char *p;
674
675 /* When using host->h_addr directly ubsan warns for it to not be
676 * aligned. First copy the pointer to aviod that. */
677 memcpy(&p, &host->h_addr, sizeof(p));
678 memcpy((char *)&server.sin_addr, p, host->h_length);
679 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100680
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100681 /* On Mac and Solaris a zero timeout almost never works. At least wait
682 * one millisecond. Let's do it for all systems, because we don't know why
683 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100684 if (waittime == 0)
685 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100686
687 /*
688 * For Unix we need to call connect() again after connect() failed.
689 * On Win32 one time is sufficient.
690 */
691 while (TRUE)
692 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100693 long elapsed_msec = 0;
694 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100695
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100696 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100697 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 sd = socket(AF_INET, SOCK_STREAM, 0);
699 if (sd == -1)
700 {
701 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200702 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100703 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100704 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100705 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100706
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100707 if (waittime >= 0)
708 {
709 /* Make connect() non-blocking. */
710 if (
711#ifdef _WIN32
712 ioctlsocket(sd, FIONBIO, &val) < 0
713#else
714 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
715#endif
716 )
717 {
718 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200719 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 "channel_open: Connect failed with errno %d", errno);
721 sock_close(sd);
722 channel_free(channel);
723 return NULL;
724 }
725 }
726
727 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200728 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100729 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
730
Bram Moolenaar045a2842016-03-08 22:33:07 +0100731 if (ret == 0)
732 /* The connection could be established. */
733 break;
734
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100735 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100736 if (waittime < 0 || (errno != EWOULDBLOCK
737 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100738#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100739 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100740#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100741 ))
742 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200743 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100744 "channel_open: Connect failed with errno %d", errno);
745 PERROR(_(e_cannot_connect));
746 sock_close(sd);
747 channel_free(channel);
748 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100749 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100750
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100751 /* Limit the waittime to 50 msec. If it doesn't work within this
752 * time we close the socket and try creating it again. */
753 waitnow = waittime > 50 ? 50 : waittime;
754
Bram Moolenaar045a2842016-03-08 22:33:07 +0100755 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100756 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100757#ifndef WIN32
758 if (errno != ECONNREFUSED)
759#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100760 {
761 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100762 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100764#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100765 int so_error = 0;
766 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100767 struct timeval start_tv;
768 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100769#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 FD_ZERO(&rfds);
771 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100772 FD_ZERO(&wfds);
773 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100774
Bram Moolenaar562ca712016-03-09 21:50:05 +0100775 tv.tv_sec = waitnow / 1000;
776 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100777#ifndef WIN32
778 gettimeofday(&start_tv, NULL);
779#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200780 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100781 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100782 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100783
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100784 if (ret < 0)
785 {
786 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200787 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100788 "channel_open: Connect failed with errno %d", errno);
789 PERROR(_(e_cannot_connect));
790 sock_close(sd);
791 channel_free(channel);
792 return NULL;
793 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100794
Bram Moolenaare081e212016-02-28 22:33:46 +0100795#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100796 /* On Win32: select() is expected to work and wait for up to
797 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100798 if (FD_ISSET(sd, &wfds))
799 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100800 elapsed_msec = waitnow;
801 if (waittime > 1 && elapsed_msec < waittime)
802 {
803 waittime -= elapsed_msec;
804 continue;
805 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100806#else
807 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 * After putting the socket in non-blocking mode, connect() will
809 * return EINPROGRESS, select() will not wait (as if writing is
810 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100811 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100812 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100814 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 {
816 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100817 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 if (ret < 0 || (so_error != 0
819 && so_error != EWOULDBLOCK
820 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100821# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100822 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100823# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100824 ))
825 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200826 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100827 "channel_open: Connect failed with errno %d",
828 so_error);
829 PERROR(_(e_cannot_connect));
830 sock_close(sd);
831 channel_free(channel);
832 return NULL;
833 }
834 }
835
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836 if (FD_ISSET(sd, &wfds) && so_error == 0)
837 /* Did not detect an error, connection is established. */
838 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100839
Bram Moolenaar045a2842016-03-08 22:33:07 +0100840 gettimeofday(&end_tv, NULL);
841 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
842 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100843#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100844 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100845
846#ifndef WIN32
847 if (waittime > 1 && elapsed_msec < waittime)
848 {
849 /* The port isn't ready but we also didn't get an error.
850 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100851 * yet. Select() may return early, wait until the remaining
852 * "waitnow" and try again. */
853 waitnow -= elapsed_msec;
854 waittime -= elapsed_msec;
855 if (waitnow > 0)
856 {
857 mch_delay((long)waitnow, TRUE);
858 ui_breakcheck();
859 waittime -= waitnow;
860 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100861 if (!got_int)
862 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100863 if (waittime <= 0)
864 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100865 waittime = 1;
866 continue;
867 }
868 /* we were interrupted, behave as if timed out */
869 }
870#endif
871
872 /* We timed out. */
873 ch_error(channel, "Connection timed out");
874 sock_close(sd);
875 channel_free(channel);
876 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100877 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100878
Bram Moolenaar045a2842016-03-08 22:33:07 +0100879 ch_log(channel, "Connection made");
880
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100881 if (waittime >= 0)
882 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100883#ifdef _WIN32
884 val = 0;
885 ioctlsocket(sd, FIONBIO, &val);
886#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100887 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100888#endif
889 }
890
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100891 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100892 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100893 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
894 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200895 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100896
897#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100898 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100899#endif
900
Bram Moolenaar77073442016-02-13 23:23:53 +0100901 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100902}
903
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100904/*
905 * Implements ch_open().
906 */
907 channel_T *
908channel_open_func(typval_T *argvars)
909{
910 char_u *address;
911 char_u *p;
912 char *rest;
913 int port;
914 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200915 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100916
917 address = get_tv_string(&argvars[0]);
918 if (argvars[1].v_type != VAR_UNKNOWN
919 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
920 {
921 EMSG(_(e_invarg));
922 return NULL;
923 }
924
925 /* parse address */
926 p = vim_strchr(address, ':');
927 if (p == NULL)
928 {
929 EMSG2(_(e_invarg2), address);
930 return NULL;
931 }
932 *p++ = NUL;
933 port = strtol((char *)p, &rest, 10);
934 if (*address == NUL || port <= 0 || *rest != NUL)
935 {
936 p[-1] = ':';
937 EMSG2(_(e_invarg2), address);
938 return NULL;
939 }
940
941 /* parse options */
942 clear_job_options(&opt);
943 opt.jo_mode = MODE_JSON;
944 opt.jo_timeout = 2000;
945 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200946 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200947 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100948 if (opt.jo_timeout < 0)
949 {
950 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200951 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100952 }
953
954 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
955 if (channel != NULL)
956 {
957 opt.jo_set = JO_ALL;
958 channel_set_options(channel, &opt);
959 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200960theend:
961 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100962 return channel;
963}
964
Bram Moolenaarde279892016-03-11 22:19:44 +0100965 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200966ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100967{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200968 sock_T *fd = &channel->ch_part[part].ch_fd;
969
Bram Moolenaarde279892016-03-11 22:19:44 +0100970 if (*fd != INVALID_FD)
971 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200972 if (part == PART_SOCK)
973 sock_close(*fd);
974 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200975 {
976 /* When using a pty the same FD is set on multiple parts, only
977 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200978 if ((part == PART_IN || channel->CH_IN_FD != *fd)
979 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
980 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200981 {
982#ifdef WIN32
983 if (channel->ch_named_pipe)
984 DisconnectNamedPipe((HANDLE)fd);
985#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200986 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200987 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200988 }
Bram Moolenaarde279892016-03-11 22:19:44 +0100989 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200990
991 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +0100992 }
993}
994
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100995 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100996channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100997{
Bram Moolenaarde279892016-03-11 22:19:44 +0100998 if (in != INVALID_FD)
999 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001000 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001001 channel->CH_IN_FD = in;
1002 }
1003 if (out != INVALID_FD)
1004 {
1005# if defined(FEAT_GUI)
1006 channel_gui_unregister_one(channel, PART_OUT);
1007# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001008 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001009 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001010 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001011# if defined(FEAT_GUI)
1012 channel_gui_register_one(channel, PART_OUT);
1013# endif
1014 }
1015 if (err != INVALID_FD)
1016 {
1017# if defined(FEAT_GUI)
1018 channel_gui_unregister_one(channel, PART_ERR);
1019# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001020 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001021 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001022 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001023# if defined(FEAT_GUI)
1024 channel_gui_register_one(channel, PART_ERR);
1025# endif
1026 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001027}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001028
Bram Moolenaard6051b52016-02-28 15:49:03 +01001029/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001030 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001031 * This does not keep a refcount, when the job is freed ch_job is cleared.
1032 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001033 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001034channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001035{
Bram Moolenaar77073442016-02-13 23:23:53 +01001036 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001037
1038 channel_set_options(channel, options);
1039
1040 if (job->jv_in_buf != NULL)
1041 {
1042 chanpart_T *in_part = &channel->ch_part[PART_IN];
1043
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001044 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001045 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001046 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001047 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001048 {
1049 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1050 {
1051 /* Special mode: send last-but-one line when appending a line
1052 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001053 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001054 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001055 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001056 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001057 }
1058 else
1059 in_part->ch_buf_top = options->jo_in_top;
1060 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001061 else
1062 in_part->ch_buf_top = 1;
1063 if (options->jo_set & JO_IN_BOT)
1064 in_part->ch_buf_bot = options->jo_in_bot;
1065 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001066 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001067 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001068}
1069
1070/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001071 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001072 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001073 */
1074 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001075find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001076{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001077 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001078 buf_T *save_curbuf = curbuf;
1079
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001080 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001081 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001082 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001083 if (buf == NULL)
1084 buf = buflist_findname_exp(name);
1085 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001086 if (buf == NULL)
1087 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001088 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001089 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001090 if (buf == NULL)
1091 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001092 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001093 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001094#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001095 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1096 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001097#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001098 if (curbuf->b_ml.ml_mfp == NULL)
1099 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001100 if (msg)
1101 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001102 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001103 changed_bytes(1, 0);
1104 curbuf = save_curbuf;
1105 }
1106
1107 return buf;
1108}
1109
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001110 static void
1111set_callback(
1112 char_u **cbp,
1113 partial_T **pp,
1114 char_u *callback,
1115 partial_T *partial)
1116{
1117 free_callback(*cbp, *pp);
1118 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001119 {
1120 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001121 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001122 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001123 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001124 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001125 func_ref(*cbp);
1126 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001127 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001128 else
1129 *cbp = NULL;
1130 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001131 if (partial != NULL)
1132 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001133}
1134
Bram Moolenaar187db502016-02-27 14:44:26 +01001135/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001136 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001137 */
1138 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001139channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001140{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001141 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001142
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001143 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001144 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001145 channel->ch_part[part].ch_mode = opt->jo_mode;
1146 if (opt->jo_set & JO_IN_MODE)
1147 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1148 if (opt->jo_set & JO_OUT_MODE)
1149 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1150 if (opt->jo_set & JO_ERR_MODE)
1151 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001152
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001153 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001154 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001155 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1156 if (opt->jo_set & JO_OUT_TIMEOUT)
1157 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1158 if (opt->jo_set & JO_ERR_TIMEOUT)
1159 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001160 if (opt->jo_set & JO_BLOCK_WRITE)
1161 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001162
1163 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001164 set_callback(&channel->ch_callback, &channel->ch_partial,
1165 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001166 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001167 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1168 &channel->ch_part[PART_OUT].ch_partial,
1169 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001170 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001171 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1172 &channel->ch_part[PART_ERR].ch_partial,
1173 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001174 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001175 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1176 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001177 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001178
1179 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1180 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001181 buf_T *buf;
1182
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001183 /* writing output to a buffer. Default mode is NL. */
1184 if (!(opt->jo_set & JO_OUT_MODE))
1185 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001186 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001187 {
1188 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1189 if (buf == NULL)
1190 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1191 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001192 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001193 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001194 int msg = TRUE;
1195
1196 if (opt->jo_set2 & JO2_OUT_MSG)
1197 msg = opt->jo_message[PART_OUT];
1198 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001199 }
1200 if (buf != NULL)
1201 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001202 if (opt->jo_set & JO_OUT_MODIFIABLE)
1203 channel->ch_part[PART_OUT].ch_nomodifiable =
1204 !opt->jo_modifiable[PART_OUT];
1205
1206 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1207 {
1208 EMSG(_(e_modifiable));
1209 }
1210 else
1211 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001212 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001213 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001214 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001215 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001216 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001217 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001218
1219 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1220 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1221 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1222 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001223 buf_T *buf;
1224
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001225 /* writing err to a buffer. Default mode is NL. */
1226 if (!(opt->jo_set & JO_ERR_MODE))
1227 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1228 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001229 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001230 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001231 {
1232 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1233 if (buf == NULL)
1234 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1235 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001236 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001237 {
1238 int msg = TRUE;
1239
1240 if (opt->jo_set2 & JO2_ERR_MSG)
1241 msg = opt->jo_message[PART_ERR];
1242 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1243 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 if (buf != NULL)
1245 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001246 if (opt->jo_set & JO_ERR_MODIFIABLE)
1247 channel->ch_part[PART_ERR].ch_nomodifiable =
1248 !opt->jo_modifiable[PART_ERR];
1249 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1250 {
1251 EMSG(_(e_modifiable));
1252 }
1253 else
1254 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001255 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001256 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001257 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001258 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001259 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001260 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001261
1262 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1263 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1264 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001265}
1266
1267/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001268 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001269 */
1270 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001271channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001272 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001273 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001274 char_u *callback,
1275 partial_T *partial,
1276 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001277{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001278 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001279 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1280
1281 if (item != NULL)
1282 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001283 item->cq_partial = partial;
1284 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001285 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001286 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001287 item->cq_callback = callback;
1288 }
1289 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001290 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001291 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001292 func_ref(item->cq_callback);
1293 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001294 item->cq_seq_nr = id;
1295 item->cq_prev = head->cq_prev;
1296 head->cq_prev = item;
1297 item->cq_next = NULL;
1298 if (item->cq_prev == NULL)
1299 head->cq_next = item;
1300 else
1301 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001302 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001303}
1304
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001305 static void
1306write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1307{
1308 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001309 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001310 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001311 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001312
Bram Moolenaar655da312016-05-28 22:22:34 +02001313 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001314 if ((p = alloc(len + 2)) == NULL)
1315 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001316 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001317
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001318 if (channel->ch_write_text_mode)
1319 p[len] = CAR;
1320 else
1321 {
1322 for (i = 0; i < len; ++i)
1323 if (p[i] == NL)
1324 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001325
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001326 p[len] = NL;
1327 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001328 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001329 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001330 vim_free(p);
1331}
1332
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001333/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001334 * Return TRUE if "channel" can be written to.
1335 * Returns FALSE if the input is closed or the write would block.
1336 */
1337 static int
1338can_write_buf_line(channel_T *channel)
1339{
1340 chanpart_T *in_part = &channel->ch_part[PART_IN];
1341
1342 if (in_part->ch_fd == INVALID_FD)
1343 return FALSE; /* pipe was closed */
1344
1345 /* for testing: block every other attempt to write */
1346 if (in_part->ch_block_write == 1)
1347 in_part->ch_block_write = -1;
1348 else if (in_part->ch_block_write == -1)
1349 in_part->ch_block_write = 1;
1350
1351 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1352#ifndef WIN32
1353 {
1354# if defined(HAVE_SELECT)
1355 struct timeval tval;
1356 fd_set wfds;
1357 int ret;
1358
1359 FD_ZERO(&wfds);
1360 FD_SET((int)in_part->ch_fd, &wfds);
1361 tval.tv_sec = 0;
1362 tval.tv_usec = 0;
1363 for (;;)
1364 {
1365 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1366# ifdef EINTR
1367 SOCK_ERRNO;
1368 if (ret == -1 && errno == EINTR)
1369 continue;
1370# endif
1371 if (ret <= 0 || in_part->ch_block_write == 1)
1372 {
1373 if (ret > 0)
1374 ch_log(channel, "FAKED Input not ready for writing");
1375 else
1376 ch_log(channel, "Input not ready for writing");
1377 return FALSE;
1378 }
1379 break;
1380 }
1381# else
1382 struct pollfd fds;
1383
1384 fds.fd = in_part->ch_fd;
1385 fds.events = POLLOUT;
1386 if (poll(&fds, 1, 0) <= 0)
1387 {
1388 ch_log(channel, "Input not ready for writing");
1389 return FALSE;
1390 }
1391 if (in_part->ch_block_write == 1)
1392 {
1393 ch_log(channel, "FAKED Input not ready for writing");
1394 return FALSE;
1395 }
1396# endif
1397 }
1398#endif
1399 return TRUE;
1400}
1401
1402/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001403 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001404 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001405 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001406channel_write_in(channel_T *channel)
1407{
1408 chanpart_T *in_part = &channel->ch_part[PART_IN];
1409 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001410 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001412
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001413 if (buf == NULL || in_part->ch_buf_append)
1414 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001415 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001416 {
1417 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001418 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001419 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001420 return;
1421 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001422
1423 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1424 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1425 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001426 if (!can_write_buf_line(channel))
1427 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001428 write_buf_line(buf, lnum, channel);
1429 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001430 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001431
1432 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001433 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001434 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001435 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001436
Bram Moolenaar014069a2016-03-03 22:51:40 +01001437 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001438 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001439 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001440#if defined(FEAT_TERMINAL)
1441 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001442 if (channel->ch_job != NULL)
1443 term_send_eof(channel);
1444#endif
1445
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001446 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001447 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001448 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001449
1450 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001451 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001452 }
1453 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001454 ch_log(channel, "Still %ld more lines to write",
1455 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001456}
1457
1458/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001459 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001460 */
1461 void
1462channel_buffer_free(buf_T *buf)
1463{
1464 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001465 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001466
1467 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001468 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001469 {
1470 chanpart_T *ch_part = &channel->ch_part[part];
1471
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001472 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001473 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001474 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001475 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001476 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001477 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001478 }
1479}
1480
1481/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001482 * Write any lines waiting to be written to "channel".
1483 */
1484 static void
1485channel_write_input(channel_T *channel)
1486{
1487 chanpart_T *in_part = &channel->ch_part[PART_IN];
1488
1489 if (in_part->ch_writeque.wq_next != NULL)
1490 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1491 else if (in_part->ch_bufref.br_buf != NULL)
1492 {
1493 if (in_part->ch_buf_append)
1494 channel_write_new_lines(in_part->ch_bufref.br_buf);
1495 else
1496 channel_write_in(channel);
1497 }
1498}
1499
1500/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001501 * Write any lines waiting to be written to a channel.
1502 */
1503 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001504channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001505{
1506 channel_T *channel;
1507
1508 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001509 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001510}
1511
1512/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001513 * Write appended lines above the last one in "buf" to the channel.
1514 */
1515 void
1516channel_write_new_lines(buf_T *buf)
1517{
1518 channel_T *channel;
1519 int found_one = FALSE;
1520
1521 /* There could be more than one channel for the buffer, loop over all of
1522 * them. */
1523 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1524 {
1525 chanpart_T *in_part = &channel->ch_part[PART_IN];
1526 linenr_T lnum;
1527 int written = 0;
1528
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001529 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530 {
1531 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001532 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001533 found_one = TRUE;
1534 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1535 ++lnum)
1536 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001537 if (!can_write_buf_line(channel))
1538 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001539 write_buf_line(buf, lnum, channel);
1540 ++written;
1541 }
1542
1543 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001544 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001545 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001546 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001547 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001548 ch_log(channel, "Still %ld more lines to write",
1549 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001550
1551 in_part->ch_buf_bot = lnum;
1552 }
1553 }
1554 if (!found_one)
1555 buf->b_write_to_channel = FALSE;
1556}
1557
1558/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001559 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001560 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001561 */
1562 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001563invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1564 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001565{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001566 typval_T rettv;
1567 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001568
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001569 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001570 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001571
Bram Moolenaar77073442016-02-13 23:23:53 +01001572 argv[0].v_type = VAR_CHANNEL;
1573 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001574
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001575 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1576 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001577 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001578 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001579}
1580
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001581/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001582 * Return the first node from "channel"/"part" without removing it.
1583 * Returns NULL if there is nothing.
1584 */
1585 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001586channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001587{
1588 readq_T *head = &channel->ch_part[part].ch_head;
1589
1590 return head->rq_next;
1591}
1592
1593/*
1594 * Return a pointer to the first NL in "node".
1595 * Skips over NUL characters.
1596 * Returns NULL if there is no NL.
1597 */
1598 char_u *
1599channel_first_nl(readq_T *node)
1600{
1601 char_u *buffer = node->rq_buffer;
1602 long_u i;
1603
1604 for (i = 0; i < node->rq_buflen; ++i)
1605 if (buffer[i] == NL)
1606 return buffer + i;
1607 return NULL;
1608}
1609
1610/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001611 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001612 * The caller must free it.
1613 * Returns NULL if there is nothing.
1614 */
1615 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001616channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001617{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001618 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001619 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001620 char_u *p;
1621
Bram Moolenaar77073442016-02-13 23:23:53 +01001622 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001623 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001624 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001625 p = node->rq_buffer;
1626 head->rq_next = node->rq_next;
1627 if (node->rq_next == NULL)
1628 head->rq_prev = NULL;
1629 else
1630 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001631 vim_free(node);
1632 return p;
1633}
1634
1635/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001636 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001637 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001638 */
1639 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001640channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001641{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001642 readq_T *head = &channel->ch_part[part].ch_head;
1643 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001644 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001645 char_u *res;
1646 char_u *p;
1647
1648 /* If there is only one buffer just get that one. */
1649 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1650 return channel_get(channel, part);
1651
1652 /* Concatenate everything into one buffer. */
1653 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001654 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001655 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001656 if (res == NULL)
1657 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001658 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001659 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001660 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001661 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001662 p += node->rq_buflen;
1663 }
1664 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001665
1666 /* Free all buffers */
1667 do
1668 {
1669 p = channel_get(channel, part);
1670 vim_free(p);
1671 } while (p != NULL);
1672
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001673 /* turn all NUL into NL */
1674 while (len > 0)
1675 {
1676 --len;
1677 if (res[len] == NUL)
1678 res[len] = NL;
1679 }
1680
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001681 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001682}
1683
1684/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001685 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001686 * Caller must check these bytes are available.
1687 */
1688 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001689channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001690{
1691 readq_T *head = &channel->ch_part[part].ch_head;
1692 readq_T *node = head->rq_next;
1693 char_u *buf = node->rq_buffer;
1694
1695 mch_memmove(buf, buf + len, node->rq_buflen - len);
1696 node->rq_buflen -= len;
1697}
1698
1699/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001700 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001701 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001702 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 */
1704 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001705channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001708 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 readq_T *last_node;
1710 readq_T *n;
1711 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001712 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001713 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001714
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001716 return FAIL;
1717
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001718 last_node = node->rq_next;
1719 len = node->rq_buflen + last_node->rq_buflen + 1;
1720 if (want_nl)
1721 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001722 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001723 {
1724 last_node = last_node->rq_next;
1725 len += last_node->rq_buflen;
1726 }
1727
1728 p = newbuf = alloc(len);
1729 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001730 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001731 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001732 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001733 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734 node->rq_buffer = newbuf;
1735 for (n = node; n != last_node; )
1736 {
1737 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001738 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001739 p += n->rq_buflen;
1740 vim_free(n->rq_buffer);
1741 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001742 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001743
1744 /* dispose of the collapsed nodes and their buffers */
1745 for (n = node->rq_next; n != last_node; )
1746 {
1747 n = n->rq_next;
1748 vim_free(n->rq_prev);
1749 }
1750 node->rq_next = last_node->rq_next;
1751 if (last_node->rq_next == NULL)
1752 head->rq_prev = node;
1753 else
1754 last_node->rq_next->rq_prev = node;
1755 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001756 return OK;
1757}
1758
1759/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001760 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001761 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001762 * Returns OK or FAIL.
1763 */
1764 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001765channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001766 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001767{
1768 readq_T *node;
1769 readq_T *head = &channel->ch_part[part].ch_head;
1770 char_u *p;
1771 int i;
1772
1773 node = (readq_T *)alloc(sizeof(readq_T));
1774 if (node == NULL)
1775 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001776 /* A NUL is added at the end, because netbeans code expects that.
1777 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 node->rq_buffer = alloc(len + 1);
1779 if (node->rq_buffer == NULL)
1780 {
1781 vim_free(node);
1782 return FAIL; /* out of memory */
1783 }
1784
1785 if (channel->ch_part[part].ch_mode == MODE_NL)
1786 {
1787 /* Drop any CR before a NL. */
1788 p = node->rq_buffer;
1789 for (i = 0; i < len; ++i)
1790 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1791 *p++ = buf[i];
1792 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001793 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001794 }
1795 else
1796 {
1797 mch_memmove(node->rq_buffer, buf, len);
1798 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001799 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001800 }
1801
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001802 if (prepend)
1803 {
1804 /* preend node to the head of the queue */
1805 node->rq_next = head->rq_next;
1806 node->rq_prev = NULL;
1807 if (head->rq_next == NULL)
1808 head->rq_prev = node;
1809 else
1810 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001811 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001812 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001813 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001814 {
1815 /* append node to the tail of the queue */
1816 node->rq_next = NULL;
1817 node->rq_prev = head->rq_prev;
1818 if (head->rq_prev == NULL)
1819 head->rq_next = node;
1820 else
1821 head->rq_prev->rq_next = node;
1822 head->rq_prev = node;
1823 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001824
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001825 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001826 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001827 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001828 fprintf(log_fd, "'");
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001829 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001830 fprintf(log_fd, "'\n");
1831 }
1832 return OK;
1833}
1834
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001835/*
1836 * Try to fill the buffer of "reader".
1837 * Returns FALSE when nothing was added.
1838 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001839 static int
1840channel_fill(js_read_T *reader)
1841{
1842 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001843 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001844 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001845 int keeplen;
1846 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001847 char_u *p;
1848
1849 if (next == NULL)
1850 return FALSE;
1851
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001852 keeplen = reader->js_end - reader->js_buf;
1853 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001854 {
1855 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001856 addlen = (int)STRLEN(next);
1857 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001858 if (p == NULL)
1859 {
1860 vim_free(next);
1861 return FALSE;
1862 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001863 mch_memmove(p, reader->js_buf, keeplen);
1864 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001865 vim_free(next);
1866 next = p;
1867 }
1868
1869 vim_free(reader->js_buf);
1870 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001871 return TRUE;
1872}
1873
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001874/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001875 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001876 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001877 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001878 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001879 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001880channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001881{
1882 js_read_T reader;
1883 typval_T listtv;
1884 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001885 chanpart_T *chanpart = &channel->ch_part[part];
1886 jsonq_T *head = &chanpart->ch_json_head;
1887 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001888 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001889
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001891 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001892
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001893 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001894 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001895 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001896 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001897 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001898
1899 /* When a message is incomplete we wait for a short while for more to
1900 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001901 * or list will make us hang.
1902 * Do not generate error messages, they will be written in a channel log. */
1903 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001904 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001905 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001906 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001907 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001908 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001909 /* Only accept the response when it is a list with at least two
1910 * items. */
1911 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001912 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001913 if (listtv.v_type != VAR_LIST)
1914 ch_error(channel, "Did not receive a list, discarding");
1915 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001916 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001917 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001918 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001919 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001920 else
1921 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001922 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1923 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001924 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001925 else
1926 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001927 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001928 item->jq_value = alloc_tv();
1929 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001930 {
1931 vim_free(item);
1932 clear_tv(&listtv);
1933 }
1934 else
1935 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001936 *item->jq_value = listtv;
1937 item->jq_prev = head->jq_prev;
1938 head->jq_prev = item;
1939 item->jq_next = NULL;
1940 if (item->jq_prev == NULL)
1941 head->jq_next = item;
1942 else
1943 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001944 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001945 }
1946 }
1947 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001948
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001949 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001950 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001951 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001952 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001953 size_t buflen = STRLEN(reader.js_buf);
1954
1955 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001956 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001957 /* First time encountering incomplete message or after receiving
1958 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001959 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001960 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001961 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001962 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001963 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001964#ifdef WIN32
1965 chanpart->ch_deadline = GetTickCount() + 100L;
1966#else
1967 gettimeofday(&chanpart->ch_deadline, NULL);
1968 chanpart->ch_deadline.tv_usec += 100 * 1000;
1969 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1970 {
1971 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1972 ++chanpart->ch_deadline.tv_sec;
1973 }
1974#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001975 }
1976 else
1977 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 int timeout;
1979#ifdef WIN32
1980 timeout = GetTickCount() > chanpart->ch_deadline;
1981#else
1982 {
1983 struct timeval now_tv;
1984
1985 gettimeofday(&now_tv, NULL);
1986 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1987 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1988 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1989 }
1990#endif
1991 if (timeout)
1992 {
1993 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001994 chanpart->ch_wait_len = 0;
1995 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001996 }
1997 else
1998 {
1999 reader.js_used = 0;
2000 ch_log(channel, "still waiting on incomplete message");
2001 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002002 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002003 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002004
2005 if (status == FAIL)
2006 {
2007 ch_error(channel, "Decoding failed - discarding input");
2008 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002009 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002010 }
2011 else if (reader.js_buf[reader.js_used] != NUL)
2012 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002013 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002014 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002015 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2016 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002017 ret = status == MAYBE ? FALSE: TRUE;
2018 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002019 else
2020 ret = FALSE;
2021
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002022 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002023 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002024}
2025
2026/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002027 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002028 */
2029 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002030remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002031{
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 if (node->cq_prev == NULL)
2033 head->cq_next = node->cq_next;
2034 else
2035 node->cq_prev->cq_next = node->cq_next;
2036 if (node->cq_next == NULL)
2037 head->cq_prev = node->cq_prev;
2038 else
2039 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002040}
2041
2042/*
2043 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002044 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002045 */
2046 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002047remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002048{
Bram Moolenaar77073442016-02-13 23:23:53 +01002049 if (node->jq_prev == NULL)
2050 head->jq_next = node->jq_next;
2051 else
2052 node->jq_prev->jq_next = node->jq_next;
2053 if (node->jq_next == NULL)
2054 head->jq_prev = node->jq_prev;
2055 else
2056 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057 vim_free(node);
2058}
2059
2060/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002061 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002062 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002063 * When "id" is zero or negative jut get the first message. But not the one
2064 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002065 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002066 * Return OK when found and return the value in "rettv".
2067 * Return FAIL otherwise.
2068 */
2069 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002070channel_get_json(
2071 channel_T *channel,
2072 ch_part_T part,
2073 int id,
2074 int without_callback,
2075 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002076{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002077 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002078 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002079
Bram Moolenaar77073442016-02-13 23:23:53 +01002080 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002081 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002082 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002083 typval_T *tv = &l->lv_first->li_tv;
2084
Bram Moolenaar958dc692016-12-01 15:34:12 +01002085 if ((without_callback || !item->jq_no_callback)
2086 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002087 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002088 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002089 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002090 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002091 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002092 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002093 ch_log(channel, "Getting JSON message %ld",
2094 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002095 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002096 return OK;
2097 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002098 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002099 }
2100 return FAIL;
2101}
2102
Bram Moolenaar958dc692016-12-01 15:34:12 +01002103/*
2104 * Put back "rettv" into the JSON queue, there was no callback for it.
2105 * Takes over the values in "rettv".
2106 */
2107 static void
2108channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2109{
2110 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2111 jsonq_T *item = head->jq_next;
2112 jsonq_T *newitem;
2113
2114 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2115 /* last item was pushed back, append to the end */
2116 item = NULL;
2117 else while (item != NULL && item->jq_no_callback)
2118 /* append after the last item that was pushed back */
2119 item = item->jq_next;
2120
2121 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2122 if (newitem == NULL)
2123 clear_tv(rettv);
2124 else
2125 {
2126 newitem->jq_value = alloc_tv();
2127 if (newitem->jq_value == NULL)
2128 {
2129 vim_free(newitem);
2130 clear_tv(rettv);
2131 }
2132 else
2133 {
2134 newitem->jq_no_callback = FALSE;
2135 *newitem->jq_value = *rettv;
2136 if (item == NULL)
2137 {
2138 /* append to the end */
2139 newitem->jq_prev = head->jq_prev;
2140 head->jq_prev = newitem;
2141 newitem->jq_next = NULL;
2142 if (newitem->jq_prev == NULL)
2143 head->jq_next = newitem;
2144 else
2145 newitem->jq_prev->jq_next = newitem;
2146 }
2147 else
2148 {
2149 /* append after "item" */
2150 newitem->jq_prev = item;
2151 newitem->jq_next = item->jq_next;
2152 item->jq_next = newitem;
2153 if (newitem->jq_next == NULL)
2154 head->jq_prev = newitem;
2155 else
2156 newitem->jq_next->jq_prev = newitem;
2157 }
2158 }
2159 }
2160}
2161
Bram Moolenaarece61b02016-02-20 21:39:05 +01002162#define CH_JSON_MAX_ARGS 4
2163
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002164/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002165 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002166 * "argv[0]" is the command string.
2167 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002168 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002169 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002170channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002171{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002172 char_u *cmd = argv[0].vval.v_string;
2173 char_u *arg;
2174 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002175
Bram Moolenaarece61b02016-02-20 21:39:05 +01002176 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002177 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002178 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002179 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002180 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002181 return;
2182 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002183 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002184 if (arg == NULL)
2185 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002186
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002187 if (STRCMP(cmd, "ex") == 0)
2188 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002189 int save_called_emsg = called_emsg;
2190
2191 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002192 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002193 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002194 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002195 --emsg_silent;
2196 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002197 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002198 (char *)get_vim_var_str(VV_ERRMSG));
2199 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002200 }
2201 else if (STRCMP(cmd, "normal") == 0)
2202 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002203 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002204
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002205 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002206 ea.arg = arg;
2207 ea.addr_count = 0;
2208 ea.forceit = TRUE; /* no mapping */
2209 ex_normal(&ea);
2210 }
2211 else if (STRCMP(cmd, "redraw") == 0)
2212 {
2213 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002214
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002215 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002216 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002217 ex_redraw(&ea);
2218 showruler(FALSE);
2219 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002220 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002221 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002222 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002223 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002224 int is_call = cmd[0] == 'c';
2225 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002226
Bram Moolenaarece61b02016-02-20 21:39:05 +01002227 if (argv[id_idx].v_type != VAR_UNKNOWN
2228 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002229 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002230 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002231 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002232 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002233 }
2234 else if (is_call && argv[2].v_type != VAR_LIST)
2235 {
2236 ch_error(channel, "third argument for call must be a list");
2237 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002238 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002239 }
2240 else
2241 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002242 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002244 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002245 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002247 /* Don't pollute the display with errors. */
2248 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002249 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002250 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002251 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002252 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002253 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002254 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002255 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002256 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002257 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2258 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002259 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002260
2261 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002262 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002263 int id = argv[id_idx].vval.v_number;
2264
Bram Moolenaar55fab432016-02-07 16:53:13 +01002265 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002266 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002267 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002268 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002269 /* If evaluation failed or the result can't be encoded
2270 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002271 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002272 err_tv.v_type = VAR_STRING;
2273 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002274 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002275 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002276 if (json != NULL)
2277 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002278 channel_send(channel,
2279 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002280 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002281 vim_free(json);
2282 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002283 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002284 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002285 if (tv == &res_tv)
2286 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002287 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002288 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002289 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002290 }
2291 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002292 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002293 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002294 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002295 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002296}
2297
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002298/*
2299 * Invoke the callback at "cbhead".
2300 * Does not redraw but sets channel_need_redraw.
2301 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002302 static void
2303invoke_one_time_callback(
2304 channel_T *channel,
2305 cbq_T *cbhead,
2306 cbq_T *item,
2307 typval_T *argv)
2308{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002309 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002310 (char *)item->cq_callback);
2311 /* Remove the item from the list first, if the callback
2312 * invokes ch_close() the list will be cleared. */
2313 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002314 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002315 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002316 vim_free(item);
2317}
2318
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002319 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002320append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002321{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002322 bufref_T save_curbuf = {NULL, 0, 0};
2323 win_T *save_curwin = NULL;
2324 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002325 linenr_T lnum = buffer->b_ml.ml_line_count;
2326 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002327 chanpart_T *ch_part = &channel->ch_part[part];
2328 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002329 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002330
2331 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2332 {
2333 if (!ch_part->ch_nomod_error)
2334 {
2335 ch_error(channel, "Buffer is not modifiable, cannot append");
2336 ch_part->ch_nomod_error = TRUE;
2337 }
2338 return;
2339 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002340
2341 /* If the buffer is also used as input insert above the last
2342 * line. Don't write these lines. */
2343 if (save_write_to)
2344 {
2345 --lnum;
2346 buffer->b_write_to_channel = FALSE;
2347 }
2348
2349 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002350 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002351
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002352 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002353
2354 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2355 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2356
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002357 u_sync(TRUE);
2358 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002359 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002360
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002361 if (empty)
2362 {
2363 /* The buffer is empty, replace the first (dummy) line. */
2364 ml_replace(lnum, msg, TRUE);
2365 lnum = 0;
2366 }
2367 else
2368 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002369 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002370
2371 /* Restore curbuf/curwin/curtab */
2372 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2373
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002374 if (ch_part->ch_nomodifiable)
2375 buffer->b_p_ma = FALSE;
2376 else
2377 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002378
2379 if (buffer->b_nwindows > 0)
2380 {
2381 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002382
2383 FOR_ALL_WINDOWS(wp)
2384 {
2385 if (wp->w_buffer == buffer
2386 && (save_write_to
2387 ? wp->w_cursor.lnum == lnum + 1
2388 : (wp->w_cursor.lnum == lnum
2389 && wp->w_cursor.col == 0)))
2390 {
2391 ++wp->w_cursor.lnum;
2392 save_curwin = curwin;
2393 curwin = wp;
2394 curbuf = curwin->w_buffer;
2395 scroll_cursor_bot(0, FALSE);
2396 curwin = save_curwin;
2397 curbuf = curwin->w_buffer;
2398 }
2399 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002400 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002401 channel_need_redraw = TRUE;
2402 }
2403
2404 if (save_write_to)
2405 {
2406 channel_T *ch;
2407
2408 /* Find channels reading from this buffer and adjust their
2409 * next-to-read line number. */
2410 buffer->b_write_to_channel = TRUE;
2411 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2412 {
2413 chanpart_T *in_part = &ch->ch_part[PART_IN];
2414
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002415 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002416 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2417 }
2418 }
2419}
2420
Bram Moolenaar437905c2016-04-26 19:01:05 +02002421 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002422drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002423{
2424 char_u *msg;
2425
2426 while ((msg = channel_get(channel, part)) != NULL)
2427 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002428 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002429 vim_free(msg);
2430 }
2431}
2432
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002433/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002434 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002435 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002436 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002437 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002438 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002439may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002440{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002441 char_u *msg = NULL;
2442 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002443 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002444 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002445 chanpart_T *ch_part = &channel->ch_part[part];
2446 ch_mode_T ch_mode = ch_part->ch_mode;
2447 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002448 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002449 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002450 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002451 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002452 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002453
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002454 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002455 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002456 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002457
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002458 /* Use a message-specific callback, part callback or channel callback */
2459 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2460 if (cbitem->cq_seq_nr == 0)
2461 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002462 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002463 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002464 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002465 partial = cbitem->cq_partial;
2466 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002467 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002468 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002469 callback = ch_part->ch_callback;
2470 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002471 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002472 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002473 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002474 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002475 partial = channel->ch_partial;
2476 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002477
Bram Moolenaarec68a992016-10-03 21:37:41 +02002478 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002479 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2480 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002481 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002482 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002483 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002484 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002485 buffer = NULL;
2486 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002487
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002488 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002489 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002490 listitem_T *item;
2491 int argc = 0;
2492
Bram Moolenaard7ece102016-02-02 23:23:02 +01002493 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002494 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002495 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002496 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002497 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002498 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002499 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002500 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002501
Bram Moolenaarece61b02016-02-20 21:39:05 +01002502 for (item = listtv->vval.v_list->lv_first;
2503 item != NULL && argc < CH_JSON_MAX_ARGS;
2504 item = item->li_next)
2505 argv[argc++] = item->li_tv;
2506 while (argc < CH_JSON_MAX_ARGS)
2507 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002508
Bram Moolenaarece61b02016-02-20 21:39:05 +01002509 if (argv[0].v_type == VAR_STRING)
2510 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002511 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002512 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002513 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002514 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002515 }
2516
Bram Moolenaarece61b02016-02-20 21:39:05 +01002517 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002518 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002519 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002520 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002521 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002522 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002523 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002524 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002525 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002526 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002527 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002528 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002529 return FALSE;
2530 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002531 else
2532 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002533 /* If there is no callback or buffer drop the message. */
2534 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002535 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002536 /* If there is a close callback it may use ch_read() to get the
2537 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002538 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002539 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002540 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002541 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002542
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002543 if (ch_mode == MODE_NL)
2544 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002545 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002546 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002547 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002548
2549 /* See if we have a message ending in NL in the first buffer. If
2550 * not try to concatenate the first and the second buffer. */
2551 while (TRUE)
2552 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002553 node = channel_peek(channel, part);
2554 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002555 if (nl != NULL)
2556 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002557 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002558 {
2559 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2560 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002561 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002562 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002563 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002564 buf = node->rq_buffer;
2565
Bram Moolenaarec68a992016-10-03 21:37:41 +02002566 if (nl == NULL)
2567 {
2568 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002569 char_u *new_buf;
2570
2571 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2572 if (new_buf == NULL)
2573 /* This might fail over and over again, should the message
2574 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002575 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002576 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002577 node->rq_buffer = buf;
2578 nl = buf + node->rq_buflen++;
2579 *nl = NUL;
2580 }
2581
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002582 /* Convert NUL to NL, the internal representation. */
2583 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2584 if (*p == NUL)
2585 *p = NL;
2586
2587 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002588 {
2589 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002590 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002591 *nl = NUL;
2592 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002593 else
2594 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002595 /* Copy the message into allocated memory (excluding the NL)
2596 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002597 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002598 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002599 }
2600 }
2601 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002602 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002603 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002604 * get everything we have.
2605 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002606 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002607 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002608
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002609 if (msg == NULL)
2610 return FALSE; /* out of memory (and avoids Coverity warning) */
2611
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002612 argv[1].v_type = VAR_STRING;
2613 argv[1].vval.v_string = msg;
2614 }
2615
Bram Moolenaara07fec92016-02-05 21:04:08 +01002616 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002617 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002618 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002619
Bram Moolenaar958dc692016-12-01 15:34:12 +01002620 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002621 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002622 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002623 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002624 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002625 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002626 break;
2627 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002628 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002629 {
2630 if (channel->ch_drop_never)
2631 {
2632 /* message must be read with ch_read() */
2633 channel_push_json(channel, part, listtv);
2634 listtv = NULL;
2635 }
2636 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002637 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002638 seq_nr);
2639 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002640 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002641 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002642 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002643 if (buffer != NULL)
2644 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002645 if (msg == NULL)
2646 /* JSON or JS mode: re-encode the message. */
2647 msg = json_encode(listtv, ch_mode);
2648 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002649 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002650#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002651 if (buffer->b_term != NULL)
2652 write_to_term(buffer, msg, channel);
2653 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002654#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002655 append_to_buffer(buffer, msg, channel, part);
2656 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002657 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002658
Bram Moolenaar187db502016-02-27 14:44:26 +01002659 if (callback != NULL)
2660 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002661 if (cbitem != NULL)
2662 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2663 else
2664 {
2665 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002666 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002667 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002668 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002669 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002670 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002671 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002672 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002673 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002674
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002675 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002676 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002677 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002678
2679 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002680}
2681
2682/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002683 * Return TRUE when channel "channel" is open for writing to.
2684 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002685 */
2686 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002687channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002688{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002689 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002690 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002691}
2692
2693/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002694 * Return TRUE when channel "channel" is open for reading or writing.
2695 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002696 */
2697 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002698channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002699{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002700 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002701 || channel->CH_IN_FD != INVALID_FD
2702 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002703 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002704}
2705
2706/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002707 * Return TRUE if "channel" has JSON or other typeahead.
2708 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002709 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002710channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002711{
2712 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2713
2714 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2715 {
2716 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2717 jsonq_T *item = head->jq_next;
2718
2719 return item != NULL;
2720 }
2721 return channel_peek(channel, part) != NULL;
2722}
2723
2724/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002725 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002726 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002727 */
2728 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002729channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002730{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002731 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002732 int has_readahead = FALSE;
2733
Bram Moolenaar77073442016-02-13 23:23:53 +01002734 if (channel == NULL)
2735 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002736 if (req_part == PART_OUT)
2737 {
2738 if (channel->CH_OUT_FD != INVALID_FD)
2739 return "open";
2740 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002741 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002742 }
2743 else if (req_part == PART_ERR)
2744 {
2745 if (channel->CH_ERR_FD != INVALID_FD)
2746 return "open";
2747 if (channel_has_readahead(channel, PART_ERR))
2748 has_readahead = TRUE;
2749 }
2750 else
2751 {
2752 if (channel_is_open(channel))
2753 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002754 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002755 if (channel_has_readahead(channel, part))
2756 {
2757 has_readahead = TRUE;
2758 break;
2759 }
2760 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002761
2762 if (has_readahead)
2763 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002764 return "closed";
2765}
2766
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002767 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002768channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002769{
2770 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002771 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002772 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002773 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002774 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002775
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002776 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002777 STRCAT(namebuf, "_");
2778 tail = STRLEN(namebuf);
2779
2780 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002781 if (chanpart->ch_fd != INVALID_FD)
2782 status = "open";
2783 else if (channel_has_readahead(channel, part))
2784 status = "buffered";
2785 else
2786 status = "closed";
2787 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002788
2789 STRCPY(namebuf + tail, "mode");
2790 switch (chanpart->ch_mode)
2791 {
2792 case MODE_NL: s = "NL"; break;
2793 case MODE_RAW: s = "RAW"; break;
2794 case MODE_JSON: s = "JSON"; break;
2795 case MODE_JS: s = "JS"; break;
2796 }
2797 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2798
2799 STRCPY(namebuf + tail, "io");
2800 if (part == PART_SOCK)
2801 s = "socket";
2802 else switch (chanpart->ch_io)
2803 {
2804 case JIO_NULL: s = "null"; break;
2805 case JIO_PIPE: s = "pipe"; break;
2806 case JIO_FILE: s = "file"; break;
2807 case JIO_BUFFER: s = "buffer"; break;
2808 case JIO_OUT: s = "out"; break;
2809 }
2810 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2811
2812 STRCPY(namebuf + tail, "timeout");
2813 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2814}
2815
2816 void
2817channel_info(channel_T *channel, dict_T *dict)
2818{
2819 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002820 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002821
2822 if (channel->ch_hostname != NULL)
2823 {
2824 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2825 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2826 channel_part_info(channel, dict, "sock", PART_SOCK);
2827 }
2828 else
2829 {
2830 channel_part_info(channel, dict, "out", PART_OUT);
2831 channel_part_info(channel, dict, "err", PART_ERR);
2832 channel_part_info(channel, dict, "in", PART_IN);
2833 }
2834}
2835
Bram Moolenaar77073442016-02-13 23:23:53 +01002836/*
2837 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002838 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002839 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002840 */
2841 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002842channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002843{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002844 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002845
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002846#ifdef FEAT_GUI
2847 channel_gui_unregister(channel);
2848#endif
2849
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002850 ch_close_part(channel, PART_SOCK);
2851 ch_close_part(channel, PART_IN);
2852 ch_close_part(channel, PART_OUT);
2853 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002854
Bram Moolenaara9f02812017-08-05 17:40:38 +02002855 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002856 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002857 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002858
Bram Moolenaara9f02812017-08-05 17:40:38 +02002859 /* Invoke callbacks and flush buffers before the close callback. */
2860 if (channel->ch_close_cb != NULL)
2861 ch_log(channel,
2862 "Invoking callbacks and flushing buffers before closing");
2863 for (part = PART_SOCK; part < PART_IN; ++part)
2864 {
2865 if (channel->ch_close_cb != NULL
2866 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2867 {
2868 /* Increment the refcount to avoid the channel being freed
2869 * halfway. */
2870 ++channel->ch_refcount;
2871 if (channel->ch_close_cb == NULL)
2872 ch_log(channel, "flushing %s buffers before closing",
2873 part_names[part]);
2874 while (may_invoke_callback(channel, part))
2875 ;
2876 --channel->ch_refcount;
2877 }
2878 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002879
Bram Moolenaara9f02812017-08-05 17:40:38 +02002880 if (channel->ch_close_cb != NULL)
2881 {
2882 typval_T argv[1];
2883 typval_T rettv;
2884 int dummy;
2885
2886 /* Increment the refcount to avoid the channel being freed
2887 * halfway. */
2888 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002889 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002890 (char *)channel->ch_close_cb);
2891 argv[0].v_type = VAR_CHANNEL;
2892 argv[0].vval.v_channel = channel;
2893 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002894 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002895 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002896 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002897 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002898
Bram Moolenaara9f02812017-08-05 17:40:38 +02002899 /* the callback is only called once */
2900 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2901 channel->ch_close_cb = NULL;
2902 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002903
Bram Moolenaara9f02812017-08-05 17:40:38 +02002904 if (channel_need_redraw)
2905 {
2906 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002907 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002908 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002909
Bram Moolenaara9f02812017-08-05 17:40:38 +02002910 if (!channel->ch_drop_never)
2911 /* any remaining messages are useless now */
2912 for (part = PART_SOCK; part < PART_IN; ++part)
2913 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002914
2915 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002916 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002917 }
2918
2919 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002920
2921#ifdef FEAT_TERMINAL
2922 term_channel_closed(channel);
2923#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002924}
2925
Bram Moolenaard04a0202016-01-26 23:30:18 +01002926/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002927 * Close the "in" part channel "channel".
2928 */
2929 void
2930channel_close_in(channel_T *channel)
2931{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002932 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002933}
2934
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002935 static void
2936remove_from_writeque(writeq_T *wq, writeq_T *entry)
2937{
2938 ga_clear(&entry->wq_ga);
2939 wq->wq_next = entry->wq_next;
2940 if (wq->wq_next == NULL)
2941 wq->wq_prev = NULL;
2942 else
2943 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002944 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002945}
2946
Bram Moolenaar0874a832016-09-01 15:11:51 +02002947/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002948 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002949 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002950 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002951channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002952{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002953 chanpart_T *ch_part = &channel->ch_part[part];
2954 jsonq_T *json_head = &ch_part->ch_json_head;
2955 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002956
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002957 while (channel_peek(channel, part) != NULL)
2958 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002959
2960 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002961 {
2962 cbq_T *node = cb_head->cq_next;
2963
2964 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002965 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002966 vim_free(node);
2967 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002968
2969 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002970 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002971 free_tv(json_head->jq_next->jq_value);
2972 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002973 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002974
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002975 free_callback(ch_part->ch_callback, ch_part->ch_partial);
2976 ch_part->ch_callback = NULL;
2977 ch_part->ch_partial = NULL;
2978
2979 while (ch_part->ch_writeque.wq_next != NULL)
2980 remove_from_writeque(&ch_part->ch_writeque,
2981 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002982}
2983
2984/*
2985 * Clear all the read buffers on "channel".
2986 */
2987 void
2988channel_clear(channel_T *channel)
2989{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002990 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01002991 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002992 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002993 channel_clear_one(channel, PART_OUT);
2994 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002995 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002996 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002997 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002998 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002999 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003000 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003001 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003002}
3003
Bram Moolenaar77073442016-02-13 23:23:53 +01003004#if defined(EXITFREE) || defined(PROTO)
3005 void
3006channel_free_all(void)
3007{
3008 channel_T *channel;
3009
Bram Moolenaard6051b52016-02-28 15:49:03 +01003010 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003011 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3012 channel_clear(channel);
3013}
3014#endif
3015
3016
Bram Moolenaar715d2852016-04-30 17:06:31 +02003017/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003018#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003019
3020/* Buffer size for reading incoming messages. */
3021#define MAXMSGSIZE 4096
3022
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003023#if defined(HAVE_SELECT)
3024/*
3025 * Add write fds where we are waiting for writing to be possible.
3026 */
3027 static int
3028channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3029{
3030 int maxfd = maxfd_arg;
3031 channel_T *ch;
3032
3033 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3034 {
3035 chanpart_T *in_part = &ch->ch_part[PART_IN];
3036
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003037 if (in_part->ch_fd != INVALID_FD
3038 && (in_part->ch_bufref.br_buf != NULL
3039 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003040 {
3041 FD_SET((int)in_part->ch_fd, wfds);
3042 if ((int)in_part->ch_fd >= maxfd)
3043 maxfd = (int)in_part->ch_fd + 1;
3044 }
3045 }
3046 return maxfd;
3047}
3048#else
3049/*
3050 * Add write fds where we are waiting for writing to be possible.
3051 */
3052 static int
3053channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3054{
3055 int nfd = nfd_in;
3056 channel_T *ch;
3057
3058 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3059 {
3060 chanpart_T *in_part = &ch->ch_part[PART_IN];
3061
Bram Moolenaar683b7962017-08-19 15:51:59 +02003062 if (in_part->ch_fd != INVALID_FD
3063 && (in_part->ch_bufref.br_buf != NULL
3064 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003065 {
3066 in_part->ch_poll_idx = nfd;
3067 fds[nfd].fd = in_part->ch_fd;
3068 fds[nfd].events = POLLOUT;
3069 ++nfd;
3070 }
3071 else
3072 in_part->ch_poll_idx = -1;
3073 }
3074 return nfd;
3075}
3076#endif
3077
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003078typedef enum {
3079 CW_READY,
3080 CW_NOT_READY,
3081 CW_ERROR
3082} channel_wait_result;
3083
Bram Moolenaard04a0202016-01-26 23:30:18 +01003084/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003085 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003086 * Return CW_READY when there is something to read.
3087 * Return CW_NOT_READY when there is nothing to read.
3088 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003089 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003090 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003091channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003092{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003093 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003094 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003095
Bram Moolenaard8070362016-02-15 21:56:54 +01003096# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003097 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003098 {
3099 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003100 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003101 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003102 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003103
3104 /* reading from a pipe, not a socket */
3105 while (TRUE)
3106 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003107 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3108
3109 if (r && nread > 0)
3110 return CW_READY;
3111 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003112 {
3113 DWORD err = GetLastError();
3114
3115 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3116 return CW_ERROR;
3117
3118 if (channel->ch_named_pipe)
3119 {
3120 DisconnectNamedPipe((HANDLE)fd);
3121 ConnectNamedPipe((HANDLE)fd, NULL);
3122 }
3123 else
3124 return CW_ERROR;
3125 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003126
3127 /* perhaps write some buffer lines */
3128 channel_write_any_lines();
3129
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003130 sleep_time = deadline - GetTickCount();
3131 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003132 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003133 /* Wait for a little while. Very short at first, up to 10 msec
3134 * after looping a few times. */
3135 if (sleep_time > delay)
3136 sleep_time = delay;
3137 Sleep(sleep_time);
3138 delay = delay * 2;
3139 if (delay > 10)
3140 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003141 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003142 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003143 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003144#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003145 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003146#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003147 struct timeval tval;
3148 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003149 fd_set wfds;
3150 int ret;
3151 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003152
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003153 tval.tv_sec = timeout / 1000;
3154 tval.tv_usec = (timeout % 1000) * 1000;
3155 for (;;)
3156 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003157 FD_ZERO(&rfds);
3158 FD_SET((int)fd, &rfds);
3159
3160 /* Write lines to a pipe when a pipe can be written to. Need to
3161 * set this every time, some buffers may be done. */
3162 maxfd = (int)fd + 1;
3163 FD_ZERO(&wfds);
3164 maxfd = channel_fill_wfds(maxfd, &wfds);
3165
3166 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003167# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003168 SOCK_ERRNO;
3169 if (ret == -1 && errno == EINTR)
3170 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003171# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003172 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003173 {
3174 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003175 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003176 channel_write_any_lines();
3177 continue;
3178 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003179 break;
3180 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003181#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003182 for (;;)
3183 {
3184 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3185 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003186
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003187 fds[0].fd = fd;
3188 fds[0].events = POLLIN;
3189 nfd = channel_fill_poll_write(nfd, fds);
3190 if (poll(fds, nfd, timeout) > 0)
3191 {
3192 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003193 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003194 channel_write_any_lines();
3195 continue;
3196 }
3197 break;
3198 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003199#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003200 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003201 return CW_NOT_READY;
3202}
3203
3204 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003205ch_close_part_on_error(
3206 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003207{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003208 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003209
3210 if (is_err)
3211 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003212 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003213 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003214 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003215
3216 /* Queue a "DETACH" netbeans message in the command queue in order to
3217 * terminate the netbeans session later. Do not end the session here
3218 * directly as we may be running in the context of a call to
3219 * netbeans_parse_messages():
3220 * netbeans_parse_messages
3221 * -> autocmd triggered while processing the netbeans cmd
3222 * -> ui_breakcheck
3223 * -> gui event loop or select loop
3224 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003225 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003226 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003227 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003228 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003229 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3230
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003231 /* When reading is not possible close this part of the channel. Don't
3232 * close the channel yet, there may be something to read on another part. */
3233 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003234
3235#ifdef FEAT_GUI
3236 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003237 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003238#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003239}
3240
3241 static void
3242channel_close_now(channel_T *channel)
3243{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003244 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003245 if (channel->ch_nb_close_cb != NULL)
3246 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003247 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003248}
3249
3250/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003251 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003252 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003253 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003254 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003255 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003256channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003257{
3258 static char_u *buf = NULL;
3259 int len = 0;
3260 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003261 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003262 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003263
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003264 fd = channel->ch_part[part].ch_fd;
3265 if (fd == INVALID_FD)
3266 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003267 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003268 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003269 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003270 }
3271 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003272
3273 /* Allocate a buffer to read into. */
3274 if (buf == NULL)
3275 {
3276 buf = alloc(MAXMSGSIZE);
3277 if (buf == NULL)
3278 return; /* out of memory! */
3279 }
3280
3281 /* Keep on reading for as long as there is something to read.
3282 * Use select() or poll() to avoid blocking on a message that is exactly
3283 * MAXMSGSIZE long. */
3284 for (;;)
3285 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003286 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003287 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003288 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003289 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003290 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003291 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003292 if (len <= 0)
3293 break; /* error or nothing more to read */
3294
3295 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003296 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003297 readlen += len;
3298 if (len < MAXMSGSIZE)
3299 break; /* did read everything that's available */
3300 }
3301
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003302 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003303 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003304 {
3305 if (!channel->ch_keep_open)
3306 ch_close_part_on_error(channel, part, (len < 0), func);
3307 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003308#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003309 else if (CH_HAS_GUI && gtk_main_level() > 0)
3310 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003311 gtk_main_quit();
3312#endif
3313}
3314
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003315/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003316 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003317 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003318 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003319 * Returns what was read in allocated memory.
3320 * Returns NULL in case of error or timeout.
3321 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003322 static char_u *
3323channel_read_block(channel_T *channel, ch_part_T part, int timeout, int raw)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003324{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003325 char_u *buf;
3326 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003327 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003328 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003329 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003330 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003331
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003332 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003333 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003334
3335 while (TRUE)
3336 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003337 node = channel_peek(channel, part);
3338 if (node != NULL)
3339 {
3340 if (mode == MODE_RAW || (mode == MODE_NL
3341 && channel_first_nl(node) != NULL))
3342 /* got a complete message */
3343 break;
3344 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3345 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003346 /* If not blocking or nothing more is coming then return what we
3347 * have. */
3348 if (raw || fd == INVALID_FD)
3349 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003350 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003351
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003352 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003353 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003354 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003355 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003356 {
3357 ch_log(channel, "Timed out");
3358 return NULL;
3359 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003360 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003361 }
3362
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003363 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003364 if (mode == MODE_RAW)
3365 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003366 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003367 }
3368 else
3369 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003370 char_u *p;
3371
3372 buf = node->rq_buffer;
3373 nl = channel_first_nl(node);
3374
3375 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003376 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003377 if (*p == NUL)
3378 *p = NL;
3379
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003380 if (nl == NULL)
3381 {
3382 /* must be a closed channel with missing NL */
3383 msg = channel_get(channel, part);
3384 }
3385 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003386 {
3387 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003388 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003389 *nl = NUL;
3390 }
3391 else
3392 {
3393 /* Copy the message into allocated memory and remove it from the
3394 * buffer. */
3395 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003396 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003397 }
3398 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003399 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003400 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003401 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003402}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003403
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003404/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003405 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003406 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003407 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003408 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003409 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003410 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003411channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003412 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003413 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003414 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003415 int id,
3416 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003417{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003418 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003419 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003420 int timeout;
3421 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003422
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003423 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003424 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003425 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003426 for (;;)
3427 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003428 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003429
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003430 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003431 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003432 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003433 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003434 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003435 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003436
Bram Moolenaard7ece102016-02-02 23:23:02 +01003437 if (!more)
3438 {
3439 /* Handle any other messages in the queue. If done some more
3440 * messages may have arrived. */
3441 if (channel_parse_messages())
3442 continue;
3443
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003444 /* Wait for up to the timeout. If there was an incomplete message
3445 * use the deadline for that. */
3446 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003447 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003448 {
3449#ifdef WIN32
3450 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3451#else
3452 {
3453 struct timeval now_tv;
3454
3455 gettimeofday(&now_tv, NULL);
3456 timeout = (chanpart->ch_deadline.tv_sec
3457 - now_tv.tv_sec) * 1000
3458 + (chanpart->ch_deadline.tv_usec
3459 - now_tv.tv_usec) / 1000
3460 + 1;
3461 }
3462#endif
3463 if (timeout < 0)
3464 {
3465 /* Something went wrong, channel_parse_json() didn't
3466 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003467 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003468 timeout = timeout_arg;
3469 }
3470 else if (timeout > timeout_arg)
3471 timeout = timeout_arg;
3472 }
3473 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003474 if (fd == INVALID_FD
3475 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003476 {
3477 if (timeout == timeout_arg)
3478 {
3479 if (fd != INVALID_FD)
3480 ch_log(channel, "Timed out");
3481 break;
3482 }
3483 }
3484 else
3485 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003486 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003487 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003488 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003489 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003490}
3491
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003492/*
3493 * Common for ch_read() and ch_readraw().
3494 */
3495 void
3496common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3497{
3498 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003499 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003500 jobopt_T opt;
3501 int mode;
3502 int timeout;
3503 int id = -1;
3504 typval_T *listtv = NULL;
3505
3506 /* return an empty string by default */
3507 rettv->v_type = VAR_STRING;
3508 rettv->vval.v_string = NULL;
3509
3510 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003511 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003512 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003513 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003514
Bram Moolenaar437905c2016-04-26 19:01:05 +02003515 if (opt.jo_set & JO_PART)
3516 part = opt.jo_part;
3517 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003518 if (channel != NULL)
3519 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003520 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003521 part = channel_part_read(channel);
3522 mode = channel_get_mode(channel, part);
3523 timeout = channel_get_timeout(channel, part);
3524 if (opt.jo_set & JO_TIMEOUT)
3525 timeout = opt.jo_timeout;
3526
3527 if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003528 rettv->vval.v_string = channel_read_block(channel, part,
3529 timeout, raw);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003530 else
3531 {
3532 if (opt.jo_set & JO_ID)
3533 id = opt.jo_id;
3534 channel_read_json_block(channel, part, timeout, id, &listtv);
3535 if (listtv != NULL)
3536 {
3537 *rettv = *listtv;
3538 vim_free(listtv);
3539 }
3540 else
3541 {
3542 rettv->v_type = VAR_SPECIAL;
3543 rettv->vval.v_number = VVAL_NONE;
3544 }
3545 }
3546 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003547
3548theend:
3549 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003550}
3551
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003552# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3553 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003554/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003555 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003556 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003557 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003558 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003559channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003560{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003561 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003562 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003563
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003564 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003565 for (channel = first_channel; channel != NULL;
3566 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003567 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003568 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003569 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003570 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003571 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003572 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003573 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003574 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003575 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003576}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003577# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003578
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003579# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003580/*
3581 * Check the channels for anything that is ready to be read.
3582 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003583 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003584 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003585 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003586channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003587{
3588 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003589 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003590 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003591
3592 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3593 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003594 if (only_keep_open && !channel->ch_keep_open)
3595 continue;
3596
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003597 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003598 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003599 {
3600 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003601 if (fd != INVALID_FD)
3602 {
3603 int r = channel_wait(channel, fd, 0);
3604
3605 if (r == CW_READY)
3606 channel_read(channel, part, "channel_handle_events");
3607 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003608 ch_close_part_on_error(channel, part, TRUE,
3609 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003610 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003611 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003612 }
3613}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003614# endif
3615
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003616# if defined(FEAT_GUI) || defined(PROTO)
3617/*
3618 * Return TRUE when there is any channel with a keep_open flag.
3619 */
3620 int
3621channel_any_keep_open()
3622{
3623 channel_T *channel;
3624
3625 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3626 if (channel->ch_keep_open)
3627 return TRUE;
3628 return FALSE;
3629}
3630# endif
3631
Bram Moolenaard04a0202016-01-26 23:30:18 +01003632/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003633 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003634 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003635 */
3636 void
3637channel_set_nonblock(channel_T *channel, ch_part_T part)
3638{
3639 chanpart_T *ch_part = &channel->ch_part[part];
3640 int fd = ch_part->ch_fd;
3641
3642 if (fd != INVALID_FD)
3643 {
3644#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003645 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003646
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003647 ioctlsocket(fd, FIONBIO, &val);
3648#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003649 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003650#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003651 ch_part->ch_nonblocking = TRUE;
3652 }
3653}
3654
3655/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003656 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003657 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003658 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003659 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003660 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003661channel_send(
3662 channel_T *channel,
3663 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003664 char_u *buf_arg,
3665 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003666 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003667{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003668 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003669 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003670 chanpart_T *ch_part = &channel->ch_part[part];
3671 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003672
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003673 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003674 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003675 {
3676 if (!channel->ch_error && fun != NULL)
3677 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003678 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003679 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003680 }
3681 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003682 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003683 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003684
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003685 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003686 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003687 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003688 fprintf(log_fd, "'");
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003689 ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003690 fprintf(log_fd, "'\n");
3691 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003692 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003693 }
3694
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003695 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003696 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003697 writeq_T *wq = &ch_part->ch_writeque;
3698 char_u *buf;
3699 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003700
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003701 if (wq->wq_next != NULL)
3702 {
3703 /* first write what was queued */
3704 buf = wq->wq_next->wq_ga.ga_data;
3705 len = wq->wq_next->wq_ga.ga_len;
3706 did_use_queue = TRUE;
3707 }
3708 else
3709 {
3710 if (len_arg == 0)
3711 /* nothing to write, called from channel_select_check() */
3712 return OK;
3713 buf = buf_arg;
3714 len = len_arg;
3715 }
3716
3717 if (part == PART_SOCK)
3718 res = sock_write(fd, (char *)buf, len);
3719 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003720 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003721 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003722#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003723 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003724 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003725 DisconnectNamedPipe((HANDLE)fd);
3726 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003727 }
3728#endif
3729
3730 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003731 if (res < 0 && (errno == EWOULDBLOCK
3732#ifdef EAGAIN
3733 || errno == EAGAIN
3734#endif
3735 ))
3736 res = 0; /* nothing got written */
3737
3738 if (res >= 0 && ch_part->ch_nonblocking)
3739 {
3740 writeq_T *entry = wq->wq_next;
3741
3742 if (did_use_queue)
3743 ch_log(channel, "Sent %d bytes now", res);
3744 if (res == len)
3745 {
3746 /* Wrote all the buf[len] bytes. */
3747 if (entry != NULL)
3748 {
3749 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003750 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003751 continue;
3752 }
3753 if (did_use_queue)
3754 ch_log(channel, "Write queue empty");
3755 }
3756 else
3757 {
3758 /* Wrote only buf[res] bytes, can't write more now. */
3759 if (entry != NULL)
3760 {
3761 if (res > 0)
3762 {
3763 /* Remove the bytes that were written. */
3764 mch_memmove(entry->wq_ga.ga_data,
3765 (char *)entry->wq_ga.ga_data + res,
3766 len - res);
3767 entry->wq_ga.ga_len -= res;
3768 }
3769 buf = buf_arg;
3770 len = len_arg;
3771 }
3772 else
3773 {
3774 buf += res;
3775 len -= res;
3776 }
3777 ch_log(channel, "Adding %d bytes to the write queue", len);
3778
3779 /* Append the not written bytes of the argument to the write
3780 * buffer. Limit entries to 4000 bytes. */
3781 if (wq->wq_prev != NULL
3782 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3783 {
3784 writeq_T *last = wq->wq_prev;
3785
3786 /* append to the last entry */
3787 if (ga_grow(&last->wq_ga, len) == OK)
3788 {
3789 mch_memmove((char *)last->wq_ga.ga_data
3790 + last->wq_ga.ga_len,
3791 buf, len);
3792 last->wq_ga.ga_len += len;
3793 }
3794 }
3795 else
3796 {
3797 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3798
3799 if (last != NULL)
3800 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003801 last->wq_prev = wq->wq_prev;
3802 last->wq_next = NULL;
3803 if (wq->wq_prev == NULL)
3804 wq->wq_next = last;
3805 else
3806 wq->wq_prev->wq_next = last;
3807 wq->wq_prev = last;
3808 ga_init2(&last->wq_ga, 1, 1000);
3809 if (ga_grow(&last->wq_ga, len) == OK)
3810 {
3811 mch_memmove(last->wq_ga.ga_data, buf, len);
3812 last->wq_ga.ga_len = len;
3813 }
3814 }
3815 }
3816 }
3817 }
3818 else if (res != len)
3819 {
3820 if (!channel->ch_error && fun != NULL)
3821 {
3822 ch_error(channel, "%s(): write failed", fun);
3823 EMSG2(_("E631: %s(): write failed"), fun);
3824 }
3825 channel->ch_error = TRUE;
3826 return FAIL;
3827 }
3828
3829 channel->ch_error = FALSE;
3830 return OK;
3831 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003832}
3833
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003834/*
3835 * Common for "ch_sendexpr()" and "ch_sendraw()".
3836 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003837 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003838 * Otherwise returns NULL.
3839 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003840 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003841send_common(
3842 typval_T *argvars,
3843 char_u *text,
3844 int id,
3845 int eval,
3846 jobopt_T *opt,
3847 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003848 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003849{
3850 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003851 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003852
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003853 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003854 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003855 if (channel == NULL)
3856 return NULL;
3857 part_send = channel_part_send(channel);
3858 *part_read = channel_part_read(channel);
3859
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003860 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003861 return NULL;
3862
3863 /* Set the callback. An empty callback means no callback and not reading
3864 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3865 * allowed. */
3866 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3867 {
3868 if (eval)
3869 {
3870 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3871 return NULL;
3872 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003873 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003874 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003875 }
3876
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003877 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003878 && opt->jo_callback == NULL)
3879 return channel;
3880 return NULL;
3881}
3882
3883/*
3884 * common for "ch_evalexpr()" and "ch_sendexpr()"
3885 */
3886 void
3887ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3888{
3889 char_u *text;
3890 typval_T *listtv;
3891 channel_T *channel;
3892 int id;
3893 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003894 ch_part_T part_send;
3895 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003896 jobopt_T opt;
3897 int timeout;
3898
3899 /* return an empty string by default */
3900 rettv->v_type = VAR_STRING;
3901 rettv->vval.v_string = NULL;
3902
Bram Moolenaar437905c2016-04-26 19:01:05 +02003903 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003904 if (channel == NULL)
3905 return;
3906 part_send = channel_part_send(channel);
3907
3908 ch_mode = channel_get_mode(channel, part_send);
3909 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3910 {
3911 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3912 return;
3913 }
3914
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003915 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003916 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003917 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003918 if (text == NULL)
3919 return;
3920
3921 channel = send_common(argvars, text, id, eval, &opt,
3922 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3923 vim_free(text);
3924 if (channel != NULL && eval)
3925 {
3926 if (opt.jo_set & JO_TIMEOUT)
3927 timeout = opt.jo_timeout;
3928 else
3929 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003930 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3931 == OK)
3932 {
3933 list_T *list = listtv->vval.v_list;
3934
3935 /* Move the item from the list and then change the type to
3936 * avoid the value being freed. */
3937 *rettv = list->lv_last->li_tv;
3938 list->lv_last->li_tv.v_type = VAR_NUMBER;
3939 free_tv(listtv);
3940 }
3941 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003942 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003943}
3944
3945/*
3946 * common for "ch_evalraw()" and "ch_sendraw()"
3947 */
3948 void
3949ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3950{
3951 char_u buf[NUMBUFLEN];
3952 char_u *text;
3953 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003954 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003955 jobopt_T opt;
3956 int timeout;
3957
3958 /* return an empty string by default */
3959 rettv->v_type = VAR_STRING;
3960 rettv->vval.v_string = NULL;
3961
3962 text = get_tv_string_buf(&argvars[1], buf);
3963 channel = send_common(argvars, text, 0, eval, &opt,
3964 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3965 if (channel != NULL && eval)
3966 {
3967 if (opt.jo_set & JO_TIMEOUT)
3968 timeout = opt.jo_timeout;
3969 else
3970 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003971 rettv->vval.v_string = channel_read_block(channel, part_read,
3972 timeout, TRUE);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003973 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003974 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003975}
3976
Bram Moolenaarf3360612017-10-01 16:21:31 +02003977# define KEEP_OPEN_TIME 20 /* msec */
3978
Bram Moolenaard04a0202016-01-26 23:30:18 +01003979# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003980/*
3981 * Add open channels to the poll struct.
3982 * Return the adjusted struct index.
3983 * The type of "fds" is hidden to avoid problems with the function proto.
3984 */
3985 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02003986channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003987{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003988 int nfd = nfd_in;
3989 channel_T *channel;
3990 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003991 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003992
Bram Moolenaar77073442016-02-13 23:23:53 +01003993 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003994 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003995 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003996 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003997 chanpart_T *ch_part = &channel->ch_part[part];
3998
3999 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004000 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004001 if (channel->ch_keep_open)
4002 {
4003 /* For unknown reason poll() returns immediately for a
4004 * keep-open channel. Instead of adding it to the fds add
4005 * a short timeout and check, like polling. */
4006 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4007 *towait = KEEP_OPEN_TIME;
4008 }
4009 else
4010 {
4011 ch_part->ch_poll_idx = nfd;
4012 fds[nfd].fd = ch_part->ch_fd;
4013 fds[nfd].events = POLLIN;
4014 nfd++;
4015 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004016 }
4017 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004018 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004019 }
4020 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004021
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004022 nfd = channel_fill_poll_write(nfd, fds);
4023
Bram Moolenaare0874f82016-01-24 20:36:41 +01004024 return nfd;
4025}
4026
4027/*
4028 * The type of "fds" is hidden to avoid problems with the function proto.
4029 */
4030 int
4031channel_poll_check(int ret_in, void *fds_in)
4032{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004033 int ret = ret_in;
4034 channel_T *channel;
4035 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004036 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004037 int idx;
4038 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004039
Bram Moolenaar77073442016-02-13 23:23:53 +01004040 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004041 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004042 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004043 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004044 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004045
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004046 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004047 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004048 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004049 --ret;
4050 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004051 else if (channel->ch_part[part].ch_fd != INVALID_FD
4052 && channel->ch_keep_open)
4053 {
4054 /* polling a keep-open channel */
4055 channel_read(channel, part, "channel_poll_check_keep_open");
4056 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004057 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004058
4059 in_part = &channel->ch_part[PART_IN];
4060 idx = in_part->ch_poll_idx;
4061 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4062 {
Bram Moolenaar683b7962017-08-19 15:51:59 +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 Moolenaard04a0202016-01-26 23:30:18 +01004070# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004071
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004072# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004073
Bram Moolenaare0874f82016-01-24 20:36:41 +01004074/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004075 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004076 */
4077 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004078channel_select_setup(
4079 int maxfd_in,
4080 void *rfds_in,
4081 void *wfds_in,
4082 struct timeval *tv,
4083 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004084{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004085 int maxfd = maxfd_in;
4086 channel_T *channel;
4087 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004088 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004089 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004090
Bram Moolenaar77073442016-02-13 23:23:53 +01004091 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004092 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004093 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004094 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004095 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004096
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004097 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004098 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004099 if (channel->ch_keep_open)
4100 {
4101 /* For unknown reason select() returns immediately for a
4102 * keep-open channel. Instead of adding it to the rfds add
4103 * a short timeout and check, like polling. */
4104 if (*tvp == NULL || tv->tv_sec > 0
4105 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4106 {
4107 *tvp = tv;
4108 tv->tv_sec = 0;
4109 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4110 }
4111 }
4112 else
4113 {
4114 FD_SET((int)fd, rfds);
4115 if (maxfd < (int)fd)
4116 maxfd = (int)fd;
4117 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004118 }
4119 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004120 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004121
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004122 maxfd = channel_fill_wfds(maxfd, wfds);
4123
Bram Moolenaare0874f82016-01-24 20:36:41 +01004124 return maxfd;
4125}
4126
4127/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004128 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004129 */
4130 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004131channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004132{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004133 int ret = ret_in;
4134 channel_T *channel;
4135 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004136 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004137 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004138 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004139
Bram Moolenaar77073442016-02-13 23:23:53 +01004140 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004141 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004142 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004143 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004144 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004145
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004146 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004147 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004148 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004149 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004150 --ret;
4151 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004152 else if (fd != INVALID_FD && channel->ch_keep_open)
4153 {
4154 /* polling a keep-open channel */
4155 channel_read(channel, part, "channel_select_check_keep_open");
4156 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004157 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004158
4159 in_part = &channel->ch_part[PART_IN];
4160 if (ret > 0 && in_part->ch_fd != INVALID_FD
4161 && FD_ISSET(in_part->ch_fd, wfds))
4162 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004163 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004164 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004165 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004166 --ret;
4167 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004168 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004169
4170 return ret;
4171}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004172# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004173
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004174/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004175 * Execute queued up commands.
4176 * Invoked from the main loop when it's safe to execute received commands.
4177 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004178 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004179 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004180channel_parse_messages(void)
4181{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004182 channel_T *channel = first_channel;
4183 int ret = FALSE;
4184 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004185 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004186#ifdef ELAPSED_FUNC
4187 ELAPSED_TYPE start_tv;
4188
4189 ELAPSED_INIT(start_tv);
4190#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004191
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004192 ++safe_to_invoke_callback;
4193
Bram Moolenaard0b65022016-03-06 21:50:33 +01004194 /* Only do this message when another message was given, otherwise we get
4195 * lots of them. */
4196 if (did_log_msg)
4197 {
4198 ch_log(NULL, "looking for messages on channels");
4199 did_log_msg = FALSE;
4200 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004201 while (channel != NULL)
4202 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004203 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004204 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004205 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004206 channel_close_now(channel);
4207 /* channel may have been freed, start over */
4208 channel = first_channel;
4209 continue;
4210 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004211 if (channel->ch_to_be_freed)
4212 {
4213 channel_free(channel);
4214 /* channel has been freed, start over */
4215 channel = first_channel;
4216 continue;
4217 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004218 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004219 {
4220 /* channel is no longer useful, free it */
4221 channel_free(channel);
4222 channel = first_channel;
4223 part = PART_SOCK;
4224 continue;
4225 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004226 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004227 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004228 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004229 /* Increase the refcount, in case the handler causes the channel
4230 * to be unreferenced or closed. */
4231 ++channel->ch_refcount;
4232 r = may_invoke_callback(channel, part);
4233 if (r == OK)
4234 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004235 if (channel_unref(channel) || (r == OK
4236#ifdef ELAPSED_FUNC
4237 /* Limit the time we loop here to 100 msec, otherwise
4238 * Vim becomes unresponsive when the callback takes
4239 * more than a bit of time. */
4240 && ELAPSED_FUNC(start_tv) < 100L
4241#endif
4242 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004243 {
4244 /* channel was freed or something was done, start over */
4245 channel = first_channel;
4246 part = PART_SOCK;
4247 continue;
4248 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004249 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004250 if (part < PART_ERR)
4251 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004252 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004253 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004254 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004255 part = PART_SOCK;
4256 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004257 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004258
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004259 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004260 {
4261 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004262 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004263 }
4264
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004265 --safe_to_invoke_callback;
4266
Bram Moolenaard7ece102016-02-02 23:23:02 +01004267 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004268}
4269
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004270/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004271 * Return TRUE if any channel has readahead. That means we should not block on
4272 * waiting for input.
4273 */
4274 int
4275channel_any_readahead(void)
4276{
4277 channel_T *channel = first_channel;
4278 ch_part_T part = PART_SOCK;
4279
4280 while (channel != NULL)
4281 {
4282 if (channel_has_readahead(channel, part))
4283 return TRUE;
4284 if (part < PART_ERR)
4285 ++part;
4286 else
4287 {
4288 channel = channel->ch_next;
4289 part = PART_SOCK;
4290 }
4291 }
4292 return FALSE;
4293}
4294
4295/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004296 * Mark references to lists used in channels.
4297 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004298 int
4299set_ref_in_channel(int copyID)
4300{
Bram Moolenaar77073442016-02-13 23:23:53 +01004301 int abort = FALSE;
4302 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004303 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004304
Bram Moolenaar77073442016-02-13 23:23:53 +01004305 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004306 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004307 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004308 tv.v_type = VAR_CHANNEL;
4309 tv.vval.v_channel = channel;
4310 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004311 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004312 return abort;
4313}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004314
4315/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004316 * Return the "part" to write to for "channel".
4317 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004318 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004319channel_part_send(channel_T *channel)
4320{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004321 if (channel->CH_SOCK_FD == INVALID_FD)
4322 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004323 return PART_SOCK;
4324}
4325
4326/*
4327 * Return the default "part" to read from for "channel".
4328 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004329 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004330channel_part_read(channel_T *channel)
4331{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004332 if (channel->CH_SOCK_FD == INVALID_FD)
4333 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004334 return PART_SOCK;
4335}
4336
4337/*
4338 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004339 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004340 */
4341 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004342channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004343{
Bram Moolenaar77073442016-02-13 23:23:53 +01004344 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004345 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004346 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004347}
4348
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004349/*
4350 * Return the timeout of "channel"/"part"
4351 */
4352 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004353channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004354{
4355 return channel->ch_part[part].ch_timeout;
4356}
4357
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004358 static int
4359handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4360{
4361 char_u *val = get_tv_string(item);
4362
4363 opt->jo_set |= jo;
4364 if (STRCMP(val, "nl") == 0)
4365 *modep = MODE_NL;
4366 else if (STRCMP(val, "raw") == 0)
4367 *modep = MODE_RAW;
4368 else if (STRCMP(val, "js") == 0)
4369 *modep = MODE_JS;
4370 else if (STRCMP(val, "json") == 0)
4371 *modep = MODE_JSON;
4372 else
4373 {
4374 EMSG2(_(e_invarg2), val);
4375 return FAIL;
4376 }
4377 return OK;
4378}
4379
4380 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004381handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004382{
4383 char_u *val = get_tv_string(item);
4384
4385 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4386 if (STRCMP(val, "null") == 0)
4387 opt->jo_io[part] = JIO_NULL;
4388 else if (STRCMP(val, "pipe") == 0)
4389 opt->jo_io[part] = JIO_PIPE;
4390 else if (STRCMP(val, "file") == 0)
4391 opt->jo_io[part] = JIO_FILE;
4392 else if (STRCMP(val, "buffer") == 0)
4393 opt->jo_io[part] = JIO_BUFFER;
4394 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4395 opt->jo_io[part] = JIO_OUT;
4396 else
4397 {
4398 EMSG2(_(e_invarg2), val);
4399 return FAIL;
4400 }
4401 return OK;
4402}
4403
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004404/*
4405 * Clear a jobopt_T before using it.
4406 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004407 void
4408clear_job_options(jobopt_T *opt)
4409{
4410 vim_memset(opt, 0, sizeof(jobopt_T));
4411}
4412
4413/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004414 * Free any members of a jobopt_T.
4415 */
4416 void
4417free_job_options(jobopt_T *opt)
4418{
4419 if (opt->jo_partial != NULL)
4420 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004421 else if (opt->jo_callback != NULL)
4422 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004423 if (opt->jo_out_partial != NULL)
4424 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004425 else if (opt->jo_out_cb != NULL)
4426 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004427 if (opt->jo_err_partial != NULL)
4428 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004429 else if (opt->jo_err_cb != NULL)
4430 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004431 if (opt->jo_close_partial != NULL)
4432 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004433 else if (opt->jo_close_cb != NULL)
4434 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004435 if (opt->jo_exit_partial != NULL)
4436 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004437 else if (opt->jo_exit_cb != NULL)
4438 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004439 if (opt->jo_env != NULL)
4440 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004441}
4442
4443/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004444 * Get the PART_ number from the first character of an option name.
4445 */
4446 static int
4447part_from_char(int c)
4448{
4449 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4450}
4451
4452/*
4453 * Get the option entries from the dict in "tv", parse them and put the result
4454 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004455 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004456 * If an option value is invalid return FAIL.
4457 */
4458 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004459get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004460{
4461 typval_T *item;
4462 char_u *val;
4463 dict_T *dict;
4464 int todo;
4465 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004466 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004467
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004468 if (tv->v_type == VAR_UNKNOWN)
4469 return OK;
4470 if (tv->v_type != VAR_DICT)
4471 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004472 EMSG(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004473 return FAIL;
4474 }
4475 dict = tv->vval.v_dict;
4476 if (dict == NULL)
4477 return OK;
4478
4479 todo = (int)dict->dv_hashtab.ht_used;
4480 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4481 if (!HASHITEM_EMPTY(hi))
4482 {
4483 item = &dict_lookup(hi)->di_tv;
4484
4485 if (STRCMP(hi->hi_key, "mode") == 0)
4486 {
4487 if (!(supported & JO_MODE))
4488 break;
4489 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4490 return FAIL;
4491 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004492 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004493 {
4494 if (!(supported & JO_IN_MODE))
4495 break;
4496 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4497 == FAIL)
4498 return FAIL;
4499 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004500 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004501 {
4502 if (!(supported & JO_OUT_MODE))
4503 break;
4504 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4505 == FAIL)
4506 return FAIL;
4507 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004508 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004509 {
4510 if (!(supported & JO_ERR_MODE))
4511 break;
4512 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4513 == FAIL)
4514 return FAIL;
4515 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004516 else if (STRCMP(hi->hi_key, "in_io") == 0
4517 || STRCMP(hi->hi_key, "out_io") == 0
4518 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004519 {
4520 if (!(supported & JO_OUT_IO))
4521 break;
4522 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4523 return FAIL;
4524 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004525 else if (STRCMP(hi->hi_key, "in_name") == 0
4526 || STRCMP(hi->hi_key, "out_name") == 0
4527 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004528 {
4529 part = part_from_char(*hi->hi_key);
4530
4531 if (!(supported & JO_OUT_IO))
4532 break;
4533 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4534 opt->jo_io_name[part] =
4535 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4536 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004537 else if (STRCMP(hi->hi_key, "pty") == 0)
4538 {
4539 if (!(supported & JO_MODE))
4540 break;
4541 opt->jo_pty = get_tv_number(item);
4542 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004543 else if (STRCMP(hi->hi_key, "in_buf") == 0
4544 || STRCMP(hi->hi_key, "out_buf") == 0
4545 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004546 {
4547 part = part_from_char(*hi->hi_key);
4548
4549 if (!(supported & JO_OUT_IO))
4550 break;
4551 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4552 opt->jo_io_buf[part] = get_tv_number(item);
4553 if (opt->jo_io_buf[part] <= 0)
4554 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004555 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004556 return FAIL;
4557 }
4558 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4559 {
4560 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4561 return FAIL;
4562 }
4563 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004564 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4565 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4566 {
4567 part = part_from_char(*hi->hi_key);
4568
4569 if (!(supported & JO_OUT_IO))
4570 break;
4571 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4572 opt->jo_modifiable[part] = get_tv_number(item);
4573 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004574 else if (STRCMP(hi->hi_key, "out_msg") == 0
4575 || STRCMP(hi->hi_key, "err_msg") == 0)
4576 {
4577 part = part_from_char(*hi->hi_key);
4578
4579 if (!(supported & JO_OUT_IO))
4580 break;
4581 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4582 opt->jo_message[part] = get_tv_number(item);
4583 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004584 else if (STRCMP(hi->hi_key, "in_top") == 0
4585 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004586 {
4587 linenr_T *lp;
4588
4589 if (!(supported & JO_OUT_IO))
4590 break;
4591 if (hi->hi_key[3] == 't')
4592 {
4593 lp = &opt->jo_in_top;
4594 opt->jo_set |= JO_IN_TOP;
4595 }
4596 else
4597 {
4598 lp = &opt->jo_in_bot;
4599 opt->jo_set |= JO_IN_BOT;
4600 }
4601 *lp = get_tv_number(item);
4602 if (*lp < 0)
4603 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004604 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004605 return FAIL;
4606 }
4607 }
4608 else if (STRCMP(hi->hi_key, "channel") == 0)
4609 {
4610 if (!(supported & JO_OUT_IO))
4611 break;
4612 opt->jo_set |= JO_CHANNEL;
4613 if (item->v_type != VAR_CHANNEL)
4614 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004615 EMSG2(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004616 return FAIL;
4617 }
4618 opt->jo_channel = item->vval.v_channel;
4619 }
4620 else if (STRCMP(hi->hi_key, "callback") == 0)
4621 {
4622 if (!(supported & JO_CALLBACK))
4623 break;
4624 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004625 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004626 if (opt->jo_callback == NULL)
4627 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004628 EMSG2(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004629 return FAIL;
4630 }
4631 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004632 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004633 {
4634 if (!(supported & JO_OUT_CALLBACK))
4635 break;
4636 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004637 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004638 if (opt->jo_out_cb == NULL)
4639 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004640 EMSG2(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004641 return FAIL;
4642 }
4643 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004644 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004645 {
4646 if (!(supported & JO_ERR_CALLBACK))
4647 break;
4648 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004649 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004650 if (opt->jo_err_cb == NULL)
4651 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004652 EMSG2(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004653 return FAIL;
4654 }
4655 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004656 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004657 {
4658 if (!(supported & JO_CLOSE_CALLBACK))
4659 break;
4660 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004661 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004662 if (opt->jo_close_cb == NULL)
4663 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004664 EMSG2(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004665 return FAIL;
4666 }
4667 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004668 else if (STRCMP(hi->hi_key, "drop") == 0)
4669 {
4670 int never = FALSE;
4671 val = get_tv_string(item);
4672
4673 if (STRCMP(val, "never") == 0)
4674 never = TRUE;
4675 else if (STRCMP(val, "auto") != 0)
4676 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004677 EMSG3(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004678 return FAIL;
4679 }
4680 opt->jo_drop_never = never;
4681 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004682 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4683 {
4684 if (!(supported & JO_EXIT_CB))
4685 break;
4686 opt->jo_set |= JO_EXIT_CB;
4687 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4688 if (opt->jo_exit_cb == NULL)
4689 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004690 EMSG2(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004691 return FAIL;
4692 }
4693 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004694#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004695 else if (STRCMP(hi->hi_key, "term_name") == 0)
4696 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004697 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004698 break;
4699 opt->jo_set2 |= JO2_TERM_NAME;
4700 opt->jo_term_name = get_tv_string_chk(item);
4701 if (opt->jo_term_name == NULL)
4702 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004703 EMSG2(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004704 return FAIL;
4705 }
4706 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004707 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4708 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004709 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004710 break;
4711 val = get_tv_string(item);
4712 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4713 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004714 EMSG3(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004715 return FAIL;
4716 }
4717 opt->jo_set2 |= JO2_TERM_FINISH;
4718 opt->jo_term_finish = *val;
4719 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004720 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4721 {
4722 char_u *p;
4723
4724 if (!(supported2 & JO2_TERM_OPENCMD))
4725 break;
4726 opt->jo_set2 |= JO2_TERM_OPENCMD;
4727 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4728 if (p != NULL)
4729 {
4730 /* Must have %d and no other %. */
4731 p = vim_strchr(p, '%');
4732 if (p != NULL && (p[1] != 'd'
4733 || vim_strchr(p + 2, '%') != NULL))
4734 p = NULL;
4735 }
4736 if (p == NULL)
4737 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004738 EMSG2(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004739 return FAIL;
4740 }
4741 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004742 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4743 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004744 char_u *p;
4745
4746 if (!(supported2 & JO2_EOF_CHARS))
4747 break;
4748 opt->jo_set2 |= JO2_EOF_CHARS;
4749 p = opt->jo_eof_chars = get_tv_string_chk(item);
4750 if (p == NULL)
4751 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004752 EMSG2(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004753 return FAIL;
4754 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004755 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004756 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4757 {
4758 if (!(supported2 & JO2_TERM_ROWS))
4759 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004760 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004761 opt->jo_term_rows = get_tv_number(item);
4762 }
4763 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4764 {
4765 if (!(supported2 & JO2_TERM_COLS))
4766 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004767 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004768 opt->jo_term_cols = get_tv_number(item);
4769 }
4770 else if (STRCMP(hi->hi_key, "vertical") == 0)
4771 {
4772 if (!(supported2 & JO2_VERTICAL))
4773 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004774 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004775 opt->jo_vertical = get_tv_number(item);
4776 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004777 else if (STRCMP(hi->hi_key, "curwin") == 0)
4778 {
4779 if (!(supported2 & JO2_CURWIN))
4780 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004781 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaarda43b612017-08-11 22:27:50 +02004782 opt->jo_curwin = get_tv_number(item);
4783 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004784 else if (STRCMP(hi->hi_key, "hidden") == 0)
4785 {
4786 if (!(supported2 & JO2_HIDDEN))
4787 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004788 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004789 opt->jo_hidden = get_tv_number(item);
4790 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004791 else if (STRCMP(hi->hi_key, "norestore") == 0)
4792 {
4793 if (!(supported2 & JO2_NORESTORE))
4794 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004795 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004796 opt->jo_term_norestore = get_tv_number(item);
4797 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004798 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4799 {
4800 if (!(supported2 & JO2_TERM_KILL))
4801 break;
4802 opt->jo_set2 |= JO2_TERM_KILL;
4803 opt->jo_term_kill = get_tv_string_chk(item);
4804 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004805#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004806 else if (STRCMP(hi->hi_key, "env") == 0)
4807 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004808 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004809 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004810 if (item->v_type != VAR_DICT)
4811 {
4812 EMSG2(_(e_invargval), "env");
4813 return FAIL;
4814 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004815 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004816 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004817 if (opt->jo_env != NULL)
4818 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004819 }
4820 else if (STRCMP(hi->hi_key, "cwd") == 0)
4821 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004822 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004823 break;
4824 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4825 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4826 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004827 EMSG2(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004828 return FAIL;
4829 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004830 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004831 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004832 else if (STRCMP(hi->hi_key, "waittime") == 0)
4833 {
4834 if (!(supported & JO_WAITTIME))
4835 break;
4836 opt->jo_set |= JO_WAITTIME;
4837 opt->jo_waittime = get_tv_number(item);
4838 }
4839 else if (STRCMP(hi->hi_key, "timeout") == 0)
4840 {
4841 if (!(supported & JO_TIMEOUT))
4842 break;
4843 opt->jo_set |= JO_TIMEOUT;
4844 opt->jo_timeout = get_tv_number(item);
4845 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004846 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004847 {
4848 if (!(supported & JO_OUT_TIMEOUT))
4849 break;
4850 opt->jo_set |= JO_OUT_TIMEOUT;
4851 opt->jo_out_timeout = get_tv_number(item);
4852 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004853 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004854 {
4855 if (!(supported & JO_ERR_TIMEOUT))
4856 break;
4857 opt->jo_set |= JO_ERR_TIMEOUT;
4858 opt->jo_err_timeout = get_tv_number(item);
4859 }
4860 else if (STRCMP(hi->hi_key, "part") == 0)
4861 {
4862 if (!(supported & JO_PART))
4863 break;
4864 opt->jo_set |= JO_PART;
4865 val = get_tv_string(item);
4866 if (STRCMP(val, "err") == 0)
4867 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004868 else if (STRCMP(val, "out") == 0)
4869 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004870 else
4871 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004872 EMSG3(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004873 return FAIL;
4874 }
4875 }
4876 else if (STRCMP(hi->hi_key, "id") == 0)
4877 {
4878 if (!(supported & JO_ID))
4879 break;
4880 opt->jo_set |= JO_ID;
4881 opt->jo_id = get_tv_number(item);
4882 }
4883 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4884 {
4885 if (!(supported & JO_STOPONEXIT))
4886 break;
4887 opt->jo_set |= JO_STOPONEXIT;
4888 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4889 opt->jo_soe_buf);
4890 if (opt->jo_stoponexit == NULL)
4891 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004892 EMSG2(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004893 return FAIL;
4894 }
4895 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004896 else if (STRCMP(hi->hi_key, "block_write") == 0)
4897 {
4898 if (!(supported & JO_BLOCK_WRITE))
4899 break;
4900 opt->jo_set |= JO_BLOCK_WRITE;
4901 opt->jo_block_write = get_tv_number(item);
4902 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004903 else
4904 break;
4905 --todo;
4906 }
4907 if (todo > 0)
4908 {
4909 EMSG2(_(e_invarg2), hi->hi_key);
4910 return FAIL;
4911 }
4912
4913 return OK;
4914}
4915
4916/*
4917 * Get the channel from the argument.
4918 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004919 * When "check_open" is TRUE check that the channel can be used.
4920 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004921 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004922 */
4923 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004924get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004925{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004926 channel_T *channel = NULL;
4927 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004928
4929 if (tv->v_type == VAR_JOB)
4930 {
4931 if (tv->vval.v_job != NULL)
4932 channel = tv->vval.v_job->jv_channel;
4933 }
4934 else if (tv->v_type == VAR_CHANNEL)
4935 {
4936 channel = tv->vval.v_channel;
4937 }
4938 else
4939 {
4940 EMSG2(_(e_invarg2), get_tv_string(tv));
4941 return NULL;
4942 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004943 if (channel != NULL && reading)
4944 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004945 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004946
Bram Moolenaar437905c2016-04-26 19:01:05 +02004947 if (check_open && (channel == NULL || (!channel_is_open(channel)
4948 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004949 {
4950 EMSG(_("E906: not an open channel"));
4951 return NULL;
4952 }
4953 return channel;
4954}
4955
4956static job_T *first_job = NULL;
4957
4958 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004959job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004960{
4961 ch_log(job->jv_channel, "Freeing job");
4962 if (job->jv_channel != NULL)
4963 {
4964 /* The link from the channel to the job doesn't count as a reference,
4965 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004966 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004967 * NULL the reference. We don't set ch_job_killed, unreferencing the
4968 * job doesn't mean it stops running. */
4969 job->jv_channel->ch_job = NULL;
4970 channel_unref(job->jv_channel);
4971 }
4972 mch_clear_job(job);
4973
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02004974 vim_free(job->jv_tty_in);
4975 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004976 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004977 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004978}
4979
4980 static void
4981job_free_job(job_T *job)
4982{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004983 if (job->jv_next != NULL)
4984 job->jv_next->jv_prev = job->jv_prev;
4985 if (job->jv_prev == NULL)
4986 first_job = job->jv_next;
4987 else
4988 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004989 vim_free(job);
4990}
4991
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004992 static void
4993job_free(job_T *job)
4994{
4995 if (!in_free_unref_items)
4996 {
4997 job_free_contents(job);
4998 job_free_job(job);
4999 }
5000}
5001
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005002#if defined(EXITFREE) || defined(PROTO)
5003 void
5004job_free_all(void)
5005{
5006 while (first_job != NULL)
5007 job_free(first_job);
5008}
5009#endif
5010
5011/*
5012 * Return TRUE if we need to check if the process of "job" has ended.
5013 */
5014 static int
5015job_need_end_check(job_T *job)
5016{
5017 return job->jv_status == JOB_STARTED
5018 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5019}
5020
5021/*
5022 * Return TRUE if the channel of "job" is still useful.
5023 */
5024 static int
5025job_channel_still_useful(job_T *job)
5026{
5027 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5028}
5029
5030/*
5031 * Return TRUE if the job should not be freed yet. Do not free the job when
5032 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5033 * or when the associated channel will do something with the job output.
5034 */
5035 static int
5036job_still_useful(job_T *job)
5037{
5038 return job_need_end_check(job) || job_channel_still_useful(job);
5039}
5040
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005041#if !defined(USE_ARGV) || defined(PROTO)
5042/*
5043 * Escape one argument for an external command.
5044 * Returns the escaped string in allocated memory. NULL when out of memory.
5045 */
5046 static char_u *
5047win32_escape_arg(char_u *arg)
5048{
5049 int slen, dlen;
5050 int escaping = 0;
5051 int i;
5052 char_u *s, *d;
5053 char_u *escaped_arg;
5054 int has_spaces = FALSE;
5055
5056 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005057 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005058 dlen = slen;
5059 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5060 {
5061 if (*s == '"' || *s == '\\')
5062 ++dlen;
5063 if (*s == ' ' || *s == '\t')
5064 has_spaces = TRUE;
5065 }
5066
5067 if (has_spaces)
5068 dlen += 2;
5069
5070 if (dlen == slen)
5071 return vim_strsave(arg);
5072
5073 /* Allocate memory for the result and fill it. */
5074 escaped_arg = alloc(dlen + 1);
5075 if (escaped_arg == NULL)
5076 return NULL;
5077 memset(escaped_arg, 0, dlen+1);
5078
5079 d = escaped_arg;
5080
5081 if (has_spaces)
5082 *d++ = '"';
5083
5084 for (s = arg; *s != NUL;)
5085 {
5086 switch (*s)
5087 {
5088 case '"':
5089 for (i = 0; i < escaping; i++)
5090 *d++ = '\\';
5091 escaping = 0;
5092 *d++ = '\\';
5093 *d++ = *s++;
5094 break;
5095 case '\\':
5096 escaping++;
5097 *d++ = *s++;
5098 break;
5099 default:
5100 escaping = 0;
5101 MB_COPY_CHAR(s, d);
5102 break;
5103 }
5104 }
5105
5106 /* add terminating quote and finish with a NUL */
5107 if (has_spaces)
5108 {
5109 for (i = 0; i < escaping; i++)
5110 *d++ = '\\';
5111 *d++ = '"';
5112 }
5113 *d = NUL;
5114
5115 return escaped_arg;
5116}
5117
5118/*
5119 * Build a command line from a list, taking care of escaping.
5120 * The result is put in gap->ga_data.
5121 * Returns FAIL when out of memory.
5122 */
5123 int
5124win32_build_cmd(list_T *l, garray_T *gap)
5125{
5126 listitem_T *li;
5127 char_u *s;
5128
5129 for (li = l->lv_first; li != NULL; li = li->li_next)
5130 {
5131 s = get_tv_string_chk(&li->li_tv);
5132 if (s == NULL)
5133 return FAIL;
5134 s = win32_escape_arg(s);
5135 if (s == NULL)
5136 return FAIL;
5137 ga_concat(gap, s);
5138 vim_free(s);
5139 if (li->li_next != NULL)
5140 ga_append(gap, ' ');
5141 }
5142 return OK;
5143}
5144#endif
5145
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005146/*
5147 * NOTE: Must call job_cleanup() only once right after the status of "job"
5148 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5149 * mch_detect_ended_job() returned non-NULL).
5150 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005151 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005152job_cleanup(job_T *job)
5153{
5154 if (job->jv_status != JOB_ENDED)
5155 return;
5156
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005157 /* Ready to cleanup the job. */
5158 job->jv_status = JOB_FINISHED;
5159
Bram Moolenaar97792de2016-10-15 18:36:49 +02005160 if (job->jv_exit_cb != NULL)
5161 {
5162 typval_T argv[3];
5163 typval_T rettv;
5164 int dummy;
5165
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005166 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005167 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005168 ++job->jv_refcount;
5169 argv[0].v_type = VAR_JOB;
5170 argv[0].vval.v_job = job;
5171 argv[1].v_type = VAR_NUMBER;
5172 argv[1].vval.v_number = job->jv_exitval;
5173 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5174 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5175 job->jv_exit_partial, NULL);
5176 clear_tv(&rettv);
5177 --job->jv_refcount;
5178 channel_need_redraw = TRUE;
5179 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005180
5181 /* Do not free the job in case the close callback of the associated channel
5182 * isn't invoked yet and may get information by job_info(). */
5183 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005184 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005185 /* The job was already unreferenced and the associated channel was
5186 * detached, now that it ended it can be freed. Careful: caller must
5187 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005188 job_free(job);
5189 }
5190}
5191
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005192/*
5193 * Mark references in jobs that are still useful.
5194 */
5195 int
5196set_ref_in_job(int copyID)
5197{
5198 int abort = FALSE;
5199 job_T *job;
5200 typval_T tv;
5201
5202 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005203 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005204 {
5205 tv.v_type = VAR_JOB;
5206 tv.vval.v_job = job;
5207 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5208 }
5209 return abort;
5210}
5211
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005212/*
5213 * Dereference "job". Note that after this "job" may have been freed.
5214 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005215 void
5216job_unref(job_T *job)
5217{
5218 if (job != NULL && --job->jv_refcount <= 0)
5219 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005220 /* Do not free the job if there is a channel where the close callback
5221 * may get the job info. */
5222 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005223 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005224 /* Do not free the job when it has not ended yet and there is a
5225 * "stoponexit" flag or an exit callback. */
5226 if (!job_need_end_check(job))
5227 {
5228 job_free(job);
5229 }
5230 else if (job->jv_channel != NULL)
5231 {
5232 /* Do remove the link to the channel, otherwise it hangs
5233 * around until Vim exits. See job_free() for refcount. */
5234 ch_log(job->jv_channel, "detaching channel from job");
5235 job->jv_channel->ch_job = NULL;
5236 channel_unref(job->jv_channel);
5237 job->jv_channel = NULL;
5238 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005239 }
5240 }
5241}
5242
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005243 int
5244free_unused_jobs_contents(int copyID, int mask)
5245{
5246 int did_free = FALSE;
5247 job_T *job;
5248
5249 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005250 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005251 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005252 {
5253 /* Free the channel and ordinary items it contains, but don't
5254 * recurse into Lists, Dictionaries etc. */
5255 job_free_contents(job);
5256 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005257 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005258 return did_free;
5259}
5260
5261 void
5262free_unused_jobs(int copyID, int mask)
5263{
5264 job_T *job;
5265 job_T *job_next;
5266
5267 for (job = first_job; job != NULL; job = job_next)
5268 {
5269 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005270 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005271 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005272 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005273 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005274 job_free_job(job);
5275 }
5276 }
5277}
5278
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005279/*
5280 * Allocate a job. Sets the refcount to one and sets options default.
5281 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005282 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005283job_alloc(void)
5284{
5285 job_T *job;
5286
5287 job = (job_T *)alloc_clear(sizeof(job_T));
5288 if (job != NULL)
5289 {
5290 job->jv_refcount = 1;
5291 job->jv_stoponexit = vim_strsave((char_u *)"term");
5292
5293 if (first_job != NULL)
5294 {
5295 first_job->jv_prev = job;
5296 job->jv_next = first_job;
5297 }
5298 first_job = job;
5299 }
5300 return job;
5301}
5302
5303 void
5304job_set_options(job_T *job, jobopt_T *opt)
5305{
5306 if (opt->jo_set & JO_STOPONEXIT)
5307 {
5308 vim_free(job->jv_stoponexit);
5309 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5310 job->jv_stoponexit = NULL;
5311 else
5312 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5313 }
5314 if (opt->jo_set & JO_EXIT_CB)
5315 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005316 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005317 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005318 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005319 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005320 job->jv_exit_partial = NULL;
5321 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005322 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005323 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005324 job->jv_exit_partial = opt->jo_exit_partial;
5325 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005326 {
5327 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005328 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005329 }
5330 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005331 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005332 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005333 func_ref(job->jv_exit_cb);
5334 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005335 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005336 }
5337}
5338
5339/*
5340 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5341 */
5342 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005343job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005344{
5345 job_T *job;
5346
5347 for (job = first_job; job != NULL; job = job->jv_next)
5348 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005349 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005350}
5351
5352/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005353 * Return TRUE when there is any job that has an exit callback and might exit,
5354 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005355 */
5356 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005357has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005358{
5359 job_T *job;
5360
5361 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005362 /* Only should check if the channel has been closed, if the channel is
5363 * open the job won't exit. */
5364 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005365 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005366 return TRUE;
5367 return FALSE;
5368}
5369
Bram Moolenaar97792de2016-10-15 18:36:49 +02005370#define MAX_CHECK_ENDED 8
5371
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005372/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005373 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005374 */
5375 void
5376job_check_ended(void)
5377{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005378 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005379
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005380 if (first_job == NULL)
5381 return;
5382
Bram Moolenaar97792de2016-10-15 18:36:49 +02005383 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005384 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005385 /* NOTE: mch_detect_ended_job() must only return a job of which the
5386 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005387 job_T *job = mch_detect_ended_job(first_job);
5388
5389 if (job == NULL)
5390 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005391 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005392 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005393
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005394 if (channel_need_redraw)
5395 {
5396 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005397 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005398 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005399}
5400
5401/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005402 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005403 * "argv_arg" is only for Unix.
5404 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005405 * The returned job has a refcount of one.
5406 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005407 */
5408 job_T *
Bram Moolenaar13568252018-03-16 20:46:58 +01005409job_start(typval_T *argvars, char **argv_arg, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005410{
5411 job_T *job;
5412 char_u *cmd = NULL;
5413#if defined(UNIX)
5414# define USE_ARGV
5415 char **argv = NULL;
5416 int argc = 0;
5417#else
5418 garray_T ga;
5419#endif
5420 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005421 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005422
5423 job = job_alloc();
5424 if (job == NULL)
5425 return NULL;
5426
5427 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005428#ifndef USE_ARGV
5429 ga_init2(&ga, (int)sizeof(char*), 20);
5430#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005431
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005432 if (opt_arg != NULL)
5433 opt = *opt_arg;
5434 else
5435 {
5436 /* Default mode is NL. */
5437 clear_job_options(&opt);
5438 opt.jo_mode = MODE_NL;
5439 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005440 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5441 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5442 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005443 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005444 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005445
5446 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005447 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005448 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5449 && opt.jo_io[part] == JIO_FILE
5450 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5451 || *opt.jo_io_name[part] == NUL))
5452 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005453 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005454 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005455 }
5456
5457 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5458 {
5459 buf_T *buf = NULL;
5460
5461 /* check that we can find the buffer before starting the job */
5462 if (opt.jo_set & JO_IN_BUF)
5463 {
5464 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5465 if (buf == NULL)
5466 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5467 }
5468 else if (!(opt.jo_set & JO_IN_NAME))
5469 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005470 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005471 }
5472 else
5473 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5474 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005475 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005476 if (buf->b_ml.ml_mfp == NULL)
5477 {
5478 char_u numbuf[NUMBUFLEN];
5479 char_u *s;
5480
5481 if (opt.jo_set & JO_IN_BUF)
5482 {
5483 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5484 s = numbuf;
5485 }
5486 else
5487 s = opt.jo_io_name[PART_IN];
5488 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005489 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005490 }
5491 job->jv_in_buf = buf;
5492 }
5493
5494 job_set_options(job, &opt);
5495
Bram Moolenaar13568252018-03-16 20:46:58 +01005496#ifdef USE_ARGV
5497 if (argv_arg != NULL)
5498 {
5499 argv = argv_arg;
5500 }
5501 else
5502#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005503 if (argvars[0].v_type == VAR_STRING)
5504 {
5505 /* Command is a string. */
5506 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005507 if (cmd == NULL || *cmd == NUL)
5508 {
5509 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005510 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005511 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005512#ifdef USE_ARGV
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005513 /* This will modify "cmd". */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005514 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005515 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005516 argv[argc] = NULL;
5517#endif
5518 }
5519 else if (argvars[0].v_type != VAR_LIST
5520 || argvars[0].vval.v_list == NULL
5521 || argvars[0].vval.v_list->lv_len < 1)
5522 {
5523 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005524 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005525 }
5526 else
5527 {
5528 list_T *l = argvars[0].vval.v_list;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005529#ifdef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005530 listitem_T *li;
5531 char_u *s;
5532
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005533 /* Pass argv[] to mch_call_shell(). */
5534 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5535 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005536 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005537 for (li = l->lv_first; li != NULL; li = li->li_next)
5538 {
5539 s = get_tv_string_chk(&li->li_tv);
5540 if (s == NULL)
5541 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005542 argv[argc++] = (char *)s;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005543 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005544 argv[argc] = NULL;
5545#else
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005546 if (win32_build_cmd(l, &ga) == FAIL)
5547 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005548 cmd = ga.ga_data;
5549#endif
5550 }
5551
5552#ifdef USE_ARGV
5553 if (ch_log_active())
5554 {
5555 garray_T ga;
5556 int i;
5557
5558 ga_init2(&ga, (int)sizeof(char), 200);
5559 for (i = 0; i < argc; ++i)
5560 {
5561 if (i > 0)
5562 ga_concat(&ga, (char_u *)" ");
5563 ga_concat(&ga, (char_u *)argv[i]);
5564 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005565 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005566 ga_clear(&ga);
5567 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005568 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005569#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005570 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005571 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005572#endif
5573
5574 /* If the channel is reading from a buffer, write lines now. */
5575 if (job->jv_channel != NULL)
5576 channel_write_in(job->jv_channel);
5577
5578theend:
5579#ifdef USE_ARGV
Bram Moolenaar13568252018-03-16 20:46:58 +01005580 if (argv != argv_arg)
5581 vim_free(argv);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005582#else
5583 vim_free(ga.ga_data);
5584#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005585 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005586 return job;
5587}
5588
5589/*
5590 * Get the status of "job" and invoke the exit callback when needed.
5591 * The returned string is not allocated.
5592 */
5593 char *
5594job_status(job_T *job)
5595{
5596 char *result;
5597
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005598 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005599 /* No need to check, dead is dead. */
5600 result = "dead";
5601 else if (job->jv_status == JOB_FAILED)
5602 result = "fail";
5603 else
5604 {
5605 result = mch_job_status(job);
5606 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005607 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005608 }
5609 return result;
5610}
5611
Bram Moolenaar8950a562016-03-12 15:22:55 +01005612/*
5613 * Implementation of job_info().
5614 */
5615 void
5616job_info(job_T *job, dict_T *dict)
5617{
5618 dictitem_T *item;
5619 varnumber_T nr;
5620
5621 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5622
5623 item = dictitem_alloc((char_u *)"channel");
5624 if (item == NULL)
5625 return;
5626 item->di_tv.v_lock = 0;
5627 item->di_tv.v_type = VAR_CHANNEL;
5628 item->di_tv.vval.v_channel = job->jv_channel;
5629 if (job->jv_channel != NULL)
5630 ++job->jv_channel->ch_refcount;
5631 if (dict_add(dict, item) == FAIL)
5632 dictitem_free(item);
5633
5634#ifdef UNIX
5635 nr = job->jv_pid;
5636#else
5637 nr = job->jv_proc_info.dwProcessId;
5638#endif
5639 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005640 dict_add_nr_str(dict, "tty_in", 0L,
5641 job->jv_tty_in != NULL ? job->jv_tty_in : (char_u *)"");
5642 dict_add_nr_str(dict, "tty_out", 0L,
5643 job->jv_tty_out != NULL ? job->jv_tty_out : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005644
5645 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005646 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005647 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5648}
5649
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005650/*
5651 * Send a signal to "job". Implements job_stop().
5652 * When "type" is not NULL use this for the type.
5653 * Otherwise use argvars[1] for the type.
5654 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005655 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005656job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005657{
5658 char_u *arg;
5659
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005660 if (type != NULL)
5661 arg = (char_u *)type;
5662 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005663 arg = (char_u *)"";
5664 else
5665 {
5666 arg = get_tv_string_chk(&argvars[1]);
5667 if (arg == NULL)
5668 {
5669 EMSG(_(e_invarg));
5670 return 0;
5671 }
5672 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005673 if (job->jv_status == JOB_FAILED)
5674 {
5675 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5676 return 0;
5677 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005678 if (job->jv_status == JOB_ENDED)
5679 {
5680 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5681 return 0;
5682 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005683 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005684 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005685 return 0;
5686
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005687 /* Assume that only "kill" will kill the job. */
5688 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005689 job->jv_channel->ch_job_killed = TRUE;
5690
5691 /* We don't try freeing the job, obviously the caller still has a
5692 * reference to it. */
5693 return 1;
5694}
5695
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005696#endif /* FEAT_JOB_CHANNEL */