blob: 38e639224c9380d2f0f4dbdafb07d4c480bd1848 [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 Moolenaar81661fb2016-02-18 22:23:34 +0100161 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200162ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100163{
164 if (log_fd != NULL)
165 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200166 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100167
Bram Moolenaar77073442016-02-13 23:23:53 +0100168 ch_log_lead("", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200169 va_start(ap, fmt);
170 vfprintf(log_fd, fmt, ap);
171 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100173 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100174 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100175 }
176}
177
178 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200179ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100180{
181 if (log_fd != NULL)
182 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200183 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100184
Bram Moolenaar77073442016-02-13 23:23:53 +0100185 ch_log_lead("ERR ", ch);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200186 va_start(ap, fmt);
187 vfprintf(log_fd, fmt, ap);
188 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100189 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100190 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100191 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 }
193}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100194
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100195#ifdef _WIN32
196# undef PERROR
197# define PERROR(msg) (void)emsg3((char_u *)"%s: %s", \
198 (char_u *)msg, (char_u *)strerror_win32(errno))
199
200 static char *
201strerror_win32(int eno)
202{
203 static LPVOID msgbuf = NULL;
204 char_u *ptr;
205
206 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200207 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100208 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200209 msgbuf = NULL;
210 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100211 FormatMessage(
212 FORMAT_MESSAGE_ALLOCATE_BUFFER |
213 FORMAT_MESSAGE_FROM_SYSTEM |
214 FORMAT_MESSAGE_IGNORE_INSERTS,
215 NULL,
216 eno,
217 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
218 (LPTSTR) &msgbuf,
219 0,
220 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200221 if (msgbuf != NULL)
222 /* chomp \r or \n */
223 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
224 switch (*ptr)
225 {
226 case '\r':
227 STRMOVE(ptr, ptr + 1);
228 ptr--;
229 break;
230 case '\n':
231 if (*(ptr + 1) == '\0')
232 *ptr = '\0';
233 else
234 *ptr = ' ';
235 break;
236 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100237 return msgbuf;
238}
239#endif
240
Bram Moolenaar77073442016-02-13 23:23:53 +0100241/*
242 * The list of all allocated channels.
243 */
244static channel_T *first_channel = NULL;
245static int next_ch_id = 0;
246
247/*
248 * Allocate a new channel. The refcount is set to 1.
249 * The channel isn't actually used until it is opened.
250 * Returns NULL if out of memory.
251 */
252 channel_T *
253add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100254{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200255 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100256 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100257
Bram Moolenaar77073442016-02-13 23:23:53 +0100258 if (channel == NULL)
259 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100260
Bram Moolenaar77073442016-02-13 23:23:53 +0100261 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100262 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100263
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200264 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100265 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100266 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100267#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100268 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100269#endif
270#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100271 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100272#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100273 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100274 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100275
Bram Moolenaar77073442016-02-13 23:23:53 +0100276 if (first_channel != NULL)
277 {
278 first_channel->ch_prev = channel;
279 channel->ch_next = first_channel;
280 }
281 first_channel = channel;
282
283 channel->ch_refcount = 1;
284 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100285}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100286
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200287 int
288has_any_channel(void)
289{
290 return first_channel != NULL;
291}
292
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100293/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100294 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100295 * Return TRUE if "channel" has a callback and the associated job wasn't
296 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100297 */
298 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100299channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100300{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100301 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100302 int has_out_msg;
303 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100304
305 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100306 if (channel->ch_job_killed && channel->ch_job == NULL)
307 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100308
309 /* If there is a close callback it may still need to be invoked. */
310 if (channel->ch_close_cb != NULL)
311 return TRUE;
312
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200313 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200314 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200315 return TRUE;
316
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100317 /* If there is no callback then nobody can get readahead. If the fd is
318 * closed and there is no readahead then the callback won't be called. */
319 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100320 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
321 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100322 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
323 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
324 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
325 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
326 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
327 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100328 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100329 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200330 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
332 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200333 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200334 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
335 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100336}
337
338/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200339 * Close a channel and free all its resources.
340 */
341 static void
342channel_free_contents(channel_T *channel)
343{
344 channel_close(channel, TRUE);
345 channel_clear(channel);
346 ch_log(channel, "Freeing channel");
347}
348
349 static void
350channel_free_channel(channel_T *channel)
351{
352 if (channel->ch_next != NULL)
353 channel->ch_next->ch_prev = channel->ch_prev;
354 if (channel->ch_prev == NULL)
355 first_channel = channel->ch_next;
356 else
357 channel->ch_prev->ch_next = channel->ch_next;
358 vim_free(channel);
359}
360
361 static void
362channel_free(channel_T *channel)
363{
364 if (!in_free_unref_items)
365 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200366 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200367 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200368 else
369 {
370 channel_free_contents(channel);
371 channel_free_channel(channel);
372 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200373 }
374}
375
376/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100378 * possible, there is no callback to be invoked or the associated job was
379 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100380 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100381 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100382 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100383channel_may_free(channel_T *channel)
384{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100385 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100386 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100387 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100388 return TRUE;
389 }
390 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100391}
392
393/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100394 * Decrement the reference count on "channel" and maybe free it when it goes
395 * down to zero. Don't free it if there is a pending action.
396 * Returns TRUE when the channel is no longer referenced.
397 */
398 int
399channel_unref(channel_T *channel)
400{
401 if (channel != NULL && --channel->ch_refcount <= 0)
402 return channel_may_free(channel);
403 return FALSE;
404}
405
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200406 int
407free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100408{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200409 int did_free = FALSE;
410 channel_T *ch;
411
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200412 /* This is invoked from the garbage collector, which only runs at a safe
413 * point. */
414 ++safe_to_invoke_callback;
415
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200416 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200417 if (!channel_still_useful(ch)
418 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200419 {
420 /* Free the channel and ordinary items it contains, but don't
421 * recurse into Lists, Dictionaries etc. */
422 channel_free_contents(ch);
423 did_free = TRUE;
424 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200425
426 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200427 return did_free;
428}
429
430 void
431free_unused_channels(int copyID, int mask)
432{
433 channel_T *ch;
434 channel_T *ch_next;
435
436 for (ch = first_channel; ch != NULL; ch = ch_next)
437 {
438 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200439 if (!channel_still_useful(ch)
440 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200441 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200442 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200443 channel_free_channel(ch);
444 }
445 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100446}
447
Bram Moolenaard04a0202016-01-26 23:30:18 +0100448#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100449
450#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
451 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100452channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100453{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100454 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200455 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100456
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100457 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100458 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200459 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100460 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200461 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100462}
463#endif
464
Bram Moolenaare0874f82016-01-24 20:36:41 +0100465/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100466 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100467 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100468#ifdef FEAT_GUI_X11
469 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200470messageFromServer(XtPointer clientData,
471 int *unused1 UNUSED,
472 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100473{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100474 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100475}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100476#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100477
Bram Moolenaard04a0202016-01-26 23:30:18 +0100478#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100479# if GTK_CHECK_VERSION(3,0,0)
480 static gboolean
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200481messageFromServer(GIOChannel *unused1 UNUSED,
482 GIOCondition unused2 UNUSED,
483 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100484{
485 channel_read_fd(GPOINTER_TO_INT(clientData));
486 return TRUE; /* Return FALSE instead in case the event source is to
487 * be removed after this function returns. */
488}
489# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100490 static void
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200491messageFromServer(gpointer clientData,
492 gint unused1 UNUSED,
493 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100494{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100495 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
Bram Moolenaar98921892016-02-23 17:14:37 +0100497# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100498#endif
499
500 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200501channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100502{
Bram Moolenaarde279892016-03-11 22:19:44 +0100503 if (!CH_HAS_GUI)
504 return;
505
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200506 /* gets stuck in handling events for a not connected channel */
507 if (channel->ch_keep_open)
508 return;
509
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100510# ifdef FEAT_GUI_X11
511 /* Tell notifier we are interested in being called
512 * when there is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100513 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
514 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100515 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100516 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100517 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200518 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100519 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100520# else
521# ifdef FEAT_GUI_GTK
522 /* Tell gdk we are interested in being called when there
523 * is input on the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100524 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100525# if GTK_CHECK_VERSION(3,0,0)
526 {
527 GIOChannel *chnnl = g_io_channel_unix_new(
528 (gint)channel->ch_part[part].ch_fd);
529
530 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
531 chnnl,
532 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200533 messageFromServer,
Bram Moolenaar98921892016-02-23 17:14:37 +0100534 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
535
536 g_io_channel_unref(chnnl);
537 }
538# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100539 channel->ch_part[part].ch_inputHandler = gdk_input_add(
540 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100541 (GdkInputCondition)
542 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200543 messageFromServer,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100544 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100545# endif
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100546# endif
547# endif
548}
549
Bram Moolenaarde279892016-03-11 22:19:44 +0100550 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100551channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100552{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100553 if (channel->CH_SOCK_FD != INVALID_FD)
554 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200555 if (channel->CH_OUT_FD != INVALID_FD
556 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100557 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200558 if (channel->CH_ERR_FD != INVALID_FD
559 && channel->CH_ERR_FD != channel->CH_SOCK_FD
560 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100561 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100562}
563
564/*
565 * Register any of our file descriptors with the GUI event handling system.
566 * Called when the GUI has started.
567 */
568 void
569channel_gui_register_all(void)
570{
Bram Moolenaar77073442016-02-13 23:23:53 +0100571 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100572
Bram Moolenaar77073442016-02-13 23:23:53 +0100573 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100574 channel_gui_register(channel);
575}
576
577 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200578channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100579{
580# ifdef FEAT_GUI_X11
581 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
582 {
583 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
584 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
585 }
586# else
587# ifdef FEAT_GUI_GTK
588 if (channel->ch_part[part].ch_inputHandler != 0)
589 {
590# if GTK_CHECK_VERSION(3,0,0)
591 g_source_remove(channel->ch_part[part].ch_inputHandler);
592# else
593 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
594# endif
595 channel->ch_part[part].ch_inputHandler = 0;
596 }
597# endif
598# endif
599}
600
601 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100602channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100603{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200604 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100605
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100606 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100607 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100608}
609
610#endif
611
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100612static char *e_cannot_connect = N_("E902: Cannot connect to port");
613
Bram Moolenaard04a0202016-01-26 23:30:18 +0100614/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100615 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100616 * "waittime" is the time in msec to wait for the connection.
617 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100618 * Returns the channel for success.
619 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100620 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100621 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100622channel_open(
623 char *hostname,
624 int port_in,
625 int waittime,
626 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100627{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100628 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100629 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100630 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100631#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100632 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100633 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100634#else
635 int port = port_in;
636#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100637 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100638 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100639
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100640#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100641 channel_init_winsock();
642#endif
643
Bram Moolenaar77073442016-02-13 23:23:53 +0100644 channel = add_channel();
645 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100646 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100647 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100648 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100649 }
650
651 /* Get the server internet address and put into addr structure */
652 /* fill in the socket address structure and connect to server */
653 vim_memset((char *)&server, 0, sizeof(server));
654 server.sin_family = AF_INET;
655 server.sin_port = htons(port);
656 if ((host = gethostbyname(hostname)) == NULL)
657 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100658 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200659 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100660 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100661 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100662 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100663 {
664 char *p;
665
666 /* When using host->h_addr directly ubsan warns for it to not be
667 * aligned. First copy the pointer to aviod that. */
668 memcpy(&p, &host->h_addr, sizeof(p));
669 memcpy((char *)&server.sin_addr, p, host->h_length);
670 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100672 /* On Mac and Solaris a zero timeout almost never works. At least wait
673 * one millisecond. Let's do it for all systems, because we don't know why
674 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100675 if (waittime == 0)
676 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100677
678 /*
679 * For Unix we need to call connect() again after connect() failed.
680 * On Win32 one time is sufficient.
681 */
682 while (TRUE)
683 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100684 long elapsed_msec = 0;
685 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100686
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100688 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100689 sd = socket(AF_INET, SOCK_STREAM, 0);
690 if (sd == -1)
691 {
692 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200693 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100694 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100695 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100697
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100698 if (waittime >= 0)
699 {
700 /* Make connect() non-blocking. */
701 if (
702#ifdef _WIN32
703 ioctlsocket(sd, FIONBIO, &val) < 0
704#else
705 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
706#endif
707 )
708 {
709 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200710 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100711 "channel_open: Connect failed with errno %d", errno);
712 sock_close(sd);
713 channel_free(channel);
714 return NULL;
715 }
716 }
717
718 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200719 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100720 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
721
Bram Moolenaar045a2842016-03-08 22:33:07 +0100722 if (ret == 0)
723 /* The connection could be established. */
724 break;
725
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100726 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100727 if (waittime < 0 || (errno != EWOULDBLOCK
728 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100729#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100730 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100731#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100732 ))
733 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200734 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100735 "channel_open: Connect failed with errno %d", errno);
736 PERROR(_(e_cannot_connect));
737 sock_close(sd);
738 channel_free(channel);
739 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100740 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100741
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100742 /* Limit the waittime to 50 msec. If it doesn't work within this
743 * time we close the socket and try creating it again. */
744 waitnow = waittime > 50 ? 50 : waittime;
745
Bram Moolenaar045a2842016-03-08 22:33:07 +0100746 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100747 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100748#ifndef WIN32
749 if (errno != ECONNREFUSED)
750#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100751 {
752 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100753 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100754 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100755#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100756 int so_error = 0;
757 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100758 struct timeval start_tv;
759 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100760#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100761 FD_ZERO(&rfds);
762 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 FD_ZERO(&wfds);
764 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100765
Bram Moolenaar562ca712016-03-09 21:50:05 +0100766 tv.tv_sec = waitnow / 1000;
767 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100768#ifndef WIN32
769 gettimeofday(&start_tv, NULL);
770#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200771 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100772 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100773 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100774
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100775 if (ret < 0)
776 {
777 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200778 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100779 "channel_open: Connect failed with errno %d", errno);
780 PERROR(_(e_cannot_connect));
781 sock_close(sd);
782 channel_free(channel);
783 return NULL;
784 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100785
Bram Moolenaare081e212016-02-28 22:33:46 +0100786#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100787 /* On Win32: select() is expected to work and wait for up to
788 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 if (FD_ISSET(sd, &wfds))
790 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100791 elapsed_msec = waitnow;
792 if (waittime > 1 && elapsed_msec < waittime)
793 {
794 waittime -= elapsed_msec;
795 continue;
796 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100797#else
798 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100799 * After putting the socket in non-blocking mode, connect() will
800 * return EINPROGRESS, select() will not wait (as if writing is
801 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100802 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100803 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100804 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100805 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100806 {
807 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100809 if (ret < 0 || (so_error != 0
810 && so_error != EWOULDBLOCK
811 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100812# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100813 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100814# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 ))
816 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200817 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100818 "channel_open: Connect failed with errno %d",
819 so_error);
820 PERROR(_(e_cannot_connect));
821 sock_close(sd);
822 channel_free(channel);
823 return NULL;
824 }
825 }
826
Bram Moolenaar045a2842016-03-08 22:33:07 +0100827 if (FD_ISSET(sd, &wfds) && so_error == 0)
828 /* Did not detect an error, connection is established. */
829 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100830
Bram Moolenaar045a2842016-03-08 22:33:07 +0100831 gettimeofday(&end_tv, NULL);
832 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
833 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100834#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100835 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100836
837#ifndef WIN32
838 if (waittime > 1 && elapsed_msec < waittime)
839 {
840 /* The port isn't ready but we also didn't get an error.
841 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100842 * yet. Select() may return early, wait until the remaining
843 * "waitnow" and try again. */
844 waitnow -= elapsed_msec;
845 waittime -= elapsed_msec;
846 if (waitnow > 0)
847 {
848 mch_delay((long)waitnow, TRUE);
849 ui_breakcheck();
850 waittime -= waitnow;
851 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100852 if (!got_int)
853 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100854 if (waittime <= 0)
855 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100856 waittime = 1;
857 continue;
858 }
859 /* we were interrupted, behave as if timed out */
860 }
861#endif
862
863 /* We timed out. */
864 ch_error(channel, "Connection timed out");
865 sock_close(sd);
866 channel_free(channel);
867 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100868 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100869
Bram Moolenaar045a2842016-03-08 22:33:07 +0100870 ch_log(channel, "Connection made");
871
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100872 if (waittime >= 0)
873 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100874#ifdef _WIN32
875 val = 0;
876 ioctlsocket(sd, FIONBIO, &val);
877#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100878 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100879#endif
880 }
881
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100882 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100883 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100884 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
885 channel->ch_port = port_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200886 channel->ch_to_be_closed |= (1 << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100887
888#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100889 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100890#endif
891
Bram Moolenaar77073442016-02-13 23:23:53 +0100892 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100893}
894
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100895/*
896 * Implements ch_open().
897 */
898 channel_T *
899channel_open_func(typval_T *argvars)
900{
901 char_u *address;
902 char_u *p;
903 char *rest;
904 int port;
905 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200906 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100907
908 address = get_tv_string(&argvars[0]);
909 if (argvars[1].v_type != VAR_UNKNOWN
910 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
911 {
912 EMSG(_(e_invarg));
913 return NULL;
914 }
915
916 /* parse address */
917 p = vim_strchr(address, ':');
918 if (p == NULL)
919 {
920 EMSG2(_(e_invarg2), address);
921 return NULL;
922 }
923 *p++ = NUL;
924 port = strtol((char *)p, &rest, 10);
925 if (*address == NUL || port <= 0 || *rest != NUL)
926 {
927 p[-1] = ':';
928 EMSG2(_(e_invarg2), address);
929 return NULL;
930 }
931
932 /* parse options */
933 clear_job_options(&opt);
934 opt.jo_mode = MODE_JSON;
935 opt.jo_timeout = 2000;
936 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200937 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200938 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939 if (opt.jo_timeout < 0)
940 {
941 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200942 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100943 }
944
945 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
946 if (channel != NULL)
947 {
948 opt.jo_set = JO_ALL;
949 channel_set_options(channel, &opt);
950 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200951theend:
952 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100953 return channel;
954}
955
Bram Moolenaarde279892016-03-11 22:19:44 +0100956 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200957ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100958{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200959 sock_T *fd = &channel->ch_part[part].ch_fd;
960
Bram Moolenaarde279892016-03-11 22:19:44 +0100961 if (*fd != INVALID_FD)
962 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200963 if (part == PART_SOCK)
964 sock_close(*fd);
965 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200966 {
967 /* When using a pty the same FD is set on multiple parts, only
968 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200969 if ((part == PART_IN || channel->CH_IN_FD != *fd)
970 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
971 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200972 {
973#ifdef WIN32
974 if (channel->ch_named_pipe)
975 DisconnectNamedPipe((HANDLE)fd);
976#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200977 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200978 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200979 }
Bram Moolenaarde279892016-03-11 22:19:44 +0100980 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200981
982 channel->ch_to_be_closed &= ~(1 << part);
Bram Moolenaarde279892016-03-11 22:19:44 +0100983 }
984}
985
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100986 void
Bram Moolenaard8070362016-02-15 21:56:54 +0100987channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100988{
Bram Moolenaarde279892016-03-11 22:19:44 +0100989 if (in != INVALID_FD)
990 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200991 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +0100992 channel->CH_IN_FD = in;
993 }
994 if (out != INVALID_FD)
995 {
996# if defined(FEAT_GUI)
997 channel_gui_unregister_one(channel, PART_OUT);
998# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200999 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001000 channel->CH_OUT_FD = out;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001001 channel->ch_to_be_closed |= (1 << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001002# if defined(FEAT_GUI)
1003 channel_gui_register_one(channel, PART_OUT);
1004# endif
1005 }
1006 if (err != INVALID_FD)
1007 {
1008# if defined(FEAT_GUI)
1009 channel_gui_unregister_one(channel, PART_ERR);
1010# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001011 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001012 channel->CH_ERR_FD = err;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001013 channel->ch_to_be_closed |= (1 << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001014# if defined(FEAT_GUI)
1015 channel_gui_register_one(channel, PART_ERR);
1016# endif
1017 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001018}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001019
Bram Moolenaard6051b52016-02-28 15:49:03 +01001020/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001021 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001022 * This does not keep a refcount, when the job is freed ch_job is cleared.
1023 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001024 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001025channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001026{
Bram Moolenaar77073442016-02-13 23:23:53 +01001027 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001028
1029 channel_set_options(channel, options);
1030
1031 if (job->jv_in_buf != NULL)
1032 {
1033 chanpart_T *in_part = &channel->ch_part[PART_IN];
1034
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001035 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001036 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001037 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001038 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001039 {
1040 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1041 {
1042 /* Special mode: send last-but-one line when appending a line
1043 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001044 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001045 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001046 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001047 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001048 }
1049 else
1050 in_part->ch_buf_top = options->jo_in_top;
1051 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001052 else
1053 in_part->ch_buf_top = 1;
1054 if (options->jo_set & JO_IN_BOT)
1055 in_part->ch_buf_bot = options->jo_in_bot;
1056 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001057 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001058 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001059}
1060
1061/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001062 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001063 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001064 */
1065 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001066find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001067{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001068 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001069 buf_T *save_curbuf = curbuf;
1070
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001071 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001072 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001073 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001074 if (buf == NULL)
1075 buf = buflist_findname_exp(name);
1076 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001077 if (buf == NULL)
1078 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001079 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001080 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001081 if (buf == NULL)
1082 return NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001083 buf_copy_options(buf, BCO_ENTER);
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001084 curbuf = buf;
Bram Moolenaar187db502016-02-27 14:44:26 +01001085#ifdef FEAT_QUICKFIX
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001086 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1087 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
Bram Moolenaar187db502016-02-27 14:44:26 +01001088#endif
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001089 if (curbuf->b_ml.ml_mfp == NULL)
1090 ml_open(curbuf);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001091 if (msg)
1092 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001093 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001094 changed_bytes(1, 0);
1095 curbuf = save_curbuf;
1096 }
1097
1098 return buf;
1099}
1100
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001101 static void
1102set_callback(
1103 char_u **cbp,
1104 partial_T **pp,
1105 char_u *callback,
1106 partial_T *partial)
1107{
1108 free_callback(*cbp, *pp);
1109 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001110 {
1111 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001112 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001113 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001114 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001115 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001116 func_ref(*cbp);
1117 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001118 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001119 else
1120 *cbp = NULL;
1121 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001122 if (partial != NULL)
1123 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001124}
1125
Bram Moolenaar187db502016-02-27 14:44:26 +01001126/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001127 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001128 */
1129 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001130channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001131{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001132 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001133
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001134 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001135 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001136 channel->ch_part[part].ch_mode = opt->jo_mode;
1137 if (opt->jo_set & JO_IN_MODE)
1138 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1139 if (opt->jo_set & JO_OUT_MODE)
1140 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1141 if (opt->jo_set & JO_ERR_MODE)
1142 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001143
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001144 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001145 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001146 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1147 if (opt->jo_set & JO_OUT_TIMEOUT)
1148 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1149 if (opt->jo_set & JO_ERR_TIMEOUT)
1150 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001151 if (opt->jo_set & JO_BLOCK_WRITE)
1152 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001153
1154 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001155 set_callback(&channel->ch_callback, &channel->ch_partial,
1156 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001157 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001158 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1159 &channel->ch_part[PART_OUT].ch_partial,
1160 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001161 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001162 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1163 &channel->ch_part[PART_ERR].ch_partial,
1164 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001165 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001166 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1167 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001168 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001169
1170 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1171 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001172 buf_T *buf;
1173
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001174 /* writing output to a buffer. Default mode is NL. */
1175 if (!(opt->jo_set & JO_OUT_MODE))
1176 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001177 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001178 {
1179 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1180 if (buf == NULL)
1181 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1182 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001183 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001184 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001185 int msg = TRUE;
1186
1187 if (opt->jo_set2 & JO2_OUT_MSG)
1188 msg = opt->jo_message[PART_OUT];
1189 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001190 }
1191 if (buf != NULL)
1192 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001193 if (opt->jo_set & JO_OUT_MODIFIABLE)
1194 channel->ch_part[PART_OUT].ch_nomodifiable =
1195 !opt->jo_modifiable[PART_OUT];
1196
1197 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1198 {
1199 EMSG(_(e_modifiable));
1200 }
1201 else
1202 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001203 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001204 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001205 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001206 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001207 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001208 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001209
1210 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1211 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1212 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1213 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001214 buf_T *buf;
1215
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001216 /* writing err to a buffer. Default mode is NL. */
1217 if (!(opt->jo_set & JO_ERR_MODE))
1218 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1219 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001220 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001221 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001222 {
1223 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1224 if (buf == NULL)
1225 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1226 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001227 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001228 {
1229 int msg = TRUE;
1230
1231 if (opt->jo_set2 & JO2_ERR_MSG)
1232 msg = opt->jo_message[PART_ERR];
1233 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1234 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001235 if (buf != NULL)
1236 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001237 if (opt->jo_set & JO_ERR_MODIFIABLE)
1238 channel->ch_part[PART_ERR].ch_nomodifiable =
1239 !opt->jo_modifiable[PART_ERR];
1240 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1241 {
1242 EMSG(_(e_modifiable));
1243 }
1244 else
1245 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001246 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001247 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001248 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001249 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001250 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001251 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001252
1253 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1254 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1255 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001256}
1257
1258/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001259 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001260 */
1261 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001262channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001263 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001264 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001265 char_u *callback,
1266 partial_T *partial,
1267 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001268{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001269 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001270 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1271
1272 if (item != NULL)
1273 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001274 item->cq_partial = partial;
1275 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001276 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001277 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001278 item->cq_callback = callback;
1279 }
1280 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001281 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001282 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001283 func_ref(item->cq_callback);
1284 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001285 item->cq_seq_nr = id;
1286 item->cq_prev = head->cq_prev;
1287 head->cq_prev = item;
1288 item->cq_next = NULL;
1289 if (item->cq_prev == NULL)
1290 head->cq_next = item;
1291 else
1292 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001293 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001294}
1295
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001296 static void
1297write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1298{
1299 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001300 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001301 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001302 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001303
Bram Moolenaar655da312016-05-28 22:22:34 +02001304 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001305 if ((p = alloc(len + 2)) == NULL)
1306 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001307 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001308
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001309 if (channel->ch_write_text_mode)
1310 p[len] = CAR;
1311 else
1312 {
1313 for (i = 0; i < len; ++i)
1314 if (p[i] == NL)
1315 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001316
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001317 p[len] = NL;
1318 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001319 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001320 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001321 vim_free(p);
1322}
1323
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001324/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001325 * Return TRUE if "channel" can be written to.
1326 * Returns FALSE if the input is closed or the write would block.
1327 */
1328 static int
1329can_write_buf_line(channel_T *channel)
1330{
1331 chanpart_T *in_part = &channel->ch_part[PART_IN];
1332
1333 if (in_part->ch_fd == INVALID_FD)
1334 return FALSE; /* pipe was closed */
1335
1336 /* for testing: block every other attempt to write */
1337 if (in_part->ch_block_write == 1)
1338 in_part->ch_block_write = -1;
1339 else if (in_part->ch_block_write == -1)
1340 in_part->ch_block_write = 1;
1341
1342 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1343#ifndef WIN32
1344 {
1345# if defined(HAVE_SELECT)
1346 struct timeval tval;
1347 fd_set wfds;
1348 int ret;
1349
1350 FD_ZERO(&wfds);
1351 FD_SET((int)in_part->ch_fd, &wfds);
1352 tval.tv_sec = 0;
1353 tval.tv_usec = 0;
1354 for (;;)
1355 {
1356 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1357# ifdef EINTR
1358 SOCK_ERRNO;
1359 if (ret == -1 && errno == EINTR)
1360 continue;
1361# endif
1362 if (ret <= 0 || in_part->ch_block_write == 1)
1363 {
1364 if (ret > 0)
1365 ch_log(channel, "FAKED Input not ready for writing");
1366 else
1367 ch_log(channel, "Input not ready for writing");
1368 return FALSE;
1369 }
1370 break;
1371 }
1372# else
1373 struct pollfd fds;
1374
1375 fds.fd = in_part->ch_fd;
1376 fds.events = POLLOUT;
1377 if (poll(&fds, 1, 0) <= 0)
1378 {
1379 ch_log(channel, "Input not ready for writing");
1380 return FALSE;
1381 }
1382 if (in_part->ch_block_write == 1)
1383 {
1384 ch_log(channel, "FAKED Input not ready for writing");
1385 return FALSE;
1386 }
1387# endif
1388 }
1389#endif
1390 return TRUE;
1391}
1392
1393/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001394 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001395 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001396 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001397channel_write_in(channel_T *channel)
1398{
1399 chanpart_T *in_part = &channel->ch_part[PART_IN];
1400 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001401 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001402 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001403
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001404 if (buf == NULL || in_part->ch_buf_append)
1405 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001406 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001407 {
1408 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001409 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001410 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001411 return;
1412 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001413
1414 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1415 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1416 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001417 if (!can_write_buf_line(channel))
1418 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001419 write_buf_line(buf, lnum, channel);
1420 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001421 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001422
1423 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001424 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001425 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001426 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001427
Bram Moolenaar014069a2016-03-03 22:51:40 +01001428 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001429 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001430 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001431#if defined(FEAT_TERMINAL)
1432 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001433 if (channel->ch_job != NULL)
1434 term_send_eof(channel);
1435#endif
1436
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001437 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001438 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001439 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001440
1441 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001442 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001443 }
1444 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001445 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001446 buf->b_ml.ml_line_count - lnum + 1);
1447}
1448
1449/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001450 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001451 */
1452 void
1453channel_buffer_free(buf_T *buf)
1454{
1455 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001456 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001457
1458 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001459 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001460 {
1461 chanpart_T *ch_part = &channel->ch_part[part];
1462
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001463 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001464 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001465 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001466 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001467 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001468 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001469 }
1470}
1471
1472/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001473 * Write any lines waiting to be written to "channel".
1474 */
1475 static void
1476channel_write_input(channel_T *channel)
1477{
1478 chanpart_T *in_part = &channel->ch_part[PART_IN];
1479
1480 if (in_part->ch_writeque.wq_next != NULL)
1481 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1482 else if (in_part->ch_bufref.br_buf != NULL)
1483 {
1484 if (in_part->ch_buf_append)
1485 channel_write_new_lines(in_part->ch_bufref.br_buf);
1486 else
1487 channel_write_in(channel);
1488 }
1489}
1490
1491/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001492 * Write any lines waiting to be written to a channel.
1493 */
1494 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001495channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001496{
1497 channel_T *channel;
1498
1499 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001500 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001501}
1502
1503/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 * Write appended lines above the last one in "buf" to the channel.
1505 */
1506 void
1507channel_write_new_lines(buf_T *buf)
1508{
1509 channel_T *channel;
1510 int found_one = FALSE;
1511
1512 /* There could be more than one channel for the buffer, loop over all of
1513 * them. */
1514 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1515 {
1516 chanpart_T *in_part = &channel->ch_part[PART_IN];
1517 linenr_T lnum;
1518 int written = 0;
1519
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001520 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001521 {
1522 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001523 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001524 found_one = TRUE;
1525 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1526 ++lnum)
1527 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001528 if (!can_write_buf_line(channel))
1529 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530 write_buf_line(buf, lnum, channel);
1531 ++written;
1532 }
1533
1534 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001535 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001536 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001537 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001538 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001539 ch_log(channel, "Still %d more lines to write",
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001540 buf->b_ml.ml_line_count - lnum);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001541
1542 in_part->ch_buf_bot = lnum;
1543 }
1544 }
1545 if (!found_one)
1546 buf->b_write_to_channel = FALSE;
1547}
1548
1549/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001550 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001551 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001552 */
1553 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001554invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1555 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001556{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001557 typval_T rettv;
1558 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001559
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001560 if (safe_to_invoke_callback == 0)
Bram Moolenaarde330112017-01-08 20:50:52 +01001561 IEMSG("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001562
Bram Moolenaar77073442016-02-13 23:23:53 +01001563 argv[0].v_type = VAR_CHANNEL;
1564 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001565
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001566 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1567 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001568 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001569 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001570}
1571
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001572/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001573 * Return the first node from "channel"/"part" without removing it.
1574 * Returns NULL if there is nothing.
1575 */
1576 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001577channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001578{
1579 readq_T *head = &channel->ch_part[part].ch_head;
1580
1581 return head->rq_next;
1582}
1583
1584/*
1585 * Return a pointer to the first NL in "node".
1586 * Skips over NUL characters.
1587 * Returns NULL if there is no NL.
1588 */
1589 char_u *
1590channel_first_nl(readq_T *node)
1591{
1592 char_u *buffer = node->rq_buffer;
1593 long_u i;
1594
1595 for (i = 0; i < node->rq_buflen; ++i)
1596 if (buffer[i] == NL)
1597 return buffer + i;
1598 return NULL;
1599}
1600
1601/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001602 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001603 * The caller must free it.
1604 * Returns NULL if there is nothing.
1605 */
1606 char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001607channel_get(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001608{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001609 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001610 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001611 char_u *p;
1612
Bram Moolenaar77073442016-02-13 23:23:53 +01001613 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001614 return NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001615 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001616 p = node->rq_buffer;
1617 head->rq_next = node->rq_next;
1618 if (node->rq_next == NULL)
1619 head->rq_prev = NULL;
1620 else
1621 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001622 vim_free(node);
1623 return p;
1624}
1625
1626/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001627 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001628 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001629 */
1630 static char_u *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001631channel_get_all(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001632{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001633 readq_T *head = &channel->ch_part[part].ch_head;
1634 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001635 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001636 char_u *res;
1637 char_u *p;
1638
1639 /* If there is only one buffer just get that one. */
1640 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
1641 return channel_get(channel, part);
1642
1643 /* Concatenate everything into one buffer. */
1644 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001645 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001646 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001647 if (res == NULL)
1648 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001649 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001650 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001651 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001652 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001653 p += node->rq_buflen;
1654 }
1655 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001656
1657 /* Free all buffers */
1658 do
1659 {
1660 p = channel_get(channel, part);
1661 vim_free(p);
1662 } while (p != NULL);
1663
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001664 /* turn all NUL into NL */
1665 while (len > 0)
1666 {
1667 --len;
1668 if (res[len] == NUL)
1669 res[len] = NL;
1670 }
1671
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001672 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001673}
1674
1675/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001676 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001677 * Caller must check these bytes are available.
1678 */
1679 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001680channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001681{
1682 readq_T *head = &channel->ch_part[part].ch_head;
1683 readq_T *node = head->rq_next;
1684 char_u *buf = node->rq_buffer;
1685
1686 mch_memmove(buf, buf + len, node->rq_buflen - len);
1687 node->rq_buflen -= len;
1688}
1689
1690/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001691 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001692 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001693 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001694 */
1695 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001696channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001697{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001698 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001699 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001700 readq_T *last_node;
1701 readq_T *n;
1702 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001704 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001705
Bram Moolenaar77073442016-02-13 23:23:53 +01001706 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001707 return FAIL;
1708
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001709 last_node = node->rq_next;
1710 len = node->rq_buflen + last_node->rq_buflen + 1;
1711 if (want_nl)
1712 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001713 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001714 {
1715 last_node = last_node->rq_next;
1716 len += last_node->rq_buflen;
1717 }
1718
1719 p = newbuf = alloc(len);
1720 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001721 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001722 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001723 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001724 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 node->rq_buffer = newbuf;
1726 for (n = node; n != last_node; )
1727 {
1728 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001729 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001730 p += n->rq_buflen;
1731 vim_free(n->rq_buffer);
1732 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001733 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001734
1735 /* dispose of the collapsed nodes and their buffers */
1736 for (n = node->rq_next; n != last_node; )
1737 {
1738 n = n->rq_next;
1739 vim_free(n->rq_prev);
1740 }
1741 node->rq_next = last_node->rq_next;
1742 if (last_node->rq_next == NULL)
1743 head->rq_prev = node;
1744 else
1745 last_node->rq_next->rq_prev = node;
1746 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001747 return OK;
1748}
1749
1750/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001751 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001752 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001753 * Returns OK or FAIL.
1754 */
1755 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001756channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001757 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001758{
1759 readq_T *node;
1760 readq_T *head = &channel->ch_part[part].ch_head;
1761 char_u *p;
1762 int i;
1763
1764 node = (readq_T *)alloc(sizeof(readq_T));
1765 if (node == NULL)
1766 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001767 /* A NUL is added at the end, because netbeans code expects that.
1768 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001769 node->rq_buffer = alloc(len + 1);
1770 if (node->rq_buffer == NULL)
1771 {
1772 vim_free(node);
1773 return FAIL; /* out of memory */
1774 }
1775
1776 if (channel->ch_part[part].ch_mode == MODE_NL)
1777 {
1778 /* Drop any CR before a NL. */
1779 p = node->rq_buffer;
1780 for (i = 0; i < len; ++i)
1781 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1782 *p++ = buf[i];
1783 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001784 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001785 }
1786 else
1787 {
1788 mch_memmove(node->rq_buffer, buf, len);
1789 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001790 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001791 }
1792
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001793 if (prepend)
1794 {
1795 /* preend node to the head of the queue */
1796 node->rq_next = head->rq_next;
1797 node->rq_prev = NULL;
1798 if (head->rq_next == NULL)
1799 head->rq_prev = node;
1800 else
1801 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001803 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001804 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001805 {
1806 /* append node to the tail of the queue */
1807 node->rq_next = NULL;
1808 node->rq_prev = head->rq_prev;
1809 if (head->rq_prev == NULL)
1810 head->rq_next = node;
1811 else
1812 head->rq_prev->rq_next = node;
1813 head->rq_prev = node;
1814 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001815
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001816 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001817 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001818 ch_log_lead(lead, channel);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001819 fprintf(log_fd, "'");
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001820 ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001821 fprintf(log_fd, "'\n");
1822 }
1823 return OK;
1824}
1825
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001826/*
1827 * Try to fill the buffer of "reader".
1828 * Returns FALSE when nothing was added.
1829 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001830 static int
1831channel_fill(js_read_T *reader)
1832{
1833 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001834 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001835 char_u *next = channel_get(channel, part);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001836 int keeplen;
1837 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001838 char_u *p;
1839
1840 if (next == NULL)
1841 return FALSE;
1842
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001843 keeplen = reader->js_end - reader->js_buf;
1844 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001845 {
1846 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001847 addlen = (int)STRLEN(next);
1848 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001849 if (p == NULL)
1850 {
1851 vim_free(next);
1852 return FALSE;
1853 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001854 mch_memmove(p, reader->js_buf, keeplen);
1855 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001856 vim_free(next);
1857 next = p;
1858 }
1859
1860 vim_free(reader->js_buf);
1861 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 return TRUE;
1863}
1864
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001865/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001866 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001867 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001868 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001869 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001870 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001871channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001872{
1873 js_read_T reader;
1874 typval_T listtv;
1875 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001876 chanpart_T *chanpart = &channel->ch_part[part];
1877 jsonq_T *head = &chanpart->ch_json_head;
1878 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001879 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001880
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001881 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001882 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001883
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001884 reader.js_buf = channel_get(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001885 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001886 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001887 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001888 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001889
1890 /* When a message is incomplete we wait for a short while for more to
1891 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001892 * or list will make us hang.
1893 * Do not generate error messages, they will be written in a channel log. */
1894 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001895 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001896 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001897 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001898 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001899 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001900 /* Only accept the response when it is a list with at least two
1901 * items. */
1902 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001903 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001904 if (listtv.v_type != VAR_LIST)
1905 ch_error(channel, "Did not receive a list, discarding");
1906 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001907 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001908 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001909 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001910 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001911 else
1912 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001913 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1914 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001915 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001916 else
1917 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001918 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001919 item->jq_value = alloc_tv();
1920 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001921 {
1922 vim_free(item);
1923 clear_tv(&listtv);
1924 }
1925 else
1926 {
Bram Moolenaar77073442016-02-13 23:23:53 +01001927 *item->jq_value = listtv;
1928 item->jq_prev = head->jq_prev;
1929 head->jq_prev = item;
1930 item->jq_next = NULL;
1931 if (item->jq_prev == NULL)
1932 head->jq_next = item;
1933 else
1934 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001935 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001936 }
1937 }
1938 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01001939
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001940 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001941 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001942 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001943 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001944 size_t buflen = STRLEN(reader.js_buf);
1945
1946 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001947 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001948 /* First time encountering incomplete message or after receiving
1949 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001950 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001951 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001952 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001953 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001954 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001955#ifdef WIN32
1956 chanpart->ch_deadline = GetTickCount() + 100L;
1957#else
1958 gettimeofday(&chanpart->ch_deadline, NULL);
1959 chanpart->ch_deadline.tv_usec += 100 * 1000;
1960 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
1961 {
1962 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
1963 ++chanpart->ch_deadline.tv_sec;
1964 }
1965#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001966 }
1967 else
1968 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969 int timeout;
1970#ifdef WIN32
1971 timeout = GetTickCount() > chanpart->ch_deadline;
1972#else
1973 {
1974 struct timeval now_tv;
1975
1976 gettimeofday(&now_tv, NULL);
1977 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
1978 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
1979 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
1980 }
1981#endif
1982 if (timeout)
1983 {
1984 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001985 chanpart->ch_wait_len = 0;
1986 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001987 }
1988 else
1989 {
1990 reader.js_used = 0;
1991 ch_log(channel, "still waiting on incomplete message");
1992 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01001993 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01001994 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001995
1996 if (status == FAIL)
1997 {
1998 ch_error(channel, "Decoding failed - discarding input");
1999 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002000 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002001 }
2002 else if (reader.js_buf[reader.js_used] != NUL)
2003 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002004 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002005 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002006 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2007 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002008 ret = status == MAYBE ? FALSE: TRUE;
2009 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002010 else
2011 ret = FALSE;
2012
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002013 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002014 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002015}
2016
2017/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002018 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002019 */
2020 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002021remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002022{
Bram Moolenaar77073442016-02-13 23:23:53 +01002023 if (node->cq_prev == NULL)
2024 head->cq_next = node->cq_next;
2025 else
2026 node->cq_prev->cq_next = node->cq_next;
2027 if (node->cq_next == NULL)
2028 head->cq_prev = node->cq_prev;
2029 else
2030 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002031}
2032
2033/*
2034 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002035 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002036 */
2037 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002038remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002039{
Bram Moolenaar77073442016-02-13 23:23:53 +01002040 if (node->jq_prev == NULL)
2041 head->jq_next = node->jq_next;
2042 else
2043 node->jq_prev->jq_next = node->jq_next;
2044 if (node->jq_next == NULL)
2045 head->jq_prev = node->jq_prev;
2046 else
2047 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002048 vim_free(node);
2049}
2050
2051/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002052 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002053 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002054 * When "id" is zero or negative jut get the first message. But not the one
2055 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002056 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002057 * Return OK when found and return the value in "rettv".
2058 * Return FAIL otherwise.
2059 */
2060 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002061channel_get_json(
2062 channel_T *channel,
2063 ch_part_T part,
2064 int id,
2065 int without_callback,
2066 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002067{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002068 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002069 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002070
Bram Moolenaar77073442016-02-13 23:23:53 +01002071 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002072 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002073 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002074 typval_T *tv = &l->lv_first->li_tv;
2075
Bram Moolenaar958dc692016-12-01 15:34:12 +01002076 if ((without_callback || !item->jq_no_callback)
2077 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002078 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002079 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002080 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002081 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002082 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002083 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002084 ch_log(channel, "Getting JSON message %d", tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002085 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002086 return OK;
2087 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002088 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002089 }
2090 return FAIL;
2091}
2092
Bram Moolenaar958dc692016-12-01 15:34:12 +01002093/*
2094 * Put back "rettv" into the JSON queue, there was no callback for it.
2095 * Takes over the values in "rettv".
2096 */
2097 static void
2098channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2099{
2100 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2101 jsonq_T *item = head->jq_next;
2102 jsonq_T *newitem;
2103
2104 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2105 /* last item was pushed back, append to the end */
2106 item = NULL;
2107 else while (item != NULL && item->jq_no_callback)
2108 /* append after the last item that was pushed back */
2109 item = item->jq_next;
2110
2111 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2112 if (newitem == NULL)
2113 clear_tv(rettv);
2114 else
2115 {
2116 newitem->jq_value = alloc_tv();
2117 if (newitem->jq_value == NULL)
2118 {
2119 vim_free(newitem);
2120 clear_tv(rettv);
2121 }
2122 else
2123 {
2124 newitem->jq_no_callback = FALSE;
2125 *newitem->jq_value = *rettv;
2126 if (item == NULL)
2127 {
2128 /* append to the end */
2129 newitem->jq_prev = head->jq_prev;
2130 head->jq_prev = newitem;
2131 newitem->jq_next = NULL;
2132 if (newitem->jq_prev == NULL)
2133 head->jq_next = newitem;
2134 else
2135 newitem->jq_prev->jq_next = newitem;
2136 }
2137 else
2138 {
2139 /* append after "item" */
2140 newitem->jq_prev = item;
2141 newitem->jq_next = item->jq_next;
2142 item->jq_next = newitem;
2143 if (newitem->jq_next == NULL)
2144 head->jq_prev = newitem;
2145 else
2146 newitem->jq_next->jq_prev = newitem;
2147 }
2148 }
2149 }
2150}
2151
Bram Moolenaarece61b02016-02-20 21:39:05 +01002152#define CH_JSON_MAX_ARGS 4
2153
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002154/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002155 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002156 * "argv[0]" is the command string.
2157 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002158 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002159 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002160channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002161{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002162 char_u *cmd = argv[0].vval.v_string;
2163 char_u *arg;
2164 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002165
Bram Moolenaarece61b02016-02-20 21:39:05 +01002166 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002167 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002168 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002169 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002170 EMSG(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002171 return;
2172 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002173 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002174 if (arg == NULL)
2175 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002176
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002177 if (STRCMP(cmd, "ex") == 0)
2178 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002179 int save_called_emsg = called_emsg;
2180
2181 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002182 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002183 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002184 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002185 --emsg_silent;
2186 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002187 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002188 (char *)get_vim_var_str(VV_ERRMSG));
2189 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002190 }
2191 else if (STRCMP(cmd, "normal") == 0)
2192 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002193 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002194
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002195 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002196 ea.arg = arg;
2197 ea.addr_count = 0;
2198 ea.forceit = TRUE; /* no mapping */
2199 ex_normal(&ea);
2200 }
2201 else if (STRCMP(cmd, "redraw") == 0)
2202 {
2203 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002204
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002205 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002206 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002207 ex_redraw(&ea);
2208 showruler(FALSE);
2209 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002210 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002211 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002212 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002213 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002214 int is_call = cmd[0] == 'c';
2215 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002216
Bram Moolenaarece61b02016-02-20 21:39:05 +01002217 if (argv[id_idx].v_type != VAR_UNKNOWN
2218 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002219 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002220 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002221 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002222 EMSG(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002223 }
2224 else if (is_call && argv[2].v_type != VAR_LIST)
2225 {
2226 ch_error(channel, "third argument for call must be a list");
2227 if (p_verbose > 2)
Bram Moolenaar5b302912016-08-24 22:11:55 +02002228 EMSG(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002229 }
2230 else
2231 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002232 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002233 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002234 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002235 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002236
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002237 /* Don't pollute the display with errors. */
2238 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002239 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002240 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002241 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002242 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002243 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002244 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002245 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002246 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002247 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2248 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002249 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002250
2251 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002252 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002253 int id = argv[id_idx].vval.v_number;
2254
Bram Moolenaar55fab432016-02-07 16:53:13 +01002255 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002256 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002257 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002258 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002259 /* If evaluation failed or the result can't be encoded
2260 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002261 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002262 err_tv.v_type = VAR_STRING;
2263 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002264 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002265 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002266 if (json != NULL)
2267 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002268 channel_send(channel,
2269 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002270 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002271 vim_free(json);
2272 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002273 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002274 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002275 if (tv == &res_tv)
2276 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002277 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002278 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002279 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002280 }
2281 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002282 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002283 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaar5b302912016-08-24 22:11:55 +02002284 EMSG2(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002285 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002286}
2287
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002288/*
2289 * Invoke the callback at "cbhead".
2290 * Does not redraw but sets channel_need_redraw.
2291 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002292 static void
2293invoke_one_time_callback(
2294 channel_T *channel,
2295 cbq_T *cbhead,
2296 cbq_T *item,
2297 typval_T *argv)
2298{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002299 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002300 (char *)item->cq_callback);
2301 /* Remove the item from the list first, if the callback
2302 * invokes ch_close() the list will be cleared. */
2303 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002304 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002305 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002306 vim_free(item);
2307}
2308
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002309 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002310append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002311{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002312 bufref_T save_curbuf = {NULL, 0, 0};
2313 win_T *save_curwin = NULL;
2314 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002315 linenr_T lnum = buffer->b_ml.ml_line_count;
2316 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002317 chanpart_T *ch_part = &channel->ch_part[part];
2318 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002319 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002320
2321 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2322 {
2323 if (!ch_part->ch_nomod_error)
2324 {
2325 ch_error(channel, "Buffer is not modifiable, cannot append");
2326 ch_part->ch_nomod_error = TRUE;
2327 }
2328 return;
2329 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002330
2331 /* If the buffer is also used as input insert above the last
2332 * line. Don't write these lines. */
2333 if (save_write_to)
2334 {
2335 --lnum;
2336 buffer->b_write_to_channel = FALSE;
2337 }
2338
2339 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002340 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002341
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002342 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002343
2344 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2345 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2346
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002347 u_sync(TRUE);
2348 /* ignore undo failure, undo is not very useful here */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002349 ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002350
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002351 if (empty)
2352 {
2353 /* The buffer is empty, replace the first (dummy) line. */
2354 ml_replace(lnum, msg, TRUE);
2355 lnum = 0;
2356 }
2357 else
2358 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002359 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002360
2361 /* Restore curbuf/curwin/curtab */
2362 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2363
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002364 if (ch_part->ch_nomodifiable)
2365 buffer->b_p_ma = FALSE;
2366 else
2367 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002368
2369 if (buffer->b_nwindows > 0)
2370 {
2371 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002372
2373 FOR_ALL_WINDOWS(wp)
2374 {
2375 if (wp->w_buffer == buffer
2376 && (save_write_to
2377 ? wp->w_cursor.lnum == lnum + 1
2378 : (wp->w_cursor.lnum == lnum
2379 && wp->w_cursor.col == 0)))
2380 {
2381 ++wp->w_cursor.lnum;
2382 save_curwin = curwin;
2383 curwin = wp;
2384 curbuf = curwin->w_buffer;
2385 scroll_cursor_bot(0, FALSE);
2386 curwin = save_curwin;
2387 curbuf = curwin->w_buffer;
2388 }
2389 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002390 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002391 channel_need_redraw = TRUE;
2392 }
2393
2394 if (save_write_to)
2395 {
2396 channel_T *ch;
2397
2398 /* Find channels reading from this buffer and adjust their
2399 * next-to-read line number. */
2400 buffer->b_write_to_channel = TRUE;
2401 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2402 {
2403 chanpart_T *in_part = &ch->ch_part[PART_IN];
2404
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002405 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002406 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2407 }
2408 }
2409}
2410
Bram Moolenaar437905c2016-04-26 19:01:05 +02002411 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002412drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002413{
2414 char_u *msg;
2415
2416 while ((msg = channel_get(channel, part)) != NULL)
2417 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002418 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002419 vim_free(msg);
2420 }
2421}
2422
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002423/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002424 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002425 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002426 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002427 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002428 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002429may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002430{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002431 char_u *msg = NULL;
2432 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002433 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002434 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002435 chanpart_T *ch_part = &channel->ch_part[part];
2436 ch_mode_T ch_mode = ch_part->ch_mode;
2437 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002438 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002439 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002440 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002441 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002442 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002443
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002444 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002445 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002446 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002447
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002448 /* Use a message-specific callback, part callback or channel callback */
2449 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2450 if (cbitem->cq_seq_nr == 0)
2451 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002452 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002453 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002454 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002455 partial = cbitem->cq_partial;
2456 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002457 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002458 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002459 callback = ch_part->ch_callback;
2460 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002461 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002462 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002463 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002464 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002465 partial = channel->ch_partial;
2466 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002467
Bram Moolenaarec68a992016-10-03 21:37:41 +02002468 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002469 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2470 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002471 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002472 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002473 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002474 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002475 buffer = NULL;
2476 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002477
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002478 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002479 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002480 listitem_T *item;
2481 int argc = 0;
2482
Bram Moolenaard7ece102016-02-02 23:23:02 +01002483 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002484 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002485 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002486 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002487 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002488 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002489 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002490 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002491
Bram Moolenaarece61b02016-02-20 21:39:05 +01002492 for (item = listtv->vval.v_list->lv_first;
2493 item != NULL && argc < CH_JSON_MAX_ARGS;
2494 item = item->li_next)
2495 argv[argc++] = item->li_tv;
2496 while (argc < CH_JSON_MAX_ARGS)
2497 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002498
Bram Moolenaarece61b02016-02-20 21:39:05 +01002499 if (argv[0].v_type == VAR_STRING)
2500 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002501 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002502 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002503 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002504 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002505 }
2506
Bram Moolenaarece61b02016-02-20 21:39:05 +01002507 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002508 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002509 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002510 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002511 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002512 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002513 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002514 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002515 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002516 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002517 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002518 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002519 return FALSE;
2520 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002521 else
2522 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002523 /* If there is no callback or buffer drop the message. */
2524 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002525 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002526 /* If there is a close callback it may use ch_read() to get the
2527 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002528 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002529 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002530 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002531 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002532
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002533 if (ch_mode == MODE_NL)
2534 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002535 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002536 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002537 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002538
2539 /* See if we have a message ending in NL in the first buffer. If
2540 * not try to concatenate the first and the second buffer. */
2541 while (TRUE)
2542 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002543 node = channel_peek(channel, part);
2544 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002545 if (nl != NULL)
2546 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002547 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002548 {
2549 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2550 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002551 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002552 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002553 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002554 buf = node->rq_buffer;
2555
Bram Moolenaarec68a992016-10-03 21:37:41 +02002556 if (nl == NULL)
2557 {
2558 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002559 char_u *new_buf;
2560
2561 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2562 if (new_buf == NULL)
2563 /* This might fail over and over again, should the message
2564 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002565 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002566 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002567 node->rq_buffer = buf;
2568 nl = buf + node->rq_buflen++;
2569 *nl = NUL;
2570 }
2571
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002572 /* Convert NUL to NL, the internal representation. */
2573 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2574 if (*p == NUL)
2575 *p = NL;
2576
2577 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002578 {
2579 /* get the whole buffer, drop the NL */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002580 msg = channel_get(channel, part);
Bram Moolenaar187db502016-02-27 14:44:26 +01002581 *nl = NUL;
2582 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002583 else
2584 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002585 /* Copy the message into allocated memory (excluding the NL)
2586 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002587 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002588 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002589 }
2590 }
2591 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002592 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002593 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002594 * get everything we have.
2595 * Convert NUL to NL, the internal representation. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002596 msg = channel_get_all(channel, part);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002597 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002598
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002599 if (msg == NULL)
2600 return FALSE; /* out of memory (and avoids Coverity warning) */
2601
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002602 argv[1].v_type = VAR_STRING;
2603 argv[1].vval.v_string = msg;
2604 }
2605
Bram Moolenaara07fec92016-02-05 21:04:08 +01002606 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002607 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002608 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002609
Bram Moolenaar958dc692016-12-01 15:34:12 +01002610 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002611 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002612 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002613 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002614 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002615 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002616 break;
2617 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002618 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002619 {
2620 if (channel->ch_drop_never)
2621 {
2622 /* message must be read with ch_read() */
2623 channel_push_json(channel, part, listtv);
2624 listtv = NULL;
2625 }
2626 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002627 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002628 seq_nr);
2629 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002630 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002631 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002632 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002633 if (buffer != NULL)
2634 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002635 if (msg == NULL)
2636 /* JSON or JS mode: re-encode the message. */
2637 msg = json_encode(listtv, ch_mode);
2638 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002639 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002640#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002641 if (buffer->b_term != NULL)
2642 write_to_term(buffer, msg, channel);
2643 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002644#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002645 append_to_buffer(buffer, msg, channel, part);
2646 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002647 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002648
Bram Moolenaar187db502016-02-27 14:44:26 +01002649 if (callback != NULL)
2650 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002651 if (cbitem != NULL)
2652 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2653 else
2654 {
2655 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002656 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002657 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002658 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002659 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002660 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002661 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002662 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002663 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002664
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002665 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002666 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002667 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002668
2669 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002670}
2671
2672/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002673 * Return TRUE when channel "channel" is open for writing to.
2674 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002675 */
2676 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002677channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002678{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002679 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002680 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002681}
2682
2683/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002684 * Return TRUE when channel "channel" is open for reading or writing.
2685 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002686 */
2687 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002688channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002689{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002690 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002691 || channel->CH_IN_FD != INVALID_FD
2692 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002693 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002694}
2695
2696/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002697 * Return TRUE if "channel" has JSON or other typeahead.
2698 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002699 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002700channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002701{
2702 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2703
2704 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2705 {
2706 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2707 jsonq_T *item = head->jq_next;
2708
2709 return item != NULL;
2710 }
2711 return channel_peek(channel, part) != NULL;
2712}
2713
2714/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002715 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002716 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002717 */
2718 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002719channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002720{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002721 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002722 int has_readahead = FALSE;
2723
Bram Moolenaar77073442016-02-13 23:23:53 +01002724 if (channel == NULL)
2725 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002726 if (req_part == PART_OUT)
2727 {
2728 if (channel->CH_OUT_FD != INVALID_FD)
2729 return "open";
2730 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002731 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002732 }
2733 else if (req_part == PART_ERR)
2734 {
2735 if (channel->CH_ERR_FD != INVALID_FD)
2736 return "open";
2737 if (channel_has_readahead(channel, PART_ERR))
2738 has_readahead = TRUE;
2739 }
2740 else
2741 {
2742 if (channel_is_open(channel))
2743 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002744 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002745 if (channel_has_readahead(channel, part))
2746 {
2747 has_readahead = TRUE;
2748 break;
2749 }
2750 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002751
2752 if (has_readahead)
2753 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002754 return "closed";
2755}
2756
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002757 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002758channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002759{
2760 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002761 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002762 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002763 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002764 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002765
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002766 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002767 STRCAT(namebuf, "_");
2768 tail = STRLEN(namebuf);
2769
2770 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002771 if (chanpart->ch_fd != INVALID_FD)
2772 status = "open";
2773 else if (channel_has_readahead(channel, part))
2774 status = "buffered";
2775 else
2776 status = "closed";
2777 dict_add_nr_str(dict, namebuf, 0, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002778
2779 STRCPY(namebuf + tail, "mode");
2780 switch (chanpart->ch_mode)
2781 {
2782 case MODE_NL: s = "NL"; break;
2783 case MODE_RAW: s = "RAW"; break;
2784 case MODE_JSON: s = "JSON"; break;
2785 case MODE_JS: s = "JS"; break;
2786 }
2787 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2788
2789 STRCPY(namebuf + tail, "io");
2790 if (part == PART_SOCK)
2791 s = "socket";
2792 else switch (chanpart->ch_io)
2793 {
2794 case JIO_NULL: s = "null"; break;
2795 case JIO_PIPE: s = "pipe"; break;
2796 case JIO_FILE: s = "file"; break;
2797 case JIO_BUFFER: s = "buffer"; break;
2798 case JIO_OUT: s = "out"; break;
2799 }
2800 dict_add_nr_str(dict, namebuf, 0, (char_u *)s);
2801
2802 STRCPY(namebuf + tail, "timeout");
2803 dict_add_nr_str(dict, namebuf, chanpart->ch_timeout, NULL);
2804}
2805
2806 void
2807channel_info(channel_T *channel, dict_T *dict)
2808{
2809 dict_add_nr_str(dict, "id", channel->ch_id, NULL);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002810 dict_add_nr_str(dict, "status", 0, (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002811
2812 if (channel->ch_hostname != NULL)
2813 {
2814 dict_add_nr_str(dict, "hostname", 0, (char_u *)channel->ch_hostname);
2815 dict_add_nr_str(dict, "port", channel->ch_port, NULL);
2816 channel_part_info(channel, dict, "sock", PART_SOCK);
2817 }
2818 else
2819 {
2820 channel_part_info(channel, dict, "out", PART_OUT);
2821 channel_part_info(channel, dict, "err", PART_ERR);
2822 channel_part_info(channel, dict, "in", PART_IN);
2823 }
2824}
2825
Bram Moolenaar77073442016-02-13 23:23:53 +01002826/*
2827 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002828 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002829 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002830 */
2831 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002832channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002833{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002834 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002835
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002836#ifdef FEAT_GUI
2837 channel_gui_unregister(channel);
2838#endif
2839
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002840 ch_close_part(channel, PART_SOCK);
2841 ch_close_part(channel, PART_IN);
2842 ch_close_part(channel, PART_OUT);
2843 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002844
Bram Moolenaara9f02812017-08-05 17:40:38 +02002845 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002846 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002847 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002848
Bram Moolenaara9f02812017-08-05 17:40:38 +02002849 /* Invoke callbacks and flush buffers before the close callback. */
2850 if (channel->ch_close_cb != NULL)
2851 ch_log(channel,
2852 "Invoking callbacks and flushing buffers before closing");
2853 for (part = PART_SOCK; part < PART_IN; ++part)
2854 {
2855 if (channel->ch_close_cb != NULL
2856 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2857 {
2858 /* Increment the refcount to avoid the channel being freed
2859 * halfway. */
2860 ++channel->ch_refcount;
2861 if (channel->ch_close_cb == NULL)
2862 ch_log(channel, "flushing %s buffers before closing",
2863 part_names[part]);
2864 while (may_invoke_callback(channel, part))
2865 ;
2866 --channel->ch_refcount;
2867 }
2868 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002869
Bram Moolenaara9f02812017-08-05 17:40:38 +02002870 if (channel->ch_close_cb != NULL)
2871 {
2872 typval_T argv[1];
2873 typval_T rettv;
2874 int dummy;
2875
2876 /* Increment the refcount to avoid the channel being freed
2877 * halfway. */
2878 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002879 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002880 (char *)channel->ch_close_cb);
2881 argv[0].v_type = VAR_CHANNEL;
2882 argv[0].vval.v_channel = channel;
2883 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002884 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002885 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002886 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002887 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002888
Bram Moolenaara9f02812017-08-05 17:40:38 +02002889 /* the callback is only called once */
2890 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2891 channel->ch_close_cb = NULL;
2892 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002893
Bram Moolenaara9f02812017-08-05 17:40:38 +02002894 if (channel_need_redraw)
2895 {
2896 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002897 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002898 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002899
Bram Moolenaara9f02812017-08-05 17:40:38 +02002900 if (!channel->ch_drop_never)
2901 /* any remaining messages are useless now */
2902 for (part = PART_SOCK; part < PART_IN; ++part)
2903 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002904
2905 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002906 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002907 }
2908
2909 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002910
2911#ifdef FEAT_TERMINAL
2912 term_channel_closed(channel);
2913#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002914}
2915
Bram Moolenaard04a0202016-01-26 23:30:18 +01002916/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002917 * Close the "in" part channel "channel".
2918 */
2919 void
2920channel_close_in(channel_T *channel)
2921{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002922 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02002923}
2924
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002925 static void
2926remove_from_writeque(writeq_T *wq, writeq_T *entry)
2927{
2928 ga_clear(&entry->wq_ga);
2929 wq->wq_next = entry->wq_next;
2930 if (wq->wq_next == NULL)
2931 wq->wq_prev = NULL;
2932 else
2933 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002934 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002935}
2936
Bram Moolenaar0874a832016-09-01 15:11:51 +02002937/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002938 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002939 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002940 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002941channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002942{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002943 chanpart_T *ch_part = &channel->ch_part[part];
2944 jsonq_T *json_head = &ch_part->ch_json_head;
2945 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002946
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002947 while (channel_peek(channel, part) != NULL)
2948 vim_free(channel_get(channel, part));
Bram Moolenaar77073442016-02-13 23:23:53 +01002949
2950 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01002951 {
2952 cbq_T *node = cb_head->cq_next;
2953
2954 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002955 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01002956 vim_free(node);
2957 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002958
2959 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002960 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002961 free_tv(json_head->jq_next->jq_value);
2962 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002963 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002964
Bram Moolenaaraba680a2017-09-09 16:42:53 +02002965 free_callback(ch_part->ch_callback, ch_part->ch_partial);
2966 ch_part->ch_callback = NULL;
2967 ch_part->ch_partial = NULL;
2968
2969 while (ch_part->ch_writeque.wq_next != NULL)
2970 remove_from_writeque(&ch_part->ch_writeque,
2971 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002972}
2973
2974/*
2975 * Clear all the read buffers on "channel".
2976 */
2977 void
2978channel_clear(channel_T *channel)
2979{
Bram Moolenaard6051b52016-02-28 15:49:03 +01002980 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01002981 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002982 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002983 channel_clear_one(channel, PART_OUT);
2984 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02002985 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002986 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01002987 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002988 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002989 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002990 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002991 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01002992}
2993
Bram Moolenaar77073442016-02-13 23:23:53 +01002994#if defined(EXITFREE) || defined(PROTO)
2995 void
2996channel_free_all(void)
2997{
2998 channel_T *channel;
2999
Bram Moolenaard6051b52016-02-28 15:49:03 +01003000 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003001 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3002 channel_clear(channel);
3003}
3004#endif
3005
3006
Bram Moolenaar715d2852016-04-30 17:06:31 +02003007/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003008#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003009
3010/* Buffer size for reading incoming messages. */
3011#define MAXMSGSIZE 4096
3012
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003013#if defined(HAVE_SELECT)
3014/*
3015 * Add write fds where we are waiting for writing to be possible.
3016 */
3017 static int
3018channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3019{
3020 int maxfd = maxfd_arg;
3021 channel_T *ch;
3022
3023 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3024 {
3025 chanpart_T *in_part = &ch->ch_part[PART_IN];
3026
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003027 if (in_part->ch_fd != INVALID_FD
3028 && (in_part->ch_bufref.br_buf != NULL
3029 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003030 {
3031 FD_SET((int)in_part->ch_fd, wfds);
3032 if ((int)in_part->ch_fd >= maxfd)
3033 maxfd = (int)in_part->ch_fd + 1;
3034 }
3035 }
3036 return maxfd;
3037}
3038#else
3039/*
3040 * Add write fds where we are waiting for writing to be possible.
3041 */
3042 static int
3043channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3044{
3045 int nfd = nfd_in;
3046 channel_T *ch;
3047
3048 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3049 {
3050 chanpart_T *in_part = &ch->ch_part[PART_IN];
3051
Bram Moolenaar683b7962017-08-19 15:51:59 +02003052 if (in_part->ch_fd != INVALID_FD
3053 && (in_part->ch_bufref.br_buf != NULL
3054 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003055 {
3056 in_part->ch_poll_idx = nfd;
3057 fds[nfd].fd = in_part->ch_fd;
3058 fds[nfd].events = POLLOUT;
3059 ++nfd;
3060 }
3061 else
3062 in_part->ch_poll_idx = -1;
3063 }
3064 return nfd;
3065}
3066#endif
3067
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003068typedef enum {
3069 CW_READY,
3070 CW_NOT_READY,
3071 CW_ERROR
3072} channel_wait_result;
3073
Bram Moolenaard04a0202016-01-26 23:30:18 +01003074/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003075 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003076 * Return CW_READY when there is something to read.
3077 * Return CW_NOT_READY when there is nothing to read.
3078 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003079 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003080 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003081channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003082{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003083 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003084 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003085
Bram Moolenaard8070362016-02-15 21:56:54 +01003086# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003087 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003088 {
3089 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003090 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003091 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003092 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003093
3094 /* reading from a pipe, not a socket */
3095 while (TRUE)
3096 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003097 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3098
3099 if (r && nread > 0)
3100 return CW_READY;
3101 if (r == 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003102 {
3103 DWORD err = GetLastError();
3104
3105 if (err != ERROR_BAD_PIPE && err != ERROR_BROKEN_PIPE)
3106 return CW_ERROR;
3107
3108 if (channel->ch_named_pipe)
3109 {
3110 DisconnectNamedPipe((HANDLE)fd);
3111 ConnectNamedPipe((HANDLE)fd, NULL);
3112 }
3113 else
3114 return CW_ERROR;
3115 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003116
3117 /* perhaps write some buffer lines */
3118 channel_write_any_lines();
3119
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003120 sleep_time = deadline - GetTickCount();
3121 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003122 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003123 /* Wait for a little while. Very short at first, up to 10 msec
3124 * after looping a few times. */
3125 if (sleep_time > delay)
3126 sleep_time = delay;
3127 Sleep(sleep_time);
3128 delay = delay * 2;
3129 if (delay > 10)
3130 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003131 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003132 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003133 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003134#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003135 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003136#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003137 struct timeval tval;
3138 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003139 fd_set wfds;
3140 int ret;
3141 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003142
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003143 tval.tv_sec = timeout / 1000;
3144 tval.tv_usec = (timeout % 1000) * 1000;
3145 for (;;)
3146 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003147 FD_ZERO(&rfds);
3148 FD_SET((int)fd, &rfds);
3149
3150 /* Write lines to a pipe when a pipe can be written to. Need to
3151 * set this every time, some buffers may be done. */
3152 maxfd = (int)fd + 1;
3153 FD_ZERO(&wfds);
3154 maxfd = channel_fill_wfds(maxfd, &wfds);
3155
3156 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003157# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003158 SOCK_ERRNO;
3159 if (ret == -1 && errno == EINTR)
3160 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003161# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003162 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003163 {
3164 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003165 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003166 channel_write_any_lines();
3167 continue;
3168 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003169 break;
3170 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003171#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003172 for (;;)
3173 {
3174 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3175 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003176
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003177 fds[0].fd = fd;
3178 fds[0].events = POLLIN;
3179 nfd = channel_fill_poll_write(nfd, fds);
3180 if (poll(fds, nfd, timeout) > 0)
3181 {
3182 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003183 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003184 channel_write_any_lines();
3185 continue;
3186 }
3187 break;
3188 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003189#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003190 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003191 return CW_NOT_READY;
3192}
3193
3194 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003195ch_close_part_on_error(
3196 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003197{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003198 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003199
3200 if (is_err)
3201 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003202 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003203 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003204 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003205
3206 /* Queue a "DETACH" netbeans message in the command queue in order to
3207 * terminate the netbeans session later. Do not end the session here
3208 * directly as we may be running in the context of a call to
3209 * netbeans_parse_messages():
3210 * netbeans_parse_messages
3211 * -> autocmd triggered while processing the netbeans cmd
3212 * -> ui_breakcheck
3213 * -> gui event loop or select loop
3214 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003215 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003216 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003217 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003218 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003219 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3220
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003221 /* When reading is not possible close this part of the channel. Don't
3222 * close the channel yet, there may be something to read on another part. */
3223 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003224
3225#ifdef FEAT_GUI
3226 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003227 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003228#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003229}
3230
3231 static void
3232channel_close_now(channel_T *channel)
3233{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003234 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003235 if (channel->ch_nb_close_cb != NULL)
3236 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003237 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003238}
3239
3240/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003241 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003242 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003243 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003244 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003245 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003246channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003247{
3248 static char_u *buf = NULL;
3249 int len = 0;
3250 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003251 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003252 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003253
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003254 fd = channel->ch_part[part].ch_fd;
3255 if (fd == INVALID_FD)
3256 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003257 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003258 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003259 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003260 }
3261 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003262
3263 /* Allocate a buffer to read into. */
3264 if (buf == NULL)
3265 {
3266 buf = alloc(MAXMSGSIZE);
3267 if (buf == NULL)
3268 return; /* out of memory! */
3269 }
3270
3271 /* Keep on reading for as long as there is something to read.
3272 * Use select() or poll() to avoid blocking on a message that is exactly
3273 * MAXMSGSIZE long. */
3274 for (;;)
3275 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003276 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003277 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003278 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003279 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003280 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003281 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003282 if (len <= 0)
3283 break; /* error or nothing more to read */
3284
3285 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003286 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003287 readlen += len;
3288 if (len < MAXMSGSIZE)
3289 break; /* did read everything that's available */
3290 }
3291
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003292 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003293 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003294 {
3295 if (!channel->ch_keep_open)
3296 ch_close_part_on_error(channel, part, (len < 0), func);
3297 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003298#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003299 else if (CH_HAS_GUI && gtk_main_level() > 0)
3300 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003301 gtk_main_quit();
3302#endif
3303}
3304
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003305/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003306 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003307 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003308 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003309 * Returns what was read in allocated memory.
3310 * Returns NULL in case of error or timeout.
3311 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003312 static char_u *
3313channel_read_block(channel_T *channel, ch_part_T part, int timeout, int raw)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003314{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003315 char_u *buf;
3316 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003317 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003318 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003319 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003320 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003321
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003322 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003323 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003324
3325 while (TRUE)
3326 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003327 node = channel_peek(channel, part);
3328 if (node != NULL)
3329 {
3330 if (mode == MODE_RAW || (mode == MODE_NL
3331 && channel_first_nl(node) != NULL))
3332 /* got a complete message */
3333 break;
3334 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3335 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003336 /* If not blocking or nothing more is coming then return what we
3337 * have. */
3338 if (raw || fd == INVALID_FD)
3339 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003340 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003341
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003342 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003343 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003344 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003345 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003346 {
3347 ch_log(channel, "Timed out");
3348 return NULL;
3349 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003350 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003351 }
3352
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003353 /* We have a complete message now. */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003354 if (mode == MODE_RAW)
3355 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003356 msg = channel_get_all(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003357 }
3358 else
3359 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003360 char_u *p;
3361
3362 buf = node->rq_buffer;
3363 nl = channel_first_nl(node);
3364
3365 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003366 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003367 if (*p == NUL)
3368 *p = NL;
3369
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003370 if (nl == NULL)
3371 {
3372 /* must be a closed channel with missing NL */
3373 msg = channel_get(channel, part);
3374 }
3375 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003376 {
3377 /* get the whole buffer */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003378 msg = channel_get(channel, part);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003379 *nl = NUL;
3380 }
3381 else
3382 {
3383 /* Copy the message into allocated memory and remove it from the
3384 * buffer. */
3385 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003386 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003387 }
3388 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003389 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003390 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003391 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003392}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003393
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003394/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003395 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003396 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003397 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003398 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003399 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003400 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003401channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003402 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003403 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003404 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003405 int id,
3406 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003407{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003408 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003409 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003410 int timeout;
3411 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003412
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003413 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003414 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003415 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003416 for (;;)
3417 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003418 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003419
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003420 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003421 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003422 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003423 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003424 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003425 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003426
Bram Moolenaard7ece102016-02-02 23:23:02 +01003427 if (!more)
3428 {
3429 /* Handle any other messages in the queue. If done some more
3430 * messages may have arrived. */
3431 if (channel_parse_messages())
3432 continue;
3433
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003434 /* Wait for up to the timeout. If there was an incomplete message
3435 * use the deadline for that. */
3436 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003437 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003438 {
3439#ifdef WIN32
3440 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3441#else
3442 {
3443 struct timeval now_tv;
3444
3445 gettimeofday(&now_tv, NULL);
3446 timeout = (chanpart->ch_deadline.tv_sec
3447 - now_tv.tv_sec) * 1000
3448 + (chanpart->ch_deadline.tv_usec
3449 - now_tv.tv_usec) / 1000
3450 + 1;
3451 }
3452#endif
3453 if (timeout < 0)
3454 {
3455 /* Something went wrong, channel_parse_json() didn't
3456 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003457 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003458 timeout = timeout_arg;
3459 }
3460 else if (timeout > timeout_arg)
3461 timeout = timeout_arg;
3462 }
3463 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003464 if (fd == INVALID_FD
3465 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003466 {
3467 if (timeout == timeout_arg)
3468 {
3469 if (fd != INVALID_FD)
3470 ch_log(channel, "Timed out");
3471 break;
3472 }
3473 }
3474 else
3475 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003476 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003477 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003478 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003479 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003480}
3481
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003482/*
3483 * Common for ch_read() and ch_readraw().
3484 */
3485 void
3486common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
3487{
3488 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003489 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003490 jobopt_T opt;
3491 int mode;
3492 int timeout;
3493 int id = -1;
3494 typval_T *listtv = NULL;
3495
3496 /* return an empty string by default */
3497 rettv->v_type = VAR_STRING;
3498 rettv->vval.v_string = NULL;
3499
3500 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003501 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003502 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003503 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003504
Bram Moolenaar437905c2016-04-26 19:01:05 +02003505 if (opt.jo_set & JO_PART)
3506 part = opt.jo_part;
3507 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003508 if (channel != NULL)
3509 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003510 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003511 part = channel_part_read(channel);
3512 mode = channel_get_mode(channel, part);
3513 timeout = channel_get_timeout(channel, part);
3514 if (opt.jo_set & JO_TIMEOUT)
3515 timeout = opt.jo_timeout;
3516
3517 if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003518 rettv->vval.v_string = channel_read_block(channel, part,
3519 timeout, raw);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003520 else
3521 {
3522 if (opt.jo_set & JO_ID)
3523 id = opt.jo_id;
3524 channel_read_json_block(channel, part, timeout, id, &listtv);
3525 if (listtv != NULL)
3526 {
3527 *rettv = *listtv;
3528 vim_free(listtv);
3529 }
3530 else
3531 {
3532 rettv->v_type = VAR_SPECIAL;
3533 rettv->vval.v_number = VVAL_NONE;
3534 }
3535 }
3536 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003537
3538theend:
3539 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003540}
3541
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003542# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3543 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003544/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003545 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003546 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003547 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003548 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003549channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003550{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003551 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003552 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003553
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003554 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003555 for (channel = first_channel; channel != NULL;
3556 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003557 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003558 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003559 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003560 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003561 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003562 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003563 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003564 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003565 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003566}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003567# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003568
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003569# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003570/*
3571 * Check the channels for anything that is ready to be read.
3572 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003573 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003574 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003575 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003576channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003577{
3578 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003579 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003580 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003581
3582 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3583 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003584 if (only_keep_open && !channel->ch_keep_open)
3585 continue;
3586
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003587 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003588 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003589 {
3590 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003591 if (fd != INVALID_FD)
3592 {
3593 int r = channel_wait(channel, fd, 0);
3594
3595 if (r == CW_READY)
3596 channel_read(channel, part, "channel_handle_events");
3597 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003598 ch_close_part_on_error(channel, part, TRUE,
3599 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003600 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003601 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003602 }
3603}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003604# endif
3605
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003606# if defined(FEAT_GUI) || defined(PROTO)
3607/*
3608 * Return TRUE when there is any channel with a keep_open flag.
3609 */
3610 int
3611channel_any_keep_open()
3612{
3613 channel_T *channel;
3614
3615 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3616 if (channel->ch_keep_open)
3617 return TRUE;
3618 return FALSE;
3619}
3620# endif
3621
Bram Moolenaard04a0202016-01-26 23:30:18 +01003622/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003623 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003624 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003625 */
3626 void
3627channel_set_nonblock(channel_T *channel, ch_part_T part)
3628{
3629 chanpart_T *ch_part = &channel->ch_part[part];
3630 int fd = ch_part->ch_fd;
3631
3632 if (fd != INVALID_FD)
3633 {
3634#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003635 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003636
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003637 ioctlsocket(fd, FIONBIO, &val);
3638#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003639 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003640#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003641 ch_part->ch_nonblocking = TRUE;
3642 }
3643}
3644
3645/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003646 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003647 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003648 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003649 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003650 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003651channel_send(
3652 channel_T *channel,
3653 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003654 char_u *buf_arg,
3655 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003656 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003657{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003658 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003659 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003660 chanpart_T *ch_part = &channel->ch_part[part];
3661 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003662
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003663 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003664 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003665 {
3666 if (!channel->ch_error && fun != NULL)
3667 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003668 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaar5b302912016-08-24 22:11:55 +02003669 EMSG2(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003670 }
3671 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003672 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003673 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003674
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003675 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003676 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003677 ch_log_lead("SEND ", channel);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003678 fprintf(log_fd, "'");
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003679 ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003680 fprintf(log_fd, "'\n");
3681 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003682 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003683 }
3684
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003685 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003686 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003687 writeq_T *wq = &ch_part->ch_writeque;
3688 char_u *buf;
3689 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003690
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003691 if (wq->wq_next != NULL)
3692 {
3693 /* first write what was queued */
3694 buf = wq->wq_next->wq_ga.ga_data;
3695 len = wq->wq_next->wq_ga.ga_len;
3696 did_use_queue = TRUE;
3697 }
3698 else
3699 {
3700 if (len_arg == 0)
3701 /* nothing to write, called from channel_select_check() */
3702 return OK;
3703 buf = buf_arg;
3704 len = len_arg;
3705 }
3706
3707 if (part == PART_SOCK)
3708 res = sock_write(fd, (char *)buf, len);
3709 else
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003710 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003711 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003712#ifdef WIN32
Bram Moolenaar3c518402017-09-08 20:47:00 +02003713 if (channel->ch_named_pipe && res < 0)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003714 {
Bram Moolenaar3c518402017-09-08 20:47:00 +02003715 DisconnectNamedPipe((HANDLE)fd);
3716 ConnectNamedPipe((HANDLE)fd, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02003717 }
3718#endif
3719
3720 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003721 if (res < 0 && (errno == EWOULDBLOCK
3722#ifdef EAGAIN
3723 || errno == EAGAIN
3724#endif
3725 ))
3726 res = 0; /* nothing got written */
3727
3728 if (res >= 0 && ch_part->ch_nonblocking)
3729 {
3730 writeq_T *entry = wq->wq_next;
3731
3732 if (did_use_queue)
3733 ch_log(channel, "Sent %d bytes now", res);
3734 if (res == len)
3735 {
3736 /* Wrote all the buf[len] bytes. */
3737 if (entry != NULL)
3738 {
3739 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003740 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003741 continue;
3742 }
3743 if (did_use_queue)
3744 ch_log(channel, "Write queue empty");
3745 }
3746 else
3747 {
3748 /* Wrote only buf[res] bytes, can't write more now. */
3749 if (entry != NULL)
3750 {
3751 if (res > 0)
3752 {
3753 /* Remove the bytes that were written. */
3754 mch_memmove(entry->wq_ga.ga_data,
3755 (char *)entry->wq_ga.ga_data + res,
3756 len - res);
3757 entry->wq_ga.ga_len -= res;
3758 }
3759 buf = buf_arg;
3760 len = len_arg;
3761 }
3762 else
3763 {
3764 buf += res;
3765 len -= res;
3766 }
3767 ch_log(channel, "Adding %d bytes to the write queue", len);
3768
3769 /* Append the not written bytes of the argument to the write
3770 * buffer. Limit entries to 4000 bytes. */
3771 if (wq->wq_prev != NULL
3772 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3773 {
3774 writeq_T *last = wq->wq_prev;
3775
3776 /* append to the last entry */
3777 if (ga_grow(&last->wq_ga, len) == OK)
3778 {
3779 mch_memmove((char *)last->wq_ga.ga_data
3780 + last->wq_ga.ga_len,
3781 buf, len);
3782 last->wq_ga.ga_len += len;
3783 }
3784 }
3785 else
3786 {
3787 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3788
3789 if (last != NULL)
3790 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003791 last->wq_prev = wq->wq_prev;
3792 last->wq_next = NULL;
3793 if (wq->wq_prev == NULL)
3794 wq->wq_next = last;
3795 else
3796 wq->wq_prev->wq_next = last;
3797 wq->wq_prev = last;
3798 ga_init2(&last->wq_ga, 1, 1000);
3799 if (ga_grow(&last->wq_ga, len) == OK)
3800 {
3801 mch_memmove(last->wq_ga.ga_data, buf, len);
3802 last->wq_ga.ga_len = len;
3803 }
3804 }
3805 }
3806 }
3807 }
3808 else if (res != len)
3809 {
3810 if (!channel->ch_error && fun != NULL)
3811 {
3812 ch_error(channel, "%s(): write failed", fun);
3813 EMSG2(_("E631: %s(): write failed"), fun);
3814 }
3815 channel->ch_error = TRUE;
3816 return FAIL;
3817 }
3818
3819 channel->ch_error = FALSE;
3820 return OK;
3821 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003822}
3823
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003824/*
3825 * Common for "ch_sendexpr()" and "ch_sendraw()".
3826 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003827 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003828 * Otherwise returns NULL.
3829 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003830 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003831send_common(
3832 typval_T *argvars,
3833 char_u *text,
3834 int id,
3835 int eval,
3836 jobopt_T *opt,
3837 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003838 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003839{
3840 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003841 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003842
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003843 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003844 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003845 if (channel == NULL)
3846 return NULL;
3847 part_send = channel_part_send(channel);
3848 *part_read = channel_part_read(channel);
3849
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003850 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003851 return NULL;
3852
3853 /* Set the callback. An empty callback means no callback and not reading
3854 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3855 * allowed. */
3856 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3857 {
3858 if (eval)
3859 {
3860 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
3861 return NULL;
3862 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003863 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003864 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003865 }
3866
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02003867 if (channel_send(channel, part_send, text, (int)STRLEN(text), fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003868 && opt->jo_callback == NULL)
3869 return channel;
3870 return NULL;
3871}
3872
3873/*
3874 * common for "ch_evalexpr()" and "ch_sendexpr()"
3875 */
3876 void
3877ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3878{
3879 char_u *text;
3880 typval_T *listtv;
3881 channel_T *channel;
3882 int id;
3883 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003884 ch_part_T part_send;
3885 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003886 jobopt_T opt;
3887 int timeout;
3888
3889 /* return an empty string by default */
3890 rettv->v_type = VAR_STRING;
3891 rettv->vval.v_string = NULL;
3892
Bram Moolenaar437905c2016-04-26 19:01:05 +02003893 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003894 if (channel == NULL)
3895 return;
3896 part_send = channel_part_send(channel);
3897
3898 ch_mode = channel_get_mode(channel, part_send);
3899 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
3900 {
3901 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
3902 return;
3903 }
3904
Bram Moolenaare9d6a292016-03-20 19:31:33 +01003905 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003906 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02003907 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003908 if (text == NULL)
3909 return;
3910
3911 channel = send_common(argvars, text, id, eval, &opt,
3912 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
3913 vim_free(text);
3914 if (channel != NULL && eval)
3915 {
3916 if (opt.jo_set & JO_TIMEOUT)
3917 timeout = opt.jo_timeout;
3918 else
3919 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003920 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
3921 == OK)
3922 {
3923 list_T *list = listtv->vval.v_list;
3924
3925 /* Move the item from the list and then change the type to
3926 * avoid the value being freed. */
3927 *rettv = list->lv_last->li_tv;
3928 list->lv_last->li_tv.v_type = VAR_NUMBER;
3929 free_tv(listtv);
3930 }
3931 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003932 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003933}
3934
3935/*
3936 * common for "ch_evalraw()" and "ch_sendraw()"
3937 */
3938 void
3939ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
3940{
3941 char_u buf[NUMBUFLEN];
3942 char_u *text;
3943 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003944 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003945 jobopt_T opt;
3946 int timeout;
3947
3948 /* return an empty string by default */
3949 rettv->v_type = VAR_STRING;
3950 rettv->vval.v_string = NULL;
3951
3952 text = get_tv_string_buf(&argvars[1], buf);
3953 channel = send_common(argvars, text, 0, eval, &opt,
3954 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
3955 if (channel != NULL && eval)
3956 {
3957 if (opt.jo_set & JO_TIMEOUT)
3958 timeout = opt.jo_timeout;
3959 else
3960 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003961 rettv->vval.v_string = channel_read_block(channel, part_read,
3962 timeout, TRUE);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003963 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003964 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003965}
3966
Bram Moolenaarf3360612017-10-01 16:21:31 +02003967# define KEEP_OPEN_TIME 20 /* msec */
3968
Bram Moolenaard04a0202016-01-26 23:30:18 +01003969# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003970/*
3971 * Add open channels to the poll struct.
3972 * Return the adjusted struct index.
3973 * The type of "fds" is hidden to avoid problems with the function proto.
3974 */
3975 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02003976channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01003977{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003978 int nfd = nfd_in;
3979 channel_T *channel;
3980 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003981 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01003982
Bram Moolenaar77073442016-02-13 23:23:53 +01003983 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003984 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003985 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003986 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003987 chanpart_T *ch_part = &channel->ch_part[part];
3988
3989 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003990 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02003991 if (channel->ch_keep_open)
3992 {
3993 /* For unknown reason poll() returns immediately for a
3994 * keep-open channel. Instead of adding it to the fds add
3995 * a short timeout and check, like polling. */
3996 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
3997 *towait = KEEP_OPEN_TIME;
3998 }
3999 else
4000 {
4001 ch_part->ch_poll_idx = nfd;
4002 fds[nfd].fd = ch_part->ch_fd;
4003 fds[nfd].events = POLLIN;
4004 nfd++;
4005 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004006 }
4007 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004008 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004009 }
4010 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004011
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004012 nfd = channel_fill_poll_write(nfd, fds);
4013
Bram Moolenaare0874f82016-01-24 20:36:41 +01004014 return nfd;
4015}
4016
4017/*
4018 * The type of "fds" is hidden to avoid problems with the function proto.
4019 */
4020 int
4021channel_poll_check(int ret_in, void *fds_in)
4022{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004023 int ret = ret_in;
4024 channel_T *channel;
4025 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004026 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004027 int idx;
4028 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004029
Bram Moolenaar77073442016-02-13 23:23:53 +01004030 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004031 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004032 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004033 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004034 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004035
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004036 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004037 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004038 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004039 --ret;
4040 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004041 else if (channel->ch_part[part].ch_fd != INVALID_FD
4042 && channel->ch_keep_open)
4043 {
4044 /* polling a keep-open channel */
4045 channel_read(channel, part, "channel_poll_check_keep_open");
4046 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004047 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004048
4049 in_part = &channel->ch_part[PART_IN];
4050 idx = in_part->ch_poll_idx;
4051 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4052 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004053 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004054 --ret;
4055 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004056 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004057
4058 return ret;
4059}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004060# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004061
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004062# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004063
Bram Moolenaare0874f82016-01-24 20:36:41 +01004064/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004065 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004066 */
4067 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004068channel_select_setup(
4069 int maxfd_in,
4070 void *rfds_in,
4071 void *wfds_in,
4072 struct timeval *tv,
4073 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004074{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004075 int maxfd = maxfd_in;
4076 channel_T *channel;
4077 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004078 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004079 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004080
Bram Moolenaar77073442016-02-13 23:23:53 +01004081 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004082 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004083 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004084 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004085 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004086
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004087 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004088 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004089 if (channel->ch_keep_open)
4090 {
4091 /* For unknown reason select() returns immediately for a
4092 * keep-open channel. Instead of adding it to the rfds add
4093 * a short timeout and check, like polling. */
4094 if (*tvp == NULL || tv->tv_sec > 0
4095 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4096 {
4097 *tvp = tv;
4098 tv->tv_sec = 0;
4099 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4100 }
4101 }
4102 else
4103 {
4104 FD_SET((int)fd, rfds);
4105 if (maxfd < (int)fd)
4106 maxfd = (int)fd;
4107 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004108 }
4109 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004110 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004111
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004112 maxfd = channel_fill_wfds(maxfd, wfds);
4113
Bram Moolenaare0874f82016-01-24 20:36:41 +01004114 return maxfd;
4115}
4116
4117/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004118 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004119 */
4120 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004121channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004122{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004123 int ret = ret_in;
4124 channel_T *channel;
4125 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004126 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004127 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004128 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004129
Bram Moolenaar77073442016-02-13 23:23:53 +01004130 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004131 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004132 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004133 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004134 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004135
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004136 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004137 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004138 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004139 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004140 --ret;
4141 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004142 else if (fd != INVALID_FD && channel->ch_keep_open)
4143 {
4144 /* polling a keep-open channel */
4145 channel_read(channel, part, "channel_select_check_keep_open");
4146 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004147 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004148
4149 in_part = &channel->ch_part[PART_IN];
4150 if (ret > 0 && in_part->ch_fd != INVALID_FD
4151 && FD_ISSET(in_part->ch_fd, wfds))
4152 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004153 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004154 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004155 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004156 --ret;
4157 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004158 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004159
4160 return ret;
4161}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004162# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004163
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004164/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004165 * Execute queued up commands.
4166 * Invoked from the main loop when it's safe to execute received commands.
4167 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004168 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004169 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004170channel_parse_messages(void)
4171{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004172 channel_T *channel = first_channel;
4173 int ret = FALSE;
4174 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004175 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004176#ifdef ELAPSED_FUNC
4177 ELAPSED_TYPE start_tv;
4178
4179 ELAPSED_INIT(start_tv);
4180#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004181
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004182 ++safe_to_invoke_callback;
4183
Bram Moolenaard0b65022016-03-06 21:50:33 +01004184 /* Only do this message when another message was given, otherwise we get
4185 * lots of them. */
4186 if (did_log_msg)
4187 {
4188 ch_log(NULL, "looking for messages on channels");
4189 did_log_msg = FALSE;
4190 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004191 while (channel != NULL)
4192 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004193 if (channel->ch_to_be_closed == 0)
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004194 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004195 channel->ch_to_be_closed = (1 << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004196 channel_close_now(channel);
4197 /* channel may have been freed, start over */
4198 channel = first_channel;
4199 continue;
4200 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004201 if (channel->ch_to_be_freed)
4202 {
4203 channel_free(channel);
4204 /* channel has been freed, start over */
4205 channel = first_channel;
4206 continue;
4207 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004208 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004209 {
4210 /* channel is no longer useful, free it */
4211 channel_free(channel);
4212 channel = first_channel;
4213 part = PART_SOCK;
4214 continue;
4215 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004216 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004217 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004218 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004219 /* Increase the refcount, in case the handler causes the channel
4220 * to be unreferenced or closed. */
4221 ++channel->ch_refcount;
4222 r = may_invoke_callback(channel, part);
4223 if (r == OK)
4224 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004225 if (channel_unref(channel) || (r == OK
4226#ifdef ELAPSED_FUNC
4227 /* Limit the time we loop here to 100 msec, otherwise
4228 * Vim becomes unresponsive when the callback takes
4229 * more than a bit of time. */
4230 && ELAPSED_FUNC(start_tv) < 100L
4231#endif
4232 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004233 {
4234 /* channel was freed or something was done, start over */
4235 channel = first_channel;
4236 part = PART_SOCK;
4237 continue;
4238 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004239 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004240 if (part < PART_ERR)
4241 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004242 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004243 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004244 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004245 part = PART_SOCK;
4246 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004247 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004248
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004249 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004250 {
4251 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004252 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004253 }
4254
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004255 --safe_to_invoke_callback;
4256
Bram Moolenaard7ece102016-02-02 23:23:02 +01004257 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004258}
4259
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004260/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004261 * Return TRUE if any channel has readahead. That means we should not block on
4262 * waiting for input.
4263 */
4264 int
4265channel_any_readahead(void)
4266{
4267 channel_T *channel = first_channel;
4268 ch_part_T part = PART_SOCK;
4269
4270 while (channel != NULL)
4271 {
4272 if (channel_has_readahead(channel, part))
4273 return TRUE;
4274 if (part < PART_ERR)
4275 ++part;
4276 else
4277 {
4278 channel = channel->ch_next;
4279 part = PART_SOCK;
4280 }
4281 }
4282 return FALSE;
4283}
4284
4285/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004286 * Mark references to lists used in channels.
4287 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004288 int
4289set_ref_in_channel(int copyID)
4290{
Bram Moolenaar77073442016-02-13 23:23:53 +01004291 int abort = FALSE;
4292 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004293 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004294
Bram Moolenaar77073442016-02-13 23:23:53 +01004295 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004296 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004297 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004298 tv.v_type = VAR_CHANNEL;
4299 tv.vval.v_channel = channel;
4300 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004301 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004302 return abort;
4303}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004304
4305/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004306 * Return the "part" to write to for "channel".
4307 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004308 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004309channel_part_send(channel_T *channel)
4310{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004311 if (channel->CH_SOCK_FD == INVALID_FD)
4312 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004313 return PART_SOCK;
4314}
4315
4316/*
4317 * Return the default "part" to read from for "channel".
4318 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004319 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004320channel_part_read(channel_T *channel)
4321{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004322 if (channel->CH_SOCK_FD == INVALID_FD)
4323 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004324 return PART_SOCK;
4325}
4326
4327/*
4328 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004329 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004330 */
4331 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004332channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004333{
Bram Moolenaar77073442016-02-13 23:23:53 +01004334 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004335 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004336 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004337}
4338
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004339/*
4340 * Return the timeout of "channel"/"part"
4341 */
4342 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004343channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004344{
4345 return channel->ch_part[part].ch_timeout;
4346}
4347
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004348 static int
4349handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4350{
4351 char_u *val = get_tv_string(item);
4352
4353 opt->jo_set |= jo;
4354 if (STRCMP(val, "nl") == 0)
4355 *modep = MODE_NL;
4356 else if (STRCMP(val, "raw") == 0)
4357 *modep = MODE_RAW;
4358 else if (STRCMP(val, "js") == 0)
4359 *modep = MODE_JS;
4360 else if (STRCMP(val, "json") == 0)
4361 *modep = MODE_JSON;
4362 else
4363 {
4364 EMSG2(_(e_invarg2), val);
4365 return FAIL;
4366 }
4367 return OK;
4368}
4369
4370 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004371handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004372{
4373 char_u *val = get_tv_string(item);
4374
4375 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4376 if (STRCMP(val, "null") == 0)
4377 opt->jo_io[part] = JIO_NULL;
4378 else if (STRCMP(val, "pipe") == 0)
4379 opt->jo_io[part] = JIO_PIPE;
4380 else if (STRCMP(val, "file") == 0)
4381 opt->jo_io[part] = JIO_FILE;
4382 else if (STRCMP(val, "buffer") == 0)
4383 opt->jo_io[part] = JIO_BUFFER;
4384 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4385 opt->jo_io[part] = JIO_OUT;
4386 else
4387 {
4388 EMSG2(_(e_invarg2), val);
4389 return FAIL;
4390 }
4391 return OK;
4392}
4393
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004394/*
4395 * Clear a jobopt_T before using it.
4396 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004397 void
4398clear_job_options(jobopt_T *opt)
4399{
4400 vim_memset(opt, 0, sizeof(jobopt_T));
4401}
4402
4403/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004404 * Free any members of a jobopt_T.
4405 */
4406 void
4407free_job_options(jobopt_T *opt)
4408{
4409 if (opt->jo_partial != NULL)
4410 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004411 else if (opt->jo_callback != NULL)
4412 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004413 if (opt->jo_out_partial != NULL)
4414 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004415 else if (opt->jo_out_cb != NULL)
4416 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004417 if (opt->jo_err_partial != NULL)
4418 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004419 else if (opt->jo_err_cb != NULL)
4420 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004421 if (opt->jo_close_partial != NULL)
4422 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004423 else if (opt->jo_close_cb != NULL)
4424 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004425 if (opt->jo_exit_partial != NULL)
4426 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004427 else if (opt->jo_exit_cb != NULL)
4428 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004429 if (opt->jo_env != NULL)
4430 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004431}
4432
4433/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004434 * Get the PART_ number from the first character of an option name.
4435 */
4436 static int
4437part_from_char(int c)
4438{
4439 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4440}
4441
4442/*
4443 * Get the option entries from the dict in "tv", parse them and put the result
4444 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004445 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004446 * If an option value is invalid return FAIL.
4447 */
4448 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004449get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004450{
4451 typval_T *item;
4452 char_u *val;
4453 dict_T *dict;
4454 int todo;
4455 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004456 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004457
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004458 if (tv->v_type == VAR_UNKNOWN)
4459 return OK;
4460 if (tv->v_type != VAR_DICT)
4461 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004462 EMSG(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004463 return FAIL;
4464 }
4465 dict = tv->vval.v_dict;
4466 if (dict == NULL)
4467 return OK;
4468
4469 todo = (int)dict->dv_hashtab.ht_used;
4470 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4471 if (!HASHITEM_EMPTY(hi))
4472 {
4473 item = &dict_lookup(hi)->di_tv;
4474
4475 if (STRCMP(hi->hi_key, "mode") == 0)
4476 {
4477 if (!(supported & JO_MODE))
4478 break;
4479 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4480 return FAIL;
4481 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004482 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004483 {
4484 if (!(supported & JO_IN_MODE))
4485 break;
4486 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4487 == FAIL)
4488 return FAIL;
4489 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004490 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004491 {
4492 if (!(supported & JO_OUT_MODE))
4493 break;
4494 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4495 == FAIL)
4496 return FAIL;
4497 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004498 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004499 {
4500 if (!(supported & JO_ERR_MODE))
4501 break;
4502 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4503 == FAIL)
4504 return FAIL;
4505 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004506 else if (STRCMP(hi->hi_key, "in_io") == 0
4507 || STRCMP(hi->hi_key, "out_io") == 0
4508 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004509 {
4510 if (!(supported & JO_OUT_IO))
4511 break;
4512 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4513 return FAIL;
4514 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004515 else if (STRCMP(hi->hi_key, "in_name") == 0
4516 || STRCMP(hi->hi_key, "out_name") == 0
4517 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004518 {
4519 part = part_from_char(*hi->hi_key);
4520
4521 if (!(supported & JO_OUT_IO))
4522 break;
4523 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4524 opt->jo_io_name[part] =
4525 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
4526 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004527 else if (STRCMP(hi->hi_key, "pty") == 0)
4528 {
4529 if (!(supported & JO_MODE))
4530 break;
4531 opt->jo_pty = get_tv_number(item);
4532 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004533 else if (STRCMP(hi->hi_key, "in_buf") == 0
4534 || STRCMP(hi->hi_key, "out_buf") == 0
4535 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004536 {
4537 part = part_from_char(*hi->hi_key);
4538
4539 if (!(supported & JO_OUT_IO))
4540 break;
4541 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
4542 opt->jo_io_buf[part] = get_tv_number(item);
4543 if (opt->jo_io_buf[part] <= 0)
4544 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004545 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004546 return FAIL;
4547 }
4548 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4549 {
4550 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
4551 return FAIL;
4552 }
4553 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004554 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4555 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4556 {
4557 part = part_from_char(*hi->hi_key);
4558
4559 if (!(supported & JO_OUT_IO))
4560 break;
4561 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
4562 opt->jo_modifiable[part] = get_tv_number(item);
4563 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004564 else if (STRCMP(hi->hi_key, "out_msg") == 0
4565 || STRCMP(hi->hi_key, "err_msg") == 0)
4566 {
4567 part = part_from_char(*hi->hi_key);
4568
4569 if (!(supported & JO_OUT_IO))
4570 break;
4571 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4572 opt->jo_message[part] = get_tv_number(item);
4573 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004574 else if (STRCMP(hi->hi_key, "in_top") == 0
4575 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004576 {
4577 linenr_T *lp;
4578
4579 if (!(supported & JO_OUT_IO))
4580 break;
4581 if (hi->hi_key[3] == 't')
4582 {
4583 lp = &opt->jo_in_top;
4584 opt->jo_set |= JO_IN_TOP;
4585 }
4586 else
4587 {
4588 lp = &opt->jo_in_bot;
4589 opt->jo_set |= JO_IN_BOT;
4590 }
4591 *lp = get_tv_number(item);
4592 if (*lp < 0)
4593 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004594 EMSG3(_(e_invargNval), hi->hi_key, get_tv_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004595 return FAIL;
4596 }
4597 }
4598 else if (STRCMP(hi->hi_key, "channel") == 0)
4599 {
4600 if (!(supported & JO_OUT_IO))
4601 break;
4602 opt->jo_set |= JO_CHANNEL;
4603 if (item->v_type != VAR_CHANNEL)
4604 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004605 EMSG2(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004606 return FAIL;
4607 }
4608 opt->jo_channel = item->vval.v_channel;
4609 }
4610 else if (STRCMP(hi->hi_key, "callback") == 0)
4611 {
4612 if (!(supported & JO_CALLBACK))
4613 break;
4614 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004615 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004616 if (opt->jo_callback == NULL)
4617 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004618 EMSG2(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004619 return FAIL;
4620 }
4621 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004622 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004623 {
4624 if (!(supported & JO_OUT_CALLBACK))
4625 break;
4626 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004627 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004628 if (opt->jo_out_cb == NULL)
4629 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004630 EMSG2(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004631 return FAIL;
4632 }
4633 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004634 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004635 {
4636 if (!(supported & JO_ERR_CALLBACK))
4637 break;
4638 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004639 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004640 if (opt->jo_err_cb == NULL)
4641 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004642 EMSG2(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004643 return FAIL;
4644 }
4645 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004646 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004647 {
4648 if (!(supported & JO_CLOSE_CALLBACK))
4649 break;
4650 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004651 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004652 if (opt->jo_close_cb == NULL)
4653 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004654 EMSG2(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004655 return FAIL;
4656 }
4657 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004658 else if (STRCMP(hi->hi_key, "drop") == 0)
4659 {
4660 int never = FALSE;
4661 val = get_tv_string(item);
4662
4663 if (STRCMP(val, "never") == 0)
4664 never = TRUE;
4665 else if (STRCMP(val, "auto") != 0)
4666 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004667 EMSG3(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004668 return FAIL;
4669 }
4670 opt->jo_drop_never = never;
4671 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004672 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4673 {
4674 if (!(supported & JO_EXIT_CB))
4675 break;
4676 opt->jo_set |= JO_EXIT_CB;
4677 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4678 if (opt->jo_exit_cb == NULL)
4679 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004680 EMSG2(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004681 return FAIL;
4682 }
4683 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004684#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004685 else if (STRCMP(hi->hi_key, "term_name") == 0)
4686 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004687 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004688 break;
4689 opt->jo_set2 |= JO2_TERM_NAME;
4690 opt->jo_term_name = get_tv_string_chk(item);
4691 if (opt->jo_term_name == NULL)
4692 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004693 EMSG2(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004694 return FAIL;
4695 }
4696 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004697 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4698 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004699 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004700 break;
4701 val = get_tv_string(item);
4702 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4703 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004704 EMSG3(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004705 return FAIL;
4706 }
4707 opt->jo_set2 |= JO2_TERM_FINISH;
4708 opt->jo_term_finish = *val;
4709 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004710 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4711 {
4712 char_u *p;
4713
4714 if (!(supported2 & JO2_TERM_OPENCMD))
4715 break;
4716 opt->jo_set2 |= JO2_TERM_OPENCMD;
4717 p = opt->jo_term_opencmd = get_tv_string_chk(item);
4718 if (p != NULL)
4719 {
4720 /* Must have %d and no other %. */
4721 p = vim_strchr(p, '%');
4722 if (p != NULL && (p[1] != 'd'
4723 || vim_strchr(p + 2, '%') != NULL))
4724 p = NULL;
4725 }
4726 if (p == NULL)
4727 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004728 EMSG2(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004729 return FAIL;
4730 }
4731 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004732 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4733 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004734 char_u *p;
4735
4736 if (!(supported2 & JO2_EOF_CHARS))
4737 break;
4738 opt->jo_set2 |= JO2_EOF_CHARS;
4739 p = opt->jo_eof_chars = get_tv_string_chk(item);
4740 if (p == NULL)
4741 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004742 EMSG2(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004743 return FAIL;
4744 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004745 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004746 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4747 {
4748 if (!(supported2 & JO2_TERM_ROWS))
4749 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004750 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004751 opt->jo_term_rows = get_tv_number(item);
4752 }
4753 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4754 {
4755 if (!(supported2 & JO2_TERM_COLS))
4756 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004757 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004758 opt->jo_term_cols = get_tv_number(item);
4759 }
4760 else if (STRCMP(hi->hi_key, "vertical") == 0)
4761 {
4762 if (!(supported2 & JO2_VERTICAL))
4763 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004764 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004765 opt->jo_vertical = get_tv_number(item);
4766 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004767 else if (STRCMP(hi->hi_key, "curwin") == 0)
4768 {
4769 if (!(supported2 & JO2_CURWIN))
4770 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004771 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaarda43b612017-08-11 22:27:50 +02004772 opt->jo_curwin = get_tv_number(item);
4773 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004774 else if (STRCMP(hi->hi_key, "hidden") == 0)
4775 {
4776 if (!(supported2 & JO2_HIDDEN))
4777 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004778 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004779 opt->jo_hidden = get_tv_number(item);
4780 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004781 else if (STRCMP(hi->hi_key, "norestore") == 0)
4782 {
4783 if (!(supported2 & JO2_NORESTORE))
4784 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004785 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004786 opt->jo_term_norestore = get_tv_number(item);
4787 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004788 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4789 {
4790 if (!(supported2 & JO2_TERM_KILL))
4791 break;
4792 opt->jo_set2 |= JO2_TERM_KILL;
4793 opt->jo_term_kill = get_tv_string_chk(item);
4794 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004795#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004796 else if (STRCMP(hi->hi_key, "env") == 0)
4797 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004798 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004799 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004800 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004801 opt->jo_env = item->vval.v_dict;
4802 ++item->vval.v_dict->dv_refcount;
4803 }
4804 else if (STRCMP(hi->hi_key, "cwd") == 0)
4805 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004806 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004807 break;
4808 opt->jo_cwd = get_tv_string_buf_chk(item, opt->jo_cwd_buf);
4809 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd))
4810 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004811 EMSG2(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004812 return FAIL;
4813 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004814 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004815 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004816 else if (STRCMP(hi->hi_key, "waittime") == 0)
4817 {
4818 if (!(supported & JO_WAITTIME))
4819 break;
4820 opt->jo_set |= JO_WAITTIME;
4821 opt->jo_waittime = get_tv_number(item);
4822 }
4823 else if (STRCMP(hi->hi_key, "timeout") == 0)
4824 {
4825 if (!(supported & JO_TIMEOUT))
4826 break;
4827 opt->jo_set |= JO_TIMEOUT;
4828 opt->jo_timeout = get_tv_number(item);
4829 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004830 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004831 {
4832 if (!(supported & JO_OUT_TIMEOUT))
4833 break;
4834 opt->jo_set |= JO_OUT_TIMEOUT;
4835 opt->jo_out_timeout = get_tv_number(item);
4836 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004837 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004838 {
4839 if (!(supported & JO_ERR_TIMEOUT))
4840 break;
4841 opt->jo_set |= JO_ERR_TIMEOUT;
4842 opt->jo_err_timeout = get_tv_number(item);
4843 }
4844 else if (STRCMP(hi->hi_key, "part") == 0)
4845 {
4846 if (!(supported & JO_PART))
4847 break;
4848 opt->jo_set |= JO_PART;
4849 val = get_tv_string(item);
4850 if (STRCMP(val, "err") == 0)
4851 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02004852 else if (STRCMP(val, "out") == 0)
4853 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004854 else
4855 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004856 EMSG3(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004857 return FAIL;
4858 }
4859 }
4860 else if (STRCMP(hi->hi_key, "id") == 0)
4861 {
4862 if (!(supported & JO_ID))
4863 break;
4864 opt->jo_set |= JO_ID;
4865 opt->jo_id = get_tv_number(item);
4866 }
4867 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
4868 {
4869 if (!(supported & JO_STOPONEXIT))
4870 break;
4871 opt->jo_set |= JO_STOPONEXIT;
4872 opt->jo_stoponexit = get_tv_string_buf_chk(item,
4873 opt->jo_soe_buf);
4874 if (opt->jo_stoponexit == NULL)
4875 {
Bram Moolenaarb3292fa2018-02-13 15:17:58 +01004876 EMSG2(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004877 return FAIL;
4878 }
4879 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004880 else if (STRCMP(hi->hi_key, "block_write") == 0)
4881 {
4882 if (!(supported & JO_BLOCK_WRITE))
4883 break;
4884 opt->jo_set |= JO_BLOCK_WRITE;
4885 opt->jo_block_write = get_tv_number(item);
4886 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004887 else
4888 break;
4889 --todo;
4890 }
4891 if (todo > 0)
4892 {
4893 EMSG2(_(e_invarg2), hi->hi_key);
4894 return FAIL;
4895 }
4896
4897 return OK;
4898}
4899
4900/*
4901 * Get the channel from the argument.
4902 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02004903 * When "check_open" is TRUE check that the channel can be used.
4904 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004905 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004906 */
4907 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004908get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004909{
Bram Moolenaar437905c2016-04-26 19:01:05 +02004910 channel_T *channel = NULL;
4911 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004912
4913 if (tv->v_type == VAR_JOB)
4914 {
4915 if (tv->vval.v_job != NULL)
4916 channel = tv->vval.v_job->jv_channel;
4917 }
4918 else if (tv->v_type == VAR_CHANNEL)
4919 {
4920 channel = tv->vval.v_channel;
4921 }
4922 else
4923 {
4924 EMSG2(_(e_invarg2), get_tv_string(tv));
4925 return NULL;
4926 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02004927 if (channel != NULL && reading)
4928 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004929 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004930
Bram Moolenaar437905c2016-04-26 19:01:05 +02004931 if (check_open && (channel == NULL || (!channel_is_open(channel)
4932 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004933 {
4934 EMSG(_("E906: not an open channel"));
4935 return NULL;
4936 }
4937 return channel;
4938}
4939
4940static job_T *first_job = NULL;
4941
4942 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004943job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004944{
4945 ch_log(job->jv_channel, "Freeing job");
4946 if (job->jv_channel != NULL)
4947 {
4948 /* The link from the channel to the job doesn't count as a reference,
4949 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004950 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004951 * NULL the reference. We don't set ch_job_killed, unreferencing the
4952 * job doesn't mean it stops running. */
4953 job->jv_channel->ch_job = NULL;
4954 channel_unref(job->jv_channel);
4955 }
4956 mch_clear_job(job);
4957
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02004958 vim_free(job->jv_tty_in);
4959 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004960 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02004961 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004962}
4963
4964 static void
4965job_free_job(job_T *job)
4966{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004967 if (job->jv_next != NULL)
4968 job->jv_next->jv_prev = job->jv_prev;
4969 if (job->jv_prev == NULL)
4970 first_job = job->jv_next;
4971 else
4972 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004973 vim_free(job);
4974}
4975
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004976 static void
4977job_free(job_T *job)
4978{
4979 if (!in_free_unref_items)
4980 {
4981 job_free_contents(job);
4982 job_free_job(job);
4983 }
4984}
4985
Bram Moolenaar7df915d2016-11-17 17:25:32 +01004986#if defined(EXITFREE) || defined(PROTO)
4987 void
4988job_free_all(void)
4989{
4990 while (first_job != NULL)
4991 job_free(first_job);
4992}
4993#endif
4994
4995/*
4996 * Return TRUE if we need to check if the process of "job" has ended.
4997 */
4998 static int
4999job_need_end_check(job_T *job)
5000{
5001 return job->jv_status == JOB_STARTED
5002 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5003}
5004
5005/*
5006 * Return TRUE if the channel of "job" is still useful.
5007 */
5008 static int
5009job_channel_still_useful(job_T *job)
5010{
5011 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5012}
5013
5014/*
5015 * Return TRUE if the job should not be freed yet. Do not free the job when
5016 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5017 * or when the associated channel will do something with the job output.
5018 */
5019 static int
5020job_still_useful(job_T *job)
5021{
5022 return job_need_end_check(job) || job_channel_still_useful(job);
5023}
5024
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005025#if !defined(USE_ARGV) || defined(PROTO)
5026/*
5027 * Escape one argument for an external command.
5028 * Returns the escaped string in allocated memory. NULL when out of memory.
5029 */
5030 static char_u *
5031win32_escape_arg(char_u *arg)
5032{
5033 int slen, dlen;
5034 int escaping = 0;
5035 int i;
5036 char_u *s, *d;
5037 char_u *escaped_arg;
5038 int has_spaces = FALSE;
5039
5040 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005041 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005042 dlen = slen;
5043 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5044 {
5045 if (*s == '"' || *s == '\\')
5046 ++dlen;
5047 if (*s == ' ' || *s == '\t')
5048 has_spaces = TRUE;
5049 }
5050
5051 if (has_spaces)
5052 dlen += 2;
5053
5054 if (dlen == slen)
5055 return vim_strsave(arg);
5056
5057 /* Allocate memory for the result and fill it. */
5058 escaped_arg = alloc(dlen + 1);
5059 if (escaped_arg == NULL)
5060 return NULL;
5061 memset(escaped_arg, 0, dlen+1);
5062
5063 d = escaped_arg;
5064
5065 if (has_spaces)
5066 *d++ = '"';
5067
5068 for (s = arg; *s != NUL;)
5069 {
5070 switch (*s)
5071 {
5072 case '"':
5073 for (i = 0; i < escaping; i++)
5074 *d++ = '\\';
5075 escaping = 0;
5076 *d++ = '\\';
5077 *d++ = *s++;
5078 break;
5079 case '\\':
5080 escaping++;
5081 *d++ = *s++;
5082 break;
5083 default:
5084 escaping = 0;
5085 MB_COPY_CHAR(s, d);
5086 break;
5087 }
5088 }
5089
5090 /* add terminating quote and finish with a NUL */
5091 if (has_spaces)
5092 {
5093 for (i = 0; i < escaping; i++)
5094 *d++ = '\\';
5095 *d++ = '"';
5096 }
5097 *d = NUL;
5098
5099 return escaped_arg;
5100}
5101
5102/*
5103 * Build a command line from a list, taking care of escaping.
5104 * The result is put in gap->ga_data.
5105 * Returns FAIL when out of memory.
5106 */
5107 int
5108win32_build_cmd(list_T *l, garray_T *gap)
5109{
5110 listitem_T *li;
5111 char_u *s;
5112
5113 for (li = l->lv_first; li != NULL; li = li->li_next)
5114 {
5115 s = get_tv_string_chk(&li->li_tv);
5116 if (s == NULL)
5117 return FAIL;
5118 s = win32_escape_arg(s);
5119 if (s == NULL)
5120 return FAIL;
5121 ga_concat(gap, s);
5122 vim_free(s);
5123 if (li->li_next != NULL)
5124 ga_append(gap, ' ');
5125 }
5126 return OK;
5127}
5128#endif
5129
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005130/*
5131 * NOTE: Must call job_cleanup() only once right after the status of "job"
5132 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5133 * mch_detect_ended_job() returned non-NULL).
5134 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005135 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005136job_cleanup(job_T *job)
5137{
5138 if (job->jv_status != JOB_ENDED)
5139 return;
5140
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005141 /* Ready to cleanup the job. */
5142 job->jv_status = JOB_FINISHED;
5143
Bram Moolenaar97792de2016-10-15 18:36:49 +02005144 if (job->jv_exit_cb != NULL)
5145 {
5146 typval_T argv[3];
5147 typval_T rettv;
5148 int dummy;
5149
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005150 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005151 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005152 ++job->jv_refcount;
5153 argv[0].v_type = VAR_JOB;
5154 argv[0].vval.v_job = job;
5155 argv[1].v_type = VAR_NUMBER;
5156 argv[1].vval.v_number = job->jv_exitval;
5157 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5158 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5159 job->jv_exit_partial, NULL);
5160 clear_tv(&rettv);
5161 --job->jv_refcount;
5162 channel_need_redraw = TRUE;
5163 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005164
5165 /* Do not free the job in case the close callback of the associated channel
5166 * isn't invoked yet and may get information by job_info(). */
5167 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005168 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005169 /* The job was already unreferenced and the associated channel was
5170 * detached, now that it ended it can be freed. Careful: caller must
5171 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005172 job_free(job);
5173 }
5174}
5175
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005176/*
5177 * Mark references in jobs that are still useful.
5178 */
5179 int
5180set_ref_in_job(int copyID)
5181{
5182 int abort = FALSE;
5183 job_T *job;
5184 typval_T tv;
5185
5186 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005187 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005188 {
5189 tv.v_type = VAR_JOB;
5190 tv.vval.v_job = job;
5191 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5192 }
5193 return abort;
5194}
5195
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005196/*
5197 * Dereference "job". Note that after this "job" may have been freed.
5198 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005199 void
5200job_unref(job_T *job)
5201{
5202 if (job != NULL && --job->jv_refcount <= 0)
5203 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005204 /* Do not free the job if there is a channel where the close callback
5205 * may get the job info. */
5206 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005207 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005208 /* Do not free the job when it has not ended yet and there is a
5209 * "stoponexit" flag or an exit callback. */
5210 if (!job_need_end_check(job))
5211 {
5212 job_free(job);
5213 }
5214 else if (job->jv_channel != NULL)
5215 {
5216 /* Do remove the link to the channel, otherwise it hangs
5217 * around until Vim exits. See job_free() for refcount. */
5218 ch_log(job->jv_channel, "detaching channel from job");
5219 job->jv_channel->ch_job = NULL;
5220 channel_unref(job->jv_channel);
5221 job->jv_channel = NULL;
5222 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005223 }
5224 }
5225}
5226
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005227 int
5228free_unused_jobs_contents(int copyID, int mask)
5229{
5230 int did_free = FALSE;
5231 job_T *job;
5232
5233 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005234 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005235 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005236 {
5237 /* Free the channel and ordinary items it contains, but don't
5238 * recurse into Lists, Dictionaries etc. */
5239 job_free_contents(job);
5240 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005241 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005242 return did_free;
5243}
5244
5245 void
5246free_unused_jobs(int copyID, int mask)
5247{
5248 job_T *job;
5249 job_T *job_next;
5250
5251 for (job = first_job; job != NULL; job = job_next)
5252 {
5253 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005254 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005255 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005256 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005257 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005258 job_free_job(job);
5259 }
5260 }
5261}
5262
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005263/*
5264 * Allocate a job. Sets the refcount to one and sets options default.
5265 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005266 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005267job_alloc(void)
5268{
5269 job_T *job;
5270
5271 job = (job_T *)alloc_clear(sizeof(job_T));
5272 if (job != NULL)
5273 {
5274 job->jv_refcount = 1;
5275 job->jv_stoponexit = vim_strsave((char_u *)"term");
5276
5277 if (first_job != NULL)
5278 {
5279 first_job->jv_prev = job;
5280 job->jv_next = first_job;
5281 }
5282 first_job = job;
5283 }
5284 return job;
5285}
5286
5287 void
5288job_set_options(job_T *job, jobopt_T *opt)
5289{
5290 if (opt->jo_set & JO_STOPONEXIT)
5291 {
5292 vim_free(job->jv_stoponexit);
5293 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5294 job->jv_stoponexit = NULL;
5295 else
5296 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5297 }
5298 if (opt->jo_set & JO_EXIT_CB)
5299 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005300 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005301 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005302 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005303 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005304 job->jv_exit_partial = NULL;
5305 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005306 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005307 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005308 job->jv_exit_partial = opt->jo_exit_partial;
5309 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005310 {
5311 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005312 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005313 }
5314 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005315 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005316 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005317 func_ref(job->jv_exit_cb);
5318 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005319 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005320 }
5321}
5322
5323/*
5324 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5325 */
5326 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005327job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005328{
5329 job_T *job;
5330
5331 for (job = first_job; job != NULL; job = job->jv_next)
5332 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005333 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005334}
5335
5336/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005337 * Return TRUE when there is any job that has an exit callback and might exit,
5338 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005339 */
5340 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005341has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005342{
5343 job_T *job;
5344
5345 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005346 /* Only should check if the channel has been closed, if the channel is
5347 * open the job won't exit. */
5348 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005349 && !job_channel_still_useful(job))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005350 return TRUE;
5351 return FALSE;
5352}
5353
Bram Moolenaar97792de2016-10-15 18:36:49 +02005354#define MAX_CHECK_ENDED 8
5355
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005356/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005357 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005358 */
5359 void
5360job_check_ended(void)
5361{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005362 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005363
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005364 if (first_job == NULL)
5365 return;
5366
Bram Moolenaar97792de2016-10-15 18:36:49 +02005367 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005368 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005369 /* NOTE: mch_detect_ended_job() must only return a job of which the
5370 * status was just set to JOB_ENDED. */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005371 job_T *job = mch_detect_ended_job(first_job);
5372
5373 if (job == NULL)
5374 break;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005375 job_cleanup(job); /* may free "job" */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005376 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005377
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005378 if (channel_need_redraw)
5379 {
5380 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005381 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005382 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005383}
5384
5385/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005386 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005387 * "argv_arg" is only for Unix.
5388 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005389 * The returned job has a refcount of one.
5390 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005391 */
5392 job_T *
Bram Moolenaar13568252018-03-16 20:46:58 +01005393job_start(typval_T *argvars, char **argv_arg, jobopt_T *opt_arg)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005394{
5395 job_T *job;
5396 char_u *cmd = NULL;
5397#if defined(UNIX)
5398# define USE_ARGV
5399 char **argv = NULL;
5400 int argc = 0;
5401#else
5402 garray_T ga;
5403#endif
5404 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005405 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005406
5407 job = job_alloc();
5408 if (job == NULL)
5409 return NULL;
5410
5411 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005412#ifndef USE_ARGV
5413 ga_init2(&ga, (int)sizeof(char*), 20);
5414#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005415
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005416 if (opt_arg != NULL)
5417 opt = *opt_arg;
5418 else
5419 {
5420 /* Default mode is NL. */
5421 clear_job_options(&opt);
5422 opt.jo_mode = MODE_NL;
5423 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005424 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5425 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5426 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005427 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005428 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005429
5430 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005431 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005432 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5433 && opt.jo_io[part] == JIO_FILE
5434 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5435 || *opt.jo_io_name[part] == NUL))
5436 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005437 EMSG(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005438 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005439 }
5440
5441 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5442 {
5443 buf_T *buf = NULL;
5444
5445 /* check that we can find the buffer before starting the job */
5446 if (opt.jo_set & JO_IN_BUF)
5447 {
5448 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5449 if (buf == NULL)
5450 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5451 }
5452 else if (!(opt.jo_set & JO_IN_NAME))
5453 {
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005454 EMSG(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005455 }
5456 else
5457 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5458 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005459 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005460 if (buf->b_ml.ml_mfp == NULL)
5461 {
5462 char_u numbuf[NUMBUFLEN];
5463 char_u *s;
5464
5465 if (opt.jo_set & JO_IN_BUF)
5466 {
5467 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5468 s = numbuf;
5469 }
5470 else
5471 s = opt.jo_io_name[PART_IN];
5472 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005473 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005474 }
5475 job->jv_in_buf = buf;
5476 }
5477
5478 job_set_options(job, &opt);
5479
Bram Moolenaar13568252018-03-16 20:46:58 +01005480#ifdef USE_ARGV
5481 if (argv_arg != NULL)
5482 {
5483 argv = argv_arg;
5484 }
5485 else
5486#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005487 if (argvars[0].v_type == VAR_STRING)
5488 {
5489 /* Command is a string. */
5490 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005491 if (cmd == NULL || *cmd == NUL)
5492 {
5493 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005494 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005495 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005496#ifdef USE_ARGV
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005497 /* This will modify "cmd". */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005498 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005499 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005500 argv[argc] = NULL;
5501#endif
5502 }
5503 else if (argvars[0].v_type != VAR_LIST
5504 || argvars[0].vval.v_list == NULL
5505 || argvars[0].vval.v_list->lv_len < 1)
5506 {
5507 EMSG(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005508 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005509 }
5510 else
5511 {
5512 list_T *l = argvars[0].vval.v_list;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005513#ifdef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005514 listitem_T *li;
5515 char_u *s;
5516
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005517 /* Pass argv[] to mch_call_shell(). */
5518 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
5519 if (argv == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005520 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005521 for (li = l->lv_first; li != NULL; li = li->li_next)
5522 {
5523 s = get_tv_string_chk(&li->li_tv);
5524 if (s == NULL)
5525 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005526 argv[argc++] = (char *)s;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005527 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005528 argv[argc] = NULL;
5529#else
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005530 if (win32_build_cmd(l, &ga) == FAIL)
5531 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005532 cmd = ga.ga_data;
5533#endif
5534 }
5535
5536#ifdef USE_ARGV
5537 if (ch_log_active())
5538 {
5539 garray_T ga;
5540 int i;
5541
5542 ga_init2(&ga, (int)sizeof(char), 200);
5543 for (i = 0; i < argc; ++i)
5544 {
5545 if (i > 0)
5546 ga_concat(&ga, (char_u *)" ");
5547 ga_concat(&ga, (char_u *)argv[i]);
5548 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005549 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005550 ga_clear(&ga);
5551 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005552 mch_job_start(argv, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005553#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005554 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005555 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005556#endif
5557
5558 /* If the channel is reading from a buffer, write lines now. */
5559 if (job->jv_channel != NULL)
5560 channel_write_in(job->jv_channel);
5561
5562theend:
5563#ifdef USE_ARGV
Bram Moolenaar13568252018-03-16 20:46:58 +01005564 if (argv != argv_arg)
5565 vim_free(argv);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005566#else
5567 vim_free(ga.ga_data);
5568#endif
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005569 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005570 return job;
5571}
5572
5573/*
5574 * Get the status of "job" and invoke the exit callback when needed.
5575 * The returned string is not allocated.
5576 */
5577 char *
5578job_status(job_T *job)
5579{
5580 char *result;
5581
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005582 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005583 /* No need to check, dead is dead. */
5584 result = "dead";
5585 else if (job->jv_status == JOB_FAILED)
5586 result = "fail";
5587 else
5588 {
5589 result = mch_job_status(job);
5590 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005591 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005592 }
5593 return result;
5594}
5595
Bram Moolenaar8950a562016-03-12 15:22:55 +01005596/*
5597 * Implementation of job_info().
5598 */
5599 void
5600job_info(job_T *job, dict_T *dict)
5601{
5602 dictitem_T *item;
5603 varnumber_T nr;
5604
5605 dict_add_nr_str(dict, "status", 0L, (char_u *)job_status(job));
5606
5607 item = dictitem_alloc((char_u *)"channel");
5608 if (item == NULL)
5609 return;
5610 item->di_tv.v_lock = 0;
5611 item->di_tv.v_type = VAR_CHANNEL;
5612 item->di_tv.vval.v_channel = job->jv_channel;
5613 if (job->jv_channel != NULL)
5614 ++job->jv_channel->ch_refcount;
5615 if (dict_add(dict, item) == FAIL)
5616 dictitem_free(item);
5617
5618#ifdef UNIX
5619 nr = job->jv_pid;
5620#else
5621 nr = job->jv_proc_info.dwProcessId;
5622#endif
5623 dict_add_nr_str(dict, "process", nr, NULL);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005624 dict_add_nr_str(dict, "tty_in", 0L,
5625 job->jv_tty_in != NULL ? job->jv_tty_in : (char_u *)"");
5626 dict_add_nr_str(dict, "tty_out", 0L,
5627 job->jv_tty_out != NULL ? job->jv_tty_out : (char_u *)"");
Bram Moolenaar8950a562016-03-12 15:22:55 +01005628
5629 dict_add_nr_str(dict, "exitval", job->jv_exitval, NULL);
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005630 dict_add_nr_str(dict, "exit_cb", 0L, job->jv_exit_cb);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005631 dict_add_nr_str(dict, "stoponexit", 0L, job->jv_stoponexit);
5632}
5633
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005634/*
5635 * Send a signal to "job". Implements job_stop().
5636 * When "type" is not NULL use this for the type.
5637 * Otherwise use argvars[1] for the type.
5638 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005639 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005640job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005641{
5642 char_u *arg;
5643
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005644 if (type != NULL)
5645 arg = (char_u *)type;
5646 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005647 arg = (char_u *)"";
5648 else
5649 {
5650 arg = get_tv_string_chk(&argvars[1]);
5651 if (arg == NULL)
5652 {
5653 EMSG(_(e_invarg));
5654 return 0;
5655 }
5656 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005657 if (job->jv_status == JOB_FAILED)
5658 {
5659 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5660 return 0;
5661 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005662 if (job->jv_status == JOB_ENDED)
5663 {
5664 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5665 return 0;
5666 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005667 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005668 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005669 return 0;
5670
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005671 /* Assume that only "kill" will kill the job. */
5672 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005673 job->jv_channel->ch_job_killed = TRUE;
5674
5675 /* We don't try freeing the job, obviously the caller still has a
5676 * reference to it. */
5677 return 1;
5678}
5679
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005680#endif /* FEAT_JOB_CHANNEL */