blob: 8b0abdc3020f7c1d5ab0cd91b0bb862778df3e26 [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 Moolenaar4f974752019-02-17 17:44:42 +010023#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +010024/* 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 Moolenaar4f974752019-02-17 17:44:42 +010068#ifdef MSWIN
Bram Moolenaard8070362016-02-15 21:56:54 +010069 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 Moolenaar24058382019-01-24 23:11:49 +010083 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010084 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010085 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010086 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010087
Bram Moolenaar24058382019-01-24 23:11:49 +010088 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 {
Bram Moolenaar24058382019-01-24 23:11:49 +010090 if (todo > MAX_NAMED_PIPE_SIZE)
91 size = MAX_NAMED_PIPE_SIZE;
92 else
Bram Moolenaardec01202019-01-28 20:04:24 +010093 size = (DWORD)todo;
Bram Moolenaar65240682019-02-10 22:23:26 +010094 // If the pipe overflows while the job does not read the data,
95 // WriteFile() will block forever. This abandons the write.
Bram Moolenaar24058382019-01-24 23:11:49 +010096 memset(&ov, 0, sizeof(ov));
Bram Moolenaar65240682019-02-10 22:23:26 +010097 nwrite = 0;
Bram Moolenaar24058382019-01-24 23:11:49 +010098 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
99 {
100 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100101
Bram Moolenaar24058382019-01-24 23:11:49 +0100102 if (err != ERROR_IO_PENDING)
103 return -1;
104 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
105 return -1;
106 FlushFileBuffers(h);
107 }
Bram Moolenaar65240682019-02-10 22:23:26 +0100108 else if (nwrite == 0)
109 // WriteFile() returns TRUE but did not write anything. This causes
110 // a hang, so bail out.
111 break;
Bram Moolenaar24058382019-01-24 23:11:49 +0100112 todo -= nwrite;
113 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100114 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100115 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100116}
117
118 static void
119fd_close(sock_T fd)
120{
121 HANDLE h = (HANDLE)fd;
122
123 CloseHandle(h);
124}
125#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100126
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100127/* Log file opened with ch_logfile(). */
128static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100129#ifdef FEAT_RELTIME
130static proftime_T log_start;
131#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100132
133 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100134ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100135{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100136 FILE *file = NULL;
137
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100138 if (log_fd != NULL)
139 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100140
141 if (*fname != NUL)
142 {
143 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
144 if (file == NULL)
145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100146 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100147 return;
148 }
149 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100150 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100151
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100152 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100153 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100154 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100155#ifdef FEAT_RELTIME
156 profile_start(&log_start);
157#endif
158 }
159}
160
161 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200162ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100163{
164 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100165}
166
167 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200168ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100169{
170 if (log_fd != NULL)
171 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100172#ifdef FEAT_RELTIME
173 proftime_T log_now;
174
175 profile_start(&log_now);
176 profile_sub(&log_now, &log_start);
177 fprintf(log_fd, "%s ", profile_msg(&log_now));
178#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100179 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200180 {
181 if (part < PART_COUNT)
182 fprintf(log_fd, "%son %d(%s): ",
183 what, ch->ch_id, part_names[part]);
184 else
185 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
186 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100187 else
188 fprintf(log_fd, "%s: ", what);
189 }
190}
191
Bram Moolenaard0b65022016-03-06 21:50:33 +0100192static int did_log_msg = TRUE;
193
Bram Moolenaar8caa43d2019-02-20 22:45:06 +0100194#ifndef PROTO // prototype is in proto.h
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100195 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200196ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100197{
198 if (log_fd != NULL)
199 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200200 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100201
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200202 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200203 va_start(ap, fmt);
204 vfprintf(log_fd, fmt, ap);
205 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100206 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100207 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100208 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100209 }
210}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200211#endif
212
213 static void
214ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200215#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
216 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200217#endif
218 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100219
220 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200221ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100222{
223 if (log_fd != NULL)
224 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200225 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100226
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200227 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200228 va_start(ap, fmt);
229 vfprintf(log_fd, fmt, ap);
230 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100231 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100232 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100233 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100234 }
235}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100236
Bram Moolenaar4f974752019-02-17 17:44:42 +0100237#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100238# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100239# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100240
241 static char *
242strerror_win32(int eno)
243{
244 static LPVOID msgbuf = NULL;
245 char_u *ptr;
246
247 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200248 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100249 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200250 msgbuf = NULL;
251 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100252 FormatMessage(
253 FORMAT_MESSAGE_ALLOCATE_BUFFER |
254 FORMAT_MESSAGE_FROM_SYSTEM |
255 FORMAT_MESSAGE_IGNORE_INSERTS,
256 NULL,
257 eno,
258 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
259 (LPTSTR) &msgbuf,
260 0,
261 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200262 if (msgbuf != NULL)
263 /* chomp \r or \n */
264 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
265 switch (*ptr)
266 {
267 case '\r':
268 STRMOVE(ptr, ptr + 1);
269 ptr--;
270 break;
271 case '\n':
272 if (*(ptr + 1) == '\0')
273 *ptr = '\0';
274 else
275 *ptr = ' ';
276 break;
277 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100278 return msgbuf;
279}
280#endif
281
Bram Moolenaar77073442016-02-13 23:23:53 +0100282/*
283 * The list of all allocated channels.
284 */
285static channel_T *first_channel = NULL;
286static int next_ch_id = 0;
287
288/*
289 * Allocate a new channel. The refcount is set to 1.
290 * The channel isn't actually used until it is opened.
291 * Returns NULL if out of memory.
292 */
293 channel_T *
294add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100295{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200296 ch_part_T part;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200297 channel_T *channel = ALLOC_CLEAR_ONE(channel_T);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100298
Bram Moolenaar77073442016-02-13 23:23:53 +0100299 if (channel == NULL)
300 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100301
Bram Moolenaar77073442016-02-13 23:23:53 +0100302 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100303 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100304
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200305 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100306 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100307 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100308#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100309 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100310#endif
311#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100315 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100316
Bram Moolenaar77073442016-02-13 23:23:53 +0100317 if (first_channel != NULL)
318 {
319 first_channel->ch_prev = channel;
320 channel->ch_next = first_channel;
321 }
322 first_channel = channel;
323
324 channel->ch_refcount = 1;
325 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100326}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100327
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200328 int
329has_any_channel(void)
330{
331 return first_channel != NULL;
332}
333
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100334/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100335 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100336 * Return TRUE if "channel" has a callback and the associated job wasn't
337 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100338 */
339 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100340channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100341{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100342 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100343 int has_out_msg;
344 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100345
346 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100347 if (channel->ch_job_killed && channel->ch_job == NULL)
348 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100349
350 /* If there is a close callback it may still need to be invoked. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200351 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100352 return TRUE;
353
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200354 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200355 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200356 return TRUE;
357
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100358 /* If there is no callback then nobody can get readahead. If the fd is
359 * closed and there is no readahead then the callback won't be called. */
360 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100361 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
362 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100363 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
364 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
365 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
366 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
367 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
368 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200369 return (channel->ch_callback.cb_name != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100370 || has_out_msg || has_err_msg))
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200371 || ((channel->ch_part[PART_OUT].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200372 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
373 && has_out_msg)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200374 || ((channel->ch_part[PART_ERR].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200375 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
376 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100377}
378
379/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200380 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
381 */
382 static int
383channel_can_close(channel_T *channel)
384{
385 return channel->ch_to_be_closed == 0;
386}
387
388/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200389 * Close a channel and free all its resources.
390 */
391 static void
392channel_free_contents(channel_T *channel)
393{
394 channel_close(channel, TRUE);
395 channel_clear(channel);
396 ch_log(channel, "Freeing channel");
397}
398
399 static void
400channel_free_channel(channel_T *channel)
401{
402 if (channel->ch_next != NULL)
403 channel->ch_next->ch_prev = channel->ch_prev;
404 if (channel->ch_prev == NULL)
405 first_channel = channel->ch_next;
406 else
407 channel->ch_prev->ch_next = channel->ch_next;
408 vim_free(channel);
409}
410
411 static void
412channel_free(channel_T *channel)
413{
414 if (!in_free_unref_items)
415 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200416 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200417 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200418 else
419 {
420 channel_free_contents(channel);
421 channel_free_channel(channel);
422 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200423 }
424}
425
426/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100427 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100428 * possible, there is no callback to be invoked or the associated job was
429 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100430 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100431 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100432 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100433channel_may_free(channel_T *channel)
434{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100435 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100436 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100437 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100438 return TRUE;
439 }
440 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100441}
442
443/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100444 * Decrement the reference count on "channel" and maybe free it when it goes
445 * down to zero. Don't free it if there is a pending action.
446 * Returns TRUE when the channel is no longer referenced.
447 */
448 int
449channel_unref(channel_T *channel)
450{
451 if (channel != NULL && --channel->ch_refcount <= 0)
452 return channel_may_free(channel);
453 return FALSE;
454}
455
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200456 int
457free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100458{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200459 int did_free = FALSE;
460 channel_T *ch;
461
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200462 /* This is invoked from the garbage collector, which only runs at a safe
463 * point. */
464 ++safe_to_invoke_callback;
465
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200466 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200467 if (!channel_still_useful(ch)
468 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200469 {
470 /* Free the channel and ordinary items it contains, but don't
471 * recurse into Lists, Dictionaries etc. */
472 channel_free_contents(ch);
473 did_free = TRUE;
474 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200475
476 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200477 return did_free;
478}
479
480 void
481free_unused_channels(int copyID, int mask)
482{
483 channel_T *ch;
484 channel_T *ch_next;
485
486 for (ch = first_channel; ch != NULL; ch = ch_next)
487 {
488 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200489 if (!channel_still_useful(ch)
490 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200491 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200492 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200493 channel_free_channel(ch);
494 }
495 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100496}
497
Bram Moolenaard04a0202016-01-26 23:30:18 +0100498#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100499
500#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
501 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100502channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100503{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100504 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200505 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100506
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100507 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100508 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200509 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100510 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200511 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100512}
513#endif
514
Bram Moolenaare0874f82016-01-24 20:36:41 +0100515/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100516 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100517 */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100518#ifdef FEAT_GUI_X11
519 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200520messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200521 int *unused1 UNUSED,
522 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100523{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100524 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100525}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100526#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100527
Bram Moolenaard04a0202016-01-26 23:30:18 +0100528#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +0100529# if GTK_CHECK_VERSION(3,0,0)
530 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200531messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200532 GIOCondition unused2 UNUSED,
533 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100534{
535 channel_read_fd(GPOINTER_TO_INT(clientData));
536 return TRUE; /* Return FALSE instead in case the event source is to
537 * be removed after this function returns. */
538}
539# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100540 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200541messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200542 gint unused1 UNUSED,
543 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100544{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100545 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100546}
Bram Moolenaar98921892016-02-23 17:14:37 +0100547# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100548#endif
549
550 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200551channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100552{
Bram Moolenaarde279892016-03-11 22:19:44 +0100553 if (!CH_HAS_GUI)
554 return;
555
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200556 /* gets stuck in handling events for a not connected channel */
557 if (channel->ch_keep_open)
558 return;
559
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100560# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200561 /* Tell notifier we are interested in being called when there is input on
562 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100563 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200564 {
565 ch_log(channel, "Registering part %s with fd %d",
566 part_names[part], channel->ch_part[part].ch_fd);
567
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100568 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100569 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100570 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100571 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200572 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100573 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200574 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100575# else
576# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200577 /* Tell gdk we are interested in being called when there is input on the
578 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100579 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100580 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200581 ch_log(channel, "Registering part %s with fd %d",
582 part_names[part], channel->ch_part[part].ch_fd);
583# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100584 GIOChannel *chnnl = g_io_channel_unix_new(
585 (gint)channel->ch_part[part].ch_fd);
586
587 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
588 chnnl,
589 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200590 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100591 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
592
593 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100594# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100595 channel->ch_part[part].ch_inputHandler = gdk_input_add(
596 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100597 (GdkInputCondition)
598 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200599 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100600 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100601# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200602 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100603# endif
604# endif
605}
606
Bram Moolenaarde279892016-03-11 22:19:44 +0100607 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100608channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100609{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100610 if (channel->CH_SOCK_FD != INVALID_FD)
611 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200612 if (channel->CH_OUT_FD != INVALID_FD
613 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100614 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200615 if (channel->CH_ERR_FD != INVALID_FD
616 && channel->CH_ERR_FD != channel->CH_SOCK_FD
617 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100618 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100619}
620
621/*
622 * Register any of our file descriptors with the GUI event handling system.
623 * Called when the GUI has started.
624 */
625 void
626channel_gui_register_all(void)
627{
Bram Moolenaar77073442016-02-13 23:23:53 +0100628 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100629
Bram Moolenaar77073442016-02-13 23:23:53 +0100630 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100631 channel_gui_register(channel);
632}
633
634 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200635channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100636{
637# ifdef FEAT_GUI_X11
638 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
639 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200640 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100641 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
642 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
643 }
644# else
645# ifdef FEAT_GUI_GTK
646 if (channel->ch_part[part].ch_inputHandler != 0)
647 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200648 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100649# if GTK_CHECK_VERSION(3,0,0)
650 g_source_remove(channel->ch_part[part].ch_inputHandler);
651# else
652 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
653# endif
654 channel->ch_part[part].ch_inputHandler = 0;
655 }
656# endif
657# endif
658}
659
660 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100661channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100662{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200663 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100664
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100665 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100666 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100667}
668
669#endif
670
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100671static char *e_cannot_connect = N_("E902: Cannot connect to port");
672
Bram Moolenaard04a0202016-01-26 23:30:18 +0100673/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100674 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100675 * "waittime" is the time in msec to wait for the connection.
676 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100677 * Returns the channel for success.
678 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100679 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100680 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100681channel_open(
682 char *hostname,
683 int port_in,
684 int waittime,
685 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100686{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100687 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100688 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100689 struct hostent *host;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100690#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100691 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100692 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100693#else
694 int port = port_in;
695#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100696 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100697 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100698
Bram Moolenaar4f974752019-02-17 17:44:42 +0100699#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100700 channel_init_winsock();
701#endif
702
Bram Moolenaar77073442016-02-13 23:23:53 +0100703 channel = add_channel();
704 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100705 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100706 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100707 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100708 }
709
710 /* Get the server internet address and put into addr structure */
711 /* fill in the socket address structure and connect to server */
712 vim_memset((char *)&server, 0, sizeof(server));
713 server.sin_family = AF_INET;
714 server.sin_port = htons(port);
715 if ((host = gethostbyname(hostname)) == NULL)
716 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100717 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200718 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100719 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100720 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100721 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100722 {
723 char *p;
724
Bram Moolenaar2e324952018-04-14 14:37:07 +0200725 /* When using host->h_addr_list[0] directly ubsan warns for it to not
726 * be aligned. First copy the pointer to avoid that. */
727 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100728 memcpy((char *)&server.sin_addr, p, host->h_length);
729 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100730
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100731 /* On Mac and Solaris a zero timeout almost never works. At least wait
732 * one millisecond. Let's do it for all systems, because we don't know why
733 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100734 if (waittime == 0)
735 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100736
737 /*
738 * For Unix we need to call connect() again after connect() failed.
739 * On Win32 one time is sufficient.
740 */
741 while (TRUE)
742 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100743 long elapsed_msec = 0;
744 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100745
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100746 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100747 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100748 sd = socket(AF_INET, SOCK_STREAM, 0);
749 if (sd == -1)
750 {
751 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200752 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100753 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100754 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100755 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100756
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100757 if (waittime >= 0)
758 {
759 /* Make connect() non-blocking. */
760 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +0100761#ifdef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100762 ioctlsocket(sd, FIONBIO, &val) < 0
763#else
764 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
765#endif
766 )
767 {
768 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200769 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100770 "channel_open: Connect failed with errno %d", errno);
771 sock_close(sd);
772 channel_free(channel);
773 return NULL;
774 }
775 }
776
777 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200778 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100779 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
780
Bram Moolenaar045a2842016-03-08 22:33:07 +0100781 if (ret == 0)
782 /* The connection could be established. */
783 break;
784
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100785 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100786 if (waittime < 0 || (errno != EWOULDBLOCK
787 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100788#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100789 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100790#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100791 ))
792 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200793 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100794 "channel_open: Connect failed with errno %d", errno);
795 PERROR(_(e_cannot_connect));
796 sock_close(sd);
797 channel_free(channel);
798 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100799 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100800
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100801 /* Limit the waittime to 50 msec. If it doesn't work within this
802 * time we close the socket and try creating it again. */
803 waitnow = waittime > 50 ? 50 : waittime;
804
Bram Moolenaar045a2842016-03-08 22:33:07 +0100805 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100806 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100807#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100808 if (errno != ECONNREFUSED)
809#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100810 {
811 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100812 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100813 fd_set wfds;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100814#ifndef MSWIN
Bram Moolenaard42119f2016-02-28 20:51:49 +0100815 int so_error = 0;
816 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100817 struct timeval start_tv;
818 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100819#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100820 FD_ZERO(&rfds);
821 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100822 FD_ZERO(&wfds);
823 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100824
Bram Moolenaar562ca712016-03-09 21:50:05 +0100825 tv.tv_sec = waitnow / 1000;
826 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100827#ifndef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100828 gettimeofday(&start_tv, NULL);
829#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200830 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100831 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100832 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100833
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100834 if (ret < 0)
835 {
836 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200837 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100838 "channel_open: Connect failed with errno %d", errno);
839 PERROR(_(e_cannot_connect));
840 sock_close(sd);
841 channel_free(channel);
842 return NULL;
843 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100844
Bram Moolenaar4f974752019-02-17 17:44:42 +0100845#ifdef MSWIN
Bram Moolenaar562ca712016-03-09 21:50:05 +0100846 /* On Win32: select() is expected to work and wait for up to
847 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100848 if (FD_ISSET(sd, &wfds))
849 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100850 elapsed_msec = waitnow;
851 if (waittime > 1 && elapsed_msec < waittime)
852 {
853 waittime -= elapsed_msec;
854 continue;
855 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100856#else
857 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100858 * After putting the socket in non-blocking mode, connect() will
859 * return EINPROGRESS, select() will not wait (as if writing is
860 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100861 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100862 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100863 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100864 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100865 {
866 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100867 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100868 if (ret < 0 || (so_error != 0
869 && so_error != EWOULDBLOCK
870 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100871# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100872 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100873# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100874 ))
875 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200876 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100877 "channel_open: Connect failed with errno %d",
878 so_error);
879 PERROR(_(e_cannot_connect));
880 sock_close(sd);
881 channel_free(channel);
882 return NULL;
883 }
884 }
885
Bram Moolenaar045a2842016-03-08 22:33:07 +0100886 if (FD_ISSET(sd, &wfds) && so_error == 0)
887 /* Did not detect an error, connection is established. */
888 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100889
Bram Moolenaar045a2842016-03-08 22:33:07 +0100890 gettimeofday(&end_tv, NULL);
891 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
892 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100893#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100894 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100895
Bram Moolenaar4f974752019-02-17 17:44:42 +0100896#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100897 if (waittime > 1 && elapsed_msec < waittime)
898 {
899 /* The port isn't ready but we also didn't get an error.
900 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100901 * yet. Select() may return early, wait until the remaining
902 * "waitnow" and try again. */
903 waitnow -= elapsed_msec;
904 waittime -= elapsed_msec;
905 if (waitnow > 0)
906 {
907 mch_delay((long)waitnow, TRUE);
908 ui_breakcheck();
909 waittime -= waitnow;
910 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100911 if (!got_int)
912 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100913 if (waittime <= 0)
914 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100915 waittime = 1;
916 continue;
917 }
918 /* we were interrupted, behave as if timed out */
919 }
920#endif
921
922 /* We timed out. */
923 ch_error(channel, "Connection timed out");
924 sock_close(sd);
925 channel_free(channel);
926 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100927 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100928
Bram Moolenaar045a2842016-03-08 22:33:07 +0100929 ch_log(channel, "Connection made");
930
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100931 if (waittime >= 0)
932 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100933#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100934 val = 0;
935 ioctlsocket(sd, FIONBIO, &val);
936#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100937 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100938#endif
939 }
940
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100941 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100942 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100943 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
944 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200945 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100946
947#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100948 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100949#endif
950
Bram Moolenaar77073442016-02-13 23:23:53 +0100951 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100952}
953
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100954/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +0200955 * Copy callback from "src" to "dest", incrementing the refcounts.
956 */
957 static void
958copy_callback(callback_T *dest, callback_T *src)
959{
960 dest->cb_partial = src->cb_partial;
961 if (dest->cb_partial != NULL)
962 {
963 dest->cb_name = src->cb_name;
964 dest->cb_free_name = FALSE;
965 ++dest->cb_partial->pt_refcount;
966 }
967 else
968 {
969 dest->cb_name = vim_strsave(src->cb_name);
970 dest->cb_free_name = TRUE;
971 func_ref(src->cb_name);
972 }
973}
974
975 static void
976free_set_callback(callback_T *cbp, callback_T *callback)
977{
978 free_callback(cbp);
979
980 if (callback->cb_name != NULL && *callback->cb_name != NUL)
981 copy_callback(cbp, callback);
982 else
983 cbp->cb_name = NULL;
984}
985
986/*
987 * Prepare buffer "buf" for writing channel output to.
988 */
989 static void
990prepare_buffer(buf_T *buf)
991{
992 buf_T *save_curbuf = curbuf;
993
994 buf_copy_options(buf, BCO_ENTER);
995 curbuf = buf;
996#ifdef FEAT_QUICKFIX
997 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
998 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
999#endif
1000 if (curbuf->b_ml.ml_mfp == NULL)
1001 ml_open(curbuf);
1002 curbuf = save_curbuf;
1003}
1004
1005/*
1006 * Find a buffer matching "name" or create a new one.
1007 * Returns NULL if there is something very wrong (error already reported).
1008 */
1009 static buf_T *
1010find_buffer(char_u *name, int err, int msg)
1011{
1012 buf_T *buf = NULL;
1013 buf_T *save_curbuf = curbuf;
1014
1015 if (name != NULL && *name != NUL)
1016 {
1017 buf = buflist_findname(name);
1018 if (buf == NULL)
1019 buf = buflist_findname_exp(name);
1020 }
1021 if (buf == NULL)
1022 {
1023 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
1024 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
1025 if (buf == NULL)
1026 return NULL;
1027 prepare_buffer(buf);
1028
1029 curbuf = buf;
1030 if (msg)
1031 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1032 : "Reading from channel output..."), TRUE);
1033 changed_bytes(1, 0);
1034 curbuf = save_curbuf;
1035 }
1036
1037 return buf;
1038}
1039
1040/*
1041 * Set various properties from an "opt" argument.
1042 */
1043 static void
1044channel_set_options(channel_T *channel, jobopt_T *opt)
1045{
1046 ch_part_T part;
1047
1048 if (opt->jo_set & JO_MODE)
1049 for (part = PART_SOCK; part < PART_COUNT; ++part)
1050 channel->ch_part[part].ch_mode = opt->jo_mode;
1051 if (opt->jo_set & JO_IN_MODE)
1052 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1053 if (opt->jo_set & JO_OUT_MODE)
1054 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1055 if (opt->jo_set & JO_ERR_MODE)
1056 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
1057 channel->ch_nonblock = opt->jo_noblock;
1058
1059 if (opt->jo_set & JO_TIMEOUT)
1060 for (part = PART_SOCK; part < PART_COUNT; ++part)
1061 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1062 if (opt->jo_set & JO_OUT_TIMEOUT)
1063 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1064 if (opt->jo_set & JO_ERR_TIMEOUT)
1065 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1066 if (opt->jo_set & JO_BLOCK_WRITE)
1067 channel->ch_part[PART_IN].ch_block_write = 1;
1068
1069 if (opt->jo_set & JO_CALLBACK)
1070 free_set_callback(&channel->ch_callback, &opt->jo_callback);
1071 if (opt->jo_set & JO_OUT_CALLBACK)
1072 free_set_callback(&channel->ch_part[PART_OUT].ch_callback,
1073 &opt->jo_out_cb);
1074 if (opt->jo_set & JO_ERR_CALLBACK)
1075 free_set_callback(&channel->ch_part[PART_ERR].ch_callback,
1076 &opt->jo_err_cb);
1077 if (opt->jo_set & JO_CLOSE_CALLBACK)
1078 free_set_callback(&channel->ch_close_cb, &opt->jo_close_cb);
1079 channel->ch_drop_never = opt->jo_drop_never;
1080
1081 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1082 {
1083 buf_T *buf;
1084
1085 /* writing output to a buffer. Default mode is NL. */
1086 if (!(opt->jo_set & JO_OUT_MODE))
1087 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
1088 if (opt->jo_set & JO_OUT_BUF)
1089 {
1090 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1091 if (buf == NULL)
1092 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1093 }
1094 else
1095 {
1096 int msg = TRUE;
1097
1098 if (opt->jo_set2 & JO2_OUT_MSG)
1099 msg = opt->jo_message[PART_OUT];
1100 buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
1101 }
1102 if (buf != NULL)
1103 {
1104 if (opt->jo_set & JO_OUT_MODIFIABLE)
1105 channel->ch_part[PART_OUT].ch_nomodifiable =
1106 !opt->jo_modifiable[PART_OUT];
1107
1108 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1109 {
1110 emsg(_(e_modifiable));
1111 }
1112 else
1113 {
1114 ch_log(channel, "writing out to buffer '%s'",
1115 (char *)buf->b_ffname);
1116 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1117 // if the buffer was deleted or unloaded resurrect it
1118 if (buf->b_ml.ml_mfp == NULL)
1119 prepare_buffer(buf);
1120 }
1121 }
1122 }
1123
1124 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1125 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1126 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1127 {
1128 buf_T *buf;
1129
1130 /* writing err to a buffer. Default mode is NL. */
1131 if (!(opt->jo_set & JO_ERR_MODE))
1132 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1133 if (opt->jo_io[PART_ERR] == JIO_OUT)
1134 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1135 else if (opt->jo_set & JO_ERR_BUF)
1136 {
1137 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1138 if (buf == NULL)
1139 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1140 }
1141 else
1142 {
1143 int msg = TRUE;
1144
1145 if (opt->jo_set2 & JO2_ERR_MSG)
1146 msg = opt->jo_message[PART_ERR];
1147 buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1148 }
1149 if (buf != NULL)
1150 {
1151 if (opt->jo_set & JO_ERR_MODIFIABLE)
1152 channel->ch_part[PART_ERR].ch_nomodifiable =
1153 !opt->jo_modifiable[PART_ERR];
1154 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1155 {
1156 emsg(_(e_modifiable));
1157 }
1158 else
1159 {
1160 ch_log(channel, "writing err to buffer '%s'",
1161 (char *)buf->b_ffname);
1162 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1163 // if the buffer was deleted or unloaded resurrect it
1164 if (buf->b_ml.ml_mfp == NULL)
1165 prepare_buffer(buf);
1166 }
1167 }
1168 }
1169
1170 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1171 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1172 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
1173}
1174
1175/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001176 * Implements ch_open().
1177 */
1178 channel_T *
1179channel_open_func(typval_T *argvars)
1180{
1181 char_u *address;
1182 char_u *p;
1183 char *rest;
1184 int port;
1185 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001186 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001187
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001188 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001189 if (argvars[1].v_type != VAR_UNKNOWN
1190 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
1191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001193 return NULL;
1194 }
1195
1196 /* parse address */
1197 p = vim_strchr(address, ':');
1198 if (p == NULL)
1199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001201 return NULL;
1202 }
1203 *p++ = NUL;
1204 port = strtol((char *)p, &rest, 10);
1205 if (*address == NUL || port <= 0 || *rest != NUL)
1206 {
1207 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001209 return NULL;
1210 }
1211
1212 /* parse options */
1213 clear_job_options(&opt);
1214 opt.jo_mode = MODE_JSON;
1215 opt.jo_timeout = 2000;
1216 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02001217 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001218 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001219 if (opt.jo_timeout < 0)
1220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001222 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001223 }
1224
1225 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1226 if (channel != NULL)
1227 {
1228 opt.jo_set = JO_ALL;
1229 channel_set_options(channel, &opt);
1230 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001231theend:
1232 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001233 return channel;
1234}
1235
Bram Moolenaarde279892016-03-11 22:19:44 +01001236 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001237ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001238{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001239 sock_T *fd = &channel->ch_part[part].ch_fd;
1240
Bram Moolenaarde279892016-03-11 22:19:44 +01001241 if (*fd != INVALID_FD)
1242 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001243 if (part == PART_SOCK)
1244 sock_close(*fd);
1245 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001246 {
1247 /* When using a pty the same FD is set on multiple parts, only
1248 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001249 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1250 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1251 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001252 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001253#ifdef MSWIN
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001254 if (channel->ch_named_pipe)
1255 DisconnectNamedPipe((HANDLE)fd);
1256#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001257 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001258 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001259 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001260 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001261
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001262 /* channel is closed, may want to end the job if it was the last */
1263 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001264 }
1265}
1266
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001267 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001268channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001269{
Bram Moolenaarde279892016-03-11 22:19:44 +01001270 if (in != INVALID_FD)
1271 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001272 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001273 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001274# if defined(UNIX)
1275 /* Do not end the job when all output channels are closed, wait until
1276 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001277 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001278 channel->ch_to_be_closed |= (1U << PART_IN);
1279# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001280 }
1281 if (out != INVALID_FD)
1282 {
1283# if defined(FEAT_GUI)
1284 channel_gui_unregister_one(channel, PART_OUT);
1285# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001286 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001287 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001288 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001289# if defined(FEAT_GUI)
1290 channel_gui_register_one(channel, PART_OUT);
1291# endif
1292 }
1293 if (err != INVALID_FD)
1294 {
1295# if defined(FEAT_GUI)
1296 channel_gui_unregister_one(channel, PART_ERR);
1297# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001298 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001299 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001300 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001301# if defined(FEAT_GUI)
1302 channel_gui_register_one(channel, PART_ERR);
1303# endif
1304 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001305}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001306
Bram Moolenaard6051b52016-02-28 15:49:03 +01001307/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001308 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001309 * This does not keep a refcount, when the job is freed ch_job is cleared.
1310 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001311 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001312channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001313{
Bram Moolenaar77073442016-02-13 23:23:53 +01001314 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001315
1316 channel_set_options(channel, options);
1317
1318 if (job->jv_in_buf != NULL)
1319 {
1320 chanpart_T *in_part = &channel->ch_part[PART_IN];
1321
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001322 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001323 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001324 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001325 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001326 {
1327 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1328 {
1329 /* Special mode: send last-but-one line when appending a line
1330 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001331 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001332 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001333 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001334 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001335 }
1336 else
1337 in_part->ch_buf_top = options->jo_in_top;
1338 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001339 else
1340 in_part->ch_buf_top = 1;
1341 if (options->jo_set & JO_IN_BOT)
1342 in_part->ch_buf_bot = options->jo_in_bot;
1343 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001344 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001345 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001346}
1347
1348/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001349 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001350 */
1351 void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001352channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001353 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001354 ch_part_T part,
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001355 callback_T *callback,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001356 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001357{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001358 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001359 cbq_T *item = ALLOC_ONE(cbq_T);
Bram Moolenaara07fec92016-02-05 21:04:08 +01001360
1361 if (item != NULL)
1362 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001363 copy_callback(&item->cq_callback, callback);
Bram Moolenaar77073442016-02-13 23:23:53 +01001364 item->cq_seq_nr = id;
1365 item->cq_prev = head->cq_prev;
1366 head->cq_prev = item;
1367 item->cq_next = NULL;
1368 if (item->cq_prev == NULL)
1369 head->cq_next = item;
1370 else
1371 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001372 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001373}
1374
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001375 static void
1376write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1377{
1378 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001379 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001380 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001381 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001382
Bram Moolenaar655da312016-05-28 22:22:34 +02001383 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001384 if ((p = alloc(len + 2)) == NULL)
1385 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001386 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001387
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001388 if (channel->ch_write_text_mode)
1389 p[len] = CAR;
1390 else
1391 {
1392 for (i = 0; i < len; ++i)
1393 if (p[i] == NL)
1394 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001395
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001396 p[len] = NL;
1397 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001398 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001399 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001400 vim_free(p);
1401}
1402
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001403/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001404 * Return TRUE if "channel" can be written to.
1405 * Returns FALSE if the input is closed or the write would block.
1406 */
1407 static int
1408can_write_buf_line(channel_T *channel)
1409{
1410 chanpart_T *in_part = &channel->ch_part[PART_IN];
1411
1412 if (in_part->ch_fd == INVALID_FD)
1413 return FALSE; /* pipe was closed */
1414
1415 /* for testing: block every other attempt to write */
1416 if (in_part->ch_block_write == 1)
1417 in_part->ch_block_write = -1;
1418 else if (in_part->ch_block_write == -1)
1419 in_part->ch_block_write = 1;
1420
1421 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001422#ifndef MSWIN
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001423 {
1424# if defined(HAVE_SELECT)
1425 struct timeval tval;
1426 fd_set wfds;
1427 int ret;
1428
1429 FD_ZERO(&wfds);
1430 FD_SET((int)in_part->ch_fd, &wfds);
1431 tval.tv_sec = 0;
1432 tval.tv_usec = 0;
1433 for (;;)
1434 {
1435 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1436# ifdef EINTR
1437 SOCK_ERRNO;
1438 if (ret == -1 && errno == EINTR)
1439 continue;
1440# endif
1441 if (ret <= 0 || in_part->ch_block_write == 1)
1442 {
1443 if (ret > 0)
1444 ch_log(channel, "FAKED Input not ready for writing");
1445 else
1446 ch_log(channel, "Input not ready for writing");
1447 return FALSE;
1448 }
1449 break;
1450 }
1451# else
1452 struct pollfd fds;
1453
1454 fds.fd = in_part->ch_fd;
1455 fds.events = POLLOUT;
1456 if (poll(&fds, 1, 0) <= 0)
1457 {
1458 ch_log(channel, "Input not ready for writing");
1459 return FALSE;
1460 }
1461 if (in_part->ch_block_write == 1)
1462 {
1463 ch_log(channel, "FAKED Input not ready for writing");
1464 return FALSE;
1465 }
1466# endif
1467 }
1468#endif
1469 return TRUE;
1470}
1471
1472/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001473 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001474 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001475 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001476channel_write_in(channel_T *channel)
1477{
1478 chanpart_T *in_part = &channel->ch_part[PART_IN];
1479 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001480 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001481 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001482
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001483 if (buf == NULL || in_part->ch_buf_append)
1484 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001485 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001486 {
1487 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001488 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001489 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001490 return;
1491 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001492
1493 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1494 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1495 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001496 if (!can_write_buf_line(channel))
1497 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001498 write_buf_line(buf, lnum, channel);
1499 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001500 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001501
1502 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001503 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001504 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001505 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001506
Bram Moolenaar014069a2016-03-03 22:51:40 +01001507 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001508 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001509 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001510#if defined(FEAT_TERMINAL)
1511 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001512 if (channel->ch_job != NULL)
1513 term_send_eof(channel);
1514#endif
1515
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001516 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001517 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001518 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001519
1520 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001521 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001522 }
1523 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001524 ch_log(channel, "Still %ld more lines to write",
1525 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001526}
1527
1528/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001529 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001530 */
1531 void
1532channel_buffer_free(buf_T *buf)
1533{
1534 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001535 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001536
1537 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001538 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001539 {
1540 chanpart_T *ch_part = &channel->ch_part[part];
1541
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001542 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001543 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001544 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001545 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001546 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001547 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001548 }
1549}
1550
1551/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001552 * Write any lines waiting to be written to "channel".
1553 */
1554 static void
1555channel_write_input(channel_T *channel)
1556{
1557 chanpart_T *in_part = &channel->ch_part[PART_IN];
1558
1559 if (in_part->ch_writeque.wq_next != NULL)
1560 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1561 else if (in_part->ch_bufref.br_buf != NULL)
1562 {
1563 if (in_part->ch_buf_append)
1564 channel_write_new_lines(in_part->ch_bufref.br_buf);
1565 else
1566 channel_write_in(channel);
1567 }
1568}
1569
1570/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001571 * Write any lines waiting to be written to a channel.
1572 */
1573 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001574channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001575{
1576 channel_T *channel;
1577
1578 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001579 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001580}
1581
1582/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001583 * Write appended lines above the last one in "buf" to the channel.
1584 */
1585 void
1586channel_write_new_lines(buf_T *buf)
1587{
1588 channel_T *channel;
1589 int found_one = FALSE;
1590
1591 /* There could be more than one channel for the buffer, loop over all of
1592 * them. */
1593 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1594 {
1595 chanpart_T *in_part = &channel->ch_part[PART_IN];
1596 linenr_T lnum;
1597 int written = 0;
1598
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001599 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001600 {
1601 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001602 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001603 found_one = TRUE;
1604 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1605 ++lnum)
1606 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001607 if (!can_write_buf_line(channel))
1608 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001609 write_buf_line(buf, lnum, channel);
1610 ++written;
1611 }
1612
1613 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001614 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001615 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001616 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001617 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001618 ch_log(channel, "Still %ld more lines to write",
1619 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001620
1621 in_part->ch_buf_bot = lnum;
1622 }
1623 }
1624 if (!found_one)
1625 buf->b_write_to_channel = FALSE;
1626}
1627
1628/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001629 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001630 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001631 */
1632 static void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001633invoke_callback(channel_T *channel, callback_T *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001634{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001635 typval_T rettv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001636
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001637 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001638 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001639
Bram Moolenaar77073442016-02-13 23:23:53 +01001640 argv[0].v_type = VAR_CHANNEL;
1641 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001642
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001643 call_callback(callback, -1, &rettv, 2, argv);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001644 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001645 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001646}
1647
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001648/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001649 * Return the first node from "channel"/"part" without removing it.
1650 * Returns NULL if there is nothing.
1651 */
1652 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001653channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001654{
1655 readq_T *head = &channel->ch_part[part].ch_head;
1656
1657 return head->rq_next;
1658}
1659
1660/*
1661 * Return a pointer to the first NL in "node".
1662 * Skips over NUL characters.
1663 * Returns NULL if there is no NL.
1664 */
1665 char_u *
1666channel_first_nl(readq_T *node)
1667{
1668 char_u *buffer = node->rq_buffer;
1669 long_u i;
1670
1671 for (i = 0; i < node->rq_buflen; ++i)
1672 if (buffer[i] == NL)
1673 return buffer + i;
1674 return NULL;
1675}
1676
1677/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001678 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001679 * The caller must free it.
1680 * Returns NULL if there is nothing.
1681 */
1682 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001683channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001684{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001685 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001686 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001687 char_u *p;
1688
Bram Moolenaar77073442016-02-13 23:23:53 +01001689 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001690 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001691 if (outlen != NULL)
1692 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001693 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001694 p = node->rq_buffer;
1695 head->rq_next = node->rq_next;
1696 if (node->rq_next == NULL)
1697 head->rq_prev = NULL;
1698 else
1699 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001700 vim_free(node);
1701 return p;
1702}
1703
1704/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001705 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001706 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001707 */
1708 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001709channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001710{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001711 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001712 readq_T *node;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001713 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001714 char_u *res;
1715 char_u *p;
1716
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001717 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001718 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001719 len += node->rq_buflen;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001720 res = alloc(len + 1);
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
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001731 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001732 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 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001740 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001741 *outlen += len;
1742 return res;
1743 }
1744
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001745 // Turn all NUL into NL, so that the result can be used as a string.
1746 p = res;
1747 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001748 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001749 if (*p == NUL)
1750 *p = NL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001751#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001752 else if (*p == 0x1b)
1753 {
1754 // crush the escape sequence OSC 0/1/2: ESC ]0;
1755 if (p + 3 < res + len
1756 && p[1] == ']'
1757 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1758 && p[3] == ';')
1759 {
1760 // '\a' becomes a NL
1761 while (p < res + (len - 1) && *p != '\a')
1762 ++p;
1763 // BEL is zero width characters, suppress display mistake
1764 // ConPTY (after 10.0.18317) requires advance checking
1765 if (p[-1] == NUL)
1766 p[-1] = 0x07;
1767 }
1768 }
1769#endif
1770 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001771 }
1772
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001773 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001774}
1775
1776/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001777 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001778 * Caller must check these bytes are available.
1779 */
1780 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001781channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001782{
1783 readq_T *head = &channel->ch_part[part].ch_head;
1784 readq_T *node = head->rq_next;
1785 char_u *buf = node->rq_buffer;
1786
1787 mch_memmove(buf, buf + len, node->rq_buflen - len);
1788 node->rq_buflen -= len;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001789 node->rq_buffer[node->rq_buflen] = NUL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001790}
1791
1792/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001793 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001794 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001795 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 */
1797 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001798channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001799{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001800 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001801 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001802 readq_T *last_node;
1803 readq_T *n;
1804 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001805 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001806 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001807
Bram Moolenaar77073442016-02-13 23:23:53 +01001808 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001809 return FAIL;
1810
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001811 last_node = node->rq_next;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001812 len = node->rq_buflen + last_node->rq_buflen;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001813 if (want_nl)
1814 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001815 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001816 {
1817 last_node = last_node->rq_next;
1818 len += last_node->rq_buflen;
1819 }
1820
Bram Moolenaar772153f2019-03-04 12:09:49 +01001821 p = newbuf = alloc(len + 1);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001822 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001823 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001824 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001825 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001826 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001827 node->rq_buffer = newbuf;
1828 for (n = node; n != last_node; )
1829 {
1830 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001831 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001832 p += n->rq_buflen;
1833 vim_free(n->rq_buffer);
1834 }
Bram Moolenaar772153f2019-03-04 12:09:49 +01001835 *p = NUL;
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001836 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001837
1838 /* dispose of the collapsed nodes and their buffers */
1839 for (n = node->rq_next; n != last_node; )
1840 {
1841 n = n->rq_next;
1842 vim_free(n->rq_prev);
1843 }
1844 node->rq_next = last_node->rq_next;
1845 if (last_node->rq_next == NULL)
1846 head->rq_prev = node;
1847 else
1848 last_node->rq_next->rq_prev = node;
1849 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001850 return OK;
1851}
1852
1853/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001854 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001855 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001856 * Returns OK or FAIL.
1857 */
1858 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001859channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001860 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001861{
1862 readq_T *node;
1863 readq_T *head = &channel->ch_part[part].ch_head;
1864 char_u *p;
1865 int i;
1866
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001867 node = ALLOC_ONE(readq_T);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001868 if (node == NULL)
1869 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001870 /* A NUL is added at the end, because netbeans code expects that.
1871 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001872 node->rq_buffer = alloc(len + 1);
1873 if (node->rq_buffer == NULL)
1874 {
1875 vim_free(node);
1876 return FAIL; /* out of memory */
1877 }
1878
1879 if (channel->ch_part[part].ch_mode == MODE_NL)
1880 {
1881 /* Drop any CR before a NL. */
1882 p = node->rq_buffer;
1883 for (i = 0; i < len; ++i)
1884 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1885 *p++ = buf[i];
1886 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001887 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001888 }
1889 else
1890 {
1891 mch_memmove(node->rq_buffer, buf, len);
1892 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001893 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001894 }
1895
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001896 if (prepend)
1897 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001898 // prepend node to the head of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001899 node->rq_next = head->rq_next;
1900 node->rq_prev = NULL;
1901 if (head->rq_next == NULL)
1902 head->rq_prev = node;
1903 else
1904 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001905 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001906 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001907 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001908 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001909 // append node to the tail of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 node->rq_next = NULL;
1911 node->rq_prev = head->rq_prev;
1912 if (head->rq_prev == NULL)
1913 head->rq_next = node;
1914 else
1915 head->rq_prev->rq_next = node;
1916 head->rq_prev = node;
1917 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001918
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001919 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001920 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001921 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001922 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001923 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001924 fprintf(log_fd, "'\n");
1925 }
1926 return OK;
1927}
1928
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001929/*
1930 * Try to fill the buffer of "reader".
1931 * Returns FALSE when nothing was added.
1932 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001933 static int
1934channel_fill(js_read_T *reader)
1935{
1936 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001937 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001938 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001939 int keeplen;
1940 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001941 char_u *p;
1942
1943 if (next == NULL)
1944 return FALSE;
1945
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001946 keeplen = reader->js_end - reader->js_buf;
1947 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001948 {
1949 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001950 addlen = (int)STRLEN(next);
1951 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001952 if (p == NULL)
1953 {
1954 vim_free(next);
1955 return FALSE;
1956 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001957 mch_memmove(p, reader->js_buf, keeplen);
1958 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001959 vim_free(next);
1960 next = p;
1961 }
1962
1963 vim_free(reader->js_buf);
1964 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001965 return TRUE;
1966}
1967
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001968/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001969 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001970 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001971 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001972 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001973 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001974channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001975{
1976 js_read_T reader;
1977 typval_T listtv;
1978 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001979 chanpart_T *chanpart = &channel->ch_part[part];
1980 jsonq_T *head = &chanpart->ch_json_head;
1981 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001982 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001983
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001984 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001985 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001986
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001987 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001988 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001989 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001990 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001991 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001992
1993 /* When a message is incomplete we wait for a short while for more to
1994 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001995 * or list will make us hang.
1996 * Do not generate error messages, they will be written in a channel log. */
1997 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001998 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001999 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002000 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002001 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002002 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002003 /* Only accept the response when it is a list with at least two
2004 * items. */
2005 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002006 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002007 if (listtv.v_type != VAR_LIST)
2008 ch_error(channel, "Did not receive a list, discarding");
2009 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002010 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002011 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002012 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002013 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002014 else
2015 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002016 item = ALLOC_ONE(jsonq_T);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002017 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002018 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002019 else
2020 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002021 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002022 item->jq_value = alloc_tv();
2023 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002024 {
2025 vim_free(item);
2026 clear_tv(&listtv);
2027 }
2028 else
2029 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002030 *item->jq_value = listtv;
2031 item->jq_prev = head->jq_prev;
2032 head->jq_prev = item;
2033 item->jq_next = NULL;
2034 if (item->jq_prev == NULL)
2035 head->jq_next = item;
2036 else
2037 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002038 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002039 }
2040 }
2041 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002042
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002043 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002044 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002045 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002046 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002047 size_t buflen = STRLEN(reader.js_buf);
2048
2049 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002050 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002051 /* First time encountering incomplete message or after receiving
2052 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002053 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002054 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002055 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002056 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002057 chanpart->ch_wait_len = buflen;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002058#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002059 chanpart->ch_deadline = GetTickCount() + 100L;
2060#else
2061 gettimeofday(&chanpart->ch_deadline, NULL);
2062 chanpart->ch_deadline.tv_usec += 100 * 1000;
2063 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2064 {
2065 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2066 ++chanpart->ch_deadline.tv_sec;
2067 }
2068#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002069 }
2070 else
2071 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002072 int timeout;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002073#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002074 timeout = GetTickCount() > chanpart->ch_deadline;
2075#else
2076 {
2077 struct timeval now_tv;
2078
2079 gettimeofday(&now_tv, NULL);
2080 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2081 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2082 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2083 }
2084#endif
2085 if (timeout)
2086 {
2087 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002088 chanpart->ch_wait_len = 0;
2089 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002090 }
2091 else
2092 {
2093 reader.js_used = 0;
2094 ch_log(channel, "still waiting on incomplete message");
2095 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002096 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002097 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002098
2099 if (status == FAIL)
2100 {
2101 ch_error(channel, "Decoding failed - discarding input");
2102 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002103 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002104 }
2105 else if (reader.js_buf[reader.js_used] != NUL)
2106 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002107 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002108 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002109 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2110 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002111 ret = status == MAYBE ? FALSE: TRUE;
2112 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002113 else
2114 ret = FALSE;
2115
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002116 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002117 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002118}
2119
2120/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002121 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002122 */
2123 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002124remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002125{
Bram Moolenaar77073442016-02-13 23:23:53 +01002126 if (node->cq_prev == NULL)
2127 head->cq_next = node->cq_next;
2128 else
2129 node->cq_prev->cq_next = node->cq_next;
2130 if (node->cq_next == NULL)
2131 head->cq_prev = node->cq_prev;
2132 else
2133 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002134}
2135
2136/*
2137 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002138 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002139 */
2140 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002141remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002142{
Bram Moolenaar77073442016-02-13 23:23:53 +01002143 if (node->jq_prev == NULL)
2144 head->jq_next = node->jq_next;
2145 else
2146 node->jq_prev->jq_next = node->jq_next;
2147 if (node->jq_next == NULL)
2148 head->jq_prev = node->jq_prev;
2149 else
2150 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002151 vim_free(node);
2152}
2153
2154/*
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002155 * Add "id" to the list of JSON message IDs we are waiting on.
2156 */
2157 static void
2158channel_add_block_id(chanpart_T *chanpart, int id)
2159{
2160 garray_T *gap = &chanpart->ch_block_ids;
2161
2162 if (gap->ga_growsize == 0)
2163 ga_init2(gap, (int)sizeof(int), 10);
2164 if (ga_grow(gap, 1) == OK)
2165 {
2166 ((int *)gap->ga_data)[gap->ga_len] = id;
2167 ++gap->ga_len;
2168 }
2169}
2170
2171/*
2172 * Remove "id" from the list of JSON message IDs we are waiting on.
2173 */
2174 static void
2175channel_remove_block_id(chanpart_T *chanpart, int id)
2176{
2177 garray_T *gap = &chanpart->ch_block_ids;
2178 int i;
2179
2180 for (i = 0; i < gap->ga_len; ++i)
2181 if (((int *)gap->ga_data)[i] == id)
2182 {
2183 --gap->ga_len;
2184 if (i < gap->ga_len)
2185 {
2186 int *p = ((int *)gap->ga_data) + i;
2187
2188 mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int));
2189 }
2190 return;
2191 }
2192 siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id);
2193}
2194
2195/*
2196 * Return TRUE if "id" is in the list of JSON message IDs we are waiting on.
2197 */
2198 static int
2199channel_has_block_id(chanpart_T *chanpart, int id)
2200{
2201 garray_T *gap = &chanpart->ch_block_ids;
2202 int i;
2203
2204 for (i = 0; i < gap->ga_len; ++i)
2205 if (((int *)gap->ga_data)[i] == id)
2206 return TRUE;
2207 return FALSE;
2208}
2209
2210/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002211 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002212 * When "id" is positive it must match the first number in the list.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002213 * When "id" is zero or negative jut get the first message. But not one
2214 * in the ch_block_ids list.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002215 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002216 * Return OK when found and return the value in "rettv".
2217 * Return FAIL otherwise.
2218 */
2219 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002220channel_get_json(
2221 channel_T *channel,
2222 ch_part_T part,
2223 int id,
2224 int without_callback,
2225 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002226{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002227 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002228 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002229
Bram Moolenaar77073442016-02-13 23:23:53 +01002230 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002231 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002232 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002233 typval_T *tv = &l->lv_first->li_tv;
2234
Bram Moolenaar958dc692016-12-01 15:34:12 +01002235 if ((without_callback || !item->jq_no_callback)
2236 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002237 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002238 || tv->vval.v_number == 0
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002239 || !channel_has_block_id(
2240 &channel->ch_part[part], tv->vval.v_number)))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002241 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002242 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002243 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002244 ch_log(channel, "Getting JSON message %ld",
2245 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002246 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002247 return OK;
2248 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002249 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002250 }
2251 return FAIL;
2252}
2253
Bram Moolenaar958dc692016-12-01 15:34:12 +01002254/*
2255 * Put back "rettv" into the JSON queue, there was no callback for it.
2256 * Takes over the values in "rettv".
2257 */
2258 static void
2259channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2260{
2261 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2262 jsonq_T *item = head->jq_next;
2263 jsonq_T *newitem;
2264
2265 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2266 /* last item was pushed back, append to the end */
2267 item = NULL;
2268 else while (item != NULL && item->jq_no_callback)
2269 /* append after the last item that was pushed back */
2270 item = item->jq_next;
2271
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002272 newitem = ALLOC_ONE(jsonq_T);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002273 if (newitem == NULL)
2274 clear_tv(rettv);
2275 else
2276 {
2277 newitem->jq_value = alloc_tv();
2278 if (newitem->jq_value == NULL)
2279 {
2280 vim_free(newitem);
2281 clear_tv(rettv);
2282 }
2283 else
2284 {
2285 newitem->jq_no_callback = FALSE;
2286 *newitem->jq_value = *rettv;
2287 if (item == NULL)
2288 {
2289 /* append to the end */
2290 newitem->jq_prev = head->jq_prev;
2291 head->jq_prev = newitem;
2292 newitem->jq_next = NULL;
2293 if (newitem->jq_prev == NULL)
2294 head->jq_next = newitem;
2295 else
2296 newitem->jq_prev->jq_next = newitem;
2297 }
2298 else
2299 {
2300 /* append after "item" */
2301 newitem->jq_prev = item;
2302 newitem->jq_next = item->jq_next;
2303 item->jq_next = newitem;
2304 if (newitem->jq_next == NULL)
2305 head->jq_prev = newitem;
2306 else
2307 newitem->jq_next->jq_prev = newitem;
2308 }
2309 }
2310 }
2311}
2312
Bram Moolenaarece61b02016-02-20 21:39:05 +01002313#define CH_JSON_MAX_ARGS 4
2314
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002315/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002316 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002317 * "argv[0]" is the command string.
2318 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002319 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002320 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002321channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002322{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002323 char_u *cmd = argv[0].vval.v_string;
2324 char_u *arg;
2325 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002326
Bram Moolenaarece61b02016-02-20 21:39:05 +01002327 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002328 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002329 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002330 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002332 return;
2333 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002334 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002335 if (arg == NULL)
2336 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002337
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002338 if (STRCMP(cmd, "ex") == 0)
2339 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002340 int save_called_emsg = called_emsg;
2341
2342 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002343 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002344 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002345 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002346 --emsg_silent;
2347 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002348 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002349 (char *)get_vim_var_str(VV_ERRMSG));
2350 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002351 }
2352 else if (STRCMP(cmd, "normal") == 0)
2353 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002354 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002355
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002356 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002357 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002358 ea.arg = arg;
2359 ea.addr_count = 0;
2360 ea.forceit = TRUE; /* no mapping */
2361 ex_normal(&ea);
2362 }
2363 else if (STRCMP(cmd, "redraw") == 0)
2364 {
2365 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002366
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002367 ch_log(channel, "redraw");
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002368 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002369 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002370 ex_redraw(&ea);
2371 showruler(FALSE);
2372 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002373 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002374 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002375 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002376 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002377 int is_call = cmd[0] == 'c';
2378 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002379
Bram Moolenaarece61b02016-02-20 21:39:05 +01002380 if (argv[id_idx].v_type != VAR_UNKNOWN
2381 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002382 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002383 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002384 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002385 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002386 }
2387 else if (is_call && argv[2].v_type != VAR_LIST)
2388 {
2389 ch_error(channel, "third argument for call must be a list");
2390 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002391 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002392 }
2393 else
2394 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002395 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002396 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002397 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002398 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002399
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002400 /* Don't pollute the display with errors. */
2401 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002402 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002403 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002404 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002405 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002406 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002407 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002408 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002409 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002410 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2411 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002412 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002413
2414 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002415 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002416 int id = argv[id_idx].vval.v_number;
2417
Bram Moolenaar55fab432016-02-07 16:53:13 +01002418 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002419 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002420 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002421 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002422 /* If evaluation failed or the result can't be encoded
2423 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002424 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002425 err_tv.v_type = VAR_STRING;
2426 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002427 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002428 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002429 if (json != NULL)
2430 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002431 channel_send(channel,
2432 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002433 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002434 vim_free(json);
2435 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002436 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002437 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002438 if (tv == &res_tv)
2439 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002440 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002441 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002442 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002443 }
2444 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002445 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002446 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002447 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002448 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002449}
2450
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002451/*
2452 * Invoke the callback at "cbhead".
2453 * Does not redraw but sets channel_need_redraw.
2454 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002455 static void
2456invoke_one_time_callback(
2457 channel_T *channel,
2458 cbq_T *cbhead,
2459 cbq_T *item,
2460 typval_T *argv)
2461{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002462 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002463 (char *)item->cq_callback.cb_name);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002464 /* Remove the item from the list first, if the callback
2465 * invokes ch_close() the list will be cleared. */
2466 remove_cb_node(cbhead, item);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002467 invoke_callback(channel, &item->cq_callback, argv);
2468 free_callback(&item->cq_callback);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002469 vim_free(item);
2470}
2471
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002472 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002473append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002474{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002475 bufref_T save_curbuf = {NULL, 0, 0};
2476 win_T *save_curwin = NULL;
2477 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002478 linenr_T lnum = buffer->b_ml.ml_line_count;
2479 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002480 chanpart_T *ch_part = &channel->ch_part[part];
2481 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002482 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002483
2484 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2485 {
2486 if (!ch_part->ch_nomod_error)
2487 {
2488 ch_error(channel, "Buffer is not modifiable, cannot append");
2489 ch_part->ch_nomod_error = TRUE;
2490 }
2491 return;
2492 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002493
2494 /* If the buffer is also used as input insert above the last
2495 * line. Don't write these lines. */
2496 if (save_write_to)
2497 {
2498 --lnum;
2499 buffer->b_write_to_channel = FALSE;
2500 }
2501
2502 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002503 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002504
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002505 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002506
2507 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2508 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2509
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002510 u_sync(TRUE);
2511 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002512 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002513
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002514 if (empty)
2515 {
2516 /* The buffer is empty, replace the first (dummy) line. */
2517 ml_replace(lnum, msg, TRUE);
2518 lnum = 0;
2519 }
2520 else
2521 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002522 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002523
2524 /* Restore curbuf/curwin/curtab */
2525 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2526
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002527 if (ch_part->ch_nomodifiable)
2528 buffer->b_p_ma = FALSE;
2529 else
2530 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002531
2532 if (buffer->b_nwindows > 0)
2533 {
2534 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002535
2536 FOR_ALL_WINDOWS(wp)
2537 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002538 if (wp->w_buffer == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002539 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002540 int move_cursor = save_write_to
2541 ? wp->w_cursor.lnum == lnum + 1
2542 : (wp->w_cursor.lnum == lnum
2543 && wp->w_cursor.col == 0);
2544
2545 // If the cursor is at or above the new line, move it one line
2546 // down. If the topline is outdated update it now.
2547 if (move_cursor || wp->w_topline > buffer->b_ml.ml_line_count)
2548 {
2549 if (move_cursor)
2550 ++wp->w_cursor.lnum;
2551 save_curwin = curwin;
2552 curwin = wp;
2553 curbuf = curwin->w_buffer;
2554 scroll_cursor_bot(0, FALSE);
2555 curwin = save_curwin;
2556 curbuf = curwin->w_buffer;
2557 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002558 }
2559 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002560 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002561 channel_need_redraw = TRUE;
2562 }
2563
2564 if (save_write_to)
2565 {
2566 channel_T *ch;
2567
2568 /* Find channels reading from this buffer and adjust their
2569 * next-to-read line number. */
2570 buffer->b_write_to_channel = TRUE;
2571 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2572 {
2573 chanpart_T *in_part = &ch->ch_part[PART_IN];
2574
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002575 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002576 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2577 }
2578 }
2579}
2580
Bram Moolenaar437905c2016-04-26 19:01:05 +02002581 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002582drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002583{
2584 char_u *msg;
2585
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002586 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002587 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002588 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002589 vim_free(msg);
2590 }
2591}
2592
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002593/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002594 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002595 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002596 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002597 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002598 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002599may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002600{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002601 char_u *msg = NULL;
2602 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002603 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002604 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002605 chanpart_T *ch_part = &channel->ch_part[part];
2606 ch_mode_T ch_mode = ch_part->ch_mode;
2607 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002608 cbq_T *cbitem;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002609 callback_T *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002610 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002611 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002612
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002613 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002614 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002615 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002616
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002617 /* Use a message-specific callback, part callback or channel callback */
2618 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2619 if (cbitem->cq_seq_nr == 0)
2620 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002621 if (cbitem != NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002622 callback = &cbitem->cq_callback;
2623 else if (ch_part->ch_callback.cb_name != NULL)
2624 callback = &ch_part->ch_callback;
2625 else if (channel->ch_callback.cb_name != NULL)
2626 callback = &channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002627
Bram Moolenaarec68a992016-10-03 21:37:41 +02002628 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002629 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2630 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002631 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002632 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002633 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002634 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002635 buffer = NULL;
2636 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002637
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002638 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002639 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002640 listitem_T *item;
2641 int argc = 0;
2642
Bram Moolenaard7ece102016-02-02 23:23:02 +01002643 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002644 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002645 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002646 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002647 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002648 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002649 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002650 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002651
Bram Moolenaarece61b02016-02-20 21:39:05 +01002652 for (item = listtv->vval.v_list->lv_first;
2653 item != NULL && argc < CH_JSON_MAX_ARGS;
2654 item = item->li_next)
2655 argv[argc++] = item->li_tv;
2656 while (argc < CH_JSON_MAX_ARGS)
2657 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002658
Bram Moolenaarece61b02016-02-20 21:39:05 +01002659 if (argv[0].v_type == VAR_STRING)
2660 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002661 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002662 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002663 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002664 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002665 }
2666
Bram Moolenaarece61b02016-02-20 21:39:05 +01002667 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002668 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002669 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002670 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002671 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002672 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002673 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002674 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002675 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002676 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002677 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002678 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002679 return FALSE;
2680 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002681 else
2682 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002683 /* If there is no callback or buffer drop the message. */
2684 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002685 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002686 /* If there is a close callback it may use ch_read() to get the
2687 * messages. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002688 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002689 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002690 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002691 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002692
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002693 if (ch_mode == MODE_NL)
2694 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002695 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002696 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002697 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002698
2699 /* See if we have a message ending in NL in the first buffer. If
2700 * not try to concatenate the first and the second buffer. */
2701 while (TRUE)
2702 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002703 node = channel_peek(channel, part);
2704 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002705 if (nl != NULL)
2706 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002707 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002708 {
2709 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2710 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002711 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002712 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002713 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002714 buf = node->rq_buffer;
2715
Bram Moolenaar772153f2019-03-04 12:09:49 +01002716 // Convert NUL to NL, the internal representation.
2717 for (p = buf; (nl == NULL || p < nl)
2718 && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002719 if (*p == NUL)
2720 *p = NL;
2721
Bram Moolenaar772153f2019-03-04 12:09:49 +01002722 if (nl == NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002723 {
Bram Moolenaar772153f2019-03-04 12:09:49 +01002724 // get the whole buffer, drop the NL
2725 msg = channel_get(channel, part, NULL);
2726 }
2727 else if (nl + 1 == buf + node->rq_buflen)
2728 {
2729 // get the whole buffer
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002730 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002731 *nl = NUL;
2732 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002733 else
2734 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002735 /* Copy the message into allocated memory (excluding the NL)
2736 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002737 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002738 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002739 }
2740 }
2741 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002742 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002743 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002744 * get everything we have.
2745 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002746 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002747 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002748
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002749 if (msg == NULL)
2750 return FALSE; /* out of memory (and avoids Coverity warning) */
2751
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002752 argv[1].v_type = VAR_STRING;
2753 argv[1].vval.v_string = msg;
2754 }
2755
Bram Moolenaara07fec92016-02-05 21:04:08 +01002756 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002757 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002758 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002759
Bram Moolenaar958dc692016-12-01 15:34:12 +01002760 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002761 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002762 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002763 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002764 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002765 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002766 break;
2767 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002768 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002769 {
2770 if (channel->ch_drop_never)
2771 {
2772 /* message must be read with ch_read() */
2773 channel_push_json(channel, part, listtv);
2774 listtv = NULL;
2775 }
2776 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002777 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002778 seq_nr);
2779 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002780 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002781 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002782 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002783 if (buffer != NULL)
2784 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002785 if (msg == NULL)
2786 /* JSON or JS mode: re-encode the message. */
2787 msg = json_encode(listtv, ch_mode);
2788 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002789 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002790#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002791 if (buffer->b_term != NULL)
2792 write_to_term(buffer, msg, channel);
2793 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002794#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002795 append_to_buffer(buffer, msg, channel, part);
2796 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002797 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002798
Bram Moolenaar187db502016-02-27 14:44:26 +01002799 if (callback != NULL)
2800 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002801 if (cbitem != NULL)
2802 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2803 else
2804 {
2805 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002806 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002807 (char *)callback->cb_name);
2808 invoke_callback(channel, callback, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002809 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002810 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002811 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002812 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002813 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002814
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002815 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002816 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002817 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002818
2819 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002820}
2821
Bram Moolenaar113e1072019-01-20 15:30:40 +01002822#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002823/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002824 * Return TRUE when channel "channel" is open for writing to.
2825 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002826 */
2827 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002828channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002829{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002830 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002831 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002832}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002833#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002834
2835/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002836 * Return TRUE when channel "channel" is open for reading or writing.
2837 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002838 */
2839 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002840channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002841{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002842 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002843 || channel->CH_IN_FD != INVALID_FD
2844 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002845 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002846}
2847
2848/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002849 * Return TRUE if "channel" has JSON or other typeahead.
2850 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002851 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002852channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002853{
2854 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2855
2856 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2857 {
2858 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002859
Bram Moolenaar4340fc92019-06-28 22:06:49 +02002860 if (head->jq_next == NULL)
2861 // Parse json from readahead, there might be a complete message to
2862 // process.
2863 channel_parse_json(channel, part);
2864
2865 return head->jq_next != NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002866 }
2867 return channel_peek(channel, part) != NULL;
2868}
2869
2870/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002871 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002872 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002873 */
2874 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002875channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002876{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002877 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002878 int has_readahead = FALSE;
2879
Bram Moolenaar77073442016-02-13 23:23:53 +01002880 if (channel == NULL)
2881 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002882 if (req_part == PART_OUT)
2883 {
2884 if (channel->CH_OUT_FD != INVALID_FD)
2885 return "open";
2886 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002887 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002888 }
2889 else if (req_part == PART_ERR)
2890 {
2891 if (channel->CH_ERR_FD != INVALID_FD)
2892 return "open";
2893 if (channel_has_readahead(channel, PART_ERR))
2894 has_readahead = TRUE;
2895 }
2896 else
2897 {
2898 if (channel_is_open(channel))
2899 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002900 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002901 if (channel_has_readahead(channel, part))
2902 {
2903 has_readahead = TRUE;
2904 break;
2905 }
2906 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002907
2908 if (has_readahead)
2909 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002910 return "closed";
2911}
2912
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002913 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002914channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002915{
2916 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002917 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002918 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002919 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002920 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002921
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002922 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002923 STRCAT(namebuf, "_");
2924 tail = STRLEN(namebuf);
2925
2926 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002927 if (chanpart->ch_fd != INVALID_FD)
2928 status = "open";
2929 else if (channel_has_readahead(channel, part))
2930 status = "buffered";
2931 else
2932 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002933 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002934
2935 STRCPY(namebuf + tail, "mode");
2936 switch (chanpart->ch_mode)
2937 {
2938 case MODE_NL: s = "NL"; break;
2939 case MODE_RAW: s = "RAW"; break;
2940 case MODE_JSON: s = "JSON"; break;
2941 case MODE_JS: s = "JS"; break;
2942 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002943 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002944
2945 STRCPY(namebuf + tail, "io");
2946 if (part == PART_SOCK)
2947 s = "socket";
2948 else switch (chanpart->ch_io)
2949 {
2950 case JIO_NULL: s = "null"; break;
2951 case JIO_PIPE: s = "pipe"; break;
2952 case JIO_FILE: s = "file"; break;
2953 case JIO_BUFFER: s = "buffer"; break;
2954 case JIO_OUT: s = "out"; break;
2955 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002956 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002957
2958 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002959 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002960}
2961
2962 void
2963channel_info(channel_T *channel, dict_T *dict)
2964{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002965 dict_add_number(dict, "id", channel->ch_id);
2966 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002967
2968 if (channel->ch_hostname != NULL)
2969 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002970 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2971 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002972 channel_part_info(channel, dict, "sock", PART_SOCK);
2973 }
2974 else
2975 {
2976 channel_part_info(channel, dict, "out", PART_OUT);
2977 channel_part_info(channel, dict, "err", PART_ERR);
2978 channel_part_info(channel, dict, "in", PART_IN);
2979 }
2980}
2981
Bram Moolenaar77073442016-02-13 23:23:53 +01002982/*
2983 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002984 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002985 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002986 */
2987 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002988channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002989{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002990 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002991
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002992#ifdef FEAT_GUI
2993 channel_gui_unregister(channel);
2994#endif
2995
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002996 ch_close_part(channel, PART_SOCK);
2997 ch_close_part(channel, PART_IN);
2998 ch_close_part(channel, PART_OUT);
2999 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003000
Bram Moolenaara9f02812017-08-05 17:40:38 +02003001 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003002 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02003003 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003004
Bram Moolenaara9f02812017-08-05 17:40:38 +02003005 /* Invoke callbacks and flush buffers before the close callback. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003006 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003007 ch_log(channel,
3008 "Invoking callbacks and flushing buffers before closing");
3009 for (part = PART_SOCK; part < PART_IN; ++part)
3010 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003011 if (channel->ch_close_cb.cb_name != NULL
Bram Moolenaara9f02812017-08-05 17:40:38 +02003012 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3013 {
3014 /* Increment the refcount to avoid the channel being freed
3015 * halfway. */
3016 ++channel->ch_refcount;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003017 if (channel->ch_close_cb.cb_name == NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003018 ch_log(channel, "flushing %s buffers before closing",
3019 part_names[part]);
3020 while (may_invoke_callback(channel, part))
3021 ;
3022 --channel->ch_refcount;
3023 }
3024 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003025
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003026 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003027 {
3028 typval_T argv[1];
3029 typval_T rettv;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003030
3031 /* Increment the refcount to avoid the channel being freed
3032 * halfway. */
3033 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003034 ch_log(channel, "Invoking close callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003035 (char *)channel->ch_close_cb.cb_name);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003036 argv[0].v_type = VAR_CHANNEL;
3037 argv[0].vval.v_channel = channel;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003038 call_callback(&channel->ch_close_cb, -1, &rettv, 1, argv);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003039 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003040 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003041
Bram Moolenaara9f02812017-08-05 17:40:38 +02003042 /* the callback is only called once */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003043 free_callback(&channel->ch_close_cb);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003044
Bram Moolenaara9f02812017-08-05 17:40:38 +02003045 if (channel_need_redraw)
3046 {
3047 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003048 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003049 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003050
Bram Moolenaara9f02812017-08-05 17:40:38 +02003051 if (!channel->ch_drop_never)
3052 /* any remaining messages are useless now */
3053 for (part = PART_SOCK; part < PART_IN; ++part)
3054 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003055
3056 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003057 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003058 }
3059
3060 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003061
3062#ifdef FEAT_TERMINAL
3063 term_channel_closed(channel);
3064#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003065}
3066
Bram Moolenaard04a0202016-01-26 23:30:18 +01003067/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003068 * Close the "in" part channel "channel".
3069 */
3070 void
3071channel_close_in(channel_T *channel)
3072{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003073 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003074}
3075
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003076 static void
3077remove_from_writeque(writeq_T *wq, writeq_T *entry)
3078{
3079 ga_clear(&entry->wq_ga);
3080 wq->wq_next = entry->wq_next;
3081 if (wq->wq_next == NULL)
3082 wq->wq_prev = NULL;
3083 else
3084 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003085 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003086}
3087
Bram Moolenaar0874a832016-09-01 15:11:51 +02003088/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003089 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003091 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003092channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003093{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003094 chanpart_T *ch_part = &channel->ch_part[part];
3095 jsonq_T *json_head = &ch_part->ch_json_head;
3096 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003097
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003098 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003099 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003100
3101 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003102 {
3103 cbq_T *node = cb_head->cq_next;
3104
3105 remove_cb_node(cb_head, node);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003106 free_callback(&node->cq_callback);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003107 vim_free(node);
3108 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003109
3110 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003111 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003112 free_tv(json_head->jq_next->jq_value);
3113 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003114 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003115
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003116 free_callback(&ch_part->ch_callback);
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003117 ga_clear(&ch_part->ch_block_ids);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003118
3119 while (ch_part->ch_writeque.wq_next != NULL)
3120 remove_from_writeque(&ch_part->ch_writeque,
3121 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003122}
3123
3124/*
3125 * Clear all the read buffers on "channel".
3126 */
3127 void
3128channel_clear(channel_T *channel)
3129{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003130 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003131 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003132 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003133 channel_clear_one(channel, PART_OUT);
3134 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003135 channel_clear_one(channel, PART_IN);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003136 free_callback(&channel->ch_callback);
3137 free_callback(&channel->ch_close_cb);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003138}
3139
Bram Moolenaar77073442016-02-13 23:23:53 +01003140#if defined(EXITFREE) || defined(PROTO)
3141 void
3142channel_free_all(void)
3143{
3144 channel_T *channel;
3145
Bram Moolenaard6051b52016-02-28 15:49:03 +01003146 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003147 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3148 channel_clear(channel);
3149}
3150#endif
3151
3152
Bram Moolenaar715d2852016-04-30 17:06:31 +02003153/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003154#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003155
3156/* Buffer size for reading incoming messages. */
3157#define MAXMSGSIZE 4096
3158
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003159#if defined(HAVE_SELECT)
3160/*
3161 * Add write fds where we are waiting for writing to be possible.
3162 */
3163 static int
3164channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3165{
3166 int maxfd = maxfd_arg;
3167 channel_T *ch;
3168
3169 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3170 {
3171 chanpart_T *in_part = &ch->ch_part[PART_IN];
3172
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003173 if (in_part->ch_fd != INVALID_FD
3174 && (in_part->ch_bufref.br_buf != NULL
3175 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003176 {
3177 FD_SET((int)in_part->ch_fd, wfds);
3178 if ((int)in_part->ch_fd >= maxfd)
3179 maxfd = (int)in_part->ch_fd + 1;
3180 }
3181 }
3182 return maxfd;
3183}
3184#else
3185/*
3186 * Add write fds where we are waiting for writing to be possible.
3187 */
3188 static int
3189channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3190{
3191 int nfd = nfd_in;
3192 channel_T *ch;
3193
3194 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3195 {
3196 chanpart_T *in_part = &ch->ch_part[PART_IN];
3197
Bram Moolenaar683b7962017-08-19 15:51:59 +02003198 if (in_part->ch_fd != INVALID_FD
3199 && (in_part->ch_bufref.br_buf != NULL
3200 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003201 {
3202 in_part->ch_poll_idx = nfd;
3203 fds[nfd].fd = in_part->ch_fd;
3204 fds[nfd].events = POLLOUT;
3205 ++nfd;
3206 }
3207 else
3208 in_part->ch_poll_idx = -1;
3209 }
3210 return nfd;
3211}
3212#endif
3213
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003214typedef enum {
3215 CW_READY,
3216 CW_NOT_READY,
3217 CW_ERROR
3218} channel_wait_result;
3219
Bram Moolenaard04a0202016-01-26 23:30:18 +01003220/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003221 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003222 * Return CW_READY when there is something to read.
3223 * Return CW_NOT_READY when there is nothing to read.
3224 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003225 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003226 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003227channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003228{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003229 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003230 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003231
Bram Moolenaar4f974752019-02-17 17:44:42 +01003232# ifdef MSWIN
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003233 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003234 {
3235 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003236 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003237 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003238 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003239
3240 /* reading from a pipe, not a socket */
3241 while (TRUE)
3242 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003243 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3244
3245 if (r && nread > 0)
3246 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003247
3248 if (channel->ch_named_pipe)
3249 {
3250 DisconnectNamedPipe((HANDLE)fd);
3251 ConnectNamedPipe((HANDLE)fd, NULL);
3252 }
3253 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003254 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003255
3256 /* perhaps write some buffer lines */
3257 channel_write_any_lines();
3258
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003259 sleep_time = deadline - GetTickCount();
3260 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003261 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003262 /* Wait for a little while. Very short at first, up to 10 msec
3263 * after looping a few times. */
3264 if (sleep_time > delay)
3265 sleep_time = delay;
3266 Sleep(sleep_time);
3267 delay = delay * 2;
3268 if (delay > 10)
3269 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003270 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003271 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003272 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003273#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003274 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003275#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003276 struct timeval tval;
3277 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003278 fd_set wfds;
3279 int ret;
3280 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003281
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003282 tval.tv_sec = timeout / 1000;
3283 tval.tv_usec = (timeout % 1000) * 1000;
3284 for (;;)
3285 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003286 FD_ZERO(&rfds);
3287 FD_SET((int)fd, &rfds);
3288
3289 /* Write lines to a pipe when a pipe can be written to. Need to
3290 * set this every time, some buffers may be done. */
3291 maxfd = (int)fd + 1;
3292 FD_ZERO(&wfds);
3293 maxfd = channel_fill_wfds(maxfd, &wfds);
3294
3295 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003296# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003297 SOCK_ERRNO;
3298 if (ret == -1 && errno == EINTR)
3299 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003300# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003301 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003302 {
3303 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003304 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003305 channel_write_any_lines();
3306 continue;
3307 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003308 break;
3309 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003310#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003311 for (;;)
3312 {
3313 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3314 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003315
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003316 fds[0].fd = fd;
3317 fds[0].events = POLLIN;
3318 nfd = channel_fill_poll_write(nfd, fds);
3319 if (poll(fds, nfd, timeout) > 0)
3320 {
3321 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003322 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003323 channel_write_any_lines();
3324 continue;
3325 }
3326 break;
3327 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003328#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003329 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003330 return CW_NOT_READY;
3331}
3332
3333 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003334ch_close_part_on_error(
3335 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003336{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003337 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003338
3339 if (is_err)
3340 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003341 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003342 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003343 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003344
3345 /* Queue a "DETACH" netbeans message in the command queue in order to
3346 * terminate the netbeans session later. Do not end the session here
3347 * directly as we may be running in the context of a call to
3348 * netbeans_parse_messages():
3349 * netbeans_parse_messages
3350 * -> autocmd triggered while processing the netbeans cmd
3351 * -> ui_breakcheck
3352 * -> gui event loop or select loop
3353 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003354 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003355 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003356 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003357 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003358 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3359
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003360 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003361 * close the channel yet, there may be something to read on another part.
3362 * When stdout and stderr use the same FD we get the error only on one of
3363 * them, also close the other. */
3364 if (part == PART_OUT || part == PART_ERR)
3365 {
3366 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3367
3368 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3369 ch_close_part(channel, other);
3370 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003371 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003372
3373#ifdef FEAT_GUI
3374 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003375 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003376#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003377}
3378
3379 static void
3380channel_close_now(channel_T *channel)
3381{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003382 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003383 if (channel->ch_nb_close_cb != NULL)
3384 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003385 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003386}
3387
3388/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003389 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003390 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003391 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003392 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003393 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003394channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003395{
3396 static char_u *buf = NULL;
3397 int len = 0;
3398 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003399 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003400 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003401
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003402 fd = channel->ch_part[part].ch_fd;
3403 if (fd == INVALID_FD)
3404 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003405 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003406 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003407 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003408 }
3409 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003410
3411 /* Allocate a buffer to read into. */
3412 if (buf == NULL)
3413 {
3414 buf = alloc(MAXMSGSIZE);
3415 if (buf == NULL)
3416 return; /* out of memory! */
3417 }
3418
3419 /* Keep on reading for as long as there is something to read.
3420 * Use select() or poll() to avoid blocking on a message that is exactly
3421 * MAXMSGSIZE long. */
3422 for (;;)
3423 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003424 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003425 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003426 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003427 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003428 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003429 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003430 if (len <= 0)
3431 break; /* error or nothing more to read */
3432
3433 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003434 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003435 readlen += len;
3436 if (len < MAXMSGSIZE)
3437 break; /* did read everything that's available */
3438 }
3439
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003440 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003441 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003442 {
3443 if (!channel->ch_keep_open)
3444 ch_close_part_on_error(channel, part, (len < 0), func);
3445 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003446#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003447 else if (CH_HAS_GUI && gtk_main_level() > 0)
3448 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003449 gtk_main_quit();
3450#endif
3451}
3452
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003453/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003454 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003455 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003456 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003457 * Returns what was read in allocated memory.
3458 * Returns NULL in case of error or timeout.
3459 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003460 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003461channel_read_block(
3462 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003463{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003464 char_u *buf;
3465 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003466 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003467 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003468 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003469 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003470
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003471 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003472 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003473
3474 while (TRUE)
3475 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003476 node = channel_peek(channel, part);
3477 if (node != NULL)
3478 {
3479 if (mode == MODE_RAW || (mode == MODE_NL
3480 && channel_first_nl(node) != NULL))
3481 /* got a complete message */
3482 break;
3483 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3484 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003485 /* If not blocking or nothing more is coming then return what we
3486 * have. */
3487 if (raw || fd == INVALID_FD)
3488 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003489 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003490
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003491 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003492 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003493 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003494 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003495 {
3496 ch_log(channel, "Timed out");
3497 return NULL;
3498 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003499 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003500 }
3501
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003502 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003503 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003504 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003505 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003506 }
3507 else
3508 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003509 char_u *p;
3510
3511 buf = node->rq_buffer;
3512 nl = channel_first_nl(node);
3513
3514 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003515 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003516 if (*p == NUL)
3517 *p = NL;
3518
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003519 if (nl == NULL)
3520 {
3521 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003522 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003523 }
3524 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003525 {
3526 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003527 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003528 *nl = NUL;
3529 }
3530 else
3531 {
3532 /* Copy the message into allocated memory and remove it from the
3533 * buffer. */
3534 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003535 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003536 }
3537 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003538 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003539 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003540 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003541}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003542
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003543/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003544 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003545 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003546 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003547 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003548 * In corner cases this can be called recursively, that is why ch_block_ids is
3549 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003550 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003551 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003552channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003553 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003554 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003555 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003556 int id,
3557 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003558{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003559 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003560 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003561 int timeout;
3562 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003563
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003564 ch_log(channel, "Blocking read JSON for id %d", id);
3565 if (id >= 0)
3566 channel_add_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003567 for (;;)
3568 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003569 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003570
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003571 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003572 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003573 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003574 if (id >= 0)
3575 channel_remove_block_id(chanpart, id);
3576 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003577 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003578 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003579
Bram Moolenaard7ece102016-02-02 23:23:02 +01003580 if (!more)
3581 {
3582 /* Handle any other messages in the queue. If done some more
3583 * messages may have arrived. */
3584 if (channel_parse_messages())
3585 continue;
3586
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003587 /* Wait for up to the timeout. If there was an incomplete message
3588 * use the deadline for that. */
3589 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003590 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003591 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003592#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003593 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3594#else
3595 {
3596 struct timeval now_tv;
3597
3598 gettimeofday(&now_tv, NULL);
3599 timeout = (chanpart->ch_deadline.tv_sec
3600 - now_tv.tv_sec) * 1000
3601 + (chanpart->ch_deadline.tv_usec
3602 - now_tv.tv_usec) / 1000
3603 + 1;
3604 }
3605#endif
3606 if (timeout < 0)
3607 {
3608 /* Something went wrong, channel_parse_json() didn't
3609 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003610 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003611 timeout = timeout_arg;
3612 }
3613 else if (timeout > timeout_arg)
3614 timeout = timeout_arg;
3615 }
3616 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003617 if (fd == INVALID_FD
3618 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003619 {
3620 if (timeout == timeout_arg)
3621 {
3622 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003623 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003624 break;
3625 }
3626 }
3627 else
3628 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003629 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003630 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003631 if (id >= 0)
3632 channel_remove_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003633 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003634}
3635
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003636/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003637 * Get the channel from the argument.
3638 * Returns NULL if the handle is invalid.
3639 * When "check_open" is TRUE check that the channel can be used.
3640 * When "reading" is TRUE "check_open" considers typeahead useful.
3641 * "part" is used to check typeahead, when PART_COUNT use the default part.
3642 */
3643 static channel_T *
3644get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3645{
3646 channel_T *channel = NULL;
3647 int has_readahead = FALSE;
3648
3649 if (tv->v_type == VAR_JOB)
3650 {
3651 if (tv->vval.v_job != NULL)
3652 channel = tv->vval.v_job->jv_channel;
3653 }
3654 else if (tv->v_type == VAR_CHANNEL)
3655 {
3656 channel = tv->vval.v_channel;
3657 }
3658 else
3659 {
3660 semsg(_(e_invarg2), tv_get_string(tv));
3661 return NULL;
3662 }
3663 if (channel != NULL && reading)
3664 has_readahead = channel_has_readahead(channel,
3665 part != PART_COUNT ? part : channel_part_read(channel));
3666
3667 if (check_open && (channel == NULL || (!channel_is_open(channel)
3668 && !(reading && has_readahead))))
3669 {
3670 emsg(_("E906: not an open channel"));
3671 return NULL;
3672 }
3673 return channel;
3674}
3675
3676/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003677 * Common for ch_read() and ch_readraw().
3678 */
3679 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003680common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003681{
3682 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003683 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003684 jobopt_T opt;
3685 int mode;
3686 int timeout;
3687 int id = -1;
3688 typval_T *listtv = NULL;
3689
3690 /* return an empty string by default */
3691 rettv->v_type = VAR_STRING;
3692 rettv->vval.v_string = NULL;
3693
3694 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003695 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003696 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003697 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003698
Bram Moolenaar437905c2016-04-26 19:01:05 +02003699 if (opt.jo_set & JO_PART)
3700 part = opt.jo_part;
3701 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003702 if (channel != NULL)
3703 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003704 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003705 part = channel_part_read(channel);
3706 mode = channel_get_mode(channel, part);
3707 timeout = channel_get_timeout(channel, part);
3708 if (opt.jo_set & JO_TIMEOUT)
3709 timeout = opt.jo_timeout;
3710
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003711 if (blob)
3712 {
3713 int outlen = 0;
3714 char_u *p = channel_read_block(channel, part,
3715 timeout, TRUE, &outlen);
3716 if (p != NULL)
3717 {
3718 blob_T *b = blob_alloc();
3719
3720 if (b != NULL)
3721 {
3722 b->bv_ga.ga_len = outlen;
3723 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3724 blob_free(b);
3725 else
3726 {
3727 memcpy(b->bv_ga.ga_data, p, outlen);
3728 rettv_blob_set(rettv, b);
3729 }
3730 }
3731 vim_free(p);
3732 }
3733 }
3734 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003735 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003736 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003737 else
3738 {
3739 if (opt.jo_set & JO_ID)
3740 id = opt.jo_id;
3741 channel_read_json_block(channel, part, timeout, id, &listtv);
3742 if (listtv != NULL)
3743 {
3744 *rettv = *listtv;
3745 vim_free(listtv);
3746 }
3747 else
3748 {
3749 rettv->v_type = VAR_SPECIAL;
3750 rettv->vval.v_number = VVAL_NONE;
3751 }
3752 }
3753 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003754
3755theend:
3756 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003757}
3758
Bram Moolenaar4f974752019-02-17 17:44:42 +01003759# if defined(MSWIN) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003760 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003761/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003762 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003763 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003764 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003765 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003766channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003767{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003768 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003769 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003770
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003771 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003772 for (channel = first_channel; channel != NULL;
3773 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003774 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003775 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003776 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003777 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003778 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003779 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003780 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003781 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003782 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003783}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003784# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003785
Bram Moolenaar4f974752019-02-17 17:44:42 +01003786# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003787/*
3788 * Check the channels for anything that is ready to be read.
3789 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003790 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003791 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003792 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003793channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003794{
3795 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003796 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003797 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003798
3799 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3800 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003801 if (only_keep_open && !channel->ch_keep_open)
3802 continue;
3803
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003804 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003805 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003806 {
3807 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003808 if (fd != INVALID_FD)
3809 {
3810 int r = channel_wait(channel, fd, 0);
3811
3812 if (r == CW_READY)
3813 channel_read(channel, part, "channel_handle_events");
3814 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003815 ch_close_part_on_error(channel, part, TRUE,
3816 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003817 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003818 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003819 }
3820}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003821# endif
3822
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003823# if defined(FEAT_GUI) || defined(PROTO)
3824/*
3825 * Return TRUE when there is any channel with a keep_open flag.
3826 */
3827 int
3828channel_any_keep_open()
3829{
3830 channel_T *channel;
3831
3832 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3833 if (channel->ch_keep_open)
3834 return TRUE;
3835 return FALSE;
3836}
3837# endif
3838
Bram Moolenaard04a0202016-01-26 23:30:18 +01003839/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003840 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003841 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003842 */
3843 void
3844channel_set_nonblock(channel_T *channel, ch_part_T part)
3845{
3846 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003847 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003848
3849 if (fd != INVALID_FD)
3850 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003851#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003852 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003853
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003854 ioctlsocket(fd, FIONBIO, &val);
3855#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003856 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003857#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003858 ch_part->ch_nonblocking = TRUE;
3859 }
3860}
3861
3862/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003863 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003864 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003865 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003866 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003867 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003868channel_send(
3869 channel_T *channel,
3870 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003871 char_u *buf_arg,
3872 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003873 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003874{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003875 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003876 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003877 chanpart_T *ch_part = &channel->ch_part[part];
3878 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003879
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003880 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003881 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003882 {
3883 if (!channel->ch_error && fun != NULL)
3884 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003885 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003886 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003887 }
3888 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003889 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003890 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003891
Bram Moolenaar0b146882018-09-06 16:27:24 +02003892 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3893 channel_set_nonblock(channel, part);
3894
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003895 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003896 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003897 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003898 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003899 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003900 fprintf(log_fd, "'\n");
3901 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003902 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003903 }
3904
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003905 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003906 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003907 writeq_T *wq = &ch_part->ch_writeque;
3908 char_u *buf;
3909 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003910
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003911 if (wq->wq_next != NULL)
3912 {
3913 /* first write what was queued */
3914 buf = wq->wq_next->wq_ga.ga_data;
3915 len = wq->wq_next->wq_ga.ga_len;
3916 did_use_queue = TRUE;
3917 }
3918 else
3919 {
3920 if (len_arg == 0)
3921 /* nothing to write, called from channel_select_check() */
3922 return OK;
3923 buf = buf_arg;
3924 len = len_arg;
3925 }
3926
3927 if (part == PART_SOCK)
3928 res = sock_write(fd, (char *)buf, len);
3929 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003930 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003931 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003932#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003933 if (channel->ch_named_pipe && res < 0)
3934 {
3935 DisconnectNamedPipe((HANDLE)fd);
3936 ConnectNamedPipe((HANDLE)fd, NULL);
3937 }
3938#endif
3939 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003940 if (res < 0 && (errno == EWOULDBLOCK
3941#ifdef EAGAIN
3942 || errno == EAGAIN
3943#endif
3944 ))
3945 res = 0; /* nothing got written */
3946
3947 if (res >= 0 && ch_part->ch_nonblocking)
3948 {
3949 writeq_T *entry = wq->wq_next;
3950
3951 if (did_use_queue)
3952 ch_log(channel, "Sent %d bytes now", res);
3953 if (res == len)
3954 {
3955 /* Wrote all the buf[len] bytes. */
3956 if (entry != NULL)
3957 {
3958 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003959 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003960 continue;
3961 }
3962 if (did_use_queue)
3963 ch_log(channel, "Write queue empty");
3964 }
3965 else
3966 {
3967 /* Wrote only buf[res] bytes, can't write more now. */
3968 if (entry != NULL)
3969 {
3970 if (res > 0)
3971 {
3972 /* Remove the bytes that were written. */
3973 mch_memmove(entry->wq_ga.ga_data,
3974 (char *)entry->wq_ga.ga_data + res,
3975 len - res);
3976 entry->wq_ga.ga_len -= res;
3977 }
3978 buf = buf_arg;
3979 len = len_arg;
3980 }
3981 else
3982 {
3983 buf += res;
3984 len -= res;
3985 }
3986 ch_log(channel, "Adding %d bytes to the write queue", len);
3987
3988 /* Append the not written bytes of the argument to the write
3989 * buffer. Limit entries to 4000 bytes. */
3990 if (wq->wq_prev != NULL
3991 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3992 {
3993 writeq_T *last = wq->wq_prev;
3994
3995 /* append to the last entry */
Bram Moolenaar0d071552019-07-08 22:04:03 +02003996 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003997 {
3998 mch_memmove((char *)last->wq_ga.ga_data
3999 + last->wq_ga.ga_len,
4000 buf, len);
4001 last->wq_ga.ga_len += len;
4002 }
4003 }
4004 else
4005 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004006 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004007
4008 if (last != NULL)
4009 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004010 last->wq_prev = wq->wq_prev;
4011 last->wq_next = NULL;
4012 if (wq->wq_prev == NULL)
4013 wq->wq_next = last;
4014 else
4015 wq->wq_prev->wq_next = last;
4016 wq->wq_prev = last;
4017 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004018 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004019 {
4020 mch_memmove(last->wq_ga.ga_data, buf, len);
4021 last->wq_ga.ga_len = len;
4022 }
4023 }
4024 }
4025 }
4026 }
4027 else if (res != len)
4028 {
4029 if (!channel->ch_error && fun != NULL)
4030 {
4031 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004032 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004033 }
4034 channel->ch_error = TRUE;
4035 return FAIL;
4036 }
4037
4038 channel->ch_error = FALSE;
4039 return OK;
4040 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004041}
4042
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004043/*
4044 * Common for "ch_sendexpr()" and "ch_sendraw()".
4045 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004046 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004047 * Otherwise returns NULL.
4048 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004049 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004050send_common(
4051 typval_T *argvars,
4052 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004053 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004054 int id,
4055 int eval,
4056 jobopt_T *opt,
4057 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004058 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004059{
4060 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004061 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004063 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004064 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004065 if (channel == NULL)
4066 return NULL;
4067 part_send = channel_part_send(channel);
4068 *part_read = channel_part_read(channel);
4069
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004070 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004071 return NULL;
4072
4073 /* Set the callback. An empty callback means no callback and not reading
4074 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4075 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004076 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004077 {
4078 if (eval)
4079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004080 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004081 return NULL;
4082 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004083 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004084 }
4085
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004086 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004087 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004088 return channel;
4089 return NULL;
4090}
4091
4092/*
4093 * common for "ch_evalexpr()" and "ch_sendexpr()"
4094 */
4095 void
4096ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4097{
4098 char_u *text;
4099 typval_T *listtv;
4100 channel_T *channel;
4101 int id;
4102 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004103 ch_part_T part_send;
4104 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004105 jobopt_T opt;
4106 int timeout;
4107
4108 /* return an empty string by default */
4109 rettv->v_type = VAR_STRING;
4110 rettv->vval.v_string = NULL;
4111
Bram Moolenaar437905c2016-04-26 19:01:05 +02004112 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004113 if (channel == NULL)
4114 return;
4115 part_send = channel_part_send(channel);
4116
4117 ch_mode = channel_get_mode(channel, part_send);
4118 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004120 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004121 return;
4122 }
4123
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004124 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004125 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004126 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004127 if (text == NULL)
4128 return;
4129
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004130 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004131 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4132 vim_free(text);
4133 if (channel != NULL && eval)
4134 {
4135 if (opt.jo_set & JO_TIMEOUT)
4136 timeout = opt.jo_timeout;
4137 else
4138 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004139 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4140 == OK)
4141 {
4142 list_T *list = listtv->vval.v_list;
4143
4144 /* Move the item from the list and then change the type to
4145 * avoid the value being freed. */
4146 *rettv = list->lv_last->li_tv;
4147 list->lv_last->li_tv.v_type = VAR_NUMBER;
4148 free_tv(listtv);
4149 }
4150 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004151 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004152}
4153
4154/*
4155 * common for "ch_evalraw()" and "ch_sendraw()"
4156 */
4157 void
4158ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4159{
4160 char_u buf[NUMBUFLEN];
4161 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004162 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004163 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004164 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004165 jobopt_T opt;
4166 int timeout;
4167
4168 /* return an empty string by default */
4169 rettv->v_type = VAR_STRING;
4170 rettv->vval.v_string = NULL;
4171
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004172 if (argvars[1].v_type == VAR_BLOB)
4173 {
4174 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4175 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4176 }
4177 else
4178 {
4179 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004180 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004181 }
4182 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004183 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4184 if (channel != NULL && eval)
4185 {
4186 if (opt.jo_set & JO_TIMEOUT)
4187 timeout = opt.jo_timeout;
4188 else
4189 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004190 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004191 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004192 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004193 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004194}
4195
Bram Moolenaarf3360612017-10-01 16:21:31 +02004196# define KEEP_OPEN_TIME 20 /* msec */
4197
Bram Moolenaard04a0202016-01-26 23:30:18 +01004198# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004199/*
4200 * Add open channels to the poll struct.
4201 * Return the adjusted struct index.
4202 * The type of "fds" is hidden to avoid problems with the function proto.
4203 */
4204 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004205channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004206{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004207 int nfd = nfd_in;
4208 channel_T *channel;
4209 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004210 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004211
Bram Moolenaar77073442016-02-13 23:23:53 +01004212 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004213 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004214 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004215 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004216 chanpart_T *ch_part = &channel->ch_part[part];
4217
4218 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004219 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004220 if (channel->ch_keep_open)
4221 {
4222 /* For unknown reason poll() returns immediately for a
4223 * keep-open channel. Instead of adding it to the fds add
4224 * a short timeout and check, like polling. */
4225 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4226 *towait = KEEP_OPEN_TIME;
4227 }
4228 else
4229 {
4230 ch_part->ch_poll_idx = nfd;
4231 fds[nfd].fd = ch_part->ch_fd;
4232 fds[nfd].events = POLLIN;
4233 nfd++;
4234 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004235 }
4236 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004237 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004238 }
4239 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004240
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004241 nfd = channel_fill_poll_write(nfd, fds);
4242
Bram Moolenaare0874f82016-01-24 20:36:41 +01004243 return nfd;
4244}
4245
4246/*
4247 * The type of "fds" is hidden to avoid problems with the function proto.
4248 */
4249 int
4250channel_poll_check(int ret_in, void *fds_in)
4251{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004252 int ret = ret_in;
4253 channel_T *channel;
4254 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004255 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004256 int idx;
4257 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004258
Bram Moolenaar77073442016-02-13 23:23:53 +01004259 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004260 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004261 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004262 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004263 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004264
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004265 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004266 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004267 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004268 --ret;
4269 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004270 else if (channel->ch_part[part].ch_fd != INVALID_FD
4271 && channel->ch_keep_open)
4272 {
4273 /* polling a keep-open channel */
4274 channel_read(channel, part, "channel_poll_check_keep_open");
4275 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004276 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004277
4278 in_part = &channel->ch_part[PART_IN];
4279 idx = in_part->ch_poll_idx;
4280 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4281 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004282 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004283 --ret;
4284 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004285 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004286
4287 return ret;
4288}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004289# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004290
Bram Moolenaar4f974752019-02-17 17:44:42 +01004291# if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004292
Bram Moolenaare0874f82016-01-24 20:36:41 +01004293/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004294 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004295 */
4296 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004297channel_select_setup(
4298 int maxfd_in,
4299 void *rfds_in,
4300 void *wfds_in,
4301 struct timeval *tv,
4302 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004303{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004304 int maxfd = maxfd_in;
4305 channel_T *channel;
4306 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004307 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004308 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004309
Bram Moolenaar77073442016-02-13 23:23:53 +01004310 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004311 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004312 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004313 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004314 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004315
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004316 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004317 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004318 if (channel->ch_keep_open)
4319 {
4320 /* For unknown reason select() returns immediately for a
4321 * keep-open channel. Instead of adding it to the rfds add
4322 * a short timeout and check, like polling. */
4323 if (*tvp == NULL || tv->tv_sec > 0
4324 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4325 {
4326 *tvp = tv;
4327 tv->tv_sec = 0;
4328 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4329 }
4330 }
4331 else
4332 {
4333 FD_SET((int)fd, rfds);
4334 if (maxfd < (int)fd)
4335 maxfd = (int)fd;
4336 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004337 }
4338 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004339 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004340
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004341 maxfd = channel_fill_wfds(maxfd, wfds);
4342
Bram Moolenaare0874f82016-01-24 20:36:41 +01004343 return maxfd;
4344}
4345
4346/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004347 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004348 */
4349 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004350channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004351{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004352 int ret = ret_in;
4353 channel_T *channel;
4354 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004355 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004356 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004357 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004358
Bram Moolenaar77073442016-02-13 23:23:53 +01004359 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004360 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004361 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004362 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004363 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004364
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004365 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004366 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004367 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004368 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004369 --ret;
4370 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004371 else if (fd != INVALID_FD && channel->ch_keep_open)
4372 {
4373 /* polling a keep-open channel */
4374 channel_read(channel, part, "channel_select_check_keep_open");
4375 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004376 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004377
4378 in_part = &channel->ch_part[PART_IN];
4379 if (ret > 0 && in_part->ch_fd != INVALID_FD
4380 && FD_ISSET(in_part->ch_fd, wfds))
4381 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004382 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004383 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004384 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004385 --ret;
4386 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004387 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004388
4389 return ret;
4390}
Bram Moolenaar4f974752019-02-17 17:44:42 +01004391# endif /* !MSWIN && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004392
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004393/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004394 * Execute queued up commands.
4395 * Invoked from the main loop when it's safe to execute received commands.
4396 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004397 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004398 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004399channel_parse_messages(void)
4400{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004401 channel_T *channel = first_channel;
4402 int ret = FALSE;
4403 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004404 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004405#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004406 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004407
4408 ELAPSED_INIT(start_tv);
4409#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004410
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004411 ++safe_to_invoke_callback;
4412
Bram Moolenaard0b65022016-03-06 21:50:33 +01004413 /* Only do this message when another message was given, otherwise we get
4414 * lots of them. */
4415 if (did_log_msg)
4416 {
4417 ch_log(NULL, "looking for messages on channels");
4418 did_log_msg = FALSE;
4419 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004420 while (channel != NULL)
4421 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004422 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004423 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004424 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004425 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004426 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004427 channel = first_channel;
4428 continue;
4429 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004430 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004431 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004432 if (channel->ch_killing)
4433 {
4434 channel_free_contents(channel);
4435 channel->ch_job->jv_channel = NULL;
4436 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004437 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004438 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004439 channel = first_channel;
4440 continue;
4441 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004442 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004443 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004444 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004445 channel_free(channel);
4446 channel = first_channel;
4447 part = PART_SOCK;
4448 continue;
4449 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004450 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004451 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004452 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004453 /* Increase the refcount, in case the handler causes the channel
4454 * to be unreferenced or closed. */
4455 ++channel->ch_refcount;
4456 r = may_invoke_callback(channel, part);
4457 if (r == OK)
4458 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004459 if (channel_unref(channel) || (r == OK
4460#ifdef ELAPSED_FUNC
4461 /* Limit the time we loop here to 100 msec, otherwise
4462 * Vim becomes unresponsive when the callback takes
4463 * more than a bit of time. */
4464 && ELAPSED_FUNC(start_tv) < 100L
4465#endif
4466 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004467 {
4468 /* channel was freed or something was done, start over */
4469 channel = first_channel;
4470 part = PART_SOCK;
4471 continue;
4472 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004473 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004474 if (part < PART_ERR)
4475 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004476 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004477 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004478 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004479 part = PART_SOCK;
4480 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004481 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004482
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004483 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004484 {
4485 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004486 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004487 }
4488
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004489 --safe_to_invoke_callback;
4490
Bram Moolenaard7ece102016-02-02 23:23:02 +01004491 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004492}
4493
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004494/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004495 * Return TRUE if any channel has readahead. That means we should not block on
4496 * waiting for input.
4497 */
4498 int
4499channel_any_readahead(void)
4500{
4501 channel_T *channel = first_channel;
4502 ch_part_T part = PART_SOCK;
4503
4504 while (channel != NULL)
4505 {
4506 if (channel_has_readahead(channel, part))
4507 return TRUE;
4508 if (part < PART_ERR)
4509 ++part;
4510 else
4511 {
4512 channel = channel->ch_next;
4513 part = PART_SOCK;
4514 }
4515 }
4516 return FALSE;
4517}
4518
4519/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004520 * Mark references to lists used in channels.
4521 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004522 int
4523set_ref_in_channel(int copyID)
4524{
Bram Moolenaar77073442016-02-13 23:23:53 +01004525 int abort = FALSE;
4526 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004527 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004528
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004529 for (channel = first_channel; !abort && channel != NULL;
4530 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004531 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004532 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004533 tv.v_type = VAR_CHANNEL;
4534 tv.vval.v_channel = channel;
4535 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004536 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004537 return abort;
4538}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004539
4540/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004541 * Return the "part" to write to for "channel".
4542 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004543 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004544channel_part_send(channel_T *channel)
4545{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004546 if (channel->CH_SOCK_FD == INVALID_FD)
4547 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004548 return PART_SOCK;
4549}
4550
4551/*
4552 * Return the default "part" to read from for "channel".
4553 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004554 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004555channel_part_read(channel_T *channel)
4556{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004557 if (channel->CH_SOCK_FD == INVALID_FD)
4558 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004559 return PART_SOCK;
4560}
4561
4562/*
4563 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004564 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004565 */
4566 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004567channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004568{
Bram Moolenaar77073442016-02-13 23:23:53 +01004569 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004570 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004571 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004572}
4573
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004574/*
4575 * Return the timeout of "channel"/"part"
4576 */
4577 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004578channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004579{
4580 return channel->ch_part[part].ch_timeout;
4581}
4582
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004583 static int
4584handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4585{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004586 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004587
4588 opt->jo_set |= jo;
4589 if (STRCMP(val, "nl") == 0)
4590 *modep = MODE_NL;
4591 else if (STRCMP(val, "raw") == 0)
4592 *modep = MODE_RAW;
4593 else if (STRCMP(val, "js") == 0)
4594 *modep = MODE_JS;
4595 else if (STRCMP(val, "json") == 0)
4596 *modep = MODE_JSON;
4597 else
4598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004599 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004600 return FAIL;
4601 }
4602 return OK;
4603}
4604
4605 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004606handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004607{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004608 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004609
4610 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4611 if (STRCMP(val, "null") == 0)
4612 opt->jo_io[part] = JIO_NULL;
4613 else if (STRCMP(val, "pipe") == 0)
4614 opt->jo_io[part] = JIO_PIPE;
4615 else if (STRCMP(val, "file") == 0)
4616 opt->jo_io[part] = JIO_FILE;
4617 else if (STRCMP(val, "buffer") == 0)
4618 opt->jo_io[part] = JIO_BUFFER;
4619 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4620 opt->jo_io[part] = JIO_OUT;
4621 else
4622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004623 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004624 return FAIL;
4625 }
4626 return OK;
4627}
4628
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004629/*
4630 * Clear a jobopt_T before using it.
4631 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004632 void
4633clear_job_options(jobopt_T *opt)
4634{
4635 vim_memset(opt, 0, sizeof(jobopt_T));
4636}
4637
4638/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004639 * Free any members of a jobopt_T.
4640 */
4641 void
4642free_job_options(jobopt_T *opt)
4643{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004644 if (opt->jo_callback.cb_partial != NULL)
4645 partial_unref(opt->jo_callback.cb_partial);
4646 else if (opt->jo_callback.cb_name != NULL)
4647 func_unref(opt->jo_callback.cb_name);
4648 if (opt->jo_out_cb.cb_partial != NULL)
4649 partial_unref(opt->jo_out_cb.cb_partial);
4650 else if (opt->jo_out_cb.cb_name != NULL)
4651 func_unref(opt->jo_out_cb.cb_name);
4652 if (opt->jo_err_cb.cb_partial != NULL)
4653 partial_unref(opt->jo_err_cb.cb_partial);
4654 else if (opt->jo_err_cb.cb_name != NULL)
4655 func_unref(opt->jo_err_cb.cb_name);
4656 if (opt->jo_close_cb.cb_partial != NULL)
4657 partial_unref(opt->jo_close_cb.cb_partial);
4658 else if (opt->jo_close_cb.cb_name != NULL)
4659 func_unref(opt->jo_close_cb.cb_name);
4660 if (opt->jo_exit_cb.cb_partial != NULL)
4661 partial_unref(opt->jo_exit_cb.cb_partial);
4662 else if (opt->jo_exit_cb.cb_name != NULL)
4663 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004664 if (opt->jo_env != NULL)
4665 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004666}
4667
4668/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004669 * Get the PART_ number from the first character of an option name.
4670 */
4671 static int
4672part_from_char(int c)
4673{
4674 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4675}
4676
4677/*
4678 * Get the option entries from the dict in "tv", parse them and put the result
4679 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004680 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004681 * If an option value is invalid return FAIL.
4682 */
4683 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004684get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004685{
4686 typval_T *item;
4687 char_u *val;
4688 dict_T *dict;
4689 int todo;
4690 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004691 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004692
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004693 if (tv->v_type == VAR_UNKNOWN)
4694 return OK;
4695 if (tv->v_type != VAR_DICT)
4696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004697 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004698 return FAIL;
4699 }
4700 dict = tv->vval.v_dict;
4701 if (dict == NULL)
4702 return OK;
4703
4704 todo = (int)dict->dv_hashtab.ht_used;
4705 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4706 if (!HASHITEM_EMPTY(hi))
4707 {
4708 item = &dict_lookup(hi)->di_tv;
4709
4710 if (STRCMP(hi->hi_key, "mode") == 0)
4711 {
4712 if (!(supported & JO_MODE))
4713 break;
4714 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4715 return FAIL;
4716 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004717 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004718 {
4719 if (!(supported & JO_IN_MODE))
4720 break;
4721 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4722 == FAIL)
4723 return FAIL;
4724 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004725 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004726 {
4727 if (!(supported & JO_OUT_MODE))
4728 break;
4729 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4730 == FAIL)
4731 return FAIL;
4732 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004733 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004734 {
4735 if (!(supported & JO_ERR_MODE))
4736 break;
4737 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4738 == FAIL)
4739 return FAIL;
4740 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004741 else if (STRCMP(hi->hi_key, "noblock") == 0)
4742 {
4743 if (!(supported & JO_MODE))
4744 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004745 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004746 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004747 else if (STRCMP(hi->hi_key, "in_io") == 0
4748 || STRCMP(hi->hi_key, "out_io") == 0
4749 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004750 {
4751 if (!(supported & JO_OUT_IO))
4752 break;
4753 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4754 return FAIL;
4755 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004756 else if (STRCMP(hi->hi_key, "in_name") == 0
4757 || STRCMP(hi->hi_key, "out_name") == 0
4758 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004759 {
4760 part = part_from_char(*hi->hi_key);
4761
4762 if (!(supported & JO_OUT_IO))
4763 break;
4764 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4765 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004766 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004767 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004768 else if (STRCMP(hi->hi_key, "pty") == 0)
4769 {
4770 if (!(supported & JO_MODE))
4771 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004772 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004773 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004774 else if (STRCMP(hi->hi_key, "in_buf") == 0
4775 || STRCMP(hi->hi_key, "out_buf") == 0
4776 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004777 {
4778 part = part_from_char(*hi->hi_key);
4779
4780 if (!(supported & JO_OUT_IO))
4781 break;
4782 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004784 if (opt->jo_io_buf[part] <= 0)
4785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004786 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004787 return FAIL;
4788 }
4789 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004791 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004792 return FAIL;
4793 }
4794 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004795 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4796 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4797 {
4798 part = part_from_char(*hi->hi_key);
4799
4800 if (!(supported & JO_OUT_IO))
4801 break;
4802 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004803 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004804 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004805 else if (STRCMP(hi->hi_key, "out_msg") == 0
4806 || STRCMP(hi->hi_key, "err_msg") == 0)
4807 {
4808 part = part_from_char(*hi->hi_key);
4809
4810 if (!(supported & JO_OUT_IO))
4811 break;
4812 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004813 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004814 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004815 else if (STRCMP(hi->hi_key, "in_top") == 0
4816 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004817 {
4818 linenr_T *lp;
4819
4820 if (!(supported & JO_OUT_IO))
4821 break;
4822 if (hi->hi_key[3] == 't')
4823 {
4824 lp = &opt->jo_in_top;
4825 opt->jo_set |= JO_IN_TOP;
4826 }
4827 else
4828 {
4829 lp = &opt->jo_in_bot;
4830 opt->jo_set |= JO_IN_BOT;
4831 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004832 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004833 if (*lp < 0)
4834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004835 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004836 return FAIL;
4837 }
4838 }
4839 else if (STRCMP(hi->hi_key, "channel") == 0)
4840 {
4841 if (!(supported & JO_OUT_IO))
4842 break;
4843 opt->jo_set |= JO_CHANNEL;
4844 if (item->v_type != VAR_CHANNEL)
4845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004846 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004847 return FAIL;
4848 }
4849 opt->jo_channel = item->vval.v_channel;
4850 }
4851 else if (STRCMP(hi->hi_key, "callback") == 0)
4852 {
4853 if (!(supported & JO_CALLBACK))
4854 break;
4855 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004856 opt->jo_callback = get_callback(item);
4857 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004860 return FAIL;
4861 }
4862 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004863 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004864 {
4865 if (!(supported & JO_OUT_CALLBACK))
4866 break;
4867 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004868 opt->jo_out_cb = get_callback(item);
4869 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004871 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004872 return FAIL;
4873 }
4874 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004875 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004876 {
4877 if (!(supported & JO_ERR_CALLBACK))
4878 break;
4879 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004880 opt->jo_err_cb = get_callback(item);
4881 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004883 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004884 return FAIL;
4885 }
4886 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004887 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004888 {
4889 if (!(supported & JO_CLOSE_CALLBACK))
4890 break;
4891 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004892 opt->jo_close_cb = get_callback(item);
4893 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004895 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004896 return FAIL;
4897 }
4898 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004899 else if (STRCMP(hi->hi_key, "drop") == 0)
4900 {
4901 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004902 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004903
4904 if (STRCMP(val, "never") == 0)
4905 never = TRUE;
4906 else if (STRCMP(val, "auto") != 0)
4907 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004908 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004909 return FAIL;
4910 }
4911 opt->jo_drop_never = never;
4912 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004913 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4914 {
4915 if (!(supported & JO_EXIT_CB))
4916 break;
4917 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004918 opt->jo_exit_cb = get_callback(item);
4919 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004921 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004922 return FAIL;
4923 }
4924 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004925#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004926 else if (STRCMP(hi->hi_key, "term_name") == 0)
4927 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004928 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004929 break;
4930 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004931 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004932 if (opt->jo_term_name == NULL)
4933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004934 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004935 return FAIL;
4936 }
4937 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004938 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4939 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004940 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004941 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004942 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004943 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004945 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004946 return FAIL;
4947 }
4948 opt->jo_set2 |= JO2_TERM_FINISH;
4949 opt->jo_term_finish = *val;
4950 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004951 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4952 {
4953 char_u *p;
4954
4955 if (!(supported2 & JO2_TERM_OPENCMD))
4956 break;
4957 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004958 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004959 if (p != NULL)
4960 {
4961 /* Must have %d and no other %. */
4962 p = vim_strchr(p, '%');
4963 if (p != NULL && (p[1] != 'd'
4964 || vim_strchr(p + 2, '%') != NULL))
4965 p = NULL;
4966 }
4967 if (p == NULL)
4968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004969 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004970 return FAIL;
4971 }
4972 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004973 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4974 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004975 char_u *p;
4976
4977 if (!(supported2 & JO2_EOF_CHARS))
4978 break;
4979 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004980 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004981 if (p == NULL)
4982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004983 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004984 return FAIL;
4985 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004986 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004987 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4988 {
4989 if (!(supported2 & JO2_TERM_ROWS))
4990 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004991 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004992 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004993 }
4994 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4995 {
4996 if (!(supported2 & JO2_TERM_COLS))
4997 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004998 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004999 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005000 }
5001 else if (STRCMP(hi->hi_key, "vertical") == 0)
5002 {
5003 if (!(supported2 & JO2_VERTICAL))
5004 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005005 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005006 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005007 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005008 else if (STRCMP(hi->hi_key, "curwin") == 0)
5009 {
5010 if (!(supported2 & JO2_CURWIN))
5011 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005012 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005013 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005014 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005015 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5016 {
5017 int nr;
5018
5019 if (!(supported2 & JO2_CURWIN))
5020 break;
5021 opt->jo_set2 |= JO2_BUFNR;
5022 nr = tv_get_number(item);
5023 if (nr <= 0)
5024 {
5025 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5026 return FAIL;
5027 }
5028 opt->jo_bufnr_buf = buflist_findnr(nr);
5029 if (opt->jo_bufnr_buf == NULL)
5030 {
5031 semsg(_(e_nobufnr), (long)nr);
5032 return FAIL;
5033 }
5034 if (opt->jo_bufnr_buf->b_nwindows == 0
5035 || opt->jo_bufnr_buf->b_term == NULL)
5036 {
5037 semsg(_(e_invarg2), "bufnr");
5038 return FAIL;
5039 }
5040 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005041 else if (STRCMP(hi->hi_key, "hidden") == 0)
5042 {
5043 if (!(supported2 & JO2_HIDDEN))
5044 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005045 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005046 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005047 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005048 else if (STRCMP(hi->hi_key, "norestore") == 0)
5049 {
5050 if (!(supported2 & JO2_NORESTORE))
5051 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005052 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005053 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005054 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005055 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5056 {
5057 if (!(supported2 & JO2_TERM_KILL))
5058 break;
5059 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005060 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005061 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005062 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005063 {
5064 char_u *p;
5065
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005066 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005067 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005068 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005069 p = tv_get_string_chk(item);
5070 if (p == NULL)
5071 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005072 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005073 return FAIL;
5074 }
5075 // Allow empty string, "winpty", "conpty".
5076 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5077 || STRCMP(p, "conpty") == 0))
5078 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005079 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005080 return FAIL;
5081 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005082 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005083 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005084# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5085 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005087 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005088 listitem_T *li;
5089 long_u rgb[16];
5090
5091 if (!(supported2 & JO2_ANSI_COLORS))
5092 break;
5093
5094 if (item == NULL || item->v_type != VAR_LIST
5095 || item->vval.v_list == NULL)
5096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005097 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005098 return FAIL;
5099 }
5100
5101 li = item->vval.v_list->lv_first;
5102 for (; li != NULL && n < 16; li = li->li_next, n++)
5103 {
5104 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005105 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005106
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005107 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005108 if (color_name == NULL)
5109 return FAIL;
5110
5111 guicolor = GUI_GET_COLOR(color_name);
5112 if (guicolor == INVALCOLOR)
5113 return FAIL;
5114
5115 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5116 }
5117
5118 if (n != 16 || li != NULL)
5119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005120 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005121 return FAIL;
5122 }
5123
5124 opt->jo_set2 |= JO2_ANSI_COLORS;
5125 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5126 }
5127# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005128#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005129 else if (STRCMP(hi->hi_key, "env") == 0)
5130 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005131 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005132 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005133 if (item->v_type != VAR_DICT)
5134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005135 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005136 return FAIL;
5137 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005138 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005139 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005140 if (opt->jo_env != NULL)
5141 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005142 }
5143 else if (STRCMP(hi->hi_key, "cwd") == 0)
5144 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005145 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005146 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005147 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005148 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005149#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005150 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5151#endif
5152 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005154 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005155 return FAIL;
5156 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005157 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005158 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005159 else if (STRCMP(hi->hi_key, "waittime") == 0)
5160 {
5161 if (!(supported & JO_WAITTIME))
5162 break;
5163 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005164 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005165 }
5166 else if (STRCMP(hi->hi_key, "timeout") == 0)
5167 {
5168 if (!(supported & JO_TIMEOUT))
5169 break;
5170 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005171 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005172 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005173 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005174 {
5175 if (!(supported & JO_OUT_TIMEOUT))
5176 break;
5177 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005178 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005179 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005180 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005181 {
5182 if (!(supported & JO_ERR_TIMEOUT))
5183 break;
5184 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005185 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005186 }
5187 else if (STRCMP(hi->hi_key, "part") == 0)
5188 {
5189 if (!(supported & JO_PART))
5190 break;
5191 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005192 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005193 if (STRCMP(val, "err") == 0)
5194 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005195 else if (STRCMP(val, "out") == 0)
5196 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005197 else
5198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005199 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005200 return FAIL;
5201 }
5202 }
5203 else if (STRCMP(hi->hi_key, "id") == 0)
5204 {
5205 if (!(supported & JO_ID))
5206 break;
5207 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005208 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005209 }
5210 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5211 {
5212 if (!(supported & JO_STOPONEXIT))
5213 break;
5214 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005215 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005216 opt->jo_soe_buf);
5217 if (opt->jo_stoponexit == NULL)
5218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005219 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005220 return FAIL;
5221 }
5222 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005223 else if (STRCMP(hi->hi_key, "block_write") == 0)
5224 {
5225 if (!(supported & JO_BLOCK_WRITE))
5226 break;
5227 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005228 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005229 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005230 else
5231 break;
5232 --todo;
5233 }
5234 if (todo > 0)
5235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005236 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005237 return FAIL;
5238 }
5239
5240 return OK;
5241}
5242
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005243static job_T *first_job = NULL;
5244
5245 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005246job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005247{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005248 int i;
5249
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005250 ch_log(job->jv_channel, "Freeing job");
5251 if (job->jv_channel != NULL)
5252 {
5253 /* The link from the channel to the job doesn't count as a reference,
5254 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005255 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005256 * NULL the reference. We don't set ch_job_killed, unreferencing the
5257 * job doesn't mean it stops running. */
5258 job->jv_channel->ch_job = NULL;
5259 channel_unref(job->jv_channel);
5260 }
5261 mch_clear_job(job);
5262
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005263 vim_free(job->jv_tty_in);
5264 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005265 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005266#ifdef UNIX
5267 vim_free(job->jv_termsig);
5268#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005269#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005270 vim_free(job->jv_tty_type);
5271#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005272 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005273 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005274 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005275 for (i = 0; job->jv_argv[i] != NULL; i++)
5276 vim_free(job->jv_argv[i]);
5277 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005278 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005279}
5280
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005281/*
5282 * Remove "job" from the list of jobs.
5283 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005284 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005285job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005286{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005287 if (job->jv_next != NULL)
5288 job->jv_next->jv_prev = job->jv_prev;
5289 if (job->jv_prev == NULL)
5290 first_job = job->jv_next;
5291 else
5292 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005293}
5294
5295 static void
5296job_free_job(job_T *job)
5297{
5298 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005299 vim_free(job);
5300}
5301
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005302 static void
5303job_free(job_T *job)
5304{
5305 if (!in_free_unref_items)
5306 {
5307 job_free_contents(job);
5308 job_free_job(job);
5309 }
5310}
5311
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005312job_T *jobs_to_free = NULL;
5313
5314/*
5315 * Put "job" in a list to be freed later, when it's no longer referenced.
5316 */
5317 static void
5318job_free_later(job_T *job)
5319{
5320 job_unlink(job);
5321 job->jv_next = jobs_to_free;
5322 jobs_to_free = job;
5323}
5324
5325 static void
5326free_jobs_to_free_later(void)
5327{
5328 job_T *job;
5329
5330 while (jobs_to_free != NULL)
5331 {
5332 job = jobs_to_free;
5333 jobs_to_free = job->jv_next;
5334 job_free_contents(job);
5335 vim_free(job);
5336 }
5337}
5338
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005339#if defined(EXITFREE) || defined(PROTO)
5340 void
5341job_free_all(void)
5342{
5343 while (first_job != NULL)
5344 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005345 free_jobs_to_free_later();
5346
5347# ifdef FEAT_TERMINAL
5348 free_unused_terminals();
5349# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005350}
5351#endif
5352
5353/*
5354 * Return TRUE if we need to check if the process of "job" has ended.
5355 */
5356 static int
5357job_need_end_check(job_T *job)
5358{
5359 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005360 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005361}
5362
5363/*
5364 * Return TRUE if the channel of "job" is still useful.
5365 */
5366 static int
5367job_channel_still_useful(job_T *job)
5368{
5369 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5370}
5371
5372/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005373 * Return TRUE if the channel of "job" is closeable.
5374 */
5375 static int
5376job_channel_can_close(job_T *job)
5377{
5378 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5379}
5380
5381/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005382 * Return TRUE if the job should not be freed yet. Do not free the job when
5383 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5384 * or when the associated channel will do something with the job output.
5385 */
5386 static int
5387job_still_useful(job_T *job)
5388{
5389 return job_need_end_check(job) || job_channel_still_useful(job);
5390}
5391
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005392#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005393/*
5394 * Return TRUE when there is any running job that we care about.
5395 */
5396 int
5397job_any_running()
5398{
5399 job_T *job;
5400
5401 for (job = first_job; job != NULL; job = job->jv_next)
5402 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005403 {
5404 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005405 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005406 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005407 return FALSE;
5408}
5409#endif
5410
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005411#if !defined(USE_ARGV) || defined(PROTO)
5412/*
5413 * Escape one argument for an external command.
5414 * Returns the escaped string in allocated memory. NULL when out of memory.
5415 */
5416 static char_u *
5417win32_escape_arg(char_u *arg)
5418{
5419 int slen, dlen;
5420 int escaping = 0;
5421 int i;
5422 char_u *s, *d;
5423 char_u *escaped_arg;
5424 int has_spaces = FALSE;
5425
5426 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005427 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005428 dlen = slen;
5429 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5430 {
5431 if (*s == '"' || *s == '\\')
5432 ++dlen;
5433 if (*s == ' ' || *s == '\t')
5434 has_spaces = TRUE;
5435 }
5436
5437 if (has_spaces)
5438 dlen += 2;
5439
5440 if (dlen == slen)
5441 return vim_strsave(arg);
5442
5443 /* Allocate memory for the result and fill it. */
5444 escaped_arg = alloc(dlen + 1);
5445 if (escaped_arg == NULL)
5446 return NULL;
5447 memset(escaped_arg, 0, dlen+1);
5448
5449 d = escaped_arg;
5450
5451 if (has_spaces)
5452 *d++ = '"';
5453
5454 for (s = arg; *s != NUL;)
5455 {
5456 switch (*s)
5457 {
5458 case '"':
5459 for (i = 0; i < escaping; i++)
5460 *d++ = '\\';
5461 escaping = 0;
5462 *d++ = '\\';
5463 *d++ = *s++;
5464 break;
5465 case '\\':
5466 escaping++;
5467 *d++ = *s++;
5468 break;
5469 default:
5470 escaping = 0;
5471 MB_COPY_CHAR(s, d);
5472 break;
5473 }
5474 }
5475
5476 /* add terminating quote and finish with a NUL */
5477 if (has_spaces)
5478 {
5479 for (i = 0; i < escaping; i++)
5480 *d++ = '\\';
5481 *d++ = '"';
5482 }
5483 *d = NUL;
5484
5485 return escaped_arg;
5486}
5487
5488/*
5489 * Build a command line from a list, taking care of escaping.
5490 * The result is put in gap->ga_data.
5491 * Returns FAIL when out of memory.
5492 */
5493 int
5494win32_build_cmd(list_T *l, garray_T *gap)
5495{
5496 listitem_T *li;
5497 char_u *s;
5498
5499 for (li = l->lv_first; li != NULL; li = li->li_next)
5500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005501 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005502 if (s == NULL)
5503 return FAIL;
5504 s = win32_escape_arg(s);
5505 if (s == NULL)
5506 return FAIL;
5507 ga_concat(gap, s);
5508 vim_free(s);
5509 if (li->li_next != NULL)
5510 ga_append(gap, ' ');
5511 }
5512 return OK;
5513}
5514#endif
5515
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005516/*
5517 * NOTE: Must call job_cleanup() only once right after the status of "job"
5518 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5519 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005520 * If the job is no longer used it will be removed from the list of jobs, and
5521 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005522 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005523 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005524job_cleanup(job_T *job)
5525{
5526 if (job->jv_status != JOB_ENDED)
5527 return;
5528
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005529 /* Ready to cleanup the job. */
5530 job->jv_status = JOB_FINISHED;
5531
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005532 /* When only channel-in is kept open, close explicitly. */
5533 if (job->jv_channel != NULL)
5534 ch_close_part(job->jv_channel, PART_IN);
5535
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005536 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005537 {
5538 typval_T argv[3];
5539 typval_T rettv;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005540
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005541 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005542 ch_log(job->jv_channel, "Invoking exit callback %s",
5543 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005544 ++job->jv_refcount;
5545 argv[0].v_type = VAR_JOB;
5546 argv[0].vval.v_job = job;
5547 argv[1].v_type = VAR_NUMBER;
5548 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005549 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005550 clear_tv(&rettv);
5551 --job->jv_refcount;
5552 channel_need_redraw = TRUE;
5553 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005554
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005555 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5556 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005557
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005558 // Do not free the job in case the close callback of the associated channel
5559 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005560 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005561 // The job was already unreferenced and the associated channel was
5562 // detached, now that it ended it can be freed. However, a caller might
5563 // still use it, thus free it a bit later.
5564 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005565}
5566
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005567/*
5568 * Mark references in jobs that are still useful.
5569 */
5570 int
5571set_ref_in_job(int copyID)
5572{
5573 int abort = FALSE;
5574 job_T *job;
5575 typval_T tv;
5576
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005577 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005578 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005579 {
5580 tv.v_type = VAR_JOB;
5581 tv.vval.v_job = job;
5582 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5583 }
5584 return abort;
5585}
5586
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005587/*
5588 * Dereference "job". Note that after this "job" may have been freed.
5589 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005590 void
5591job_unref(job_T *job)
5592{
5593 if (job != NULL && --job->jv_refcount <= 0)
5594 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005595 /* Do not free the job if there is a channel where the close callback
5596 * may get the job info. */
5597 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005598 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005599 /* Do not free the job when it has not ended yet and there is a
5600 * "stoponexit" flag or an exit callback. */
5601 if (!job_need_end_check(job))
5602 {
5603 job_free(job);
5604 }
5605 else if (job->jv_channel != NULL)
5606 {
5607 /* Do remove the link to the channel, otherwise it hangs
5608 * around until Vim exits. See job_free() for refcount. */
5609 ch_log(job->jv_channel, "detaching channel from job");
5610 job->jv_channel->ch_job = NULL;
5611 channel_unref(job->jv_channel);
5612 job->jv_channel = NULL;
5613 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005614 }
5615 }
5616}
5617
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005618 int
5619free_unused_jobs_contents(int copyID, int mask)
5620{
5621 int did_free = FALSE;
5622 job_T *job;
5623
5624 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005625 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005626 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005627 {
5628 /* Free the channel and ordinary items it contains, but don't
5629 * recurse into Lists, Dictionaries etc. */
5630 job_free_contents(job);
5631 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005632 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005633 return did_free;
5634}
5635
5636 void
5637free_unused_jobs(int copyID, int mask)
5638{
5639 job_T *job;
5640 job_T *job_next;
5641
5642 for (job = first_job; job != NULL; job = job_next)
5643 {
5644 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005645 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005646 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005647 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005648 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005649 job_free_job(job);
5650 }
5651 }
5652}
5653
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005654/*
5655 * Allocate a job. Sets the refcount to one and sets options default.
5656 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005657 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005658job_alloc(void)
5659{
5660 job_T *job;
5661
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005662 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005663 if (job != NULL)
5664 {
5665 job->jv_refcount = 1;
5666 job->jv_stoponexit = vim_strsave((char_u *)"term");
5667
5668 if (first_job != NULL)
5669 {
5670 first_job->jv_prev = job;
5671 job->jv_next = first_job;
5672 }
5673 first_job = job;
5674 }
5675 return job;
5676}
5677
5678 void
5679job_set_options(job_T *job, jobopt_T *opt)
5680{
5681 if (opt->jo_set & JO_STOPONEXIT)
5682 {
5683 vim_free(job->jv_stoponexit);
5684 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5685 job->jv_stoponexit = NULL;
5686 else
5687 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5688 }
5689 if (opt->jo_set & JO_EXIT_CB)
5690 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005691 free_callback(&job->jv_exit_cb);
5692 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005693 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005694 job->jv_exit_cb.cb_name = NULL;
5695 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005696 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005697 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005698 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005699 }
5700}
5701
5702/*
5703 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5704 */
5705 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005706job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005707{
5708 job_T *job;
5709
5710 for (job = first_job; job != NULL; job = job->jv_next)
5711 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005712 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005713}
5714
5715/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005716 * Return TRUE when there is any job that has an exit callback and might exit,
5717 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005718 */
5719 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005720has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005721{
5722 job_T *job;
5723
5724 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005725 /* Only should check if the channel has been closed, if the channel is
5726 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005727 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5728 || (job->jv_status == JOB_FINISHED
5729 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005730 return TRUE;
5731 return FALSE;
5732}
5733
Bram Moolenaar97792de2016-10-15 18:36:49 +02005734#define MAX_CHECK_ENDED 8
5735
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005736/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005737 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005738 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005739 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005740 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005741job_check_ended(void)
5742{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005743 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005744 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005745
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005746 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005747 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005748 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005749
Bram Moolenaar97792de2016-10-15 18:36:49 +02005750 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005751 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005752 // NOTE: mch_detect_ended_job() must only return a job of which the
5753 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005754 job_T *job = mch_detect_ended_job(first_job);
5755
5756 if (job == NULL)
5757 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005758 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005759 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005760 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005761
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005762 // Actually free jobs that were cleaned up.
5763 free_jobs_to_free_later();
5764
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005765 if (channel_need_redraw)
5766 {
5767 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005768 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005769 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005770 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005771}
5772
5773/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005774 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005775 * "argv_arg" is only for Unix.
5776 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005777 * The returned job has a refcount of one.
5778 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005779 */
5780 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005781job_start(
5782 typval_T *argvars,
5783 char **argv_arg,
5784 jobopt_T *opt_arg,
5785 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005786{
5787 job_T *job;
5788 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005789 char **argv = NULL;
5790 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005791#if defined(UNIX)
5792# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005793 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005794#else
5795 garray_T ga;
5796#endif
5797 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005798 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005799
5800 job = job_alloc();
5801 if (job == NULL)
5802 return NULL;
5803
5804 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005805#ifndef USE_ARGV
5806 ga_init2(&ga, (int)sizeof(char*), 20);
5807#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005808
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005809 if (opt_arg != NULL)
5810 opt = *opt_arg;
5811 else
5812 {
5813 /* Default mode is NL. */
5814 clear_job_options(&opt);
5815 opt.jo_mode = MODE_NL;
5816 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005817 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5818 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5819 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005820 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005821 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005822
5823 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005824 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005825 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5826 && opt.jo_io[part] == JIO_FILE
5827 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5828 || *opt.jo_io_name[part] == NUL))
5829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005830 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005831 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005832 }
5833
5834 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5835 {
5836 buf_T *buf = NULL;
5837
5838 /* check that we can find the buffer before starting the job */
5839 if (opt.jo_set & JO_IN_BUF)
5840 {
5841 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5842 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005843 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005844 }
5845 else if (!(opt.jo_set & JO_IN_NAME))
5846 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005847 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005848 }
5849 else
5850 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5851 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005852 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005853 if (buf->b_ml.ml_mfp == NULL)
5854 {
5855 char_u numbuf[NUMBUFLEN];
5856 char_u *s;
5857
5858 if (opt.jo_set & JO_IN_BUF)
5859 {
5860 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5861 s = numbuf;
5862 }
5863 else
5864 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005865 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005866 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005867 }
5868 job->jv_in_buf = buf;
5869 }
5870
5871 job_set_options(job, &opt);
5872
Bram Moolenaar13568252018-03-16 20:46:58 +01005873#ifdef USE_ARGV
5874 if (argv_arg != NULL)
5875 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005876 /* Make a copy of argv_arg for job->jv_argv. */
5877 for (i = 0; argv_arg[i] != NULL; i++)
5878 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005879 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005880 if (argv == NULL)
5881 goto theend;
5882 for (i = 0; i < argc; i++)
5883 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5884 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005885 }
5886 else
5887#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005888 if (argvars[0].v_type == VAR_STRING)
5889 {
5890 /* Command is a string. */
5891 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005892 if (cmd == NULL || *cmd == NUL)
5893 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005894 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005895 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005896 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005897
5898 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005899 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005900 }
5901 else if (argvars[0].v_type != VAR_LIST
5902 || argvars[0].vval.v_list == NULL
5903 || argvars[0].vval.v_list->lv_len < 1)
5904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005905 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005906 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005907 }
5908 else
5909 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005910 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005911
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005912 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005913 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005914#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005915 if (win32_build_cmd(l, &ga) == FAIL)
5916 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005917 cmd = ga.ga_data;
5918#endif
5919 }
5920
Bram Moolenaar20608922018-04-21 22:30:08 +02005921 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005922 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005923
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005924#ifdef USE_ARGV
5925 if (ch_log_active())
5926 {
5927 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005928
5929 ga_init2(&ga, (int)sizeof(char), 200);
5930 for (i = 0; i < argc; ++i)
5931 {
5932 if (i > 0)
5933 ga_concat(&ga, (char_u *)" ");
5934 ga_concat(&ga, (char_u *)argv[i]);
5935 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005936 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005937 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005938 ga_clear(&ga);
5939 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005940 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005941#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005942 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005943 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005944#endif
5945
5946 /* If the channel is reading from a buffer, write lines now. */
5947 if (job->jv_channel != NULL)
5948 channel_write_in(job->jv_channel);
5949
5950theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005951#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005952 vim_free(ga.ga_data);
5953#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005954 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005955 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005956 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005957 return job;
5958}
5959
5960/*
5961 * Get the status of "job" and invoke the exit callback when needed.
5962 * The returned string is not allocated.
5963 */
5964 char *
5965job_status(job_T *job)
5966{
5967 char *result;
5968
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005969 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005970 /* No need to check, dead is dead. */
5971 result = "dead";
5972 else if (job->jv_status == JOB_FAILED)
5973 result = "fail";
5974 else
5975 {
5976 result = mch_job_status(job);
5977 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005978 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005979 }
5980 return result;
5981}
5982
Bram Moolenaar8950a562016-03-12 15:22:55 +01005983/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005984 * Send a signal to "job". Implements job_stop().
5985 * When "type" is not NULL use this for the type.
5986 * Otherwise use argvars[1] for the type.
5987 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005988 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005989job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005990{
5991 char_u *arg;
5992
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005993 if (type != NULL)
5994 arg = (char_u *)type;
5995 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005996 arg = (char_u *)"";
5997 else
5998 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005999 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006000 if (arg == NULL)
6001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006002 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006003 return 0;
6004 }
6005 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006006 if (job->jv_status == JOB_FAILED)
6007 {
6008 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6009 return 0;
6010 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006011 if (job->jv_status == JOB_ENDED)
6012 {
6013 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6014 return 0;
6015 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006016 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006017 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006018 return 0;
6019
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006020 /* Assume that only "kill" will kill the job. */
6021 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006022 job->jv_channel->ch_job_killed = TRUE;
6023
6024 /* We don't try freeing the job, obviously the caller still has a
6025 * reference to it. */
6026 return 1;
6027}
6028
Bram Moolenaarf2732452018-06-03 14:47:35 +02006029 void
6030invoke_prompt_callback(void)
6031{
6032 typval_T rettv;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006033 typval_T argv[2];
6034 char_u *text;
6035 char_u *prompt;
6036 linenr_T lnum = curbuf->b_ml.ml_line_count;
6037
6038 // Add a new line for the prompt before invoking the callback, so that
6039 // text can always be inserted above the last line.
6040 ml_append(lnum, (char_u *)"", 0, FALSE);
6041 curwin->w_cursor.lnum = lnum + 1;
6042 curwin->w_cursor.col = 0;
6043
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006044 if (curbuf->b_prompt_callback.cb_name == NULL
6045 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006046 return;
6047 text = ml_get(lnum);
6048 prompt = prompt_text();
6049 if (STRLEN(text) >= STRLEN(prompt))
6050 text += STRLEN(prompt);
6051 argv[0].v_type = VAR_STRING;
6052 argv[0].vval.v_string = vim_strsave(text);
6053 argv[1].v_type = VAR_UNKNOWN;
6054
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006055 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006056 clear_tv(&argv[0]);
6057 clear_tv(&rettv);
6058}
6059
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006060/*
6061 * Return TRUE when the interrupt callback was invoked.
6062 */
6063 int
6064invoke_prompt_interrupt(void)
6065{
6066 typval_T rettv;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006067 typval_T argv[1];
6068
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006069 if (curbuf->b_prompt_interrupt.cb_name == NULL
6070 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006071 return FALSE;
6072 argv[0].v_type = VAR_UNKNOWN;
6073
6074 got_int = FALSE; // don't skip executing commands
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006075 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006076 clear_tv(&rettv);
6077 return TRUE;
6078}
6079
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006080/*
6081 * "prompt_setcallback({buffer}, {callback})" function
6082 */
6083 void
6084f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6085{
6086 buf_T *buf;
6087 callback_T callback;
6088
6089 if (check_secure())
6090 return;
6091 buf = tv_get_buf(&argvars[0], FALSE);
6092 if (buf == NULL)
6093 return;
6094
6095 callback = get_callback(&argvars[1]);
6096 if (callback.cb_name == NULL)
6097 return;
6098
6099 free_callback(&buf->b_prompt_callback);
6100 set_callback(&buf->b_prompt_callback, &callback);
6101}
6102
6103/*
6104 * "prompt_setinterrupt({buffer}, {callback})" function
6105 */
6106 void
6107f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6108{
6109 buf_T *buf;
6110 callback_T callback;
6111
6112 if (check_secure())
6113 return;
6114 buf = tv_get_buf(&argvars[0], FALSE);
6115 if (buf == NULL)
6116 return;
6117
6118 callback = get_callback(&argvars[1]);
6119 if (callback.cb_name == NULL)
6120 return;
6121
6122 free_callback(&buf->b_prompt_interrupt);
6123 set_callback(&buf->b_prompt_interrupt, &callback);
6124}
6125
6126/*
6127 * "prompt_setprompt({buffer}, {text})" function
6128 */
6129 void
6130f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6131{
6132 buf_T *buf;
6133 char_u *text;
6134
6135 if (check_secure())
6136 return;
6137 buf = tv_get_buf(&argvars[0], FALSE);
6138 if (buf == NULL)
6139 return;
6140
6141 text = tv_get_string(&argvars[1]);
6142 vim_free(buf->b_prompt_text);
6143 buf->b_prompt_text = vim_strsave(text);
6144}
6145
6146/*
6147 * "ch_canread()" function
6148 */
6149 void
6150f_ch_canread(typval_T *argvars, typval_T *rettv)
6151{
6152 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6153
6154 rettv->vval.v_number = 0;
6155 if (channel != NULL)
6156 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6157 || channel_has_readahead(channel, PART_OUT)
6158 || channel_has_readahead(channel, PART_ERR);
6159}
6160
6161/*
6162 * "ch_close()" function
6163 */
6164 void
6165f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6166{
6167 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6168
6169 if (channel != NULL)
6170 {
6171 channel_close(channel, FALSE);
6172 channel_clear(channel);
6173 }
6174}
6175
6176/*
6177 * "ch_close()" function
6178 */
6179 void
6180f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6181{
6182 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6183
6184 if (channel != NULL)
6185 channel_close_in(channel);
6186}
6187
6188/*
6189 * "ch_getbufnr()" function
6190 */
6191 void
6192f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6193{
6194 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6195
6196 rettv->vval.v_number = -1;
6197 if (channel != NULL)
6198 {
6199 char_u *what = tv_get_string(&argvars[1]);
6200 int part;
6201
6202 if (STRCMP(what, "err") == 0)
6203 part = PART_ERR;
6204 else if (STRCMP(what, "out") == 0)
6205 part = PART_OUT;
6206 else if (STRCMP(what, "in") == 0)
6207 part = PART_IN;
6208 else
6209 part = PART_SOCK;
6210 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6211 rettv->vval.v_number =
6212 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6213 }
6214}
6215
6216/*
6217 * "ch_getjob()" function
6218 */
6219 void
6220f_ch_getjob(typval_T *argvars, typval_T *rettv)
6221{
6222 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6223
6224 if (channel != NULL)
6225 {
6226 rettv->v_type = VAR_JOB;
6227 rettv->vval.v_job = channel->ch_job;
6228 if (channel->ch_job != NULL)
6229 ++channel->ch_job->jv_refcount;
6230 }
6231}
6232
6233/*
6234 * "ch_info()" function
6235 */
6236 void
6237f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6238{
6239 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6240
6241 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6242 channel_info(channel, rettv->vval.v_dict);
6243}
6244
6245/*
6246 * "ch_log()" function
6247 */
6248 void
6249f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6250{
6251 char_u *msg = tv_get_string(&argvars[0]);
6252 channel_T *channel = NULL;
6253
6254 if (argvars[1].v_type != VAR_UNKNOWN)
6255 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6256
6257 ch_log(channel, "%s", msg);
6258}
6259
6260/*
6261 * "ch_logfile()" function
6262 */
6263 void
6264f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6265{
6266 char_u *fname;
6267 char_u *opt = (char_u *)"";
6268 char_u buf[NUMBUFLEN];
6269
6270 /* Don't open a file in restricted mode. */
6271 if (check_restricted() || check_secure())
6272 return;
6273 fname = tv_get_string(&argvars[0]);
6274 if (argvars[1].v_type == VAR_STRING)
6275 opt = tv_get_string_buf(&argvars[1], buf);
6276 ch_logfile(fname, opt);
6277}
6278
6279/*
6280 * "ch_open()" function
6281 */
6282 void
6283f_ch_open(typval_T *argvars, typval_T *rettv)
6284{
6285 rettv->v_type = VAR_CHANNEL;
6286 if (check_restricted() || check_secure())
6287 return;
6288 rettv->vval.v_channel = channel_open_func(argvars);
6289}
6290
6291/*
6292 * "ch_read()" function
6293 */
6294 void
6295f_ch_read(typval_T *argvars, typval_T *rettv)
6296{
6297 common_channel_read(argvars, rettv, FALSE, FALSE);
6298}
6299
6300/*
6301 * "ch_readblob()" function
6302 */
6303 void
6304f_ch_readblob(typval_T *argvars, typval_T *rettv)
6305{
6306 common_channel_read(argvars, rettv, TRUE, TRUE);
6307}
6308
6309/*
6310 * "ch_readraw()" function
6311 */
6312 void
6313f_ch_readraw(typval_T *argvars, typval_T *rettv)
6314{
6315 common_channel_read(argvars, rettv, TRUE, FALSE);
6316}
6317
6318/*
6319 * "ch_evalexpr()" function
6320 */
6321 void
6322f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6323{
6324 ch_expr_common(argvars, rettv, TRUE);
6325}
6326
6327/*
6328 * "ch_sendexpr()" function
6329 */
6330 void
6331f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6332{
6333 ch_expr_common(argvars, rettv, FALSE);
6334}
6335
6336/*
6337 * "ch_evalraw()" function
6338 */
6339 void
6340f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6341{
6342 ch_raw_common(argvars, rettv, TRUE);
6343}
6344
6345/*
6346 * "ch_sendraw()" function
6347 */
6348 void
6349f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6350{
6351 ch_raw_common(argvars, rettv, FALSE);
6352}
6353
6354/*
6355 * "ch_setoptions()" function
6356 */
6357 void
6358f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6359{
6360 channel_T *channel;
6361 jobopt_T opt;
6362
6363 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6364 if (channel == NULL)
6365 return;
6366 clear_job_options(&opt);
6367 if (get_job_options(&argvars[1], &opt,
6368 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6369 channel_set_options(channel, &opt);
6370 free_job_options(&opt);
6371}
6372
6373/*
6374 * "ch_status()" function
6375 */
6376 void
6377f_ch_status(typval_T *argvars, typval_T *rettv)
6378{
6379 channel_T *channel;
6380 jobopt_T opt;
6381 int part = -1;
6382
6383 /* return an empty string by default */
6384 rettv->v_type = VAR_STRING;
6385 rettv->vval.v_string = NULL;
6386
6387 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6388
6389 if (argvars[1].v_type != VAR_UNKNOWN)
6390 {
6391 clear_job_options(&opt);
6392 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6393 && (opt.jo_set & JO_PART))
6394 part = opt.jo_part;
6395 }
6396
6397 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6398}
6399
6400/*
6401 * Get the job from the argument.
6402 * Returns NULL if the job is invalid.
6403 */
6404 static job_T *
6405get_job_arg(typval_T *tv)
6406{
6407 job_T *job;
6408
6409 if (tv->v_type != VAR_JOB)
6410 {
6411 semsg(_(e_invarg2), tv_get_string(tv));
6412 return NULL;
6413 }
6414 job = tv->vval.v_job;
6415
6416 if (job == NULL)
6417 emsg(_("E916: not a valid job"));
6418 return job;
6419}
6420
6421/*
6422 * "job_getchannel()" function
6423 */
6424 void
6425f_job_getchannel(typval_T *argvars, typval_T *rettv)
6426{
6427 job_T *job = get_job_arg(&argvars[0]);
6428
6429 if (job != NULL)
6430 {
6431 rettv->v_type = VAR_CHANNEL;
6432 rettv->vval.v_channel = job->jv_channel;
6433 if (job->jv_channel != NULL)
6434 ++job->jv_channel->ch_refcount;
6435 }
6436}
6437
6438/*
6439 * Implementation of job_info().
6440 */
6441 static void
6442job_info(job_T *job, dict_T *dict)
6443{
6444 dictitem_T *item;
6445 varnumber_T nr;
6446 list_T *l;
6447 int i;
6448
6449 dict_add_string(dict, "status", (char_u *)job_status(job));
6450
6451 item = dictitem_alloc((char_u *)"channel");
6452 if (item == NULL)
6453 return;
6454 item->di_tv.v_type = VAR_CHANNEL;
6455 item->di_tv.vval.v_channel = job->jv_channel;
6456 if (job->jv_channel != NULL)
6457 ++job->jv_channel->ch_refcount;
6458 if (dict_add(dict, item) == FAIL)
6459 dictitem_free(item);
6460
6461#ifdef UNIX
6462 nr = job->jv_pid;
6463#else
6464 nr = job->jv_proc_info.dwProcessId;
6465#endif
6466 dict_add_number(dict, "process", nr);
6467 dict_add_string(dict, "tty_in", job->jv_tty_in);
6468 dict_add_string(dict, "tty_out", job->jv_tty_out);
6469
6470 dict_add_number(dict, "exitval", job->jv_exitval);
6471 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6472 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6473#ifdef UNIX
6474 dict_add_string(dict, "termsig", job->jv_termsig);
6475#endif
6476#ifdef MSWIN
6477 dict_add_string(dict, "tty_type", job->jv_tty_type);
6478#endif
6479
6480 l = list_alloc();
6481 if (l != NULL)
6482 {
6483 dict_add_list(dict, "cmd", l);
6484 if (job->jv_argv != NULL)
6485 for (i = 0; job->jv_argv[i] != NULL; i++)
6486 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6487 }
6488}
6489
6490/*
6491 * Implementation of job_info() to return info for all jobs.
6492 */
6493 static void
6494job_info_all(list_T *l)
6495{
6496 job_T *job;
6497 typval_T tv;
6498
6499 for (job = first_job; job != NULL; job = job->jv_next)
6500 {
6501 tv.v_type = VAR_JOB;
6502 tv.vval.v_job = job;
6503
6504 if (list_append_tv(l, &tv) != OK)
6505 return;
6506 }
6507}
6508
6509/*
6510 * "job_info()" function
6511 */
6512 void
6513f_job_info(typval_T *argvars, typval_T *rettv)
6514{
6515 if (argvars[0].v_type != VAR_UNKNOWN)
6516 {
6517 job_T *job = get_job_arg(&argvars[0]);
6518
6519 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6520 job_info(job, rettv->vval.v_dict);
6521 }
6522 else if (rettv_list_alloc(rettv) == OK)
6523 job_info_all(rettv->vval.v_list);
6524}
6525
6526/*
6527 * "job_setoptions()" function
6528 */
6529 void
6530f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6531{
6532 job_T *job = get_job_arg(&argvars[0]);
6533 jobopt_T opt;
6534
6535 if (job == NULL)
6536 return;
6537 clear_job_options(&opt);
6538 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6539 job_set_options(job, &opt);
6540 free_job_options(&opt);
6541}
6542
6543/*
6544 * "job_start()" function
6545 */
6546 void
6547f_job_start(typval_T *argvars, typval_T *rettv)
6548{
6549 rettv->v_type = VAR_JOB;
6550 if (check_restricted() || check_secure())
6551 return;
6552 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6553}
6554
6555/*
6556 * "job_status()" function
6557 */
6558 void
6559f_job_status(typval_T *argvars, typval_T *rettv)
6560{
6561 job_T *job = get_job_arg(&argvars[0]);
6562
6563 if (job != NULL)
6564 {
6565 rettv->v_type = VAR_STRING;
6566 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6567 }
6568}
6569
6570/*
6571 * "job_stop()" function
6572 */
6573 void
6574f_job_stop(typval_T *argvars, typval_T *rettv)
6575{
6576 job_T *job = get_job_arg(&argvars[0]);
6577
6578 if (job != NULL)
6579 rettv->vval.v_number = job_stop(job, argvars, NULL);
6580}
6581
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006582#endif /* FEAT_JOB_CHANNEL */