blob: 3a80748a1259123102235abcf3e57031776eb32c [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{
Bram Moolenaarb091f302019-01-19 14:37:00 +010083 HANDLE h = (HANDLE)fd;
84 DWORD nwrite;
85 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010086
Bram Moolenaarb091f302019-01-19 14:37:00 +010087 // If the pipe overflows while the job does not read the data, WriteFile
88 // will block forever. This abandons the write.
89 memset(&ov, 0, sizeof(ov));
90 if (!WriteFile(h, buf, (DWORD)len, &nwrite, &ov))
91 {
92 DWORD err = GetLastError();
93
94 if (err != ERROR_IO_PENDING)
95 return -1;
96 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
97 return -1;
98 FlushFileBuffers(h);
99 }
Bram Moolenaard8070362016-02-15 21:56:54 +0100100 return (int)nwrite;
101}
102
103 static void
104fd_close(sock_T fd)
105{
106 HANDLE h = (HANDLE)fd;
107
108 CloseHandle(h);
109}
110#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100111
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100112/* Log file opened with ch_logfile(). */
113static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100114#ifdef FEAT_RELTIME
115static proftime_T log_start;
116#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100117
118 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100119ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100120{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100121 FILE *file = NULL;
122
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100123 if (log_fd != NULL)
124 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100125
126 if (*fname != NUL)
127 {
128 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
129 if (file == NULL)
130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100131 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100132 return;
133 }
134 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100136
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100138 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100139 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100140#ifdef FEAT_RELTIME
141 profile_start(&log_start);
142#endif
143 }
144}
145
146 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200147ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100148{
149 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100150}
151
152 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200153ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154{
155 if (log_fd != NULL)
156 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100157#ifdef FEAT_RELTIME
158 proftime_T log_now;
159
160 profile_start(&log_now);
161 profile_sub(&log_now, &log_start);
162 fprintf(log_fd, "%s ", profile_msg(&log_now));
163#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100164 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200165 {
166 if (part < PART_COUNT)
167 fprintf(log_fd, "%son %d(%s): ",
168 what, ch->ch_id, part_names[part]);
169 else
170 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
171 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100172 else
173 fprintf(log_fd, "%s: ", what);
174 }
175}
176
Bram Moolenaard0b65022016-03-06 21:50:33 +0100177static int did_log_msg = TRUE;
178
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200179#ifndef PROTO /* prototype is in vim.h */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100180 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200181ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100182{
183 if (log_fd != NULL)
184 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200185 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100186
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200187 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200188 va_start(ap, fmt);
189 vfprintf(log_fd, fmt, ap);
190 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100191 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100193 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100194 }
195}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200196#endif
197
198 static void
199ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200200#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
201 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200202#endif
203 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100204
205 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200206ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100207{
208 if (log_fd != NULL)
209 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200210 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100211
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200212 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200213 va_start(ap, fmt);
214 vfprintf(log_fd, fmt, ap);
215 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100216 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100217 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100218 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219 }
220}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100221
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100222#ifdef _WIN32
223# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100224# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100225
226 static char *
227strerror_win32(int eno)
228{
229 static LPVOID msgbuf = NULL;
230 char_u *ptr;
231
232 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200233 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100234 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200235 msgbuf = NULL;
236 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100237 FormatMessage(
238 FORMAT_MESSAGE_ALLOCATE_BUFFER |
239 FORMAT_MESSAGE_FROM_SYSTEM |
240 FORMAT_MESSAGE_IGNORE_INSERTS,
241 NULL,
242 eno,
243 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
244 (LPTSTR) &msgbuf,
245 0,
246 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200247 if (msgbuf != NULL)
248 /* chomp \r or \n */
249 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
250 switch (*ptr)
251 {
252 case '\r':
253 STRMOVE(ptr, ptr + 1);
254 ptr--;
255 break;
256 case '\n':
257 if (*(ptr + 1) == '\0')
258 *ptr = '\0';
259 else
260 *ptr = ' ';
261 break;
262 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100263 return msgbuf;
264}
265#endif
266
Bram Moolenaar77073442016-02-13 23:23:53 +0100267/*
268 * The list of all allocated channels.
269 */
270static channel_T *first_channel = NULL;
271static int next_ch_id = 0;
272
273/*
274 * Allocate a new channel. The refcount is set to 1.
275 * The channel isn't actually used until it is opened.
276 * Returns NULL if out of memory.
277 */
278 channel_T *
279add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100280{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200281 ch_part_T part;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100282 channel_T *channel = (channel_T *)alloc_clear((int)sizeof(channel_T));
Bram Moolenaare0874f82016-01-24 20:36:41 +0100283
Bram Moolenaar77073442016-02-13 23:23:53 +0100284 if (channel == NULL)
285 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100286
Bram Moolenaar77073442016-02-13 23:23:53 +0100287 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100288 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100289
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200290 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100291 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100292 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100293#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100294 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100295#endif
296#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100297 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100298#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100299 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100300 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100301
Bram Moolenaar77073442016-02-13 23:23:53 +0100302 if (first_channel != NULL)
303 {
304 first_channel->ch_prev = channel;
305 channel->ch_next = first_channel;
306 }
307 first_channel = channel;
308
309 channel->ch_refcount = 1;
310 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100311}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100312
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200313 int
314has_any_channel(void)
315{
316 return first_channel != NULL;
317}
318
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100319/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100320 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100321 * Return TRUE if "channel" has a callback and the associated job wasn't
322 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100323 */
324 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100325channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100326{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100327 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100328 int has_out_msg;
329 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100330
331 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100332 if (channel->ch_job_killed && channel->ch_job == NULL)
333 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100334
335 /* If there is a close callback it may still need to be invoked. */
336 if (channel->ch_close_cb != NULL)
337 return TRUE;
338
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200339 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200340 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200341 return TRUE;
342
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 /* If there is no callback then nobody can get readahead. If the fd is
344 * closed and there is no readahead then the callback won't be called. */
345 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100346 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
347 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
349 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
350 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
351 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
352 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
353 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100354 return (channel->ch_callback != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100355 || has_out_msg || has_err_msg))
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200356 || ((channel->ch_part[PART_OUT].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200357 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
358 && has_out_msg)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200359 || ((channel->ch_part[PART_ERR].ch_callback != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200360 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
361 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100362}
363
364/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200365 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
366 */
367 static int
368channel_can_close(channel_T *channel)
369{
370 return channel->ch_to_be_closed == 0;
371}
372
373/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200374 * Close a channel and free all its resources.
375 */
376 static void
377channel_free_contents(channel_T *channel)
378{
379 channel_close(channel, TRUE);
380 channel_clear(channel);
381 ch_log(channel, "Freeing channel");
382}
383
384 static void
385channel_free_channel(channel_T *channel)
386{
387 if (channel->ch_next != NULL)
388 channel->ch_next->ch_prev = channel->ch_prev;
389 if (channel->ch_prev == NULL)
390 first_channel = channel->ch_next;
391 else
392 channel->ch_prev->ch_next = channel->ch_next;
393 vim_free(channel);
394}
395
396 static void
397channel_free(channel_T *channel)
398{
399 if (!in_free_unref_items)
400 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200401 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200402 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200403 else
404 {
405 channel_free_contents(channel);
406 channel_free_channel(channel);
407 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200408 }
409}
410
411/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100412 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100413 * possible, there is no callback to be invoked or the associated job was
414 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100415 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100416 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100417 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100418channel_may_free(channel_T *channel)
419{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100420 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100421 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100422 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100423 return TRUE;
424 }
425 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100426}
427
428/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100429 * Decrement the reference count on "channel" and maybe free it when it goes
430 * down to zero. Don't free it if there is a pending action.
431 * Returns TRUE when the channel is no longer referenced.
432 */
433 int
434channel_unref(channel_T *channel)
435{
436 if (channel != NULL && --channel->ch_refcount <= 0)
437 return channel_may_free(channel);
438 return FALSE;
439}
440
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200441 int
442free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100443{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200444 int did_free = FALSE;
445 channel_T *ch;
446
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200447 /* This is invoked from the garbage collector, which only runs at a safe
448 * point. */
449 ++safe_to_invoke_callback;
450
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200451 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200452 if (!channel_still_useful(ch)
453 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200454 {
455 /* Free the channel and ordinary items it contains, but don't
456 * recurse into Lists, Dictionaries etc. */
457 channel_free_contents(ch);
458 did_free = TRUE;
459 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200460
461 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200462 return did_free;
463}
464
465 void
466free_unused_channels(int copyID, int mask)
467{
468 channel_T *ch;
469 channel_T *ch_next;
470
471 for (ch = first_channel; ch != NULL; ch = ch_next)
472 {
473 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200474 if (!channel_still_useful(ch)
475 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200476 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200477 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200478 channel_free_channel(ch);
479 }
480 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100481}
482
Bram Moolenaard04a0202016-01-26 23:30:18 +0100483#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100484
485#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
486 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100487channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100488{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100489 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200490 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100491
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100492 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100493 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200494 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100495 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200496 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100497}
498#endif
499
Bram Moolenaare0874f82016-01-24 20:36:41 +0100500/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100501 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100502 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100503#ifdef FEAT_GUI_X11
504 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200505messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200506 int *unused1 UNUSED,
507 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100508{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100509 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100510}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100511#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100512
Bram Moolenaard04a0202016-01-26 23:30:18 +0100513#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100514# if GTK_CHECK_VERSION(3,0,0)
515 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200516messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200517 GIOCondition unused2 UNUSED,
518 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100519{
520 channel_read_fd(GPOINTER_TO_INT(clientData));
521 return TRUE; /* Return FALSE instead in case the event source is to
522 * be removed after this function returns. */
523}
524# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100525 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200526messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200527 gint unused1 UNUSED,
528 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100529{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100530 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100531}
Bram Moolenaar98921892016-02-23 17:14:37 +0100532# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100533#endif
534
535 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200536channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100537{
Bram Moolenaarde279892016-03-11 22:19:44 +0100538 if (!CH_HAS_GUI)
539 return;
540
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200541 /* gets stuck in handling events for a not connected channel */
542 if (channel->ch_keep_open)
543 return;
544
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100545# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200546 /* Tell notifier we are interested in being called when there is input on
547 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100548 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200549 {
550 ch_log(channel, "Registering part %s with fd %d",
551 part_names[part], channel->ch_part[part].ch_fd);
552
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100553 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100554 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100555 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100556 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200557 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100558 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200559 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560# else
561# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200562 /* Tell gdk we are interested in being called when there is input on the
563 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100564 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100565 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200566 ch_log(channel, "Registering part %s with fd %d",
567 part_names[part], channel->ch_part[part].ch_fd);
568# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100569 GIOChannel *chnnl = g_io_channel_unix_new(
570 (gint)channel->ch_part[part].ch_fd);
571
572 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
573 chnnl,
574 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200575 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100576 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
577
578 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100579# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100580 channel->ch_part[part].ch_inputHandler = gdk_input_add(
581 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100582 (GdkInputCondition)
583 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200584 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100585 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100586# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200587 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100588# endif
589# endif
590}
591
Bram Moolenaarde279892016-03-11 22:19:44 +0100592 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100593channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100594{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100595 if (channel->CH_SOCK_FD != INVALID_FD)
596 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200597 if (channel->CH_OUT_FD != INVALID_FD
598 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100599 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200600 if (channel->CH_ERR_FD != INVALID_FD
601 && channel->CH_ERR_FD != channel->CH_SOCK_FD
602 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100603 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100604}
605
606/*
607 * Register any of our file descriptors with the GUI event handling system.
608 * Called when the GUI has started.
609 */
610 void
611channel_gui_register_all(void)
612{
Bram Moolenaar77073442016-02-13 23:23:53 +0100613 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100614
Bram Moolenaar77073442016-02-13 23:23:53 +0100615 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100616 channel_gui_register(channel);
617}
618
619 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200620channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100621{
622# ifdef FEAT_GUI_X11
623 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
624 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200625 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100626 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
627 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
628 }
629# else
630# ifdef FEAT_GUI_GTK
631 if (channel->ch_part[part].ch_inputHandler != 0)
632 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200633 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100634# if GTK_CHECK_VERSION(3,0,0)
635 g_source_remove(channel->ch_part[part].ch_inputHandler);
636# else
637 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
638# endif
639 channel->ch_part[part].ch_inputHandler = 0;
640 }
641# endif
642# endif
643}
644
645 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100646channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100647{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200648 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100649
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100650 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100651 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100652}
653
654#endif
655
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100656static char *e_cannot_connect = N_("E902: Cannot connect to port");
657
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100659 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100660 * "waittime" is the time in msec to wait for the connection.
661 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100662 * Returns the channel for success.
663 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100664 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100665 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100666channel_open(
667 char *hostname,
668 int port_in,
669 int waittime,
670 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100671{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100672 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100674 struct hostent *host;
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100675#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100676 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100677 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100678#else
679 int port = port_in;
680#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100681 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100682 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100683
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100684#ifdef WIN32
Bram Moolenaard04a0202016-01-26 23:30:18 +0100685 channel_init_winsock();
686#endif
687
Bram Moolenaar77073442016-02-13 23:23:53 +0100688 channel = add_channel();
689 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100690 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100691 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100692 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693 }
694
695 /* Get the server internet address and put into addr structure */
696 /* fill in the socket address structure and connect to server */
697 vim_memset((char *)&server, 0, sizeof(server));
698 server.sin_family = AF_INET;
699 server.sin_port = htons(port);
700 if ((host = gethostbyname(hostname)) == NULL)
701 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100702 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200703 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100704 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100705 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100706 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100707 {
708 char *p;
709
Bram Moolenaar2e324952018-04-14 14:37:07 +0200710 /* When using host->h_addr_list[0] directly ubsan warns for it to not
711 * be aligned. First copy the pointer to avoid that. */
712 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100713 memcpy((char *)&server.sin_addr, p, host->h_length);
714 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100715
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100716 /* On Mac and Solaris a zero timeout almost never works. At least wait
717 * one millisecond. Let's do it for all systems, because we don't know why
718 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100719 if (waittime == 0)
720 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100721
722 /*
723 * For Unix we need to call connect() again after connect() failed.
724 * On Win32 one time is sufficient.
725 */
726 while (TRUE)
727 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100728 long elapsed_msec = 0;
729 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100730
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100731 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100732 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100733 sd = socket(AF_INET, SOCK_STREAM, 0);
734 if (sd == -1)
735 {
736 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200737 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100738 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100739 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100740 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100741
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100742 if (waittime >= 0)
743 {
744 /* Make connect() non-blocking. */
745 if (
746#ifdef _WIN32
747 ioctlsocket(sd, FIONBIO, &val) < 0
748#else
749 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
750#endif
751 )
752 {
753 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200754 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100755 "channel_open: Connect failed with errno %d", errno);
756 sock_close(sd);
757 channel_free(channel);
758 return NULL;
759 }
760 }
761
762 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200763 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100764 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
765
Bram Moolenaar045a2842016-03-08 22:33:07 +0100766 if (ret == 0)
767 /* The connection could be established. */
768 break;
769
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100771 if (waittime < 0 || (errno != EWOULDBLOCK
772 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100773#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100774 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100775#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100776 ))
777 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200778 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +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;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100784 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100785
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100786 /* Limit the waittime to 50 msec. If it doesn't work within this
787 * time we close the socket and try creating it again. */
788 waitnow = waittime > 50 ? 50 : waittime;
789
Bram Moolenaar045a2842016-03-08 22:33:07 +0100790 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100791 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100792#ifndef WIN32
793 if (errno != ECONNREFUSED)
794#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100795 {
796 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100797 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100798 fd_set wfds;
Bram Moolenaare081e212016-02-28 22:33:46 +0100799#ifndef WIN32
Bram Moolenaard42119f2016-02-28 20:51:49 +0100800 int so_error = 0;
801 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100802 struct timeval start_tv;
803 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100804#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100805 FD_ZERO(&rfds);
806 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100807 FD_ZERO(&wfds);
808 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100809
Bram Moolenaar562ca712016-03-09 21:50:05 +0100810 tv.tv_sec = waitnow / 1000;
811 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100812#ifndef WIN32
813 gettimeofday(&start_tv, NULL);
814#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200815 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100816 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100817 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100818
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100819 if (ret < 0)
820 {
821 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200822 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100823 "channel_open: Connect failed with errno %d", errno);
824 PERROR(_(e_cannot_connect));
825 sock_close(sd);
826 channel_free(channel);
827 return NULL;
828 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100829
Bram Moolenaare081e212016-02-28 22:33:46 +0100830#ifdef WIN32
Bram Moolenaar562ca712016-03-09 21:50:05 +0100831 /* On Win32: select() is expected to work and wait for up to
832 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100833 if (FD_ISSET(sd, &wfds))
834 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100835 elapsed_msec = waitnow;
836 if (waittime > 1 && elapsed_msec < waittime)
837 {
838 waittime -= elapsed_msec;
839 continue;
840 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100841#else
842 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100843 * After putting the socket in non-blocking mode, connect() will
844 * return EINPROGRESS, select() will not wait (as if writing is
845 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100846 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100847 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100848 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100849 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100850 {
851 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100852 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100853 if (ret < 0 || (so_error != 0
854 && so_error != EWOULDBLOCK
855 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100856# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100857 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100858# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100859 ))
860 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200861 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100862 "channel_open: Connect failed with errno %d",
863 so_error);
864 PERROR(_(e_cannot_connect));
865 sock_close(sd);
866 channel_free(channel);
867 return NULL;
868 }
869 }
870
Bram Moolenaar045a2842016-03-08 22:33:07 +0100871 if (FD_ISSET(sd, &wfds) && so_error == 0)
872 /* Did not detect an error, connection is established. */
873 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100874
Bram Moolenaar045a2842016-03-08 22:33:07 +0100875 gettimeofday(&end_tv, NULL);
876 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
877 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100878#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100879 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100880
881#ifndef WIN32
882 if (waittime > 1 && elapsed_msec < waittime)
883 {
884 /* The port isn't ready but we also didn't get an error.
885 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100886 * yet. Select() may return early, wait until the remaining
887 * "waitnow" and try again. */
888 waitnow -= elapsed_msec;
889 waittime -= elapsed_msec;
890 if (waitnow > 0)
891 {
892 mch_delay((long)waitnow, TRUE);
893 ui_breakcheck();
894 waittime -= waitnow;
895 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100896 if (!got_int)
897 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100898 if (waittime <= 0)
899 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100900 waittime = 1;
901 continue;
902 }
903 /* we were interrupted, behave as if timed out */
904 }
905#endif
906
907 /* We timed out. */
908 ch_error(channel, "Connection timed out");
909 sock_close(sd);
910 channel_free(channel);
911 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100912 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100913
Bram Moolenaar045a2842016-03-08 22:33:07 +0100914 ch_log(channel, "Connection made");
915
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100916 if (waittime >= 0)
917 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100918#ifdef _WIN32
919 val = 0;
920 ioctlsocket(sd, FIONBIO, &val);
921#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100922 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100923#endif
924 }
925
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100926 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100927 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100928 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
929 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200930 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100931
932#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100933 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100934#endif
935
Bram Moolenaar77073442016-02-13 23:23:53 +0100936 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100937}
938
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100939/*
940 * Implements ch_open().
941 */
942 channel_T *
943channel_open_func(typval_T *argvars)
944{
945 char_u *address;
946 char_u *p;
947 char *rest;
948 int port;
949 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200950 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100951
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100952 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100953 if (argvars[1].v_type != VAR_UNKNOWN
954 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100956 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100957 return NULL;
958 }
959
960 /* parse address */
961 p = vim_strchr(address, ':');
962 if (p == NULL)
963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100964 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100965 return NULL;
966 }
967 *p++ = NUL;
968 port = strtol((char *)p, &rest, 10);
969 if (*address == NUL || port <= 0 || *rest != NUL)
970 {
971 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100973 return NULL;
974 }
975
976 /* parse options */
977 clear_job_options(&opt);
978 opt.jo_mode = MODE_JSON;
979 opt.jo_timeout = 2000;
980 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200981 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200982 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100983 if (opt.jo_timeout < 0)
984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100985 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200986 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100987 }
988
989 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
990 if (channel != NULL)
991 {
992 opt.jo_set = JO_ALL;
993 channel_set_options(channel, &opt);
994 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +0200995theend:
996 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100997 return channel;
998}
999
Bram Moolenaarde279892016-03-11 22:19:44 +01001000 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001001ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001002{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001003 sock_T *fd = &channel->ch_part[part].ch_fd;
1004
Bram Moolenaarde279892016-03-11 22:19:44 +01001005 if (*fd != INVALID_FD)
1006 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001007 if (part == PART_SOCK)
1008 sock_close(*fd);
1009 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001010 {
1011 /* When using a pty the same FD is set on multiple parts, only
1012 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001013 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1014 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1015 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001016 {
1017#ifdef WIN32
1018 if (channel->ch_named_pipe)
1019 DisconnectNamedPipe((HANDLE)fd);
1020#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001021 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001022 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001023 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001024 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001025
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001026 /* channel is closed, may want to end the job if it was the last */
1027 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001028 }
1029}
1030
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001031 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001032channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001033{
Bram Moolenaarde279892016-03-11 22:19:44 +01001034 if (in != INVALID_FD)
1035 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001036 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001037 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001038# if defined(UNIX)
1039 /* Do not end the job when all output channels are closed, wait until
1040 * the job ended. */
1041 if (isatty(in))
1042 channel->ch_to_be_closed |= (1U << PART_IN);
1043# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001044 }
1045 if (out != INVALID_FD)
1046 {
1047# if defined(FEAT_GUI)
1048 channel_gui_unregister_one(channel, PART_OUT);
1049# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001050 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001051 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001052 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001053# if defined(FEAT_GUI)
1054 channel_gui_register_one(channel, PART_OUT);
1055# endif
1056 }
1057 if (err != INVALID_FD)
1058 {
1059# if defined(FEAT_GUI)
1060 channel_gui_unregister_one(channel, PART_ERR);
1061# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001062 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001063 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001064 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001065# if defined(FEAT_GUI)
1066 channel_gui_register_one(channel, PART_ERR);
1067# endif
1068 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001069}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001070
Bram Moolenaard6051b52016-02-28 15:49:03 +01001071/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001072 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001073 * This does not keep a refcount, when the job is freed ch_job is cleared.
1074 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001075 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001076channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001077{
Bram Moolenaar77073442016-02-13 23:23:53 +01001078 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001079
1080 channel_set_options(channel, options);
1081
1082 if (job->jv_in_buf != NULL)
1083 {
1084 chanpart_T *in_part = &channel->ch_part[PART_IN];
1085
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001086 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001087 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001088 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001089 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001090 {
1091 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1092 {
1093 /* Special mode: send last-but-one line when appending a line
1094 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001095 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001096 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001097 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001098 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001099 }
1100 else
1101 in_part->ch_buf_top = options->jo_in_top;
1102 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001103 else
1104 in_part->ch_buf_top = 1;
1105 if (options->jo_set & JO_IN_BOT)
1106 in_part->ch_buf_bot = options->jo_in_bot;
1107 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001108 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001109 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001110}
1111
1112/*
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001113 * Prepare buffer "buf" for writing channel output to.
1114 */
1115 static void
1116prepare_buffer(buf_T *buf)
1117{
1118 buf_T *save_curbuf = curbuf;
1119
1120 buf_copy_options(buf, BCO_ENTER);
1121 curbuf = buf;
1122#ifdef FEAT_QUICKFIX
1123 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1124 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1125#endif
1126 if (curbuf->b_ml.ml_mfp == NULL)
1127 ml_open(curbuf);
1128 curbuf = save_curbuf;
1129}
1130
1131/*
Bram Moolenaar187db502016-02-27 14:44:26 +01001132 * Find a buffer matching "name" or create a new one.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001133 * Returns NULL if there is something very wrong (error already reported).
Bram Moolenaar187db502016-02-27 14:44:26 +01001134 */
1135 static buf_T *
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001136find_buffer(char_u *name, int err, int msg)
Bram Moolenaar187db502016-02-27 14:44:26 +01001137{
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001138 buf_T *buf = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01001139 buf_T *save_curbuf = curbuf;
1140
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001141 if (name != NULL && *name != NUL)
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001142 {
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001143 buf = buflist_findname(name);
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001144 if (buf == NULL)
1145 buf = buflist_findname_exp(name);
1146 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001147 if (buf == NULL)
1148 {
Bram Moolenaare26643e2016-02-27 21:53:02 +01001149 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001150 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001151 if (buf == NULL)
1152 return NULL;
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001153 prepare_buffer(buf);
1154
Bram Moolenaara4f6ca72016-03-20 17:28:35 +01001155 curbuf = buf;
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001156 if (msg)
1157 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001158 : "Reading from channel output..."), TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01001159 changed_bytes(1, 0);
1160 curbuf = save_curbuf;
1161 }
1162
1163 return buf;
1164}
1165
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001166 static void
1167set_callback(
1168 char_u **cbp,
1169 partial_T **pp,
1170 char_u *callback,
1171 partial_T *partial)
1172{
1173 free_callback(*cbp, *pp);
1174 if (callback != NULL && *callback != NUL)
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001175 {
1176 if (partial != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001177 *cbp = partial_name(partial);
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001178 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001179 {
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001180 *cbp = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001181 func_ref(*cbp);
1182 }
Bram Moolenaar5ef2e762016-07-15 21:29:35 +02001183 }
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001184 else
1185 *cbp = NULL;
1186 *pp = partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001187 if (partial != NULL)
1188 ++partial->pt_refcount;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001189}
1190
Bram Moolenaar187db502016-02-27 14:44:26 +01001191/*
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001192 * Set various properties from an "opt" argument.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001193 */
1194 void
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001195channel_set_options(channel_T *channel, jobopt_T *opt)
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001196{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001197 ch_part_T part;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001198
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001199 if (opt->jo_set & JO_MODE)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001200 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001201 channel->ch_part[part].ch_mode = opt->jo_mode;
1202 if (opt->jo_set & JO_IN_MODE)
1203 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1204 if (opt->jo_set & JO_OUT_MODE)
1205 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1206 if (opt->jo_set & JO_ERR_MODE)
1207 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
Bram Moolenaar0b146882018-09-06 16:27:24 +02001208 channel->ch_nonblock = opt->jo_noblock;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001209
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001210 if (opt->jo_set & JO_TIMEOUT)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001211 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001212 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1213 if (opt->jo_set & JO_OUT_TIMEOUT)
1214 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1215 if (opt->jo_set & JO_ERR_TIMEOUT)
1216 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001217 if (opt->jo_set & JO_BLOCK_WRITE)
1218 channel->ch_part[PART_IN].ch_block_write = 1;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001219
1220 if (opt->jo_set & JO_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001221 set_callback(&channel->ch_callback, &channel->ch_partial,
1222 opt->jo_callback, opt->jo_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001223 if (opt->jo_set & JO_OUT_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001224 set_callback(&channel->ch_part[PART_OUT].ch_callback,
1225 &channel->ch_part[PART_OUT].ch_partial,
1226 opt->jo_out_cb, opt->jo_out_partial);
Bram Moolenaarb6b52522016-02-20 23:30:07 +01001227 if (opt->jo_set & JO_ERR_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001228 set_callback(&channel->ch_part[PART_ERR].ch_callback,
1229 &channel->ch_part[PART_ERR].ch_partial,
1230 opt->jo_err_cb, opt->jo_err_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01001231 if (opt->jo_set & JO_CLOSE_CALLBACK)
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02001232 set_callback(&channel->ch_close_cb, &channel->ch_close_partial,
1233 opt->jo_close_cb, opt->jo_close_partial);
Bram Moolenaar958dc692016-12-01 15:34:12 +01001234 channel->ch_drop_never = opt->jo_drop_never;
Bram Moolenaar187db502016-02-27 14:44:26 +01001235
1236 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1237 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001238 buf_T *buf;
1239
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01001240 /* writing output to a buffer. Default mode is NL. */
1241 if (!(opt->jo_set & JO_OUT_MODE))
1242 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001243 if (opt->jo_set & JO_OUT_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001244 {
1245 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1246 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001247 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001248 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001249 else
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001250 {
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001251 int msg = TRUE;
1252
1253 if (opt->jo_set2 & JO2_OUT_MSG)
1254 msg = opt->jo_message[PART_OUT];
1255 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001256 }
1257 if (buf != NULL)
1258 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001259 if (opt->jo_set & JO_OUT_MODIFIABLE)
1260 channel->ch_part[PART_OUT].ch_nomodifiable =
1261 !opt->jo_modifiable[PART_OUT];
1262
1263 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001265 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001266 }
1267 else
1268 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001269 ch_log(channel, "writing out to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001270 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001271 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001272 // if the buffer was deleted or unloaded resurrect it
1273 if (buf->b_ml.ml_mfp == NULL)
1274 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001275 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001276 }
Bram Moolenaar187db502016-02-27 14:44:26 +01001277 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001278
1279 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1280 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1281 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1282 {
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001283 buf_T *buf;
1284
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001285 /* writing err to a buffer. Default mode is NL. */
1286 if (!(opt->jo_set & JO_ERR_MODE))
1287 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1288 if (opt->jo_io[PART_ERR] == JIO_OUT)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001289 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +01001290 else if (opt->jo_set & JO_ERR_BUF)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001291 {
1292 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1293 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001294 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001295 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001296 else
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001297 {
1298 int msg = TRUE;
1299
1300 if (opt->jo_set2 & JO2_ERR_MSG)
1301 msg = opt->jo_message[PART_ERR];
1302 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1303 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001304 if (buf != NULL)
1305 {
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001306 if (opt->jo_set & JO_ERR_MODIFIABLE)
1307 channel->ch_part[PART_ERR].ch_nomodifiable =
1308 !opt->jo_modifiable[PART_ERR];
1309 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001311 emsg(_(e_modifiable));
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001312 }
1313 else
1314 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001315 ch_log(channel, "writing err to buffer '%s'",
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001316 (char *)buf->b_ffname);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001317 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
Bram Moolenaar8b62d872019-01-05 00:02:57 +01001318 // if the buffer was deleted or unloaded resurrect it
1319 if (buf->b_ml.ml_mfp == NULL)
1320 prepare_buffer(buf);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001321 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001322 }
Bram Moolenaar6ff02c92016-03-08 20:12:44 +01001323 }
Bram Moolenaar03602ec2016-03-20 20:57:45 +01001324
1325 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1326 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1327 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01001328}
1329
1330/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001331 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001332 */
1333 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001334channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001335 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001336 ch_part_T part,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001337 char_u *callback,
1338 partial_T *partial,
1339 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001340{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001341 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001342 cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T));
1343
1344 if (item != NULL)
1345 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001346 item->cq_partial = partial;
1347 if (partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001348 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001349 ++partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001350 item->cq_callback = callback;
1351 }
1352 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001353 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02001354 item->cq_callback = vim_strsave(callback);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001355 func_ref(item->cq_callback);
1356 }
Bram Moolenaar77073442016-02-13 23:23:53 +01001357 item->cq_seq_nr = id;
1358 item->cq_prev = head->cq_prev;
1359 head->cq_prev = item;
1360 item->cq_next = NULL;
1361 if (item->cq_prev == NULL)
1362 head->cq_next = item;
1363 else
1364 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001365 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001366}
1367
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001368 static void
1369write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1370{
1371 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001372 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001373 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001374 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375
Bram Moolenaar655da312016-05-28 22:22:34 +02001376 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001377 if ((p = alloc(len + 2)) == NULL)
1378 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001379 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001380
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001381 if (channel->ch_write_text_mode)
1382 p[len] = CAR;
1383 else
1384 {
1385 for (i = 0; i < len; ++i)
1386 if (p[i] == NL)
1387 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001388
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001389 p[len] = NL;
1390 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001391 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001392 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001393 vim_free(p);
1394}
1395
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001396/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001397 * Return TRUE if "channel" can be written to.
1398 * Returns FALSE if the input is closed or the write would block.
1399 */
1400 static int
1401can_write_buf_line(channel_T *channel)
1402{
1403 chanpart_T *in_part = &channel->ch_part[PART_IN];
1404
1405 if (in_part->ch_fd == INVALID_FD)
1406 return FALSE; /* pipe was closed */
1407
1408 /* for testing: block every other attempt to write */
1409 if (in_part->ch_block_write == 1)
1410 in_part->ch_block_write = -1;
1411 else if (in_part->ch_block_write == -1)
1412 in_part->ch_block_write = 1;
1413
1414 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
1415#ifndef WIN32
1416 {
1417# if defined(HAVE_SELECT)
1418 struct timeval tval;
1419 fd_set wfds;
1420 int ret;
1421
1422 FD_ZERO(&wfds);
1423 FD_SET((int)in_part->ch_fd, &wfds);
1424 tval.tv_sec = 0;
1425 tval.tv_usec = 0;
1426 for (;;)
1427 {
1428 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1429# ifdef EINTR
1430 SOCK_ERRNO;
1431 if (ret == -1 && errno == EINTR)
1432 continue;
1433# endif
1434 if (ret <= 0 || in_part->ch_block_write == 1)
1435 {
1436 if (ret > 0)
1437 ch_log(channel, "FAKED Input not ready for writing");
1438 else
1439 ch_log(channel, "Input not ready for writing");
1440 return FALSE;
1441 }
1442 break;
1443 }
1444# else
1445 struct pollfd fds;
1446
1447 fds.fd = in_part->ch_fd;
1448 fds.events = POLLOUT;
1449 if (poll(&fds, 1, 0) <= 0)
1450 {
1451 ch_log(channel, "Input not ready for writing");
1452 return FALSE;
1453 }
1454 if (in_part->ch_block_write == 1)
1455 {
1456 ch_log(channel, "FAKED Input not ready for writing");
1457 return FALSE;
1458 }
1459# endif
1460 }
1461#endif
1462 return TRUE;
1463}
1464
1465/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001466 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001467 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001468 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001469channel_write_in(channel_T *channel)
1470{
1471 chanpart_T *in_part = &channel->ch_part[PART_IN];
1472 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001473 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001474 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001475
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001476 if (buf == NULL || in_part->ch_buf_append)
1477 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001478 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001479 {
1480 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001481 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001482 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001483 return;
1484 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001485
1486 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1487 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1488 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001489 if (!can_write_buf_line(channel))
1490 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001491 write_buf_line(buf, lnum, channel);
1492 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001493 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001494
1495 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001496 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001497 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001498 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001499
Bram Moolenaar014069a2016-03-03 22:51:40 +01001500 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001501 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001502 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001503#if defined(FEAT_TERMINAL)
1504 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001505 if (channel->ch_job != NULL)
1506 term_send_eof(channel);
1507#endif
1508
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001509 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001510 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001511 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001512
1513 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001514 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001515 }
1516 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001517 ch_log(channel, "Still %ld more lines to write",
1518 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001519}
1520
1521/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001522 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001523 */
1524 void
1525channel_buffer_free(buf_T *buf)
1526{
1527 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001528 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001529
1530 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001531 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001532 {
1533 chanpart_T *ch_part = &channel->ch_part[part];
1534
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001535 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001536 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001537 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001538 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001539 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001540 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001541 }
1542}
1543
1544/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001545 * Write any lines waiting to be written to "channel".
1546 */
1547 static void
1548channel_write_input(channel_T *channel)
1549{
1550 chanpart_T *in_part = &channel->ch_part[PART_IN];
1551
1552 if (in_part->ch_writeque.wq_next != NULL)
1553 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1554 else if (in_part->ch_bufref.br_buf != NULL)
1555 {
1556 if (in_part->ch_buf_append)
1557 channel_write_new_lines(in_part->ch_bufref.br_buf);
1558 else
1559 channel_write_in(channel);
1560 }
1561}
1562
1563/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001564 * Write any lines waiting to be written to a channel.
1565 */
1566 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001567channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001568{
1569 channel_T *channel;
1570
1571 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001572 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001573}
1574
1575/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001576 * Write appended lines above the last one in "buf" to the channel.
1577 */
1578 void
1579channel_write_new_lines(buf_T *buf)
1580{
1581 channel_T *channel;
1582 int found_one = FALSE;
1583
1584 /* There could be more than one channel for the buffer, loop over all of
1585 * them. */
1586 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1587 {
1588 chanpart_T *in_part = &channel->ch_part[PART_IN];
1589 linenr_T lnum;
1590 int written = 0;
1591
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001592 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001593 {
1594 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001595 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001596 found_one = TRUE;
1597 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1598 ++lnum)
1599 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001600 if (!can_write_buf_line(channel))
1601 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001602 write_buf_line(buf, lnum, channel);
1603 ++written;
1604 }
1605
1606 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001607 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001608 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001609 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001610 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001611 ch_log(channel, "Still %ld more lines to write",
1612 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001613
1614 in_part->ch_buf_bot = lnum;
1615 }
1616 }
1617 if (!found_one)
1618 buf->b_write_to_channel = FALSE;
1619}
1620
1621/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001622 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001623 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001624 */
1625 static void
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001626invoke_callback(channel_T *channel, char_u *callback, partial_T *partial,
1627 typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001628{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001629 typval_T rettv;
1630 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001631
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001632 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001633 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001634
Bram Moolenaar77073442016-02-13 23:23:53 +01001635 argv[0].v_type = VAR_CHANNEL;
1636 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001637
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001638 call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, NULL,
1639 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001640 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001641 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001642}
1643
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001644/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001645 * Return the first node from "channel"/"part" without removing it.
1646 * Returns NULL if there is nothing.
1647 */
1648 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001649channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001650{
1651 readq_T *head = &channel->ch_part[part].ch_head;
1652
1653 return head->rq_next;
1654}
1655
1656/*
1657 * Return a pointer to the first NL in "node".
1658 * Skips over NUL characters.
1659 * Returns NULL if there is no NL.
1660 */
1661 char_u *
1662channel_first_nl(readq_T *node)
1663{
1664 char_u *buffer = node->rq_buffer;
1665 long_u i;
1666
1667 for (i = 0; i < node->rq_buflen; ++i)
1668 if (buffer[i] == NL)
1669 return buffer + i;
1670 return NULL;
1671}
1672
1673/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001674 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001675 * The caller must free it.
1676 * Returns NULL if there is nothing.
1677 */
1678 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001680{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001681 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001682 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001683 char_u *p;
1684
Bram Moolenaar77073442016-02-13 23:23:53 +01001685 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001687 if (outlen != NULL)
1688 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001690 p = node->rq_buffer;
1691 head->rq_next = node->rq_next;
1692 if (node->rq_next == NULL)
1693 head->rq_prev = NULL;
1694 else
1695 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001696 vim_free(node);
1697 return p;
1698}
1699
1700/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001701 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001702 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001703 */
1704 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001705channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001706{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001707 readq_T *head = &channel->ch_part[part].ch_head;
1708 readq_T *node = head->rq_next;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001709 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001710 char_u *res;
1711 char_u *p;
1712
1713 /* If there is only one buffer just get that one. */
1714 if (head->rq_next == NULL || head->rq_next->rq_next == NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 return channel_get(channel, part, outlen);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001716
1717 /* Concatenate everything into one buffer. */
1718 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001719 len += node->rq_buflen;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001720 res = lalloc(len + 1, TRUE);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001721 if (res == NULL)
1722 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001723 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001724 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001726 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001727 p += node->rq_buflen;
1728 }
1729 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001730
1731 /* Free all buffers */
1732 do
1733 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001734 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001735 vim_free(p);
1736 } while (p != NULL);
1737
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001738 if (outlen != NULL)
1739 {
1740 *outlen += len;
1741 return res;
1742 }
1743
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001744 /* turn all NUL into NL */
1745 while (len > 0)
1746 {
1747 --len;
1748 if (res[len] == NUL)
1749 res[len] = NL;
1750 }
1751
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001752 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001753}
1754
1755/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001756 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001757 * Caller must check these bytes are available.
1758 */
1759 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001760channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001761{
1762 readq_T *head = &channel->ch_part[part].ch_head;
1763 readq_T *node = head->rq_next;
1764 char_u *buf = node->rq_buffer;
1765
1766 mch_memmove(buf, buf + len, node->rq_buflen - len);
1767 node->rq_buflen -= len;
1768}
1769
1770/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001771 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001772 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001773 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001774 */
1775 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001776channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001777{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001778 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001779 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001780 readq_T *last_node;
1781 readq_T *n;
1782 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001783 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001784 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001785
Bram Moolenaar77073442016-02-13 23:23:53 +01001786 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001787 return FAIL;
1788
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001789 last_node = node->rq_next;
1790 len = node->rq_buflen + last_node->rq_buflen + 1;
1791 if (want_nl)
1792 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001793 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001794 {
1795 last_node = last_node->rq_next;
1796 len += last_node->rq_buflen;
1797 }
1798
1799 p = newbuf = alloc(len);
1800 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001802 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001803 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001804 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001805 node->rq_buffer = newbuf;
1806 for (n = node; n != last_node; )
1807 {
1808 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001809 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001810 p += n->rq_buflen;
1811 vim_free(n->rq_buffer);
1812 }
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001813 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001814
1815 /* dispose of the collapsed nodes and their buffers */
1816 for (n = node->rq_next; n != last_node; )
1817 {
1818 n = n->rq_next;
1819 vim_free(n->rq_prev);
1820 }
1821 node->rq_next = last_node->rq_next;
1822 if (last_node->rq_next == NULL)
1823 head->rq_prev = node;
1824 else
1825 last_node->rq_next->rq_prev = node;
1826 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001827 return OK;
1828}
1829
1830/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001831 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001832 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001833 * Returns OK or FAIL.
1834 */
1835 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001836channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001837 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001838{
1839 readq_T *node;
1840 readq_T *head = &channel->ch_part[part].ch_head;
1841 char_u *p;
1842 int i;
1843
1844 node = (readq_T *)alloc(sizeof(readq_T));
1845 if (node == NULL)
1846 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001847 /* A NUL is added at the end, because netbeans code expects that.
1848 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001849 node->rq_buffer = alloc(len + 1);
1850 if (node->rq_buffer == NULL)
1851 {
1852 vim_free(node);
1853 return FAIL; /* out of memory */
1854 }
1855
1856 if (channel->ch_part[part].ch_mode == MODE_NL)
1857 {
1858 /* Drop any CR before a NL. */
1859 p = node->rq_buffer;
1860 for (i = 0; i < len; ++i)
1861 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1862 *p++ = buf[i];
1863 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001864 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001865 }
1866 else
1867 {
1868 mch_memmove(node->rq_buffer, buf, len);
1869 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001870 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001871 }
1872
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001873 if (prepend)
1874 {
1875 /* preend node to the head of the queue */
1876 node->rq_next = head->rq_next;
1877 node->rq_prev = NULL;
1878 if (head->rq_next == NULL)
1879 head->rq_prev = node;
1880 else
1881 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001882 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001883 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001884 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001885 {
1886 /* append node to the tail of the queue */
1887 node->rq_next = NULL;
1888 node->rq_prev = head->rq_prev;
1889 if (head->rq_prev == NULL)
1890 head->rq_next = node;
1891 else
1892 head->rq_prev->rq_next = node;
1893 head->rq_prev = node;
1894 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001895
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001896 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001897 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001898 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001899 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001900 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001901 fprintf(log_fd, "'\n");
1902 }
1903 return OK;
1904}
1905
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001906/*
1907 * Try to fill the buffer of "reader".
1908 * Returns FALSE when nothing was added.
1909 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 static int
1911channel_fill(js_read_T *reader)
1912{
1913 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001914 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001915 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001916 int keeplen;
1917 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001918 char_u *p;
1919
1920 if (next == NULL)
1921 return FALSE;
1922
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001923 keeplen = reader->js_end - reader->js_buf;
1924 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001925 {
1926 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001927 addlen = (int)STRLEN(next);
1928 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001929 if (p == NULL)
1930 {
1931 vim_free(next);
1932 return FALSE;
1933 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001934 mch_memmove(p, reader->js_buf, keeplen);
1935 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001936 vim_free(next);
1937 next = p;
1938 }
1939
1940 vim_free(reader->js_buf);
1941 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001942 return TRUE;
1943}
1944
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001945/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001946 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001947 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001948 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001949 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001950 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001951channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001952{
1953 js_read_T reader;
1954 typval_T listtv;
1955 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001956 chanpart_T *chanpart = &channel->ch_part[part];
1957 jsonq_T *head = &chanpart->ch_json_head;
1958 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001959 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001960
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001961 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001962 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001963
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001964 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001965 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001966 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001967 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001968 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969
1970 /* When a message is incomplete we wait for a short while for more to
1971 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001972 * or list will make us hang.
1973 * Do not generate error messages, they will be written in a channel log. */
1974 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001975 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001976 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001977 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001978 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001979 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01001980 /* Only accept the response when it is a list with at least two
1981 * items. */
1982 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001983 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001984 if (listtv.v_type != VAR_LIST)
1985 ch_error(channel, "Did not receive a list, discarding");
1986 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001987 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001988 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001989 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01001990 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001991 else
1992 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01001993 item = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
1994 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001995 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001996 else
1997 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01001998 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01001999 item->jq_value = alloc_tv();
2000 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002001 {
2002 vim_free(item);
2003 clear_tv(&listtv);
2004 }
2005 else
2006 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002007 *item->jq_value = listtv;
2008 item->jq_prev = head->jq_prev;
2009 head->jq_prev = item;
2010 item->jq_next = NULL;
2011 if (item->jq_prev == NULL)
2012 head->jq_next = item;
2013 else
2014 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002015 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 }
2017 }
2018 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002019
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002020 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002021 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002022 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002023 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002024 size_t buflen = STRLEN(reader.js_buf);
2025
2026 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002027 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002028 /* First time encountering incomplete message or after receiving
2029 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002030 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002031 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002032 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002033 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002034 chanpart->ch_wait_len = buflen;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002035#ifdef WIN32
2036 chanpart->ch_deadline = GetTickCount() + 100L;
2037#else
2038 gettimeofday(&chanpart->ch_deadline, NULL);
2039 chanpart->ch_deadline.tv_usec += 100 * 1000;
2040 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2041 {
2042 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2043 ++chanpart->ch_deadline.tv_sec;
2044 }
2045#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002046 }
2047 else
2048 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002049 int timeout;
2050#ifdef WIN32
2051 timeout = GetTickCount() > chanpart->ch_deadline;
2052#else
2053 {
2054 struct timeval now_tv;
2055
2056 gettimeofday(&now_tv, NULL);
2057 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2058 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2059 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2060 }
2061#endif
2062 if (timeout)
2063 {
2064 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002065 chanpart->ch_wait_len = 0;
2066 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002067 }
2068 else
2069 {
2070 reader.js_used = 0;
2071 ch_log(channel, "still waiting on incomplete message");
2072 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002073 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002074 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002075
2076 if (status == FAIL)
2077 {
2078 ch_error(channel, "Decoding failed - discarding input");
2079 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002080 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002081 }
2082 else if (reader.js_buf[reader.js_used] != NUL)
2083 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002084 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002085 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002086 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2087 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002088 ret = status == MAYBE ? FALSE: TRUE;
2089 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002090 else
2091 ret = FALSE;
2092
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002093 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002094 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002095}
2096
2097/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002098 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002099 */
2100 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002101remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002102{
Bram Moolenaar77073442016-02-13 23:23:53 +01002103 if (node->cq_prev == NULL)
2104 head->cq_next = node->cq_next;
2105 else
2106 node->cq_prev->cq_next = node->cq_next;
2107 if (node->cq_next == NULL)
2108 head->cq_prev = node->cq_prev;
2109 else
2110 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002111}
2112
2113/*
2114 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002115 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002116 */
2117 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002118remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002119{
Bram Moolenaar77073442016-02-13 23:23:53 +01002120 if (node->jq_prev == NULL)
2121 head->jq_next = node->jq_next;
2122 else
2123 node->jq_prev->jq_next = node->jq_next;
2124 if (node->jq_next == NULL)
2125 head->jq_prev = node->jq_prev;
2126 else
2127 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002128 vim_free(node);
2129}
2130
2131/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002132 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002133 * When "id" is positive it must match the first number in the list.
Bram Moolenaare56bf152016-02-08 23:23:42 +01002134 * When "id" is zero or negative jut get the first message. But not the one
2135 * with id ch_block_id.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002136 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002137 * Return OK when found and return the value in "rettv".
2138 * Return FAIL otherwise.
2139 */
2140 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002141channel_get_json(
2142 channel_T *channel,
2143 ch_part_T part,
2144 int id,
2145 int without_callback,
2146 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002147{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002148 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002149 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002150
Bram Moolenaar77073442016-02-13 23:23:53 +01002151 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002152 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002153 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002154 typval_T *tv = &l->lv_first->li_tv;
2155
Bram Moolenaar958dc692016-12-01 15:34:12 +01002156 if ((without_callback || !item->jq_no_callback)
2157 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002158 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002159 || tv->vval.v_number == 0
Bram Moolenaar958dc692016-12-01 15:34:12 +01002160 || tv->vval.v_number != channel->ch_part[part].ch_block_id))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002161 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002162 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002163 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002164 ch_log(channel, "Getting JSON message %ld",
2165 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002166 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002167 return OK;
2168 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002169 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002170 }
2171 return FAIL;
2172}
2173
Bram Moolenaar958dc692016-12-01 15:34:12 +01002174/*
2175 * Put back "rettv" into the JSON queue, there was no callback for it.
2176 * Takes over the values in "rettv".
2177 */
2178 static void
2179channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2180{
2181 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2182 jsonq_T *item = head->jq_next;
2183 jsonq_T *newitem;
2184
2185 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2186 /* last item was pushed back, append to the end */
2187 item = NULL;
2188 else while (item != NULL && item->jq_no_callback)
2189 /* append after the last item that was pushed back */
2190 item = item->jq_next;
2191
2192 newitem = (jsonq_T *)alloc((unsigned)sizeof(jsonq_T));
2193 if (newitem == NULL)
2194 clear_tv(rettv);
2195 else
2196 {
2197 newitem->jq_value = alloc_tv();
2198 if (newitem->jq_value == NULL)
2199 {
2200 vim_free(newitem);
2201 clear_tv(rettv);
2202 }
2203 else
2204 {
2205 newitem->jq_no_callback = FALSE;
2206 *newitem->jq_value = *rettv;
2207 if (item == NULL)
2208 {
2209 /* append to the end */
2210 newitem->jq_prev = head->jq_prev;
2211 head->jq_prev = newitem;
2212 newitem->jq_next = NULL;
2213 if (newitem->jq_prev == NULL)
2214 head->jq_next = newitem;
2215 else
2216 newitem->jq_prev->jq_next = newitem;
2217 }
2218 else
2219 {
2220 /* append after "item" */
2221 newitem->jq_prev = item;
2222 newitem->jq_next = item->jq_next;
2223 item->jq_next = newitem;
2224 if (newitem->jq_next == NULL)
2225 head->jq_prev = newitem;
2226 else
2227 newitem->jq_next->jq_prev = newitem;
2228 }
2229 }
2230 }
2231}
2232
Bram Moolenaarece61b02016-02-20 21:39:05 +01002233#define CH_JSON_MAX_ARGS 4
2234
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002235/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002236 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002237 * "argv[0]" is the command string.
2238 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002239 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002240 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002241channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002242{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002243 char_u *cmd = argv[0].vval.v_string;
2244 char_u *arg;
2245 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002246
Bram Moolenaarece61b02016-02-20 21:39:05 +01002247 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002248 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002249 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002250 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002251 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002252 return;
2253 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002254 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002255 if (arg == NULL)
2256 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002257
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002258 if (STRCMP(cmd, "ex") == 0)
2259 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002260 int save_called_emsg = called_emsg;
2261
2262 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002263 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002264 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002265 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002266 --emsg_silent;
2267 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002268 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002269 (char *)get_vim_var_str(VV_ERRMSG));
2270 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002271 }
2272 else if (STRCMP(cmd, "normal") == 0)
2273 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002274 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002275
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002276 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002277 ea.arg = arg;
2278 ea.addr_count = 0;
2279 ea.forceit = TRUE; /* no mapping */
2280 ex_normal(&ea);
2281 }
2282 else if (STRCMP(cmd, "redraw") == 0)
2283 {
2284 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002285
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002286 ch_log(channel, "redraw");
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002287 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002288 ex_redraw(&ea);
2289 showruler(FALSE);
2290 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002291 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002292 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002293 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002294 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002295 int is_call = cmd[0] == 'c';
2296 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002297
Bram Moolenaarece61b02016-02-20 21:39:05 +01002298 if (argv[id_idx].v_type != VAR_UNKNOWN
2299 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002300 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002301 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002302 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002303 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002304 }
2305 else if (is_call && argv[2].v_type != VAR_LIST)
2306 {
2307 ch_error(channel, "third argument for call must be a list");
2308 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002310 }
2311 else
2312 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002313 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002314 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002315 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002316 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002317
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002318 /* Don't pollute the display with errors. */
2319 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002320 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002321 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002322 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002323 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002324 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002326 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002327 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002328 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2329 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002330 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331
2332 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002333 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002334 int id = argv[id_idx].vval.v_number;
2335
Bram Moolenaar55fab432016-02-07 16:53:13 +01002336 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002337 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002338 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002339 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002340 /* If evaluation failed or the result can't be encoded
2341 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002342 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002343 err_tv.v_type = VAR_STRING;
2344 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002345 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002346 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002347 if (json != NULL)
2348 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002349 channel_send(channel,
2350 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002351 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002352 vim_free(json);
2353 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002354 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002355 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002356 if (tv == &res_tv)
2357 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002358 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002359 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002360 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002361 }
2362 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002363 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002364 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002365 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002366 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002367}
2368
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002369/*
2370 * Invoke the callback at "cbhead".
2371 * Does not redraw but sets channel_need_redraw.
2372 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002373 static void
2374invoke_one_time_callback(
2375 channel_T *channel,
2376 cbq_T *cbhead,
2377 cbq_T *item,
2378 typval_T *argv)
2379{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002380 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002381 (char *)item->cq_callback);
2382 /* Remove the item from the list first, if the callback
2383 * invokes ch_close() the list will be cleared. */
2384 remove_cb_node(cbhead, item);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002385 invoke_callback(channel, item->cq_callback, item->cq_partial, argv);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02002386 free_callback(item->cq_callback, item->cq_partial);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002387 vim_free(item);
2388}
2389
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002390 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002391append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002392{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002393 bufref_T save_curbuf = {NULL, 0, 0};
2394 win_T *save_curwin = NULL;
2395 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002396 linenr_T lnum = buffer->b_ml.ml_line_count;
2397 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002398 chanpart_T *ch_part = &channel->ch_part[part];
2399 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002400 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002401
2402 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2403 {
2404 if (!ch_part->ch_nomod_error)
2405 {
2406 ch_error(channel, "Buffer is not modifiable, cannot append");
2407 ch_part->ch_nomod_error = TRUE;
2408 }
2409 return;
2410 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002411
2412 /* If the buffer is also used as input insert above the last
2413 * line. Don't write these lines. */
2414 if (save_write_to)
2415 {
2416 --lnum;
2417 buffer->b_write_to_channel = FALSE;
2418 }
2419
2420 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002421 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002422
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002423 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002424
2425 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2426 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2427
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002428 u_sync(TRUE);
2429 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002430 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002431
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002432 if (empty)
2433 {
2434 /* The buffer is empty, replace the first (dummy) line. */
2435 ml_replace(lnum, msg, TRUE);
2436 lnum = 0;
2437 }
2438 else
2439 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002440 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002441
2442 /* Restore curbuf/curwin/curtab */
2443 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2444
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002445 if (ch_part->ch_nomodifiable)
2446 buffer->b_p_ma = FALSE;
2447 else
2448 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002449
2450 if (buffer->b_nwindows > 0)
2451 {
2452 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002453
2454 FOR_ALL_WINDOWS(wp)
2455 {
2456 if (wp->w_buffer == buffer
2457 && (save_write_to
2458 ? wp->w_cursor.lnum == lnum + 1
2459 : (wp->w_cursor.lnum == lnum
2460 && wp->w_cursor.col == 0)))
2461 {
2462 ++wp->w_cursor.lnum;
2463 save_curwin = curwin;
2464 curwin = wp;
2465 curbuf = curwin->w_buffer;
2466 scroll_cursor_bot(0, FALSE);
2467 curwin = save_curwin;
2468 curbuf = curwin->w_buffer;
2469 }
2470 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002471 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002472 channel_need_redraw = TRUE;
2473 }
2474
2475 if (save_write_to)
2476 {
2477 channel_T *ch;
2478
2479 /* Find channels reading from this buffer and adjust their
2480 * next-to-read line number. */
2481 buffer->b_write_to_channel = TRUE;
2482 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2483 {
2484 chanpart_T *in_part = &ch->ch_part[PART_IN];
2485
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002486 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002487 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2488 }
2489 }
2490}
2491
Bram Moolenaar437905c2016-04-26 19:01:05 +02002492 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002493drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002494{
2495 char_u *msg;
2496
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002497 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002498 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002499 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002500 vim_free(msg);
2501 }
2502}
2503
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002504/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002505 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002506 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002507 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002508 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002509 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002510may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002511{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002512 char_u *msg = NULL;
2513 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002514 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002515 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002516 chanpart_T *ch_part = &channel->ch_part[part];
2517 ch_mode_T ch_mode = ch_part->ch_mode;
2518 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002519 cbq_T *cbitem;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002520 char_u *callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002521 partial_T *partial = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002522 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002523 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002524
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002525 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002526 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002527 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002528
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002529 /* Use a message-specific callback, part callback or channel callback */
2530 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2531 if (cbitem->cq_seq_nr == 0)
2532 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002533 if (cbitem != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002534 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002535 callback = cbitem->cq_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002536 partial = cbitem->cq_partial;
2537 }
Bram Moolenaarec68a992016-10-03 21:37:41 +02002538 else if (ch_part->ch_callback != NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002539 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002540 callback = ch_part->ch_callback;
2541 partial = ch_part->ch_partial;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002542 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002543 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002544 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002545 callback = channel->ch_callback;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002546 partial = channel->ch_partial;
2547 }
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002548
Bram Moolenaarec68a992016-10-03 21:37:41 +02002549 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002550 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2551 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002552 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002553 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002554 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002555 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002556 buffer = NULL;
2557 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002558
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002559 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002560 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002561 listitem_T *item;
2562 int argc = 0;
2563
Bram Moolenaard7ece102016-02-02 23:23:02 +01002564 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002565 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002566 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002567 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002568 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002569 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002570 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002571 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002572
Bram Moolenaarece61b02016-02-20 21:39:05 +01002573 for (item = listtv->vval.v_list->lv_first;
2574 item != NULL && argc < CH_JSON_MAX_ARGS;
2575 item = item->li_next)
2576 argv[argc++] = item->li_tv;
2577 while (argc < CH_JSON_MAX_ARGS)
2578 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002579
Bram Moolenaarece61b02016-02-20 21:39:05 +01002580 if (argv[0].v_type == VAR_STRING)
2581 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002582 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002583 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002584 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002585 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002586 }
2587
Bram Moolenaarece61b02016-02-20 21:39:05 +01002588 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002589 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002590 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002591 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002592 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002593 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002594 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002595 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002596 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002597 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002598 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002599 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002600 return FALSE;
2601 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002602 else
2603 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002604 /* If there is no callback or buffer drop the message. */
2605 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002606 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002607 /* If there is a close callback it may use ch_read() to get the
2608 * messages. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002609 if (channel->ch_close_cb == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002610 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002611 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002612 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002613
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002614 if (ch_mode == MODE_NL)
2615 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002616 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002617 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002618 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002619
2620 /* See if we have a message ending in NL in the first buffer. If
2621 * not try to concatenate the first and the second buffer. */
2622 while (TRUE)
2623 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002624 node = channel_peek(channel, part);
2625 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002626 if (nl != NULL)
2627 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002628 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002629 {
2630 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2631 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002632 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002633 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002634 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002635 buf = node->rq_buffer;
2636
Bram Moolenaarec68a992016-10-03 21:37:41 +02002637 if (nl == NULL)
2638 {
2639 /* Flush remaining message that is missing a NL. */
Bram Moolenaar866c6882017-04-07 14:02:01 +02002640 char_u *new_buf;
2641
2642 new_buf = vim_realloc(buf, node->rq_buflen + 1);
2643 if (new_buf == NULL)
2644 /* This might fail over and over again, should the message
2645 * be dropped? */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002646 return FALSE;
Bram Moolenaar866c6882017-04-07 14:02:01 +02002647 buf = new_buf;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002648 node->rq_buffer = buf;
2649 nl = buf + node->rq_buflen++;
2650 *nl = NUL;
2651 }
2652
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002653 /* Convert NUL to NL, the internal representation. */
2654 for (p = buf; p < nl && p < buf + node->rq_buflen; ++p)
2655 if (*p == NUL)
2656 *p = NL;
2657
2658 if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar187db502016-02-27 14:44:26 +01002659 {
2660 /* get the whole buffer, drop the NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002661 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002662 *nl = NUL;
2663 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002664 else
2665 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002666 /* Copy the message into allocated memory (excluding the NL)
2667 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002668 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002669 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002670 }
2671 }
2672 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002673 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002674 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002675 * get everything we have.
2676 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002677 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002678 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002679
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002680 if (msg == NULL)
2681 return FALSE; /* out of memory (and avoids Coverity warning) */
2682
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002683 argv[1].v_type = VAR_STRING;
2684 argv[1].vval.v_string = msg;
2685 }
2686
Bram Moolenaara07fec92016-02-05 21:04:08 +01002687 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002688 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002689 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002690
Bram Moolenaar958dc692016-12-01 15:34:12 +01002691 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002692 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002693 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002694 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002695 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002696 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002697 break;
2698 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002699 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002700 {
2701 if (channel->ch_drop_never)
2702 {
2703 /* message must be read with ch_read() */
2704 channel_push_json(channel, part, listtv);
2705 listtv = NULL;
2706 }
2707 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002708 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002709 seq_nr);
2710 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002711 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002712 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002713 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002714 if (buffer != NULL)
2715 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002716 if (msg == NULL)
2717 /* JSON or JS mode: re-encode the message. */
2718 msg = json_encode(listtv, ch_mode);
2719 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002720 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002721#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002722 if (buffer->b_term != NULL)
2723 write_to_term(buffer, msg, channel);
2724 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002725#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002726 append_to_buffer(buffer, msg, channel, part);
2727 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002728 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002729
Bram Moolenaar187db502016-02-27 14:44:26 +01002730 if (callback != NULL)
2731 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002732 if (cbitem != NULL)
2733 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2734 else
2735 {
2736 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002737 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002738 (char *)callback);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002739 invoke_callback(channel, callback, partial, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002740 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002741 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002742 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002743 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002744 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002745
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002746 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002747 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002748 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002749
2750 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002751}
2752
Bram Moolenaar113e1072019-01-20 15:30:40 +01002753#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002754/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002755 * Return TRUE when channel "channel" is open for writing to.
2756 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002757 */
2758 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002759channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002760{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002761 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002762 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002763}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002764#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002765
2766/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002767 * Return TRUE when channel "channel" is open for reading or writing.
2768 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002769 */
2770 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002771channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002772{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002773 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002774 || channel->CH_IN_FD != INVALID_FD
2775 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002776 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002777}
2778
2779/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002780 * Return TRUE if "channel" has JSON or other typeahead.
2781 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002782 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002783channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002784{
2785 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2786
2787 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2788 {
2789 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2790 jsonq_T *item = head->jq_next;
2791
2792 return item != NULL;
2793 }
2794 return channel_peek(channel, part) != NULL;
2795}
2796
2797/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002798 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002799 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002800 */
2801 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002802channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002803{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002804 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002805 int has_readahead = FALSE;
2806
Bram Moolenaar77073442016-02-13 23:23:53 +01002807 if (channel == NULL)
2808 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002809 if (req_part == PART_OUT)
2810 {
2811 if (channel->CH_OUT_FD != INVALID_FD)
2812 return "open";
2813 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002814 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002815 }
2816 else if (req_part == PART_ERR)
2817 {
2818 if (channel->CH_ERR_FD != INVALID_FD)
2819 return "open";
2820 if (channel_has_readahead(channel, PART_ERR))
2821 has_readahead = TRUE;
2822 }
2823 else
2824 {
2825 if (channel_is_open(channel))
2826 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002827 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002828 if (channel_has_readahead(channel, part))
2829 {
2830 has_readahead = TRUE;
2831 break;
2832 }
2833 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002834
2835 if (has_readahead)
2836 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002837 return "closed";
2838}
2839
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002840 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002841channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002842{
2843 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002844 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002845 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002846 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002847 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002848
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002849 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002850 STRCAT(namebuf, "_");
2851 tail = STRLEN(namebuf);
2852
2853 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002854 if (chanpart->ch_fd != INVALID_FD)
2855 status = "open";
2856 else if (channel_has_readahead(channel, part))
2857 status = "buffered";
2858 else
2859 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002860 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002861
2862 STRCPY(namebuf + tail, "mode");
2863 switch (chanpart->ch_mode)
2864 {
2865 case MODE_NL: s = "NL"; break;
2866 case MODE_RAW: s = "RAW"; break;
2867 case MODE_JSON: s = "JSON"; break;
2868 case MODE_JS: s = "JS"; break;
2869 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002870 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002871
2872 STRCPY(namebuf + tail, "io");
2873 if (part == PART_SOCK)
2874 s = "socket";
2875 else switch (chanpart->ch_io)
2876 {
2877 case JIO_NULL: s = "null"; break;
2878 case JIO_PIPE: s = "pipe"; break;
2879 case JIO_FILE: s = "file"; break;
2880 case JIO_BUFFER: s = "buffer"; break;
2881 case JIO_OUT: s = "out"; break;
2882 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002883 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002884
2885 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002886 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002887}
2888
2889 void
2890channel_info(channel_T *channel, dict_T *dict)
2891{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002892 dict_add_number(dict, "id", channel->ch_id);
2893 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002894
2895 if (channel->ch_hostname != NULL)
2896 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002897 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2898 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002899 channel_part_info(channel, dict, "sock", PART_SOCK);
2900 }
2901 else
2902 {
2903 channel_part_info(channel, dict, "out", PART_OUT);
2904 channel_part_info(channel, dict, "err", PART_ERR);
2905 channel_part_info(channel, dict, "in", PART_IN);
2906 }
2907}
2908
Bram Moolenaar77073442016-02-13 23:23:53 +01002909/*
2910 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002911 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002912 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002913 */
2914 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002915channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002916{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002917 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002918
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002919#ifdef FEAT_GUI
2920 channel_gui_unregister(channel);
2921#endif
2922
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002923 ch_close_part(channel, PART_SOCK);
2924 ch_close_part(channel, PART_IN);
2925 ch_close_part(channel, PART_OUT);
2926 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002927
Bram Moolenaara9f02812017-08-05 17:40:38 +02002928 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002929 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002930 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002931
Bram Moolenaara9f02812017-08-05 17:40:38 +02002932 /* Invoke callbacks and flush buffers before the close callback. */
2933 if (channel->ch_close_cb != NULL)
2934 ch_log(channel,
2935 "Invoking callbacks and flushing buffers before closing");
2936 for (part = PART_SOCK; part < PART_IN; ++part)
2937 {
2938 if (channel->ch_close_cb != NULL
2939 || channel->ch_part[part].ch_bufref.br_buf != NULL)
2940 {
2941 /* Increment the refcount to avoid the channel being freed
2942 * halfway. */
2943 ++channel->ch_refcount;
2944 if (channel->ch_close_cb == NULL)
2945 ch_log(channel, "flushing %s buffers before closing",
2946 part_names[part]);
2947 while (may_invoke_callback(channel, part))
2948 ;
2949 --channel->ch_refcount;
2950 }
2951 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002952
Bram Moolenaara9f02812017-08-05 17:40:38 +02002953 if (channel->ch_close_cb != NULL)
2954 {
2955 typval_T argv[1];
2956 typval_T rettv;
2957 int dummy;
2958
2959 /* Increment the refcount to avoid the channel being freed
2960 * halfway. */
2961 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002962 ch_log(channel, "Invoking close callback %s",
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002963 (char *)channel->ch_close_cb);
2964 argv[0].v_type = VAR_CHANNEL;
2965 argv[0].vval.v_channel = channel;
2966 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002967 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002968 channel->ch_close_partial, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02002969 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002970 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002971
Bram Moolenaara9f02812017-08-05 17:40:38 +02002972 /* the callback is only called once */
2973 free_callback(channel->ch_close_cb, channel->ch_close_partial);
2974 channel->ch_close_cb = NULL;
2975 channel->ch_close_partial = NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002976
Bram Moolenaara9f02812017-08-05 17:40:38 +02002977 if (channel_need_redraw)
2978 {
2979 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02002980 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02002981 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02002982
Bram Moolenaara9f02812017-08-05 17:40:38 +02002983 if (!channel->ch_drop_never)
2984 /* any remaining messages are useless now */
2985 for (part = PART_SOCK; part < PART_IN; ++part)
2986 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01002987
2988 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02002989 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002990 }
2991
2992 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002993
2994#ifdef FEAT_TERMINAL
2995 term_channel_closed(channel);
2996#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01002997}
2998
Bram Moolenaard04a0202016-01-26 23:30:18 +01002999/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003000 * Close the "in" part channel "channel".
3001 */
3002 void
3003channel_close_in(channel_T *channel)
3004{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003005 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003006}
3007
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003008 static void
3009remove_from_writeque(writeq_T *wq, writeq_T *entry)
3010{
3011 ga_clear(&entry->wq_ga);
3012 wq->wq_next = entry->wq_next;
3013 if (wq->wq_next == NULL)
3014 wq->wq_prev = NULL;
3015 else
3016 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003017 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003018}
3019
Bram Moolenaar0874a832016-09-01 15:11:51 +02003020/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003021 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003022 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003023 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003024channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003025{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003026 chanpart_T *ch_part = &channel->ch_part[part];
3027 jsonq_T *json_head = &ch_part->ch_json_head;
3028 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003029
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003030 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003031 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003032
3033 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003034 {
3035 cbq_T *node = cb_head->cq_next;
3036
3037 remove_cb_node(cb_head, node);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003038 free_callback(node->cq_callback, node->cq_partial);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003039 vim_free(node);
3040 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003041
3042 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003043 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003044 free_tv(json_head->jq_next->jq_value);
3045 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003046 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003047
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003048 free_callback(ch_part->ch_callback, ch_part->ch_partial);
3049 ch_part->ch_callback = NULL;
3050 ch_part->ch_partial = NULL;
3051
3052 while (ch_part->ch_writeque.wq_next != NULL)
3053 remove_from_writeque(&ch_part->ch_writeque,
3054 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003055}
3056
3057/*
3058 * Clear all the read buffers on "channel".
3059 */
3060 void
3061channel_clear(channel_T *channel)
3062{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003063 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003064 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003065 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003066 channel_clear_one(channel, PART_OUT);
3067 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003068 channel_clear_one(channel, PART_IN);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003069 free_callback(channel->ch_callback, channel->ch_partial);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003070 channel->ch_callback = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003071 channel->ch_partial = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02003072 free_callback(channel->ch_close_cb, channel->ch_close_partial);
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003073 channel->ch_close_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003074 channel->ch_close_partial = NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003075}
3076
Bram Moolenaar77073442016-02-13 23:23:53 +01003077#if defined(EXITFREE) || defined(PROTO)
3078 void
3079channel_free_all(void)
3080{
3081 channel_T *channel;
3082
Bram Moolenaard6051b52016-02-28 15:49:03 +01003083 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003084 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3085 channel_clear(channel);
3086}
3087#endif
3088
3089
Bram Moolenaar715d2852016-04-30 17:06:31 +02003090/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003091#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003092
3093/* Buffer size for reading incoming messages. */
3094#define MAXMSGSIZE 4096
3095
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003096#if defined(HAVE_SELECT)
3097/*
3098 * Add write fds where we are waiting for writing to be possible.
3099 */
3100 static int
3101channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3102{
3103 int maxfd = maxfd_arg;
3104 channel_T *ch;
3105
3106 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3107 {
3108 chanpart_T *in_part = &ch->ch_part[PART_IN];
3109
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003110 if (in_part->ch_fd != INVALID_FD
3111 && (in_part->ch_bufref.br_buf != NULL
3112 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003113 {
3114 FD_SET((int)in_part->ch_fd, wfds);
3115 if ((int)in_part->ch_fd >= maxfd)
3116 maxfd = (int)in_part->ch_fd + 1;
3117 }
3118 }
3119 return maxfd;
3120}
3121#else
3122/*
3123 * Add write fds where we are waiting for writing to be possible.
3124 */
3125 static int
3126channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3127{
3128 int nfd = nfd_in;
3129 channel_T *ch;
3130
3131 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3132 {
3133 chanpart_T *in_part = &ch->ch_part[PART_IN];
3134
Bram Moolenaar683b7962017-08-19 15:51:59 +02003135 if (in_part->ch_fd != INVALID_FD
3136 && (in_part->ch_bufref.br_buf != NULL
3137 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003138 {
3139 in_part->ch_poll_idx = nfd;
3140 fds[nfd].fd = in_part->ch_fd;
3141 fds[nfd].events = POLLOUT;
3142 ++nfd;
3143 }
3144 else
3145 in_part->ch_poll_idx = -1;
3146 }
3147 return nfd;
3148}
3149#endif
3150
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003151typedef enum {
3152 CW_READY,
3153 CW_NOT_READY,
3154 CW_ERROR
3155} channel_wait_result;
3156
Bram Moolenaard04a0202016-01-26 23:30:18 +01003157/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003158 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003159 * Return CW_READY when there is something to read.
3160 * Return CW_NOT_READY when there is nothing to read.
3161 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003162 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003163 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003164channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003165{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003166 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003167 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003168
Bram Moolenaard8070362016-02-15 21:56:54 +01003169# ifdef WIN32
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003170 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003171 {
3172 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003173 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003174 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003175 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003176
3177 /* reading from a pipe, not a socket */
3178 while (TRUE)
3179 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003180 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3181
3182 if (r && nread > 0)
3183 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003184
3185 if (channel->ch_named_pipe)
3186 {
3187 DisconnectNamedPipe((HANDLE)fd);
3188 ConnectNamedPipe((HANDLE)fd, NULL);
3189 }
3190 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003191 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003192
3193 /* perhaps write some buffer lines */
3194 channel_write_any_lines();
3195
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003196 sleep_time = deadline - GetTickCount();
3197 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003198 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003199 /* Wait for a little while. Very short at first, up to 10 msec
3200 * after looping a few times. */
3201 if (sleep_time > delay)
3202 sleep_time = delay;
3203 Sleep(sleep_time);
3204 delay = delay * 2;
3205 if (delay > 10)
3206 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003207 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003208 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003209 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003210#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003211 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003212#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003213 struct timeval tval;
3214 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003215 fd_set wfds;
3216 int ret;
3217 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003218
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003219 tval.tv_sec = timeout / 1000;
3220 tval.tv_usec = (timeout % 1000) * 1000;
3221 for (;;)
3222 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003223 FD_ZERO(&rfds);
3224 FD_SET((int)fd, &rfds);
3225
3226 /* Write lines to a pipe when a pipe can be written to. Need to
3227 * set this every time, some buffers may be done. */
3228 maxfd = (int)fd + 1;
3229 FD_ZERO(&wfds);
3230 maxfd = channel_fill_wfds(maxfd, &wfds);
3231
3232 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003233# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003234 SOCK_ERRNO;
3235 if (ret == -1 && errno == EINTR)
3236 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003237# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003238 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003239 {
3240 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003241 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003242 channel_write_any_lines();
3243 continue;
3244 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003245 break;
3246 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003247#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003248 for (;;)
3249 {
3250 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3251 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003252
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003253 fds[0].fd = fd;
3254 fds[0].events = POLLIN;
3255 nfd = channel_fill_poll_write(nfd, fds);
3256 if (poll(fds, nfd, timeout) > 0)
3257 {
3258 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003259 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003260 channel_write_any_lines();
3261 continue;
3262 }
3263 break;
3264 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003265#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003266 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003267 return CW_NOT_READY;
3268}
3269
3270 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003271ch_close_part_on_error(
3272 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003273{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003274 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003275
3276 if (is_err)
3277 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003278 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003279 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003280 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003281
3282 /* Queue a "DETACH" netbeans message in the command queue in order to
3283 * terminate the netbeans session later. Do not end the session here
3284 * directly as we may be running in the context of a call to
3285 * netbeans_parse_messages():
3286 * netbeans_parse_messages
3287 * -> autocmd triggered while processing the netbeans cmd
3288 * -> ui_breakcheck
3289 * -> gui event loop or select loop
3290 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003291 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003292 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003293 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003294 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003295 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3296
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003297 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003298 * close the channel yet, there may be something to read on another part.
3299 * When stdout and stderr use the same FD we get the error only on one of
3300 * them, also close the other. */
3301 if (part == PART_OUT || part == PART_ERR)
3302 {
3303 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3304
3305 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3306 ch_close_part(channel, other);
3307 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003308 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003309
3310#ifdef FEAT_GUI
3311 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003312 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003313#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003314}
3315
3316 static void
3317channel_close_now(channel_T *channel)
3318{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003319 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003320 if (channel->ch_nb_close_cb != NULL)
3321 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003322 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003323}
3324
3325/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003326 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003327 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003328 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003329 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003330 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003331channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003332{
3333 static char_u *buf = NULL;
3334 int len = 0;
3335 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003336 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003337 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003338
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003339 fd = channel->ch_part[part].ch_fd;
3340 if (fd == INVALID_FD)
3341 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003342 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003343 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003344 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003345 }
3346 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003347
3348 /* Allocate a buffer to read into. */
3349 if (buf == NULL)
3350 {
3351 buf = alloc(MAXMSGSIZE);
3352 if (buf == NULL)
3353 return; /* out of memory! */
3354 }
3355
3356 /* Keep on reading for as long as there is something to read.
3357 * Use select() or poll() to avoid blocking on a message that is exactly
3358 * MAXMSGSIZE long. */
3359 for (;;)
3360 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003361 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003362 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003363 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003364 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003365 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003366 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003367 if (len <= 0)
3368 break; /* error or nothing more to read */
3369
3370 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003371 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003372 readlen += len;
3373 if (len < MAXMSGSIZE)
3374 break; /* did read everything that's available */
3375 }
3376
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003377 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003378 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003379 {
3380 if (!channel->ch_keep_open)
3381 ch_close_part_on_error(channel, part, (len < 0), func);
3382 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003383#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003384 else if (CH_HAS_GUI && gtk_main_level() > 0)
3385 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003386 gtk_main_quit();
3387#endif
3388}
3389
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003390/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003391 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003392 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003393 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003394 * Returns what was read in allocated memory.
3395 * Returns NULL in case of error or timeout.
3396 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003397 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003398channel_read_block(
3399 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003400{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003401 char_u *buf;
3402 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003403 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003404 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003405 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003406 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003407
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003408 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003409 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003410
3411 while (TRUE)
3412 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003413 node = channel_peek(channel, part);
3414 if (node != NULL)
3415 {
3416 if (mode == MODE_RAW || (mode == MODE_NL
3417 && channel_first_nl(node) != NULL))
3418 /* got a complete message */
3419 break;
3420 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3421 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003422 /* If not blocking or nothing more is coming then return what we
3423 * have. */
3424 if (raw || fd == INVALID_FD)
3425 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003426 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003427
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003428 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003429 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003430 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003431 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003432 {
3433 ch_log(channel, "Timed out");
3434 return NULL;
3435 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003436 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003437 }
3438
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003439 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003440 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003441 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003442 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003443 }
3444 else
3445 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003446 char_u *p;
3447
3448 buf = node->rq_buffer;
3449 nl = channel_first_nl(node);
3450
3451 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003452 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003453 if (*p == NUL)
3454 *p = NL;
3455
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003456 if (nl == NULL)
3457 {
3458 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003459 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003460 }
3461 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003462 {
3463 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003464 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003465 *nl = NUL;
3466 }
3467 else
3468 {
3469 /* Copy the message into allocated memory and remove it from the
3470 * buffer. */
3471 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003472 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003473 }
3474 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003475 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003476 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003477 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003478}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003479
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003480/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003481 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003482 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003483 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003484 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003485 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003486 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003487channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003488 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003489 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003490 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003491 int id,
3492 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003493{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003494 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003495 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003496 int timeout;
3497 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003498
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003499 ch_log(channel, "Reading JSON");
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003500 if (id != -1)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003501 chanpart->ch_block_id = id;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003502 for (;;)
3503 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003504 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003505
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003506 /* search for message "id" */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003507 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003508 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003509 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003510 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003511 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003512
Bram Moolenaard7ece102016-02-02 23:23:02 +01003513 if (!more)
3514 {
3515 /* Handle any other messages in the queue. If done some more
3516 * messages may have arrived. */
3517 if (channel_parse_messages())
3518 continue;
3519
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003520 /* Wait for up to the timeout. If there was an incomplete message
3521 * use the deadline for that. */
3522 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003523 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003524 {
3525#ifdef WIN32
3526 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3527#else
3528 {
3529 struct timeval now_tv;
3530
3531 gettimeofday(&now_tv, NULL);
3532 timeout = (chanpart->ch_deadline.tv_sec
3533 - now_tv.tv_sec) * 1000
3534 + (chanpart->ch_deadline.tv_usec
3535 - now_tv.tv_usec) / 1000
3536 + 1;
3537 }
3538#endif
3539 if (timeout < 0)
3540 {
3541 /* Something went wrong, channel_parse_json() didn't
3542 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003543 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003544 timeout = timeout_arg;
3545 }
3546 else if (timeout > timeout_arg)
3547 timeout = timeout_arg;
3548 }
3549 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003550 if (fd == INVALID_FD
3551 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003552 {
3553 if (timeout == timeout_arg)
3554 {
3555 if (fd != INVALID_FD)
3556 ch_log(channel, "Timed out");
3557 break;
3558 }
3559 }
3560 else
3561 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003562 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003563 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003564 chanpart->ch_block_id = 0;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003565 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003566}
3567
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003568/*
3569 * Common for ch_read() and ch_readraw().
3570 */
3571 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003572common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003573{
3574 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003575 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003576 jobopt_T opt;
3577 int mode;
3578 int timeout;
3579 int id = -1;
3580 typval_T *listtv = NULL;
3581
3582 /* return an empty string by default */
3583 rettv->v_type = VAR_STRING;
3584 rettv->vval.v_string = NULL;
3585
3586 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003587 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003588 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003589 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003590
Bram Moolenaar437905c2016-04-26 19:01:05 +02003591 if (opt.jo_set & JO_PART)
3592 part = opt.jo_part;
3593 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003594 if (channel != NULL)
3595 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003596 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003597 part = channel_part_read(channel);
3598 mode = channel_get_mode(channel, part);
3599 timeout = channel_get_timeout(channel, part);
3600 if (opt.jo_set & JO_TIMEOUT)
3601 timeout = opt.jo_timeout;
3602
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003603 if (blob)
3604 {
3605 int outlen = 0;
3606 char_u *p = channel_read_block(channel, part,
3607 timeout, TRUE, &outlen);
3608 if (p != NULL)
3609 {
3610 blob_T *b = blob_alloc();
3611
3612 if (b != NULL)
3613 {
3614 b->bv_ga.ga_len = outlen;
3615 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3616 blob_free(b);
3617 else
3618 {
3619 memcpy(b->bv_ga.ga_data, p, outlen);
3620 rettv_blob_set(rettv, b);
3621 }
3622 }
3623 vim_free(p);
3624 }
3625 }
3626 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003627 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003628 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003629 else
3630 {
3631 if (opt.jo_set & JO_ID)
3632 id = opt.jo_id;
3633 channel_read_json_block(channel, part, timeout, id, &listtv);
3634 if (listtv != NULL)
3635 {
3636 *rettv = *listtv;
3637 vim_free(listtv);
3638 }
3639 else
3640 {
3641 rettv->v_type = VAR_SPECIAL;
3642 rettv->vval.v_number = VVAL_NONE;
3643 }
3644 }
3645 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003646
3647theend:
3648 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003649}
3650
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003651# if defined(WIN32) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
3652 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003653/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003654 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003655 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003656 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003657 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003658channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003659{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003660 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003661 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003662
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003663 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003664 for (channel = first_channel; channel != NULL;
3665 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003666 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003667 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003668 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003669 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003670 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003671 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003672 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003673 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003674 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003675}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003676# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003677
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003678# if defined(WIN32) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003679/*
3680 * Check the channels for anything that is ready to be read.
3681 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003682 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003683 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003684 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003685channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003686{
3687 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003688 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003689 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003690
3691 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3692 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003693 if (only_keep_open && !channel->ch_keep_open)
3694 continue;
3695
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003696 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003697 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003698 {
3699 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003700 if (fd != INVALID_FD)
3701 {
3702 int r = channel_wait(channel, fd, 0);
3703
3704 if (r == CW_READY)
3705 channel_read(channel, part, "channel_handle_events");
3706 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003707 ch_close_part_on_error(channel, part, TRUE,
3708 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003709 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003710 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003711 }
3712}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003713# endif
3714
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003715# if defined(FEAT_GUI) || defined(PROTO)
3716/*
3717 * Return TRUE when there is any channel with a keep_open flag.
3718 */
3719 int
3720channel_any_keep_open()
3721{
3722 channel_T *channel;
3723
3724 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3725 if (channel->ch_keep_open)
3726 return TRUE;
3727 return FALSE;
3728}
3729# endif
3730
Bram Moolenaard04a0202016-01-26 23:30:18 +01003731/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003732 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003733 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003734 */
3735 void
3736channel_set_nonblock(channel_T *channel, ch_part_T part)
3737{
3738 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003739 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003740
3741 if (fd != INVALID_FD)
3742 {
3743#ifdef _WIN32
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003744 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003745
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003746 ioctlsocket(fd, FIONBIO, &val);
3747#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003748 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003749#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003750 ch_part->ch_nonblocking = TRUE;
3751 }
3752}
3753
3754/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003755 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003756 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003757 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003758 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003759 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003760channel_send(
3761 channel_T *channel,
3762 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003763 char_u *buf_arg,
3764 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003765 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003766{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003767 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003768 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003769 chanpart_T *ch_part = &channel->ch_part[part];
3770 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003771
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003772 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003773 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003774 {
3775 if (!channel->ch_error && fun != NULL)
3776 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003777 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003778 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003779 }
3780 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003781 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003782 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003783
Bram Moolenaar0b146882018-09-06 16:27:24 +02003784 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3785 channel_set_nonblock(channel, part);
3786
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003787 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003788 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003789 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003790 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003791 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003792 fprintf(log_fd, "'\n");
3793 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003794 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003795 }
3796
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003797 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003798 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003799 writeq_T *wq = &ch_part->ch_writeque;
3800 char_u *buf;
3801 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003802
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003803 if (wq->wq_next != NULL)
3804 {
3805 /* first write what was queued */
3806 buf = wq->wq_next->wq_ga.ga_data;
3807 len = wq->wq_next->wq_ga.ga_len;
3808 did_use_queue = TRUE;
3809 }
3810 else
3811 {
3812 if (len_arg == 0)
3813 /* nothing to write, called from channel_select_check() */
3814 return OK;
3815 buf = buf_arg;
3816 len = len_arg;
3817 }
3818
3819 if (part == PART_SOCK)
3820 res = sock_write(fd, (char *)buf, len);
3821 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003822 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003823 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar31faed62019-01-22 23:01:40 +01003824#ifdef WIN32
3825 if (channel->ch_named_pipe && res < 0)
3826 {
3827 DisconnectNamedPipe((HANDLE)fd);
3828 ConnectNamedPipe((HANDLE)fd, NULL);
3829 }
3830#endif
3831 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003832 if (res < 0 && (errno == EWOULDBLOCK
3833#ifdef EAGAIN
3834 || errno == EAGAIN
3835#endif
3836 ))
3837 res = 0; /* nothing got written */
3838
3839 if (res >= 0 && ch_part->ch_nonblocking)
3840 {
3841 writeq_T *entry = wq->wq_next;
3842
3843 if (did_use_queue)
3844 ch_log(channel, "Sent %d bytes now", res);
3845 if (res == len)
3846 {
3847 /* Wrote all the buf[len] bytes. */
3848 if (entry != NULL)
3849 {
3850 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003851 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003852 continue;
3853 }
3854 if (did_use_queue)
3855 ch_log(channel, "Write queue empty");
3856 }
3857 else
3858 {
3859 /* Wrote only buf[res] bytes, can't write more now. */
3860 if (entry != NULL)
3861 {
3862 if (res > 0)
3863 {
3864 /* Remove the bytes that were written. */
3865 mch_memmove(entry->wq_ga.ga_data,
3866 (char *)entry->wq_ga.ga_data + res,
3867 len - res);
3868 entry->wq_ga.ga_len -= res;
3869 }
3870 buf = buf_arg;
3871 len = len_arg;
3872 }
3873 else
3874 {
3875 buf += res;
3876 len -= res;
3877 }
3878 ch_log(channel, "Adding %d bytes to the write queue", len);
3879
3880 /* Append the not written bytes of the argument to the write
3881 * buffer. Limit entries to 4000 bytes. */
3882 if (wq->wq_prev != NULL
3883 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3884 {
3885 writeq_T *last = wq->wq_prev;
3886
3887 /* append to the last entry */
3888 if (ga_grow(&last->wq_ga, len) == OK)
3889 {
3890 mch_memmove((char *)last->wq_ga.ga_data
3891 + last->wq_ga.ga_len,
3892 buf, len);
3893 last->wq_ga.ga_len += len;
3894 }
3895 }
3896 else
3897 {
3898 writeq_T *last = (writeq_T *)alloc((int)sizeof(writeq_T));
3899
3900 if (last != NULL)
3901 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003902 last->wq_prev = wq->wq_prev;
3903 last->wq_next = NULL;
3904 if (wq->wq_prev == NULL)
3905 wq->wq_next = last;
3906 else
3907 wq->wq_prev->wq_next = last;
3908 wq->wq_prev = last;
3909 ga_init2(&last->wq_ga, 1, 1000);
3910 if (ga_grow(&last->wq_ga, len) == OK)
3911 {
3912 mch_memmove(last->wq_ga.ga_data, buf, len);
3913 last->wq_ga.ga_len = len;
3914 }
3915 }
3916 }
3917 }
3918 }
3919 else if (res != len)
3920 {
3921 if (!channel->ch_error && fun != NULL)
3922 {
3923 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003924 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003925 }
3926 channel->ch_error = TRUE;
3927 return FAIL;
3928 }
3929
3930 channel->ch_error = FALSE;
3931 return OK;
3932 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003933}
3934
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003935/*
3936 * Common for "ch_sendexpr()" and "ch_sendraw()".
3937 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02003938 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003939 * Otherwise returns NULL.
3940 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003941 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003942send_common(
3943 typval_T *argvars,
3944 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003945 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003946 int id,
3947 int eval,
3948 jobopt_T *opt,
3949 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003950 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003951{
3952 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003953 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003954
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003955 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003956 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003957 if (channel == NULL)
3958 return NULL;
3959 part_send = channel_part_send(channel);
3960 *part_read = channel_part_read(channel);
3961
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003962 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003963 return NULL;
3964
3965 /* Set the callback. An empty callback means no callback and not reading
3966 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
3967 * allowed. */
3968 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
3969 {
3970 if (eval)
3971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003972 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003973 return NULL;
3974 }
Bram Moolenaar6fc82272016-08-28 19:26:43 +02003975 channel_set_req_callback(channel, *part_read,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003976 opt->jo_callback, opt->jo_partial, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003977 }
3978
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003979 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003980 && opt->jo_callback == NULL)
3981 return channel;
3982 return NULL;
3983}
3984
3985/*
3986 * common for "ch_evalexpr()" and "ch_sendexpr()"
3987 */
3988 void
3989ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
3990{
3991 char_u *text;
3992 typval_T *listtv;
3993 channel_T *channel;
3994 int id;
3995 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003996 ch_part_T part_send;
3997 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003998 jobopt_T opt;
3999 int timeout;
4000
4001 /* return an empty string by default */
4002 rettv->v_type = VAR_STRING;
4003 rettv->vval.v_string = NULL;
4004
Bram Moolenaar437905c2016-04-26 19:01:05 +02004005 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004006 if (channel == NULL)
4007 return;
4008 part_send = channel_part_send(channel);
4009
4010 ch_mode = channel_get_mode(channel, part_send);
4011 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004013 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004014 return;
4015 }
4016
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004017 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004018 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004019 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004020 if (text == NULL)
4021 return;
4022
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004023 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004024 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4025 vim_free(text);
4026 if (channel != NULL && eval)
4027 {
4028 if (opt.jo_set & JO_TIMEOUT)
4029 timeout = opt.jo_timeout;
4030 else
4031 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004032 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4033 == OK)
4034 {
4035 list_T *list = listtv->vval.v_list;
4036
4037 /* Move the item from the list and then change the type to
4038 * avoid the value being freed. */
4039 *rettv = list->lv_last->li_tv;
4040 list->lv_last->li_tv.v_type = VAR_NUMBER;
4041 free_tv(listtv);
4042 }
4043 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004044 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004045}
4046
4047/*
4048 * common for "ch_evalraw()" and "ch_sendraw()"
4049 */
4050 void
4051ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4052{
4053 char_u buf[NUMBUFLEN];
4054 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004055 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004056 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004057 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004058 jobopt_T opt;
4059 int timeout;
4060
4061 /* return an empty string by default */
4062 rettv->v_type = VAR_STRING;
4063 rettv->vval.v_string = NULL;
4064
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004065 if (argvars[1].v_type == VAR_BLOB)
4066 {
4067 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4068 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4069 }
4070 else
4071 {
4072 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004073 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004074 }
4075 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004076 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4077 if (channel != NULL && eval)
4078 {
4079 if (opt.jo_set & JO_TIMEOUT)
4080 timeout = opt.jo_timeout;
4081 else
4082 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004083 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004084 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004086 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004087}
4088
Bram Moolenaarf3360612017-10-01 16:21:31 +02004089# define KEEP_OPEN_TIME 20 /* msec */
4090
Bram Moolenaard04a0202016-01-26 23:30:18 +01004091# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004092/*
4093 * Add open channels to the poll struct.
4094 * Return the adjusted struct index.
4095 * The type of "fds" is hidden to avoid problems with the function proto.
4096 */
4097 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004098channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004099{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004100 int nfd = nfd_in;
4101 channel_T *channel;
4102 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004103 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004104
Bram Moolenaar77073442016-02-13 23:23:53 +01004105 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004106 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004107 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004108 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004109 chanpart_T *ch_part = &channel->ch_part[part];
4110
4111 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004112 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004113 if (channel->ch_keep_open)
4114 {
4115 /* For unknown reason poll() returns immediately for a
4116 * keep-open channel. Instead of adding it to the fds add
4117 * a short timeout and check, like polling. */
4118 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4119 *towait = KEEP_OPEN_TIME;
4120 }
4121 else
4122 {
4123 ch_part->ch_poll_idx = nfd;
4124 fds[nfd].fd = ch_part->ch_fd;
4125 fds[nfd].events = POLLIN;
4126 nfd++;
4127 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004128 }
4129 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004130 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004131 }
4132 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004133
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004134 nfd = channel_fill_poll_write(nfd, fds);
4135
Bram Moolenaare0874f82016-01-24 20:36:41 +01004136 return nfd;
4137}
4138
4139/*
4140 * The type of "fds" is hidden to avoid problems with the function proto.
4141 */
4142 int
4143channel_poll_check(int ret_in, void *fds_in)
4144{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004145 int ret = ret_in;
4146 channel_T *channel;
4147 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004148 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004149 int idx;
4150 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004151
Bram Moolenaar77073442016-02-13 23:23:53 +01004152 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004153 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004154 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004155 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004156 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004157
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004158 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004159 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004160 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004161 --ret;
4162 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004163 else if (channel->ch_part[part].ch_fd != INVALID_FD
4164 && channel->ch_keep_open)
4165 {
4166 /* polling a keep-open channel */
4167 channel_read(channel, part, "channel_poll_check_keep_open");
4168 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004169 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004170
4171 in_part = &channel->ch_part[PART_IN];
4172 idx = in_part->ch_poll_idx;
4173 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4174 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004175 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004176 --ret;
4177 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004178 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004179
4180 return ret;
4181}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004182# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004183
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004184# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004185
Bram Moolenaare0874f82016-01-24 20:36:41 +01004186/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004187 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004188 */
4189 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004190channel_select_setup(
4191 int maxfd_in,
4192 void *rfds_in,
4193 void *wfds_in,
4194 struct timeval *tv,
4195 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004196{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004197 int maxfd = maxfd_in;
4198 channel_T *channel;
4199 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004200 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004201 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004202
Bram Moolenaar77073442016-02-13 23:23:53 +01004203 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004204 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004205 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004206 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004207 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004208
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004209 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004210 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004211 if (channel->ch_keep_open)
4212 {
4213 /* For unknown reason select() returns immediately for a
4214 * keep-open channel. Instead of adding it to the rfds add
4215 * a short timeout and check, like polling. */
4216 if (*tvp == NULL || tv->tv_sec > 0
4217 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4218 {
4219 *tvp = tv;
4220 tv->tv_sec = 0;
4221 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4222 }
4223 }
4224 else
4225 {
4226 FD_SET((int)fd, rfds);
4227 if (maxfd < (int)fd)
4228 maxfd = (int)fd;
4229 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004230 }
4231 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004232 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004233
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004234 maxfd = channel_fill_wfds(maxfd, wfds);
4235
Bram Moolenaare0874f82016-01-24 20:36:41 +01004236 return maxfd;
4237}
4238
4239/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004240 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004241 */
4242 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004243channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004244{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004245 int ret = ret_in;
4246 channel_T *channel;
4247 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004248 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004249 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004250 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004251
Bram Moolenaar77073442016-02-13 23:23:53 +01004252 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004253 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004254 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004255 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004256 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004257
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004258 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004259 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004260 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004261 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004262 --ret;
4263 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004264 else if (fd != INVALID_FD && channel->ch_keep_open)
4265 {
4266 /* polling a keep-open channel */
4267 channel_read(channel, part, "channel_select_check_keep_open");
4268 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004269 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004270
4271 in_part = &channel->ch_part[PART_IN];
4272 if (ret > 0 && in_part->ch_fd != INVALID_FD
4273 && FD_ISSET(in_part->ch_fd, wfds))
4274 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004275 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004276 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004277 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004278 --ret;
4279 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004280 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004281
4282 return ret;
4283}
Bram Moolenaared5a78e2016-02-19 21:05:03 +01004284# endif /* !WIN32 && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004285
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004286/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004287 * Execute queued up commands.
4288 * Invoked from the main loop when it's safe to execute received commands.
4289 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004290 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004291 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004292channel_parse_messages(void)
4293{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004294 channel_T *channel = first_channel;
4295 int ret = FALSE;
4296 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004297 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004298#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004299 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004300
4301 ELAPSED_INIT(start_tv);
4302#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004303
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004304 ++safe_to_invoke_callback;
4305
Bram Moolenaard0b65022016-03-06 21:50:33 +01004306 /* Only do this message when another message was given, otherwise we get
4307 * lots of them. */
4308 if (did_log_msg)
4309 {
4310 ch_log(NULL, "looking for messages on channels");
4311 did_log_msg = FALSE;
4312 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004313 while (channel != NULL)
4314 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004315 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004316 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004317 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004318 channel_close_now(channel);
4319 /* channel may have been freed, start over */
4320 channel = first_channel;
4321 continue;
4322 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004323 if (channel->ch_to_be_freed)
4324 {
4325 channel_free(channel);
4326 /* channel has been freed, start over */
4327 channel = first_channel;
4328 continue;
4329 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004330 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004331 {
4332 /* channel is no longer useful, free it */
4333 channel_free(channel);
4334 channel = first_channel;
4335 part = PART_SOCK;
4336 continue;
4337 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004338 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004339 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004340 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004341 /* Increase the refcount, in case the handler causes the channel
4342 * to be unreferenced or closed. */
4343 ++channel->ch_refcount;
4344 r = may_invoke_callback(channel, part);
4345 if (r == OK)
4346 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004347 if (channel_unref(channel) || (r == OK
4348#ifdef ELAPSED_FUNC
4349 /* Limit the time we loop here to 100 msec, otherwise
4350 * Vim becomes unresponsive when the callback takes
4351 * more than a bit of time. */
4352 && ELAPSED_FUNC(start_tv) < 100L
4353#endif
4354 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004355 {
4356 /* channel was freed or something was done, start over */
4357 channel = first_channel;
4358 part = PART_SOCK;
4359 continue;
4360 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004361 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004362 if (part < PART_ERR)
4363 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004364 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004365 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004366 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004367 part = PART_SOCK;
4368 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004369 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004370
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004371 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004372 {
4373 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004374 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004375 }
4376
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004377 --safe_to_invoke_callback;
4378
Bram Moolenaard7ece102016-02-02 23:23:02 +01004379 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004380}
4381
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004382/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004383 * Return TRUE if any channel has readahead. That means we should not block on
4384 * waiting for input.
4385 */
4386 int
4387channel_any_readahead(void)
4388{
4389 channel_T *channel = first_channel;
4390 ch_part_T part = PART_SOCK;
4391
4392 while (channel != NULL)
4393 {
4394 if (channel_has_readahead(channel, part))
4395 return TRUE;
4396 if (part < PART_ERR)
4397 ++part;
4398 else
4399 {
4400 channel = channel->ch_next;
4401 part = PART_SOCK;
4402 }
4403 }
4404 return FALSE;
4405}
4406
4407/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004408 * Mark references to lists used in channels.
4409 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004410 int
4411set_ref_in_channel(int copyID)
4412{
Bram Moolenaar77073442016-02-13 23:23:53 +01004413 int abort = FALSE;
4414 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004415 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004416
Bram Moolenaar77073442016-02-13 23:23:53 +01004417 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004418 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004419 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004420 tv.v_type = VAR_CHANNEL;
4421 tv.vval.v_channel = channel;
4422 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004423 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004424 return abort;
4425}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004426
4427/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004428 * Return the "part" to write to for "channel".
4429 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004430 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004431channel_part_send(channel_T *channel)
4432{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004433 if (channel->CH_SOCK_FD == INVALID_FD)
4434 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004435 return PART_SOCK;
4436}
4437
4438/*
4439 * Return the default "part" to read from for "channel".
4440 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004441 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004442channel_part_read(channel_T *channel)
4443{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004444 if (channel->CH_SOCK_FD == INVALID_FD)
4445 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004446 return PART_SOCK;
4447}
4448
4449/*
4450 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004451 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004452 */
4453 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004454channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004455{
Bram Moolenaar77073442016-02-13 23:23:53 +01004456 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004457 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004458 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004459}
4460
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004461/*
4462 * Return the timeout of "channel"/"part"
4463 */
4464 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004465channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004466{
4467 return channel->ch_part[part].ch_timeout;
4468}
4469
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004470 static int
4471handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4472{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004473 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004474
4475 opt->jo_set |= jo;
4476 if (STRCMP(val, "nl") == 0)
4477 *modep = MODE_NL;
4478 else if (STRCMP(val, "raw") == 0)
4479 *modep = MODE_RAW;
4480 else if (STRCMP(val, "js") == 0)
4481 *modep = MODE_JS;
4482 else if (STRCMP(val, "json") == 0)
4483 *modep = MODE_JSON;
4484 else
4485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004486 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004487 return FAIL;
4488 }
4489 return OK;
4490}
4491
4492 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004493handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004494{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004495 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004496
4497 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4498 if (STRCMP(val, "null") == 0)
4499 opt->jo_io[part] = JIO_NULL;
4500 else if (STRCMP(val, "pipe") == 0)
4501 opt->jo_io[part] = JIO_PIPE;
4502 else if (STRCMP(val, "file") == 0)
4503 opt->jo_io[part] = JIO_FILE;
4504 else if (STRCMP(val, "buffer") == 0)
4505 opt->jo_io[part] = JIO_BUFFER;
4506 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4507 opt->jo_io[part] = JIO_OUT;
4508 else
4509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004510 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004511 return FAIL;
4512 }
4513 return OK;
4514}
4515
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004516/*
4517 * Clear a jobopt_T before using it.
4518 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004519 void
4520clear_job_options(jobopt_T *opt)
4521{
4522 vim_memset(opt, 0, sizeof(jobopt_T));
4523}
4524
4525/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004526 * Free any members of a jobopt_T.
4527 */
4528 void
4529free_job_options(jobopt_T *opt)
4530{
4531 if (opt->jo_partial != NULL)
4532 partial_unref(opt->jo_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004533 else if (opt->jo_callback != NULL)
4534 func_unref(opt->jo_callback);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004535 if (opt->jo_out_partial != NULL)
4536 partial_unref(opt->jo_out_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004537 else if (opt->jo_out_cb != NULL)
4538 func_unref(opt->jo_out_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004539 if (opt->jo_err_partial != NULL)
4540 partial_unref(opt->jo_err_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004541 else if (opt->jo_err_cb != NULL)
4542 func_unref(opt->jo_err_cb);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004543 if (opt->jo_close_partial != NULL)
4544 partial_unref(opt->jo_close_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004545 else if (opt->jo_close_cb != NULL)
4546 func_unref(opt->jo_close_cb);
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004547 if (opt->jo_exit_partial != NULL)
4548 partial_unref(opt->jo_exit_partial);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004549 else if (opt->jo_exit_cb != NULL)
4550 func_unref(opt->jo_exit_cb);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004551 if (opt->jo_env != NULL)
4552 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004553}
4554
4555/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004556 * Get the PART_ number from the first character of an option name.
4557 */
4558 static int
4559part_from_char(int c)
4560{
4561 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4562}
4563
4564/*
4565 * Get the option entries from the dict in "tv", parse them and put the result
4566 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004567 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004568 * If an option value is invalid return FAIL.
4569 */
4570 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004571get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004572{
4573 typval_T *item;
4574 char_u *val;
4575 dict_T *dict;
4576 int todo;
4577 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004578 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004579
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004580 if (tv->v_type == VAR_UNKNOWN)
4581 return OK;
4582 if (tv->v_type != VAR_DICT)
4583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004584 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004585 return FAIL;
4586 }
4587 dict = tv->vval.v_dict;
4588 if (dict == NULL)
4589 return OK;
4590
4591 todo = (int)dict->dv_hashtab.ht_used;
4592 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4593 if (!HASHITEM_EMPTY(hi))
4594 {
4595 item = &dict_lookup(hi)->di_tv;
4596
4597 if (STRCMP(hi->hi_key, "mode") == 0)
4598 {
4599 if (!(supported & JO_MODE))
4600 break;
4601 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4602 return FAIL;
4603 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004604 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004605 {
4606 if (!(supported & JO_IN_MODE))
4607 break;
4608 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4609 == FAIL)
4610 return FAIL;
4611 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004612 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004613 {
4614 if (!(supported & JO_OUT_MODE))
4615 break;
4616 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4617 == FAIL)
4618 return FAIL;
4619 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004620 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004621 {
4622 if (!(supported & JO_ERR_MODE))
4623 break;
4624 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4625 == FAIL)
4626 return FAIL;
4627 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004628 else if (STRCMP(hi->hi_key, "noblock") == 0)
4629 {
4630 if (!(supported & JO_MODE))
4631 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004632 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004633 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004634 else if (STRCMP(hi->hi_key, "in_io") == 0
4635 || STRCMP(hi->hi_key, "out_io") == 0
4636 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004637 {
4638 if (!(supported & JO_OUT_IO))
4639 break;
4640 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4641 return FAIL;
4642 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004643 else if (STRCMP(hi->hi_key, "in_name") == 0
4644 || STRCMP(hi->hi_key, "out_name") == 0
4645 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004646 {
4647 part = part_from_char(*hi->hi_key);
4648
4649 if (!(supported & JO_OUT_IO))
4650 break;
4651 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4652 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004653 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004654 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004655 else if (STRCMP(hi->hi_key, "pty") == 0)
4656 {
4657 if (!(supported & JO_MODE))
4658 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004659 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004660 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004661 else if (STRCMP(hi->hi_key, "in_buf") == 0
4662 || STRCMP(hi->hi_key, "out_buf") == 0
4663 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004664 {
4665 part = part_from_char(*hi->hi_key);
4666
4667 if (!(supported & JO_OUT_IO))
4668 break;
4669 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004670 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004671 if (opt->jo_io_buf[part] <= 0)
4672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004673 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004674 return FAIL;
4675 }
4676 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004678 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004679 return FAIL;
4680 }
4681 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004682 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4683 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4684 {
4685 part = part_from_char(*hi->hi_key);
4686
4687 if (!(supported & JO_OUT_IO))
4688 break;
4689 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004690 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004691 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004692 else if (STRCMP(hi->hi_key, "out_msg") == 0
4693 || STRCMP(hi->hi_key, "err_msg") == 0)
4694 {
4695 part = part_from_char(*hi->hi_key);
4696
4697 if (!(supported & JO_OUT_IO))
4698 break;
4699 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004700 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004701 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004702 else if (STRCMP(hi->hi_key, "in_top") == 0
4703 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004704 {
4705 linenr_T *lp;
4706
4707 if (!(supported & JO_OUT_IO))
4708 break;
4709 if (hi->hi_key[3] == 't')
4710 {
4711 lp = &opt->jo_in_top;
4712 opt->jo_set |= JO_IN_TOP;
4713 }
4714 else
4715 {
4716 lp = &opt->jo_in_bot;
4717 opt->jo_set |= JO_IN_BOT;
4718 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004719 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004720 if (*lp < 0)
4721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004722 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004723 return FAIL;
4724 }
4725 }
4726 else if (STRCMP(hi->hi_key, "channel") == 0)
4727 {
4728 if (!(supported & JO_OUT_IO))
4729 break;
4730 opt->jo_set |= JO_CHANNEL;
4731 if (item->v_type != VAR_CHANNEL)
4732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004733 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004734 return FAIL;
4735 }
4736 opt->jo_channel = item->vval.v_channel;
4737 }
4738 else if (STRCMP(hi->hi_key, "callback") == 0)
4739 {
4740 if (!(supported & JO_CALLBACK))
4741 break;
4742 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004743 opt->jo_callback = get_callback(item, &opt->jo_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004744 if (opt->jo_callback == NULL)
4745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004746 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004747 return FAIL;
4748 }
4749 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004750 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004751 {
4752 if (!(supported & JO_OUT_CALLBACK))
4753 break;
4754 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004755 opt->jo_out_cb = get_callback(item, &opt->jo_out_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004756 if (opt->jo_out_cb == NULL)
4757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004758 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004759 return FAIL;
4760 }
4761 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004762 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004763 {
4764 if (!(supported & JO_ERR_CALLBACK))
4765 break;
4766 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004767 opt->jo_err_cb = get_callback(item, &opt->jo_err_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004768 if (opt->jo_err_cb == NULL)
4769 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004770 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004771 return FAIL;
4772 }
4773 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004774 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004775 {
4776 if (!(supported & JO_CLOSE_CALLBACK))
4777 break;
4778 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004779 opt->jo_close_cb = get_callback(item, &opt->jo_close_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004780 if (opt->jo_close_cb == NULL)
4781 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004782 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004783 return FAIL;
4784 }
4785 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004786 else if (STRCMP(hi->hi_key, "drop") == 0)
4787 {
4788 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004789 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004790
4791 if (STRCMP(val, "never") == 0)
4792 never = TRUE;
4793 else if (STRCMP(val, "auto") != 0)
4794 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004795 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004796 return FAIL;
4797 }
4798 opt->jo_drop_never = never;
4799 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004800 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4801 {
4802 if (!(supported & JO_EXIT_CB))
4803 break;
4804 opt->jo_set |= JO_EXIT_CB;
4805 opt->jo_exit_cb = get_callback(item, &opt->jo_exit_partial);
4806 if (opt->jo_exit_cb == NULL)
4807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004808 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004809 return FAIL;
4810 }
4811 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004812#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004813 else if (STRCMP(hi->hi_key, "term_name") == 0)
4814 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004815 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004816 break;
4817 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004818 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004819 if (opt->jo_term_name == NULL)
4820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004821 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004822 return FAIL;
4823 }
4824 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004825 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4826 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004827 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004828 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004829 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004830 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004832 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004833 return FAIL;
4834 }
4835 opt->jo_set2 |= JO2_TERM_FINISH;
4836 opt->jo_term_finish = *val;
4837 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004838 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4839 {
4840 char_u *p;
4841
4842 if (!(supported2 & JO2_TERM_OPENCMD))
4843 break;
4844 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004846 if (p != NULL)
4847 {
4848 /* Must have %d and no other %. */
4849 p = vim_strchr(p, '%');
4850 if (p != NULL && (p[1] != 'd'
4851 || vim_strchr(p + 2, '%') != NULL))
4852 p = NULL;
4853 }
4854 if (p == NULL)
4855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004857 return FAIL;
4858 }
4859 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004860 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4861 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004862 char_u *p;
4863
4864 if (!(supported2 & JO2_EOF_CHARS))
4865 break;
4866 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004867 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004868 if (p == NULL)
4869 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004870 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004871 return FAIL;
4872 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004873 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004874 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4875 {
4876 if (!(supported2 & JO2_TERM_ROWS))
4877 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004878 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004879 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004880 }
4881 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4882 {
4883 if (!(supported2 & JO2_TERM_COLS))
4884 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004885 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004886 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004887 }
4888 else if (STRCMP(hi->hi_key, "vertical") == 0)
4889 {
4890 if (!(supported2 & JO2_VERTICAL))
4891 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004892 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004893 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004894 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02004895 else if (STRCMP(hi->hi_key, "curwin") == 0)
4896 {
4897 if (!(supported2 & JO2_CURWIN))
4898 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004899 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004900 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02004901 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004902 else if (STRCMP(hi->hi_key, "hidden") == 0)
4903 {
4904 if (!(supported2 & JO2_HIDDEN))
4905 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004906 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004907 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02004908 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004909 else if (STRCMP(hi->hi_key, "norestore") == 0)
4910 {
4911 if (!(supported2 & JO2_NORESTORE))
4912 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004913 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004914 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01004915 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004916 else if (STRCMP(hi->hi_key, "term_kill") == 0)
4917 {
4918 if (!(supported2 & JO2_TERM_KILL))
4919 break;
4920 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004921 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004922 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004923# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4924 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004926 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004927 listitem_T *li;
4928 long_u rgb[16];
4929
4930 if (!(supported2 & JO2_ANSI_COLORS))
4931 break;
4932
4933 if (item == NULL || item->v_type != VAR_LIST
4934 || item->vval.v_list == NULL)
4935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004936 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004937 return FAIL;
4938 }
4939
4940 li = item->vval.v_list->lv_first;
4941 for (; li != NULL && n < 16; li = li->li_next, n++)
4942 {
4943 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004944 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004945
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004947 if (color_name == NULL)
4948 return FAIL;
4949
4950 guicolor = GUI_GET_COLOR(color_name);
4951 if (guicolor == INVALCOLOR)
4952 return FAIL;
4953
4954 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4955 }
4956
4957 if (n != 16 || li != NULL)
4958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004959 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004960 return FAIL;
4961 }
4962
4963 opt->jo_set2 |= JO2_ANSI_COLORS;
4964 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4965 }
4966# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004967#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004968 else if (STRCMP(hi->hi_key, "env") == 0)
4969 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004970 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004971 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004972 if (item->v_type != VAR_DICT)
4973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004974 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02004975 return FAIL;
4976 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004977 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004978 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02004979 if (opt->jo_env != NULL)
4980 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004981 }
4982 else if (STRCMP(hi->hi_key, "cwd") == 0)
4983 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004984 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004985 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004986 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02004987 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02004988#ifndef WIN32 // Win32 directories don't have the concept of "executable"
4989 || mch_access((char *)opt->jo_cwd, X_OK) != 0
4990#endif
4991 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004993 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004994 return FAIL;
4995 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004996 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004997 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004998 else if (STRCMP(hi->hi_key, "waittime") == 0)
4999 {
5000 if (!(supported & JO_WAITTIME))
5001 break;
5002 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005003 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005004 }
5005 else if (STRCMP(hi->hi_key, "timeout") == 0)
5006 {
5007 if (!(supported & JO_TIMEOUT))
5008 break;
5009 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005010 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005011 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005012 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005013 {
5014 if (!(supported & JO_OUT_TIMEOUT))
5015 break;
5016 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005017 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005018 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005019 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005020 {
5021 if (!(supported & JO_ERR_TIMEOUT))
5022 break;
5023 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005024 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005025 }
5026 else if (STRCMP(hi->hi_key, "part") == 0)
5027 {
5028 if (!(supported & JO_PART))
5029 break;
5030 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005031 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005032 if (STRCMP(val, "err") == 0)
5033 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005034 else if (STRCMP(val, "out") == 0)
5035 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005036 else
5037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005038 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005039 return FAIL;
5040 }
5041 }
5042 else if (STRCMP(hi->hi_key, "id") == 0)
5043 {
5044 if (!(supported & JO_ID))
5045 break;
5046 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005047 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005048 }
5049 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5050 {
5051 if (!(supported & JO_STOPONEXIT))
5052 break;
5053 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005054 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005055 opt->jo_soe_buf);
5056 if (opt->jo_stoponexit == NULL)
5057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005058 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005059 return FAIL;
5060 }
5061 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005062 else if (STRCMP(hi->hi_key, "block_write") == 0)
5063 {
5064 if (!(supported & JO_BLOCK_WRITE))
5065 break;
5066 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005067 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005068 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005069 else
5070 break;
5071 --todo;
5072 }
5073 if (todo > 0)
5074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005075 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005076 return FAIL;
5077 }
5078
5079 return OK;
5080}
5081
5082/*
5083 * Get the channel from the argument.
5084 * Returns NULL if the handle is invalid.
Bram Moolenaar437905c2016-04-26 19:01:05 +02005085 * When "check_open" is TRUE check that the channel can be used.
5086 * When "reading" is TRUE "check_open" considers typeahead useful.
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005087 * "part" is used to check typeahead, when PART_COUNT use the default part.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005088 */
5089 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005090get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005091{
Bram Moolenaar437905c2016-04-26 19:01:05 +02005092 channel_T *channel = NULL;
5093 int has_readahead = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005094
5095 if (tv->v_type == VAR_JOB)
5096 {
5097 if (tv->vval.v_job != NULL)
5098 channel = tv->vval.v_job->jv_channel;
5099 }
5100 else if (tv->v_type == VAR_CHANNEL)
5101 {
5102 channel = tv->vval.v_channel;
5103 }
5104 else
5105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005106 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005107 return NULL;
5108 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02005109 if (channel != NULL && reading)
5110 has_readahead = channel_has_readahead(channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005111 part != PART_COUNT ? part : channel_part_read(channel));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005112
Bram Moolenaar437905c2016-04-26 19:01:05 +02005113 if (check_open && (channel == NULL || (!channel_is_open(channel)
5114 && !(reading && has_readahead))))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005116 emsg(_("E906: not an open channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005117 return NULL;
5118 }
5119 return channel;
5120}
5121
5122static job_T *first_job = NULL;
5123
5124 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005125job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005126{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005127 int i;
5128
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005129 ch_log(job->jv_channel, "Freeing job");
5130 if (job->jv_channel != NULL)
5131 {
5132 /* The link from the channel to the job doesn't count as a reference,
5133 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005134 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005135 * NULL the reference. We don't set ch_job_killed, unreferencing the
5136 * job doesn't mean it stops running. */
5137 job->jv_channel->ch_job = NULL;
5138 channel_unref(job->jv_channel);
5139 }
5140 mch_clear_job(job);
5141
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005142 vim_free(job->jv_tty_in);
5143 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005144 vim_free(job->jv_stoponexit);
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005145 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar20608922018-04-21 22:30:08 +02005146 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005147 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005148 for (i = 0; job->jv_argv[i] != NULL; i++)
5149 vim_free(job->jv_argv[i]);
5150 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005151 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005152}
5153
5154 static void
5155job_free_job(job_T *job)
5156{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005157 if (job->jv_next != NULL)
5158 job->jv_next->jv_prev = job->jv_prev;
5159 if (job->jv_prev == NULL)
5160 first_job = job->jv_next;
5161 else
5162 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005163 vim_free(job);
5164}
5165
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005166 static void
5167job_free(job_T *job)
5168{
5169 if (!in_free_unref_items)
5170 {
5171 job_free_contents(job);
5172 job_free_job(job);
5173 }
5174}
5175
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005176#if defined(EXITFREE) || defined(PROTO)
5177 void
5178job_free_all(void)
5179{
5180 while (first_job != NULL)
5181 job_free(first_job);
5182}
5183#endif
5184
5185/*
5186 * Return TRUE if we need to check if the process of "job" has ended.
5187 */
5188 static int
5189job_need_end_check(job_T *job)
5190{
5191 return job->jv_status == JOB_STARTED
5192 && (job->jv_stoponexit != NULL || job->jv_exit_cb != NULL);
5193}
5194
5195/*
5196 * Return TRUE if the channel of "job" is still useful.
5197 */
5198 static int
5199job_channel_still_useful(job_T *job)
5200{
5201 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5202}
5203
5204/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005205 * Return TRUE if the channel of "job" is closeable.
5206 */
5207 static int
5208job_channel_can_close(job_T *job)
5209{
5210 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5211}
5212
5213/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005214 * Return TRUE if the job should not be freed yet. Do not free the job when
5215 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5216 * or when the associated channel will do something with the job output.
5217 */
5218 static int
5219job_still_useful(job_T *job)
5220{
5221 return job_need_end_check(job) || job_channel_still_useful(job);
5222}
5223
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005224#if defined(GUI_MAY_FORK) || defined(PROTO)
5225/*
5226 * Return TRUE when there is any running job that we care about.
5227 */
5228 int
5229job_any_running()
5230{
5231 job_T *job;
5232
5233 for (job = first_job; job != NULL; job = job->jv_next)
5234 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005235 {
5236 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005237 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005238 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005239 return FALSE;
5240}
5241#endif
5242
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005243#if !defined(USE_ARGV) || defined(PROTO)
5244/*
5245 * Escape one argument for an external command.
5246 * Returns the escaped string in allocated memory. NULL when out of memory.
5247 */
5248 static char_u *
5249win32_escape_arg(char_u *arg)
5250{
5251 int slen, dlen;
5252 int escaping = 0;
5253 int i;
5254 char_u *s, *d;
5255 char_u *escaped_arg;
5256 int has_spaces = FALSE;
5257
5258 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005259 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005260 dlen = slen;
5261 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5262 {
5263 if (*s == '"' || *s == '\\')
5264 ++dlen;
5265 if (*s == ' ' || *s == '\t')
5266 has_spaces = TRUE;
5267 }
5268
5269 if (has_spaces)
5270 dlen += 2;
5271
5272 if (dlen == slen)
5273 return vim_strsave(arg);
5274
5275 /* Allocate memory for the result and fill it. */
5276 escaped_arg = alloc(dlen + 1);
5277 if (escaped_arg == NULL)
5278 return NULL;
5279 memset(escaped_arg, 0, dlen+1);
5280
5281 d = escaped_arg;
5282
5283 if (has_spaces)
5284 *d++ = '"';
5285
5286 for (s = arg; *s != NUL;)
5287 {
5288 switch (*s)
5289 {
5290 case '"':
5291 for (i = 0; i < escaping; i++)
5292 *d++ = '\\';
5293 escaping = 0;
5294 *d++ = '\\';
5295 *d++ = *s++;
5296 break;
5297 case '\\':
5298 escaping++;
5299 *d++ = *s++;
5300 break;
5301 default:
5302 escaping = 0;
5303 MB_COPY_CHAR(s, d);
5304 break;
5305 }
5306 }
5307
5308 /* add terminating quote and finish with a NUL */
5309 if (has_spaces)
5310 {
5311 for (i = 0; i < escaping; i++)
5312 *d++ = '\\';
5313 *d++ = '"';
5314 }
5315 *d = NUL;
5316
5317 return escaped_arg;
5318}
5319
5320/*
5321 * Build a command line from a list, taking care of escaping.
5322 * The result is put in gap->ga_data.
5323 * Returns FAIL when out of memory.
5324 */
5325 int
5326win32_build_cmd(list_T *l, garray_T *gap)
5327{
5328 listitem_T *li;
5329 char_u *s;
5330
5331 for (li = l->lv_first; li != NULL; li = li->li_next)
5332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005333 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005334 if (s == NULL)
5335 return FAIL;
5336 s = win32_escape_arg(s);
5337 if (s == NULL)
5338 return FAIL;
5339 ga_concat(gap, s);
5340 vim_free(s);
5341 if (li->li_next != NULL)
5342 ga_append(gap, ' ');
5343 }
5344 return OK;
5345}
5346#endif
5347
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005348/*
5349 * NOTE: Must call job_cleanup() only once right after the status of "job"
5350 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5351 * mch_detect_ended_job() returned non-NULL).
5352 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005353 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005354job_cleanup(job_T *job)
5355{
5356 if (job->jv_status != JOB_ENDED)
5357 return;
5358
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005359 /* Ready to cleanup the job. */
5360 job->jv_status = JOB_FINISHED;
5361
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005362 /* When only channel-in is kept open, close explicitly. */
5363 if (job->jv_channel != NULL)
5364 ch_close_part(job->jv_channel, PART_IN);
5365
Bram Moolenaar97792de2016-10-15 18:36:49 +02005366 if (job->jv_exit_cb != NULL)
5367 {
5368 typval_T argv[3];
5369 typval_T rettv;
5370 int dummy;
5371
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005372 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005373 ch_log(job->jv_channel, "Invoking exit callback %s", job->jv_exit_cb);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005374 ++job->jv_refcount;
5375 argv[0].v_type = VAR_JOB;
5376 argv[0].vval.v_job = job;
5377 argv[1].v_type = VAR_NUMBER;
5378 argv[1].vval.v_number = job->jv_exitval;
5379 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
5380 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
5381 job->jv_exit_partial, NULL);
5382 clear_tv(&rettv);
5383 --job->jv_refcount;
5384 channel_need_redraw = TRUE;
5385 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005386
5387 /* Do not free the job in case the close callback of the associated channel
5388 * isn't invoked yet and may get information by job_info(). */
5389 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar97792de2016-10-15 18:36:49 +02005390 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005391 /* The job was already unreferenced and the associated channel was
5392 * detached, now that it ended it can be freed. Careful: caller must
5393 * not use "job" after this! */
Bram Moolenaar97792de2016-10-15 18:36:49 +02005394 job_free(job);
5395 }
5396}
5397
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005398/*
5399 * Mark references in jobs that are still useful.
5400 */
5401 int
5402set_ref_in_job(int copyID)
5403{
5404 int abort = FALSE;
5405 job_T *job;
5406 typval_T tv;
5407
5408 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005409 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005410 {
5411 tv.v_type = VAR_JOB;
5412 tv.vval.v_job = job;
5413 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5414 }
5415 return abort;
5416}
5417
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005418/*
5419 * Dereference "job". Note that after this "job" may have been freed.
5420 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005421 void
5422job_unref(job_T *job)
5423{
5424 if (job != NULL && --job->jv_refcount <= 0)
5425 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005426 /* Do not free the job if there is a channel where the close callback
5427 * may get the job info. */
5428 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005429 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005430 /* Do not free the job when it has not ended yet and there is a
5431 * "stoponexit" flag or an exit callback. */
5432 if (!job_need_end_check(job))
5433 {
5434 job_free(job);
5435 }
5436 else if (job->jv_channel != NULL)
5437 {
5438 /* Do remove the link to the channel, otherwise it hangs
5439 * around until Vim exits. See job_free() for refcount. */
5440 ch_log(job->jv_channel, "detaching channel from job");
5441 job->jv_channel->ch_job = NULL;
5442 channel_unref(job->jv_channel);
5443 job->jv_channel = NULL;
5444 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005445 }
5446 }
5447}
5448
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005449 int
5450free_unused_jobs_contents(int copyID, int mask)
5451{
5452 int did_free = FALSE;
5453 job_T *job;
5454
5455 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005456 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005457 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005458 {
5459 /* Free the channel and ordinary items it contains, but don't
5460 * recurse into Lists, Dictionaries etc. */
5461 job_free_contents(job);
5462 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005463 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005464 return did_free;
5465}
5466
5467 void
5468free_unused_jobs(int copyID, int mask)
5469{
5470 job_T *job;
5471 job_T *job_next;
5472
5473 for (job = first_job; job != NULL; job = job_next)
5474 {
5475 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005476 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005477 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005478 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005479 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005480 job_free_job(job);
5481 }
5482 }
5483}
5484
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005485/*
5486 * Allocate a job. Sets the refcount to one and sets options default.
5487 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005488 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005489job_alloc(void)
5490{
5491 job_T *job;
5492
5493 job = (job_T *)alloc_clear(sizeof(job_T));
5494 if (job != NULL)
5495 {
5496 job->jv_refcount = 1;
5497 job->jv_stoponexit = vim_strsave((char_u *)"term");
5498
5499 if (first_job != NULL)
5500 {
5501 first_job->jv_prev = job;
5502 job->jv_next = first_job;
5503 }
5504 first_job = job;
5505 }
5506 return job;
5507}
5508
5509 void
5510job_set_options(job_T *job, jobopt_T *opt)
5511{
5512 if (opt->jo_set & JO_STOPONEXIT)
5513 {
5514 vim_free(job->jv_stoponexit);
5515 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5516 job->jv_stoponexit = NULL;
5517 else
5518 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5519 }
5520 if (opt->jo_set & JO_EXIT_CB)
5521 {
Bram Moolenaar1436d8d2016-07-11 22:41:15 +02005522 free_callback(job->jv_exit_cb, job->jv_exit_partial);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005523 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005524 {
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005525 job->jv_exit_cb = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005526 job->jv_exit_partial = NULL;
5527 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005528 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005529 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005530 job->jv_exit_partial = opt->jo_exit_partial;
5531 if (job->jv_exit_partial != NULL)
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005532 {
5533 job->jv_exit_cb = opt->jo_exit_cb;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005534 ++job->jv_exit_partial->pt_refcount;
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005535 }
5536 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005537 {
Bram Moolenaar57e69ff2016-07-30 23:05:09 +02005538 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005539 func_ref(job->jv_exit_cb);
5540 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005541 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005542 }
5543}
5544
5545/*
5546 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5547 */
5548 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005549job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005550{
5551 job_T *job;
5552
5553 for (job = first_job; job != NULL; job = job->jv_next)
5554 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005555 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005556}
5557
5558/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005559 * Return TRUE when there is any job that has an exit callback and might exit,
5560 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005561 */
5562 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005563has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005564{
5565 job_T *job;
5566
5567 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005568 /* Only should check if the channel has been closed, if the channel is
5569 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005570 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5571 || (job->jv_status == JOB_FINISHED
5572 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005573 return TRUE;
5574 return FALSE;
5575}
5576
Bram Moolenaar97792de2016-10-15 18:36:49 +02005577#define MAX_CHECK_ENDED 8
5578
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005579/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005580 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005581 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005582 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005583 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005584job_check_ended(void)
5585{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005586 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005587 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005588
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005589 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005590 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005591 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005592
Bram Moolenaar97792de2016-10-15 18:36:49 +02005593 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005594 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005595 // NOTE: mch_detect_ended_job() must only return a job of which the
5596 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005597 job_T *job = mch_detect_ended_job(first_job);
5598
5599 if (job == NULL)
5600 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005601 did_end = TRUE;
5602 job_cleanup(job); // may free "job"
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005603 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005604
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005605 if (channel_need_redraw)
5606 {
5607 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005608 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005609 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005610 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005611}
5612
5613/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005614 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005615 * "argv_arg" is only for Unix.
5616 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005617 * The returned job has a refcount of one.
5618 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005619 */
5620 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005621job_start(
5622 typval_T *argvars,
5623 char **argv_arg,
5624 jobopt_T *opt_arg,
5625 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005626{
5627 job_T *job;
5628 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005629 char **argv = NULL;
5630 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005631#if defined(UNIX)
5632# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005633 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005634#else
5635 garray_T ga;
5636#endif
5637 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005638 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005639
5640 job = job_alloc();
5641 if (job == NULL)
5642 return NULL;
5643
5644 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005645#ifndef USE_ARGV
5646 ga_init2(&ga, (int)sizeof(char*), 20);
5647#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005648
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005649 if (opt_arg != NULL)
5650 opt = *opt_arg;
5651 else
5652 {
5653 /* Default mode is NL. */
5654 clear_job_options(&opt);
5655 opt.jo_mode = MODE_NL;
5656 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005657 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5658 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5659 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005660 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005661 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005662
5663 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005664 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005665 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5666 && opt.jo_io[part] == JIO_FILE
5667 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5668 || *opt.jo_io_name[part] == NUL))
5669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005670 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005671 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005672 }
5673
5674 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5675 {
5676 buf_T *buf = NULL;
5677
5678 /* check that we can find the buffer before starting the job */
5679 if (opt.jo_set & JO_IN_BUF)
5680 {
5681 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5682 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005683 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005684 }
5685 else if (!(opt.jo_set & JO_IN_NAME))
5686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005687 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005688 }
5689 else
5690 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5691 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005692 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005693 if (buf->b_ml.ml_mfp == NULL)
5694 {
5695 char_u numbuf[NUMBUFLEN];
5696 char_u *s;
5697
5698 if (opt.jo_set & JO_IN_BUF)
5699 {
5700 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5701 s = numbuf;
5702 }
5703 else
5704 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005705 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005706 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005707 }
5708 job->jv_in_buf = buf;
5709 }
5710
5711 job_set_options(job, &opt);
5712
Bram Moolenaar13568252018-03-16 20:46:58 +01005713#ifdef USE_ARGV
5714 if (argv_arg != NULL)
5715 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005716 /* Make a copy of argv_arg for job->jv_argv. */
5717 for (i = 0; argv_arg[i] != NULL; i++)
5718 argc++;
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005719 argv = (char **)alloc(sizeof(char *) * (argc + 1));
Bram Moolenaar20608922018-04-21 22:30:08 +02005720 if (argv == NULL)
5721 goto theend;
5722 for (i = 0; i < argc; i++)
5723 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5724 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005725 }
5726 else
5727#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005728 if (argvars[0].v_type == VAR_STRING)
5729 {
5730 /* Command is a string. */
5731 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005732 if (cmd == NULL || *cmd == NUL)
5733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005734 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005735 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005736 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005737
5738 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005739 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005740 }
5741 else if (argvars[0].v_type != VAR_LIST
5742 || argvars[0].vval.v_list == NULL
5743 || argvars[0].vval.v_list->lv_len < 1)
5744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005745 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005746 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005747 }
5748 else
5749 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005750 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005751
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005752 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005753 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005754#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005755 if (win32_build_cmd(l, &ga) == FAIL)
5756 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005757 cmd = ga.ga_data;
5758#endif
5759 }
5760
Bram Moolenaar20608922018-04-21 22:30:08 +02005761 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005762 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005763
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005764#ifdef USE_ARGV
5765 if (ch_log_active())
5766 {
5767 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005768
5769 ga_init2(&ga, (int)sizeof(char), 200);
5770 for (i = 0; i < argc; ++i)
5771 {
5772 if (i > 0)
5773 ga_concat(&ga, (char_u *)" ");
5774 ga_concat(&ga, (char_u *)argv[i]);
5775 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005776 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005777 ga_clear(&ga);
5778 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005779 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005780#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005781 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005782 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005783#endif
5784
5785 /* If the channel is reading from a buffer, write lines now. */
5786 if (job->jv_channel != NULL)
5787 channel_write_in(job->jv_channel);
5788
5789theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005790#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005791 vim_free(ga.ga_data);
5792#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005793 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005794 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005795 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005796 return job;
5797}
5798
5799/*
5800 * Get the status of "job" and invoke the exit callback when needed.
5801 * The returned string is not allocated.
5802 */
5803 char *
5804job_status(job_T *job)
5805{
5806 char *result;
5807
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005808 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005809 /* No need to check, dead is dead. */
5810 result = "dead";
5811 else if (job->jv_status == JOB_FAILED)
5812 result = "fail";
5813 else
5814 {
5815 result = mch_job_status(job);
5816 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005817 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005818 }
5819 return result;
5820}
5821
Bram Moolenaar8950a562016-03-12 15:22:55 +01005822/*
5823 * Implementation of job_info().
5824 */
5825 void
5826job_info(job_T *job, dict_T *dict)
5827{
5828 dictitem_T *item;
5829 varnumber_T nr;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005830 list_T *l;
5831 int i;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005832
Bram Moolenaare0be1672018-07-08 16:50:37 +02005833 dict_add_string(dict, "status", (char_u *)job_status(job));
Bram Moolenaar8950a562016-03-12 15:22:55 +01005834
5835 item = dictitem_alloc((char_u *)"channel");
5836 if (item == NULL)
5837 return;
Bram Moolenaar8950a562016-03-12 15:22:55 +01005838 item->di_tv.v_type = VAR_CHANNEL;
5839 item->di_tv.vval.v_channel = job->jv_channel;
5840 if (job->jv_channel != NULL)
5841 ++job->jv_channel->ch_refcount;
5842 if (dict_add(dict, item) == FAIL)
5843 dictitem_free(item);
5844
5845#ifdef UNIX
5846 nr = job->jv_pid;
5847#else
5848 nr = job->jv_proc_info.dwProcessId;
5849#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005850 dict_add_number(dict, "process", nr);
5851 dict_add_string(dict, "tty_in", job->jv_tty_in);
5852 dict_add_string(dict, "tty_out", job->jv_tty_out);
Bram Moolenaar8950a562016-03-12 15:22:55 +01005853
Bram Moolenaare0be1672018-07-08 16:50:37 +02005854 dict_add_number(dict, "exitval", job->jv_exitval);
5855 dict_add_string(dict, "exit_cb", job->jv_exit_cb);
5856 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005857
5858 l = list_alloc();
5859 if (l != NULL)
5860 {
5861 dict_add_list(dict, "cmd", l);
Bram Moolenaar20608922018-04-21 22:30:08 +02005862 if (job->jv_argv != NULL)
5863 for (i = 0; job->jv_argv[i] != NULL; i++)
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005864 list_append_string(l, (char_u *)job->jv_argv[i], -1);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005865 }
5866}
5867
5868/*
5869 * Implementation of job_info() to return info for all jobs.
5870 */
5871 void
5872job_info_all(list_T *l)
5873{
5874 job_T *job;
5875 typval_T tv;
5876
5877 for (job = first_job; job != NULL; job = job->jv_next)
5878 {
5879 tv.v_type = VAR_JOB;
5880 tv.vval.v_job = job;
5881
5882 if (list_append_tv(l, &tv) != OK)
5883 return;
5884 }
Bram Moolenaar8950a562016-03-12 15:22:55 +01005885}
5886
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005887/*
5888 * Send a signal to "job". Implements job_stop().
5889 * When "type" is not NULL use this for the type.
5890 * Otherwise use argvars[1] for the type.
5891 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005892 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005893job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005894{
5895 char_u *arg;
5896
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005897 if (type != NULL)
5898 arg = (char_u *)type;
5899 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005900 arg = (char_u *)"";
5901 else
5902 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005903 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005904 if (arg == NULL)
5905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005906 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005907 return 0;
5908 }
5909 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02005910 if (job->jv_status == JOB_FAILED)
5911 {
5912 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
5913 return 0;
5914 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005915 if (job->jv_status == JOB_ENDED)
5916 {
5917 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
5918 return 0;
5919 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005920 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005921 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005922 return 0;
5923
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02005924 /* Assume that only "kill" will kill the job. */
5925 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005926 job->jv_channel->ch_job_killed = TRUE;
5927
5928 /* We don't try freeing the job, obviously the caller still has a
5929 * reference to it. */
5930 return 1;
5931}
5932
Bram Moolenaarf2732452018-06-03 14:47:35 +02005933 void
5934invoke_prompt_callback(void)
5935{
5936 typval_T rettv;
5937 int dummy;
5938 typval_T argv[2];
5939 char_u *text;
5940 char_u *prompt;
5941 linenr_T lnum = curbuf->b_ml.ml_line_count;
5942
5943 // Add a new line for the prompt before invoking the callback, so that
5944 // text can always be inserted above the last line.
5945 ml_append(lnum, (char_u *)"", 0, FALSE);
5946 curwin->w_cursor.lnum = lnum + 1;
5947 curwin->w_cursor.col = 0;
5948
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005949 if (curbuf->b_prompt_callback == NULL || *curbuf->b_prompt_callback == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02005950 return;
5951 text = ml_get(lnum);
5952 prompt = prompt_text();
5953 if (STRLEN(text) >= STRLEN(prompt))
5954 text += STRLEN(prompt);
5955 argv[0].v_type = VAR_STRING;
5956 argv[0].vval.v_string = vim_strsave(text);
5957 argv[1].v_type = VAR_UNKNOWN;
5958
5959 call_func(curbuf->b_prompt_callback,
5960 (int)STRLEN(curbuf->b_prompt_callback),
5961 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE,
5962 curbuf->b_prompt_partial, NULL);
5963 clear_tv(&argv[0]);
5964 clear_tv(&rettv);
5965}
5966
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02005967/*
5968 * Return TRUE when the interrupt callback was invoked.
5969 */
5970 int
5971invoke_prompt_interrupt(void)
5972{
5973 typval_T rettv;
5974 int dummy;
5975 typval_T argv[1];
5976
5977 if (curbuf->b_prompt_interrupt == NULL
5978 || *curbuf->b_prompt_interrupt == NUL)
5979 return FALSE;
5980 argv[0].v_type = VAR_UNKNOWN;
5981
5982 got_int = FALSE; // don't skip executing commands
5983 call_func(curbuf->b_prompt_interrupt,
5984 (int)STRLEN(curbuf->b_prompt_interrupt),
5985 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE,
5986 curbuf->b_prompt_int_partial, NULL);
5987 clear_tv(&rettv);
5988 return TRUE;
5989}
5990
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005991#endif /* FEAT_JOB_CHANNEL */