blob: f40d16ab966f417d0e24d27c804a83bb01751cb0 [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 Moolenaar5843f5f2019-08-20 20:13:45 +020058static ch_mode_T channel_get_mode(channel_T *channel, ch_part_T part);
59static int channel_get_timeout(channel_T *channel, ch_part_T part);
60static ch_part_T channel_part_send(channel_T *channel);
61static ch_part_T channel_part_read(channel_T *channel);
62static void free_job_options(jobopt_T *opt);
Bram Moolenaarb2658a12016-04-26 17:16:24 +020063
Bram Moolenaar187db502016-02-27 14:44:26 +010064/* Whether a redraw is needed for appending a line to a buffer. */
65static int channel_need_redraw = FALSE;
66
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +020067/* Whether we are inside channel_parse_messages() or another situation where it
68 * is safe to invoke callbacks. */
69static int safe_to_invoke_callback = 0;
Bram Moolenaar187db502016-02-27 14:44:26 +010070
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +020071static char *part_names[] = {"sock", "out", "err", "in"};
72
Bram Moolenaar4f974752019-02-17 17:44:42 +010073#ifdef MSWIN
Bram Moolenaard8070362016-02-15 21:56:54 +010074 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010075fd_read(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010076{
77 HANDLE h = (HANDLE)fd;
78 DWORD nread;
79
80 if (!ReadFile(h, buf, (DWORD)len, &nread, NULL))
81 return -1;
82 return (int)nread;
83}
84
85 static int
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010086fd_write(sock_T fd, char *buf, size_t len)
Bram Moolenaard8070362016-02-15 21:56:54 +010087{
Bram Moolenaar24058382019-01-24 23:11:49 +010088 size_t todo = len;
Bram Moolenaarb091f302019-01-19 14:37:00 +010089 HANDLE h = (HANDLE)fd;
Bram Moolenaar24058382019-01-24 23:11:49 +010090 DWORD nwrite, size, done = 0;
Bram Moolenaarb091f302019-01-19 14:37:00 +010091 OVERLAPPED ov;
Bram Moolenaard8070362016-02-15 21:56:54 +010092
Bram Moolenaar24058382019-01-24 23:11:49 +010093 while (todo > 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +010094 {
Bram Moolenaar24058382019-01-24 23:11:49 +010095 if (todo > MAX_NAMED_PIPE_SIZE)
96 size = MAX_NAMED_PIPE_SIZE;
97 else
Bram Moolenaardec01202019-01-28 20:04:24 +010098 size = (DWORD)todo;
Bram Moolenaar65240682019-02-10 22:23:26 +010099 // If the pipe overflows while the job does not read the data,
100 // WriteFile() will block forever. This abandons the write.
Bram Moolenaar24058382019-01-24 23:11:49 +0100101 memset(&ov, 0, sizeof(ov));
Bram Moolenaar65240682019-02-10 22:23:26 +0100102 nwrite = 0;
Bram Moolenaar24058382019-01-24 23:11:49 +0100103 if (!WriteFile(h, buf + done, size, &nwrite, &ov))
104 {
105 DWORD err = GetLastError();
Bram Moolenaarb091f302019-01-19 14:37:00 +0100106
Bram Moolenaar24058382019-01-24 23:11:49 +0100107 if (err != ERROR_IO_PENDING)
108 return -1;
109 if (!GetOverlappedResult(h, &ov, &nwrite, FALSE))
110 return -1;
111 FlushFileBuffers(h);
112 }
Bram Moolenaar65240682019-02-10 22:23:26 +0100113 else if (nwrite == 0)
114 // WriteFile() returns TRUE but did not write anything. This causes
115 // a hang, so bail out.
116 break;
Bram Moolenaar24058382019-01-24 23:11:49 +0100117 todo -= nwrite;
118 done += nwrite;
Bram Moolenaarb091f302019-01-19 14:37:00 +0100119 }
Bram Moolenaar24058382019-01-24 23:11:49 +0100120 return (int)done;
Bram Moolenaard8070362016-02-15 21:56:54 +0100121}
122
123 static void
124fd_close(sock_T fd)
125{
126 HANDLE h = (HANDLE)fd;
127
128 CloseHandle(h);
129}
130#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100131
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100132/* Log file opened with ch_logfile(). */
133static FILE *log_fd = NULL;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100134#ifdef FEAT_RELTIME
135static proftime_T log_start;
136#endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100137
138 void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100139ch_logfile(char_u *fname, char_u *opt)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100140{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100141 FILE *file = NULL;
142
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100143 if (log_fd != NULL)
144 fclose(log_fd);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100145
146 if (*fname != NUL)
147 {
148 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
149 if (file == NULL)
150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100151 semsg(_(e_notopen), fname);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100152 return;
153 }
154 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100155 log_fd = file;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100156
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100157 if (log_fd != NULL)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100158 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100159 fprintf(log_fd, "==== start log session ====\n");
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100160#ifdef FEAT_RELTIME
161 profile_start(&log_start);
162#endif
163 }
164}
165
166 int
Bram Moolenaarcf089462016-06-12 21:18:43 +0200167ch_log_active(void)
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100168{
169 return log_fd != NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100170}
171
172 static void
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200173ch_log_lead(const char *what, channel_T *ch, ch_part_T part)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100174{
175 if (log_fd != NULL)
176 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100177#ifdef FEAT_RELTIME
178 proftime_T log_now;
179
180 profile_start(&log_now);
181 profile_sub(&log_now, &log_start);
182 fprintf(log_fd, "%s ", profile_msg(&log_now));
183#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100184 if (ch != NULL)
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200185 {
186 if (part < PART_COUNT)
187 fprintf(log_fd, "%son %d(%s): ",
188 what, ch->ch_id, part_names[part]);
189 else
190 fprintf(log_fd, "%son %d: ", what, ch->ch_id);
191 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100192 else
193 fprintf(log_fd, "%s: ", what);
194 }
195}
196
Bram Moolenaard0b65022016-03-06 21:50:33 +0100197static int did_log_msg = TRUE;
198
Bram Moolenaar8caa43d2019-02-20 22:45:06 +0100199#ifndef PROTO // prototype is in proto.h
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100200 void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200201ch_log(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100202{
203 if (log_fd != NULL)
204 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200205 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100206
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200207 ch_log_lead("", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200208 va_start(ap, fmt);
209 vfprintf(log_fd, fmt, ap);
210 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100211 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100212 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100213 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100214 }
215}
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200216#endif
217
218 static void
219ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaare80757c2018-04-10 12:42:44 +0200220#ifdef USE_PRINTF_FORMAT_ATTRIBUTE
221 __attribute__((format(printf, 2, 3)))
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +0200222#endif
223 ;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100224
225 static void
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200226ch_error(channel_T *ch, const char *fmt, ...)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100227{
228 if (log_fd != NULL)
229 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200230 va_list ap;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100231
Bram Moolenaar4b16ee72018-08-09 22:15:34 +0200232 ch_log_lead("ERR ", ch, PART_COUNT);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200233 va_start(ap, fmt);
234 vfprintf(log_fd, fmt, ap);
235 va_end(ap);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100236 fputc('\n', log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100237 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +0100238 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100239 }
240}
Bram Moolenaard04a0202016-01-26 23:30:18 +0100241
Bram Moolenaar4f974752019-02-17 17:44:42 +0100242#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100243# undef PERROR
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100244# define PERROR(msg) (void)semsg("%s: %s", msg, strerror_win32(errno))
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100245
246 static char *
247strerror_win32(int eno)
248{
249 static LPVOID msgbuf = NULL;
250 char_u *ptr;
251
252 if (msgbuf)
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200253 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100254 LocalFree(msgbuf);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200255 msgbuf = NULL;
256 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100257 FormatMessage(
258 FORMAT_MESSAGE_ALLOCATE_BUFFER |
259 FORMAT_MESSAGE_FROM_SYSTEM |
260 FORMAT_MESSAGE_IGNORE_INSERTS,
261 NULL,
262 eno,
263 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
264 (LPTSTR) &msgbuf,
265 0,
266 NULL);
Bram Moolenaaraad30bb2016-06-26 17:31:03 +0200267 if (msgbuf != NULL)
268 /* chomp \r or \n */
269 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
270 switch (*ptr)
271 {
272 case '\r':
273 STRMOVE(ptr, ptr + 1);
274 ptr--;
275 break;
276 case '\n':
277 if (*(ptr + 1) == '\0')
278 *ptr = '\0';
279 else
280 *ptr = ' ';
281 break;
282 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100283 return msgbuf;
284}
285#endif
286
Bram Moolenaar77073442016-02-13 23:23:53 +0100287/*
288 * The list of all allocated channels.
289 */
290static channel_T *first_channel = NULL;
291static int next_ch_id = 0;
292
293/*
294 * Allocate a new channel. The refcount is set to 1.
295 * The channel isn't actually used until it is opened.
296 * Returns NULL if out of memory.
297 */
298 channel_T *
299add_channel(void)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100300{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200301 ch_part_T part;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200302 channel_T *channel = ALLOC_CLEAR_ONE(channel_T);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100303
Bram Moolenaar77073442016-02-13 23:23:53 +0100304 if (channel == NULL)
305 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100306
Bram Moolenaar77073442016-02-13 23:23:53 +0100307 channel->ch_id = next_ch_id++;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100308 ch_log(channel, "Created channel");
Bram Moolenaar77073442016-02-13 23:23:53 +0100309
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200310 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100311 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100312 channel->ch_part[part].ch_fd = INVALID_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100313#ifdef FEAT_GUI_X11
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100314 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100315#endif
316#ifdef FEAT_GUI_GTK
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100317 channel->ch_part[part].ch_inputHandler = 0;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100318#endif
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100319 channel->ch_part[part].ch_timeout = 2000;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100320 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100321
Bram Moolenaar77073442016-02-13 23:23:53 +0100322 if (first_channel != NULL)
323 {
324 first_channel->ch_prev = channel;
325 channel->ch_next = first_channel;
326 }
327 first_channel = channel;
328
329 channel->ch_refcount = 1;
330 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100331}
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100332
Bram Moolenaarb9c31e72016-09-29 15:18:57 +0200333 int
334has_any_channel(void)
335{
336 return first_channel != NULL;
337}
338
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100339/*
Bram Moolenaard6051b52016-02-28 15:49:03 +0100340 * Called when the refcount of a channel is zero.
Bram Moolenaar46c85432016-02-26 11:17:46 +0100341 * Return TRUE if "channel" has a callback and the associated job wasn't
342 * killed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100343 */
344 static int
Bram Moolenaar46c85432016-02-26 11:17:46 +0100345channel_still_useful(channel_T *channel)
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100346{
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100347 int has_sock_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100348 int has_out_msg;
349 int has_err_msg;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100350
351 /* If the job was killed the channel is not expected to work anymore. */
Bram Moolenaar46c85432016-02-26 11:17:46 +0100352 if (channel->ch_job_killed && channel->ch_job == NULL)
353 return FALSE;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100354
355 /* If there is a close callback it may still need to be invoked. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200356 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100357 return TRUE;
358
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200359 /* If reading from or a buffer it's still useful. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200360 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
Bram Moolenaar5d96e3a2016-05-08 21:47:01 +0200361 return TRUE;
362
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100363 /* If there is no callback then nobody can get readahead. If the fd is
364 * closed and there is no readahead then the callback won't be called. */
365 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
Bram Moolenaard23a8232018-02-10 18:45:26 +0100366 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
367 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
Bram Moolenaarfdd6ce42016-02-28 22:21:38 +0100368 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
369 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
370 || channel->ch_part[PART_OUT].ch_json_head.jq_next != NULL;
371 has_err_msg = channel->ch_part[PART_ERR].ch_fd != INVALID_FD
372 || channel->ch_part[PART_ERR].ch_head.rq_next != NULL
373 || channel->ch_part[PART_ERR].ch_json_head.jq_next != NULL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200374 return (channel->ch_callback.cb_name != NULL && (has_sock_msg
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100375 || has_out_msg || has_err_msg))
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200376 || ((channel->ch_part[PART_OUT].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200377 || channel->ch_part[PART_OUT].ch_bufref.br_buf != NULL)
378 && has_out_msg)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200379 || ((channel->ch_part[PART_ERR].ch_callback.cb_name != NULL
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200380 || channel->ch_part[PART_ERR].ch_bufref.br_buf != NULL)
381 && has_err_msg);
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100382}
383
384/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200385 * Return TRUE if "channel" is closeable (i.e. all readable fds are closed).
386 */
387 static int
388channel_can_close(channel_T *channel)
389{
390 return channel->ch_to_be_closed == 0;
391}
392
393/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200394 * Close a channel and free all its resources.
395 */
396 static void
397channel_free_contents(channel_T *channel)
398{
399 channel_close(channel, TRUE);
400 channel_clear(channel);
401 ch_log(channel, "Freeing channel");
402}
403
404 static void
405channel_free_channel(channel_T *channel)
406{
407 if (channel->ch_next != NULL)
408 channel->ch_next->ch_prev = channel->ch_prev;
409 if (channel->ch_prev == NULL)
410 first_channel = channel->ch_next;
411 else
412 channel->ch_prev->ch_next = channel->ch_next;
413 vim_free(channel);
414}
415
416 static void
417channel_free(channel_T *channel)
418{
419 if (!in_free_unref_items)
420 {
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200421 if (safe_to_invoke_callback == 0)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200422 channel->ch_to_be_freed = TRUE;
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200423 else
424 {
425 channel_free_contents(channel);
426 channel_free_channel(channel);
427 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200428 }
429}
430
431/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100432 * Close a channel and free all its resources if there is no further action
Bram Moolenaar46c85432016-02-26 11:17:46 +0100433 * possible, there is no callback to be invoked or the associated job was
434 * killed.
Bram Moolenaar70765942016-02-28 19:28:59 +0100435 * Return TRUE if the channel was freed.
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100436 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100437 static int
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100438channel_may_free(channel_T *channel)
439{
Bram Moolenaar46c85432016-02-26 11:17:46 +0100440 if (!channel_still_useful(channel))
Bram Moolenaar70765942016-02-28 19:28:59 +0100441 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100442 channel_free(channel);
Bram Moolenaar70765942016-02-28 19:28:59 +0100443 return TRUE;
444 }
445 return FALSE;
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +0100446}
447
448/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100449 * Decrement the reference count on "channel" and maybe free it when it goes
450 * down to zero. Don't free it if there is a pending action.
451 * Returns TRUE when the channel is no longer referenced.
452 */
453 int
454channel_unref(channel_T *channel)
455{
456 if (channel != NULL && --channel->ch_refcount <= 0)
457 return channel_may_free(channel);
458 return FALSE;
459}
460
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200461 int
462free_unused_channels_contents(int copyID, int mask)
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100463{
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464 int did_free = FALSE;
465 channel_T *ch;
466
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200467 /* This is invoked from the garbage collector, which only runs at a safe
468 * point. */
469 ++safe_to_invoke_callback;
470
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200471 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
Bram Moolenaar674127e2016-04-26 20:30:07 +0200472 if (!channel_still_useful(ch)
473 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200474 {
475 /* Free the channel and ordinary items it contains, but don't
476 * recurse into Lists, Dictionaries etc. */
477 channel_free_contents(ch);
478 did_free = TRUE;
479 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +0200480
481 --safe_to_invoke_callback;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200482 return did_free;
483}
484
485 void
486free_unused_channels(int copyID, int mask)
487{
488 channel_T *ch;
489 channel_T *ch_next;
490
491 for (ch = first_channel; ch != NULL; ch = ch_next)
492 {
493 ch_next = ch->ch_next;
Bram Moolenaar674127e2016-04-26 20:30:07 +0200494 if (!channel_still_useful(ch)
495 && (ch->ch_copyID & mask) != (copyID & mask))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200496 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200497 /* Free the channel struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200498 channel_free_channel(ch);
499 }
500 }
Bram Moolenaare0874f82016-01-24 20:36:41 +0100501}
502
Bram Moolenaard04a0202016-01-26 23:30:18 +0100503#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar77073442016-02-13 23:23:53 +0100504
Bram Moolenaarea781452019-09-04 18:53:12 +0200505# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
506/*
507 * Lookup the channel from the socket. Set "partp" to the fd index.
508 * Returns NULL when the socket isn't found.
509 */
510 static channel_T *
511channel_fd2channel(sock_T fd, ch_part_T *partp)
512{
513 channel_T *channel;
514 ch_part_T part;
515
516 if (fd != INVALID_FD)
517 for (channel = first_channel; channel != NULL;
518 channel = channel->ch_next)
519 {
520 for (part = PART_SOCK; part < PART_IN; ++part)
521 if (channel->ch_part[part].ch_fd == fd)
522 {
523 *partp = part;
524 return channel;
525 }
526 }
527 return NULL;
528}
529
Bram Moolenaar77073442016-02-13 23:23:53 +0100530 static void
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100531channel_read_fd(int fd)
Bram Moolenaar77073442016-02-13 23:23:53 +0100532{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100533 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200534 ch_part_T part;
Bram Moolenaar77073442016-02-13 23:23:53 +0100535
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100536 channel = channel_fd2channel(fd, &part);
Bram Moolenaar77073442016-02-13 23:23:53 +0100537 if (channel == NULL)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200538 ch_error(NULL, "Channel for fd %d not found", fd);
Bram Moolenaar77073442016-02-13 23:23:53 +0100539 else
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200540 channel_read(channel, part, "channel_read_fd");
Bram Moolenaar77073442016-02-13 23:23:53 +0100541}
Bram Moolenaarea781452019-09-04 18:53:12 +0200542# endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100543
Bram Moolenaare0874f82016-01-24 20:36:41 +0100544/*
Bram Moolenaard04a0202016-01-26 23:30:18 +0100545 * Read a command from netbeans.
Bram Moolenaare0874f82016-01-24 20:36:41 +0100546 */
Bram Moolenaarea781452019-09-04 18:53:12 +0200547# ifdef FEAT_GUI_X11
Bram Moolenaard04a0202016-01-26 23:30:18 +0100548 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200549messageFromServerX11(XtPointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200550 int *unused1 UNUSED,
551 XtInputId *unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100552{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100553 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100554}
Bram Moolenaarea781452019-09-04 18:53:12 +0200555# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100556
Bram Moolenaarea781452019-09-04 18:53:12 +0200557# ifdef FEAT_GUI_GTK
558# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100559 static gboolean
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200560messageFromServerGtk3(GIOChannel *unused1 UNUSED,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200561 GIOCondition unused2 UNUSED,
562 gpointer clientData)
Bram Moolenaar98921892016-02-23 17:14:37 +0100563{
564 channel_read_fd(GPOINTER_TO_INT(clientData));
565 return TRUE; /* Return FALSE instead in case the event source is to
566 * be removed after this function returns. */
567}
Bram Moolenaarea781452019-09-04 18:53:12 +0200568# else
Bram Moolenaard04a0202016-01-26 23:30:18 +0100569 static void
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200570messageFromServerGtk2(gpointer clientData,
Bram Moolenaarf8df45d2016-05-25 21:48:13 +0200571 gint unused1 UNUSED,
572 GdkInputCondition unused2 UNUSED)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100573{
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100574 channel_read_fd((int)(long)clientData);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100575}
Bram Moolenaarea781452019-09-04 18:53:12 +0200576# endif
Bram Moolenaar98921892016-02-23 17:14:37 +0100577# endif
Bram Moolenaare0874f82016-01-24 20:36:41 +0100578
579 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200580channel_gui_register_one(channel_T *channel, ch_part_T part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100581{
Bram Moolenaarde279892016-03-11 22:19:44 +0100582 if (!CH_HAS_GUI)
583 return;
584
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200585 /* gets stuck in handling events for a not connected channel */
586 if (channel->ch_keep_open)
587 return;
588
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100589# ifdef FEAT_GUI_X11
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200590 /* Tell notifier we are interested in being called when there is input on
591 * the editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100592 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200593 {
594 ch_log(channel, "Registering part %s with fd %d",
595 part_names[part], channel->ch_part[part].ch_fd);
596
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100597 channel->ch_part[part].ch_inputHandler = XtAppAddInput(
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100598 (XtAppContext)app_context,
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100599 channel->ch_part[part].ch_fd,
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100600 (XtPointer)(XtInputReadMask + XtInputExceptMask),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200601 messageFromServerX11,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100602 (XtPointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200603 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100604# else
605# ifdef FEAT_GUI_GTK
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200606 /* Tell gdk we are interested in being called when there is input on the
607 * editor connection socket. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100608 if (channel->ch_part[part].ch_inputHandler == 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100609 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200610 ch_log(channel, "Registering part %s with fd %d",
611 part_names[part], channel->ch_part[part].ch_fd);
612# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100613 GIOChannel *chnnl = g_io_channel_unix_new(
614 (gint)channel->ch_part[part].ch_fd);
615
616 channel->ch_part[part].ch_inputHandler = g_io_add_watch(
617 chnnl,
618 G_IO_IN|G_IO_HUP|G_IO_ERR|G_IO_PRI,
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200619 messageFromServerGtk3,
Bram Moolenaar98921892016-02-23 17:14:37 +0100620 GINT_TO_POINTER(channel->ch_part[part].ch_fd));
621
622 g_io_channel_unref(chnnl);
Bram Moolenaar98921892016-02-23 17:14:37 +0100623# else
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100624 channel->ch_part[part].ch_inputHandler = gdk_input_add(
625 (gint)channel->ch_part[part].ch_fd,
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100626 (GdkInputCondition)
627 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200628 messageFromServerGtk2,
Bram Moolenaarfffd5562016-02-20 18:44:39 +0100629 (gpointer)(long)channel->ch_part[part].ch_fd);
Bram Moolenaar98921892016-02-23 17:14:37 +0100630# endif
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200631 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100632# endif
633# endif
634}
635
Bram Moolenaarde279892016-03-11 22:19:44 +0100636 static void
Bram Moolenaar77073442016-02-13 23:23:53 +0100637channel_gui_register(channel_T *channel)
Bram Moolenaare0874f82016-01-24 20:36:41 +0100638{
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100639 if (channel->CH_SOCK_FD != INVALID_FD)
640 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200641 if (channel->CH_OUT_FD != INVALID_FD
642 && channel->CH_OUT_FD != channel->CH_SOCK_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100643 channel_gui_register_one(channel, PART_OUT);
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200644 if (channel->CH_ERR_FD != INVALID_FD
645 && channel->CH_ERR_FD != channel->CH_SOCK_FD
646 && channel->CH_ERR_FD != channel->CH_OUT_FD)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100647 channel_gui_register_one(channel, PART_ERR);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100648}
649
650/*
651 * Register any of our file descriptors with the GUI event handling system.
652 * Called when the GUI has started.
653 */
654 void
655channel_gui_register_all(void)
656{
Bram Moolenaar77073442016-02-13 23:23:53 +0100657 channel_T *channel;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100658
Bram Moolenaar77073442016-02-13 23:23:53 +0100659 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100660 channel_gui_register(channel);
661}
662
663 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200664channel_gui_unregister_one(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100665{
666# ifdef FEAT_GUI_X11
667 if (channel->ch_part[part].ch_inputHandler != (XtInputId)NULL)
668 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200669 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100670 XtRemoveInput(channel->ch_part[part].ch_inputHandler);
671 channel->ch_part[part].ch_inputHandler = (XtInputId)NULL;
672 }
673# else
674# ifdef FEAT_GUI_GTK
675 if (channel->ch_part[part].ch_inputHandler != 0)
676 {
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200677 ch_log(channel, "Unregistering part %s", part_names[part]);
Bram Moolenaarde279892016-03-11 22:19:44 +0100678# if GTK_CHECK_VERSION(3,0,0)
679 g_source_remove(channel->ch_part[part].ch_inputHandler);
680# else
681 gdk_input_remove(channel->ch_part[part].ch_inputHandler);
682# endif
683 channel->ch_part[part].ch_inputHandler = 0;
684 }
685# endif
686# endif
687}
688
689 static void
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100690channel_gui_unregister(channel_T *channel)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100691{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +0200692 ch_part_T part;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +0100693
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100694 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarde279892016-03-11 22:19:44 +0100695 channel_gui_unregister_one(channel, part);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100696}
697
Bram Moolenaarea781452019-09-04 18:53:12 +0200698#endif // FEAT_GUI
Bram Moolenaard04a0202016-01-26 23:30:18 +0100699
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100700static char *e_cannot_connect = N_("E902: Cannot connect to port");
701
Bram Moolenaard04a0202016-01-26 23:30:18 +0100702/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100703 * Open a socket channel to "hostname":"port".
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100704 * "waittime" is the time in msec to wait for the connection.
705 * When negative wait forever.
Bram Moolenaar77073442016-02-13 23:23:53 +0100706 * Returns the channel for success.
707 * Returns NULL for failure.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100708 */
Bram Moolenaar77073442016-02-13 23:23:53 +0100709 channel_T *
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100710channel_open(
711 char *hostname,
712 int port_in,
713 int waittime,
714 void (*nb_close_cb)(void))
Bram Moolenaard04a0202016-01-26 23:30:18 +0100715{
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100716 int sd = -1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100717 struct sockaddr_in server;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100718 struct hostent *host;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100719#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100720 u_short port = port_in;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100721 u_long val = 1;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100722#else
723 int port = port_in;
724#endif
Bram Moolenaar77073442016-02-13 23:23:53 +0100725 channel_T *channel;
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100726 int ret;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100727
Bram Moolenaar4f974752019-02-17 17:44:42 +0100728#ifdef MSWIN
Bram Moolenaard04a0202016-01-26 23:30:18 +0100729 channel_init_winsock();
730#endif
731
Bram Moolenaar77073442016-02-13 23:23:53 +0100732 channel = add_channel();
733 if (channel == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100734 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100735 ch_error(NULL, "Cannot allocate channel.");
Bram Moolenaar77073442016-02-13 23:23:53 +0100736 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100737 }
738
739 /* Get the server internet address and put into addr structure */
740 /* fill in the socket address structure and connect to server */
741 vim_memset((char *)&server, 0, sizeof(server));
742 server.sin_family = AF_INET;
743 server.sin_port = htons(port);
744 if ((host = gethostbyname(hostname)) == NULL)
745 {
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100746 ch_error(channel, "in gethostbyname() in channel_open()");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200747 PERROR(_("E901: gethostbyname() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100748 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100749 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100750 }
Bram Moolenaar7173b472017-01-14 17:04:38 +0100751 {
752 char *p;
753
Bram Moolenaar2e324952018-04-14 14:37:07 +0200754 /* When using host->h_addr_list[0] directly ubsan warns for it to not
755 * be aligned. First copy the pointer to avoid that. */
756 memcpy(&p, &host->h_addr_list[0], sizeof(p));
Bram Moolenaar7173b472017-01-14 17:04:38 +0100757 memcpy((char *)&server.sin_addr, p, host->h_length);
758 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100759
Bram Moolenaar254e00d2016-02-19 23:23:12 +0100760 /* On Mac and Solaris a zero timeout almost never works. At least wait
761 * one millisecond. Let's do it for all systems, because we don't know why
762 * this is needed. */
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100763 if (waittime == 0)
764 waittime = 1;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100765
766 /*
767 * For Unix we need to call connect() again after connect() failed.
768 * On Win32 one time is sufficient.
769 */
770 while (TRUE)
771 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100772 long elapsed_msec = 0;
773 int waitnow;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100774
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100775 if (sd >= 0)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100776 sock_close(sd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100777 sd = socket(AF_INET, SOCK_STREAM, 0);
778 if (sd == -1)
779 {
780 ch_error(channel, "in socket() in channel_open().");
Bram Moolenaar5b302912016-08-24 22:11:55 +0200781 PERROR(_("E898: socket() in channel_open()"));
Bram Moolenaar7b3ca762016-02-14 19:13:43 +0100782 channel_free(channel);
Bram Moolenaar77073442016-02-13 23:23:53 +0100783 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100784 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100785
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100786 if (waittime >= 0)
787 {
788 /* Make connect() non-blocking. */
789 if (
Bram Moolenaar4f974752019-02-17 17:44:42 +0100790#ifdef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100791 ioctlsocket(sd, FIONBIO, &val) < 0
792#else
793 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
794#endif
795 )
796 {
797 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200798 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100799 "channel_open: Connect failed with errno %d", errno);
800 sock_close(sd);
801 channel_free(channel);
802 return NULL;
803 }
804 }
805
806 /* Try connecting to the server. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200807 ch_log(channel, "Connecting to %s port %d", hostname, port);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100808 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
809
Bram Moolenaar045a2842016-03-08 22:33:07 +0100810 if (ret == 0)
811 /* The connection could be established. */
812 break;
813
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100814 SOCK_ERRNO;
Bram Moolenaar045a2842016-03-08 22:33:07 +0100815 if (waittime < 0 || (errno != EWOULDBLOCK
816 && errno != ECONNREFUSED
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100817#ifdef EINPROGRESS
Bram Moolenaar045a2842016-03-08 22:33:07 +0100818 && errno != EINPROGRESS
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100819#endif
Bram Moolenaar045a2842016-03-08 22:33:07 +0100820 ))
821 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200822 ch_error(channel,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100823 "channel_open: Connect failed with errno %d", errno);
824 PERROR(_(e_cannot_connect));
825 sock_close(sd);
826 channel_free(channel);
827 return NULL;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100828 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100829
Bram Moolenaar40e8cb22016-03-10 21:10:58 +0100830 /* Limit the waittime to 50 msec. If it doesn't work within this
831 * time we close the socket and try creating it again. */
832 waitnow = waittime > 50 ? 50 : waittime;
833
Bram Moolenaar045a2842016-03-08 22:33:07 +0100834 /* If connect() didn't finish then try using select() to wait for the
Bram Moolenaar562ca712016-03-09 21:50:05 +0100835 * connection to be made. For Win32 always use select() to wait. */
Bram Moolenaar4f974752019-02-17 17:44:42 +0100836#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100837 if (errno != ECONNREFUSED)
838#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100839 {
840 struct timeval tv;
Bram Moolenaard42119f2016-02-28 20:51:49 +0100841 fd_set rfds;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100842 fd_set wfds;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100843#ifndef MSWIN
Bram Moolenaard42119f2016-02-28 20:51:49 +0100844 int so_error = 0;
845 socklen_t so_error_len = sizeof(so_error);
Bram Moolenaar045a2842016-03-08 22:33:07 +0100846 struct timeval start_tv;
847 struct timeval end_tv;
Bram Moolenaare081e212016-02-28 22:33:46 +0100848#endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100849 FD_ZERO(&rfds);
850 FD_SET(sd, &rfds);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100851 FD_ZERO(&wfds);
852 FD_SET(sd, &wfds);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100853
Bram Moolenaar562ca712016-03-09 21:50:05 +0100854 tv.tv_sec = waitnow / 1000;
855 tv.tv_usec = (waitnow % 1000) * 1000;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100856#ifndef MSWIN
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100857 gettimeofday(&start_tv, NULL);
858#endif
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200859 ch_log(channel,
Bram Moolenaar562ca712016-03-09 21:50:05 +0100860 "Waiting for connection (waiting %d msec)...", waitnow);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100861 ret = select((int)sd + 1, &rfds, &wfds, NULL, &tv);
Bram Moolenaare74e8e72016-02-16 22:01:30 +0100862
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100863 if (ret < 0)
864 {
865 SOCK_ERRNO;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200866 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100867 "channel_open: Connect failed with errno %d", errno);
868 PERROR(_(e_cannot_connect));
869 sock_close(sd);
870 channel_free(channel);
871 return NULL;
872 }
Bram Moolenaard42119f2016-02-28 20:51:49 +0100873
Bram Moolenaar4f974752019-02-17 17:44:42 +0100874#ifdef MSWIN
Bram Moolenaar562ca712016-03-09 21:50:05 +0100875 /* On Win32: select() is expected to work and wait for up to
876 * "waitnow" msec for the socket to be open. */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100877 if (FD_ISSET(sd, &wfds))
878 break;
Bram Moolenaar562ca712016-03-09 21:50:05 +0100879 elapsed_msec = waitnow;
880 if (waittime > 1 && elapsed_msec < waittime)
881 {
882 waittime -= elapsed_msec;
883 continue;
884 }
Bram Moolenaare081e212016-02-28 22:33:46 +0100885#else
886 /* On Linux-like systems: See socket(7) for the behavior
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100887 * After putting the socket in non-blocking mode, connect() will
888 * return EINPROGRESS, select() will not wait (as if writing is
889 * possible), need to use getsockopt() to check if the socket is
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100890 * actually able to connect.
Bram Moolenaar045a2842016-03-08 22:33:07 +0100891 * We detect a failure to connect when either read and write fds
Bram Moolenaard42119f2016-02-28 20:51:49 +0100892 * are set. Use getsockopt() to find out what kind of failure. */
Bram Moolenaar42bc6dd2016-03-02 20:48:47 +0100893 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
Bram Moolenaard42119f2016-02-28 20:51:49 +0100894 {
895 ret = getsockopt(sd,
Bram Moolenaar045a2842016-03-08 22:33:07 +0100896 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
Bram Moolenaard42119f2016-02-28 20:51:49 +0100897 if (ret < 0 || (so_error != 0
898 && so_error != EWOULDBLOCK
899 && so_error != ECONNREFUSED
Bram Moolenaare081e212016-02-28 22:33:46 +0100900# ifdef EINPROGRESS
Bram Moolenaard42119f2016-02-28 20:51:49 +0100901 && so_error != EINPROGRESS
Bram Moolenaare081e212016-02-28 22:33:46 +0100902# endif
Bram Moolenaard42119f2016-02-28 20:51:49 +0100903 ))
904 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200905 ch_error(channel,
Bram Moolenaard42119f2016-02-28 20:51:49 +0100906 "channel_open: Connect failed with errno %d",
907 so_error);
908 PERROR(_(e_cannot_connect));
909 sock_close(sd);
910 channel_free(channel);
911 return NULL;
912 }
913 }
914
Bram Moolenaar045a2842016-03-08 22:33:07 +0100915 if (FD_ISSET(sd, &wfds) && so_error == 0)
916 /* Did not detect an error, connection is established. */
917 break;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100918
Bram Moolenaar045a2842016-03-08 22:33:07 +0100919 gettimeofday(&end_tv, NULL);
920 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
921 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100922#endif
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100923 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100924
Bram Moolenaar4f974752019-02-17 17:44:42 +0100925#ifndef MSWIN
Bram Moolenaar045a2842016-03-08 22:33:07 +0100926 if (waittime > 1 && elapsed_msec < waittime)
927 {
928 /* The port isn't ready but we also didn't get an error.
929 * This happens when the server didn't open the socket
Bram Moolenaar562ca712016-03-09 21:50:05 +0100930 * yet. Select() may return early, wait until the remaining
931 * "waitnow" and try again. */
932 waitnow -= elapsed_msec;
933 waittime -= elapsed_msec;
934 if (waitnow > 0)
935 {
936 mch_delay((long)waitnow, TRUE);
937 ui_breakcheck();
938 waittime -= waitnow;
939 }
Bram Moolenaar045a2842016-03-08 22:33:07 +0100940 if (!got_int)
941 {
Bram Moolenaar562ca712016-03-09 21:50:05 +0100942 if (waittime <= 0)
943 /* give it one more try */
Bram Moolenaar045a2842016-03-08 22:33:07 +0100944 waittime = 1;
945 continue;
946 }
947 /* we were interrupted, behave as if timed out */
948 }
949#endif
950
951 /* We timed out. */
952 ch_error(channel, "Connection timed out");
953 sock_close(sd);
954 channel_free(channel);
955 return NULL;
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100956 }
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100957
Bram Moolenaar045a2842016-03-08 22:33:07 +0100958 ch_log(channel, "Connection made");
959
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100960 if (waittime >= 0)
961 {
Bram Moolenaar4f974752019-02-17 17:44:42 +0100962#ifdef MSWIN
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100963 val = 0;
964 ioctlsocket(sd, FIONBIO, &val);
965#else
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100966 (void)fcntl(sd, F_SETFL, 0);
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100967#endif
968 }
969
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100970 channel->CH_SOCK_FD = (sock_T)sd;
Bram Moolenaar4e221c92016-02-23 13:20:22 +0100971 channel->ch_nb_close_cb = nb_close_cb;
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100972 channel->ch_hostname = (char *)vim_strsave((char_u *)hostname);
973 channel->ch_port = port_in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +0200974 channel->ch_to_be_closed |= (1U << PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100975
976#ifdef FEAT_GUI
Bram Moolenaarde279892016-03-11 22:19:44 +0100977 channel_gui_register_one(channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100978#endif
979
Bram Moolenaar77073442016-02-13 23:23:53 +0100980 return channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100981}
982
Bram Moolenaar8e2c9422016-03-12 13:43:33 +0100983/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +0200984 * Copy callback from "src" to "dest", incrementing the refcounts.
985 */
986 static void
987copy_callback(callback_T *dest, callback_T *src)
988{
989 dest->cb_partial = src->cb_partial;
990 if (dest->cb_partial != NULL)
991 {
992 dest->cb_name = src->cb_name;
993 dest->cb_free_name = FALSE;
994 ++dest->cb_partial->pt_refcount;
995 }
996 else
997 {
998 dest->cb_name = vim_strsave(src->cb_name);
999 dest->cb_free_name = TRUE;
1000 func_ref(src->cb_name);
1001 }
1002}
1003
1004 static void
1005free_set_callback(callback_T *cbp, callback_T *callback)
1006{
1007 free_callback(cbp);
1008
1009 if (callback->cb_name != NULL && *callback->cb_name != NUL)
1010 copy_callback(cbp, callback);
1011 else
1012 cbp->cb_name = NULL;
1013}
1014
1015/*
1016 * Prepare buffer "buf" for writing channel output to.
1017 */
1018 static void
1019prepare_buffer(buf_T *buf)
1020{
1021 buf_T *save_curbuf = curbuf;
1022
1023 buf_copy_options(buf, BCO_ENTER);
1024 curbuf = buf;
1025#ifdef FEAT_QUICKFIX
1026 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
1027 set_option_value((char_u *)"bh", 0L, (char_u *)"hide", OPT_LOCAL);
1028#endif
1029 if (curbuf->b_ml.ml_mfp == NULL)
1030 ml_open(curbuf);
1031 curbuf = save_curbuf;
1032}
1033
1034/*
1035 * Find a buffer matching "name" or create a new one.
1036 * Returns NULL if there is something very wrong (error already reported).
1037 */
1038 static buf_T *
Bram Moolenaar261f3462019-09-07 15:45:32 +02001039channel_find_buffer(char_u *name, int err, int msg)
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02001040{
1041 buf_T *buf = NULL;
1042 buf_T *save_curbuf = curbuf;
1043
1044 if (name != NULL && *name != NUL)
1045 {
1046 buf = buflist_findname(name);
1047 if (buf == NULL)
1048 buf = buflist_findname_exp(name);
1049 }
1050 if (buf == NULL)
1051 {
1052 buf = buflist_new(name == NULL || *name == NUL ? NULL : name,
1053 NULL, (linenr_T)0, BLN_LISTED | BLN_NEW);
1054 if (buf == NULL)
1055 return NULL;
1056 prepare_buffer(buf);
1057
1058 curbuf = buf;
1059 if (msg)
1060 ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1061 : "Reading from channel output..."), TRUE);
1062 changed_bytes(1, 0);
1063 curbuf = save_curbuf;
1064 }
1065
1066 return buf;
1067}
1068
1069/*
1070 * Set various properties from an "opt" argument.
1071 */
1072 static void
1073channel_set_options(channel_T *channel, jobopt_T *opt)
1074{
1075 ch_part_T part;
1076
1077 if (opt->jo_set & JO_MODE)
1078 for (part = PART_SOCK; part < PART_COUNT; ++part)
1079 channel->ch_part[part].ch_mode = opt->jo_mode;
1080 if (opt->jo_set & JO_IN_MODE)
1081 channel->ch_part[PART_IN].ch_mode = opt->jo_in_mode;
1082 if (opt->jo_set & JO_OUT_MODE)
1083 channel->ch_part[PART_OUT].ch_mode = opt->jo_out_mode;
1084 if (opt->jo_set & JO_ERR_MODE)
1085 channel->ch_part[PART_ERR].ch_mode = opt->jo_err_mode;
1086 channel->ch_nonblock = opt->jo_noblock;
1087
1088 if (opt->jo_set & JO_TIMEOUT)
1089 for (part = PART_SOCK; part < PART_COUNT; ++part)
1090 channel->ch_part[part].ch_timeout = opt->jo_timeout;
1091 if (opt->jo_set & JO_OUT_TIMEOUT)
1092 channel->ch_part[PART_OUT].ch_timeout = opt->jo_out_timeout;
1093 if (opt->jo_set & JO_ERR_TIMEOUT)
1094 channel->ch_part[PART_ERR].ch_timeout = opt->jo_err_timeout;
1095 if (opt->jo_set & JO_BLOCK_WRITE)
1096 channel->ch_part[PART_IN].ch_block_write = 1;
1097
1098 if (opt->jo_set & JO_CALLBACK)
1099 free_set_callback(&channel->ch_callback, &opt->jo_callback);
1100 if (opt->jo_set & JO_OUT_CALLBACK)
1101 free_set_callback(&channel->ch_part[PART_OUT].ch_callback,
1102 &opt->jo_out_cb);
1103 if (opt->jo_set & JO_ERR_CALLBACK)
1104 free_set_callback(&channel->ch_part[PART_ERR].ch_callback,
1105 &opt->jo_err_cb);
1106 if (opt->jo_set & JO_CLOSE_CALLBACK)
1107 free_set_callback(&channel->ch_close_cb, &opt->jo_close_cb);
1108 channel->ch_drop_never = opt->jo_drop_never;
1109
1110 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1111 {
1112 buf_T *buf;
1113
1114 /* writing output to a buffer. Default mode is NL. */
1115 if (!(opt->jo_set & JO_OUT_MODE))
1116 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
1117 if (opt->jo_set & JO_OUT_BUF)
1118 {
1119 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1120 if (buf == NULL)
1121 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_OUT]);
1122 }
1123 else
1124 {
1125 int msg = TRUE;
1126
1127 if (opt->jo_set2 & JO2_OUT_MSG)
1128 msg = opt->jo_message[PART_OUT];
Bram Moolenaar261f3462019-09-07 15:45:32 +02001129 buf = channel_find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02001130 }
1131 if (buf != NULL)
1132 {
1133 if (opt->jo_set & JO_OUT_MODIFIABLE)
1134 channel->ch_part[PART_OUT].ch_nomodifiable =
1135 !opt->jo_modifiable[PART_OUT];
1136
1137 if (!buf->b_p_ma && !channel->ch_part[PART_OUT].ch_nomodifiable)
1138 {
1139 emsg(_(e_modifiable));
1140 }
1141 else
1142 {
1143 ch_log(channel, "writing out to buffer '%s'",
1144 (char *)buf->b_ffname);
1145 set_bufref(&channel->ch_part[PART_OUT].ch_bufref, buf);
1146 // if the buffer was deleted or unloaded resurrect it
1147 if (buf->b_ml.ml_mfp == NULL)
1148 prepare_buffer(buf);
1149 }
1150 }
1151 }
1152
1153 if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
1154 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1155 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1156 {
1157 buf_T *buf;
1158
1159 /* writing err to a buffer. Default mode is NL. */
1160 if (!(opt->jo_set & JO_ERR_MODE))
1161 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1162 if (opt->jo_io[PART_ERR] == JIO_OUT)
1163 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1164 else if (opt->jo_set & JO_ERR_BUF)
1165 {
1166 buf = buflist_findnr(opt->jo_io_buf[PART_ERR]);
1167 if (buf == NULL)
1168 semsg(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
1169 }
1170 else
1171 {
1172 int msg = TRUE;
1173
1174 if (opt->jo_set2 & JO2_ERR_MSG)
1175 msg = opt->jo_message[PART_ERR];
Bram Moolenaar261f3462019-09-07 15:45:32 +02001176 buf = channel_find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02001177 }
1178 if (buf != NULL)
1179 {
1180 if (opt->jo_set & JO_ERR_MODIFIABLE)
1181 channel->ch_part[PART_ERR].ch_nomodifiable =
1182 !opt->jo_modifiable[PART_ERR];
1183 if (!buf->b_p_ma && !channel->ch_part[PART_ERR].ch_nomodifiable)
1184 {
1185 emsg(_(e_modifiable));
1186 }
1187 else
1188 {
1189 ch_log(channel, "writing err to buffer '%s'",
1190 (char *)buf->b_ffname);
1191 set_bufref(&channel->ch_part[PART_ERR].ch_bufref, buf);
1192 // if the buffer was deleted or unloaded resurrect it
1193 if (buf->b_ml.ml_mfp == NULL)
1194 prepare_buffer(buf);
1195 }
1196 }
1197 }
1198
1199 channel->ch_part[PART_OUT].ch_io = opt->jo_io[PART_OUT];
1200 channel->ch_part[PART_ERR].ch_io = opt->jo_io[PART_ERR];
1201 channel->ch_part[PART_IN].ch_io = opt->jo_io[PART_IN];
1202}
1203
1204/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001205 * Implements ch_open().
1206 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001207 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001208channel_open_func(typval_T *argvars)
1209{
1210 char_u *address;
1211 char_u *p;
1212 char *rest;
1213 int port;
1214 jobopt_T opt;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001215 channel_T *channel = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001217 address = tv_get_string(&argvars[0]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001218 if (argvars[1].v_type != VAR_UNKNOWN
1219 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
1220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001222 return NULL;
1223 }
1224
1225 /* parse address */
1226 p = vim_strchr(address, ':');
1227 if (p == NULL)
1228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001230 return NULL;
1231 }
1232 *p++ = NUL;
1233 port = strtol((char *)p, &rest, 10);
1234 if (*address == NUL || port <= 0 || *rest != NUL)
1235 {
1236 p[-1] = ':';
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001237 semsg(_(e_invarg2), address);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001238 return NULL;
1239 }
1240
1241 /* parse options */
1242 clear_job_options(&opt);
1243 opt.jo_mode = MODE_JSON;
1244 opt.jo_timeout = 2000;
1245 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02001246 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001247 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001248 if (opt.jo_timeout < 0)
1249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001251 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001252 }
1253
1254 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
1255 if (channel != NULL)
1256 {
1257 opt.jo_set = JO_ALL;
1258 channel_set_options(channel, &opt);
1259 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02001260theend:
1261 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01001262 return channel;
1263}
1264
Bram Moolenaarde279892016-03-11 22:19:44 +01001265 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001266ch_close_part(channel_T *channel, ch_part_T part)
Bram Moolenaarde279892016-03-11 22:19:44 +01001267{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001268 sock_T *fd = &channel->ch_part[part].ch_fd;
1269
Bram Moolenaarde279892016-03-11 22:19:44 +01001270 if (*fd != INVALID_FD)
1271 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001272 if (part == PART_SOCK)
1273 sock_close(*fd);
1274 else
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001275 {
1276 /* When using a pty the same FD is set on multiple parts, only
1277 * close it when the last reference is closed. */
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02001278 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1279 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1280 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001281 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001282#ifdef MSWIN
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001283 if (channel->ch_named_pipe)
1284 DisconnectNamedPipe((HANDLE)fd);
1285#endif
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001286 fd_close(*fd);
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001287 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02001288 }
Bram Moolenaarde279892016-03-11 22:19:44 +01001289 *fd = INVALID_FD;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001290
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001291 /* channel is closed, may want to end the job if it was the last */
1292 channel->ch_to_be_closed &= ~(1U << part);
Bram Moolenaarde279892016-03-11 22:19:44 +01001293 }
1294}
1295
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001296 void
Bram Moolenaard8070362016-02-15 21:56:54 +01001297channel_set_pipes(channel_T *channel, sock_T in, sock_T out, sock_T err)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001298{
Bram Moolenaarde279892016-03-11 22:19:44 +01001299 if (in != INVALID_FD)
1300 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001301 ch_close_part(channel, PART_IN);
Bram Moolenaarde279892016-03-11 22:19:44 +01001302 channel->CH_IN_FD = in;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001303# if defined(UNIX)
1304 /* Do not end the job when all output channels are closed, wait until
1305 * the job ended. */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001306 if (mch_isatty(in))
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001307 channel->ch_to_be_closed |= (1U << PART_IN);
1308# endif
Bram Moolenaarde279892016-03-11 22:19:44 +01001309 }
1310 if (out != INVALID_FD)
1311 {
1312# if defined(FEAT_GUI)
1313 channel_gui_unregister_one(channel, PART_OUT);
1314# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001315 ch_close_part(channel, PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001316 channel->CH_OUT_FD = out;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001317 channel->ch_to_be_closed |= (1U << PART_OUT);
Bram Moolenaarde279892016-03-11 22:19:44 +01001318# if defined(FEAT_GUI)
1319 channel_gui_register_one(channel, PART_OUT);
1320# endif
1321 }
1322 if (err != INVALID_FD)
1323 {
1324# if defined(FEAT_GUI)
1325 channel_gui_unregister_one(channel, PART_ERR);
1326# endif
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001327 ch_close_part(channel, PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001328 channel->CH_ERR_FD = err;
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02001329 channel->ch_to_be_closed |= (1U << PART_ERR);
Bram Moolenaarde279892016-03-11 22:19:44 +01001330# if defined(FEAT_GUI)
1331 channel_gui_register_one(channel, PART_ERR);
1332# endif
1333 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001334}
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001335
Bram Moolenaard6051b52016-02-28 15:49:03 +01001336/*
Bram Moolenaar014069a2016-03-03 22:51:40 +01001337 * Sets the job the channel is associated with and associated options.
Bram Moolenaard6051b52016-02-28 15:49:03 +01001338 * This does not keep a refcount, when the job is freed ch_job is cleared.
1339 */
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001340 void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001341channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01001342{
Bram Moolenaar77073442016-02-13 23:23:53 +01001343 channel->ch_job = job;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001344
1345 channel_set_options(channel, options);
1346
1347 if (job->jv_in_buf != NULL)
1348 {
1349 chanpart_T *in_part = &channel->ch_part[PART_IN];
1350
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001351 set_bufref(&in_part->ch_bufref, job->jv_in_buf);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001352 ch_log(channel, "reading from buffer '%s'",
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001353 (char *)in_part->ch_bufref.br_buf->b_ffname);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001354 if (options->jo_set & JO_IN_TOP)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001355 {
1356 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1357 {
1358 /* Special mode: send last-but-one line when appending a line
1359 * to the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001360 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001361 in_part->ch_buf_append = TRUE;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001362 in_part->ch_buf_top =
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001363 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001364 }
1365 else
1366 in_part->ch_buf_top = options->jo_in_top;
1367 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001368 else
1369 in_part->ch_buf_top = 1;
1370 if (options->jo_set & JO_IN_BOT)
1371 in_part->ch_buf_bot = options->jo_in_bot;
1372 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001373 in_part->ch_buf_bot = in_part->ch_bufref.br_buf->b_ml.ml_line_count;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001374 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01001375}
1376
1377/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001378 * Set the callback for "channel"/"part" for the response with "id".
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001379 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001380 static void
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001381channel_set_req_callback(
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001382 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001383 ch_part_T part,
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001384 callback_T *callback,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001385 int id)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001386{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001387 cbq_T *head = &channel->ch_part[part].ch_cb_head;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001388 cbq_T *item = ALLOC_ONE(cbq_T);
Bram Moolenaara07fec92016-02-05 21:04:08 +01001389
1390 if (item != NULL)
1391 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001392 copy_callback(&item->cq_callback, callback);
Bram Moolenaar77073442016-02-13 23:23:53 +01001393 item->cq_seq_nr = id;
1394 item->cq_prev = head->cq_prev;
1395 head->cq_prev = item;
1396 item->cq_next = NULL;
1397 if (item->cq_prev == NULL)
1398 head->cq_next = item;
1399 else
1400 item->cq_prev->cq_next = item;
Bram Moolenaara07fec92016-02-05 21:04:08 +01001401 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001402}
1403
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001404 static void
1405write_buf_line(buf_T *buf, linenr_T lnum, channel_T *channel)
1406{
1407 char_u *line = ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar367aabd2016-03-08 17:13:06 +01001408 int len = (int)STRLEN(line);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001409 char_u *p;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001410 int i;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001411
Bram Moolenaar655da312016-05-28 22:22:34 +02001412 /* Need to make a copy to be able to append a NL. */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001413 if ((p = alloc(len + 2)) == NULL)
1414 return;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001415 memcpy((char *)p, (char *)line, len);
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001416
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001417 if (channel->ch_write_text_mode)
1418 p[len] = CAR;
1419 else
1420 {
1421 for (i = 0; i < len; ++i)
1422 if (p[i] == NL)
1423 p[i] = NUL;
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02001424
Bram Moolenaaref68e4f2017-09-02 16:28:36 +02001425 p[len] = NL;
1426 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001427 p[len + 1] = NUL;
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01001428 channel_send(channel, PART_IN, p, len + 1, "write_buf_line");
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001429 vim_free(p);
1430}
1431
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001432/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001433 * Return TRUE if "channel" can be written to.
1434 * Returns FALSE if the input is closed or the write would block.
1435 */
1436 static int
1437can_write_buf_line(channel_T *channel)
1438{
1439 chanpart_T *in_part = &channel->ch_part[PART_IN];
1440
1441 if (in_part->ch_fd == INVALID_FD)
1442 return FALSE; /* pipe was closed */
1443
1444 /* for testing: block every other attempt to write */
1445 if (in_part->ch_block_write == 1)
1446 in_part->ch_block_write = -1;
1447 else if (in_part->ch_block_write == -1)
1448 in_part->ch_block_write = 1;
1449
1450 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */
Bram Moolenaar4f974752019-02-17 17:44:42 +01001451#ifndef MSWIN
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001452 {
1453# if defined(HAVE_SELECT)
1454 struct timeval tval;
1455 fd_set wfds;
1456 int ret;
1457
1458 FD_ZERO(&wfds);
1459 FD_SET((int)in_part->ch_fd, &wfds);
1460 tval.tv_sec = 0;
1461 tval.tv_usec = 0;
1462 for (;;)
1463 {
1464 ret = select((int)in_part->ch_fd + 1, NULL, &wfds, NULL, &tval);
1465# ifdef EINTR
1466 SOCK_ERRNO;
1467 if (ret == -1 && errno == EINTR)
1468 continue;
1469# endif
1470 if (ret <= 0 || in_part->ch_block_write == 1)
1471 {
1472 if (ret > 0)
1473 ch_log(channel, "FAKED Input not ready for writing");
1474 else
1475 ch_log(channel, "Input not ready for writing");
1476 return FALSE;
1477 }
1478 break;
1479 }
1480# else
1481 struct pollfd fds;
1482
1483 fds.fd = in_part->ch_fd;
1484 fds.events = POLLOUT;
1485 if (poll(&fds, 1, 0) <= 0)
1486 {
1487 ch_log(channel, "Input not ready for writing");
1488 return FALSE;
1489 }
1490 if (in_part->ch_block_write == 1)
1491 {
1492 ch_log(channel, "FAKED Input not ready for writing");
1493 return FALSE;
1494 }
1495# endif
1496 }
1497#endif
1498 return TRUE;
1499}
1500
1501/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001502 * Write any buffer lines to the input channel.
Bram Moolenaar014069a2016-03-03 22:51:40 +01001503 */
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001504 static void
Bram Moolenaar014069a2016-03-03 22:51:40 +01001505channel_write_in(channel_T *channel)
1506{
1507 chanpart_T *in_part = &channel->ch_part[PART_IN];
1508 linenr_T lnum;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001509 buf_T *buf = in_part->ch_bufref.br_buf;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001510 int written = 0;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001511
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001512 if (buf == NULL || in_part->ch_buf_append)
1513 return; /* no buffer or using appending */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001514 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
Bram Moolenaar014069a2016-03-03 22:51:40 +01001515 {
1516 /* buffer was wiped out or unloaded */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001517 ch_log(channel, "input buffer has been wiped out");
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001518 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001519 return;
1520 }
Bram Moolenaar014069a2016-03-03 22:51:40 +01001521
1522 for (lnum = in_part->ch_buf_top; lnum <= in_part->ch_buf_bot
1523 && lnum <= buf->b_ml.ml_line_count; ++lnum)
1524 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001525 if (!can_write_buf_line(channel))
1526 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001527 write_buf_line(buf, lnum, channel);
1528 ++written;
Bram Moolenaar014069a2016-03-03 22:51:40 +01001529 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001530
1531 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001532 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001533 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001534 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001535
Bram Moolenaar014069a2016-03-03 22:51:40 +01001536 in_part->ch_buf_top = lnum;
Bram Moolenaard8b55492016-09-01 14:35:22 +02001537 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001538 {
Bram Moolenaardada6d22017-09-02 17:18:35 +02001539#if defined(FEAT_TERMINAL)
1540 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */
Bram Moolenaar3346cc42017-09-02 14:54:21 +02001541 if (channel->ch_job != NULL)
1542 term_send_eof(channel);
1543#endif
1544
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001545 /* Writing is done, no longer need the buffer. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001546 in_part->ch_bufref.br_buf = NULL;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001547 ch_log(channel, "Finished writing all lines to channel");
Bram Moolenaard8b55492016-09-01 14:35:22 +02001548
1549 /* Close the pipe/socket, so that the other side gets EOF. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001550 ch_close_part(channel, PART_IN);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001551 }
1552 else
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001553 ch_log(channel, "Still %ld more lines to write",
1554 (long)(buf->b_ml.ml_line_count - lnum + 1));
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001555}
1556
1557/*
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02001558 * Handle buffer "buf" being freed, remove it from any channels.
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001559 */
1560 void
1561channel_buffer_free(buf_T *buf)
1562{
1563 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001564 ch_part_T part;
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001565
1566 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001567 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001568 {
1569 chanpart_T *ch_part = &channel->ch_part[part];
1570
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001571 if (ch_part->ch_bufref.br_buf == buf)
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001572 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001573 ch_log(channel, "%s buffer has been wiped out",
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001574 part_names[part]);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001575 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarde7eb0a2016-05-09 20:54:33 +02001576 }
Bram Moolenaare0f76d02016-05-09 20:38:53 +02001577 }
1578}
1579
1580/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001581 * Write any lines waiting to be written to "channel".
1582 */
1583 static void
1584channel_write_input(channel_T *channel)
1585{
1586 chanpart_T *in_part = &channel->ch_part[PART_IN];
1587
1588 if (in_part->ch_writeque.wq_next != NULL)
1589 channel_send(channel, PART_IN, (char_u *)"", 0, "channel_write_input");
1590 else if (in_part->ch_bufref.br_buf != NULL)
1591 {
1592 if (in_part->ch_buf_append)
1593 channel_write_new_lines(in_part->ch_bufref.br_buf);
1594 else
1595 channel_write_in(channel);
1596 }
1597}
1598
1599/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001600 * Write any lines waiting to be written to a channel.
1601 */
1602 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02001603channel_write_any_lines(void)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001604{
1605 channel_T *channel;
1606
1607 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02001608 channel_write_input(channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +01001609}
1610
1611/*
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001612 * Write appended lines above the last one in "buf" to the channel.
1613 */
1614 void
1615channel_write_new_lines(buf_T *buf)
1616{
1617 channel_T *channel;
1618 int found_one = FALSE;
1619
1620 /* There could be more than one channel for the buffer, loop over all of
1621 * them. */
1622 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1623 {
1624 chanpart_T *in_part = &channel->ch_part[PART_IN];
1625 linenr_T lnum;
1626 int written = 0;
1627
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001628 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001629 {
1630 if (in_part->ch_fd == INVALID_FD)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001631 continue; /* pipe was closed */
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001632 found_one = TRUE;
1633 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1634 ++lnum)
1635 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001636 if (!can_write_buf_line(channel))
1637 break;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001638 write_buf_line(buf, lnum, channel);
1639 ++written;
1640 }
1641
1642 if (written == 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001643 ch_log(channel, "written line %d to channel", (int)lnum - 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001644 else if (written > 1)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001645 ch_log(channel, "written %d lines to channel", written);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02001646 if (lnum < buf->b_ml.ml_line_count)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02001647 ch_log(channel, "Still %ld more lines to write",
1648 (long)(buf->b_ml.ml_line_count - lnum));
Bram Moolenaar99ef0622016-03-06 20:22:25 +01001649
1650 in_part->ch_buf_bot = lnum;
1651 }
1652 }
1653 if (!found_one)
1654 buf->b_write_to_channel = FALSE;
1655}
1656
1657/*
Bram Moolenaar77073442016-02-13 23:23:53 +01001658 * Invoke the "callback" on channel "channel".
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001659 * This does not redraw but sets channel_need_redraw;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001660 */
1661 static void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001662invoke_callback(channel_T *channel, callback_T *callback, typval_T *argv)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001663{
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001664 typval_T rettv;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001665
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001666 if (safe_to_invoke_callback == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001667 iemsg("INTERNAL: Invoking callback when it is not safe");
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02001668
Bram Moolenaar77073442016-02-13 23:23:53 +01001669 argv[0].v_type = VAR_CHANNEL;
1670 argv[0].vval.v_channel = channel;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001671
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001672 call_callback(callback, -1, &rettv, 2, argv);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01001673 clear_tv(&rettv);
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02001674 channel_need_redraw = TRUE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01001675}
1676
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01001677/*
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001678 * Return the first node from "channel"/"part" without removing it.
1679 * Returns NULL if there is nothing.
1680 */
1681 readq_T *
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001682channel_peek(channel_T *channel, ch_part_T part)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001683{
1684 readq_T *head = &channel->ch_part[part].ch_head;
1685
1686 return head->rq_next;
1687}
1688
1689/*
1690 * Return a pointer to the first NL in "node".
1691 * Skips over NUL characters.
1692 * Returns NULL if there is no NL.
1693 */
1694 char_u *
1695channel_first_nl(readq_T *node)
1696{
1697 char_u *buffer = node->rq_buffer;
1698 long_u i;
1699
1700 for (i = 0; i < node->rq_buflen; ++i)
1701 if (buffer[i] == NL)
1702 return buffer + i;
1703 return NULL;
1704}
1705
1706/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001707 * Return the first buffer from channel "channel"/"part" and remove it.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001708 * The caller must free it.
1709 * Returns NULL if there is nothing.
1710 */
1711 char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001712channel_get(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001713{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001714 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001715 readq_T *node = head->rq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001716 char_u *p;
1717
Bram Moolenaar77073442016-02-13 23:23:53 +01001718 if (node == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001719 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720 if (outlen != NULL)
1721 *outlen += node->rq_buflen;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001722 /* dispose of the node but keep the buffer */
Bram Moolenaar77073442016-02-13 23:23:53 +01001723 p = node->rq_buffer;
1724 head->rq_next = node->rq_next;
1725 if (node->rq_next == NULL)
1726 head->rq_prev = NULL;
1727 else
1728 node->rq_next->rq_prev = NULL;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001729 vim_free(node);
1730 return p;
1731}
1732
1733/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001734 * Returns the whole buffer contents concatenated for "channel"/"part".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001735 * Replaces NUL bytes with NL.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001736 */
1737 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001738channel_get_all(channel_T *channel, ch_part_T part, int *outlen)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001739{
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001740 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01001741 readq_T *node;
Bram Moolenaaradb78a72016-06-27 21:10:31 +02001742 long_u len = 0;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001743 char_u *res;
1744 char_u *p;
1745
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001746 // Concatenate everything into one buffer.
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001747 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001748 len += node->rq_buflen;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001749 res = alloc(len + 1);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001750 if (res == NULL)
1751 return NULL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001752 p = res;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001753 for (node = head->rq_next; node != NULL; node = node->rq_next)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001754 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001755 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001756 p += node->rq_buflen;
1757 }
1758 *p = NUL;
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001759
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001760 // Free all buffers
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001761 do
1762 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001763 p = channel_get(channel, part, NULL);
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001764 vim_free(p);
1765 } while (p != NULL);
1766
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001767 if (outlen != NULL)
1768 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001769 // Returning the length, keep NUL characters.
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001770 *outlen += len;
1771 return res;
1772 }
1773
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001774 // Turn all NUL into NL, so that the result can be used as a string.
1775 p = res;
1776 while (p < res + len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001777 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001778 if (*p == NUL)
1779 *p = NL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001780#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001781 else if (*p == 0x1b)
1782 {
1783 // crush the escape sequence OSC 0/1/2: ESC ]0;
1784 if (p + 3 < res + len
1785 && p[1] == ']'
1786 && (p[2] == '0' || p[2] == '1' || p[2] == '2')
1787 && p[3] == ';')
1788 {
1789 // '\a' becomes a NL
1790 while (p < res + (len - 1) && *p != '\a')
1791 ++p;
1792 // BEL is zero width characters, suppress display mistake
1793 // ConPTY (after 10.0.18317) requires advance checking
1794 if (p[-1] == NUL)
1795 p[-1] = 0x07;
1796 }
1797 }
1798#endif
1799 ++p;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001800 }
1801
Bram Moolenaaree1f7b32016-03-28 14:42:14 +02001802 return res;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001803}
1804
1805/*
Bram Moolenaarcf089462016-06-12 21:18:43 +02001806 * Consume "len" bytes from the head of "node".
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001807 * Caller must check these bytes are available.
1808 */
1809 void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001810channel_consume(channel_T *channel, ch_part_T part, int len)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001811{
1812 readq_T *head = &channel->ch_part[part].ch_head;
1813 readq_T *node = head->rq_next;
1814 char_u *buf = node->rq_buffer;
1815
1816 mch_memmove(buf, buf + len, node->rq_buflen - len);
1817 node->rq_buflen -= len;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001818 node->rq_buffer[node->rq_buflen] = NUL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001819}
1820
1821/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001822 * Collapses the first and second buffer for "channel"/"part".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001823 * Returns FAIL if that is not possible.
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001824 * When "want_nl" is TRUE collapse more buffers until a NL is found.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001825 */
1826 int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001827channel_collapse(channel_T *channel, ch_part_T part, int want_nl)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001828{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001829 readq_T *head = &channel->ch_part[part].ch_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01001830 readq_T *node = head->rq_next;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001831 readq_T *last_node;
1832 readq_T *n;
1833 char_u *newbuf;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001834 char_u *p;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001835 long_u len;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001836
Bram Moolenaar77073442016-02-13 23:23:53 +01001837 if (node == NULL || node->rq_next == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001838 return FAIL;
1839
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001840 last_node = node->rq_next;
Bram Moolenaar772153f2019-03-04 12:09:49 +01001841 len = node->rq_buflen + last_node->rq_buflen;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001842 if (want_nl)
1843 while (last_node->rq_next != NULL
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001844 && channel_first_nl(last_node) == NULL)
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001845 {
1846 last_node = last_node->rq_next;
1847 len += last_node->rq_buflen;
1848 }
1849
Bram Moolenaar772153f2019-03-04 12:09:49 +01001850 p = newbuf = alloc(len + 1);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001851 if (newbuf == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001852 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001853 mch_memmove(p, node->rq_buffer, node->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001854 p += node->rq_buflen;
Bram Moolenaar77073442016-02-13 23:23:53 +01001855 vim_free(node->rq_buffer);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001856 node->rq_buffer = newbuf;
1857 for (n = node; n != last_node; )
1858 {
1859 n = n->rq_next;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001860 mch_memmove(p, n->rq_buffer, n->rq_buflen);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001861 p += n->rq_buflen;
1862 vim_free(n->rq_buffer);
1863 }
Bram Moolenaar772153f2019-03-04 12:09:49 +01001864 *p = NUL;
Bram Moolenaarbbe8d912016-06-05 16:10:57 +02001865 node->rq_buflen = (long_u)(p - newbuf);
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001866
1867 /* dispose of the collapsed nodes and their buffers */
1868 for (n = node->rq_next; n != last_node; )
1869 {
1870 n = n->rq_next;
1871 vim_free(n->rq_prev);
1872 }
1873 node->rq_next = last_node->rq_next;
1874 if (last_node->rq_next == NULL)
1875 head->rq_prev = node;
1876 else
1877 last_node->rq_next->rq_prev = node;
1878 vim_free(last_node);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001879 return OK;
1880}
1881
1882/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001883 * Store "buf[len]" on "channel"/"part".
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001884 * When "prepend" is TRUE put in front, otherwise append at the end.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001885 * Returns OK or FAIL.
1886 */
1887 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001888channel_save(channel_T *channel, ch_part_T part, char_u *buf, int len,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001889 int prepend, char *lead)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001890{
1891 readq_T *node;
1892 readq_T *head = &channel->ch_part[part].ch_head;
1893 char_u *p;
1894 int i;
1895
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001896 node = ALLOC_ONE(readq_T);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001897 if (node == NULL)
1898 return FAIL; /* out of memory */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02001899 /* A NUL is added at the end, because netbeans code expects that.
1900 * Otherwise a NUL may appear inside the text. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001901 node->rq_buffer = alloc(len + 1);
1902 if (node->rq_buffer == NULL)
1903 {
1904 vim_free(node);
1905 return FAIL; /* out of memory */
1906 }
1907
1908 if (channel->ch_part[part].ch_mode == MODE_NL)
1909 {
1910 /* Drop any CR before a NL. */
1911 p = node->rq_buffer;
1912 for (i = 0; i < len; ++i)
1913 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1914 *p++ = buf[i];
1915 *p = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001916 node->rq_buflen = (long_u)(p - node->rq_buffer);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001917 }
1918 else
1919 {
1920 mch_memmove(node->rq_buffer, buf, len);
1921 node->rq_buffer[len] = NUL;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02001922 node->rq_buflen = (long_u)len;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001923 }
1924
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001925 if (prepend)
1926 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001927 // prepend node to the head of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001928 node->rq_next = head->rq_next;
1929 node->rq_prev = NULL;
1930 if (head->rq_next == NULL)
1931 head->rq_prev = node;
1932 else
1933 head->rq_next->rq_prev = node;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001934 head->rq_next = node;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001935 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001936 else
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001937 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001938 // append node to the tail of the queue
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001939 node->rq_next = NULL;
1940 node->rq_prev = head->rq_prev;
1941 if (head->rq_prev == NULL)
1942 head->rq_next = node;
1943 else
1944 head->rq_prev->rq_next = node;
1945 head->rq_prev = node;
1946 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001947
Bram Moolenaar71eeb742017-09-13 22:18:01 +02001948 if (ch_log_active() && lead != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001949 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02001950 ch_log_lead(lead, channel, part);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001951 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02001952 vim_ignored = (int)fwrite(buf, len, 1, log_fd);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001953 fprintf(log_fd, "'\n");
1954 }
1955 return OK;
1956}
1957
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001958/*
1959 * Try to fill the buffer of "reader".
1960 * Returns FALSE when nothing was added.
1961 */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001962 static int
1963channel_fill(js_read_T *reader)
1964{
1965 channel_T *channel = (channel_T *)reader->js_cookie;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02001966 ch_part_T part = reader->js_cookie_arg;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001967 char_u *next = channel_get(channel, part, NULL);
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001968 int keeplen;
1969 int addlen;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001970 char_u *p;
1971
1972 if (next == NULL)
1973 return FALSE;
1974
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001975 keeplen = reader->js_end - reader->js_buf;
1976 if (keeplen > 0)
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001977 {
1978 /* Prepend unused text. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001979 addlen = (int)STRLEN(next);
1980 p = alloc(keeplen + addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001981 if (p == NULL)
1982 {
1983 vim_free(next);
1984 return FALSE;
1985 }
Bram Moolenaar88989cc2017-02-06 21:56:09 +01001986 mch_memmove(p, reader->js_buf, keeplen);
1987 mch_memmove(p + keeplen, next, addlen + 1);
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001988 vim_free(next);
1989 next = p;
1990 }
1991
1992 vim_free(reader->js_buf);
1993 reader->js_buf = next;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02001994 return TRUE;
1995}
1996
Bram Moolenaar42d38a22016-02-20 18:18:59 +01001997/*
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001998 * Use the read buffer of "channel"/"part" and parse a JSON message that is
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001999 * complete. The messages are added to the queue.
Bram Moolenaard7ece102016-02-02 23:23:02 +01002000 * Return TRUE if there is more to read.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002001 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002002 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002003channel_parse_json(channel_T *channel, ch_part_T part)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002004{
2005 js_read_T reader;
2006 typval_T listtv;
2007 jsonq_T *item;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002008 chanpart_T *chanpart = &channel->ch_part[part];
2009 jsonq_T *head = &chanpart->ch_json_head;
2010 int status;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002011 int ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002012
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002013 if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002014 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002015
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002016 reader.js_buf = channel_get(channel, part, NULL);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002017 reader.js_used = 0;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002018 reader.js_fill = channel_fill;
Bram Moolenaar77073442016-02-13 23:23:53 +01002019 reader.js_cookie = channel;
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002020 reader.js_cookie_arg = part;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002021
2022 /* When a message is incomplete we wait for a short while for more to
2023 * arrive. After the delay drop the input, otherwise a truncated string
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002024 * or list will make us hang.
2025 * Do not generate error messages, they will be written in a channel log. */
2026 ++emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002027 status = json_decode(&reader, &listtv,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002028 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaar03c60c12017-01-10 15:15:37 +01002029 --emsg_silent;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002030 if (status == OK)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002031 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +01002032 /* Only accept the response when it is a list with at least two
2033 * items. */
2034 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002035 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002036 if (listtv.v_type != VAR_LIST)
2037 ch_error(channel, "Did not receive a list, discarding");
2038 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002039 ch_error(channel, "Expected list with two items, got %d",
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002040 listtv.vval.v_list->lv_len);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002041 clear_tv(&listtv);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002042 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002043 else
2044 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002045 item = ALLOC_ONE(jsonq_T);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002046 if (item == NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002047 clear_tv(&listtv);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002048 else
2049 {
Bram Moolenaar958dc692016-12-01 15:34:12 +01002050 item->jq_no_callback = FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01002051 item->jq_value = alloc_tv();
2052 if (item->jq_value == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002053 {
2054 vim_free(item);
2055 clear_tv(&listtv);
2056 }
2057 else
2058 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002059 *item->jq_value = listtv;
2060 item->jq_prev = head->jq_prev;
2061 head->jq_prev = item;
2062 item->jq_next = NULL;
2063 if (item->jq_prev == NULL)
2064 head->jq_next = item;
2065 else
2066 item->jq_prev->jq_next = item;
Bram Moolenaard7ece102016-02-02 23:23:02 +01002067 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002068 }
2069 }
2070 }
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002071
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002072 if (status == OK)
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002073 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002074 else if (status == MAYBE)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002075 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002076 size_t buflen = STRLEN(reader.js_buf);
2077
2078 if (chanpart->ch_wait_len < buflen)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002079 {
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002080 /* First time encountering incomplete message or after receiving
2081 * more (but still incomplete): set a deadline of 100 msec. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002082 ch_log(channel,
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002083 "Incomplete message (%d bytes) - wait 100 msec for more",
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01002084 (int)buflen);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002085 reader.js_used = 0;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002086 chanpart->ch_wait_len = buflen;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002087#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002088 chanpart->ch_deadline = GetTickCount() + 100L;
2089#else
2090 gettimeofday(&chanpart->ch_deadline, NULL);
2091 chanpart->ch_deadline.tv_usec += 100 * 1000;
2092 if (chanpart->ch_deadline.tv_usec > 1000 * 1000)
2093 {
2094 chanpart->ch_deadline.tv_usec -= 1000 * 1000;
2095 ++chanpart->ch_deadline.tv_sec;
2096 }
2097#endif
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002098 }
2099 else
2100 {
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002101 int timeout;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002102#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002103 timeout = GetTickCount() > chanpart->ch_deadline;
2104#else
2105 {
2106 struct timeval now_tv;
2107
2108 gettimeofday(&now_tv, NULL);
2109 timeout = now_tv.tv_sec > chanpart->ch_deadline.tv_sec
2110 || (now_tv.tv_sec == chanpart->ch_deadline.tv_sec
2111 && now_tv.tv_usec > chanpart->ch_deadline.tv_usec);
2112 }
2113#endif
2114 if (timeout)
2115 {
2116 status = FAIL;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002117 chanpart->ch_wait_len = 0;
2118 ch_log(channel, "timed out");
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002119 }
2120 else
2121 {
2122 reader.js_used = 0;
2123 ch_log(channel, "still waiting on incomplete message");
2124 }
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002125 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002126 }
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002127
2128 if (status == FAIL)
2129 {
2130 ch_error(channel, "Decoding failed - discarding input");
2131 ret = FALSE;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01002132 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002133 }
2134 else if (reader.js_buf[reader.js_used] != NUL)
2135 {
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002136 /* Put the unread part back into the channel. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002137 channel_save(channel, part, reader.js_buf + reader.js_used,
Bram Moolenaar46c00a62016-03-28 14:11:42 +02002138 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2139 TRUE, NULL);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002140 ret = status == MAYBE ? FALSE: TRUE;
2141 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01002142 else
2143 ret = FALSE;
2144
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002145 vim_free(reader.js_buf);
Bram Moolenaard7ece102016-02-02 23:23:02 +01002146 return ret;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002147}
2148
2149/*
Bram Moolenaard46ae142016-02-16 13:33:52 +01002150 * Remove "node" from the queue that it is in. Does not free it.
Bram Moolenaara07fec92016-02-05 21:04:08 +01002151 */
2152 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002153remove_cb_node(cbq_T *head, cbq_T *node)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002154{
Bram Moolenaar77073442016-02-13 23:23:53 +01002155 if (node->cq_prev == NULL)
2156 head->cq_next = node->cq_next;
2157 else
2158 node->cq_prev->cq_next = node->cq_next;
2159 if (node->cq_next == NULL)
2160 head->cq_prev = node->cq_prev;
2161 else
2162 node->cq_next->cq_prev = node->cq_prev;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002163}
2164
2165/*
2166 * Remove "node" from the queue that it is in and free it.
Bram Moolenaar77073442016-02-13 23:23:53 +01002167 * Caller should have freed or used node->jq_value.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002168 */
2169 static void
Bram Moolenaar77073442016-02-13 23:23:53 +01002170remove_json_node(jsonq_T *head, jsonq_T *node)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002171{
Bram Moolenaar77073442016-02-13 23:23:53 +01002172 if (node->jq_prev == NULL)
2173 head->jq_next = node->jq_next;
2174 else
2175 node->jq_prev->jq_next = node->jq_next;
2176 if (node->jq_next == NULL)
2177 head->jq_prev = node->jq_prev;
2178 else
2179 node->jq_next->jq_prev = node->jq_prev;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002180 vim_free(node);
2181}
2182
2183/*
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002184 * Add "id" to the list of JSON message IDs we are waiting on.
2185 */
2186 static void
2187channel_add_block_id(chanpart_T *chanpart, int id)
2188{
2189 garray_T *gap = &chanpart->ch_block_ids;
2190
2191 if (gap->ga_growsize == 0)
2192 ga_init2(gap, (int)sizeof(int), 10);
2193 if (ga_grow(gap, 1) == OK)
2194 {
2195 ((int *)gap->ga_data)[gap->ga_len] = id;
2196 ++gap->ga_len;
2197 }
2198}
2199
2200/*
2201 * Remove "id" from the list of JSON message IDs we are waiting on.
2202 */
2203 static void
2204channel_remove_block_id(chanpart_T *chanpart, int id)
2205{
2206 garray_T *gap = &chanpart->ch_block_ids;
2207 int i;
2208
2209 for (i = 0; i < gap->ga_len; ++i)
2210 if (((int *)gap->ga_data)[i] == id)
2211 {
2212 --gap->ga_len;
2213 if (i < gap->ga_len)
2214 {
2215 int *p = ((int *)gap->ga_data) + i;
2216
2217 mch_memmove(p, p + 1, (gap->ga_len - i) * sizeof(int));
2218 }
2219 return;
2220 }
2221 siemsg("INTERNAL: channel_remove_block_id: cannot find id %d", id);
2222}
2223
2224/*
2225 * Return TRUE if "id" is in the list of JSON message IDs we are waiting on.
2226 */
2227 static int
2228channel_has_block_id(chanpart_T *chanpart, int id)
2229{
2230 garray_T *gap = &chanpart->ch_block_ids;
2231 int i;
2232
2233 for (i = 0; i < gap->ga_len; ++i)
2234 if (((int *)gap->ga_data)[i] == id)
2235 return TRUE;
2236 return FALSE;
2237}
2238
2239/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002240 * Get a message from the JSON queue for channel "channel".
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002241 * When "id" is positive it must match the first number in the list.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002242 * When "id" is zero or negative jut get the first message. But not one
2243 * in the ch_block_ids list.
Bram Moolenaar958dc692016-12-01 15:34:12 +01002244 * When "without_callback" is TRUE also get messages that were pushed back.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002245 * Return OK when found and return the value in "rettv".
2246 * Return FAIL otherwise.
2247 */
2248 static int
Bram Moolenaar958dc692016-12-01 15:34:12 +01002249channel_get_json(
2250 channel_T *channel,
2251 ch_part_T part,
2252 int id,
2253 int without_callback,
2254 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002255{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002256 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar77073442016-02-13 23:23:53 +01002257 jsonq_T *item = head->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002258
Bram Moolenaar77073442016-02-13 23:23:53 +01002259 while (item != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002260 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002261 list_T *l = item->jq_value->vval.v_list;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002262 typval_T *tv = &l->lv_first->li_tv;
2263
Bram Moolenaar958dc692016-12-01 15:34:12 +01002264 if ((without_callback || !item->jq_no_callback)
2265 && ((id > 0 && tv->v_type == VAR_NUMBER && tv->vval.v_number == id)
Bram Moolenaare56bf152016-02-08 23:23:42 +01002266 || (id <= 0 && (tv->v_type != VAR_NUMBER
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002267 || tv->vval.v_number == 0
Bram Moolenaar38ea7842019-06-09 19:51:58 +02002268 || !channel_has_block_id(
2269 &channel->ch_part[part], tv->vval.v_number)))))
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002270 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002271 *rettv = item->jq_value;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01002272 if (tv->v_type == VAR_NUMBER)
Bram Moolenaar4ac2e8d2018-04-08 12:38:26 +02002273 ch_log(channel, "Getting JSON message %ld",
2274 (long)tv->vval.v_number);
Bram Moolenaar77073442016-02-13 23:23:53 +01002275 remove_json_node(head, item);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002276 return OK;
2277 }
Bram Moolenaar77073442016-02-13 23:23:53 +01002278 item = item->jq_next;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002279 }
2280 return FAIL;
2281}
2282
Bram Moolenaar958dc692016-12-01 15:34:12 +01002283/*
2284 * Put back "rettv" into the JSON queue, there was no callback for it.
2285 * Takes over the values in "rettv".
2286 */
2287 static void
2288channel_push_json(channel_T *channel, ch_part_T part, typval_T *rettv)
2289{
2290 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2291 jsonq_T *item = head->jq_next;
2292 jsonq_T *newitem;
2293
2294 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2295 /* last item was pushed back, append to the end */
2296 item = NULL;
2297 else while (item != NULL && item->jq_no_callback)
2298 /* append after the last item that was pushed back */
2299 item = item->jq_next;
2300
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002301 newitem = ALLOC_ONE(jsonq_T);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002302 if (newitem == NULL)
2303 clear_tv(rettv);
2304 else
2305 {
2306 newitem->jq_value = alloc_tv();
2307 if (newitem->jq_value == NULL)
2308 {
2309 vim_free(newitem);
2310 clear_tv(rettv);
2311 }
2312 else
2313 {
2314 newitem->jq_no_callback = FALSE;
2315 *newitem->jq_value = *rettv;
2316 if (item == NULL)
2317 {
2318 /* append to the end */
2319 newitem->jq_prev = head->jq_prev;
2320 head->jq_prev = newitem;
2321 newitem->jq_next = NULL;
2322 if (newitem->jq_prev == NULL)
2323 head->jq_next = newitem;
2324 else
2325 newitem->jq_prev->jq_next = newitem;
2326 }
2327 else
2328 {
2329 /* append after "item" */
2330 newitem->jq_prev = item;
2331 newitem->jq_next = item->jq_next;
2332 item->jq_next = newitem;
2333 if (newitem->jq_next == NULL)
2334 head->jq_prev = newitem;
2335 else
2336 newitem->jq_next->jq_prev = newitem;
2337 }
2338 }
2339 }
2340}
2341
Bram Moolenaarece61b02016-02-20 21:39:05 +01002342#define CH_JSON_MAX_ARGS 4
2343
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002344/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002345 * Execute a command received over "channel"/"part"
Bram Moolenaarece61b02016-02-20 21:39:05 +01002346 * "argv[0]" is the command string.
2347 * "argv[1]" etc. have further arguments, type is VAR_UNKNOWN if missing.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002348 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002349 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002350channel_exe_cmd(channel_T *channel, ch_part_T part, typval_T *argv)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002351{
Bram Moolenaarece61b02016-02-20 21:39:05 +01002352 char_u *cmd = argv[0].vval.v_string;
2353 char_u *arg;
2354 int options = channel->ch_part[part].ch_mode == MODE_JS ? JSON_JS : 0;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002355
Bram Moolenaarece61b02016-02-20 21:39:05 +01002356 if (argv[1].v_type != VAR_STRING)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002357 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002358 ch_error(channel, "received command with non-string argument");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002359 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002360 emsg(_("E903: received command with non-string argument"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002361 return;
2362 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002363 arg = argv[1].vval.v_string;
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002364 if (arg == NULL)
2365 arg = (char_u *)"";
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002366
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002367 if (STRCMP(cmd, "ex") == 0)
2368 {
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002369 int save_called_emsg = called_emsg;
2370
2371 called_emsg = FALSE;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002372 ch_log(channel, "Executing ex command '%s'", (char *)arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002373 ++emsg_silent;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002374 do_cmdline_cmd(arg);
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002375 --emsg_silent;
2376 if (called_emsg)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002377 ch_log(channel, "Ex command error: '%s'",
Bram Moolenaarc4dcd602016-03-26 22:56:46 +01002378 (char *)get_vim_var_str(VV_ERRMSG));
2379 called_emsg = save_called_emsg;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002380 }
2381 else if (STRCMP(cmd, "normal") == 0)
2382 {
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002383 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002384
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002385 ch_log(channel, "Executing normal command '%s'", (char *)arg);
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002386 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002387 ea.arg = arg;
2388 ea.addr_count = 0;
2389 ea.forceit = TRUE; /* no mapping */
2390 ex_normal(&ea);
2391 }
2392 else if (STRCMP(cmd, "redraw") == 0)
2393 {
2394 exarg_T ea;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002395
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002396 ch_log(channel, "redraw");
Bram Moolenaar4ca41532019-05-09 21:48:37 +02002397 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar14ad6112016-02-01 21:47:13 +01002398 ea.forceit = *arg != NUL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002399 ex_redraw(&ea);
2400 showruler(FALSE);
2401 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002402 out_flush_cursor(TRUE, FALSE);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002403 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002404 else if (STRCMP(cmd, "expr") == 0 || STRCMP(cmd, "call") == 0)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002405 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002406 int is_call = cmd[0] == 'c';
2407 int id_idx = is_call ? 3 : 2;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002408
Bram Moolenaarece61b02016-02-20 21:39:05 +01002409 if (argv[id_idx].v_type != VAR_UNKNOWN
2410 && argv[id_idx].v_type != VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002411 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002412 ch_error(channel, "last argument for expr/call must be a number");
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002413 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002414 emsg(_("E904: last argument for expr/call must be a number"));
Bram Moolenaarece61b02016-02-20 21:39:05 +01002415 }
2416 else if (is_call && argv[2].v_type != VAR_LIST)
2417 {
2418 ch_error(channel, "third argument for call must be a list");
2419 if (p_verbose > 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002420 emsg(_("E904: third argument for call must be a list"));
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002421 }
2422 else
2423 {
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002424 typval_T *tv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002425 typval_T res_tv;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002426 typval_T err_tv;
Bram Moolenaar55fab432016-02-07 16:53:13 +01002427 char_u *json = NULL;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002428
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002429 /* Don't pollute the display with errors. */
2430 ++emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002431 if (!is_call)
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002432 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002433 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002434 tv = eval_expr(arg, NULL);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002435 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002436 else
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002437 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002438 ch_log(channel, "Calling '%s'", (char *)arg);
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002439 if (func_call(arg, &argv[2], NULL, NULL, &res_tv) == OK)
2440 tv = &res_tv;
Bram Moolenaarac74d5e2016-03-20 14:31:00 +01002441 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002442
2443 if (argv[id_idx].v_type == VAR_NUMBER)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002444 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002445 int id = argv[id_idx].vval.v_number;
2446
Bram Moolenaar55fab432016-02-07 16:53:13 +01002447 if (tv != NULL)
Bram Moolenaarf1f07922016-08-26 17:58:53 +02002448 json = json_encode_nr_expr(id, tv, options | JSON_NL);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002449 if (tv == NULL || (json != NULL && *json == NUL))
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002450 {
Bram Moolenaar55fab432016-02-07 16:53:13 +01002451 /* If evaluation failed or the result can't be encoded
2452 * then return the string "ERROR". */
Bram Moolenaar77073442016-02-13 23:23:53 +01002453 vim_free(json);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002454 err_tv.v_type = VAR_STRING;
2455 err_tv.vval.v_string = (char_u *)"ERROR";
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002456 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002457 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002458 if (json != NULL)
2459 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002460 channel_send(channel,
2461 part == PART_SOCK ? PART_SOCK : PART_IN,
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +02002462 json, (int)STRLEN(json), (char *)cmd);
Bram Moolenaar55fab432016-02-07 16:53:13 +01002463 vim_free(json);
2464 }
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002465 }
Bram Moolenaar55fab432016-02-07 16:53:13 +01002466 --emsg_skip;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002467 if (tv == &res_tv)
2468 clear_tv(tv);
Bram Moolenaarc8fe3382016-09-04 20:44:42 +02002469 else
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +01002470 free_tv(tv);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01002471 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002472 }
2473 else if (p_verbose > 2)
Bram Moolenaarece61b02016-02-20 21:39:05 +01002474 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002475 ch_error(channel, "Received unknown command: %s", (char *)cmd);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 semsg(_("E905: received unknown command: %s"), cmd);
Bram Moolenaarece61b02016-02-20 21:39:05 +01002477 }
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002478}
2479
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002480/*
2481 * Invoke the callback at "cbhead".
2482 * Does not redraw but sets channel_need_redraw.
2483 */
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002484 static void
2485invoke_one_time_callback(
2486 channel_T *channel,
2487 cbq_T *cbhead,
2488 cbq_T *item,
2489 typval_T *argv)
2490{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002491 ch_log(channel, "Invoking one-time callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002492 (char *)item->cq_callback.cb_name);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002493 /* Remove the item from the list first, if the callback
2494 * invokes ch_close() the list will be cleared. */
2495 remove_cb_node(cbhead, item);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002496 invoke_callback(channel, &item->cq_callback, argv);
2497 free_callback(&item->cq_callback);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002498 vim_free(item);
2499}
2500
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002501 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002502append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, ch_part_T part)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002503{
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002504 bufref_T save_curbuf = {NULL, 0, 0};
2505 win_T *save_curwin = NULL;
2506 tabpage_T *save_curtab = NULL;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002507 linenr_T lnum = buffer->b_ml.ml_line_count;
2508 int save_write_to = buffer->b_write_to_channel;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002509 chanpart_T *ch_part = &channel->ch_part[part];
2510 int save_p_ma = buffer->b_p_ma;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002511 int empty = (buffer->b_ml.ml_flags & ML_EMPTY) ? 1 : 0;
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002512
2513 if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
2514 {
2515 if (!ch_part->ch_nomod_error)
2516 {
2517 ch_error(channel, "Buffer is not modifiable, cannot append");
2518 ch_part->ch_nomod_error = TRUE;
2519 }
2520 return;
2521 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002522
2523 /* If the buffer is also used as input insert above the last
2524 * line. Don't write these lines. */
2525 if (save_write_to)
2526 {
2527 --lnum;
2528 buffer->b_write_to_channel = FALSE;
2529 }
2530
2531 /* Append to the buffer */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002532 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002533
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002534 buffer->b_p_ma = TRUE;
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002535
2536 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */
2537 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2538
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002539 u_sync(TRUE);
2540 /* ignore undo failure, undo is not very useful here */
Bram Moolenaar42335f52018-09-13 15:33:43 +02002541 vim_ignored = u_save(lnum - empty, lnum + 1);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002542
Bram Moolenaar169ebb02016-09-07 23:32:23 +02002543 if (empty)
2544 {
2545 /* The buffer is empty, replace the first (dummy) line. */
2546 ml_replace(lnum, msg, TRUE);
2547 lnum = 0;
2548 }
2549 else
2550 ml_append(lnum, msg, 0, FALSE);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002551 appended_lines_mark(lnum, 1L);
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02002552
2553 /* Restore curbuf/curwin/curtab */
2554 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2555
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02002556 if (ch_part->ch_nomodifiable)
2557 buffer->b_p_ma = FALSE;
2558 else
2559 buffer->b_p_ma = save_p_ma;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002560
2561 if (buffer->b_nwindows > 0)
2562 {
2563 win_T *wp;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002564
2565 FOR_ALL_WINDOWS(wp)
2566 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002567 if (wp->w_buffer == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002568 {
Bram Moolenaar4641a122019-07-29 22:10:23 +02002569 int move_cursor = save_write_to
2570 ? wp->w_cursor.lnum == lnum + 1
2571 : (wp->w_cursor.lnum == lnum
2572 && wp->w_cursor.col == 0);
2573
2574 // If the cursor is at or above the new line, move it one line
2575 // down. If the topline is outdated update it now.
2576 if (move_cursor || wp->w_topline > buffer->b_ml.ml_line_count)
2577 {
2578 if (move_cursor)
2579 ++wp->w_cursor.lnum;
2580 save_curwin = curwin;
2581 curwin = wp;
2582 curbuf = curwin->w_buffer;
2583 scroll_cursor_bot(0, FALSE);
2584 curwin = save_curwin;
2585 curbuf = curwin->w_buffer;
2586 }
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002587 }
2588 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +02002589 redraw_buf_and_status_later(buffer, VALID);
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002590 channel_need_redraw = TRUE;
2591 }
2592
2593 if (save_write_to)
2594 {
2595 channel_T *ch;
2596
2597 /* Find channels reading from this buffer and adjust their
2598 * next-to-read line number. */
2599 buffer->b_write_to_channel = TRUE;
2600 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2601 {
2602 chanpart_T *in_part = &ch->ch_part[PART_IN];
2603
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002604 if (in_part->ch_bufref.br_buf == buffer)
Bram Moolenaar99ef0622016-03-06 20:22:25 +01002605 in_part->ch_buf_bot = buffer->b_ml.ml_line_count;
2606 }
2607 }
2608}
2609
Bram Moolenaar437905c2016-04-26 19:01:05 +02002610 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002611drop_messages(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002612{
2613 char_u *msg;
2614
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002615 while ((msg = channel_get(channel, part, NULL)) != NULL)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002616 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002617 ch_log(channel, "Dropping message '%s'", (char *)msg);
Bram Moolenaar437905c2016-04-26 19:01:05 +02002618 vim_free(msg);
2619 }
2620}
2621
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002622/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002623 * Invoke a callback for "channel"/"part" if needed.
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02002624 * This does not redraw but sets channel_need_redraw when redraw is needed.
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002625 * Return TRUE when a message was handled, there might be another one.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002626 */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002627 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002628may_invoke_callback(channel_T *channel, ch_part_T part)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002629{
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002630 char_u *msg = NULL;
2631 typval_T *listtv = NULL;
Bram Moolenaarece61b02016-02-20 21:39:05 +01002632 typval_T argv[CH_JSON_MAX_ARGS];
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002633 int seq_nr = -1;
Bram Moolenaarec68a992016-10-03 21:37:41 +02002634 chanpart_T *ch_part = &channel->ch_part[part];
2635 ch_mode_T ch_mode = ch_part->ch_mode;
2636 cbq_T *cbhead = &ch_part->ch_cb_head;
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002637 cbq_T *cbitem;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002638 callback_T *callback = NULL;
Bram Moolenaar187db502016-02-27 14:44:26 +01002639 buf_T *buffer = NULL;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002640 char_u *p;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002641
Bram Moolenaar4e221c92016-02-23 13:20:22 +01002642 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002643 /* this channel is handled elsewhere (netbeans) */
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002644 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002645
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002646 /* Use a message-specific callback, part callback or channel callback */
2647 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2648 if (cbitem->cq_seq_nr == 0)
2649 break;
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002650 if (cbitem != NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002651 callback = &cbitem->cq_callback;
2652 else if (ch_part->ch_callback.cb_name != NULL)
2653 callback = &ch_part->ch_callback;
2654 else if (channel->ch_callback.cb_name != NULL)
2655 callback = &channel->ch_callback;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002656
Bram Moolenaarec68a992016-10-03 21:37:41 +02002657 buffer = ch_part->ch_bufref.br_buf;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002658 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2659 || buffer->b_ml.ml_mfp == NULL))
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002660 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002661 /* buffer was wiped out or unloaded */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002662 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
Bram Moolenaarec68a992016-10-03 21:37:41 +02002663 ch_part->ch_bufref.br_buf = NULL;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01002664 buffer = NULL;
2665 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002666
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002667 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002668 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002669 listitem_T *item;
2670 int argc = 0;
2671
Bram Moolenaard7ece102016-02-02 23:23:02 +01002672 /* Get any json message in the queue. */
Bram Moolenaar958dc692016-12-01 15:34:12 +01002673 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002674 {
Bram Moolenaard7ece102016-02-02 23:23:02 +01002675 /* Parse readahead, return when there is still no message. */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002676 channel_parse_json(channel, part);
Bram Moolenaar958dc692016-12-01 15:34:12 +01002677 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002678 return FALSE;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002679 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002680
Bram Moolenaarece61b02016-02-20 21:39:05 +01002681 for (item = listtv->vval.v_list->lv_first;
2682 item != NULL && argc < CH_JSON_MAX_ARGS;
2683 item = item->li_next)
2684 argv[argc++] = item->li_tv;
2685 while (argc < CH_JSON_MAX_ARGS)
2686 argv[argc++].v_type = VAR_UNKNOWN;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002687
Bram Moolenaarece61b02016-02-20 21:39:05 +01002688 if (argv[0].v_type == VAR_STRING)
2689 {
Bram Moolenaarece61b02016-02-20 21:39:05 +01002690 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */
Bram Moolenaarece61b02016-02-20 21:39:05 +01002691 channel_exe_cmd(channel, part, argv);
Bram Moolenaar77073442016-02-13 23:23:53 +01002692 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002693 return TRUE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002694 }
2695
Bram Moolenaarece61b02016-02-20 21:39:05 +01002696 if (argv[0].v_type != VAR_NUMBER)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002697 {
Bram Moolenaar77073442016-02-13 23:23:53 +01002698 ch_error(channel,
Bram Moolenaar81661fb2016-02-18 22:23:34 +01002699 "Dropping message with invalid sequence number type");
Bram Moolenaar77073442016-02-13 23:23:53 +01002700 free_tv(listtv);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002701 return FALSE;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002702 }
Bram Moolenaarece61b02016-02-20 21:39:05 +01002703 seq_nr = argv[0].vval.v_number;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002704 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002705 else if (channel_peek(channel, part) == NULL)
Bram Moolenaard7ece102016-02-02 23:23:02 +01002706 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002707 /* nothing to read on RAW or NL channel */
Bram Moolenaard7ece102016-02-02 23:23:02 +01002708 return FALSE;
2709 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002710 else
2711 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002712 /* If there is no callback or buffer drop the message. */
2713 if (callback == NULL && buffer == NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002714 {
Bram Moolenaar437905c2016-04-26 19:01:05 +02002715 /* If there is a close callback it may use ch_read() to get the
2716 * messages. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002717 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002718 drop_messages(channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002719 return FALSE;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002720 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002721
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002722 if (ch_mode == MODE_NL)
2723 {
Bram Moolenaarec68a992016-10-03 21:37:41 +02002724 char_u *nl = NULL;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002725 char_u *buf;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002726 readq_T *node;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002727
2728 /* See if we have a message ending in NL in the first buffer. If
2729 * not try to concatenate the first and the second buffer. */
2730 while (TRUE)
2731 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002732 node = channel_peek(channel, part);
2733 nl = channel_first_nl(node);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002734 if (nl != NULL)
2735 break;
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +02002736 if (channel_collapse(channel, part, TRUE) == FAIL)
Bram Moolenaarec68a992016-10-03 21:37:41 +02002737 {
2738 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2739 break;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002740 return FALSE; /* incomplete message */
Bram Moolenaarec68a992016-10-03 21:37:41 +02002741 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002742 }
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002743 buf = node->rq_buffer;
2744
Bram Moolenaar772153f2019-03-04 12:09:49 +01002745 // Convert NUL to NL, the internal representation.
2746 for (p = buf; (nl == NULL || p < nl)
2747 && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002748 if (*p == NUL)
2749 *p = NL;
2750
Bram Moolenaar772153f2019-03-04 12:09:49 +01002751 if (nl == NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01002752 {
Bram Moolenaar772153f2019-03-04 12:09:49 +01002753 // get the whole buffer, drop the NL
2754 msg = channel_get(channel, part, NULL);
2755 }
2756 else if (nl + 1 == buf + node->rq_buflen)
2757 {
2758 // get the whole buffer
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002759 msg = channel_get(channel, part, NULL);
Bram Moolenaar187db502016-02-27 14:44:26 +01002760 *nl = NUL;
2761 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002762 else
2763 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002764 /* Copy the message into allocated memory (excluding the NL)
2765 * and remove it from the buffer (including the NL). */
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002766 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002767 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002768 }
2769 }
2770 else
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002771 {
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002772 /* For a raw channel we don't know where the message ends, just
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002773 * get everything we have.
2774 * Convert NUL to NL, the internal representation. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002775 msg = channel_get_all(channel, part, NULL);
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02002776 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01002777
Bram Moolenaarbf73b912016-03-02 21:16:59 +01002778 if (msg == NULL)
2779 return FALSE; /* out of memory (and avoids Coverity warning) */
2780
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002781 argv[1].v_type = VAR_STRING;
2782 argv[1].vval.v_string = msg;
2783 }
2784
Bram Moolenaara07fec92016-02-05 21:04:08 +01002785 if (seq_nr > 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002786 {
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002787 int done = FALSE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002788
Bram Moolenaar958dc692016-12-01 15:34:12 +01002789 /* JSON or JS mode: invoke the one-time callback with the matching nr */
Bram Moolenaar5983ad02016-03-05 20:54:36 +01002790 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002791 if (cbitem->cq_seq_nr == seq_nr)
Bram Moolenaara07fec92016-02-05 21:04:08 +01002792 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002793 invoke_one_time_callback(channel, cbhead, cbitem, argv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002794 done = TRUE;
Bram Moolenaara07fec92016-02-05 21:04:08 +01002795 break;
2796 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002797 if (!done)
Bram Moolenaar958dc692016-12-01 15:34:12 +01002798 {
2799 if (channel->ch_drop_never)
2800 {
2801 /* message must be read with ch_read() */
2802 channel_push_json(channel, part, listtv);
2803 listtv = NULL;
2804 }
2805 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002806 ch_log(channel, "Dropping message %d without callback",
Bram Moolenaar958dc692016-12-01 15:34:12 +01002807 seq_nr);
2808 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002809 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002810 else if (callback != NULL || buffer != NULL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002811 {
Bram Moolenaar187db502016-02-27 14:44:26 +01002812 if (buffer != NULL)
2813 {
Bram Moolenaarcc7f8be2016-02-29 22:55:56 +01002814 if (msg == NULL)
2815 /* JSON or JS mode: re-encode the message. */
2816 msg = json_encode(listtv, ch_mode);
2817 if (msg != NULL)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002818 {
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002819#ifdef FEAT_TERMINAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002820 if (buffer->b_term != NULL)
2821 write_to_term(buffer, msg, channel);
2822 else
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02002823#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002824 append_to_buffer(buffer, msg, channel, part);
2825 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002826 }
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002827
Bram Moolenaar187db502016-02-27 14:44:26 +01002828 if (callback != NULL)
2829 {
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002830 if (cbitem != NULL)
2831 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2832 else
2833 {
2834 /* invoke the channel callback */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002835 ch_log(channel, "Invoking channel callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002836 (char *)callback->cb_name);
2837 invoke_callback(channel, callback, argv);
Bram Moolenaard6547fc2016-03-03 19:35:02 +01002838 }
Bram Moolenaar187db502016-02-27 14:44:26 +01002839 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002840 }
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002841 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002842 ch_log(channel, "Dropping message %d", seq_nr);
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01002843
Bram Moolenaar19d2f152016-02-01 21:38:19 +01002844 if (listtv != NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +01002845 free_tv(listtv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002846 vim_free(msg);
Bram Moolenaardf5b27b2016-02-02 18:43:17 +01002847
2848 return TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002849}
2850
Bram Moolenaar113e1072019-01-20 15:30:40 +01002851#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002852/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002853 * Return TRUE when channel "channel" is open for writing to.
2854 * Also returns FALSE or invalid "channel".
Bram Moolenaard04a0202016-01-26 23:30:18 +01002855 */
2856 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002857channel_can_write_to(channel_T *channel)
Bram Moolenaard04a0202016-01-26 23:30:18 +01002858{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002859 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002860 || channel->CH_IN_FD != INVALID_FD);
Bram Moolenaard04a0202016-01-26 23:30:18 +01002861}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002862#endif
Bram Moolenaard04a0202016-01-26 23:30:18 +01002863
2864/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002865 * Return TRUE when channel "channel" is open for reading or writing.
2866 * Also returns FALSE for invalid "channel".
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002867 */
2868 int
Bram Moolenaar77073442016-02-13 23:23:53 +01002869channel_is_open(channel_T *channel)
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002870{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002871 return channel != NULL && (channel->CH_SOCK_FD != INVALID_FD
Bram Moolenaar42d38a22016-02-20 18:18:59 +01002872 || channel->CH_IN_FD != INVALID_FD
2873 || channel->CH_OUT_FD != INVALID_FD
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01002874 || channel->CH_ERR_FD != INVALID_FD);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01002875}
2876
2877/*
Bram Moolenaar437905c2016-04-26 19:01:05 +02002878 * Return TRUE if "channel" has JSON or other typeahead.
2879 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002880 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002881channel_has_readahead(channel_T *channel, ch_part_T part)
Bram Moolenaar437905c2016-04-26 19:01:05 +02002882{
2883 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
2884
2885 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2886 {
2887 jsonq_T *head = &channel->ch_part[part].ch_json_head;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002888
Bram Moolenaar4340fc92019-06-28 22:06:49 +02002889 if (head->jq_next == NULL)
2890 // Parse json from readahead, there might be a complete message to
2891 // process.
2892 channel_parse_json(channel, part);
2893
2894 return head->jq_next != NULL;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002895 }
2896 return channel_peek(channel, part) != NULL;
2897}
2898
2899/*
Bram Moolenaar77073442016-02-13 23:23:53 +01002900 * Return a string indicating the status of the channel.
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002901 * If "req_part" is not negative check that part.
Bram Moolenaar77073442016-02-13 23:23:53 +01002902 */
2903 char *
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002904channel_status(channel_T *channel, int req_part)
Bram Moolenaar77073442016-02-13 23:23:53 +01002905{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002906 ch_part_T part;
Bram Moolenaar437905c2016-04-26 19:01:05 +02002907 int has_readahead = FALSE;
2908
Bram Moolenaar77073442016-02-13 23:23:53 +01002909 if (channel == NULL)
2910 return "fail";
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002911 if (req_part == PART_OUT)
2912 {
2913 if (channel->CH_OUT_FD != INVALID_FD)
2914 return "open";
2915 if (channel_has_readahead(channel, PART_OUT))
Bram Moolenaar437905c2016-04-26 19:01:05 +02002916 has_readahead = TRUE;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002917 }
2918 else if (req_part == PART_ERR)
2919 {
2920 if (channel->CH_ERR_FD != INVALID_FD)
2921 return "open";
2922 if (channel_has_readahead(channel, PART_ERR))
2923 has_readahead = TRUE;
2924 }
2925 else
2926 {
2927 if (channel_is_open(channel))
2928 return "open";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002929 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002930 if (channel_has_readahead(channel, part))
2931 {
2932 has_readahead = TRUE;
2933 break;
2934 }
2935 }
Bram Moolenaar437905c2016-04-26 19:01:05 +02002936
2937 if (has_readahead)
2938 return "buffered";
Bram Moolenaar77073442016-02-13 23:23:53 +01002939 return "closed";
2940}
2941
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002942 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02002943channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002944{
2945 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002946 char namebuf[20]; /* longest is "sock_timeout" */
Bram Moolenaar3f3fbd32016-03-21 12:36:28 +01002947 size_t tail;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002948 char *status;
Bram Moolenaar573e4452016-03-21 22:35:10 +01002949 char *s = "";
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002950
Bram Moolenaar925ccfd2016-03-28 22:38:02 +02002951 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002952 STRCAT(namebuf, "_");
2953 tail = STRLEN(namebuf);
2954
2955 STRCPY(namebuf + tail, "status");
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002956 if (chanpart->ch_fd != INVALID_FD)
2957 status = "open";
2958 else if (channel_has_readahead(channel, part))
2959 status = "buffered";
2960 else
2961 status = "closed";
Bram Moolenaare0be1672018-07-08 16:50:37 +02002962 dict_add_string(dict, namebuf, (char_u *)status);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002963
2964 STRCPY(namebuf + tail, "mode");
2965 switch (chanpart->ch_mode)
2966 {
2967 case MODE_NL: s = "NL"; break;
2968 case MODE_RAW: s = "RAW"; break;
2969 case MODE_JSON: s = "JSON"; break;
2970 case MODE_JS: s = "JS"; break;
2971 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002972 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002973
2974 STRCPY(namebuf + tail, "io");
2975 if (part == PART_SOCK)
2976 s = "socket";
2977 else switch (chanpart->ch_io)
2978 {
2979 case JIO_NULL: s = "null"; break;
2980 case JIO_PIPE: s = "pipe"; break;
2981 case JIO_FILE: s = "file"; break;
2982 case JIO_BUFFER: s = "buffer"; break;
2983 case JIO_OUT: s = "out"; break;
2984 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02002985 dict_add_string(dict, namebuf, (char_u *)s);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002986
2987 STRCPY(namebuf + tail, "timeout");
Bram Moolenaare0be1672018-07-08 16:50:37 +02002988 dict_add_number(dict, namebuf, chanpart->ch_timeout);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002989}
2990
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002991 static void
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002992channel_info(channel_T *channel, dict_T *dict)
2993{
Bram Moolenaare0be1672018-07-08 16:50:37 +02002994 dict_add_number(dict, "id", channel->ch_id);
2995 dict_add_string(dict, "status", (char_u *)channel_status(channel, -1));
Bram Moolenaar03602ec2016-03-20 20:57:45 +01002996
2997 if (channel->ch_hostname != NULL)
2998 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02002999 dict_add_string(dict, "hostname", (char_u *)channel->ch_hostname);
3000 dict_add_number(dict, "port", channel->ch_port);
Bram Moolenaar03602ec2016-03-20 20:57:45 +01003001 channel_part_info(channel, dict, "sock", PART_SOCK);
3002 }
3003 else
3004 {
3005 channel_part_info(channel, dict, "out", PART_OUT);
3006 channel_part_info(channel, dict, "err", PART_ERR);
3007 channel_part_info(channel, dict, "in", PART_IN);
3008 }
3009}
3010
Bram Moolenaar77073442016-02-13 23:23:53 +01003011/*
3012 * Close channel "channel".
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01003013 * Trigger the close callback if "invoke_close_cb" is TRUE.
Bram Moolenaar187db502016-02-27 14:44:26 +01003014 * Does not clear the buffers.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003015 */
3016 void
Bram Moolenaar8b374212016-02-24 20:43:06 +01003017channel_close(channel_T *channel, int invoke_close_cb)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003018{
Bram Moolenaar81661fb2016-02-18 22:23:34 +01003019 ch_log(channel, "Closing channel");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003020
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003021#ifdef FEAT_GUI
3022 channel_gui_unregister(channel);
3023#endif
3024
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003025 ch_close_part(channel, PART_SOCK);
3026 ch_close_part(channel, PART_IN);
3027 ch_close_part(channel, PART_OUT);
3028 ch_close_part(channel, PART_ERR);
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01003029
Bram Moolenaara9f02812017-08-05 17:40:38 +02003030 if (invoke_close_cb)
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003031 {
Bram Moolenaara9f02812017-08-05 17:40:38 +02003032 ch_part_T part;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003033
Bram Moolenaara9f02812017-08-05 17:40:38 +02003034 /* Invoke callbacks and flush buffers before the close callback. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003035 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003036 ch_log(channel,
3037 "Invoking callbacks and flushing buffers before closing");
3038 for (part = PART_SOCK; part < PART_IN; ++part)
3039 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003040 if (channel->ch_close_cb.cb_name != NULL
Bram Moolenaara9f02812017-08-05 17:40:38 +02003041 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3042 {
3043 /* Increment the refcount to avoid the channel being freed
3044 * halfway. */
3045 ++channel->ch_refcount;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003046 if (channel->ch_close_cb.cb_name == NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003047 ch_log(channel, "flushing %s buffers before closing",
3048 part_names[part]);
3049 while (may_invoke_callback(channel, part))
3050 ;
3051 --channel->ch_refcount;
3052 }
3053 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003054
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003055 if (channel->ch_close_cb.cb_name != NULL)
Bram Moolenaara9f02812017-08-05 17:40:38 +02003056 {
3057 typval_T argv[1];
3058 typval_T rettv;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003059
3060 /* Increment the refcount to avoid the channel being freed
3061 * halfway. */
3062 ++channel->ch_refcount;
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003063 ch_log(channel, "Invoking close callback %s",
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003064 (char *)channel->ch_close_cb.cb_name);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003065 argv[0].v_type = VAR_CHANNEL;
3066 argv[0].vval.v_channel = channel;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003067 call_callback(&channel->ch_close_cb, -1, &rettv, 1, argv);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003068 clear_tv(&rettv);
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003069 channel_need_redraw = TRUE;
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003070
Bram Moolenaara9f02812017-08-05 17:40:38 +02003071 /* the callback is only called once */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003072 free_callback(&channel->ch_close_cb);
Bram Moolenaar437905c2016-04-26 19:01:05 +02003073
Bram Moolenaara9f02812017-08-05 17:40:38 +02003074 if (channel_need_redraw)
3075 {
3076 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003077 redraw_after_callback(TRUE);
Bram Moolenaara9f02812017-08-05 17:40:38 +02003078 }
Bram Moolenaarcefe4f92016-05-04 21:49:19 +02003079
Bram Moolenaara9f02812017-08-05 17:40:38 +02003080 if (!channel->ch_drop_never)
3081 /* any remaining messages are useless now */
3082 for (part = PART_SOCK; part < PART_IN; ++part)
3083 drop_messages(channel, part);
Bram Moolenaar5fd8b782017-11-11 15:54:00 +01003084
3085 --channel->ch_refcount;
Bram Moolenaara9f02812017-08-05 17:40:38 +02003086 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +01003087 }
3088
3089 channel->ch_nb_close_cb = NULL;
Bram Moolenaard85f2712017-07-28 21:51:57 +02003090
3091#ifdef FEAT_TERMINAL
3092 term_channel_closed(channel);
3093#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +01003094}
3095
Bram Moolenaard04a0202016-01-26 23:30:18 +01003096/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02003097 * Close the "in" part channel "channel".
3098 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003099 static void
Bram Moolenaar0874a832016-09-01 15:11:51 +02003100channel_close_in(channel_T *channel)
3101{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003102 ch_close_part(channel, PART_IN);
Bram Moolenaar0874a832016-09-01 15:11:51 +02003103}
3104
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003105 static void
3106remove_from_writeque(writeq_T *wq, writeq_T *entry)
3107{
3108 ga_clear(&entry->wq_ga);
3109 wq->wq_next = entry->wq_next;
3110 if (wq->wq_next == NULL)
3111 wq->wq_prev = NULL;
3112 else
3113 wq->wq_next->wq_prev = NULL;
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003114 vim_free(entry);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003115}
3116
Bram Moolenaar0874a832016-09-01 15:11:51 +02003117/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003118 * Clear the read buffer on "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003119 */
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003120 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003121channel_clear_one(channel_T *channel, ch_part_T part)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003122{
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003123 chanpart_T *ch_part = &channel->ch_part[part];
3124 jsonq_T *json_head = &ch_part->ch_json_head;
3125 cbq_T *cb_head = &ch_part->ch_cb_head;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003126
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003127 while (channel_peek(channel, part) != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003128 vim_free(channel_get(channel, part, NULL));
Bram Moolenaar77073442016-02-13 23:23:53 +01003129
3130 while (cb_head->cq_next != NULL)
Bram Moolenaard46ae142016-02-16 13:33:52 +01003131 {
3132 cbq_T *node = cb_head->cq_next;
3133
3134 remove_cb_node(cb_head, node);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003135 free_callback(&node->cq_callback);
Bram Moolenaard46ae142016-02-16 13:33:52 +01003136 vim_free(node);
3137 }
Bram Moolenaar77073442016-02-13 23:23:53 +01003138
3139 while (json_head->jq_next != NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003140 {
Bram Moolenaar77073442016-02-13 23:23:53 +01003141 free_tv(json_head->jq_next->jq_value);
3142 remove_json_node(json_head, json_head->jq_next);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003143 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01003144
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003145 free_callback(&ch_part->ch_callback);
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003146 ga_clear(&ch_part->ch_block_ids);
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003147
3148 while (ch_part->ch_writeque.wq_next != NULL)
3149 remove_from_writeque(&ch_part->ch_writeque,
3150 ch_part->ch_writeque.wq_next);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003151}
3152
3153/*
3154 * Clear all the read buffers on "channel".
3155 */
3156 void
3157channel_clear(channel_T *channel)
3158{
Bram Moolenaard6051b52016-02-28 15:49:03 +01003159 ch_log(channel, "Clearing channel");
Bram Moolenaard23a8232018-02-10 18:45:26 +01003160 VIM_CLEAR(channel->ch_hostname);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003161 channel_clear_one(channel, PART_SOCK);
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003162 channel_clear_one(channel, PART_OUT);
3163 channel_clear_one(channel, PART_ERR);
Bram Moolenaar5b5adf52017-09-09 18:16:43 +02003164 channel_clear_one(channel, PART_IN);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003165 free_callback(&channel->ch_callback);
3166 free_callback(&channel->ch_close_cb);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003167}
3168
Bram Moolenaar77073442016-02-13 23:23:53 +01003169#if defined(EXITFREE) || defined(PROTO)
3170 void
3171channel_free_all(void)
3172{
3173 channel_T *channel;
3174
Bram Moolenaard6051b52016-02-28 15:49:03 +01003175 ch_log(NULL, "channel_free_all()");
Bram Moolenaar77073442016-02-13 23:23:53 +01003176 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3177 channel_clear(channel);
3178}
3179#endif
3180
3181
Bram Moolenaar715d2852016-04-30 17:06:31 +02003182/* Sent when the netbeans channel is found closed when reading. */
Bram Moolenaareed284a2016-02-22 23:13:33 +01003183#define DETACH_MSG_RAW "DETACH\n"
Bram Moolenaard04a0202016-01-26 23:30:18 +01003184
3185/* Buffer size for reading incoming messages. */
3186#define MAXMSGSIZE 4096
3187
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003188#if defined(HAVE_SELECT)
3189/*
3190 * Add write fds where we are waiting for writing to be possible.
3191 */
3192 static int
3193channel_fill_wfds(int maxfd_arg, fd_set *wfds)
3194{
3195 int maxfd = maxfd_arg;
3196 channel_T *ch;
3197
3198 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3199 {
3200 chanpart_T *in_part = &ch->ch_part[PART_IN];
3201
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003202 if (in_part->ch_fd != INVALID_FD
3203 && (in_part->ch_bufref.br_buf != NULL
3204 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003205 {
3206 FD_SET((int)in_part->ch_fd, wfds);
3207 if ((int)in_part->ch_fd >= maxfd)
3208 maxfd = (int)in_part->ch_fd + 1;
3209 }
3210 }
3211 return maxfd;
3212}
3213#else
3214/*
3215 * Add write fds where we are waiting for writing to be possible.
3216 */
3217 static int
3218channel_fill_poll_write(int nfd_in, struct pollfd *fds)
3219{
3220 int nfd = nfd_in;
3221 channel_T *ch;
3222
3223 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
3224 {
3225 chanpart_T *in_part = &ch->ch_part[PART_IN];
3226
Bram Moolenaar683b7962017-08-19 15:51:59 +02003227 if (in_part->ch_fd != INVALID_FD
3228 && (in_part->ch_bufref.br_buf != NULL
3229 || in_part->ch_writeque.wq_next != NULL))
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003230 {
3231 in_part->ch_poll_idx = nfd;
3232 fds[nfd].fd = in_part->ch_fd;
3233 fds[nfd].events = POLLOUT;
3234 ++nfd;
3235 }
3236 else
3237 in_part->ch_poll_idx = -1;
3238 }
3239 return nfd;
3240}
3241#endif
3242
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003243typedef enum {
3244 CW_READY,
3245 CW_NOT_READY,
3246 CW_ERROR
3247} channel_wait_result;
3248
Bram Moolenaard04a0202016-01-26 23:30:18 +01003249/*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003250 * Check for reading from "fd" with "timeout" msec.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003251 * Return CW_READY when there is something to read.
3252 * Return CW_NOT_READY when there is nothing to read.
3253 * Return CW_ERROR when there is an error.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003254 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003255 static channel_wait_result
Bram Moolenaard8070362016-02-15 21:56:54 +01003256channel_wait(channel_T *channel, sock_T fd, int timeout)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003257{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003258 if (timeout > 0)
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003259 ch_log(channel, "Waiting for up to %d msec", timeout);
Bram Moolenaard8070362016-02-15 21:56:54 +01003260
Bram Moolenaar4f974752019-02-17 17:44:42 +01003261# ifdef MSWIN
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003262 if (fd != channel->CH_SOCK_FD)
Bram Moolenaard8070362016-02-15 21:56:54 +01003263 {
3264 DWORD nread;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003265 int sleep_time;
Bram Moolenaard8070362016-02-15 21:56:54 +01003266 DWORD deadline = GetTickCount() + timeout;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003267 int delay = 1;
Bram Moolenaard8070362016-02-15 21:56:54 +01003268
3269 /* reading from a pipe, not a socket */
3270 while (TRUE)
3271 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003272 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3273
3274 if (r && nread > 0)
3275 return CW_READY;
Bram Moolenaar31faed62019-01-22 23:01:40 +01003276
3277 if (channel->ch_named_pipe)
3278 {
3279 DisconnectNamedPipe((HANDLE)fd);
3280 ConnectNamedPipe((HANDLE)fd, NULL);
3281 }
3282 else if (r == 0)
Bram Moolenaarb091f302019-01-19 14:37:00 +01003283 return CW_ERROR;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003284
3285 /* perhaps write some buffer lines */
3286 channel_write_any_lines();
3287
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003288 sleep_time = deadline - GetTickCount();
3289 if (sleep_time <= 0)
Bram Moolenaard8070362016-02-15 21:56:54 +01003290 break;
Bram Moolenaar84e1d2b2016-03-28 14:20:41 +02003291 /* Wait for a little while. Very short at first, up to 10 msec
3292 * after looping a few times. */
3293 if (sleep_time > delay)
3294 sleep_time = delay;
3295 Sleep(sleep_time);
3296 delay = delay * 2;
3297 if (delay > 10)
3298 delay = 10;
Bram Moolenaard8070362016-02-15 21:56:54 +01003299 }
Bram Moolenaard8070362016-02-15 21:56:54 +01003300 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003301 else
Bram Moolenaard8070362016-02-15 21:56:54 +01003302#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003303 {
Bram Moolenaar9186a272016-02-23 19:34:01 +01003304#if defined(HAVE_SELECT)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003305 struct timeval tval;
3306 fd_set rfds;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003307 fd_set wfds;
3308 int ret;
3309 int maxfd;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003310
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003311 tval.tv_sec = timeout / 1000;
3312 tval.tv_usec = (timeout % 1000) * 1000;
3313 for (;;)
3314 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003315 FD_ZERO(&rfds);
3316 FD_SET((int)fd, &rfds);
3317
3318 /* Write lines to a pipe when a pipe can be written to. Need to
3319 * set this every time, some buffers may be done. */
3320 maxfd = (int)fd + 1;
3321 FD_ZERO(&wfds);
3322 maxfd = channel_fill_wfds(maxfd, &wfds);
3323
3324 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
Bram Moolenaar9186a272016-02-23 19:34:01 +01003325# ifdef EINTR
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003326 SOCK_ERRNO;
3327 if (ret == -1 && errno == EINTR)
3328 continue;
Bram Moolenaar9186a272016-02-23 19:34:01 +01003329# endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003330 if (ret > 0)
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003331 {
3332 if (FD_ISSET(fd, &rfds))
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003333 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003334 channel_write_any_lines();
3335 continue;
3336 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003337 break;
3338 }
Bram Moolenaar9186a272016-02-23 19:34:01 +01003339#else
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003340 for (;;)
3341 {
3342 struct pollfd fds[MAX_OPEN_CHANNELS + 1];
3343 int nfd = 1;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003344
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003345 fds[0].fd = fd;
3346 fds[0].events = POLLIN;
3347 nfd = channel_fill_poll_write(nfd, fds);
3348 if (poll(fds, nfd, timeout) > 0)
3349 {
3350 if (fds[0].revents & POLLIN)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003351 return CW_READY;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02003352 channel_write_any_lines();
3353 continue;
3354 }
3355 break;
3356 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003357#endif
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003358 }
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003359 return CW_NOT_READY;
3360}
3361
3362 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003363ch_close_part_on_error(
3364 channel_T *channel, ch_part_T part, int is_err, char *func)
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003365{
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003366 char msg[] = "%s(): Read %s from ch_part[%d], closing";
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003367
3368 if (is_err)
3369 /* Do not call emsg(), most likely the other end just exited. */
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003370 ch_error(channel, msg, func, "error", part);
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003371 else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003372 ch_log(channel, msg, func, "EOF", part);
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003373
3374 /* Queue a "DETACH" netbeans message in the command queue in order to
3375 * terminate the netbeans session later. Do not end the session here
3376 * directly as we may be running in the context of a call to
3377 * netbeans_parse_messages():
3378 * netbeans_parse_messages
3379 * -> autocmd triggered while processing the netbeans cmd
3380 * -> ui_breakcheck
3381 * -> gui event loop or select loop
3382 * -> channel_read()
Bram Moolenaar715d2852016-04-30 17:06:31 +02003383 * Only send "DETACH" for a netbeans channel.
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003384 */
Bram Moolenaar715d2852016-04-30 17:06:31 +02003385 if (channel->ch_nb_close_cb != NULL)
Bram Moolenaar8ddef482016-10-09 15:43:25 +02003386 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003387 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3388
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003389 /* When reading is not possible close this part of the channel. Don't
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02003390 * close the channel yet, there may be something to read on another part.
3391 * When stdout and stderr use the same FD we get the error only on one of
3392 * them, also close the other. */
3393 if (part == PART_OUT || part == PART_ERR)
3394 {
3395 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3396
3397 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3398 ch_close_part(channel, other);
3399 }
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003400 ch_close_part(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003401
3402#ifdef FEAT_GUI
3403 /* Stop listening to GUI events right away. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003404 channel_gui_unregister_one(channel, part);
Bram Moolenaarbf981ee2016-05-28 13:20:31 +02003405#endif
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02003406}
3407
3408 static void
3409channel_close_now(channel_T *channel)
3410{
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003411 ch_log(channel, "Closing channel because all readable fds are closed");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003412 if (channel->ch_nb_close_cb != NULL)
3413 (*channel->ch_nb_close_cb)();
Bram Moolenaar958dc692016-12-01 15:34:12 +01003414 channel_close(channel, TRUE);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003415}
3416
3417/*
Bram Moolenaar77073442016-02-13 23:23:53 +01003418 * Read from channel "channel" for as long as there is something to read.
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003419 * "part" is PART_SOCK, PART_OUT or PART_ERR.
Bram Moolenaar655da312016-05-28 22:22:34 +02003420 * The data is put in the read queue. No callbacks are invoked here.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003421 */
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003422 static void
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003423channel_read(channel_T *channel, ch_part_T part, char *func)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003424{
3425 static char_u *buf = NULL;
3426 int len = 0;
3427 int readlen = 0;
Bram Moolenaard8070362016-02-15 21:56:54 +01003428 sock_T fd;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003429 int use_socket = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003430
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003431 fd = channel->ch_part[part].ch_fd;
3432 if (fd == INVALID_FD)
3433 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003434 ch_error(channel, "channel_read() called while %s part is closed",
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003435 part_names[part]);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003436 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003437 }
3438 use_socket = fd == channel->CH_SOCK_FD;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003439
3440 /* Allocate a buffer to read into. */
3441 if (buf == NULL)
3442 {
3443 buf = alloc(MAXMSGSIZE);
3444 if (buf == NULL)
3445 return; /* out of memory! */
3446 }
3447
3448 /* Keep on reading for as long as there is something to read.
3449 * Use select() or poll() to avoid blocking on a message that is exactly
3450 * MAXMSGSIZE long. */
3451 for (;;)
3452 {
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003453 if (channel_wait(channel, fd, 0) != CW_READY)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003454 break;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003455 if (use_socket)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003456 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003457 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003458 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003459 if (len <= 0)
3460 break; /* error or nothing more to read */
3461
3462 /* Store the read message in the queue. */
Bram Moolenaar46c00a62016-03-28 14:11:42 +02003463 channel_save(channel, part, buf, len, FALSE, "RECV ");
Bram Moolenaard04a0202016-01-26 23:30:18 +01003464 readlen += len;
3465 if (len < MAXMSGSIZE)
3466 break; /* did read everything that's available */
3467 }
3468
Bram Moolenaar4cafa6d2016-02-26 11:52:39 +01003469 /* Reading a disconnection (readlen == 0), or an error. */
Bram Moolenaarbd73ae12016-02-22 22:19:22 +01003470 if (readlen <= 0)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003471 {
3472 if (!channel->ch_keep_open)
3473 ch_close_part_on_error(channel, part, (len < 0), func);
3474 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01003475#if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003476 else if (CH_HAS_GUI && gtk_main_level() > 0)
3477 /* signal the main loop that there is something to read */
Bram Moolenaard04a0202016-01-26 23:30:18 +01003478 gtk_main_quit();
3479#endif
3480}
3481
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003482/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003483 * Read from RAW or NL "channel"/"part". Blocks until there is something to
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003484 * read or the timeout expires.
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003485 * When "raw" is TRUE don't block waiting on a NL.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003486 * Does not trigger timers or handle messages.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003487 * Returns what was read in allocated memory.
3488 * Returns NULL in case of error or timeout.
3489 */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003490 static char_u *
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003491channel_read_block(
3492 channel_T *channel, ch_part_T part, int timeout, int raw, int *outlen)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003493{
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003494 char_u *buf;
3495 char_u *msg;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003496 ch_mode_T mode = channel->ch_part[part].ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003497 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003498 char_u *nl;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003499 readq_T *node;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003500
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003501 ch_log(channel, "Blocking %s read, timeout: %d msec",
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003502 mode == MODE_RAW ? "RAW" : "NL", timeout);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003503
3504 while (TRUE)
3505 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003506 node = channel_peek(channel, part);
3507 if (node != NULL)
3508 {
3509 if (mode == MODE_RAW || (mode == MODE_NL
3510 && channel_first_nl(node) != NULL))
3511 /* got a complete message */
3512 break;
3513 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3514 continue;
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003515 /* If not blocking or nothing more is coming then return what we
3516 * have. */
3517 if (raw || fd == INVALID_FD)
3518 break;
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003519 }
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003520
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003521 /* Wait for up to the channel timeout. */
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003522 if (fd == INVALID_FD)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003523 return NULL;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003524 if (channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003525 {
3526 ch_log(channel, "Timed out");
3527 return NULL;
3528 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003529 channel_read(channel, part, "channel_read_block");
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003530 }
3531
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01003532 /* We have a complete message now. */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003533 if (mode == MODE_RAW || outlen != NULL)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003534 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003535 msg = channel_get_all(channel, part, outlen);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003536 }
3537 else
3538 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003539 char_u *p;
3540
3541 buf = node->rq_buffer;
3542 nl = channel_first_nl(node);
3543
3544 /* Convert NUL to NL, the internal representation. */
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003545 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003546 if (*p == NUL)
3547 *p = NL;
3548
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003549 if (nl == NULL)
3550 {
3551 /* must be a closed channel with missing NL */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003552 msg = channel_get(channel, part, NULL);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003553 }
3554 else if (nl + 1 == buf + node->rq_buflen)
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003555 {
3556 /* get the whole buffer */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003557 msg = channel_get(channel, part, NULL);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003558 *nl = NUL;
3559 }
3560 else
3561 {
3562 /* Copy the message into allocated memory and remove it from the
3563 * buffer. */
3564 msg = vim_strnsave(buf, (int)(nl - buf));
Bram Moolenaar5f1032d2016-06-07 22:16:36 +02003565 channel_consume(channel, part, (int)(nl - buf) + 1);
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003566 }
3567 }
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003568 if (ch_log_active())
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003569 ch_log(channel, "Returning %d bytes", (int)STRLEN(msg));
Bram Moolenaar9a6e33a2016-02-16 19:25:12 +01003570 return msg;
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003571}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003572
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003573static int channel_blocking_wait = 0;
3574
3575/*
3576 * Return TRUE if in a blocking wait that might trigger callbacks.
3577 */
3578 int
3579channel_in_blocking_wait(void)
3580{
3581 return channel_blocking_wait > 0;
3582}
3583
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003584/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003585 * Read one JSON message with ID "id" from "channel"/"part" and store the
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003586 * result in "rettv".
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003587 * When "id" is -1 accept any message;
Bram Moolenaar4d919d72016-02-05 22:36:41 +01003588 * Blocks until the message is received or the timeout is reached.
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003589 * In corner cases this can be called recursively, that is why ch_block_ids is
3590 * a list.
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003591 */
Bram Moolenaar958dc692016-12-01 15:34:12 +01003592 static int
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01003593channel_read_json_block(
Bram Moolenaard6051b52016-02-28 15:49:03 +01003594 channel_T *channel,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003595 ch_part_T part,
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003596 int timeout_arg,
Bram Moolenaard6051b52016-02-28 15:49:03 +01003597 int id,
3598 typval_T **rettv)
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003599{
Bram Moolenaare56bf152016-02-08 23:23:42 +01003600 int more;
Bram Moolenaard8070362016-02-15 21:56:54 +01003601 sock_T fd;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003602 int timeout;
3603 chanpart_T *chanpart = &channel->ch_part[part];
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003604 int retval = FAIL;
Bram Moolenaard7ece102016-02-02 23:23:02 +01003605
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003606 ch_log(channel, "Blocking read JSON for id %d", id);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003607 ++channel_blocking_wait;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003608
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003609 if (id >= 0)
3610 channel_add_block_id(chanpart, id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003611
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003612 for (;;)
3613 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003614 more = channel_parse_json(channel, part);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003615
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003616 // search for message "id"
Bram Moolenaar958dc692016-12-01 15:34:12 +01003617 if (channel_get_json(channel, part, id, TRUE, rettv) == OK)
Bram Moolenaare56bf152016-02-08 23:23:42 +01003618 {
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003619 ch_log(channel, "Received JSON for id %d", id);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003620 retval = OK;
3621 break;
Bram Moolenaare56bf152016-02-08 23:23:42 +01003622 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003623
Bram Moolenaard7ece102016-02-02 23:23:02 +01003624 if (!more)
3625 {
3626 /* Handle any other messages in the queue. If done some more
3627 * messages may have arrived. */
3628 if (channel_parse_messages())
3629 continue;
3630
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003631 /* Wait for up to the timeout. If there was an incomplete message
3632 * use the deadline for that. */
3633 timeout = timeout_arg;
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003634 if (chanpart->ch_wait_len > 0)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003635 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003636#ifdef MSWIN
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003637 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3638#else
3639 {
3640 struct timeval now_tv;
3641
3642 gettimeofday(&now_tv, NULL);
3643 timeout = (chanpart->ch_deadline.tv_sec
3644 - now_tv.tv_sec) * 1000
3645 + (chanpart->ch_deadline.tv_usec
3646 - now_tv.tv_usec) / 1000
3647 + 1;
3648 }
3649#endif
3650 if (timeout < 0)
3651 {
3652 /* Something went wrong, channel_parse_json() didn't
3653 * discard message. Cancel waiting. */
Bram Moolenaar88989cc2017-02-06 21:56:09 +01003654 chanpart->ch_wait_len = 0;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003655 timeout = timeout_arg;
3656 }
3657 else if (timeout > timeout_arg)
3658 timeout = timeout_arg;
3659 }
3660 fd = chanpart->ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003661 if (fd == INVALID_FD
3662 || channel_wait(channel, fd, timeout) != CW_READY)
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003663 {
3664 if (timeout == timeout_arg)
3665 {
3666 if (fd != INVALID_FD)
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003667 ch_log(channel, "Timed out on id %d", id);
Bram Moolenaarba61ac02016-03-20 16:40:37 +01003668 break;
3669 }
3670 }
3671 else
3672 channel_read(channel, part, "channel_read_json_block");
Bram Moolenaard7ece102016-02-02 23:23:02 +01003673 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01003674 }
Bram Moolenaar38ea7842019-06-09 19:51:58 +02003675 if (id >= 0)
3676 channel_remove_block_id(chanpart, id);
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003677 --channel_blocking_wait;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003678
Bram Moolenaar8aeec402019-09-15 23:02:04 +02003679 return retval;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003680}
3681
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003682/*
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02003683 * Get the channel from the argument.
3684 * Returns NULL if the handle is invalid.
3685 * When "check_open" is TRUE check that the channel can be used.
3686 * When "reading" is TRUE "check_open" considers typeahead useful.
3687 * "part" is used to check typeahead, when PART_COUNT use the default part.
3688 */
3689 static channel_T *
3690get_channel_arg(typval_T *tv, int check_open, int reading, ch_part_T part)
3691{
3692 channel_T *channel = NULL;
3693 int has_readahead = FALSE;
3694
3695 if (tv->v_type == VAR_JOB)
3696 {
3697 if (tv->vval.v_job != NULL)
3698 channel = tv->vval.v_job->jv_channel;
3699 }
3700 else if (tv->v_type == VAR_CHANNEL)
3701 {
3702 channel = tv->vval.v_channel;
3703 }
3704 else
3705 {
3706 semsg(_(e_invarg2), tv_get_string(tv));
3707 return NULL;
3708 }
3709 if (channel != NULL && reading)
3710 has_readahead = channel_has_readahead(channel,
3711 part != PART_COUNT ? part : channel_part_read(channel));
3712
3713 if (check_open && (channel == NULL || (!channel_is_open(channel)
3714 && !(reading && has_readahead))))
3715 {
3716 emsg(_("E906: not an open channel"));
3717 return NULL;
3718 }
3719 return channel;
3720}
3721
3722/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003723 * Common for ch_read() and ch_readraw().
3724 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003725 static void
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003726common_channel_read(typval_T *argvars, typval_T *rettv, int raw, int blob)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003727{
3728 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003729 ch_part_T part = PART_COUNT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003730 jobopt_T opt;
3731 int mode;
3732 int timeout;
3733 int id = -1;
3734 typval_T *listtv = NULL;
3735
3736 /* return an empty string by default */
3737 rettv->v_type = VAR_STRING;
3738 rettv->vval.v_string = NULL;
3739
3740 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02003741 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003742 == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003743 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003744
Bram Moolenaar437905c2016-04-26 19:01:05 +02003745 if (opt.jo_set & JO_PART)
3746 part = opt.jo_part;
3747 channel = get_channel_arg(&argvars[0], TRUE, TRUE, part);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003748 if (channel != NULL)
3749 {
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003750 if (part == PART_COUNT)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003751 part = channel_part_read(channel);
3752 mode = channel_get_mode(channel, part);
3753 timeout = channel_get_timeout(channel, part);
3754 if (opt.jo_set & JO_TIMEOUT)
3755 timeout = opt.jo_timeout;
3756
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003757 if (blob)
3758 {
3759 int outlen = 0;
3760 char_u *p = channel_read_block(channel, part,
3761 timeout, TRUE, &outlen);
3762 if (p != NULL)
3763 {
3764 blob_T *b = blob_alloc();
3765
3766 if (b != NULL)
3767 {
3768 b->bv_ga.ga_len = outlen;
3769 if (ga_grow(&b->bv_ga, outlen) == FAIL)
3770 blob_free(b);
3771 else
3772 {
3773 memcpy(b->bv_ga.ga_data, p, outlen);
3774 rettv_blob_set(rettv, b);
3775 }
3776 }
3777 vim_free(p);
3778 }
3779 }
3780 else if (raw || mode == MODE_RAW || mode == MODE_NL)
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01003781 rettv->vval.v_string = channel_read_block(channel, part,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003782 timeout, raw, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003783 else
3784 {
3785 if (opt.jo_set & JO_ID)
3786 id = opt.jo_id;
3787 channel_read_json_block(channel, part, timeout, id, &listtv);
3788 if (listtv != NULL)
3789 {
3790 *rettv = *listtv;
3791 vim_free(listtv);
3792 }
3793 else
3794 {
3795 rettv->v_type = VAR_SPECIAL;
3796 rettv->vval.v_number = VVAL_NONE;
3797 }
3798 }
3799 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02003800
3801theend:
3802 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01003803}
3804
Bram Moolenaar4f974752019-02-17 17:44:42 +01003805# if defined(MSWIN) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003806/*
3807 * Check the channels for anything that is ready to be read.
3808 * The data is put in the read queue.
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003809 * if "only_keep_open" is TRUE only check channels where ch_keep_open is set.
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003810 */
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003811 void
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003812channel_handle_events(int only_keep_open)
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003813{
3814 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003815 ch_part_T part;
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003816 sock_T fd;
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003817
3818 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3819 {
Bram Moolenaar13ebb032017-08-26 22:02:51 +02003820 if (only_keep_open && !channel->ch_keep_open)
3821 continue;
3822
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003823 /* check the socket and pipes */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003824 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003825 {
3826 fd = channel->ch_part[part].ch_fd;
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003827 if (fd != INVALID_FD)
3828 {
3829 int r = channel_wait(channel, fd, 0);
3830
3831 if (r == CW_READY)
3832 channel_read(channel, part, "channel_handle_events");
3833 else if (r == CW_ERROR)
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003834 ch_close_part_on_error(channel, part, TRUE,
3835 "channel_handle_events");
Bram Moolenaarb2658a12016-04-26 17:16:24 +02003836 }
Bram Moolenaarb7522a22016-02-21 17:20:55 +01003837 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +01003838 }
3839}
Bram Moolenaar85be35f2016-01-27 21:08:18 +01003840# endif
3841
Bram Moolenaar4ab79682017-08-27 14:50:47 +02003842# if defined(FEAT_GUI) || defined(PROTO)
3843/*
3844 * Return TRUE when there is any channel with a keep_open flag.
3845 */
3846 int
3847channel_any_keep_open()
3848{
3849 channel_T *channel;
3850
3851 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3852 if (channel->ch_keep_open)
3853 return TRUE;
3854 return FALSE;
3855}
3856# endif
3857
Bram Moolenaard04a0202016-01-26 23:30:18 +01003858/*
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003859 * Set "channel"/"part" to non-blocking.
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003860 * Only works for sockets and pipes.
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003861 */
3862 void
3863channel_set_nonblock(channel_T *channel, ch_part_T part)
3864{
3865 chanpart_T *ch_part = &channel->ch_part[part];
Bram Moolenaar0b146882018-09-06 16:27:24 +02003866 int fd = ch_part->ch_fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003867
3868 if (fd != INVALID_FD)
3869 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003870#ifdef MSWIN
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003871 u_long val = 1;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003872
Bram Moolenaarf66a2cd2017-08-18 21:53:22 +02003873 ioctlsocket(fd, FIONBIO, &val);
3874#else
Bram Moolenaardc926dd2017-08-19 21:26:44 +02003875 (void)fcntl(fd, F_SETFL, O_NONBLOCK);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003876#endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003877 ch_part->ch_nonblocking = TRUE;
3878 }
3879}
3880
3881/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003882 * Write "buf" (NUL terminated string) to "channel"/"part".
Bram Moolenaard04a0202016-01-26 23:30:18 +01003883 * When "fun" is not NULL an error message might be given.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003884 * Return FAIL or OK.
Bram Moolenaard04a0202016-01-26 23:30:18 +01003885 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003886 int
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003887channel_send(
3888 channel_T *channel,
3889 ch_part_T part,
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003890 char_u *buf_arg,
3891 int len_arg,
Bram Moolenaar79cbdcb2016-11-11 21:14:03 +01003892 char *fun)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003893{
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003894 int res;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003895 sock_T fd;
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003896 chanpart_T *ch_part = &channel->ch_part[part];
3897 int did_use_queue = FALSE;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003898
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003899 fd = ch_part->ch_fd;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01003900 if (fd == INVALID_FD)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003901 {
3902 if (!channel->ch_error && fun != NULL)
3903 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02003904 ch_error(channel, "%s(): write while not connected", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003905 semsg(_("E630: %s(): write while not connected"), fun);
Bram Moolenaard04a0202016-01-26 23:30:18 +01003906 }
3907 channel->ch_error = TRUE;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003908 return FAIL;
Bram Moolenaard04a0202016-01-26 23:30:18 +01003909 }
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003910
Bram Moolenaar0b146882018-09-06 16:27:24 +02003911 if (channel->ch_nonblock && !ch_part->ch_nonblocking)
3912 channel_set_nonblock(channel, part);
3913
Bram Moolenaar71eeb742017-09-13 22:18:01 +02003914 if (ch_log_active())
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003915 {
Bram Moolenaar4b16ee72018-08-09 22:15:34 +02003916 ch_log_lead("SEND ", channel, part);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003917 fprintf(log_fd, "'");
Bram Moolenaar42335f52018-09-13 15:33:43 +02003918 vim_ignored = (int)fwrite(buf_arg, len_arg, 1, log_fd);
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003919 fprintf(log_fd, "'\n");
3920 fflush(log_fd);
Bram Moolenaard0b65022016-03-06 21:50:33 +01003921 did_log_msg = TRUE;
Bram Moolenaar6463ca22016-02-13 17:04:46 +01003922 }
3923
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003924 for (;;)
Bram Moolenaard04a0202016-01-26 23:30:18 +01003925 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003926 writeq_T *wq = &ch_part->ch_writeque;
3927 char_u *buf;
3928 int len;
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01003929
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003930 if (wq->wq_next != NULL)
3931 {
3932 /* first write what was queued */
3933 buf = wq->wq_next->wq_ga.ga_data;
3934 len = wq->wq_next->wq_ga.ga_len;
3935 did_use_queue = TRUE;
3936 }
3937 else
3938 {
3939 if (len_arg == 0)
3940 /* nothing to write, called from channel_select_check() */
3941 return OK;
3942 buf = buf_arg;
3943 len = len_arg;
3944 }
3945
3946 if (part == PART_SOCK)
3947 res = sock_write(fd, (char *)buf, len);
3948 else
Bram Moolenaar31faed62019-01-22 23:01:40 +01003949 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003950 res = fd_write(fd, (char *)buf, len);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003951#ifdef MSWIN
Bram Moolenaar31faed62019-01-22 23:01:40 +01003952 if (channel->ch_named_pipe && res < 0)
3953 {
3954 DisconnectNamedPipe((HANDLE)fd);
3955 ConnectNamedPipe((HANDLE)fd, NULL);
3956 }
3957#endif
3958 }
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003959 if (res < 0 && (errno == EWOULDBLOCK
3960#ifdef EAGAIN
3961 || errno == EAGAIN
3962#endif
3963 ))
3964 res = 0; /* nothing got written */
3965
3966 if (res >= 0 && ch_part->ch_nonblocking)
3967 {
3968 writeq_T *entry = wq->wq_next;
3969
3970 if (did_use_queue)
3971 ch_log(channel, "Sent %d bytes now", res);
3972 if (res == len)
3973 {
3974 /* Wrote all the buf[len] bytes. */
3975 if (entry != NULL)
3976 {
3977 /* Remove the entry from the write queue. */
Bram Moolenaaraba680a2017-09-09 16:42:53 +02003978 remove_from_writeque(wq, entry);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02003979 continue;
3980 }
3981 if (did_use_queue)
3982 ch_log(channel, "Write queue empty");
3983 }
3984 else
3985 {
3986 /* Wrote only buf[res] bytes, can't write more now. */
3987 if (entry != NULL)
3988 {
3989 if (res > 0)
3990 {
3991 /* Remove the bytes that were written. */
3992 mch_memmove(entry->wq_ga.ga_data,
3993 (char *)entry->wq_ga.ga_data + res,
3994 len - res);
3995 entry->wq_ga.ga_len -= res;
3996 }
3997 buf = buf_arg;
3998 len = len_arg;
3999 }
4000 else
4001 {
4002 buf += res;
4003 len -= res;
4004 }
4005 ch_log(channel, "Adding %d bytes to the write queue", len);
4006
4007 /* Append the not written bytes of the argument to the write
4008 * buffer. Limit entries to 4000 bytes. */
4009 if (wq->wq_prev != NULL
4010 && wq->wq_prev->wq_ga.ga_len + len < 4000)
4011 {
4012 writeq_T *last = wq->wq_prev;
4013
4014 /* append to the last entry */
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((char *)last->wq_ga.ga_data
4018 + last->wq_ga.ga_len,
4019 buf, len);
4020 last->wq_ga.ga_len += len;
4021 }
4022 }
4023 else
4024 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004025 writeq_T *last = ALLOC_ONE(writeq_T);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004026
4027 if (last != NULL)
4028 {
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004029 last->wq_prev = wq->wq_prev;
4030 last->wq_next = NULL;
4031 if (wq->wq_prev == NULL)
4032 wq->wq_next = last;
4033 else
4034 wq->wq_prev->wq_next = last;
4035 wq->wq_prev = last;
4036 ga_init2(&last->wq_ga, 1, 1000);
Bram Moolenaar0d071552019-07-08 22:04:03 +02004037 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004038 {
4039 mch_memmove(last->wq_ga.ga_data, buf, len);
4040 last->wq_ga.ga_len = len;
4041 }
4042 }
4043 }
4044 }
4045 }
4046 else if (res != len)
4047 {
4048 if (!channel->ch_error && fun != NULL)
4049 {
4050 ch_error(channel, "%s(): write failed", fun);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004051 semsg(_("E631: %s(): write failed"), fun);
Bram Moolenaar97bd5e62017-08-18 20:50:30 +02004052 }
4053 channel->ch_error = TRUE;
4054 return FAIL;
4055 }
4056
4057 channel->ch_error = FALSE;
4058 return OK;
4059 }
Bram Moolenaard04a0202016-01-26 23:30:18 +01004060}
4061
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004062/*
4063 * Common for "ch_sendexpr()" and "ch_sendraw()".
4064 * Returns the channel if the caller should read the response.
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02004065 * Sets "part_read" to the read fd.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004066 * Otherwise returns NULL.
4067 */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004068 static channel_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004069send_common(
4070 typval_T *argvars,
4071 char_u *text,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004072 int len,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004073 int id,
4074 int eval,
4075 jobopt_T *opt,
4076 char *fun,
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004077 ch_part_T *part_read)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004078{
4079 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004080 ch_part_T part_send;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004081
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004082 clear_job_options(opt);
Bram Moolenaar437905c2016-04-26 19:01:05 +02004083 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004084 if (channel == NULL)
4085 return NULL;
4086 part_send = channel_part_send(channel);
4087 *part_read = channel_part_read(channel);
4088
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004089 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004090 return NULL;
4091
4092 /* Set the callback. An empty callback means no callback and not reading
4093 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4094 * allowed. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004095 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004096 {
4097 if (eval)
4098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004099 semsg(_("E917: Cannot use a callback with %s()"), fun);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004100 return NULL;
4101 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004102 channel_set_req_callback(channel, *part_read, &opt->jo_callback, id);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004103 }
4104
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004105 if (channel_send(channel, part_send, text, len, fun) == OK
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004106 && opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004107 return channel;
4108 return NULL;
4109}
4110
4111/*
4112 * common for "ch_evalexpr()" and "ch_sendexpr()"
4113 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004114 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004115ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
4116{
4117 char_u *text;
4118 typval_T *listtv;
4119 channel_T *channel;
4120 int id;
4121 ch_mode_T ch_mode;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004122 ch_part_T part_send;
4123 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004124 jobopt_T opt;
4125 int timeout;
4126
4127 /* return an empty string by default */
4128 rettv->v_type = VAR_STRING;
4129 rettv->vval.v_string = NULL;
4130
Bram Moolenaar437905c2016-04-26 19:01:05 +02004131 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004132 if (channel == NULL)
4133 return;
4134 part_send = channel_part_send(channel);
4135
4136 ch_mode = channel_get_mode(channel, part_send);
4137 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
4138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004139 emsg(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004140 return;
4141 }
4142
Bram Moolenaare9d6a292016-03-20 19:31:33 +01004143 id = ++channel->ch_last_msg_id;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004144 text = json_encode_nr_expr(id, &argvars[1],
Bram Moolenaarf1f07922016-08-26 17:58:53 +02004145 (ch_mode == MODE_JS ? JSON_JS : 0) | JSON_NL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004146 if (text == NULL)
4147 return;
4148
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004149 channel = send_common(argvars, text, (int)STRLEN(text), id, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004150 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
4151 vim_free(text);
4152 if (channel != NULL && eval)
4153 {
4154 if (opt.jo_set & JO_TIMEOUT)
4155 timeout = opt.jo_timeout;
4156 else
4157 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004158 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4159 == OK)
4160 {
4161 list_T *list = listtv->vval.v_list;
4162
4163 /* Move the item from the list and then change the type to
4164 * avoid the value being freed. */
4165 *rettv = list->lv_last->li_tv;
4166 list->lv_last->li_tv.v_type = VAR_NUMBER;
4167 free_tv(listtv);
4168 }
4169 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004170 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004171}
4172
4173/*
4174 * common for "ch_evalraw()" and "ch_sendraw()"
4175 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004176 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004177ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
4178{
4179 char_u buf[NUMBUFLEN];
4180 char_u *text;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004181 int len;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004182 channel_T *channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004183 ch_part_T part_read;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004184 jobopt_T opt;
4185 int timeout;
4186
4187 /* return an empty string by default */
4188 rettv->v_type = VAR_STRING;
4189 rettv->vval.v_string = NULL;
4190
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004191 if (argvars[1].v_type == VAR_BLOB)
4192 {
4193 text = argvars[1].vval.v_blob->bv_ga.ga_data;
4194 len = argvars[1].vval.v_blob->bv_ga.ga_len;
4195 }
4196 else
4197 {
4198 text = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaare4074252019-01-17 14:31:14 +01004199 len = (int)STRLEN(text);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004200 }
4201 channel = send_common(argvars, text, len, 0, eval, &opt,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004202 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
4203 if (channel != NULL && eval)
4204 {
4205 if (opt.jo_set & JO_TIMEOUT)
4206 timeout = opt.jo_timeout;
4207 else
4208 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar620ca2d2017-12-09 19:13:13 +01004209 rettv->vval.v_string = channel_read_block(channel, part_read,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004210 timeout, TRUE, NULL);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004211 }
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004212 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004213}
4214
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004215#define KEEP_OPEN_TIME 20 /* msec */
Bram Moolenaarf3360612017-10-01 16:21:31 +02004216
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004217#if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004218/*
4219 * Add open channels to the poll struct.
4220 * Return the adjusted struct index.
4221 * The type of "fds" is hidden to avoid problems with the function proto.
4222 */
4223 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004224channel_poll_setup(int nfd_in, void *fds_in, int *towait)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004225{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004226 int nfd = nfd_in;
4227 channel_T *channel;
4228 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004229 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004230
Bram Moolenaar77073442016-02-13 23:23:53 +01004231 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004232 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004233 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004234 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004235 chanpart_T *ch_part = &channel->ch_part[part];
4236
4237 if (ch_part->ch_fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004238 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004239 if (channel->ch_keep_open)
4240 {
4241 /* For unknown reason poll() returns immediately for a
4242 * keep-open channel. Instead of adding it to the fds add
4243 * a short timeout and check, like polling. */
4244 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4245 *towait = KEEP_OPEN_TIME;
4246 }
4247 else
4248 {
4249 ch_part->ch_poll_idx = nfd;
4250 fds[nfd].fd = ch_part->ch_fd;
4251 fds[nfd].events = POLLIN;
4252 nfd++;
4253 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004254 }
4255 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004256 channel->ch_part[part].ch_poll_idx = -1;
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004257 }
4258 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004259
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004260 nfd = channel_fill_poll_write(nfd, fds);
4261
Bram Moolenaare0874f82016-01-24 20:36:41 +01004262 return nfd;
4263}
4264
4265/*
4266 * The type of "fds" is hidden to avoid problems with the function proto.
4267 */
4268 int
4269channel_poll_check(int ret_in, void *fds_in)
4270{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004271 int ret = ret_in;
4272 channel_T *channel;
4273 struct pollfd *fds = fds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004274 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004275 int idx;
4276 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004277
Bram Moolenaar77073442016-02-13 23:23:53 +01004278 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004279 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004280 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004281 {
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004282 idx = channel->ch_part[part].ch_poll_idx;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004283
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004284 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLIN))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004285 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004286 channel_read(channel, part, "channel_poll_check");
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004287 --ret;
4288 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004289 else if (channel->ch_part[part].ch_fd != INVALID_FD
4290 && channel->ch_keep_open)
4291 {
4292 /* polling a keep-open channel */
4293 channel_read(channel, part, "channel_poll_check_keep_open");
4294 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004295 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004296
4297 in_part = &channel->ch_part[PART_IN];
4298 idx = in_part->ch_poll_idx;
4299 if (ret > 0 && idx != -1 && (fds[idx].revents & POLLOUT))
4300 {
Bram Moolenaar683b7962017-08-19 15:51:59 +02004301 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004302 --ret;
4303 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004304 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004305
4306 return ret;
4307}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004308#endif /* UNIX && !HAVE_SELECT */
Bram Moolenaare0874f82016-01-24 20:36:41 +01004309
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004310#if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
Bram Moolenaarf3360612017-10-01 16:21:31 +02004311
Bram Moolenaare0874f82016-01-24 20:36:41 +01004312/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004313 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004314 */
4315 int
Bram Moolenaarf3360612017-10-01 16:21:31 +02004316channel_select_setup(
4317 int maxfd_in,
4318 void *rfds_in,
4319 void *wfds_in,
4320 struct timeval *tv,
4321 struct timeval **tvp)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004322{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004323 int maxfd = maxfd_in;
4324 channel_T *channel;
4325 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004326 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004327 ch_part_T part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004328
Bram Moolenaar77073442016-02-13 23:23:53 +01004329 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004330 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004331 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004332 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004333 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004334
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004335 if (fd != INVALID_FD)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004336 {
Bram Moolenaarf3360612017-10-01 16:21:31 +02004337 if (channel->ch_keep_open)
4338 {
4339 /* For unknown reason select() returns immediately for a
4340 * keep-open channel. Instead of adding it to the rfds add
4341 * a short timeout and check, like polling. */
4342 if (*tvp == NULL || tv->tv_sec > 0
4343 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4344 {
4345 *tvp = tv;
4346 tv->tv_sec = 0;
4347 tv->tv_usec = KEEP_OPEN_TIME * 1000;
4348 }
4349 }
4350 else
4351 {
4352 FD_SET((int)fd, rfds);
4353 if (maxfd < (int)fd)
4354 maxfd = (int)fd;
4355 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004356 }
4357 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004358 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004359
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004360 maxfd = channel_fill_wfds(maxfd, wfds);
4361
Bram Moolenaare0874f82016-01-24 20:36:41 +01004362 return maxfd;
4363}
4364
4365/*
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004366 * The "fd_set" type is hidden to avoid problems with the function proto.
Bram Moolenaare0874f82016-01-24 20:36:41 +01004367 */
4368 int
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004369channel_select_check(int ret_in, void *rfds_in, void *wfds_in)
Bram Moolenaare0874f82016-01-24 20:36:41 +01004370{
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004371 int ret = ret_in;
4372 channel_T *channel;
4373 fd_set *rfds = rfds_in;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004374 fd_set *wfds = wfds_in;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004375 ch_part_T part;
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004376 chanpart_T *in_part;
Bram Moolenaare0874f82016-01-24 20:36:41 +01004377
Bram Moolenaar77073442016-02-13 23:23:53 +01004378 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004379 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004380 for (part = PART_SOCK; part < PART_IN; ++part)
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004381 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004382 sock_T fd = channel->ch_part[part].ch_fd;
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004383
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004384 if (ret > 0 && fd != INVALID_FD && FD_ISSET(fd, rfds))
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004385 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004386 channel_read(channel, part, "channel_select_check");
Bram Moolenaar3c518402017-09-08 20:47:00 +02004387 FD_CLR(fd, rfds);
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004388 --ret;
4389 }
Bram Moolenaarf3360612017-10-01 16:21:31 +02004390 else if (fd != INVALID_FD && channel->ch_keep_open)
4391 {
4392 /* polling a keep-open channel */
4393 channel_read(channel, part, "channel_select_check_keep_open");
4394 }
Bram Moolenaar7b3ca762016-02-14 19:13:43 +01004395 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004396
4397 in_part = &channel->ch_part[PART_IN];
4398 if (ret > 0 && in_part->ch_fd != INVALID_FD
4399 && FD_ISSET(in_part->ch_fd, wfds))
4400 {
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004401 // Clear the flag first, ch_fd may change in channel_write_input().
Bram Moolenaar3c518402017-09-08 20:47:00 +02004402 FD_CLR(in_part->ch_fd, wfds);
Bram Moolenaar9af97782018-04-03 12:51:01 +02004403 channel_write_input(channel);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02004404 --ret;
4405 }
Bram Moolenaar16eb4f82016-02-14 23:02:34 +01004406 }
Bram Moolenaare0874f82016-01-24 20:36:41 +01004407
4408 return ret;
4409}
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004410#endif // !MSWIN && HAVE_SELECT
Bram Moolenaare0874f82016-01-24 20:36:41 +01004411
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004412/*
Bram Moolenaard7ece102016-02-02 23:23:02 +01004413 * Execute queued up commands.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02004414 * Invoked from the main loop when it's safe to execute received commands,
4415 * and during a blocking wait for ch_evalexpr().
Bram Moolenaard7ece102016-02-02 23:23:02 +01004416 * Return TRUE when something was done.
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004417 */
Bram Moolenaard7ece102016-02-02 23:23:02 +01004418 int
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004419channel_parse_messages(void)
4420{
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004421 channel_T *channel = first_channel;
4422 int ret = FALSE;
4423 int r;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004424 ch_part_T part = PART_SOCK;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004425#ifdef ELAPSED_FUNC
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01004426 elapsed_T start_tv;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004427
4428 ELAPSED_INIT(start_tv);
4429#endif
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004430
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004431 ++safe_to_invoke_callback;
4432
Bram Moolenaard0b65022016-03-06 21:50:33 +01004433 /* Only do this message when another message was given, otherwise we get
4434 * lots of them. */
4435 if (did_log_msg)
4436 {
4437 ch_log(NULL, "looking for messages on channels");
4438 did_log_msg = FALSE;
4439 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004440 while (channel != NULL)
4441 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004442 if (channel_can_close(channel))
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004443 {
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02004444 channel->ch_to_be_closed = (1U << PART_COUNT);
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004445 channel_close_now(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004446 // channel may have been freed, start over
Bram Moolenaarcf7ff702016-05-09 17:20:14 +02004447 channel = first_channel;
4448 continue;
4449 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01004450 if (channel->ch_to_be_freed || channel->ch_killing)
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004451 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004452 if (channel->ch_killing)
4453 {
4454 channel_free_contents(channel);
4455 channel->ch_job->jv_channel = NULL;
4456 }
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004457 channel_free(channel);
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004458 // channel has been freed, start over
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004459 channel = first_channel;
4460 continue;
4461 }
Bram Moolenaar46c85432016-02-26 11:17:46 +01004462 if (channel->ch_refcount == 0 && !channel_still_useful(channel))
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004463 {
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01004464 // channel is no longer useful, free it
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01004465 channel_free(channel);
4466 channel = first_channel;
4467 part = PART_SOCK;
4468 continue;
4469 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004470 if (channel->ch_part[part].ch_fd != INVALID_FD
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004471 || channel_has_readahead(channel, part))
Bram Moolenaard7ece102016-02-02 23:23:02 +01004472 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004473 /* Increase the refcount, in case the handler causes the channel
4474 * to be unreferenced or closed. */
4475 ++channel->ch_refcount;
4476 r = may_invoke_callback(channel, part);
4477 if (r == OK)
4478 ret = TRUE;
Bram Moolenaar833eb1d2016-11-24 17:22:50 +01004479 if (channel_unref(channel) || (r == OK
4480#ifdef ELAPSED_FUNC
4481 /* Limit the time we loop here to 100 msec, otherwise
4482 * Vim becomes unresponsive when the callback takes
4483 * more than a bit of time. */
4484 && ELAPSED_FUNC(start_tv) < 100L
4485#endif
4486 ))
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004487 {
4488 /* channel was freed or something was done, start over */
4489 channel = first_channel;
4490 part = PART_SOCK;
4491 continue;
4492 }
Bram Moolenaard7ece102016-02-02 23:23:02 +01004493 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004494 if (part < PART_ERR)
4495 ++part;
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004496 else
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004497 {
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004498 channel = channel->ch_next;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004499 part = PART_SOCK;
4500 }
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01004501 }
Bram Moolenaar187db502016-02-27 14:44:26 +01004502
Bram Moolenaar7f7c3322016-04-18 19:27:24 +02004503 if (channel_need_redraw)
Bram Moolenaar187db502016-02-27 14:44:26 +01004504 {
4505 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02004506 redraw_after_callback(TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +01004507 }
4508
Bram Moolenaarfb6ffc72016-05-09 17:58:04 +02004509 --safe_to_invoke_callback;
4510
Bram Moolenaard7ece102016-02-02 23:23:02 +01004511 return ret;
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01004512}
4513
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004514/*
Bram Moolenaar8a8199e2016-11-26 15:13:33 +01004515 * Return TRUE if any channel has readahead. That means we should not block on
4516 * waiting for input.
4517 */
4518 int
4519channel_any_readahead(void)
4520{
4521 channel_T *channel = first_channel;
4522 ch_part_T part = PART_SOCK;
4523
4524 while (channel != NULL)
4525 {
4526 if (channel_has_readahead(channel, part))
4527 return TRUE;
4528 if (part < PART_ERR)
4529 ++part;
4530 else
4531 {
4532 channel = channel->ch_next;
4533 part = PART_SOCK;
4534 }
4535 }
4536 return FALSE;
4537}
4538
4539/*
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +01004540 * Mark references to lists used in channels.
4541 */
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004542 int
4543set_ref_in_channel(int copyID)
4544{
Bram Moolenaar77073442016-02-13 23:23:53 +01004545 int abort = FALSE;
4546 channel_T *channel;
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004547 typval_T tv;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004548
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004549 for (channel = first_channel; !abort && channel != NULL;
4550 channel = channel->ch_next)
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004551 if (channel_still_useful(channel))
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004552 {
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004553 tv.v_type = VAR_CHANNEL;
4554 tv.vval.v_channel = channel;
4555 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004556 }
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004557 return abort;
4558}
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004559
4560/*
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004561 * Return the "part" to write to for "channel".
4562 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004563 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004564channel_part_send(channel_T *channel)
4565{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004566 if (channel->CH_SOCK_FD == INVALID_FD)
4567 return PART_IN;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004568 return PART_SOCK;
4569}
4570
4571/*
4572 * Return the default "part" to read from for "channel".
4573 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004574 static ch_part_T
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004575channel_part_read(channel_T *channel)
4576{
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004577 if (channel->CH_SOCK_FD == INVALID_FD)
4578 return PART_OUT;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004579 return PART_SOCK;
4580}
4581
4582/*
4583 * Return the mode of "channel"/"part"
Bram Moolenaar77073442016-02-13 23:23:53 +01004584 * If "channel" is invalid returns MODE_JSON.
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004585 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004586 static ch_mode_T
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004587channel_get_mode(channel_T *channel, ch_part_T part)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004588{
Bram Moolenaar77073442016-02-13 23:23:53 +01004589 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004590 return MODE_JSON;
Bram Moolenaar42d38a22016-02-20 18:18:59 +01004591 return channel->ch_part[part].ch_mode;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +01004592}
4593
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004594/*
4595 * Return the timeout of "channel"/"part"
4596 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004597 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004598channel_get_timeout(channel_T *channel, ch_part_T part)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01004599{
4600 return channel->ch_part[part].ch_timeout;
4601}
4602
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004603 static int
4604handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
4605{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004606 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004607
4608 opt->jo_set |= jo;
4609 if (STRCMP(val, "nl") == 0)
4610 *modep = MODE_NL;
4611 else if (STRCMP(val, "raw") == 0)
4612 *modep = MODE_RAW;
4613 else if (STRCMP(val, "js") == 0)
4614 *modep = MODE_JS;
4615 else if (STRCMP(val, "json") == 0)
4616 *modep = MODE_JSON;
4617 else
4618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004619 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004620 return FAIL;
4621 }
4622 return OK;
4623}
4624
4625 static int
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004626handle_io(typval_T *item, ch_part_T part, jobopt_T *opt)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004627{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004628 char_u *val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004629
4630 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
4631 if (STRCMP(val, "null") == 0)
4632 opt->jo_io[part] = JIO_NULL;
4633 else if (STRCMP(val, "pipe") == 0)
4634 opt->jo_io[part] = JIO_PIPE;
4635 else if (STRCMP(val, "file") == 0)
4636 opt->jo_io[part] = JIO_FILE;
4637 else if (STRCMP(val, "buffer") == 0)
4638 opt->jo_io[part] = JIO_BUFFER;
4639 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
4640 opt->jo_io[part] = JIO_OUT;
4641 else
4642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004643 semsg(_(e_invarg2), val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004644 return FAIL;
4645 }
4646 return OK;
4647}
4648
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004649/*
4650 * Clear a jobopt_T before using it.
4651 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004652 void
4653clear_job_options(jobopt_T *opt)
4654{
4655 vim_memset(opt, 0, sizeof(jobopt_T));
4656}
4657
4658/*
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004659 * Free any members of a jobopt_T.
4660 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004661 static void
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004662free_job_options(jobopt_T *opt)
4663{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004664 if (opt->jo_callback.cb_partial != NULL)
4665 partial_unref(opt->jo_callback.cb_partial);
4666 else if (opt->jo_callback.cb_name != NULL)
4667 func_unref(opt->jo_callback.cb_name);
4668 if (opt->jo_out_cb.cb_partial != NULL)
4669 partial_unref(opt->jo_out_cb.cb_partial);
4670 else if (opt->jo_out_cb.cb_name != NULL)
4671 func_unref(opt->jo_out_cb.cb_name);
4672 if (opt->jo_err_cb.cb_partial != NULL)
4673 partial_unref(opt->jo_err_cb.cb_partial);
4674 else if (opt->jo_err_cb.cb_name != NULL)
4675 func_unref(opt->jo_err_cb.cb_name);
4676 if (opt->jo_close_cb.cb_partial != NULL)
4677 partial_unref(opt->jo_close_cb.cb_partial);
4678 else if (opt->jo_close_cb.cb_name != NULL)
4679 func_unref(opt->jo_close_cb.cb_name);
4680 if (opt->jo_exit_cb.cb_partial != NULL)
4681 partial_unref(opt->jo_exit_cb.cb_partial);
4682 else if (opt->jo_exit_cb.cb_name != NULL)
4683 func_unref(opt->jo_exit_cb.cb_name);
Bram Moolenaar05aafed2017-08-11 19:12:11 +02004684 if (opt->jo_env != NULL)
4685 dict_unref(opt->jo_env);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02004686}
4687
4688/*
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004689 * Get the PART_ number from the first character of an option name.
4690 */
4691 static int
4692part_from_char(int c)
4693{
4694 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
4695}
4696
4697/*
4698 * Get the option entries from the dict in "tv", parse them and put the result
4699 * in "opt".
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004700 * Only accept JO_ options in "supported" and JO2_ options in "supported2".
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004701 * If an option value is invalid return FAIL.
4702 */
4703 int
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004704get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004705{
4706 typval_T *item;
4707 char_u *val;
4708 dict_T *dict;
4709 int todo;
4710 hashitem_T *hi;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004711 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004712
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004713 if (tv->v_type == VAR_UNKNOWN)
4714 return OK;
4715 if (tv->v_type != VAR_DICT)
4716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004717 emsg(_(e_dictreq));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004718 return FAIL;
4719 }
4720 dict = tv->vval.v_dict;
4721 if (dict == NULL)
4722 return OK;
4723
4724 todo = (int)dict->dv_hashtab.ht_used;
4725 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
4726 if (!HASHITEM_EMPTY(hi))
4727 {
4728 item = &dict_lookup(hi)->di_tv;
4729
4730 if (STRCMP(hi->hi_key, "mode") == 0)
4731 {
4732 if (!(supported & JO_MODE))
4733 break;
4734 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
4735 return FAIL;
4736 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004737 else if (STRCMP(hi->hi_key, "in_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004738 {
4739 if (!(supported & JO_IN_MODE))
4740 break;
4741 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
4742 == FAIL)
4743 return FAIL;
4744 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004745 else if (STRCMP(hi->hi_key, "out_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004746 {
4747 if (!(supported & JO_OUT_MODE))
4748 break;
4749 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
4750 == FAIL)
4751 return FAIL;
4752 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004753 else if (STRCMP(hi->hi_key, "err_mode") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004754 {
4755 if (!(supported & JO_ERR_MODE))
4756 break;
4757 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
4758 == FAIL)
4759 return FAIL;
4760 }
Bram Moolenaar0b146882018-09-06 16:27:24 +02004761 else if (STRCMP(hi->hi_key, "noblock") == 0)
4762 {
4763 if (!(supported & JO_MODE))
4764 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004765 opt->jo_noblock = tv_get_number(item);
Bram Moolenaar0b146882018-09-06 16:27:24 +02004766 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004767 else if (STRCMP(hi->hi_key, "in_io") == 0
4768 || STRCMP(hi->hi_key, "out_io") == 0
4769 || STRCMP(hi->hi_key, "err_io") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004770 {
4771 if (!(supported & JO_OUT_IO))
4772 break;
4773 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
4774 return FAIL;
4775 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004776 else if (STRCMP(hi->hi_key, "in_name") == 0
4777 || STRCMP(hi->hi_key, "out_name") == 0
4778 || STRCMP(hi->hi_key, "err_name") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004779 {
4780 part = part_from_char(*hi->hi_key);
4781
4782 if (!(supported & JO_OUT_IO))
4783 break;
4784 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
4785 opt->jo_io_name[part] =
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004786 tv_get_string_buf_chk(item, opt->jo_io_name_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004787 }
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004788 else if (STRCMP(hi->hi_key, "pty") == 0)
4789 {
4790 if (!(supported & JO_MODE))
4791 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 opt->jo_pty = tv_get_number(item);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02004793 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004794 else if (STRCMP(hi->hi_key, "in_buf") == 0
4795 || STRCMP(hi->hi_key, "out_buf") == 0
4796 || STRCMP(hi->hi_key, "err_buf") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004797 {
4798 part = part_from_char(*hi->hi_key);
4799
4800 if (!(supported & JO_OUT_IO))
4801 break;
4802 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004803 opt->jo_io_buf[part] = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004804 if (opt->jo_io_buf[part] <= 0)
4805 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004806 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004807 return FAIL;
4808 }
4809 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
4810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004811 semsg(_(e_nobufnr), (long)opt->jo_io_buf[part]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004812 return FAIL;
4813 }
4814 }
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004815 else if (STRCMP(hi->hi_key, "out_modifiable") == 0
4816 || STRCMP(hi->hi_key, "err_modifiable") == 0)
4817 {
4818 part = part_from_char(*hi->hi_key);
4819
4820 if (!(supported & JO_OUT_IO))
4821 break;
4822 opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004823 opt->jo_modifiable[part] = tv_get_number(item);
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02004824 }
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004825 else if (STRCMP(hi->hi_key, "out_msg") == 0
4826 || STRCMP(hi->hi_key, "err_msg") == 0)
4827 {
4828 part = part_from_char(*hi->hi_key);
4829
4830 if (!(supported & JO_OUT_IO))
4831 break;
4832 opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004833 opt->jo_message[part] = tv_get_number(item);
Bram Moolenaar169ebb02016-09-07 23:32:23 +02004834 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004835 else if (STRCMP(hi->hi_key, "in_top") == 0
4836 || STRCMP(hi->hi_key, "in_bot") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004837 {
4838 linenr_T *lp;
4839
4840 if (!(supported & JO_OUT_IO))
4841 break;
4842 if (hi->hi_key[3] == 't')
4843 {
4844 lp = &opt->jo_in_top;
4845 opt->jo_set |= JO_IN_TOP;
4846 }
4847 else
4848 {
4849 lp = &opt->jo_in_bot;
4850 opt->jo_set |= JO_IN_BOT;
4851 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004852 *lp = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004853 if (*lp < 0)
4854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004855 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004856 return FAIL;
4857 }
4858 }
4859 else if (STRCMP(hi->hi_key, "channel") == 0)
4860 {
4861 if (!(supported & JO_OUT_IO))
4862 break;
4863 opt->jo_set |= JO_CHANNEL;
4864 if (item->v_type != VAR_CHANNEL)
4865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004866 semsg(_(e_invargval), "channel");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004867 return FAIL;
4868 }
4869 opt->jo_channel = item->vval.v_channel;
4870 }
4871 else if (STRCMP(hi->hi_key, "callback") == 0)
4872 {
4873 if (!(supported & JO_CALLBACK))
4874 break;
4875 opt->jo_set |= JO_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004876 opt->jo_callback = get_callback(item);
4877 if (opt->jo_callback.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004879 semsg(_(e_invargval), "callback");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004880 return FAIL;
4881 }
4882 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004883 else if (STRCMP(hi->hi_key, "out_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004884 {
4885 if (!(supported & JO_OUT_CALLBACK))
4886 break;
4887 opt->jo_set |= JO_OUT_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004888 opt->jo_out_cb = get_callback(item);
4889 if (opt->jo_out_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004891 semsg(_(e_invargval), "out_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004892 return FAIL;
4893 }
4894 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004895 else if (STRCMP(hi->hi_key, "err_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004896 {
4897 if (!(supported & JO_ERR_CALLBACK))
4898 break;
4899 opt->jo_set |= JO_ERR_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004900 opt->jo_err_cb = get_callback(item);
4901 if (opt->jo_err_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004903 semsg(_(e_invargval), "err_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004904 return FAIL;
4905 }
4906 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01004907 else if (STRCMP(hi->hi_key, "close_cb") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004908 {
4909 if (!(supported & JO_CLOSE_CALLBACK))
4910 break;
4911 opt->jo_set |= JO_CLOSE_CALLBACK;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004912 opt->jo_close_cb = get_callback(item);
4913 if (opt->jo_close_cb.cb_name == NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004915 semsg(_(e_invargval), "close_cb");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01004916 return FAIL;
4917 }
4918 }
Bram Moolenaar958dc692016-12-01 15:34:12 +01004919 else if (STRCMP(hi->hi_key, "drop") == 0)
4920 {
4921 int never = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004922 val = tv_get_string(item);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004923
4924 if (STRCMP(val, "never") == 0)
4925 never = TRUE;
4926 else if (STRCMP(val, "auto") != 0)
4927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004928 semsg(_(e_invargNval), "drop", val);
Bram Moolenaar958dc692016-12-01 15:34:12 +01004929 return FAIL;
4930 }
4931 opt->jo_drop_never = never;
4932 }
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004933 else if (STRCMP(hi->hi_key, "exit_cb") == 0)
4934 {
4935 if (!(supported & JO_EXIT_CB))
4936 break;
4937 opt->jo_set |= JO_EXIT_CB;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004938 opt->jo_exit_cb = get_callback(item);
4939 if (opt->jo_exit_cb.cb_name == NULL)
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004941 semsg(_(e_invargval), "exit_cb");
Bram Moolenaaref3abc62016-05-29 16:44:26 +02004942 return FAIL;
4943 }
4944 }
Bram Moolenaar8456ea82017-08-05 15:02:05 +02004945#ifdef FEAT_TERMINAL
Bram Moolenaar78712a72017-08-05 14:50:12 +02004946 else if (STRCMP(hi->hi_key, "term_name") == 0)
4947 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004948 if (!(supported2 & JO2_TERM_NAME))
Bram Moolenaar78712a72017-08-05 14:50:12 +02004949 break;
4950 opt->jo_set2 |= JO2_TERM_NAME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004951 opt->jo_term_name = tv_get_string_chk(item);
Bram Moolenaar78712a72017-08-05 14:50:12 +02004952 if (opt->jo_term_name == NULL)
4953 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004954 semsg(_(e_invargval), "term_name");
Bram Moolenaar78712a72017-08-05 14:50:12 +02004955 return FAIL;
4956 }
4957 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004958 else if (STRCMP(hi->hi_key, "term_finish") == 0)
4959 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02004960 if (!(supported2 & JO2_TERM_FINISH))
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004961 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 val = tv_get_string(item);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004963 if (STRCMP(val, "open") != 0 && STRCMP(val, "close") != 0)
4964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004965 semsg(_(e_invargNval), "term_finish", val);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02004966 return FAIL;
4967 }
4968 opt->jo_set2 |= JO2_TERM_FINISH;
4969 opt->jo_term_finish = *val;
4970 }
Bram Moolenaar37c45832017-08-12 16:01:04 +02004971 else if (STRCMP(hi->hi_key, "term_opencmd") == 0)
4972 {
4973 char_u *p;
4974
4975 if (!(supported2 & JO2_TERM_OPENCMD))
4976 break;
4977 opt->jo_set2 |= JO2_TERM_OPENCMD;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004978 p = opt->jo_term_opencmd = tv_get_string_chk(item);
Bram Moolenaar37c45832017-08-12 16:01:04 +02004979 if (p != NULL)
4980 {
4981 /* Must have %d and no other %. */
4982 p = vim_strchr(p, '%');
4983 if (p != NULL && (p[1] != 'd'
4984 || vim_strchr(p + 2, '%') != NULL))
4985 p = NULL;
4986 }
4987 if (p == NULL)
4988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004989 semsg(_(e_invargval), "term_opencmd");
Bram Moolenaar37c45832017-08-12 16:01:04 +02004990 return FAIL;
4991 }
4992 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004993 else if (STRCMP(hi->hi_key, "eof_chars") == 0)
4994 {
Bram Moolenaar3346cc42017-09-02 14:54:21 +02004995 char_u *p;
4996
4997 if (!(supported2 & JO2_EOF_CHARS))
4998 break;
4999 opt->jo_set2 |= JO2_EOF_CHARS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005000 p = opt->jo_eof_chars = tv_get_string_chk(item);
Bram Moolenaar3346cc42017-09-02 14:54:21 +02005001 if (p == NULL)
5002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005003 semsg(_(e_invargval), "eof_chars");
Bram Moolenaar3346cc42017-09-02 14:54:21 +02005004 return FAIL;
5005 }
Bram Moolenaar3346cc42017-09-02 14:54:21 +02005006 }
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005007 else if (STRCMP(hi->hi_key, "term_rows") == 0)
5008 {
5009 if (!(supported2 & JO2_TERM_ROWS))
5010 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005011 opt->jo_set2 |= JO2_TERM_ROWS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005012 opt->jo_term_rows = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005013 }
5014 else if (STRCMP(hi->hi_key, "term_cols") == 0)
5015 {
5016 if (!(supported2 & JO2_TERM_COLS))
5017 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005018 opt->jo_set2 |= JO2_TERM_COLS;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005019 opt->jo_term_cols = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005020 }
5021 else if (STRCMP(hi->hi_key, "vertical") == 0)
5022 {
5023 if (!(supported2 & JO2_VERTICAL))
5024 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005025 opt->jo_set2 |= JO2_VERTICAL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005026 opt->jo_vertical = tv_get_number(item);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005027 }
Bram Moolenaarda43b612017-08-11 22:27:50 +02005028 else if (STRCMP(hi->hi_key, "curwin") == 0)
5029 {
5030 if (!(supported2 & JO2_CURWIN))
5031 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005032 opt->jo_set2 |= JO2_CURWIN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005033 opt->jo_curwin = tv_get_number(item);
Bram Moolenaarda43b612017-08-11 22:27:50 +02005034 }
Bram Moolenaar87abab92019-06-03 21:14:59 +02005035 else if (STRCMP(hi->hi_key, "bufnr") == 0)
5036 {
5037 int nr;
5038
5039 if (!(supported2 & JO2_CURWIN))
5040 break;
5041 opt->jo_set2 |= JO2_BUFNR;
5042 nr = tv_get_number(item);
5043 if (nr <= 0)
5044 {
5045 semsg(_(e_invargNval), hi->hi_key, tv_get_string(item));
5046 return FAIL;
5047 }
5048 opt->jo_bufnr_buf = buflist_findnr(nr);
5049 if (opt->jo_bufnr_buf == NULL)
5050 {
5051 semsg(_(e_nobufnr), (long)nr);
5052 return FAIL;
5053 }
5054 if (opt->jo_bufnr_buf->b_nwindows == 0
5055 || opt->jo_bufnr_buf->b_term == NULL)
5056 {
5057 semsg(_(e_invarg2), "bufnr");
5058 return FAIL;
5059 }
5060 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005061 else if (STRCMP(hi->hi_key, "hidden") == 0)
5062 {
5063 if (!(supported2 & JO2_HIDDEN))
5064 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005065 opt->jo_set2 |= JO2_HIDDEN;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005066 opt->jo_hidden = tv_get_number(item);
Bram Moolenaar8cad9302017-08-12 14:32:32 +02005067 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005068 else if (STRCMP(hi->hi_key, "norestore") == 0)
5069 {
5070 if (!(supported2 & JO2_NORESTORE))
5071 break;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005072 opt->jo_set2 |= JO2_NORESTORE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005073 opt->jo_term_norestore = tv_get_number(item);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005074 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005075 else if (STRCMP(hi->hi_key, "term_kill") == 0)
5076 {
5077 if (!(supported2 & JO2_TERM_KILL))
5078 break;
5079 opt->jo_set2 |= JO2_TERM_KILL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005080 opt->jo_term_kill = tv_get_string_chk(item);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005081 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005082 else if (STRCMP(hi->hi_key, "tty_type") == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005083 {
5084 char_u *p;
5085
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005086 if (!(supported2 & JO2_TTY_TYPE))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005087 break;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005088 opt->jo_set2 |= JO2_TTY_TYPE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005089 p = tv_get_string_chk(item);
5090 if (p == NULL)
5091 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005092 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005093 return FAIL;
5094 }
5095 // Allow empty string, "winpty", "conpty".
5096 if (!(*p == NUL || STRCMP(p, "winpty") == 0
5097 || STRCMP(p, "conpty") == 0))
5098 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005099 semsg(_(e_invargval), "tty_type");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005100 return FAIL;
5101 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005102 opt->jo_tty_type = p[0];
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005103 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005104# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5105 else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
5106 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005107 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005108 listitem_T *li;
5109 long_u rgb[16];
5110
5111 if (!(supported2 & JO2_ANSI_COLORS))
5112 break;
5113
5114 if (item == NULL || item->v_type != VAR_LIST
5115 || item->vval.v_list == 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 li = item->vval.v_list->lv_first;
5122 for (; li != NULL && n < 16; li = li->li_next, n++)
5123 {
5124 char_u *color_name;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005125 guicolor_T guicolor;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005126
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005127 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005128 if (color_name == NULL)
5129 return FAIL;
5130
5131 guicolor = GUI_GET_COLOR(color_name);
5132 if (guicolor == INVALCOLOR)
5133 return FAIL;
5134
5135 rgb[n] = GUI_MCH_GET_RGB(guicolor);
5136 }
5137
5138 if (n != 16 || li != NULL)
5139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005140 semsg(_(e_invargval), "ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005141 return FAIL;
5142 }
5143
5144 opt->jo_set2 |= JO2_ANSI_COLORS;
5145 memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
5146 }
5147# endif
Bram Moolenaar8456ea82017-08-05 15:02:05 +02005148#endif
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005149 else if (STRCMP(hi->hi_key, "env") == 0)
5150 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005151 if (!(supported2 & JO2_ENV))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005152 break;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005153 if (item->v_type != VAR_DICT)
5154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005155 semsg(_(e_invargval), "env");
Bram Moolenaar22efba42018-04-07 13:22:21 +02005156 return FAIL;
5157 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005158 opt->jo_set2 |= JO2_ENV;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005159 opt->jo_env = item->vval.v_dict;
Bram Moolenaar22efba42018-04-07 13:22:21 +02005160 if (opt->jo_env != NULL)
5161 ++opt->jo_env->dv_refcount;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005162 }
5163 else if (STRCMP(hi->hi_key, "cwd") == 0)
5164 {
Bram Moolenaar08d384f2017-08-11 21:51:23 +02005165 if (!(supported2 & JO2_CWD))
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005166 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005167 opt->jo_cwd = tv_get_string_buf_chk(item, opt->jo_cwd_buf);
Bram Moolenaar839e81e2018-10-19 16:53:39 +02005168 if (opt->jo_cwd == NULL || !mch_isdir(opt->jo_cwd)
Bram Moolenaar4f974752019-02-17 17:44:42 +01005169#ifndef MSWIN // Win32 directories don't have the concept of "executable"
Bram Moolenaar42a4ea12018-10-19 17:36:01 +02005170 || mch_access((char *)opt->jo_cwd, X_OK) != 0
5171#endif
5172 )
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005174 semsg(_(e_invargval), "cwd");
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005175 return FAIL;
5176 }
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005177 opt->jo_set2 |= JO2_CWD;
Bram Moolenaar05aafed2017-08-11 19:12:11 +02005178 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005179 else if (STRCMP(hi->hi_key, "waittime") == 0)
5180 {
5181 if (!(supported & JO_WAITTIME))
5182 break;
5183 opt->jo_set |= JO_WAITTIME;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005184 opt->jo_waittime = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005185 }
5186 else if (STRCMP(hi->hi_key, "timeout") == 0)
5187 {
5188 if (!(supported & JO_TIMEOUT))
5189 break;
5190 opt->jo_set |= JO_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 opt->jo_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005192 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005193 else if (STRCMP(hi->hi_key, "out_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005194 {
5195 if (!(supported & JO_OUT_TIMEOUT))
5196 break;
5197 opt->jo_set |= JO_OUT_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005198 opt->jo_out_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005199 }
Bram Moolenaard6c2f052016-03-14 23:22:59 +01005200 else if (STRCMP(hi->hi_key, "err_timeout") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005201 {
5202 if (!(supported & JO_ERR_TIMEOUT))
5203 break;
5204 opt->jo_set |= JO_ERR_TIMEOUT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005205 opt->jo_err_timeout = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005206 }
5207 else if (STRCMP(hi->hi_key, "part") == 0)
5208 {
5209 if (!(supported & JO_PART))
5210 break;
5211 opt->jo_set |= JO_PART;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005212 val = tv_get_string(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005213 if (STRCMP(val, "err") == 0)
5214 opt->jo_part = PART_ERR;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02005215 else if (STRCMP(val, "out") == 0)
5216 opt->jo_part = PART_OUT;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005217 else
5218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005219 semsg(_(e_invargNval), "part", val);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005220 return FAIL;
5221 }
5222 }
5223 else if (STRCMP(hi->hi_key, "id") == 0)
5224 {
5225 if (!(supported & JO_ID))
5226 break;
5227 opt->jo_set |= JO_ID;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005228 opt->jo_id = tv_get_number(item);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005229 }
5230 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
5231 {
5232 if (!(supported & JO_STOPONEXIT))
5233 break;
5234 opt->jo_set |= JO_STOPONEXIT;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005235 opt->jo_stoponexit = tv_get_string_buf_chk(item,
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005236 opt->jo_soe_buf);
5237 if (opt->jo_stoponexit == NULL)
5238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005239 semsg(_(e_invargval), "stoponexit");
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005240 return FAIL;
5241 }
5242 }
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005243 else if (STRCMP(hi->hi_key, "block_write") == 0)
5244 {
5245 if (!(supported & JO_BLOCK_WRITE))
5246 break;
5247 opt->jo_set |= JO_BLOCK_WRITE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005248 opt->jo_block_write = tv_get_number(item);
Bram Moolenaar8b877ac2016-03-28 19:16:20 +02005249 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005250 else
5251 break;
5252 --todo;
5253 }
5254 if (todo > 0)
5255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005256 semsg(_(e_invarg2), hi->hi_key);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005257 return FAIL;
5258 }
5259
5260 return OK;
5261}
5262
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005263static job_T *first_job = NULL;
5264
5265 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005266job_free_contents(job_T *job)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005267{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005268 int i;
5269
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005270 ch_log(job->jv_channel, "Freeing job");
5271 if (job->jv_channel != NULL)
5272 {
5273 /* The link from the channel to the job doesn't count as a reference,
5274 * thus don't decrement the refcount of the job. The reference from
Bram Moolenaaraad30bb2016-06-26 17:31:03 +02005275 * the job to the channel does count the reference, decrement it and
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005276 * NULL the reference. We don't set ch_job_killed, unreferencing the
5277 * job doesn't mean it stops running. */
5278 job->jv_channel->ch_job = NULL;
5279 channel_unref(job->jv_channel);
5280 }
5281 mch_clear_job(job);
5282
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02005283 vim_free(job->jv_tty_in);
5284 vim_free(job->jv_tty_out);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005285 vim_free(job->jv_stoponexit);
Bram Moolenaarb3051ce2019-01-31 15:52:11 +01005286#ifdef UNIX
5287 vim_free(job->jv_termsig);
5288#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005289#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01005290 vim_free(job->jv_tty_type);
5291#endif
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005292 free_callback(&job->jv_exit_cb);
Bram Moolenaar20608922018-04-21 22:30:08 +02005293 if (job->jv_argv != NULL)
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005294 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005295 for (i = 0; job->jv_argv[i] != NULL; i++)
5296 vim_free(job->jv_argv[i]);
5297 vim_free(job->jv_argv);
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005298 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005299}
5300
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005301/*
5302 * Remove "job" from the list of jobs.
5303 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005304 static void
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005305job_unlink(job_T *job)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005306{
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005307 if (job->jv_next != NULL)
5308 job->jv_next->jv_prev = job->jv_prev;
5309 if (job->jv_prev == NULL)
5310 first_job = job->jv_next;
5311 else
5312 job->jv_prev->jv_next = job->jv_next;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005313}
5314
5315 static void
5316job_free_job(job_T *job)
5317{
5318 job_unlink(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005319 vim_free(job);
5320}
5321
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005322 static void
5323job_free(job_T *job)
5324{
5325 if (!in_free_unref_items)
5326 {
5327 job_free_contents(job);
5328 job_free_job(job);
5329 }
5330}
5331
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005332static job_T *jobs_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005333
5334/*
5335 * Put "job" in a list to be freed later, when it's no longer referenced.
5336 */
5337 static void
5338job_free_later(job_T *job)
5339{
5340 job_unlink(job);
5341 job->jv_next = jobs_to_free;
5342 jobs_to_free = job;
5343}
5344
5345 static void
5346free_jobs_to_free_later(void)
5347{
5348 job_T *job;
5349
5350 while (jobs_to_free != NULL)
5351 {
5352 job = jobs_to_free;
5353 jobs_to_free = job->jv_next;
5354 job_free_contents(job);
5355 vim_free(job);
5356 }
5357}
5358
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005359#if defined(EXITFREE) || defined(PROTO)
5360 void
5361job_free_all(void)
5362{
5363 while (first_job != NULL)
5364 job_free(first_job);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005365 free_jobs_to_free_later();
5366
5367# ifdef FEAT_TERMINAL
5368 free_unused_terminals();
5369# endif
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005370}
5371#endif
5372
5373/*
5374 * Return TRUE if we need to check if the process of "job" has ended.
5375 */
5376 static int
5377job_need_end_check(job_T *job)
5378{
5379 return job->jv_status == JOB_STARTED
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005380 && (job->jv_stoponexit != NULL || job->jv_exit_cb.cb_name != NULL);
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005381}
5382
5383/*
5384 * Return TRUE if the channel of "job" is still useful.
5385 */
5386 static int
5387job_channel_still_useful(job_T *job)
5388{
5389 return job->jv_channel != NULL && channel_still_useful(job->jv_channel);
5390}
5391
5392/*
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005393 * Return TRUE if the channel of "job" is closeable.
5394 */
5395 static int
5396job_channel_can_close(job_T *job)
5397{
5398 return job->jv_channel != NULL && channel_can_close(job->jv_channel);
5399}
5400
5401/*
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005402 * Return TRUE if the job should not be freed yet. Do not free the job when
5403 * it has not ended yet and there is a "stoponexit" flag, an exit callback
5404 * or when the associated channel will do something with the job output.
5405 */
5406 static int
5407job_still_useful(job_T *job)
5408{
5409 return job_need_end_check(job) || job_channel_still_useful(job);
5410}
5411
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005412#if defined(GUI_MAY_FORK) || defined(GUI_MAY_SPAWN) || defined(PROTO)
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005413/*
5414 * Return TRUE when there is any running job that we care about.
5415 */
5416 int
5417job_any_running()
5418{
5419 job_T *job;
5420
5421 for (job = first_job; job != NULL; job = job->jv_next)
5422 if (job_still_useful(job))
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005423 {
5424 ch_log(NULL, "GUI not forking because a job is running");
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005425 return TRUE;
Bram Moolenaare65fffd2018-05-13 14:40:15 +02005426 }
Bram Moolenaarb0b98d52018-05-05 21:01:00 +02005427 return FALSE;
5428}
5429#endif
5430
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005431#if !defined(USE_ARGV) || defined(PROTO)
5432/*
5433 * Escape one argument for an external command.
5434 * Returns the escaped string in allocated memory. NULL when out of memory.
5435 */
5436 static char_u *
5437win32_escape_arg(char_u *arg)
5438{
5439 int slen, dlen;
5440 int escaping = 0;
5441 int i;
5442 char_u *s, *d;
5443 char_u *escaped_arg;
5444 int has_spaces = FALSE;
5445
5446 /* First count the number of extra bytes required. */
Bram Moolenaare85928a2017-08-27 13:10:10 +02005447 slen = (int)STRLEN(arg);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005448 dlen = slen;
5449 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5450 {
5451 if (*s == '"' || *s == '\\')
5452 ++dlen;
5453 if (*s == ' ' || *s == '\t')
5454 has_spaces = TRUE;
5455 }
5456
5457 if (has_spaces)
5458 dlen += 2;
5459
5460 if (dlen == slen)
5461 return vim_strsave(arg);
5462
5463 /* Allocate memory for the result and fill it. */
5464 escaped_arg = alloc(dlen + 1);
5465 if (escaped_arg == NULL)
5466 return NULL;
5467 memset(escaped_arg, 0, dlen+1);
5468
5469 d = escaped_arg;
5470
5471 if (has_spaces)
5472 *d++ = '"';
5473
5474 for (s = arg; *s != NUL;)
5475 {
5476 switch (*s)
5477 {
5478 case '"':
5479 for (i = 0; i < escaping; i++)
5480 *d++ = '\\';
5481 escaping = 0;
5482 *d++ = '\\';
5483 *d++ = *s++;
5484 break;
5485 case '\\':
5486 escaping++;
5487 *d++ = *s++;
5488 break;
5489 default:
5490 escaping = 0;
5491 MB_COPY_CHAR(s, d);
5492 break;
5493 }
5494 }
5495
5496 /* add terminating quote and finish with a NUL */
5497 if (has_spaces)
5498 {
5499 for (i = 0; i < escaping; i++)
5500 *d++ = '\\';
5501 *d++ = '"';
5502 }
5503 *d = NUL;
5504
5505 return escaped_arg;
5506}
5507
5508/*
5509 * Build a command line from a list, taking care of escaping.
5510 * The result is put in gap->ga_data.
5511 * Returns FAIL when out of memory.
5512 */
5513 int
5514win32_build_cmd(list_T *l, garray_T *gap)
5515{
5516 listitem_T *li;
5517 char_u *s;
5518
5519 for (li = l->lv_first; li != NULL; li = li->li_next)
5520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005521 s = tv_get_string_chk(&li->li_tv);
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005522 if (s == NULL)
5523 return FAIL;
5524 s = win32_escape_arg(s);
5525 if (s == NULL)
5526 return FAIL;
5527 ga_concat(gap, s);
5528 vim_free(s);
5529 if (li->li_next != NULL)
5530 ga_append(gap, ' ');
5531 }
5532 return OK;
5533}
5534#endif
5535
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005536/*
5537 * NOTE: Must call job_cleanup() only once right after the status of "job"
5538 * changed to JOB_ENDED (i.e. after job_status() returned "dead" first or
5539 * mch_detect_ended_job() returned non-NULL).
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005540 * If the job is no longer used it will be removed from the list of jobs, and
5541 * deleted a bit later.
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005542 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005543 void
Bram Moolenaar97792de2016-10-15 18:36:49 +02005544job_cleanup(job_T *job)
5545{
5546 if (job->jv_status != JOB_ENDED)
5547 return;
5548
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005549 /* Ready to cleanup the job. */
5550 job->jv_status = JOB_FINISHED;
5551
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005552 /* When only channel-in is kept open, close explicitly. */
5553 if (job->jv_channel != NULL)
5554 ch_close_part(job->jv_channel, PART_IN);
5555
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005556 if (job->jv_exit_cb.cb_name != NULL)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005557 {
5558 typval_T argv[3];
5559 typval_T rettv;
Bram Moolenaar97792de2016-10-15 18:36:49 +02005560
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005561 /* Invoke the exit callback. Make sure the refcount is > 0. */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005562 ch_log(job->jv_channel, "Invoking exit callback %s",
5563 job->jv_exit_cb.cb_name);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005564 ++job->jv_refcount;
5565 argv[0].v_type = VAR_JOB;
5566 argv[0].vval.v_job = job;
5567 argv[1].v_type = VAR_NUMBER;
5568 argv[1].vval.v_number = job->jv_exitval;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005569 call_callback(&job->jv_exit_cb, -1, &rettv, 2, argv);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005570 clear_tv(&rettv);
5571 --job->jv_refcount;
5572 channel_need_redraw = TRUE;
5573 }
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005574
Bram Moolenaar8caa43d2019-02-20 22:45:06 +01005575 if (job->jv_channel != NULL && job->jv_channel->ch_anonymous_pipe)
5576 job->jv_channel->ch_killing = TRUE;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005577
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005578 // Do not free the job in case the close callback of the associated channel
5579 // isn't invoked yet and may get information by job_info().
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005580 if (job->jv_refcount == 0 && !job_channel_still_useful(job))
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005581 // The job was already unreferenced and the associated channel was
5582 // detached, now that it ended it can be freed. However, a caller might
5583 // still use it, thus free it a bit later.
5584 job_free_later(job);
Bram Moolenaar97792de2016-10-15 18:36:49 +02005585}
5586
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005587/*
5588 * Mark references in jobs that are still useful.
5589 */
5590 int
5591set_ref_in_job(int copyID)
5592{
5593 int abort = FALSE;
5594 job_T *job;
5595 typval_T tv;
5596
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005597 for (job = first_job; !abort && job != NULL; job = job->jv_next)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005598 if (job_still_useful(job))
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005599 {
5600 tv.v_type = VAR_JOB;
5601 tv.vval.v_job = job;
5602 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5603 }
5604 return abort;
5605}
5606
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005607/*
5608 * Dereference "job". Note that after this "job" may have been freed.
5609 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005610 void
5611job_unref(job_T *job)
5612{
5613 if (job != NULL && --job->jv_refcount <= 0)
5614 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005615 /* Do not free the job if there is a channel where the close callback
5616 * may get the job info. */
5617 if (!job_channel_still_useful(job))
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005618 {
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005619 /* Do not free the job when it has not ended yet and there is a
5620 * "stoponexit" flag or an exit callback. */
5621 if (!job_need_end_check(job))
5622 {
5623 job_free(job);
5624 }
5625 else if (job->jv_channel != NULL)
5626 {
5627 /* Do remove the link to the channel, otherwise it hangs
5628 * around until Vim exits. See job_free() for refcount. */
5629 ch_log(job->jv_channel, "detaching channel from job");
5630 job->jv_channel->ch_job = NULL;
5631 channel_unref(job->jv_channel);
5632 job->jv_channel = NULL;
5633 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005634 }
5635 }
5636}
5637
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005638 int
5639free_unused_jobs_contents(int copyID, int mask)
5640{
5641 int did_free = FALSE;
5642 job_T *job;
5643
5644 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005645 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005646 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005647 {
5648 /* Free the channel and ordinary items it contains, but don't
5649 * recurse into Lists, Dictionaries etc. */
5650 job_free_contents(job);
5651 did_free = TRUE;
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005652 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005653 return did_free;
5654}
5655
5656 void
5657free_unused_jobs(int copyID, int mask)
5658{
5659 job_T *job;
5660 job_T *job_next;
5661
5662 for (job = first_job; job != NULL; job = job_next)
5663 {
5664 job_next = job->jv_next;
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005665 if ((job->jv_copyID & mask) != (copyID & mask)
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005666 && !job_still_useful(job))
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005667 {
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005668 /* Free the job struct itself. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005669 job_free_job(job);
5670 }
5671 }
5672}
5673
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005674/*
5675 * Allocate a job. Sets the refcount to one and sets options default.
5676 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02005677 job_T *
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005678job_alloc(void)
5679{
5680 job_T *job;
5681
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005682 job = ALLOC_CLEAR_ONE(job_T);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005683 if (job != NULL)
5684 {
5685 job->jv_refcount = 1;
5686 job->jv_stoponexit = vim_strsave((char_u *)"term");
5687
5688 if (first_job != NULL)
5689 {
5690 first_job->jv_prev = job;
5691 job->jv_next = first_job;
5692 }
5693 first_job = job;
5694 }
5695 return job;
5696}
5697
5698 void
5699job_set_options(job_T *job, jobopt_T *opt)
5700{
5701 if (opt->jo_set & JO_STOPONEXIT)
5702 {
5703 vim_free(job->jv_stoponexit);
5704 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
5705 job->jv_stoponexit = NULL;
5706 else
5707 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
5708 }
5709 if (opt->jo_set & JO_EXIT_CB)
5710 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005711 free_callback(&job->jv_exit_cb);
5712 if (opt->jo_exit_cb.cb_name == NULL || *opt->jo_exit_cb.cb_name == NUL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005713 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005714 job->jv_exit_cb.cb_name = NULL;
5715 job->jv_exit_cb.cb_partial = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005716 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005717 else
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005718 copy_callback(&job->jv_exit_cb, &opt->jo_exit_cb);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005719 }
5720}
5721
5722/*
5723 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
5724 */
5725 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005726job_stop_on_exit(void)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005727{
5728 job_T *job;
5729
5730 for (job = first_job; job != NULL; job = job->jv_next)
5731 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02005732 mch_signal_job(job, job->jv_stoponexit);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005733}
5734
5735/*
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005736 * Return TRUE when there is any job that has an exit callback and might exit,
5737 * which means job_check_ended() should be called more often.
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005738 */
5739 int
Bram Moolenaarcf089462016-06-12 21:18:43 +02005740has_pending_job(void)
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005741{
5742 job_T *job;
5743
5744 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaar01688ad2016-10-27 20:00:07 +02005745 /* Only should check if the channel has been closed, if the channel is
5746 * open the job won't exit. */
Bram Moolenaar4e9d4432018-04-24 20:54:07 +02005747 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5748 || (job->jv_status == JOB_FINISHED
5749 && job_channel_can_close(job)))
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005750 return TRUE;
5751 return FALSE;
5752}
5753
Bram Moolenaar97792de2016-10-15 18:36:49 +02005754#define MAX_CHECK_ENDED 8
5755
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005756/*
Bram Moolenaar36e0f7d2016-05-08 13:21:12 +02005757 * Called once in a while: check if any jobs that seem useful have ended.
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005758 * Returns TRUE if a job did end.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005759 */
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005760 int
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005761job_check_ended(void)
5762{
Bram Moolenaar97792de2016-10-15 18:36:49 +02005763 int i;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005764 int did_end = FALSE;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005765
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005766 // be quick if there are no jobs to check
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005767 if (first_job == NULL)
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005768 return did_end;
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005769
Bram Moolenaar97792de2016-10-15 18:36:49 +02005770 for (i = 0; i < MAX_CHECK_ENDED; ++i)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005771 {
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005772 // NOTE: mch_detect_ended_job() must only return a job of which the
5773 // status was just set to JOB_ENDED.
Bram Moolenaar97792de2016-10-15 18:36:49 +02005774 job_T *job = mch_detect_ended_job(first_job);
5775
5776 if (job == NULL)
5777 break;
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005778 did_end = TRUE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005779 job_cleanup(job); // may add "job" to jobs_to_free
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005780 }
Bram Moolenaar97792de2016-10-15 18:36:49 +02005781
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01005782 // Actually free jobs that were cleaned up.
5783 free_jobs_to_free_later();
5784
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005785 if (channel_need_redraw)
5786 {
5787 channel_need_redraw = FALSE;
Bram Moolenaar02e177d2017-08-26 23:43:28 +02005788 redraw_after_callback(TRUE);
Bram Moolenaarcf7c11a2016-06-02 20:05:26 +02005789 }
Bram Moolenaarcd1a62d2018-12-14 21:32:02 +01005790 return did_end;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005791}
5792
5793/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005794 * Create a job and return it. Implements job_start().
Bram Moolenaar13568252018-03-16 20:46:58 +01005795 * "argv_arg" is only for Unix.
5796 * When "argv_arg" is NULL then "argvars" is used.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02005797 * The returned job has a refcount of one.
5798 * Returns NULL when out of memory.
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005799 */
5800 job_T *
Bram Moolenaar493359e2018-06-12 20:25:52 +02005801job_start(
5802 typval_T *argvars,
5803 char **argv_arg,
5804 jobopt_T *opt_arg,
5805 int is_terminal UNUSED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005806{
5807 job_T *job;
5808 char_u *cmd = NULL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005809 char **argv = NULL;
5810 int argc = 0;
Bram Moolenaar20608922018-04-21 22:30:08 +02005811#if defined(UNIX)
5812# define USE_ARGV
Bram Moolenaar850d4272018-04-30 10:38:40 +02005813 int i;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005814#else
5815 garray_T ga;
5816#endif
5817 jobopt_T opt;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005818 ch_part_T part;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005819
5820 job = job_alloc();
5821 if (job == NULL)
5822 return NULL;
5823
5824 job->jv_status = JOB_FAILED;
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005825#ifndef USE_ARGV
5826 ga_init2(&ga, (int)sizeof(char*), 20);
5827#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005828
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005829 if (opt_arg != NULL)
5830 opt = *opt_arg;
5831 else
5832 {
5833 /* Default mode is NL. */
5834 clear_job_options(&opt);
5835 opt.jo_mode = MODE_NL;
5836 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar8ed54002017-08-11 22:22:36 +02005837 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5838 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5839 JO2_ENV + JO2_CWD) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02005840 goto theend;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005841 }
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005842
5843 /* Check that when io is "file" that there is a file name. */
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005844 for (part = PART_OUT; part < PART_COUNT; ++part)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005845 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5846 && opt.jo_io[part] == JIO_FILE
5847 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5848 || *opt.jo_io_name[part] == NUL))
5849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005850 emsg(_("E920: _io file requires _name to be set"));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005851 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005852 }
5853
5854 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5855 {
5856 buf_T *buf = NULL;
5857
5858 /* check that we can find the buffer before starting the job */
5859 if (opt.jo_set & JO_IN_BUF)
5860 {
5861 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5862 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005863 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005864 }
5865 else if (!(opt.jo_set & JO_IN_NAME))
5866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005867 emsg(_("E915: in_io buffer requires in_buf or in_name to be set"));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005868 }
5869 else
5870 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
5871 if (buf == NULL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005872 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005873 if (buf->b_ml.ml_mfp == NULL)
5874 {
5875 char_u numbuf[NUMBUFLEN];
5876 char_u *s;
5877
5878 if (opt.jo_set & JO_IN_BUF)
5879 {
5880 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
5881 s = numbuf;
5882 }
5883 else
5884 s = opt.jo_io_name[PART_IN];
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005885 semsg(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005886 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005887 }
5888 job->jv_in_buf = buf;
5889 }
5890
5891 job_set_options(job, &opt);
5892
Bram Moolenaar13568252018-03-16 20:46:58 +01005893#ifdef USE_ARGV
5894 if (argv_arg != NULL)
5895 {
Bram Moolenaar20608922018-04-21 22:30:08 +02005896 /* Make a copy of argv_arg for job->jv_argv. */
5897 for (i = 0; argv_arg[i] != NULL; i++)
5898 argc++;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005899 argv = ALLOC_MULT(char *, argc + 1);
Bram Moolenaar20608922018-04-21 22:30:08 +02005900 if (argv == NULL)
5901 goto theend;
5902 for (i = 0; i < argc; i++)
5903 argv[i] = (char *)vim_strsave((char_u *)argv_arg[i]);
5904 argv[argc] = NULL;
Bram Moolenaar13568252018-03-16 20:46:58 +01005905 }
5906 else
5907#endif
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005908 if (argvars[0].v_type == VAR_STRING)
5909 {
5910 /* Command is a string. */
5911 cmd = argvars[0].vval.v_string;
Bram Moolenaar80385682016-03-27 19:13:35 +02005912 if (cmd == NULL || *cmd == NUL)
5913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005914 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005915 goto theend;
Bram Moolenaar80385682016-03-27 19:13:35 +02005916 }
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005917
5918 if (build_argv_from_string(cmd, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005919 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005920 }
5921 else if (argvars[0].v_type != VAR_LIST
5922 || argvars[0].vval.v_list == NULL
5923 || argvars[0].vval.v_list->lv_len < 1)
5924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005925 emsg(_(e_invarg));
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005926 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005927 }
5928 else
5929 {
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005930 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005931
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005932 if (build_argv_from_list(l, &argv, &argc) == FAIL)
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005933 goto theend;
Bram Moolenaar20608922018-04-21 22:30:08 +02005934#ifndef USE_ARGV
Bram Moolenaardcaa6132017-08-13 17:13:09 +02005935 if (win32_build_cmd(l, &ga) == FAIL)
5936 goto theend;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005937 cmd = ga.ga_data;
5938#endif
5939 }
5940
Bram Moolenaar20608922018-04-21 22:30:08 +02005941 /* Save the command used to start the job. */
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005942 job->jv_argv = argv;
Bram Moolenaare1fc5152018-04-21 19:49:08 +02005943
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005944#ifdef USE_ARGV
5945 if (ch_log_active())
5946 {
5947 garray_T ga;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005948
5949 ga_init2(&ga, (int)sizeof(char), 200);
5950 for (i = 0; i < argc; ++i)
5951 {
5952 if (i > 0)
5953 ga_concat(&ga, (char_u *)" ");
5954 ga_concat(&ga, (char_u *)argv[i]);
5955 }
Bram Moolenaar04af1962019-04-12 21:19:04 +02005956 ga_append(&ga, NUL);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005957 ch_log(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005958 ga_clear(&ga);
5959 }
Bram Moolenaar493359e2018-06-12 20:25:52 +02005960 mch_job_start(argv, job, &opt, is_terminal);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005961#else
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02005962 ch_log(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar5a1feb82017-07-22 18:04:08 +02005963 mch_job_start((char *)cmd, job, &opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005964#endif
5965
5966 /* If the channel is reading from a buffer, write lines now. */
5967 if (job->jv_channel != NULL)
5968 channel_write_in(job->jv_channel);
5969
5970theend:
Bram Moolenaar20608922018-04-21 22:30:08 +02005971#ifndef USE_ARGV
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005972 vim_free(ga.ga_data);
5973#endif
Bram Moolenaarb2ed6802018-05-13 14:05:18 +02005974 if (argv != job->jv_argv)
Bram Moolenaar20608922018-04-21 22:30:08 +02005975 vim_free(argv);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +02005976 free_job_options(&opt);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005977 return job;
5978}
5979
5980/*
5981 * Get the status of "job" and invoke the exit callback when needed.
5982 * The returned string is not allocated.
5983 */
5984 char *
5985job_status(job_T *job)
5986{
5987 char *result;
5988
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005989 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005990 /* No need to check, dead is dead. */
5991 result = "dead";
5992 else if (job->jv_status == JOB_FAILED)
5993 result = "fail";
5994 else
5995 {
5996 result = mch_job_status(job);
5997 if (job->jv_status == JOB_ENDED)
Bram Moolenaar97792de2016-10-15 18:36:49 +02005998 job_cleanup(job);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005999 }
6000 return result;
6001}
6002
Bram Moolenaar8950a562016-03-12 15:22:55 +01006003/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006004 * Send a signal to "job". Implements job_stop().
6005 * When "type" is not NULL use this for the type.
6006 * Otherwise use argvars[1] for the type.
6007 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006008 int
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006009job_stop(job_T *job, typval_T *argvars, char *type)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006010{
6011 char_u *arg;
6012
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006013 if (type != NULL)
6014 arg = (char_u *)type;
6015 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006016 arg = (char_u *)"";
6017 else
6018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006019 arg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006020 if (arg == NULL)
6021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006022 emsg(_(e_invarg));
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006023 return 0;
6024 }
6025 }
Bram Moolenaar61a66052017-07-22 18:39:00 +02006026 if (job->jv_status == JOB_FAILED)
6027 {
6028 ch_log(job->jv_channel, "Job failed to start, job_stop() skipped");
6029 return 0;
6030 }
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006031 if (job->jv_status == JOB_ENDED)
6032 {
6033 ch_log(job->jv_channel, "Job has already ended, job_stop() skipped");
6034 return 0;
6035 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02006036 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar2d33e902017-08-11 16:31:54 +02006037 if (mch_signal_job(job, arg) == FAIL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006038 return 0;
6039
Bram Moolenaar1a9020d2017-04-29 16:24:38 +02006040 /* Assume that only "kill" will kill the job. */
6041 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01006042 job->jv_channel->ch_job_killed = TRUE;
6043
6044 /* We don't try freeing the job, obviously the caller still has a
6045 * reference to it. */
6046 return 1;
6047}
6048
Bram Moolenaarf2732452018-06-03 14:47:35 +02006049 void
6050invoke_prompt_callback(void)
6051{
6052 typval_T rettv;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006053 typval_T argv[2];
6054 char_u *text;
6055 char_u *prompt;
6056 linenr_T lnum = curbuf->b_ml.ml_line_count;
6057
6058 // Add a new line for the prompt before invoking the callback, so that
6059 // text can always be inserted above the last line.
6060 ml_append(lnum, (char_u *)"", 0, FALSE);
6061 curwin->w_cursor.lnum = lnum + 1;
6062 curwin->w_cursor.col = 0;
6063
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006064 if (curbuf->b_prompt_callback.cb_name == NULL
6065 || *curbuf->b_prompt_callback.cb_name == NUL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006066 return;
6067 text = ml_get(lnum);
6068 prompt = prompt_text();
6069 if (STRLEN(text) >= STRLEN(prompt))
6070 text += STRLEN(prompt);
6071 argv[0].v_type = VAR_STRING;
6072 argv[0].vval.v_string = vim_strsave(text);
6073 argv[1].v_type = VAR_UNKNOWN;
6074
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006075 call_callback(&curbuf->b_prompt_callback, -1, &rettv, 1, argv);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006076 clear_tv(&argv[0]);
6077 clear_tv(&rettv);
6078}
6079
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006080/*
6081 * Return TRUE when the interrupt callback was invoked.
6082 */
6083 int
6084invoke_prompt_interrupt(void)
6085{
6086 typval_T rettv;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006087 typval_T argv[1];
6088
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02006089 if (curbuf->b_prompt_interrupt.cb_name == NULL
6090 || *curbuf->b_prompt_interrupt.cb_name == NUL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006091 return FALSE;
6092 argv[0].v_type = VAR_UNKNOWN;
6093
6094 got_int = FALSE; // don't skip executing commands
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006095 call_callback(&curbuf->b_prompt_interrupt, -1, &rettv, 0, argv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02006096 clear_tv(&rettv);
6097 return TRUE;
6098}
6099
Bram Moolenaar0a1f56f2019-06-24 00:43:35 +02006100/*
6101 * "prompt_setcallback({buffer}, {callback})" function
6102 */
6103 void
6104f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
6105{
6106 buf_T *buf;
6107 callback_T callback;
6108
6109 if (check_secure())
6110 return;
6111 buf = tv_get_buf(&argvars[0], FALSE);
6112 if (buf == NULL)
6113 return;
6114
6115 callback = get_callback(&argvars[1]);
6116 if (callback.cb_name == NULL)
6117 return;
6118
6119 free_callback(&buf->b_prompt_callback);
6120 set_callback(&buf->b_prompt_callback, &callback);
6121}
6122
6123/*
6124 * "prompt_setinterrupt({buffer}, {callback})" function
6125 */
6126 void
6127f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
6128{
6129 buf_T *buf;
6130 callback_T callback;
6131
6132 if (check_secure())
6133 return;
6134 buf = tv_get_buf(&argvars[0], FALSE);
6135 if (buf == NULL)
6136 return;
6137
6138 callback = get_callback(&argvars[1]);
6139 if (callback.cb_name == NULL)
6140 return;
6141
6142 free_callback(&buf->b_prompt_interrupt);
6143 set_callback(&buf->b_prompt_interrupt, &callback);
6144}
6145
6146/*
6147 * "prompt_setprompt({buffer}, {text})" function
6148 */
6149 void
6150f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
6151{
6152 buf_T *buf;
6153 char_u *text;
6154
6155 if (check_secure())
6156 return;
6157 buf = tv_get_buf(&argvars[0], FALSE);
6158 if (buf == NULL)
6159 return;
6160
6161 text = tv_get_string(&argvars[1]);
6162 vim_free(buf->b_prompt_text);
6163 buf->b_prompt_text = vim_strsave(text);
6164}
6165
6166/*
6167 * "ch_canread()" function
6168 */
6169 void
6170f_ch_canread(typval_T *argvars, typval_T *rettv)
6171{
6172 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6173
6174 rettv->vval.v_number = 0;
6175 if (channel != NULL)
6176 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
6177 || channel_has_readahead(channel, PART_OUT)
6178 || channel_has_readahead(channel, PART_ERR);
6179}
6180
6181/*
6182 * "ch_close()" function
6183 */
6184 void
6185f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
6186{
6187 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6188
6189 if (channel != NULL)
6190 {
6191 channel_close(channel, FALSE);
6192 channel_clear(channel);
6193 }
6194}
6195
6196/*
6197 * "ch_close()" function
6198 */
6199 void
6200f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
6201{
6202 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
6203
6204 if (channel != NULL)
6205 channel_close_in(channel);
6206}
6207
6208/*
6209 * "ch_getbufnr()" function
6210 */
6211 void
6212f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
6213{
6214 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6215
6216 rettv->vval.v_number = -1;
6217 if (channel != NULL)
6218 {
6219 char_u *what = tv_get_string(&argvars[1]);
6220 int part;
6221
6222 if (STRCMP(what, "err") == 0)
6223 part = PART_ERR;
6224 else if (STRCMP(what, "out") == 0)
6225 part = PART_OUT;
6226 else if (STRCMP(what, "in") == 0)
6227 part = PART_IN;
6228 else
6229 part = PART_SOCK;
6230 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
6231 rettv->vval.v_number =
6232 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
6233 }
6234}
6235
6236/*
6237 * "ch_getjob()" function
6238 */
6239 void
6240f_ch_getjob(typval_T *argvars, typval_T *rettv)
6241{
6242 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6243
6244 if (channel != NULL)
6245 {
6246 rettv->v_type = VAR_JOB;
6247 rettv->vval.v_job = channel->ch_job;
6248 if (channel->ch_job != NULL)
6249 ++channel->ch_job->jv_refcount;
6250 }
6251}
6252
6253/*
6254 * "ch_info()" function
6255 */
6256 void
6257f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
6258{
6259 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6260
6261 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
6262 channel_info(channel, rettv->vval.v_dict);
6263}
6264
6265/*
6266 * "ch_log()" function
6267 */
6268 void
6269f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
6270{
6271 char_u *msg = tv_get_string(&argvars[0]);
6272 channel_T *channel = NULL;
6273
6274 if (argvars[1].v_type != VAR_UNKNOWN)
6275 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
6276
6277 ch_log(channel, "%s", msg);
6278}
6279
6280/*
6281 * "ch_logfile()" function
6282 */
6283 void
6284f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
6285{
6286 char_u *fname;
6287 char_u *opt = (char_u *)"";
6288 char_u buf[NUMBUFLEN];
6289
6290 /* Don't open a file in restricted mode. */
6291 if (check_restricted() || check_secure())
6292 return;
6293 fname = tv_get_string(&argvars[0]);
6294 if (argvars[1].v_type == VAR_STRING)
6295 opt = tv_get_string_buf(&argvars[1], buf);
6296 ch_logfile(fname, opt);
6297}
6298
6299/*
6300 * "ch_open()" function
6301 */
6302 void
6303f_ch_open(typval_T *argvars, typval_T *rettv)
6304{
6305 rettv->v_type = VAR_CHANNEL;
6306 if (check_restricted() || check_secure())
6307 return;
6308 rettv->vval.v_channel = channel_open_func(argvars);
6309}
6310
6311/*
6312 * "ch_read()" function
6313 */
6314 void
6315f_ch_read(typval_T *argvars, typval_T *rettv)
6316{
6317 common_channel_read(argvars, rettv, FALSE, FALSE);
6318}
6319
6320/*
6321 * "ch_readblob()" function
6322 */
6323 void
6324f_ch_readblob(typval_T *argvars, typval_T *rettv)
6325{
6326 common_channel_read(argvars, rettv, TRUE, TRUE);
6327}
6328
6329/*
6330 * "ch_readraw()" function
6331 */
6332 void
6333f_ch_readraw(typval_T *argvars, typval_T *rettv)
6334{
6335 common_channel_read(argvars, rettv, TRUE, FALSE);
6336}
6337
6338/*
6339 * "ch_evalexpr()" function
6340 */
6341 void
6342f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
6343{
6344 ch_expr_common(argvars, rettv, TRUE);
6345}
6346
6347/*
6348 * "ch_sendexpr()" function
6349 */
6350 void
6351f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
6352{
6353 ch_expr_common(argvars, rettv, FALSE);
6354}
6355
6356/*
6357 * "ch_evalraw()" function
6358 */
6359 void
6360f_ch_evalraw(typval_T *argvars, typval_T *rettv)
6361{
6362 ch_raw_common(argvars, rettv, TRUE);
6363}
6364
6365/*
6366 * "ch_sendraw()" function
6367 */
6368 void
6369f_ch_sendraw(typval_T *argvars, typval_T *rettv)
6370{
6371 ch_raw_common(argvars, rettv, FALSE);
6372}
6373
6374/*
6375 * "ch_setoptions()" function
6376 */
6377 void
6378f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6379{
6380 channel_T *channel;
6381 jobopt_T opt;
6382
6383 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6384 if (channel == NULL)
6385 return;
6386 clear_job_options(&opt);
6387 if (get_job_options(&argvars[1], &opt,
6388 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
6389 channel_set_options(channel, &opt);
6390 free_job_options(&opt);
6391}
6392
6393/*
6394 * "ch_status()" function
6395 */
6396 void
6397f_ch_status(typval_T *argvars, typval_T *rettv)
6398{
6399 channel_T *channel;
6400 jobopt_T opt;
6401 int part = -1;
6402
6403 /* return an empty string by default */
6404 rettv->v_type = VAR_STRING;
6405 rettv->vval.v_string = NULL;
6406
6407 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6408
6409 if (argvars[1].v_type != VAR_UNKNOWN)
6410 {
6411 clear_job_options(&opt);
6412 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
6413 && (opt.jo_set & JO_PART))
6414 part = opt.jo_part;
6415 }
6416
6417 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
6418}
6419
6420/*
6421 * Get the job from the argument.
6422 * Returns NULL if the job is invalid.
6423 */
6424 static job_T *
6425get_job_arg(typval_T *tv)
6426{
6427 job_T *job;
6428
6429 if (tv->v_type != VAR_JOB)
6430 {
6431 semsg(_(e_invarg2), tv_get_string(tv));
6432 return NULL;
6433 }
6434 job = tv->vval.v_job;
6435
6436 if (job == NULL)
6437 emsg(_("E916: not a valid job"));
6438 return job;
6439}
6440
6441/*
6442 * "job_getchannel()" function
6443 */
6444 void
6445f_job_getchannel(typval_T *argvars, typval_T *rettv)
6446{
6447 job_T *job = get_job_arg(&argvars[0]);
6448
6449 if (job != NULL)
6450 {
6451 rettv->v_type = VAR_CHANNEL;
6452 rettv->vval.v_channel = job->jv_channel;
6453 if (job->jv_channel != NULL)
6454 ++job->jv_channel->ch_refcount;
6455 }
6456}
6457
6458/*
6459 * Implementation of job_info().
6460 */
6461 static void
6462job_info(job_T *job, dict_T *dict)
6463{
6464 dictitem_T *item;
6465 varnumber_T nr;
6466 list_T *l;
6467 int i;
6468
6469 dict_add_string(dict, "status", (char_u *)job_status(job));
6470
6471 item = dictitem_alloc((char_u *)"channel");
6472 if (item == NULL)
6473 return;
6474 item->di_tv.v_type = VAR_CHANNEL;
6475 item->di_tv.vval.v_channel = job->jv_channel;
6476 if (job->jv_channel != NULL)
6477 ++job->jv_channel->ch_refcount;
6478 if (dict_add(dict, item) == FAIL)
6479 dictitem_free(item);
6480
6481#ifdef UNIX
6482 nr = job->jv_pid;
6483#else
6484 nr = job->jv_proc_info.dwProcessId;
6485#endif
6486 dict_add_number(dict, "process", nr);
6487 dict_add_string(dict, "tty_in", job->jv_tty_in);
6488 dict_add_string(dict, "tty_out", job->jv_tty_out);
6489
6490 dict_add_number(dict, "exitval", job->jv_exitval);
6491 dict_add_string(dict, "exit_cb", job->jv_exit_cb.cb_name);
6492 dict_add_string(dict, "stoponexit", job->jv_stoponexit);
6493#ifdef UNIX
6494 dict_add_string(dict, "termsig", job->jv_termsig);
6495#endif
6496#ifdef MSWIN
6497 dict_add_string(dict, "tty_type", job->jv_tty_type);
6498#endif
6499
6500 l = list_alloc();
6501 if (l != NULL)
6502 {
6503 dict_add_list(dict, "cmd", l);
6504 if (job->jv_argv != NULL)
6505 for (i = 0; job->jv_argv[i] != NULL; i++)
6506 list_append_string(l, (char_u *)job->jv_argv[i], -1);
6507 }
6508}
6509
6510/*
6511 * Implementation of job_info() to return info for all jobs.
6512 */
6513 static void
6514job_info_all(list_T *l)
6515{
6516 job_T *job;
6517 typval_T tv;
6518
6519 for (job = first_job; job != NULL; job = job->jv_next)
6520 {
6521 tv.v_type = VAR_JOB;
6522 tv.vval.v_job = job;
6523
6524 if (list_append_tv(l, &tv) != OK)
6525 return;
6526 }
6527}
6528
6529/*
6530 * "job_info()" function
6531 */
6532 void
6533f_job_info(typval_T *argvars, typval_T *rettv)
6534{
6535 if (argvars[0].v_type != VAR_UNKNOWN)
6536 {
6537 job_T *job = get_job_arg(&argvars[0]);
6538
6539 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6540 job_info(job, rettv->vval.v_dict);
6541 }
6542 else if (rettv_list_alloc(rettv) == OK)
6543 job_info_all(rettv->vval.v_list);
6544}
6545
6546/*
6547 * "job_setoptions()" function
6548 */
6549 void
6550f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6551{
6552 job_T *job = get_job_arg(&argvars[0]);
6553 jobopt_T opt;
6554
6555 if (job == NULL)
6556 return;
6557 clear_job_options(&opt);
6558 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
6559 job_set_options(job, &opt);
6560 free_job_options(&opt);
6561}
6562
6563/*
6564 * "job_start()" function
6565 */
6566 void
6567f_job_start(typval_T *argvars, typval_T *rettv)
6568{
6569 rettv->v_type = VAR_JOB;
6570 if (check_restricted() || check_secure())
6571 return;
6572 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
6573}
6574
6575/*
6576 * "job_status()" function
6577 */
6578 void
6579f_job_status(typval_T *argvars, typval_T *rettv)
6580{
6581 job_T *job = get_job_arg(&argvars[0]);
6582
6583 if (job != NULL)
6584 {
6585 rettv->v_type = VAR_STRING;
6586 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6587 }
6588}
6589
6590/*
6591 * "job_stop()" function
6592 */
6593 void
6594f_job_stop(typval_T *argvars, typval_T *rettv)
6595{
6596 job_T *job = get_job_arg(&argvars[0]);
6597
6598 if (job != NULL)
6599 rettv->vval.v_number = job_stop(job, argvars, NULL);
6600}
6601
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006602#endif /* FEAT_JOB_CHANNEL */