blob: 5d1b83aafa9748ca395a04f5cf2662b7b3404ea9 [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;
1636 int dummy;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001637
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001638 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001640
Bram Moolenaar77073442016-02-13 23:23:53 +01001641 argv[0].v_type = VAR_CHANNEL;
1642 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001643
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001644 call_callback(callback, -1, &rettv, 2, argv, NULL,
1645 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001646 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001647 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001648}
1649
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001650/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001651 * Return the first node from "channel"/"part" without removing it.
1652 * Returns NULL if there is nothing.
1653 */
1654 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001655channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001656{
1657 readq_T *head = &channel->ch_part[part].ch_head;
1658
1659 return head->rq_next;
1660}
1661
1662/*
1663 * Return a pointer to the first NL in "node".
1664 * Skips over NUL characters.
1665 * Returns NULL if there is no NL.
1666 */
1667 char_u *
1668channel_first_nl(readq_T *node)
1669{
1670 char_u *buffer = node->rq_buffer;
1671 long_u i;
1672
1673 for (i = 0; i < node->rq_buflen; ++i)
1674 if (buffer[i] == NL)
1675 return buffer + i;
1676 return NULL;
1677}
1678
1679/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001680 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001681 * The caller must free it.
1682 * Returns NULL if there is nothing.
1683 */
1684 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001685channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001686{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001687 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001688 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001689 char_u *p;
1690
Bram Moolenaar77073442016-02-13 23:23:53 +01001691 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001692 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001693 if (outlen != NULL)
1694 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001695 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001696 p = node->rq_buffer;
1697 head->rq_next = node->rq_next;
1698 if (node->rq_next == NULL)
1699 head->rq_prev = NULL;
1700 else
1701 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001702 vim_free(node);
1703 return p;
1704}
1705
1706/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001708 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001709 */
1710 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001711channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001712{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001713 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001714 readq_T *node;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001715 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001716 char_u *res;
1717 char_u *p;
1718
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001719 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001720 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001721 len += node->rq_buflen;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001722 res = alloc(len + 1);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001723 if (res == NULL)
1724 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001725 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001726 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001727 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001728 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001729 p += node->rq_buflen;
1730 }
1731 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001732
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001733 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001734 do
1735 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001737 vim_free(p);
1738 } while (p != NULL);
1739
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 if (outlen != NULL)
1741 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001742 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 *outlen += len;
1744 return res;
1745 }
1746
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001747 // Turn all NUL into NL, so that the result can be used as a string.
1748 p = res;
1749 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001750 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001751 if (*p == NUL)
1752 *p = NL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001753#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001754 else if (*p == 0x1b)
1755 {
1756 // crush the escape sequence OSC 0/1/2: ESC ]0;
1757 if (p + 3 < res + len
1758 && p[1] == ']'
1759 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1760 && p[3] == ';')
1761 {
1762 // '\a' becomes a NL
1763 while (p < res + (len - 1) && *p != '\a')
1764 ++p;
1765 // BEL is zero width characters, suppress display mistake
1766 // ConPTY (after 10.0.18317) requires advance checking
1767 if (p[-1] == NUL)
1768 p[-1] = 0x07;
1769 }
1770 }
1771#endif
1772 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001773 }
1774
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001775 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001776}
1777
1778/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001779 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001780 * Caller must check these bytes are available.
1781 */
1782 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001783channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001784{
1785 readq_T *head = &channel->ch_part[part].ch_head;
1786 readq_T *node = head->rq_next;
1787 char_u *buf = node->rq_buffer;
1788
1789 mch_memmove(buf, buf + len, node->rq_buflen - len);
1790 node->rq_buflen -= len;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001791 node->rq_buffer[node->rq_buflen] = NUL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001792}
1793
1794/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001795 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001796 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001797 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001798 */
1799 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001800channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001801{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001802 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001803 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001804 readq_T *last_node;
1805 readq_T *n;
1806 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001807 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001808 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001809
Bram Moolenaar77073442016-02-13 23:23:53 +01001810 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001811 return FAIL;
1812
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001813 last_node = node->rq_next;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001814 len = node->rq_buflen + last_node->rq_buflen;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001815 if (want_nl)
1816 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001817 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001818 {
1819 last_node = last_node->rq_next;
1820 len += last_node->rq_buflen;
1821 }
1822
Bram Moolenaar772153f2019-03-04 12:09:49 +01001823 p = newbuf = alloc(len + 1);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001825 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001826 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001827 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001828 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001829 node->rq_buffer = newbuf;
1830 for (n = node; n != last_node; )
1831 {
1832 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001833 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001834 p += n->rq_buflen;
1835 vim_free(n->rq_buffer);
1836 }
Bram Moolenaar772153f2019-03-04 12:09:49 +01001837 *p = NUL;
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001838 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001839
1840 /* dispose of the collapsed nodes and their buffers */
1841 for (n = node->rq_next; n != last_node; )
1842 {
1843 n = n->rq_next;
1844 vim_free(n->rq_prev);
1845 }
1846 node->rq_next = last_node->rq_next;
1847 if (last_node->rq_next == NULL)
1848 head->rq_prev = node;
1849 else
1850 last_node->rq_next->rq_prev = node;
1851 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001852 return OK;
1853}
1854
1855/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001856 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001857 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001858 * Returns OK or FAIL.
1859 */
1860 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001861channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001862 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001863{
1864 readq_T *node;
1865 readq_T *head = &channel->ch_part[part].ch_head;
1866 char_u *p;
1867 int i;
1868
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001869 node = ALLOC_ONE(readq_T);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001870 if (node == NULL)
1871 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001872 /* A NUL is added at the end, because netbeans code expects that.
1873 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001874 node->rq_buffer = alloc(len + 1);
1875 if (node->rq_buffer == NULL)
1876 {
1877 vim_free(node);
1878 return FAIL; /* out of memory */
1879 }
1880
1881 if (channel->ch_part[part].ch_mode == MODE_NL)
1882 {
1883 /* Drop any CR before a NL. */
1884 p = node->rq_buffer;
1885 for (i = 0; i < len; ++i)
1886 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1887 *p++ = buf[i];
1888 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001889 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890 }
1891 else
1892 {
1893 mch_memmove(node->rq_buffer, buf, len);
1894 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001895 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001896 }
1897
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001898 if (prepend)
1899 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001900 // prepend node to the head of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001901 node->rq_next = head->rq_next;
1902 node->rq_prev = NULL;
1903 if (head->rq_next == NULL)
1904 head->rq_prev = node;
1905 else
1906 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001907 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001908 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001909 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001910 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001911 // append node to the tail of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001912 node->rq_next = NULL;
1913 node->rq_prev = head->rq_prev;
1914 if (head->rq_prev == NULL)
1915 head->rq_next = node;
1916 else
1917 head->rq_prev->rq_next = node;
1918 head->rq_prev = node;
1919 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001920
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001921 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001922 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001923 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001924 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001925 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001926 fprintf(log_fd, "'\n");
1927 }
1928 return OK;
1929}
1930
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001931/*
1932 * Try to fill the buffer of "reader".
1933 * Returns FALSE when nothing was added.
1934 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001935 static int
1936channel_fill(js_read_T *reader)
1937{
1938 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001939 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001940 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001941 int keeplen;
1942 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001943 char_u *p;
1944
1945 if (next == NULL)
1946 return FALSE;
1947
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001948 keeplen = reader->js_end - reader->js_buf;
1949 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001950 {
1951 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001952 addlen = (int)STRLEN(next);
1953 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001954 if (p == NULL)
1955 {
1956 vim_free(next);
1957 return FALSE;
1958 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001959 mch_memmove(p, reader->js_buf, keeplen);
1960 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001961 vim_free(next);
1962 next = p;
1963 }
1964
1965 vim_free(reader->js_buf);
1966 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001967 return TRUE;
1968}
1969
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001970/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001971 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001972 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01001973 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001974 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01001975 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001976channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001977{
1978 js_read_T reader;
1979 typval_T listtv;
1980 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001981 chanpart_T *chanpart = &channel->ch_part[part];
1982 jsonq_T *head = &chanpart->ch_json_head;
1983 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01001984 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001985
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001986 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01001987 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001988
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001989 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001990 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001991 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01001992 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001993 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001994
1995 /* When a message is incomplete we wait for a short while for more to
1996 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001997 * or list will make us hang.
1998 * Do not generate error messages, they will be written in a channel log. */
1999 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002000 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002001 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002002 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002003 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002004 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002005 /* Only accept the response when it is a list with at least two
2006 * items. */
2007 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002008 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002009 if (listtv.v_type != VAR_LIST)
2010 ch_error(channel, "Did not receive a list, discarding");
2011 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002012 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002013 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002014 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002015 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002016 else
2017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002018 item = ALLOC_ONE(jsonq_T);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002019 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002020 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002021 else
2022 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002023 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002024 item->jq_value = alloc_tv();
2025 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002026 {
2027 vim_free(item);
2028 clear_tv(&listtv);
2029 }
2030 else
2031 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002032 *item->jq_value = listtv;
2033 item->jq_prev = head->jq_prev;
2034 head->jq_prev = item;
2035 item->jq_next = NULL;
2036 if (item->jq_prev == NULL)
2037 head->jq_next = item;
2038 else
2039 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002040 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041 }
2042 }
2043 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002044
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002045 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002046 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002047 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002048 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002049 size_t buflen = STRLEN(reader.js_buf);
2050
2051 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002052 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002053 /* First time encountering incomplete message or after receiving
2054 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002055 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002056 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002057 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002058 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002059 chanpart->ch_wait_len = buflen;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002060#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002061 chanpart->ch_deadline = GetTickCount() + 100L;
2062#else
2063 gettimeofday(&chanpart->ch_deadline, NULL);
2064 chanpart->ch_deadline.tv_usec += 100 * 1000;
2065 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2066 {
2067 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2068 ++chanpart->ch_deadline.tv_sec;
2069 }
2070#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002071 }
2072 else
2073 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002074 int timeout;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002075#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002076 timeout = GetTickCount() > chanpart->ch_deadline;
2077#else
2078 {
2079 struct timeval now_tv;
2080
2081 gettimeofday(&now_tv, NULL);
2082 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2083 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2084 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2085 }
2086#endif
2087 if (timeout)
2088 {
2089 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002090 chanpart->ch_wait_len = 0;
2091 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002092 }
2093 else
2094 {
2095 reader.js_used = 0;
2096 ch_log(channel, "still waiting on incomplete message");
2097 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002098 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002099 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002100
2101 if (status == FAIL)
2102 {
2103 ch_error(channel, "Decoding failed - discarding input");
2104 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002105 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002106 }
2107 else if (reader.js_buf[reader.js_used] != NUL)
2108 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002109 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002110 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002111 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2112 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002113 ret = status == MAYBE ? FALSE: TRUE;
2114 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002115 else
2116 ret = FALSE;
2117
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002118 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002119 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002120}
2121
2122/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002123 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002124 */
2125 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002126remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002127{
Bram Moolenaar77073442016-02-13 23:23:53 +01002128 if (node->cq_prev == NULL)
2129 head->cq_next = node->cq_next;
2130 else
2131 node->cq_prev->cq_next = node->cq_next;
2132 if (node->cq_next == NULL)
2133 head->cq_prev = node->cq_prev;
2134 else
2135 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002136}
2137
2138/*
2139 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002140 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002141 */
2142 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002143remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002144{
Bram Moolenaar77073442016-02-13 23:23:53 +01002145 if (node->jq_prev == NULL)
2146 head->jq_next = node->jq_next;
2147 else
2148 node->jq_prev->jq_next = node->jq_next;
2149 if (node->jq_next == NULL)
2150 head->jq_prev = node->jq_prev;
2151 else
2152 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002153 vim_free(node);
2154}
2155
2156/*
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002157 * Add "id" to the list of JSON message IDs we are waiting on.
2158 */
2159 static void
2160channel_add_block_id(chanpart_T *chanpart, int id)
2161{
2162 garray_T *gap = &chanpart->ch_block_ids;
2163
2164 if (gap->ga_growsize == 0)
2165 ga_init2(gap, (int)sizeof(int), 10);
2166 if (ga_grow(gap, 1) == OK)
2167 {
2168 ((int *)gap->ga_data)[gap->ga_len] = id;
2169 ++gap->ga_len;
2170 }
2171}
2172
2173/*
2174 * Remove "id" from the list of JSON message IDs we are waiting on.
2175 */
2176 static void
2177channel_remove_block_id(chanpart_T *chanpart, int id)
2178{
2179 garray_T *gap = &chanpart->ch_block_ids;
2180 int i;
2181
2182 for (i = 0; i < gap->ga_len; ++i)
2183 if (((int *)gap->ga_data)[i] == id)
2184 {
2185 --gap->ga_len;
2186 if (i < gap->ga_len)
2187 {
2188 int *p = ((int *)gap->ga_data) + i;
2189
2190 mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int));
2191 }
2192 return;
2193 }
2194 siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id);
2195}
2196
2197/*
2198 * Return TRUE if "id" is in the list of JSON message IDs we are waiting on.
2199 */
2200 static int
2201channel_has_block_id(chanpart_T *chanpart, int id)
2202{
2203 garray_T *gap = &chanpart->ch_block_ids;
2204 int i;
2205
2206 for (i = 0; i < gap->ga_len; ++i)
2207 if (((int *)gap->ga_data)[i] == id)
2208 return TRUE;
2209 return FALSE;
2210}
2211
2212/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002213 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002214 * When "id" is positive it must match the first number in the list.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002215 * When "id" is zero or negative jut get the first message. But not one
2216 * in the ch_block_ids list.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002217 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002218 * Return OK when found and return the value in "rettv".
2219 * Return FAIL otherwise.
2220 */
2221 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002222channel_get_json(
2223 channel_T *channel,
2224 ch_part_T part,
2225 int id,
2226 int without_callback,
2227 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002228{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002229 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002230 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002231
Bram Moolenaar77073442016-02-13 23:23:53 +01002232 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002233 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002234 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002235 typval_T *tv = &l->lv_first->li_tv;
2236
Bram Moolenaar958dc692016-12-01 15:34:12 +01002237 if ((without_callback || !item->jq_no_callback)
2238 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002239 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002240 || tv->vval.v_number == 0
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002241 || !channel_has_block_id(
2242 &channel->ch_part[part], tv->vval.v_number)))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002243 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002244 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002245 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002246 ch_log(channel, "Getting JSON message %ld",
2247 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002248 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002249 return OK;
2250 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002251 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002252 }
2253 return FAIL;
2254}
2255
Bram Moolenaar958dc692016-12-01 15:34:12 +01002256/*
2257 * Put back "rettv" into the JSON queue, there was no callback for it.
2258 * Takes over the values in "rettv".
2259 */
2260 static void
2261channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2262{
2263 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2264 jsonq_T *item = head->jq_next;
2265 jsonq_T *newitem;
2266
2267 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2268 /* last item was pushed back, append to the end */
2269 item = NULL;
2270 else while (item != NULL && item->jq_no_callback)
2271 /* append after the last item that was pushed back */
2272 item = item->jq_next;
2273
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002274 newitem = ALLOC_ONE(jsonq_T);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002275 if (newitem == NULL)
2276 clear_tv(rettv);
2277 else
2278 {
2279 newitem->jq_value = alloc_tv();
2280 if (newitem->jq_value == NULL)
2281 {
2282 vim_free(newitem);
2283 clear_tv(rettv);
2284 }
2285 else
2286 {
2287 newitem->jq_no_callback = FALSE;
2288 *newitem->jq_value = *rettv;
2289 if (item == NULL)
2290 {
2291 /* append to the end */
2292 newitem->jq_prev = head->jq_prev;
2293 head->jq_prev = newitem;
2294 newitem->jq_next = NULL;
2295 if (newitem->jq_prev == NULL)
2296 head->jq_next = newitem;
2297 else
2298 newitem->jq_prev->jq_next = newitem;
2299 }
2300 else
2301 {
2302 /* append after "item" */
2303 newitem->jq_prev = item;
2304 newitem->jq_next = item->jq_next;
2305 item->jq_next = newitem;
2306 if (newitem->jq_next == NULL)
2307 head->jq_prev = newitem;
2308 else
2309 newitem->jq_next->jq_prev = newitem;
2310 }
2311 }
2312 }
2313}
2314
Bram Moolenaarece61b02016-02-20 21:39:05 +01002315#define CH_JSON_MAX_ARGS 4
2316
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002317/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002318 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002319 * "argv[0]" is the command string.
2320 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002321 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002322 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002323channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002324{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002325 char_u *cmd = argv[0].vval.v_string;
2326 char_u *arg;
2327 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002328
Bram Moolenaarece61b02016-02-20 21:39:05 +01002329 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002330 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002331 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002332 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002334 return;
2335 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002336 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002337 if (arg == NULL)
2338 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002339
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002340 if (STRCMP(cmd, "ex") == 0)
2341 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002342 int save_called_emsg = called_emsg;
2343
2344 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002345 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002346 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002347 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002348 --emsg_silent;
2349 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002350 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002351 (char *)get_vim_var_str(VV_ERRMSG));
2352 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002353 }
2354 else if (STRCMP(cmd, "normal") == 0)
2355 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002356 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002357
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002358 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002359 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002360 ea.arg = arg;
2361 ea.addr_count = 0;
2362 ea.forceit = TRUE; /* no mapping */
2363 ex_normal(&ea);
2364 }
2365 else if (STRCMP(cmd, "redraw") == 0)
2366 {
2367 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002368
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002369 ch_log(channel, "redraw");
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002370 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002371 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002372 ex_redraw(&ea);
2373 showruler(FALSE);
2374 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002375 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002376 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002377 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002378 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002379 int is_call = cmd[0] == 'c';
2380 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002381
Bram Moolenaarece61b02016-02-20 21:39:05 +01002382 if (argv[id_idx].v_type != VAR_UNKNOWN
2383 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002384 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002385 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002386 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002387 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002388 }
2389 else if (is_call && argv[2].v_type != VAR_LIST)
2390 {
2391 ch_error(channel, "third argument for call must be a list");
2392 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002393 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002394 }
2395 else
2396 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002397 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002398 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002399 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002400 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002401
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002402 /* Don't pollute the display with errors. */
2403 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002405 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002406 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002407 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002408 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002409 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002410 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002411 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002412 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2413 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002414 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002415
2416 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002417 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002418 int id = argv[id_idx].vval.v_number;
2419
Bram Moolenaar55fab432016-02-07 16:53:13 +01002420 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002421 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002422 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002423 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002424 /* If evaluation failed or the result can't be encoded
2425 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002426 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002427 err_tv.v_type = VAR_STRING;
2428 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002429 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002430 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002431 if (json != NULL)
2432 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002433 channel_send(channel,
2434 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002435 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002436 vim_free(json);
2437 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002438 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002439 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002440 if (tv == &res_tv)
2441 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002442 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002443 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002444 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002445 }
2446 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002447 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002448 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002449 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002450 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002451}
2452
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002453/*
2454 * Invoke the callback at "cbhead".
2455 * Does not redraw but sets channel_need_redraw.
2456 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002457 static void
2458invoke_one_time_callback(
2459 channel_T *channel,
2460 cbq_T *cbhead,
2461 cbq_T *item,
2462 typval_T *argv)
2463{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002464 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002465 (char *)item->cq_callback.cb_name);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002466 /* Remove the item from the list first, if the callback
2467 * invokes ch_close() the list will be cleared. */
2468 remove_cb_node(cbhead, item);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002469 invoke_callback(channel, &item->cq_callback, argv);
2470 free_callback(&item->cq_callback);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002471 vim_free(item);
2472}
2473
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002474 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002475append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002476{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002477 bufref_T save_curbuf = {NULL, 0, 0};
2478 win_T *save_curwin = NULL;
2479 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002480 linenr_T lnum = buffer->b_ml.ml_line_count;
2481 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002482 chanpart_T *ch_part = &channel->ch_part[part];
2483 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002484 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002485
2486 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2487 {
2488 if (!ch_part->ch_nomod_error)
2489 {
2490 ch_error(channel, "Buffer is not modifiable, cannot append");
2491 ch_part->ch_nomod_error = TRUE;
2492 }
2493 return;
2494 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002495
2496 /* If the buffer is also used as input insert above the last
2497 * line. Don't write these lines. */
2498 if (save_write_to)
2499 {
2500 --lnum;
2501 buffer->b_write_to_channel = FALSE;
2502 }
2503
2504 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002505 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002506
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002507 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002508
2509 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2510 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2511
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002512 u_sync(TRUE);
2513 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002514 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002515
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002516 if (empty)
2517 {
2518 /* The buffer is empty, replace the first (dummy) line. */
2519 ml_replace(lnum, msg, TRUE);
2520 lnum = 0;
2521 }
2522 else
2523 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002524 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002525
2526 /* Restore curbuf/curwin/curtab */
2527 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2528
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002529 if (ch_part->ch_nomodifiable)
2530 buffer->b_p_ma = FALSE;
2531 else
2532 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002533
2534 if (buffer->b_nwindows > 0)
2535 {
2536 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002537
2538 FOR_ALL_WINDOWS(wp)
2539 {
2540 if (wp->w_buffer == buffer
2541 && (save_write_to
2542 ? wp->w_cursor.lnum == lnum + 1
2543 : (wp->w_cursor.lnum == lnum
2544 && wp->w_cursor.col == 0)))
2545 {
2546 ++wp->w_cursor.lnum;
2547 save_curwin = curwin;
2548 curwin = wp;
2549 curbuf = curwin->w_buffer;
2550 scroll_cursor_bot(0, FALSE);
2551 curwin = save_curwin;
2552 curbuf = curwin->w_buffer;
2553 }
2554 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002555 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002556 channel_need_redraw = TRUE;
2557 }
2558
2559 if (save_write_to)
2560 {
2561 channel_T *ch;
2562
2563 /* Find channels reading from this buffer and adjust their
2564 * next-to-read line number. */
2565 buffer->b_write_to_channel = TRUE;
2566 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2567 {
2568 chanpart_T *in_part = &ch->ch_part[PART_IN];
2569
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002570 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002571 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2572 }
2573 }
2574}
2575
Bram Moolenaar437905c2016-04-26 19:01:05 +02002576 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002577drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002578{
2579 char_u *msg;
2580
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002581 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002582 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002583 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002584 vim_free(msg);
2585 }
2586}
2587
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002588/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002589 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002590 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002591 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002592 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002593 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002594may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002595{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002596 char_u *msg = NULL;
2597 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002598 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002599 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002600 chanpart_T *ch_part = &channel->ch_part[part];
2601 ch_mode_T ch_mode = ch_part->ch_mode;
2602 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002603 cbq_T *cbitem;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002604 callback_T *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002605 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002606 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002607
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002608 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002609 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002610 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002611
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002612 /* Use a message-specific callback, part callback or channel callback */
2613 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2614 if (cbitem->cq_seq_nr == 0)
2615 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002616 if (cbitem != NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002617 callback = &cbitem->cq_callback;
2618 else if (ch_part->ch_callback.cb_name != NULL)
2619 callback = &ch_part->ch_callback;
2620 else if (channel->ch_callback.cb_name != NULL)
2621 callback = &channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002622
Bram Moolenaarec68a992016-10-03 21:37:41 +02002623 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002624 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2625 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002626 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002627 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002628 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002629 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002630 buffer = NULL;
2631 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002632
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002633 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002634 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002635 listitem_T *item;
2636 int argc = 0;
2637
Bram Moolenaard7ece102016-02-02 23:23:02 +01002638 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002639 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002640 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002641 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002642 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002643 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002644 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002645 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002646
Bram Moolenaarece61b02016-02-20 21:39:05 +01002647 for (item = listtv->vval.v_list->lv_first;
2648 item != NULL && argc < CH_JSON_MAX_ARGS;
2649 item = item->li_next)
2650 argv[argc++] = item->li_tv;
2651 while (argc < CH_JSON_MAX_ARGS)
2652 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002653
Bram Moolenaarece61b02016-02-20 21:39:05 +01002654 if (argv[0].v_type == VAR_STRING)
2655 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002656 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002657 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002658 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002659 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002660 }
2661
Bram Moolenaarece61b02016-02-20 21:39:05 +01002662 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002663 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002664 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002665 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002666 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002667 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002668 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002669 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002670 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002671 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002672 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002673 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002674 return FALSE;
2675 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002676 else
2677 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002678 /* If there is no callback or buffer drop the message. */
2679 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002680 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002681 /* If there is a close callback it may use ch_read() to get the
2682 * messages. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002683 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002684 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002685 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002686 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002687
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002688 if (ch_mode == MODE_NL)
2689 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002690 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002691 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002692 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002693
2694 /* See if we have a message ending in NL in the first buffer. If
2695 * not try to concatenate the first and the second buffer. */
2696 while (TRUE)
2697 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002698 node = channel_peek(channel, part);
2699 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002700 if (nl != NULL)
2701 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002702 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002703 {
2704 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2705 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002706 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002707 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002708 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002709 buf = node->rq_buffer;
2710
Bram Moolenaar772153f2019-03-04 12:09:49 +01002711 // Convert NUL to NL, the internal representation.
2712 for (p = buf; (nl == NULL || p < nl)
2713 && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002714 if (*p == NUL)
2715 *p = NL;
2716
Bram Moolenaar772153f2019-03-04 12:09:49 +01002717 if (nl == NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002718 {
Bram Moolenaar772153f2019-03-04 12:09:49 +01002719 // get the whole buffer, drop the NL
2720 msg = channel_get(channel, part, NULL);
2721 }
2722 else if (nl + 1 == buf + node->rq_buflen)
2723 {
2724 // get the whole buffer
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002725 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002726 *nl = NUL;
2727 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002728 else
2729 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002730 /* Copy the message into allocated memory (excluding the NL)
2731 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002732 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002733 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002734 }
2735 }
2736 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002737 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002738 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002739 * get everything we have.
2740 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002741 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002742 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002743
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002744 if (msg == NULL)
2745 return FALSE; /* out of memory (and avoids Coverity warning) */
2746
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002747 argv[1].v_type = VAR_STRING;
2748 argv[1].vval.v_string = msg;
2749 }
2750
Bram Moolenaara07fec92016-02-05 21:04:08 +01002751 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002752 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002753 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002754
Bram Moolenaar958dc692016-12-01 15:34:12 +01002755 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002756 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002757 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002758 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002759 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002760 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002761 break;
2762 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002763 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002764 {
2765 if (channel->ch_drop_never)
2766 {
2767 /* message must be read with ch_read() */
2768 channel_push_json(channel, part, listtv);
2769 listtv = NULL;
2770 }
2771 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002772 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002773 seq_nr);
2774 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002775 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002776 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002777 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002778 if (buffer != NULL)
2779 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002780 if (msg == NULL)
2781 /* JSON or JS mode: re-encode the message. */
2782 msg = json_encode(listtv, ch_mode);
2783 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002784 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002785#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002786 if (buffer->b_term != NULL)
2787 write_to_term(buffer, msg, channel);
2788 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002789#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002790 append_to_buffer(buffer, msg, channel, part);
2791 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002792 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002793
Bram Moolenaar187db502016-02-27 14:44:26 +01002794 if (callback != NULL)
2795 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002796 if (cbitem != NULL)
2797 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2798 else
2799 {
2800 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002801 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002802 (char *)callback->cb_name);
2803 invoke_callback(channel, callback, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002804 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002805 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002806 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002807 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002808 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002809
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002810 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002811 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002812 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002813
2814 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002815}
2816
Bram Moolenaar113e1072019-01-20 15:30:40 +01002817#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002818/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002819 * Return TRUE when channel "channel" is open for writing to.
2820 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002821 */
2822 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002823channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002824{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002825 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002826 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002827}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002828#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002829
2830/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002831 * Return TRUE when channel "channel" is open for reading or writing.
2832 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002833 */
2834 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002835channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002836{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002837 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002838 || channel->CH_IN_FD != INVALID_FD
2839 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002840 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002841}
2842
2843/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002844 * Return TRUE if "channel" has JSON or other typeahead.
2845 */
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002846 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002847channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002848{
2849 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2850
2851 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2852 {
2853 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002854
Bram Moolenaar4340fc92019-06-28 22:06:49 +02002855 if (head->jq_next == NULL)
2856 // Parse json from readahead, there might be a complete message to
2857 // process.
2858 channel_parse_json(channel, part);
2859
2860 return head->jq_next != NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002861 }
2862 return channel_peek(channel, part) != NULL;
2863}
2864
2865/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002866 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002867 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002868 */
2869 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002870channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002871{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002872 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002873 int has_readahead = FALSE;
2874
Bram Moolenaar77073442016-02-13 23:23:53 +01002875 if (channel == NULL)
2876 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002877 if (req_part == PART_OUT)
2878 {
2879 if (channel->CH_OUT_FD != INVALID_FD)
2880 return "open";
2881 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002882 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002883 }
2884 else if (req_part == PART_ERR)
2885 {
2886 if (channel->CH_ERR_FD != INVALID_FD)
2887 return "open";
2888 if (channel_has_readahead(channel, PART_ERR))
2889 has_readahead = TRUE;
2890 }
2891 else
2892 {
2893 if (channel_is_open(channel))
2894 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002895 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002896 if (channel_has_readahead(channel, part))
2897 {
2898 has_readahead = TRUE;
2899 break;
2900 }
2901 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002902
2903 if (has_readahead)
2904 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002905 return "closed";
2906}
2907
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002908 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002909channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002910{
2911 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002912 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002913 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002914 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002915 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002916
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002917 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002918 STRCAT(namebuf, "_");
2919 tail = STRLEN(namebuf);
2920
2921 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002922 if (chanpart->ch_fd != INVALID_FD)
2923 status = "open";
2924 else if (channel_has_readahead(channel, part))
2925 status = "buffered";
2926 else
2927 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002928 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002929
2930 STRCPY(namebuf + tail, "mode");
2931 switch (chanpart->ch_mode)
2932 {
2933 case MODE_NL: s = "NL"; break;
2934 case MODE_RAW: s = "RAW"; break;
2935 case MODE_JSON: s = "JSON"; break;
2936 case MODE_JS: s = "JS"; break;
2937 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002938 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002939
2940 STRCPY(namebuf + tail, "io");
2941 if (part == PART_SOCK)
2942 s = "socket";
2943 else switch (chanpart->ch_io)
2944 {
2945 case JIO_NULL: s = "null"; break;
2946 case JIO_PIPE: s = "pipe"; break;
2947 case JIO_FILE: s = "file"; break;
2948 case JIO_BUFFER: s = "buffer"; break;
2949 case JIO_OUT: s = "out"; break;
2950 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002951 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002952
2953 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002954 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002955}
2956
2957 void
2958channel_info(channel_T *channel, dict_T *dict)
2959{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002960 dict_add_number(dict, "id", channel->ch_id);
2961 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002962
2963 if (channel->ch_hostname != NULL)
2964 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002965 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
2966 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002967 channel_part_info(channel, dict, "sock", PART_SOCK);
2968 }
2969 else
2970 {
2971 channel_part_info(channel, dict, "out", PART_OUT);
2972 channel_part_info(channel, dict, "err", PART_ERR);
2973 channel_part_info(channel, dict, "in", PART_IN);
2974 }
2975}
2976
Bram Moolenaar77073442016-02-13 23:23:53 +01002977/*
2978 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01002979 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01002980 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01002981 */
2982 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01002983channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002984{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002985 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01002986
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002987#ifdef FEAT_GUI
2988 channel_gui_unregister(channel);
2989#endif
2990
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002991 ch_close_part(channel, PART_SOCK);
2992 ch_close_part(channel, PART_IN);
2993 ch_close_part(channel, PART_OUT);
2994 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01002995
Bram Moolenaara9f02812017-08-05 17:40:38 +02002996 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002997 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02002998 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002999
Bram Moolenaara9f02812017-08-05 17:40:38 +02003000 /* Invoke callbacks and flush buffers before the close callback. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003001 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003002 ch_log(channel,
3003 "Invoking callbacks and flushing buffers before closing");
3004 for (part = PART_SOCK; part < PART_IN; ++part)
3005 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003006 if (channel->ch_close_cb.cb_name != NULL
Bram Moolenaara9f02812017-08-05 17:40:38 +02003007 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3008 {
3009 /* Increment the refcount to avoid the channel being freed
3010 * halfway. */
3011 ++channel->ch_refcount;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003012 if (channel->ch_close_cb.cb_name == NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003013 ch_log(channel, "flushing %s buffers before closing",
3014 part_names[part]);
3015 while (may_invoke_callback(channel, part))
3016 ;
3017 --channel->ch_refcount;
3018 }
3019 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003020
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003021 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003022 {
3023 typval_T argv[1];
3024 typval_T rettv;
3025 int dummy;
3026
3027 /* Increment the refcount to avoid the channel being freed
3028 * halfway. */
3029 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003030 ch_log(channel, "Invoking close callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003031 (char *)channel->ch_close_cb.cb_name);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003032 argv[0].v_type = VAR_CHANNEL;
3033 argv[0].vval.v_channel = channel;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003034 call_callback(&channel->ch_close_cb, -1,
3035 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003036 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003037 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003038
Bram Moolenaara9f02812017-08-05 17:40:38 +02003039 /* the callback is only called once */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003040 free_callback(&channel->ch_close_cb);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003041
Bram Moolenaara9f02812017-08-05 17:40:38 +02003042 if (channel_need_redraw)
3043 {
3044 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003045 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003046 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003047
Bram Moolenaara9f02812017-08-05 17:40:38 +02003048 if (!channel->ch_drop_never)
3049 /* any remaining messages are useless now */
3050 for (part = PART_SOCK; part < PART_IN; ++part)
3051 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003052
3053 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003054 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003055 }
3056
3057 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003058
3059#ifdef FEAT_TERMINAL
3060 term_channel_closed(channel);
3061#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003062}
3063
Bram Moolenaard04a0202016-01-26 23:30:18 +01003064/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003065 * Close the "in" part channel "channel".
3066 */
3067 void
3068channel_close_in(channel_T *channel)
3069{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003070 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003071}
3072
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003073 static void
3074remove_from_writeque(writeq_T *wq, writeq_T *entry)
3075{
3076 ga_clear(&entry->wq_ga);
3077 wq->wq_next = entry->wq_next;
3078 if (wq->wq_next == NULL)
3079 wq->wq_prev = NULL;
3080 else
3081 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003082 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003083}
3084
Bram Moolenaar0874a832016-09-01 15:11:51 +02003085/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003086 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003087 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003088 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003089channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003090{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003091 chanpart_T *ch_part = &channel->ch_part[part];
3092 jsonq_T *json_head = &ch_part->ch_json_head;
3093 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003094
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003095 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003096 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003097
3098 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003099 {
3100 cbq_T *node = cb_head->cq_next;
3101
3102 remove_cb_node(cb_head, node);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003103 free_callback(&node->cq_callback);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003104 vim_free(node);
3105 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003106
3107 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003108 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003109 free_tv(json_head->jq_next->jq_value);
3110 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003111 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003112
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003113 free_callback(&ch_part->ch_callback);
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003114 ga_clear(&ch_part->ch_block_ids);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003115
3116 while (ch_part->ch_writeque.wq_next != NULL)
3117 remove_from_writeque(&ch_part->ch_writeque,
3118 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003119}
3120
3121/*
3122 * Clear all the read buffers on "channel".
3123 */
3124 void
3125channel_clear(channel_T *channel)
3126{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003127 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003128 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003129 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003130 channel_clear_one(channel, PART_OUT);
3131 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003132 channel_clear_one(channel, PART_IN);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003133 free_callback(&channel->ch_callback);
3134 free_callback(&channel->ch_close_cb);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003135}
3136
Bram Moolenaar77073442016-02-13 23:23:53 +01003137#if defined(EXITFREE) || defined(PROTO)
3138 void
3139channel_free_all(void)
3140{
3141 channel_T *channel;
3142
Bram Moolenaard6051b52016-02-28 15:49:03 +01003143 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003144 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3145 channel_clear(channel);
3146}
3147#endif
3148
3149
Bram Moolenaar715d2852016-04-30 17:06:31 +02003150/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003151#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003152
3153/* Buffer size for reading incoming messages. */
3154#define MAXMSGSIZE 4096
3155
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003156#if defined(HAVE_SELECT)
3157/*
3158 * Add write fds where we are waiting for writing to be possible.
3159 */
3160 static int
3161channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3162{
3163 int maxfd = maxfd_arg;
3164 channel_T *ch;
3165
3166 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3167 {
3168 chanpart_T *in_part = &ch->ch_part[PART_IN];
3169
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003170 if (in_part->ch_fd != INVALID_FD
3171 && (in_part->ch_bufref.br_buf != NULL
3172 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003173 {
3174 FD_SET((int)in_part->ch_fd, wfds);
3175 if ((int)in_part->ch_fd >= maxfd)
3176 maxfd = (int)in_part->ch_fd + 1;
3177 }
3178 }
3179 return maxfd;
3180}
3181#else
3182/*
3183 * Add write fds where we are waiting for writing to be possible.
3184 */
3185 static int
3186channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3187{
3188 int nfd = nfd_in;
3189 channel_T *ch;
3190
3191 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3192 {
3193 chanpart_T *in_part = &ch->ch_part[PART_IN];
3194
Bram Moolenaar683b7962017-08-19 15:51:59 +02003195 if (in_part->ch_fd != INVALID_FD
3196 && (in_part->ch_bufref.br_buf != NULL
3197 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003198 {
3199 in_part->ch_poll_idx = nfd;
3200 fds[nfd].fd = in_part->ch_fd;
3201 fds[nfd].events = POLLOUT;
3202 ++nfd;
3203 }
3204 else
3205 in_part->ch_poll_idx = -1;
3206 }
3207 return nfd;
3208}
3209#endif
3210
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003211typedef enum {
3212 CW_READY,
3213 CW_NOT_READY,
3214 CW_ERROR
3215} channel_wait_result;
3216
Bram Moolenaard04a0202016-01-26 23:30:18 +01003217/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003218 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003219 * Return CW_READY when there is something to read.
3220 * Return CW_NOT_READY when there is nothing to read.
3221 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003222 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003223 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003224channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003225{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003226 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003227 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003228
Bram Moolenaar4f974752019-02-17 17:44:42 +01003229# ifdef MSWIN
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003230 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003231 {
3232 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003233 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003234 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003235 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003236
3237 /* reading from a pipe, not a socket */
3238 while (TRUE)
3239 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003240 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3241
3242 if (r && nread > 0)
3243 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003244
3245 if (channel->ch_named_pipe)
3246 {
3247 DisconnectNamedPipe((HANDLE)fd);
3248 ConnectNamedPipe((HANDLE)fd, NULL);
3249 }
3250 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003251 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003252
3253 /* perhaps write some buffer lines */
3254 channel_write_any_lines();
3255
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003256 sleep_time = deadline - GetTickCount();
3257 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003258 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003259 /* Wait for a little while. Very short at first, up to 10 msec
3260 * after looping a few times. */
3261 if (sleep_time > delay)
3262 sleep_time = delay;
3263 Sleep(sleep_time);
3264 delay = delay * 2;
3265 if (delay > 10)
3266 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003267 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003268 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003269 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003270#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003271 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003272#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003273 struct timeval tval;
3274 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003275 fd_set wfds;
3276 int ret;
3277 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003278
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003279 tval.tv_sec = timeout / 1000;
3280 tval.tv_usec = (timeout % 1000) * 1000;
3281 for (;;)
3282 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003283 FD_ZERO(&rfds);
3284 FD_SET((int)fd, &rfds);
3285
3286 /* Write lines to a pipe when a pipe can be written to. Need to
3287 * set this every time, some buffers may be done. */
3288 maxfd = (int)fd + 1;
3289 FD_ZERO(&wfds);
3290 maxfd = channel_fill_wfds(maxfd, &wfds);
3291
3292 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003293# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003294 SOCK_ERRNO;
3295 if (ret == -1 && errno == EINTR)
3296 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003297# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003298 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003299 {
3300 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003301 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003302 channel_write_any_lines();
3303 continue;
3304 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003305 break;
3306 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003307#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003308 for (;;)
3309 {
3310 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3311 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003312
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003313 fds[0].fd = fd;
3314 fds[0].events = POLLIN;
3315 nfd = channel_fill_poll_write(nfd, fds);
3316 if (poll(fds, nfd, timeout) > 0)
3317 {
3318 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003319 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003320 channel_write_any_lines();
3321 continue;
3322 }
3323 break;
3324 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003325#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003326 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003327 return CW_NOT_READY;
3328}
3329
3330 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003331ch_close_part_on_error(
3332 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003333{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003334 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003335
3336 if (is_err)
3337 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003338 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003339 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003340 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003341
3342 /* Queue a "DETACH" netbeans message in the command queue in order to
3343 * terminate the netbeans session later. Do not end the session here
3344 * directly as we may be running in the context of a call to
3345 * netbeans_parse_messages():
3346 * netbeans_parse_messages
3347 * -> autocmd triggered while processing the netbeans cmd
3348 * -> ui_breakcheck
3349 * -> gui event loop or select loop
3350 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003351 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003352 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003353 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003354 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003355 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3356
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003357 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003358 * close the channel yet, there may be something to read on another part.
3359 * When stdout and stderr use the same FD we get the error only on one of
3360 * them, also close the other. */
3361 if (part == PART_OUT || part == PART_ERR)
3362 {
3363 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3364
3365 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3366 ch_close_part(channel, other);
3367 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003368 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003369
3370#ifdef FEAT_GUI
3371 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003372 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003373#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003374}
3375
3376 static void
3377channel_close_now(channel_T *channel)
3378{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003379 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003380 if (channel->ch_nb_close_cb != NULL)
3381 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003382 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003383}
3384
3385/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003386 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003387 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003388 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003389 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003390 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003391channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003392{
3393 static char_u *buf = NULL;
3394 int len = 0;
3395 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003396 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003397 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003398
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003399 fd = channel->ch_part[part].ch_fd;
3400 if (fd == INVALID_FD)
3401 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003402 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003403 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003404 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003405 }
3406 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003407
3408 /* Allocate a buffer to read into. */
3409 if (buf == NULL)
3410 {
3411 buf = alloc(MAXMSGSIZE);
3412 if (buf == NULL)
3413 return; /* out of memory! */
3414 }
3415
3416 /* Keep on reading for as long as there is something to read.
3417 * Use select() or poll() to avoid blocking on a message that is exactly
3418 * MAXMSGSIZE long. */
3419 for (;;)
3420 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003421 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003422 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003423 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003424 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003425 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003426 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003427 if (len <= 0)
3428 break; /* error or nothing more to read */
3429
3430 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003431 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003432 readlen += len;
3433 if (len < MAXMSGSIZE)
3434 break; /* did read everything that's available */
3435 }
3436
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003437 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003438 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003439 {
3440 if (!channel->ch_keep_open)
3441 ch_close_part_on_error(channel, part, (len < 0), func);
3442 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003443#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003444 else if (CH_HAS_GUI && gtk_main_level() > 0)
3445 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003446 gtk_main_quit();
3447#endif
3448}
3449
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003450/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003451 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003452 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003453 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003454 * Returns what was read in allocated memory.
3455 * Returns NULL in case of error or timeout.
3456 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003457 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003458channel_read_block(
3459 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003460{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003461 char_u *buf;
3462 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003463 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003464 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003465 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003466 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003467
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003468 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003469 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003470
3471 while (TRUE)
3472 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003473 node = channel_peek(channel, part);
3474 if (node != NULL)
3475 {
3476 if (mode == MODE_RAW || (mode == MODE_NL
3477 && channel_first_nl(node) != NULL))
3478 /* got a complete message */
3479 break;
3480 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3481 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003482 /* If not blocking or nothing more is coming then return what we
3483 * have. */
3484 if (raw || fd == INVALID_FD)
3485 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003486 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003487
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003488 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003489 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003490 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003491 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003492 {
3493 ch_log(channel, "Timed out");
3494 return NULL;
3495 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003497 }
3498
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003499 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003500 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003501 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003502 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003503 }
3504 else
3505 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003506 char_u *p;
3507
3508 buf = node->rq_buffer;
3509 nl = channel_first_nl(node);
3510
3511 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003512 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003513 if (*p == NUL)
3514 *p = NL;
3515
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003516 if (nl == NULL)
3517 {
3518 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003519 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003520 }
3521 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003522 {
3523 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003524 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003525 *nl = NUL;
3526 }
3527 else
3528 {
3529 /* Copy the message into allocated memory and remove it from the
3530 * buffer. */
3531 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003532 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003533 }
3534 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003535 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003536 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003537 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003538}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003539
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003540/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003541 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003542 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003543 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003544 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003545 * In corner cases this can be called recursively, that is why ch_block_ids is
3546 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003547 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003548 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003549channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003550 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003551 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003552 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003553 int id,
3554 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003555{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003556 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003557 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003558 int timeout;
3559 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaard7ece102016-02-02 23:23:02 +01003560
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003561 ch_log(channel, "Blocking read JSON for id %d", id);
3562 if (id >= 0)
3563 channel_add_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003564 for (;;)
3565 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003566 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003567
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003568 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003569 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003570 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003571 if (id >= 0)
3572 channel_remove_block_id(chanpart, id);
3573 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003574 return OK;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003575 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003576
Bram Moolenaard7ece102016-02-02 23:23:02 +01003577 if (!more)
3578 {
3579 /* Handle any other messages in the queue. If done some more
3580 * messages may have arrived. */
3581 if (channel_parse_messages())
3582 continue;
3583
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003584 /* Wait for up to the timeout. If there was an incomplete message
3585 * use the deadline for that. */
3586 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003587 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003588 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003589#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003590 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3591#else
3592 {
3593 struct timeval now_tv;
3594
3595 gettimeofday(&now_tv, NULL);
3596 timeout = (chanpart->ch_deadline.tv_sec
3597 - now_tv.tv_sec) * 1000
3598 + (chanpart->ch_deadline.tv_usec
3599 - now_tv.tv_usec) / 1000
3600 + 1;
3601 }
3602#endif
3603 if (timeout < 0)
3604 {
3605 /* Something went wrong, channel_parse_json() didn't
3606 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003607 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003608 timeout = timeout_arg;
3609 }
3610 else if (timeout > timeout_arg)
3611 timeout = timeout_arg;
3612 }
3613 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003614 if (fd == INVALID_FD
3615 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003616 {
3617 if (timeout == timeout_arg)
3618 {
3619 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003620 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003621 break;
3622 }
3623 }
3624 else
3625 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003626 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003627 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003628 if (id >= 0)
3629 channel_remove_block_id(chanpart, id);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003630 return FAIL;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003631}
3632
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003633/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003634 * Get the channel from the argument.
3635 * Returns NULL if the handle is invalid.
3636 * When "check_open" is TRUE check that the channel can be used.
3637 * When "reading" is TRUE "check_open" considers typeahead useful.
3638 * "part" is used to check typeahead, when PART_COUNT use the default part.
3639 */
3640 static channel_T *
3641get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3642{
3643 channel_T *channel = NULL;
3644 int has_readahead = FALSE;
3645
3646 if (tv->v_type == VAR_JOB)
3647 {
3648 if (tv->vval.v_job != NULL)
3649 channel = tv->vval.v_job->jv_channel;
3650 }
3651 else if (tv->v_type == VAR_CHANNEL)
3652 {
3653 channel = tv->vval.v_channel;
3654 }
3655 else
3656 {
3657 semsg(_(e_invarg2), tv_get_string(tv));
3658 return NULL;
3659 }
3660 if (channel != NULL && reading)
3661 has_readahead = channel_has_readahead(channel,
3662 part != PART_COUNT ? part : channel_part_read(channel));
3663
3664 if (check_open && (channel == NULL || (!channel_is_open(channel)
3665 && !(reading && has_readahead))))
3666 {
3667 emsg(_("E906: not an open channel"));
3668 return NULL;
3669 }
3670 return channel;
3671}
3672
3673/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003674 * Common for ch_read() and ch_readraw().
3675 */
3676 void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003677common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003678{
3679 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003680 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003681 jobopt_T opt;
3682 int mode;
3683 int timeout;
3684 int id = -1;
3685 typval_T *listtv = NULL;
3686
3687 /* return an empty string by default */
3688 rettv->v_type = VAR_STRING;
3689 rettv->vval.v_string = NULL;
3690
3691 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003692 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003693 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003694 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003695
Bram Moolenaar437905c2016-04-26 19:01:05 +02003696 if (opt.jo_set & JO_PART)
3697 part = opt.jo_part;
3698 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003699 if (channel != NULL)
3700 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003701 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003702 part = channel_part_read(channel);
3703 mode = channel_get_mode(channel, part);
3704 timeout = channel_get_timeout(channel, part);
3705 if (opt.jo_set & JO_TIMEOUT)
3706 timeout = opt.jo_timeout;
3707
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003708 if (blob)
3709 {
3710 int outlen = 0;
3711 char_u *p = channel_read_block(channel, part,
3712 timeout, TRUE, &outlen);
3713 if (p != NULL)
3714 {
3715 blob_T *b = blob_alloc();
3716
3717 if (b != NULL)
3718 {
3719 b->bv_ga.ga_len = outlen;
3720 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3721 blob_free(b);
3722 else
3723 {
3724 memcpy(b->bv_ga.ga_data, p, outlen);
3725 rettv_blob_set(rettv, b);
3726 }
3727 }
3728 vim_free(p);
3729 }
3730 }
3731 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003732 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003733 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003734 else
3735 {
3736 if (opt.jo_set & JO_ID)
3737 id = opt.jo_id;
3738 channel_read_json_block(channel, part, timeout, id, &listtv);
3739 if (listtv != NULL)
3740 {
3741 *rettv = *listtv;
3742 vim_free(listtv);
3743 }
3744 else
3745 {
3746 rettv->v_type = VAR_SPECIAL;
3747 rettv->vval.v_number = VVAL_NONE;
3748 }
3749 }
3750 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003751
3752theend:
3753 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003754}
3755
Bram Moolenaar4f974752019-02-17 17:44:42 +01003756# if defined(MSWIN) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003757 || defined(PROTO)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003758/*
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003759 * Lookup the channel from the socket. Set "partp" to the fd index.
Bram Moolenaar77073442016-02-13 23:23:53 +01003760 * Returns NULL when the socket isn't found.
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003761 */
Bram Moolenaar77073442016-02-13 23:23:53 +01003762 channel_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003763channel_fd2channel(sock_T fd, ch_part_T *partp)
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003764{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003765 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003766 ch_part_T part;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003767
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003768 if (fd != INVALID_FD)
Bram Moolenaar77073442016-02-13 23:23:53 +01003769 for (channel = first_channel; channel != NULL;
3770 channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003771 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003772 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003773 if (channel->ch_part[part].ch_fd == fd)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003774 {
Bram Moolenaarfffd5562016-02-20 18:44:39 +01003775 *partp = part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003776 return channel;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01003777 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003778 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003779 return NULL;
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003780}
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003781# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003782
Bram Moolenaar4f974752019-02-17 17:44:42 +01003783# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003784/*
3785 * Check the channels for anything that is ready to be read.
3786 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003787 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003788 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003789 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003790channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003791{
3792 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003793 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003794 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003795
3796 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3797 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003798 if (only_keep_open && !channel->ch_keep_open)
3799 continue;
3800
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003801 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003802 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003803 {
3804 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003805 if (fd != INVALID_FD)
3806 {
3807 int r = channel_wait(channel, fd, 0);
3808
3809 if (r == CW_READY)
3810 channel_read(channel, part, "channel_handle_events");
3811 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003812 ch_close_part_on_error(channel, part, TRUE,
3813 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003814 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003815 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003816 }
3817}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003818# endif
3819
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003820# if defined(FEAT_GUI) || defined(PROTO)
3821/*
3822 * Return TRUE when there is any channel with a keep_open flag.
3823 */
3824 int
3825channel_any_keep_open()
3826{
3827 channel_T *channel;
3828
3829 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3830 if (channel->ch_keep_open)
3831 return TRUE;
3832 return FALSE;
3833}
3834# endif
3835
Bram Moolenaard04a0202016-01-26 23:30:18 +01003836/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003837 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003838 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003839 */
3840 void
3841channel_set_nonblock(channel_T *channel, ch_part_T part)
3842{
3843 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003844 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003845
3846 if (fd != INVALID_FD)
3847 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003848#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003849 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003850
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003851 ioctlsocket(fd, FIONBIO, &val);
3852#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003853 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003854#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003855 ch_part->ch_nonblocking = TRUE;
3856 }
3857}
3858
3859/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003860 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003861 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003862 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003863 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003864 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003865channel_send(
3866 channel_T *channel,
3867 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003868 char_u *buf_arg,
3869 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003870 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003871{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003872 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003873 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003874 chanpart_T *ch_part = &channel->ch_part[part];
3875 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003876
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003877 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003878 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003879 {
3880 if (!channel->ch_error && fun != NULL)
3881 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003882 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003883 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003884 }
3885 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003886 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003887 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003888
Bram Moolenaar0b146882018-09-06 16:27:24 +02003889 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3890 channel_set_nonblock(channel, part);
3891
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003892 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003893 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003894 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003895 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003896 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003897 fprintf(log_fd, "'\n");
3898 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003899 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003900 }
3901
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003902 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003903 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003904 writeq_T *wq = &ch_part->ch_writeque;
3905 char_u *buf;
3906 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003907
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003908 if (wq->wq_next != NULL)
3909 {
3910 /* first write what was queued */
3911 buf = wq->wq_next->wq_ga.ga_data;
3912 len = wq->wq_next->wq_ga.ga_len;
3913 did_use_queue = TRUE;
3914 }
3915 else
3916 {
3917 if (len_arg == 0)
3918 /* nothing to write, called from channel_select_check() */
3919 return OK;
3920 buf = buf_arg;
3921 len = len_arg;
3922 }
3923
3924 if (part == PART_SOCK)
3925 res = sock_write(fd, (char *)buf, len);
3926 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003927 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003928 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003929#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003930 if (channel->ch_named_pipe && res < 0)
3931 {
3932 DisconnectNamedPipe((HANDLE)fd);
3933 ConnectNamedPipe((HANDLE)fd, NULL);
3934 }
3935#endif
3936 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003937 if (res < 0 && (errno == EWOULDBLOCK
3938#ifdef EAGAIN
3939 || errno == EAGAIN
3940#endif
3941 ))
3942 res = 0; /* nothing got written */
3943
3944 if (res >= 0 && ch_part->ch_nonblocking)
3945 {
3946 writeq_T *entry = wq->wq_next;
3947
3948 if (did_use_queue)
3949 ch_log(channel, "Sent %d bytes now", res);
3950 if (res == len)
3951 {
3952 /* Wrote all the buf[len] bytes. */
3953 if (entry != NULL)
3954 {
3955 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003956 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003957 continue;
3958 }
3959 if (did_use_queue)
3960 ch_log(channel, "Write queue empty");
3961 }
3962 else
3963 {
3964 /* Wrote only buf[res] bytes, can't write more now. */
3965 if (entry != NULL)
3966 {
3967 if (res > 0)
3968 {
3969 /* Remove the bytes that were written. */
3970 mch_memmove(entry->wq_ga.ga_data,
3971 (char *)entry->wq_ga.ga_data + res,
3972 len - res);
3973 entry->wq_ga.ga_len -= res;
3974 }
3975 buf = buf_arg;
3976 len = len_arg;
3977 }
3978 else
3979 {
3980 buf += res;
3981 len -= res;
3982 }
3983 ch_log(channel, "Adding %d bytes to the write queue", len);
3984
3985 /* Append the not written bytes of the argument to the write
3986 * buffer. Limit entries to 4000 bytes. */
3987 if (wq->wq_prev != NULL
3988 && wq->wq_prev->wq_ga.ga_len + len < 4000)
3989 {
3990 writeq_T *last = wq->wq_prev;
3991
3992 /* append to the last entry */
Bram Moolenaar0d071552019-07-08 22:04:03 +02003993 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003994 {
3995 mch_memmove((char *)last->wq_ga.ga_data
3996 + last->wq_ga.ga_len,
3997 buf, len);
3998 last->wq_ga.ga_len += len;
3999 }
4000 }
4001 else
4002 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004003 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004004
4005 if (last != NULL)
4006 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004007 last->wq_prev = wq->wq_prev;
4008 last->wq_next = NULL;
4009 if (wq->wq_prev == NULL)
4010 wq->wq_next = last;
4011 else
4012 wq->wq_prev->wq_next = last;
4013 wq->wq_prev = last;
4014 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004015 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004016 {
4017 mch_memmove(last->wq_ga.ga_data, buf, len);
4018 last->wq_ga.ga_len = len;
4019 }
4020 }
4021 }
4022 }
4023 }
4024 else if (res != len)
4025 {
4026 if (!channel->ch_error && fun != NULL)
4027 {
4028 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004029 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004030 }
4031 channel->ch_error = TRUE;
4032 return FAIL;
4033 }
4034
4035 channel->ch_error = FALSE;
4036 return OK;
4037 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004038}
4039
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004040/*
4041 * Common for "ch_sendexpr()" and "ch_sendraw()".
4042 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004043 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004044 * Otherwise returns NULL.
4045 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004046 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004047send_common(
4048 typval_T *argvars,
4049 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004050 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004051 int id,
4052 int eval,
4053 jobopt_T *opt,
4054 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004055 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004056{
4057 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004058 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004059
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004060 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004061 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062 if (channel == NULL)
4063 return NULL;
4064 part_send = channel_part_send(channel);
4065 *part_read = channel_part_read(channel);
4066
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004067 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004068 return NULL;
4069
4070 /* Set the callback. An empty callback means no callback and not reading
4071 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4072 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004073 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004074 {
4075 if (eval)
4076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004077 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004078 return NULL;
4079 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004080 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004081 }
4082
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004083 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004084 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004085 return channel;
4086 return NULL;
4087}
4088
4089/*
4090 * common for "ch_evalexpr()" and "ch_sendexpr()"
4091 */
4092 void
4093ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4094{
4095 char_u *text;
4096 typval_T *listtv;
4097 channel_T *channel;
4098 int id;
4099 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004100 ch_part_T part_send;
4101 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004102 jobopt_T opt;
4103 int timeout;
4104
4105 /* return an empty string by default */
4106 rettv->v_type = VAR_STRING;
4107 rettv->vval.v_string = NULL;
4108
Bram Moolenaar437905c2016-04-26 19:01:05 +02004109 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004110 if (channel == NULL)
4111 return;
4112 part_send = channel_part_send(channel);
4113
4114 ch_mode = channel_get_mode(channel, part_send);
4115 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004117 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004118 return;
4119 }
4120
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004121 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004122 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004123 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004124 if (text == NULL)
4125 return;
4126
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004127 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004128 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4129 vim_free(text);
4130 if (channel != NULL && eval)
4131 {
4132 if (opt.jo_set & JO_TIMEOUT)
4133 timeout = opt.jo_timeout;
4134 else
4135 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004136 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4137 == OK)
4138 {
4139 list_T *list = listtv->vval.v_list;
4140
4141 /* Move the item from the list and then change the type to
4142 * avoid the value being freed. */
4143 *rettv = list->lv_last->li_tv;
4144 list->lv_last->li_tv.v_type = VAR_NUMBER;
4145 free_tv(listtv);
4146 }
4147 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004148 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004149}
4150
4151/*
4152 * common for "ch_evalraw()" and "ch_sendraw()"
4153 */
4154 void
4155ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4156{
4157 char_u buf[NUMBUFLEN];
4158 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004159 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004160 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004161 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004162 jobopt_T opt;
4163 int timeout;
4164
4165 /* return an empty string by default */
4166 rettv->v_type = VAR_STRING;
4167 rettv->vval.v_string = NULL;
4168
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004169 if (argvars[1].v_type == VAR_BLOB)
4170 {
4171 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4172 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4173 }
4174 else
4175 {
4176 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004177 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004178 }
4179 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004180 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4181 if (channel != NULL && eval)
4182 {
4183 if (opt.jo_set & JO_TIMEOUT)
4184 timeout = opt.jo_timeout;
4185 else
4186 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004187 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004188 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004189 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004190 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004191}
4192
Bram Moolenaarf3360612017-10-01 16:21:31 +02004193# define KEEP_OPEN_TIME 20 /* msec */
4194
Bram Moolenaard04a0202016-01-26 23:30:18 +01004195# if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004196/*
4197 * Add open channels to the poll struct.
4198 * Return the adjusted struct index.
4199 * The type of "fds" is hidden to avoid problems with the function proto.
4200 */
4201 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004202channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004203{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004204 int nfd = nfd_in;
4205 channel_T *channel;
4206 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004207 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004208
Bram Moolenaar77073442016-02-13 23:23:53 +01004209 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004210 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004211 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004212 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004213 chanpart_T *ch_part = &channel->ch_part[part];
4214
4215 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004216 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004217 if (channel->ch_keep_open)
4218 {
4219 /* For unknown reason poll() returns immediately for a
4220 * keep-open channel. Instead of adding it to the fds add
4221 * a short timeout and check, like polling. */
4222 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4223 *towait = KEEP_OPEN_TIME;
4224 }
4225 else
4226 {
4227 ch_part->ch_poll_idx = nfd;
4228 fds[nfd].fd = ch_part->ch_fd;
4229 fds[nfd].events = POLLIN;
4230 nfd++;
4231 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004232 }
4233 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004234 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004235 }
4236 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004237
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004238 nfd = channel_fill_poll_write(nfd, fds);
4239
Bram Moolenaare0874f82016-01-24 20:36:41 +01004240 return nfd;
4241}
4242
4243/*
4244 * The type of "fds" is hidden to avoid problems with the function proto.
4245 */
4246 int
4247channel_poll_check(int ret_in, void *fds_in)
4248{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004249 int ret = ret_in;
4250 channel_T *channel;
4251 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004252 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004253 int idx;
4254 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004255
Bram Moolenaar77073442016-02-13 23:23:53 +01004256 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004257 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004258 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004259 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004260 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004261
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004262 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004263 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004264 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004265 --ret;
4266 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004267 else if (channel->ch_part[part].ch_fd != INVALID_FD
4268 && channel->ch_keep_open)
4269 {
4270 /* polling a keep-open channel */
4271 channel_read(channel, part, "channel_poll_check_keep_open");
4272 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004273 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004274
4275 in_part = &channel->ch_part[PART_IN];
4276 idx = in_part->ch_poll_idx;
4277 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4278 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004279 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004280 --ret;
4281 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004282 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004283
4284 return ret;
4285}
Bram Moolenaard04a0202016-01-26 23:30:18 +01004286# endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004287
Bram Moolenaar4f974752019-02-17 17:44:42 +01004288# if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004289
Bram Moolenaare0874f82016-01-24 20:36:41 +01004290/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004291 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004292 */
4293 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004294channel_select_setup(
4295 int maxfd_in,
4296 void *rfds_in,
4297 void *wfds_in,
4298 struct timeval *tv,
4299 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004300{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004301 int maxfd = maxfd_in;
4302 channel_T *channel;
4303 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004304 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004305 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004306
Bram Moolenaar77073442016-02-13 23:23:53 +01004307 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004308 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004309 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004310 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004311 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004312
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004313 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004314 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004315 if (channel->ch_keep_open)
4316 {
4317 /* For unknown reason select() returns immediately for a
4318 * keep-open channel. Instead of adding it to the rfds add
4319 * a short timeout and check, like polling. */
4320 if (*tvp == NULL || tv->tv_sec > 0
4321 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4322 {
4323 *tvp = tv;
4324 tv->tv_sec = 0;
4325 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4326 }
4327 }
4328 else
4329 {
4330 FD_SET((int)fd, rfds);
4331 if (maxfd < (int)fd)
4332 maxfd = (int)fd;
4333 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004334 }
4335 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004336 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004337
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004338 maxfd = channel_fill_wfds(maxfd, wfds);
4339
Bram Moolenaare0874f82016-01-24 20:36:41 +01004340 return maxfd;
4341}
4342
4343/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004344 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004345 */
4346 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004347channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004348{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004349 int ret = ret_in;
4350 channel_T *channel;
4351 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004352 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004353 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004354 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004355
Bram Moolenaar77073442016-02-13 23:23:53 +01004356 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004357 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004358 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004359 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004360 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004361
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004362 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004363 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004364 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004365 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004366 --ret;
4367 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004368 else if (fd != INVALID_FD && channel->ch_keep_open)
4369 {
4370 /* polling a keep-open channel */
4371 channel_read(channel, part, "channel_select_check_keep_open");
4372 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004373 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004374
4375 in_part = &channel->ch_part[PART_IN];
4376 if (ret > 0 && in_part->ch_fd != INVALID_FD
4377 && FD_ISSET(in_part->ch_fd, wfds))
4378 {
Bram Moolenaar9af97782018-04-03 12:51:01 +02004379 /* Clear the flag first, ch_fd may change in channel_write_input(). */
Bram Moolenaar3c518402017-09-08 20:47:00 +02004380 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004381 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004382 --ret;
4383 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004384 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004385
4386 return ret;
4387}
Bram Moolenaar4f974752019-02-17 17:44:42 +01004388# endif /* !MSWIN && HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004389
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004390/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004391 * Execute queued up commands.
4392 * Invoked from the main loop when it's safe to execute received commands.
4393 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004394 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004395 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004396channel_parse_messages(void)
4397{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004398 channel_T *channel = first_channel;
4399 int ret = FALSE;
4400 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004401 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004402#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004403 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004404
4405 ELAPSED_INIT(start_tv);
4406#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004407
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004408 ++safe_to_invoke_callback;
4409
Bram Moolenaard0b65022016-03-06 21:50:33 +01004410 /* Only do this message when another message was given, otherwise we get
4411 * lots of them. */
4412 if (did_log_msg)
4413 {
4414 ch_log(NULL, "looking for messages on channels");
4415 did_log_msg = FALSE;
4416 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004417 while (channel != NULL)
4418 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004419 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004420 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004421 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004422 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004423 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004424 channel = first_channel;
4425 continue;
4426 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004427 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004428 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004429 if (channel->ch_killing)
4430 {
4431 channel_free_contents(channel);
4432 channel->ch_job->jv_channel = NULL;
4433 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004434 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004435 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004436 channel = first_channel;
4437 continue;
4438 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004439 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004440 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004441 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004442 channel_free(channel);
4443 channel = first_channel;
4444 part = PART_SOCK;
4445 continue;
4446 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004447 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004448 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004449 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004450 /* Increase the refcount, in case the handler causes the channel
4451 * to be unreferenced or closed. */
4452 ++channel->ch_refcount;
4453 r = may_invoke_callback(channel, part);
4454 if (r == OK)
4455 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004456 if (channel_unref(channel) || (r == OK
4457#ifdef ELAPSED_FUNC
4458 /* Limit the time we loop here to 100 msec, otherwise
4459 * Vim becomes unresponsive when the callback takes
4460 * more than a bit of time. */
4461 && ELAPSED_FUNC(start_tv) < 100L
4462#endif
4463 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004464 {
4465 /* channel was freed or something was done, start over */
4466 channel = first_channel;
4467 part = PART_SOCK;
4468 continue;
4469 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004470 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004471 if (part < PART_ERR)
4472 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004473 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004474 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004475 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004476 part = PART_SOCK;
4477 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004478 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004479
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004480 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004481 {
4482 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004483 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004484 }
4485
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004486 --safe_to_invoke_callback;
4487
Bram Moolenaard7ece102016-02-02 23:23:02 +01004488 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004489}
4490
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004491/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004492 * Return TRUE if any channel has readahead. That means we should not block on
4493 * waiting for input.
4494 */
4495 int
4496channel_any_readahead(void)
4497{
4498 channel_T *channel = first_channel;
4499 ch_part_T part = PART_SOCK;
4500
4501 while (channel != NULL)
4502 {
4503 if (channel_has_readahead(channel, part))
4504 return TRUE;
4505 if (part < PART_ERR)
4506 ++part;
4507 else
4508 {
4509 channel = channel->ch_next;
4510 part = PART_SOCK;
4511 }
4512 }
4513 return FALSE;
4514}
4515
4516/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004517 * Mark references to lists used in channels.
4518 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004519 int
4520set_ref_in_channel(int copyID)
4521{
Bram Moolenaar77073442016-02-13 23:23:53 +01004522 int abort = FALSE;
4523 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004524 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004525
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004526 for (channel = first_channel; !abort && channel != NULL;
4527 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004528 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004529 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004530 tv.v_type = VAR_CHANNEL;
4531 tv.vval.v_channel = channel;
4532 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004533 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004534 return abort;
4535}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004536
4537/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004538 * Return the "part" to write to for "channel".
4539 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004540 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004541channel_part_send(channel_T *channel)
4542{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004543 if (channel->CH_SOCK_FD == INVALID_FD)
4544 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004545 return PART_SOCK;
4546}
4547
4548/*
4549 * Return the default "part" to read from for "channel".
4550 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004551 ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004552channel_part_read(channel_T *channel)
4553{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004554 if (channel->CH_SOCK_FD == INVALID_FD)
4555 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004556 return PART_SOCK;
4557}
4558
4559/*
4560 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004561 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004562 */
4563 ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004564channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004565{
Bram Moolenaar77073442016-02-13 23:23:53 +01004566 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004567 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004568 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004569}
4570
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004571/*
4572 * Return the timeout of "channel"/"part"
4573 */
4574 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004575channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004576{
4577 return channel->ch_part[part].ch_timeout;
4578}
4579
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004580 static int
4581handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4582{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004583 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004584
4585 opt->jo_set |= jo;
4586 if (STRCMP(val, "nl") == 0)
4587 *modep = MODE_NL;
4588 else if (STRCMP(val, "raw") == 0)
4589 *modep = MODE_RAW;
4590 else if (STRCMP(val, "js") == 0)
4591 *modep = MODE_JS;
4592 else if (STRCMP(val, "json") == 0)
4593 *modep = MODE_JSON;
4594 else
4595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004596 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004597 return FAIL;
4598 }
4599 return OK;
4600}
4601
4602 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004603handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004604{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004605 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004606
4607 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4608 if (STRCMP(val, "null") == 0)
4609 opt->jo_io[part] = JIO_NULL;
4610 else if (STRCMP(val, "pipe") == 0)
4611 opt->jo_io[part] = JIO_PIPE;
4612 else if (STRCMP(val, "file") == 0)
4613 opt->jo_io[part] = JIO_FILE;
4614 else if (STRCMP(val, "buffer") == 0)
4615 opt->jo_io[part] = JIO_BUFFER;
4616 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4617 opt->jo_io[part] = JIO_OUT;
4618 else
4619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004620 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004621 return FAIL;
4622 }
4623 return OK;
4624}
4625
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004626/*
4627 * Clear a jobopt_T before using it.
4628 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004629 void
4630clear_job_options(jobopt_T *opt)
4631{
4632 vim_memset(opt, 0, sizeof(jobopt_T));
4633}
4634
4635/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004636 * Free any members of a jobopt_T.
4637 */
4638 void
4639free_job_options(jobopt_T *opt)
4640{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004641 if (opt->jo_callback.cb_partial != NULL)
4642 partial_unref(opt->jo_callback.cb_partial);
4643 else if (opt->jo_callback.cb_name != NULL)
4644 func_unref(opt->jo_callback.cb_name);
4645 if (opt->jo_out_cb.cb_partial != NULL)
4646 partial_unref(opt->jo_out_cb.cb_partial);
4647 else if (opt->jo_out_cb.cb_name != NULL)
4648 func_unref(opt->jo_out_cb.cb_name);
4649 if (opt->jo_err_cb.cb_partial != NULL)
4650 partial_unref(opt->jo_err_cb.cb_partial);
4651 else if (opt->jo_err_cb.cb_name != NULL)
4652 func_unref(opt->jo_err_cb.cb_name);
4653 if (opt->jo_close_cb.cb_partial != NULL)
4654 partial_unref(opt->jo_close_cb.cb_partial);
4655 else if (opt->jo_close_cb.cb_name != NULL)
4656 func_unref(opt->jo_close_cb.cb_name);
4657 if (opt->jo_exit_cb.cb_partial != NULL)
4658 partial_unref(opt->jo_exit_cb.cb_partial);
4659 else if (opt->jo_exit_cb.cb_name != NULL)
4660 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004661 if (opt->jo_env != NULL)
4662 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004663}
4664
4665/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004666 * Get the PART_ number from the first character of an option name.
4667 */
4668 static int
4669part_from_char(int c)
4670{
4671 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4672}
4673
4674/*
4675 * Get the option entries from the dict in "tv", parse them and put the result
4676 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004677 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004678 * If an option value is invalid return FAIL.
4679 */
4680 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004681get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004682{
4683 typval_T *item;
4684 char_u *val;
4685 dict_T *dict;
4686 int todo;
4687 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004688 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004689
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004690 if (tv->v_type == VAR_UNKNOWN)
4691 return OK;
4692 if (tv->v_type != VAR_DICT)
4693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004694 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004695 return FAIL;
4696 }
4697 dict = tv->vval.v_dict;
4698 if (dict == NULL)
4699 return OK;
4700
4701 todo = (int)dict->dv_hashtab.ht_used;
4702 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4703 if (!HASHITEM_EMPTY(hi))
4704 {
4705 item = &dict_lookup(hi)->di_tv;
4706
4707 if (STRCMP(hi->hi_key, "mode") == 0)
4708 {
4709 if (!(supported & JO_MODE))
4710 break;
4711 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4712 return FAIL;
4713 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004714 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004715 {
4716 if (!(supported & JO_IN_MODE))
4717 break;
4718 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4719 == FAIL)
4720 return FAIL;
4721 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004722 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004723 {
4724 if (!(supported & JO_OUT_MODE))
4725 break;
4726 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4727 == FAIL)
4728 return FAIL;
4729 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004730 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004731 {
4732 if (!(supported & JO_ERR_MODE))
4733 break;
4734 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4735 == FAIL)
4736 return FAIL;
4737 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004738 else if (STRCMP(hi->hi_key, "noblock") == 0)
4739 {
4740 if (!(supported & JO_MODE))
4741 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004742 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004743 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004744 else if (STRCMP(hi->hi_key, "in_io") == 0
4745 || STRCMP(hi->hi_key, "out_io") == 0
4746 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004747 {
4748 if (!(supported & JO_OUT_IO))
4749 break;
4750 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4751 return FAIL;
4752 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004753 else if (STRCMP(hi->hi_key, "in_name") == 0
4754 || STRCMP(hi->hi_key, "out_name") == 0
4755 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004756 {
4757 part = part_from_char(*hi->hi_key);
4758
4759 if (!(supported & JO_OUT_IO))
4760 break;
4761 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4762 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004763 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004764 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004765 else if (STRCMP(hi->hi_key, "pty") == 0)
4766 {
4767 if (!(supported & JO_MODE))
4768 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004769 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004770 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004771 else if (STRCMP(hi->hi_key, "in_buf") == 0
4772 || STRCMP(hi->hi_key, "out_buf") == 0
4773 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004774 {
4775 part = part_from_char(*hi->hi_key);
4776
4777 if (!(supported & JO_OUT_IO))
4778 break;
4779 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004780 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004781 if (opt->jo_io_buf[part] <= 0)
4782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004783 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004784 return FAIL;
4785 }
4786 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004788 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004789 return FAIL;
4790 }
4791 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004792 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4793 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4794 {
4795 part = part_from_char(*hi->hi_key);
4796
4797 if (!(supported & JO_OUT_IO))
4798 break;
4799 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004800 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004801 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004802 else if (STRCMP(hi->hi_key, "out_msg") == 0
4803 || STRCMP(hi->hi_key, "err_msg") == 0)
4804 {
4805 part = part_from_char(*hi->hi_key);
4806
4807 if (!(supported & JO_OUT_IO))
4808 break;
4809 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004810 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004811 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004812 else if (STRCMP(hi->hi_key, "in_top") == 0
4813 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004814 {
4815 linenr_T *lp;
4816
4817 if (!(supported & JO_OUT_IO))
4818 break;
4819 if (hi->hi_key[3] == 't')
4820 {
4821 lp = &opt->jo_in_top;
4822 opt->jo_set |= JO_IN_TOP;
4823 }
4824 else
4825 {
4826 lp = &opt->jo_in_bot;
4827 opt->jo_set |= JO_IN_BOT;
4828 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004829 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004830 if (*lp < 0)
4831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004832 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004833 return FAIL;
4834 }
4835 }
4836 else if (STRCMP(hi->hi_key, "channel") == 0)
4837 {
4838 if (!(supported & JO_OUT_IO))
4839 break;
4840 opt->jo_set |= JO_CHANNEL;
4841 if (item->v_type != VAR_CHANNEL)
4842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004843 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004844 return FAIL;
4845 }
4846 opt->jo_channel = item->vval.v_channel;
4847 }
4848 else if (STRCMP(hi->hi_key, "callback") == 0)
4849 {
4850 if (!(supported & JO_CALLBACK))
4851 break;
4852 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004853 opt->jo_callback = get_callback(item);
4854 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004857 return FAIL;
4858 }
4859 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004860 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004861 {
4862 if (!(supported & JO_OUT_CALLBACK))
4863 break;
4864 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004865 opt->jo_out_cb = get_callback(item);
4866 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004868 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004869 return FAIL;
4870 }
4871 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004872 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004873 {
4874 if (!(supported & JO_ERR_CALLBACK))
4875 break;
4876 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004877 opt->jo_err_cb = get_callback(item);
4878 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004880 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004881 return FAIL;
4882 }
4883 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004884 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004885 {
4886 if (!(supported & JO_CLOSE_CALLBACK))
4887 break;
4888 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004889 opt->jo_close_cb = get_callback(item);
4890 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004892 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004893 return FAIL;
4894 }
4895 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004896 else if (STRCMP(hi->hi_key, "drop") == 0)
4897 {
4898 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004899 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004900
4901 if (STRCMP(val, "never") == 0)
4902 never = TRUE;
4903 else if (STRCMP(val, "auto") != 0)
4904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004905 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004906 return FAIL;
4907 }
4908 opt->jo_drop_never = never;
4909 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004910 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4911 {
4912 if (!(supported & JO_EXIT_CB))
4913 break;
4914 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004915 opt->jo_exit_cb = get_callback(item);
4916 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004918 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004919 return FAIL;
4920 }
4921 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004922#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004923 else if (STRCMP(hi->hi_key, "term_name") == 0)
4924 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004925 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004926 break;
4927 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004928 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004929 if (opt->jo_term_name == NULL)
4930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004931 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004932 return FAIL;
4933 }
4934 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004935 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4936 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004937 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004938 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004940 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004942 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004943 return FAIL;
4944 }
4945 opt->jo_set2 |= JO2_TERM_FINISH;
4946 opt->jo_term_finish = *val;
4947 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004948 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4949 {
4950 char_u *p;
4951
4952 if (!(supported2 & JO2_TERM_OPENCMD))
4953 break;
4954 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004955 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004956 if (p != NULL)
4957 {
4958 /* Must have %d and no other %. */
4959 p = vim_strchr(p, '%');
4960 if (p != NULL && (p[1] != 'd'
4961 || vim_strchr(p + 2, '%') != NULL))
4962 p = NULL;
4963 }
4964 if (p == NULL)
4965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004966 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004967 return FAIL;
4968 }
4969 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004970 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4971 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004972 char_u *p;
4973
4974 if (!(supported2 & JO2_EOF_CHARS))
4975 break;
4976 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004977 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004978 if (p == NULL)
4979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004980 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004981 return FAIL;
4982 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004983 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004984 else if (STRCMP(hi->hi_key, "term_rows") == 0)
4985 {
4986 if (!(supported2 & JO2_TERM_ROWS))
4987 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004988 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004989 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004990 }
4991 else if (STRCMP(hi->hi_key, "term_cols") == 0)
4992 {
4993 if (!(supported2 & JO2_TERM_COLS))
4994 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004995 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004996 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004997 }
4998 else if (STRCMP(hi->hi_key, "vertical") == 0)
4999 {
5000 if (!(supported2 & JO2_VERTICAL))
5001 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005002 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005003 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005004 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005005 else if (STRCMP(hi->hi_key, "curwin") == 0)
5006 {
5007 if (!(supported2 & JO2_CURWIN))
5008 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005009 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005010 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005011 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005012 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5013 {
5014 int nr;
5015
5016 if (!(supported2 & JO2_CURWIN))
5017 break;
5018 opt->jo_set2 |= JO2_BUFNR;
5019 nr = tv_get_number(item);
5020 if (nr <= 0)
5021 {
5022 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5023 return FAIL;
5024 }
5025 opt->jo_bufnr_buf = buflist_findnr(nr);
5026 if (opt->jo_bufnr_buf == NULL)
5027 {
5028 semsg(_(e_nobufnr), (long)nr);
5029 return FAIL;
5030 }
5031 if (opt->jo_bufnr_buf->b_nwindows == 0
5032 || opt->jo_bufnr_buf->b_term == NULL)
5033 {
5034 semsg(_(e_invarg2), "bufnr");
5035 return FAIL;
5036 }
5037 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005038 else if (STRCMP(hi->hi_key, "hidden") == 0)
5039 {
5040 if (!(supported2 & JO2_HIDDEN))
5041 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005042 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005043 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005044 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005045 else if (STRCMP(hi->hi_key, "norestore") == 0)
5046 {
5047 if (!(supported2 & JO2_NORESTORE))
5048 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005049 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005050 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005051 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005052 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5053 {
5054 if (!(supported2 & JO2_TERM_KILL))
5055 break;
5056 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005057 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005058 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005059 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005060 {
5061 char_u *p;
5062
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005063 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005064 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005065 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005066 p = tv_get_string_chk(item);
5067 if (p == NULL)
5068 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005069 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005070 return FAIL;
5071 }
5072 // Allow empty string, "winpty", "conpty".
5073 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5074 || STRCMP(p, "conpty") == 0))
5075 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005076 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005077 return FAIL;
5078 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005079 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005080 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005081# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5082 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005084 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005085 listitem_T *li;
5086 long_u rgb[16];
5087
5088 if (!(supported2 & JO2_ANSI_COLORS))
5089 break;
5090
5091 if (item == NULL || item->v_type != VAR_LIST
5092 || item->vval.v_list == NULL)
5093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005094 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005095 return FAIL;
5096 }
5097
5098 li = item->vval.v_list->lv_first;
5099 for (; li != NULL && n < 16; li = li->li_next, n++)
5100 {
5101 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005102 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005103
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005104 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005105 if (color_name == NULL)
5106 return FAIL;
5107
5108 guicolor = GUI_GET_COLOR(color_name);
5109 if (guicolor == INVALCOLOR)
5110 return FAIL;
5111
5112 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5113 }
5114
5115 if (n != 16 || li != NULL)
5116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005117 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005118 return FAIL;
5119 }
5120
5121 opt->jo_set2 |= JO2_ANSI_COLORS;
5122 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5123 }
5124# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005125#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005126 else if (STRCMP(hi->hi_key, "env") == 0)
5127 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005128 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005129 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005130 if (item->v_type != VAR_DICT)
5131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005132 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005133 return FAIL;
5134 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005135 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005136 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005137 if (opt->jo_env != NULL)
5138 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005139 }
5140 else if (STRCMP(hi->hi_key, "cwd") == 0)
5141 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005142 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005143 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005144 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005145 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005146#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005147 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5148#endif
5149 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005151 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005152 return FAIL;
5153 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005154 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005155 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005156 else if (STRCMP(hi->hi_key, "waittime") == 0)
5157 {
5158 if (!(supported & JO_WAITTIME))
5159 break;
5160 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005161 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005162 }
5163 else if (STRCMP(hi->hi_key, "timeout") == 0)
5164 {
5165 if (!(supported & JO_TIMEOUT))
5166 break;
5167 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005168 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005169 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005170 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005171 {
5172 if (!(supported & JO_OUT_TIMEOUT))
5173 break;
5174 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005175 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005176 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005177 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005178 {
5179 if (!(supported & JO_ERR_TIMEOUT))
5180 break;
5181 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005182 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005183 }
5184 else if (STRCMP(hi->hi_key, "part") == 0)
5185 {
5186 if (!(supported & JO_PART))
5187 break;
5188 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005189 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005190 if (STRCMP(val, "err") == 0)
5191 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005192 else if (STRCMP(val, "out") == 0)
5193 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005194 else
5195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005196 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005197 return FAIL;
5198 }
5199 }
5200 else if (STRCMP(hi->hi_key, "id") == 0)
5201 {
5202 if (!(supported & JO_ID))
5203 break;
5204 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005205 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005206 }
5207 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5208 {
5209 if (!(supported & JO_STOPONEXIT))
5210 break;
5211 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005212 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 opt->jo_soe_buf);
5214 if (opt->jo_stoponexit == NULL)
5215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005216 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005217 return FAIL;
5218 }
5219 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005220 else if (STRCMP(hi->hi_key, "block_write") == 0)
5221 {
5222 if (!(supported & JO_BLOCK_WRITE))
5223 break;
5224 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005225 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005226 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005227 else
5228 break;
5229 --todo;
5230 }
5231 if (todo > 0)
5232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005233 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005234 return FAIL;
5235 }
5236
5237 return OK;
5238}
5239
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005240static job_T *first_job = NULL;
5241
5242 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005243job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005244{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005245 int i;
5246
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005247 ch_log(job->jv_channel, "Freeing job");
5248 if (job->jv_channel != NULL)
5249 {
5250 /* The link from the channel to the job doesn't count as a reference,
5251 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005252 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005253 * NULL the reference. We don't set ch_job_killed, unreferencing the
5254 * job doesn't mean it stops running. */
5255 job->jv_channel->ch_job = NULL;
5256 channel_unref(job->jv_channel);
5257 }
5258 mch_clear_job(job);
5259
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005260 vim_free(job->jv_tty_in);
5261 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005262 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005263#ifdef UNIX
5264 vim_free(job->jv_termsig);
5265#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005266#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005267 vim_free(job->jv_tty_type);
5268#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005269 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005270 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005271 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005272 for (i = 0; job->jv_argv[i] != NULL; i++)
5273 vim_free(job->jv_argv[i]);
5274 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005275 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005276}
5277
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005278/*
5279 * Remove "job" from the list of jobs.
5280 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005281 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005282job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005283{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005284 if (job->jv_next != NULL)
5285 job->jv_next->jv_prev = job->jv_prev;
5286 if (job->jv_prev == NULL)
5287 first_job = job->jv_next;
5288 else
5289 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005290}
5291
5292 static void
5293job_free_job(job_T *job)
5294{
5295 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005296 vim_free(job);
5297}
5298
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005299 static void
5300job_free(job_T *job)
5301{
5302 if (!in_free_unref_items)
5303 {
5304 job_free_contents(job);
5305 job_free_job(job);
5306 }
5307}
5308
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005309job_T *jobs_to_free = NULL;
5310
5311/*
5312 * Put "job" in a list to be freed later, when it's no longer referenced.
5313 */
5314 static void
5315job_free_later(job_T *job)
5316{
5317 job_unlink(job);
5318 job->jv_next = jobs_to_free;
5319 jobs_to_free = job;
5320}
5321
5322 static void
5323free_jobs_to_free_later(void)
5324{
5325 job_T *job;
5326
5327 while (jobs_to_free != NULL)
5328 {
5329 job = jobs_to_free;
5330 jobs_to_free = job->jv_next;
5331 job_free_contents(job);
5332 vim_free(job);
5333 }
5334}
5335
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005336#if defined(EXITFREE) || defined(PROTO)
5337 void
5338job_free_all(void)
5339{
5340 while (first_job != NULL)
5341 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005342 free_jobs_to_free_later();
5343
5344# ifdef FEAT_TERMINAL
5345 free_unused_terminals();
5346# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005347}
5348#endif
5349
5350/*
5351 * Return TRUE if we need to check if the process of "job" has ended.
5352 */
5353 static int
5354job_need_end_check(job_T *job)
5355{
5356 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005357 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005358}
5359
5360/*
5361 * Return TRUE if the channel of "job" is still useful.
5362 */
5363 static int
5364job_channel_still_useful(job_T *job)
5365{
5366 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5367}
5368
5369/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005370 * Return TRUE if the channel of "job" is closeable.
5371 */
5372 static int
5373job_channel_can_close(job_T *job)
5374{
5375 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5376}
5377
5378/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005379 * Return TRUE if the job should not be freed yet. Do not free the job when
5380 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5381 * or when the associated channel will do something with the job output.
5382 */
5383 static int
5384job_still_useful(job_T *job)
5385{
5386 return job_need_end_check(job) || job_channel_still_useful(job);
5387}
5388
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005389#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005390/*
5391 * Return TRUE when there is any running job that we care about.
5392 */
5393 int
5394job_any_running()
5395{
5396 job_T *job;
5397
5398 for (job = first_job; job != NULL; job = job->jv_next)
5399 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005400 {
5401 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005402 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005403 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005404 return FALSE;
5405}
5406#endif
5407
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005408#if !defined(USE_ARGV) || defined(PROTO)
5409/*
5410 * Escape one argument for an external command.
5411 * Returns the escaped string in allocated memory. NULL when out of memory.
5412 */
5413 static char_u *
5414win32_escape_arg(char_u *arg)
5415{
5416 int slen, dlen;
5417 int escaping = 0;
5418 int i;
5419 char_u *s, *d;
5420 char_u *escaped_arg;
5421 int has_spaces = FALSE;
5422
5423 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005424 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005425 dlen = slen;
5426 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5427 {
5428 if (*s == '"' || *s == '\\')
5429 ++dlen;
5430 if (*s == ' ' || *s == '\t')
5431 has_spaces = TRUE;
5432 }
5433
5434 if (has_spaces)
5435 dlen += 2;
5436
5437 if (dlen == slen)
5438 return vim_strsave(arg);
5439
5440 /* Allocate memory for the result and fill it. */
5441 escaped_arg = alloc(dlen + 1);
5442 if (escaped_arg == NULL)
5443 return NULL;
5444 memset(escaped_arg, 0, dlen+1);
5445
5446 d = escaped_arg;
5447
5448 if (has_spaces)
5449 *d++ = '"';
5450
5451 for (s = arg; *s != NUL;)
5452 {
5453 switch (*s)
5454 {
5455 case '"':
5456 for (i = 0; i < escaping; i++)
5457 *d++ = '\\';
5458 escaping = 0;
5459 *d++ = '\\';
5460 *d++ = *s++;
5461 break;
5462 case '\\':
5463 escaping++;
5464 *d++ = *s++;
5465 break;
5466 default:
5467 escaping = 0;
5468 MB_COPY_CHAR(s, d);
5469 break;
5470 }
5471 }
5472
5473 /* add terminating quote and finish with a NUL */
5474 if (has_spaces)
5475 {
5476 for (i = 0; i < escaping; i++)
5477 *d++ = '\\';
5478 *d++ = '"';
5479 }
5480 *d = NUL;
5481
5482 return escaped_arg;
5483}
5484
5485/*
5486 * Build a command line from a list, taking care of escaping.
5487 * The result is put in gap->ga_data.
5488 * Returns FAIL when out of memory.
5489 */
5490 int
5491win32_build_cmd(list_T *l, garray_T *gap)
5492{
5493 listitem_T *li;
5494 char_u *s;
5495
5496 for (li = l->lv_first; li != NULL; li = li->li_next)
5497 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005498 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005499 if (s == NULL)
5500 return FAIL;
5501 s = win32_escape_arg(s);
5502 if (s == NULL)
5503 return FAIL;
5504 ga_concat(gap, s);
5505 vim_free(s);
5506 if (li->li_next != NULL)
5507 ga_append(gap, ' ');
5508 }
5509 return OK;
5510}
5511#endif
5512
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005513/*
5514 * NOTE: Must call job_cleanup() only once right after the status of "job"
5515 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5516 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005517 * If the job is no longer used it will be removed from the list of jobs, and
5518 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005519 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005520 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005521job_cleanup(job_T *job)
5522{
5523 if (job->jv_status != JOB_ENDED)
5524 return;
5525
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005526 /* Ready to cleanup the job. */
5527 job->jv_status = JOB_FINISHED;
5528
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005529 /* When only channel-in is kept open, close explicitly. */
5530 if (job->jv_channel != NULL)
5531 ch_close_part(job->jv_channel, PART_IN);
5532
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005533 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005534 {
5535 typval_T argv[3];
5536 typval_T rettv;
5537 int dummy;
5538
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005539 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005540 ch_log(job->jv_channel, "Invoking exit callback %s",
5541 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005542 ++job->jv_refcount;
5543 argv[0].v_type = VAR_JOB;
5544 argv[0].vval.v_job = job;
5545 argv[1].v_type = VAR_NUMBER;
5546 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005547 call_callback(&job->jv_exit_cb, -1,
5548 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005549 clear_tv(&rettv);
5550 --job->jv_refcount;
5551 channel_need_redraw = TRUE;
5552 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005553
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005554 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5555 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005556
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005557 // Do not free the job in case the close callback of the associated channel
5558 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005559 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005560 // The job was already unreferenced and the associated channel was
5561 // detached, now that it ended it can be freed. However, a caller might
5562 // still use it, thus free it a bit later.
5563 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005564}
5565
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005566/*
5567 * Mark references in jobs that are still useful.
5568 */
5569 int
5570set_ref_in_job(int copyID)
5571{
5572 int abort = FALSE;
5573 job_T *job;
5574 typval_T tv;
5575
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005576 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005577 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005578 {
5579 tv.v_type = VAR_JOB;
5580 tv.vval.v_job = job;
5581 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5582 }
5583 return abort;
5584}
5585
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005586/*
5587 * Dereference "job". Note that after this "job" may have been freed.
5588 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005589 void
5590job_unref(job_T *job)
5591{
5592 if (job != NULL && --job->jv_refcount <= 0)
5593 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005594 /* Do not free the job if there is a channel where the close callback
5595 * may get the job info. */
5596 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005597 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005598 /* Do not free the job when it has not ended yet and there is a
5599 * "stoponexit" flag or an exit callback. */
5600 if (!job_need_end_check(job))
5601 {
5602 job_free(job);
5603 }
5604 else if (job->jv_channel != NULL)
5605 {
5606 /* Do remove the link to the channel, otherwise it hangs
5607 * around until Vim exits. See job_free() for refcount. */
5608 ch_log(job->jv_channel, "detaching channel from job");
5609 job->jv_channel->ch_job = NULL;
5610 channel_unref(job->jv_channel);
5611 job->jv_channel = NULL;
5612 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005613 }
5614 }
5615}
5616
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005617 int
5618free_unused_jobs_contents(int copyID, int mask)
5619{
5620 int did_free = FALSE;
5621 job_T *job;
5622
5623 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005624 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005625 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005626 {
5627 /* Free the channel and ordinary items it contains, but don't
5628 * recurse into Lists, Dictionaries etc. */
5629 job_free_contents(job);
5630 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005631 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005632 return did_free;
5633}
5634
5635 void
5636free_unused_jobs(int copyID, int mask)
5637{
5638 job_T *job;
5639 job_T *job_next;
5640
5641 for (job = first_job; job != NULL; job = job_next)
5642 {
5643 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005644 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005645 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005646 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005647 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005648 job_free_job(job);
5649 }
5650 }
5651}
5652
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005653/*
5654 * Allocate a job. Sets the refcount to one and sets options default.
5655 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005656 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005657job_alloc(void)
5658{
5659 job_T *job;
5660
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005661 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005662 if (job != NULL)
5663 {
5664 job->jv_refcount = 1;
5665 job->jv_stoponexit = vim_strsave((char_u *)"term");
5666
5667 if (first_job != NULL)
5668 {
5669 first_job->jv_prev = job;
5670 job->jv_next = first_job;
5671 }
5672 first_job = job;
5673 }
5674 return job;
5675}
5676
5677 void
5678job_set_options(job_T *job, jobopt_T *opt)
5679{
5680 if (opt->jo_set & JO_STOPONEXIT)
5681 {
5682 vim_free(job->jv_stoponexit);
5683 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5684 job->jv_stoponexit = NULL;
5685 else
5686 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5687 }
5688 if (opt->jo_set & JO_EXIT_CB)
5689 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005690 free_callback(&job->jv_exit_cb);
5691 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005692 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005693 job->jv_exit_cb.cb_name = NULL;
5694 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005695 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005696 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005697 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005698 }
5699}
5700
5701/*
5702 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5703 */
5704 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005705job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005706{
5707 job_T *job;
5708
5709 for (job = first_job; job != NULL; job = job->jv_next)
5710 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005711 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005712}
5713
5714/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005715 * Return TRUE when there is any job that has an exit callback and might exit,
5716 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005717 */
5718 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005719has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005720{
5721 job_T *job;
5722
5723 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005724 /* Only should check if the channel has been closed, if the channel is
5725 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005726 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5727 || (job->jv_status == JOB_FINISHED
5728 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005729 return TRUE;
5730 return FALSE;
5731}
5732
Bram Moolenaar97792de2016-10-15 18:36:49 +02005733#define MAX_CHECK_ENDED 8
5734
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005735/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005736 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005737 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005738 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005739 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005740job_check_ended(void)
5741{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005742 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005743 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005744
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005745 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005746 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005747 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005748
Bram Moolenaar97792de2016-10-15 18:36:49 +02005749 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005750 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005751 // NOTE: mch_detect_ended_job() must only return a job of which the
5752 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005753 job_T *job = mch_detect_ended_job(first_job);
5754
5755 if (job == NULL)
5756 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005757 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005758 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005759 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005760
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005761 // Actually free jobs that were cleaned up.
5762 free_jobs_to_free_later();
5763
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005764 if (channel_need_redraw)
5765 {
5766 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005767 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005768 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005769 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005770}
5771
5772/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005773 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005774 * "argv_arg" is only for Unix.
5775 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005776 * The returned job has a refcount of one.
5777 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005778 */
5779 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005780job_start(
5781 typval_T *argvars,
5782 char **argv_arg,
5783 jobopt_T *opt_arg,
5784 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005785{
5786 job_T *job;
5787 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005788 char **argv = NULL;
5789 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005790#if defined(UNIX)
5791# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005792 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005793#else
5794 garray_T ga;
5795#endif
5796 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005797 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005798
5799 job = job_alloc();
5800 if (job == NULL)
5801 return NULL;
5802
5803 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005804#ifndef USE_ARGV
5805 ga_init2(&ga, (int)sizeof(char*), 20);
5806#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005807
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005808 if (opt_arg != NULL)
5809 opt = *opt_arg;
5810 else
5811 {
5812 /* Default mode is NL. */
5813 clear_job_options(&opt);
5814 opt.jo_mode = MODE_NL;
5815 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005816 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5817 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5818 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005819 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005820 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005821
5822 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005823 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005824 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5825 && opt.jo_io[part] == JIO_FILE
5826 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5827 || *opt.jo_io_name[part] == NUL))
5828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005829 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005830 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005831 }
5832
5833 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5834 {
5835 buf_T *buf = NULL;
5836
5837 /* check that we can find the buffer before starting the job */
5838 if (opt.jo_set & JO_IN_BUF)
5839 {
5840 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5841 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005842 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005843 }
5844 else if (!(opt.jo_set & JO_IN_NAME))
5845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005846 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005847 }
5848 else
5849 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5850 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005851 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005852 if (buf->b_ml.ml_mfp == NULL)
5853 {
5854 char_u numbuf[NUMBUFLEN];
5855 char_u *s;
5856
5857 if (opt.jo_set & JO_IN_BUF)
5858 {
5859 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5860 s = numbuf;
5861 }
5862 else
5863 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005864 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005865 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005866 }
5867 job->jv_in_buf = buf;
5868 }
5869
5870 job_set_options(job, &opt);
5871
Bram Moolenaar13568252018-03-16 20:46:58 +01005872#ifdef USE_ARGV
5873 if (argv_arg != NULL)
5874 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005875 /* Make a copy of argv_arg for job->jv_argv. */
5876 for (i = 0; argv_arg[i] != NULL; i++)
5877 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005878 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005879 if (argv == NULL)
5880 goto theend;
5881 for (i = 0; i < argc; i++)
5882 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5883 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005884 }
5885 else
5886#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005887 if (argvars[0].v_type == VAR_STRING)
5888 {
5889 /* Command is a string. */
5890 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005891 if (cmd == NULL || *cmd == NUL)
5892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005893 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005894 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005895 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005896
5897 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005898 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005899 }
5900 else if (argvars[0].v_type != VAR_LIST
5901 || argvars[0].vval.v_list == NULL
5902 || argvars[0].vval.v_list->lv_len < 1)
5903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005904 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005905 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005906 }
5907 else
5908 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005909 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005910
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005911 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005912 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005913#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005914 if (win32_build_cmd(l, &ga) == FAIL)
5915 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005916 cmd = ga.ga_data;
5917#endif
5918 }
5919
Bram Moolenaar20608922018-04-21 22:30:08 +02005920 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005921 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005922
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005923#ifdef USE_ARGV
5924 if (ch_log_active())
5925 {
5926 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005927
5928 ga_init2(&ga, (int)sizeof(char), 200);
5929 for (i = 0; i < argc; ++i)
5930 {
5931 if (i > 0)
5932 ga_concat(&ga, (char_u *)" ");
5933 ga_concat(&ga, (char_u *)argv[i]);
5934 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005935 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005936 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005937 ga_clear(&ga);
5938 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005939 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005940#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005941 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005942 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005943#endif
5944
5945 /* If the channel is reading from a buffer, write lines now. */
5946 if (job->jv_channel != NULL)
5947 channel_write_in(job->jv_channel);
5948
5949theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005950#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005951 vim_free(ga.ga_data);
5952#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005953 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005954 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005955 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005956 return job;
5957}
5958
5959/*
5960 * Get the status of "job" and invoke the exit callback when needed.
5961 * The returned string is not allocated.
5962 */
5963 char *
5964job_status(job_T *job)
5965{
5966 char *result;
5967
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005968 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005969 /* No need to check, dead is dead. */
5970 result = "dead";
5971 else if (job->jv_status == JOB_FAILED)
5972 result = "fail";
5973 else
5974 {
5975 result = mch_job_status(job);
5976 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005977 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005978 }
5979 return result;
5980}
5981
Bram Moolenaar8950a562016-03-12 15:22:55 +01005982/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005983 * Send a signal to "job". Implements job_stop().
5984 * When "type" is not NULL use this for the type.
5985 * Otherwise use argvars[1] for the type.
5986 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005987 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005988job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005989{
5990 char_u *arg;
5991
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005992 if (type != NULL)
5993 arg = (char_u *)type;
5994 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005995 arg = (char_u *)"";
5996 else
5997 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005998 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005999 if (arg == NULL)
6000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006001 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006002 return 0;
6003 }
6004 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006005 if (job->jv_status == JOB_FAILED)
6006 {
6007 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6008 return 0;
6009 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006010 if (job->jv_status == JOB_ENDED)
6011 {
6012 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6013 return 0;
6014 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006015 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006016 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006017 return 0;
6018
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006019 /* Assume that only "kill" will kill the job. */
6020 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006021 job->jv_channel->ch_job_killed = TRUE;
6022
6023 /* We don't try freeing the job, obviously the caller still has a
6024 * reference to it. */
6025 return 1;
6026}
6027
Bram Moolenaarf2732452018-06-03 14:47:35 +02006028 void
6029invoke_prompt_callback(void)
6030{
6031 typval_T rettv;
6032 int dummy;
6033 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 Moolenaar3a97bb32019-06-01 13:28:35 +02006055 call_callback(&curbuf->b_prompt_callback, -1,
6056 &rettv, 1, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006057 clear_tv(&argv[0]);
6058 clear_tv(&rettv);
6059}
6060
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006061/*
6062 * Return TRUE when the interrupt callback was invoked.
6063 */
6064 int
6065invoke_prompt_interrupt(void)
6066{
6067 typval_T rettv;
6068 int dummy;
6069 typval_T argv[1];
6070
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006071 if (curbuf->b_prompt_interrupt.cb_name == NULL
6072 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006073 return FALSE;
6074 argv[0].v_type = VAR_UNKNOWN;
6075
6076 got_int = FALSE; // don't skip executing commands
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006077 call_callback(&curbuf->b_prompt_interrupt, -1,
6078 &rettv, 0, argv, NULL, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006079 clear_tv(&rettv);
6080 return TRUE;
6081}
6082
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006083/*
6084 * "prompt_setcallback({buffer}, {callback})" function
6085 */
6086 void
6087f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6088{
6089 buf_T *buf;
6090 callback_T callback;
6091
6092 if (check_secure())
6093 return;
6094 buf = tv_get_buf(&argvars[0], FALSE);
6095 if (buf == NULL)
6096 return;
6097
6098 callback = get_callback(&argvars[1]);
6099 if (callback.cb_name == NULL)
6100 return;
6101
6102 free_callback(&buf->b_prompt_callback);
6103 set_callback(&buf->b_prompt_callback, &callback);
6104}
6105
6106/*
6107 * "prompt_setinterrupt({buffer}, {callback})" function
6108 */
6109 void
6110f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6111{
6112 buf_T *buf;
6113 callback_T callback;
6114
6115 if (check_secure())
6116 return;
6117 buf = tv_get_buf(&argvars[0], FALSE);
6118 if (buf == NULL)
6119 return;
6120
6121 callback = get_callback(&argvars[1]);
6122 if (callback.cb_name == NULL)
6123 return;
6124
6125 free_callback(&buf->b_prompt_interrupt);
6126 set_callback(&buf->b_prompt_interrupt, &callback);
6127}
6128
6129/*
6130 * "prompt_setprompt({buffer}, {text})" function
6131 */
6132 void
6133f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6134{
6135 buf_T *buf;
6136 char_u *text;
6137
6138 if (check_secure())
6139 return;
6140 buf = tv_get_buf(&argvars[0], FALSE);
6141 if (buf == NULL)
6142 return;
6143
6144 text = tv_get_string(&argvars[1]);
6145 vim_free(buf->b_prompt_text);
6146 buf->b_prompt_text = vim_strsave(text);
6147}
6148
6149/*
6150 * "ch_canread()" function
6151 */
6152 void
6153f_ch_canread(typval_T *argvars, typval_T *rettv)
6154{
6155 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6156
6157 rettv->vval.v_number = 0;
6158 if (channel != NULL)
6159 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6160 || channel_has_readahead(channel, PART_OUT)
6161 || channel_has_readahead(channel, PART_ERR);
6162}
6163
6164/*
6165 * "ch_close()" function
6166 */
6167 void
6168f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6169{
6170 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6171
6172 if (channel != NULL)
6173 {
6174 channel_close(channel, FALSE);
6175 channel_clear(channel);
6176 }
6177}
6178
6179/*
6180 * "ch_close()" function
6181 */
6182 void
6183f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6184{
6185 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6186
6187 if (channel != NULL)
6188 channel_close_in(channel);
6189}
6190
6191/*
6192 * "ch_getbufnr()" function
6193 */
6194 void
6195f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6196{
6197 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6198
6199 rettv->vval.v_number = -1;
6200 if (channel != NULL)
6201 {
6202 char_u *what = tv_get_string(&argvars[1]);
6203 int part;
6204
6205 if (STRCMP(what, "err") == 0)
6206 part = PART_ERR;
6207 else if (STRCMP(what, "out") == 0)
6208 part = PART_OUT;
6209 else if (STRCMP(what, "in") == 0)
6210 part = PART_IN;
6211 else
6212 part = PART_SOCK;
6213 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6214 rettv->vval.v_number =
6215 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6216 }
6217}
6218
6219/*
6220 * "ch_getjob()" function
6221 */
6222 void
6223f_ch_getjob(typval_T *argvars, typval_T *rettv)
6224{
6225 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6226
6227 if (channel != NULL)
6228 {
6229 rettv->v_type = VAR_JOB;
6230 rettv->vval.v_job = channel->ch_job;
6231 if (channel->ch_job != NULL)
6232 ++channel->ch_job->jv_refcount;
6233 }
6234}
6235
6236/*
6237 * "ch_info()" function
6238 */
6239 void
6240f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6241{
6242 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6243
6244 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6245 channel_info(channel, rettv->vval.v_dict);
6246}
6247
6248/*
6249 * "ch_log()" function
6250 */
6251 void
6252f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6253{
6254 char_u *msg = tv_get_string(&argvars[0]);
6255 channel_T *channel = NULL;
6256
6257 if (argvars[1].v_type != VAR_UNKNOWN)
6258 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6259
6260 ch_log(channel, "%s", msg);
6261}
6262
6263/*
6264 * "ch_logfile()" function
6265 */
6266 void
6267f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6268{
6269 char_u *fname;
6270 char_u *opt = (char_u *)"";
6271 char_u buf[NUMBUFLEN];
6272
6273 /* Don't open a file in restricted mode. */
6274 if (check_restricted() || check_secure())
6275 return;
6276 fname = tv_get_string(&argvars[0]);
6277 if (argvars[1].v_type == VAR_STRING)
6278 opt = tv_get_string_buf(&argvars[1], buf);
6279 ch_logfile(fname, opt);
6280}
6281
6282/*
6283 * "ch_open()" function
6284 */
6285 void
6286f_ch_open(typval_T *argvars, typval_T *rettv)
6287{
6288 rettv->v_type = VAR_CHANNEL;
6289 if (check_restricted() || check_secure())
6290 return;
6291 rettv->vval.v_channel = channel_open_func(argvars);
6292}
6293
6294/*
6295 * "ch_read()" function
6296 */
6297 void
6298f_ch_read(typval_T *argvars, typval_T *rettv)
6299{
6300 common_channel_read(argvars, rettv, FALSE, FALSE);
6301}
6302
6303/*
6304 * "ch_readblob()" function
6305 */
6306 void
6307f_ch_readblob(typval_T *argvars, typval_T *rettv)
6308{
6309 common_channel_read(argvars, rettv, TRUE, TRUE);
6310}
6311
6312/*
6313 * "ch_readraw()" function
6314 */
6315 void
6316f_ch_readraw(typval_T *argvars, typval_T *rettv)
6317{
6318 common_channel_read(argvars, rettv, TRUE, FALSE);
6319}
6320
6321/*
6322 * "ch_evalexpr()" function
6323 */
6324 void
6325f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6326{
6327 ch_expr_common(argvars, rettv, TRUE);
6328}
6329
6330/*
6331 * "ch_sendexpr()" function
6332 */
6333 void
6334f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6335{
6336 ch_expr_common(argvars, rettv, FALSE);
6337}
6338
6339/*
6340 * "ch_evalraw()" function
6341 */
6342 void
6343f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6344{
6345 ch_raw_common(argvars, rettv, TRUE);
6346}
6347
6348/*
6349 * "ch_sendraw()" function
6350 */
6351 void
6352f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6353{
6354 ch_raw_common(argvars, rettv, FALSE);
6355}
6356
6357/*
6358 * "ch_setoptions()" function
6359 */
6360 void
6361f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6362{
6363 channel_T *channel;
6364 jobopt_T opt;
6365
6366 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6367 if (channel == NULL)
6368 return;
6369 clear_job_options(&opt);
6370 if (get_job_options(&argvars[1], &opt,
6371 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6372 channel_set_options(channel, &opt);
6373 free_job_options(&opt);
6374}
6375
6376/*
6377 * "ch_status()" function
6378 */
6379 void
6380f_ch_status(typval_T *argvars, typval_T *rettv)
6381{
6382 channel_T *channel;
6383 jobopt_T opt;
6384 int part = -1;
6385
6386 /* return an empty string by default */
6387 rettv->v_type = VAR_STRING;
6388 rettv->vval.v_string = NULL;
6389
6390 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6391
6392 if (argvars[1].v_type != VAR_UNKNOWN)
6393 {
6394 clear_job_options(&opt);
6395 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6396 && (opt.jo_set & JO_PART))
6397 part = opt.jo_part;
6398 }
6399
6400 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6401}
6402
6403/*
6404 * Get the job from the argument.
6405 * Returns NULL if the job is invalid.
6406 */
6407 static job_T *
6408get_job_arg(typval_T *tv)
6409{
6410 job_T *job;
6411
6412 if (tv->v_type != VAR_JOB)
6413 {
6414 semsg(_(e_invarg2), tv_get_string(tv));
6415 return NULL;
6416 }
6417 job = tv->vval.v_job;
6418
6419 if (job == NULL)
6420 emsg(_("E916: not a valid job"));
6421 return job;
6422}
6423
6424/*
6425 * "job_getchannel()" function
6426 */
6427 void
6428f_job_getchannel(typval_T *argvars, typval_T *rettv)
6429{
6430 job_T *job = get_job_arg(&argvars[0]);
6431
6432 if (job != NULL)
6433 {
6434 rettv->v_type = VAR_CHANNEL;
6435 rettv->vval.v_channel = job->jv_channel;
6436 if (job->jv_channel != NULL)
6437 ++job->jv_channel->ch_refcount;
6438 }
6439}
6440
6441/*
6442 * Implementation of job_info().
6443 */
6444 static void
6445job_info(job_T *job, dict_T *dict)
6446{
6447 dictitem_T *item;
6448 varnumber_T nr;
6449 list_T *l;
6450 int i;
6451
6452 dict_add_string(dict, "status", (char_u *)job_status(job));
6453
6454 item = dictitem_alloc((char_u *)"channel");
6455 if (item == NULL)
6456 return;
6457 item->di_tv.v_type = VAR_CHANNEL;
6458 item->di_tv.vval.v_channel = job->jv_channel;
6459 if (job->jv_channel != NULL)
6460 ++job->jv_channel->ch_refcount;
6461 if (dict_add(dict, item) == FAIL)
6462 dictitem_free(item);
6463
6464#ifdef UNIX
6465 nr = job->jv_pid;
6466#else
6467 nr = job->jv_proc_info.dwProcessId;
6468#endif
6469 dict_add_number(dict, "process", nr);
6470 dict_add_string(dict, "tty_in", job->jv_tty_in);
6471 dict_add_string(dict, "tty_out", job->jv_tty_out);
6472
6473 dict_add_number(dict, "exitval", job->jv_exitval);
6474 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6475 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6476#ifdef UNIX
6477 dict_add_string(dict, "termsig", job->jv_termsig);
6478#endif
6479#ifdef MSWIN
6480 dict_add_string(dict, "tty_type", job->jv_tty_type);
6481#endif
6482
6483 l = list_alloc();
6484 if (l != NULL)
6485 {
6486 dict_add_list(dict, "cmd", l);
6487 if (job->jv_argv != NULL)
6488 for (i = 0; job->jv_argv[i] != NULL; i++)
6489 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6490 }
6491}
6492
6493/*
6494 * Implementation of job_info() to return info for all jobs.
6495 */
6496 static void
6497job_info_all(list_T *l)
6498{
6499 job_T *job;
6500 typval_T tv;
6501
6502 for (job = first_job; job != NULL; job = job->jv_next)
6503 {
6504 tv.v_type = VAR_JOB;
6505 tv.vval.v_job = job;
6506
6507 if (list_append_tv(l, &tv) != OK)
6508 return;
6509 }
6510}
6511
6512/*
6513 * "job_info()" function
6514 */
6515 void
6516f_job_info(typval_T *argvars, typval_T *rettv)
6517{
6518 if (argvars[0].v_type != VAR_UNKNOWN)
6519 {
6520 job_T *job = get_job_arg(&argvars[0]);
6521
6522 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6523 job_info(job, rettv->vval.v_dict);
6524 }
6525 else if (rettv_list_alloc(rettv) == OK)
6526 job_info_all(rettv->vval.v_list);
6527}
6528
6529/*
6530 * "job_setoptions()" function
6531 */
6532 void
6533f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6534{
6535 job_T *job = get_job_arg(&argvars[0]);
6536 jobopt_T opt;
6537
6538 if (job == NULL)
6539 return;
6540 clear_job_options(&opt);
6541 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6542 job_set_options(job, &opt);
6543 free_job_options(&opt);
6544}
6545
6546/*
6547 * "job_start()" function
6548 */
6549 void
6550f_job_start(typval_T *argvars, typval_T *rettv)
6551{
6552 rettv->v_type = VAR_JOB;
6553 if (check_restricted() || check_secure())
6554 return;
6555 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6556}
6557
6558/*
6559 * "job_status()" function
6560 */
6561 void
6562f_job_status(typval_T *argvars, typval_T *rettv)
6563{
6564 job_T *job = get_job_arg(&argvars[0]);
6565
6566 if (job != NULL)
6567 {
6568 rettv->v_type = VAR_STRING;
6569 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6570 }
6571}
6572
6573/*
6574 * "job_stop()" function
6575 */
6576 void
6577f_job_stop(typval_T *argvars, typval_T *rettv)
6578{
6579 job_T *job = get_job_arg(&argvars[0]);
6580
6581 if (job != NULL)
6582 rettv->vval.v_number = job_stop(job, argvars, NULL);
6583}
6584
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006585#endif /* FEAT_JOB_CHANNEL */